Post Reply 
TEXTOUT_P : width ?
10-02-2015, 08:41 AM (This post was last modified: 10-02-2015 08:41 AM by primer.)
Post: #1
TEXTOUT_P : width ?
Hello,
I would like to know the width of a rendered text with TEXTOUT_P.
is there a way to evaluate it ?
Also by taking into account that TEXTOUT_P does use font size from home setting if not specified. (user guide page 588)

I'm looking for something similar than TTF_SizeText from SDL, for the ones who knows.

Thank you,
Regards.

primer
Find all posts by this user
Quote this message in a reply
10-02-2015, 09:17 AM
Post: #2
RE: TEXTOUT_P : width ?
No such function. We need to implement it ourselves.
It needed for text editor and formula editor (to edit and print formula in natural notation), since HP does not provide such functions. I also missing natural notation in notes, so custom editor is must-have. We can't expect these features in next firmware update (i expect maximum Search-Replace in editor).

So the rescue of drowning are business of drowning themselves!
Find all posts by this user
Quote this message in a reply
10-02-2015, 12:51 PM
Post: #3
RE: TEXTOUT_P : width ?
I think it can be done. I have an idea but I don't know if it will be fast enough.

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
10-03-2015, 08:56 AM (This post was last modified: 10-03-2015 09:02 AM by komame.)
Post: #4
RE: TEXTOUT_P : width ?
I created a function that calculates the width (in pixels) of the text, but it is not very useful, because it is very slow.
However, I believe that it is possible to optimize and significantly speed up. I have some ideas.

Code:
TEXT_P_W();

EXPORT TEXT_WIDTH()
BEGIN
local width; 
 PRINT();
 width:=TEXT_P_W("ABC abc 123",1);
 PRINT(width);
 width:=TEXT_P_W("ABC abc 123",3);
 PRINT(width);
 width:=TEXT_P_W("ABC abc 123",7);
 PRINT(width);
END;

TEXT_P_W(t,s)
BEGIN
 local x,y;
 DIMGROB(G9,320,40);
 TEXTOUT_P(t,G9,0,0,s);
 FOR x FROM 319 DOWNTO 0 DO
  FOR y FROM 0 TO 39 DO
   IF(GETPIX_P(G9,x,y)≠#FFFFFF) THEN
    RETURN x;
   END;
  END;
 END;
END;

function TEXT_P_W(t,s), where:
t - the text to calculate width
s - font size

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
10-03-2015, 05:42 PM
Post: #5
RE: TEXTOUT_P : width ?
(10-03-2015 08:56 AM)komame Wrote:  I created a function that calculates the width (in pixels) of the text, but it is not very useful, because it is very slow.
However, I believe that it is possible to optimize and significantly speed up. I have some ideas.

Code:
TEXT_P_W();

EXPORT TEXT_WIDTH()
BEGIN
local width; 
 PRINT();
 width:=TEXT_P_W("ABC abc 123",1);
 PRINT(width);
 width:=TEXT_P_W("ABC abc 123",3);
 PRINT(width);
 width:=TEXT_P_W("ABC abc 123",7);
 PRINT(width);
END;

TEXT_P_W(t,s)
BEGIN
 local x,y;
 DIMGROB(G9,320,40);
 TEXTOUT_P(t,G9,0,0,s);
 FOR x FROM 319 DOWNTO 0 DO
  FOR y FROM 0 TO 39 DO
   IF(GETPIX_P(G9,x,y)≠#FFFFFF) THEN
    RETURN x;
   END;
  END;
 END;
END;

function TEXT_P_W(t,s), where:
t - the text to calculate width
s - font size

Having fixed fonts maybe is faster just having an array with the known char widths per font size

My website: erwin.ried.cl
Visit this user's website Find all posts by this user
Quote this message in a reply
10-03-2015, 07:45 PM
Post: #6
RE: TEXTOUT_P : width ?
This is the fastest way I found to calculacte the width and height of the text.

The following functions should take less than 2ms.

Width:
Code:
TEXTW_P(t,s)
BEGIN
 local x,a=0,b=511,m;
 DIMGROB_P(G9,512,1);
 TEXTOUT_P(t,G9,0,0,s,0,511,0);
 FOR x FROM 1 TO 10 DO
   IP((a+b)/2)▶m;
   IFTE(GETPIX_P(G9,m,0)=#FFFFFFh,m▶b,m▶a);
 END;
 RETURN m;
END;

Height:
Code:
TEXTH_P(t,s)
BEGIN
 local x,a=0,b=31,m;
 DIMGROB_P(G9,1,32);
 TEXTOUT_P(t,G9,0,0,s,0,1,0);
 FOR x FROM 1 TO 6 DO
   IP((a+b)/2)▶m;
   IFTE(GETPIX_P(G9,0,m)=#FFFFFFh,m▶b,m▶a);
 END;
 RETURN m;
END;

as before:
t - text for check the width or height
s - font size (0 = default from Home settings)

During the performance tests when I created this solution I think I detected a bug (very little) in the function TEXTOUT_P, but I will write about this in a separate thread.

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
10-04-2015, 04:21 PM
Post: #7
RE: TEXTOUT_P : width ?
After minor changes I managed to get a little faster version.
The time required for the calculation is ~1.5ms for width and ~0.9ms for height.

Width v2:
Code:
TEXT_P_W(t,s)
BEGIN
 local x,a,b=511,m=255;
 DIMGROB_P(G9,512,1);
 TEXTOUT_P(t,G9,0,0,s,0,511,0);
 FOR x FROM 1 TO 9 DO
  IFTE(GETPIX_P(G9,m,0)=#FFFFFFh,m▶b,m▶a);
  IP((a+b)/2)▶m;
 END;
 RETURN m;
END;

Height v2:
Code:
TEXT_P_H(t,s)
BEGIN
 local x,a=0,b=31,m=15;
 DIMGROB_P(G9,1,32);
 TEXTOUT_P(t,G9,0,0,s,0,1,0);
 FOR x FROM 1 TO 5 DO
   IFTE(GETPIX_P(G9,0,m)=#FFFFFFh,m▶b,m▶a);
   IP((a+b)/2)▶m;
 END;
 RETURN m;
END;

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
10-05-2015, 03:04 PM
Post: #8
RE: TEXTOUT_P : width ?
Thank you very much komame, it's really appreciated.
I was afraid about performance, but that's OK.

best regards,

primer
Find all posts by this user
Quote this message in a reply
10-12-2015, 11:58 AM (This post was last modified: 10-12-2015 12:00 PM by Tyann.)
Post: #9
RE: TEXTOUT_P : width ?
Bonjour
Ces deux fonctions sont très utiles, bravo et merci
Si je peut me permettre une suggestion.
Ici vous utilisez G9, et cela peut engendrer un conflit, si
le programmeur n'y pense pas.
Il est possible de mettre en paramètres la variable Gx à utiliser
ainsi le programmeur qui utilisera vos fonctions choisira lui même.

Qu'en pensez-vous ?

Hello
These two functions are very useful, congratulations and thank you
If I may make a suggestion .
Here you use G9, and this may create a conflict if
the programmer does not think .
You can set parameters to use the variable Gx
and the programmer who will use your duties will choose itself.

What do you think ?

Code:

EXPORT TEXTW_P(t,g,s)
BEGIN
 LOCAL x,a,b:=511,m;
 DIMGROB(g,512,1);
 TEXTOUT_P(t,g,0,0,s,0,511,0);
 FOR x FROM 1 TO 10 DO
  m:=IP((a+b)/2);
  IFTE(GETPIX_P(g,m,0)==#FFFFFFh,m▶b,m▶a);
 END;
 m;
END;

Sorry for my english
Find all posts by this user
Quote this message in a reply
10-14-2015, 10:02 PM
Post: #10
RE: TEXTOUT_P : width ?
(10-12-2015 11:58 AM)Tyann Wrote:  What do you think ?
I think it could be a builtin command.
and as a bonus, you even don't need to choose a graphic var.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)