problem with GetTextHeight?

Started by HandsFree, Wed 08/04/2020 20:27:08

Previous topic - Next topic

HandsFree

Hi,

I see there are several posts about this but I still don't understand what's happening with the GetTextHeight function.
I'm trying to see if a certain string will fit in the bouderies of a label.

Code: ags

  int height = GetTextHeight(s, eFonttext, lblText.Width);
  Display ("The height is %d.", height);
  if (height <= lblText.Height) lblText.Text = s;

It doesn't work however. The display shows a number that is indeed lower than the height of the label but the text is actually too big and isn't shown completely.
Somewhere it is suggested to use width + 1, but that doesn't make a difference.

Is this bit from the helptext relevant?
The height is returned in normal 320-resolution pixels...

How can I make this work?
thanks

Crimson Wizard

#1
Firstly, yes, you should use (field width + 1), because of the bug in AGS which was kept for a while for compatibility reasons (this may be changed in some major future version though).

Secondly, GUI elements in AGS sometimes add their own padding and spacing rules, which is not properly documented anywhere. This information may be only discovered by analyzing engine code.

For example, from what I wrote down for myself, Labels add 1 extra pixel to font's line spacing, which means that label have to be larger by 1 * (number of lines - 1) pixels to accomodate the text.

UPD
Coincidentally, I have got an example of code from my recent MAGS game where I also was resizing a label :):
Code: ags

    int texth = GetTextHeight(info, font, textw); // textw == wanted label width
    int fonth = GetFontHeight(font);
    int lines = texth / fonth;
    // Accomodate Label's adding 1 to font's line spacing
    texth += (lines - 1);
    // apply textw and texth to the label after this


NOTE: GetFontHeight() returns height of a single line in that font.
Also... lol, but why don't I use textw + 1 myself...



Quote from: HandsFree on Wed 08/04/2020 20:27:08
Is this bit from the helptext relevant?
The height is returned in normal 320-resolution pixels...

No, it's safe to ignore, this is related to the way very old versions of AGS worked. Manual still contains outdated bits like this.

HandsFree

Experimenting a bit I found that the label can hold a maximum of 20 lines, which according to GetTextHeight is 320.
The height of the label is 442. The difference is more than one pixel per line.

Of course knowing this I can now do
Code: ags

  int height = GetTextHeight(s, eFonttext, lblText.Width + 1);
  if (height <= 320) lblText.Text = s;

But is that the best way?

Crimson Wizard

#3
Quote from: HandsFree on Wed 08/04/2020 21:32:51
Experimenting a bit I found that the label can hold a maximum of 20 lines,

Oh. Which version of AGS are you using? 3.5.0 should not have any limit. Don't remember exact version when it was removed though.

Quote from: HandsFree on Wed 08/04/2020 21:32:51
The height of the label is 442. The difference is more than one pixel per line.

I don't quite understand the situation. Is text longer than 20 lines? or do these 20 lines not fit in the label?
Have you tried using the code I posted above?

HandsFree

I meant that I noticed that the 21st line in this label is not displayed completely.
When the text is 20 lines, GetTextHeight gives 320. The height of the label is 442.

I'll try the code you posted.

Crimson Wizard

#5
Quote from: HandsFree on Wed 08/04/2020 21:52:22
I meant that I noticed that the 21st line in this label is not displayed completely.
When the text is 20 lines, GetTextHeight gives 320. The height of the label is 442.

Hmmm, this is weird. Can you double check that you use same font in calculations as set in the Label?
You could use lblText.Font in your math to make sure it always uses one from the label.


EDIT: Another thought, is the font outlined? I do not remember if GetTextHeight accounts for an outline. Probably not.

HandsFree

I used lblText.Font (indeed better anyway) but no difference. The font is not outlined as far as I know (how can I verify that?).
When I check GetFontHeight it gives 16, which corresponds to GetTextHeight being 320 with 20 lines.
But still 21 lines don't fit in the label with a height of 442.

HandsFree

Oops sorry, found the problem.
The height of the gui that holds the label is less than 442. Don't know how that happened.
Thanks for the help.

SMF spam blocked by CleanTalk