Recent

Author Topic: autosize not working correctly on TLabel  (Read 2318 times)

robert rozee

  • New Member
  • *
  • Posts: 34
autosize not working correctly on TLabel
« on: July 23, 2021, 09:30:13 pm »
hi,
    i've found an 'interesting' behaviour, but am not sure if it is a bug in Lazarus/FPC, or a bug in the Linux distro i'm running - Linux Mint 20.2 with the XFCE desktop. i do have a workaround, but would be interested in hearing if others have encountered the same problem and what (if anything) should be done about it. the version of Lazarus i'm using is 2.0.12, while the FPC is 3.2.0.

the problem involves a TLabel that has AutoSize set True (ie, the default). when compiled and run on a 64-bit Linux Mint 20.2 machine, the TLabel is exactly 2 pixels shorter than it should be, with the descenders of letters chopped off. the exact same code/executable when run on a 64-bit Linux Mint 19.3 machine has the TLabel auto-sizing itself to the correct height, without any clipping of the descenders. i've tried recompiling for 32-bit Linux Mint, and again the auto-sizing is working correctly on a 32-bit Linux Mint 19.3 VM, but not on the 20.2 machine. i've tried setting the TLabel to various different fonts, and various font sizes; under Linux Mint 20.2 the TLabel is always 2 pixels too short. i've also spotted the errant behavior with code compiled a year ago with an earlier version of Lazarus/FPC when the old executable is run on the 20.2 machine.

the workaround, as demonstrated in the attached example, is to set AutoSize to False, and then resize the TLabel thus:

Code: Pascal  [Select][+][-]
  1. Label1.AutoSize:=false;
  2. Label1.Height:=Label1.Canvas.TextHeight(Label1.Caption);
  3. Label1.Width:=Label1.Canvas.TextWidth(Label1.Caption)

that the above method works, suggests Lazarus/FPC is (either wrongly or rightly) getting the AutoSize height from somewhere other than Canvas.TextHeight. however, if autosizing were handled entirely by Linux/XFCE, then the problem would appear to be there. it is telling that the problem is revealed by an upgrade of the operating system - but then it could be that Linux Mint 20.2 'fixes' a bug in earlier versions of itself that Lazarus/FPC relies upon!

and to answer the obvious question: yes, the 2-pixel-short TLabel was causing a major problem in a rather large program i've been working on for several years. i was relying upon a TLabel correctly autosizing to fit a single character, in order to calculate a whole load of dimensions for a serial terminal emulator. the TLabel in this case acts as the terminal's cursor.


would appreciate it if others could compile and test the example program and report back. the TLabel dimensions are displayed, and the two buttons switch between AutoSize on and off (with resizing then from Canvas.TextHeight and Canvas.TextWidth). under Linux Mint 19.3 the buttons produce no change, while with 20.2 you can see the label height change.


cheers,
rob   :-)
« Last Edit: July 23, 2021, 09:31:53 pm by robert rozee »

winni

  • Hero Member
  • *****
  • Posts: 2666
Re: autosize not working correctly on TLabel
« Reply #1 on: July 23, 2021, 09:40:47 pm »

robert rozee

  • New Member
  • *
  • Posts: 34
Re: autosize not working correctly on TLabel
« Reply #2 on: July 24, 2021, 03:39:14 pm »
aha, it does look like others have faced the same problems as me, and arrived at much the same workaround.

out of curiosity, why doesn't Lazarus/FPC just make use of TLabel.Canvas.TextHeight and TLabel.Canvas.TextWidth to get the new height and width of a TLabel whenever the Caption is assigned to, or whenever the font properties are changed? this seems a far simpler approach than carrying out calculations (that don't always work) based on the font data.

it occurs to me that a possible reason why i am seeing a difference in behavior between 19.3 and 20.2 versions of Linux Mint is that 'type 1' font support (according to some sources) has been removed in versions 20 and onwards. this may imply that the fonts supplied with 20.2 have been converted, with some loss of details along the way.


cheers,
rob   :-)


wp

  • Hero Member
  • *****
  • Posts: 8749
Re: autosize not working correctly on TLabel
« Reply #3 on: July 24, 2021, 04:08:11 pm »
the version of Lazarus i'm using is 2.0.12, while the FPC is 3.2.0.
On Ubuntu 20.10 I could see the reported issue with Laz 2.0.10, but I cannot see it with Laz trunk - I do remember that AutoSize calculation has been re-done not so long ago. Probably the new code is used in Laz 2.2 - please try the currently available release candidate.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018