How to determine the height of a Text (TextExtent te_Height ?) ?

8 posts / 0 new
Last post
zzd10h
zzd10h's picture
Offline
Last seen: 6 years 4 months ago
Joined: 2012-08-24 20:56
How to determine the height of a Text (TextExtent te_Height ?) ?

Hi,
I just wonder if the te_Height returned by TextExtent() is really the size in pixels of a text displayed on a RastPort ?

Considering I have a window of 132 pixels height with a Text whose TextExtent.te_Height is 128.
If I move my text to y = 130, it should be vertically centered, no ?

In the following test, I try to vertically center a text of 128 pixels height in a 132 pixels height window (window height returned by window->Height and by SysMon).

I use a Topaz.font with ta_YSize of 128 (but the same problem occurs with all fonts)
the text height is returned by texteDimension.te_Height from TextExtent structure

  1. struct TextExtent texteDimension ;
  2. TextExtent(window->RPort,bufferHeure, strlen(bufferHeure),&texteDimension);

IMAGE(http://www.indiego.rocks/file/download/8882a292f0cda5f10c2325e1804c3602)

So if TextExtent.te_Height is not really the height of a displayed text, is it a way to find the height of a displayed font ?

Thank you by advance
Guillaume

thomas
thomas's picture
Offline
Last seen: 6 hours 25 min ago
Joined: 2011-05-16 14:23
Re: How to determine the height of a Text (TextExtent te...

I am sure that te_Height holds the right value, You just did not Move() to the right place.

The y coordinate of the text points to the base line of the font, not to the bottom of it.

The base line is the bottom of letters like a, b or c while letters like q or g draw below the base line!

RastPort->TxBaseline holds the offset of the base line you have to add to the y coordinate to get from the top to the base line.

IMHO TextExtent is a bit overkill. In most cases RastPort->TxHeight should be sufficient.

This is a small code snippet which centers a piece of text in a given window:

  1. len = strlen(text);
  2. text_w = TextLength(rp, text, len);
  3. text_h = rp->TxHeight;
  4. Move(rp, (win_w - text_w) / 2, (win_h - text_h) / 2 + rp->TxBaseline);
  5. Text (rp, text, len);
zzd10h
zzd10h's picture
Offline
Last seen: 6 years 4 months ago
Joined: 2012-08-24 20:56
Re: How to determine the height of a Text (TextExtent te...

Thank you Thomas,
you are (again) right, I didn't thought about Baseline.

As I want only to use numeric and "/" ":", by shifting your calculation by

Move(rp, (win_w - text_w) / 2, (win_h - text_h) / 2 + rp->TxBaseline + ( (text_h - rp->TxBaseline) /2) ) ;

it works well now with Topaz.font :)

In the 2 following picture, the BaseLine is represented by the horizontal blue line

IMAGE(http://www.indiego.rocks/file/download/d0dbeaf2897d5e1057a2a3640b23d004)

But not for all fonts, Nimbus Sans L Bold.font and Ledsitex St.font for example have to use the Baseline to be centered (even if 'q' letter is displayed below this baseline)

For them, I have to use your method untouched
Move(rp, (win_w - text_w) / 2, (win_h - text_h) / 2 + rp->TxBaseline);

IMAGE(http://www.indiego.rocks/file/download/bb6a9e40a3bcd710a5e14d7cde759598)

thomas
thomas's picture
Offline
Last seen: 6 hours 25 min ago
Joined: 2011-05-16 14:23
Re: How to determine the height of a Text (TextExtent te...

(win_h - text_h) / 2 + rp->TxBaseline + ( (text_h - rp->TxBaseline) /2)

Did you ever have maths at school? This is the same as (win_h - rp->TxBaseline) / 2 + rp->TxBaseline, i.e. using the base line instead of the height.

In the 2 following picture, the BaseLine is represented by the horizontal blue line

Either your first hardcopy is wrong or the font you are using is faulty.

Here is a small program which properly plots the base line for a given font into a window: http://thomas-rapp.homepage.t-online.de/examples/baseline.c

Example: IMAGE(http://thomas-rapp.homepage.t-online.de/download/Baseline.png)

zzd10h
zzd10h's picture
Offline
Last seen: 6 years 4 months ago
Joined: 2012-08-24 20:56
Re: How to determine the height of a Text (TextExtent te...

Regarding your nice comment about my school years, as I stated in my reply, I simply added something in bold to your expression to make it works for Topaz.font. For others fonts, i used yours unmodified,
So true, i didn't simplified the operation to be more clear.

My font should not be faulty, it's Topaz, but I volontary shifted the font to the bottom because I don't care about characters with descenders and I want that displayed chars are centered. Therefore the baseline is not respected.

But thx for your sample. I will take a look tomorrow.

zzd10h
zzd10h's picture
Offline
Last seen: 6 years 4 months ago
Joined: 2012-08-24 20:56
Re: How to determine the height of a Text (TextExtent te...

Launching your baseline.c with

1) struct TextAttr attr = {"topaz.font",64};

IMAGE(http://www.indiego.rocks/file/download/b46cdfbffb14c5b5bab2223d538f5bfd)

2) struct TextAttr attr = {"Nimbus Sans L Bold.font",64};

IMAGE(http://www.indiego.rocks/file/download/049fa66910526c64372ebd8207ce76cd)

Same problem than my screenshots for Topaz.font.

As I want to use only numbers chararcters and I want that there are centered, I will have to shift to bottom the Topaz Text().
BUT strangely, for Nimbus font , the numbers seems well centered even if displayed on the baseline.

thomas
thomas's picture
Offline
Last seen: 6 hours 25 min ago
Joined: 2011-05-16 14:23
Re: How to determine the height of a Text (TextExtent te...

Maybe the Nimbus font contains letters with signs above the top, for example Spanish or Czech. (French accents are only printed above lower-case letters AFAIK).

zzd10h
zzd10h's picture
Offline
Last seen: 6 years 4 months ago
Joined: 2012-08-24 20:56
Re: How to determine the height of a Text (TextExtent te...

Maybe, thank you.

Log in or register to post comments