Recent

Author Topic: EasyLazFreeType - what does TextWidth() return?  (Read 4598 times)

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
EasyLazFreeType - what does TextWidth() return?
« on: January 21, 2016, 04:43:37 pm »
I somehow need to find out the width of a text string. I can't link to LCL or generate a FPCanvas bitmap. I only have access to the TTF files.

In the LazUtils there is a easylazfreetype.pas unit, and I'm trying to figure out the works of the TFreeTypeFont.TextWidth() method.

Using the following program I get two results of 8 and 23  - but what do those values represent? Points, Pixels?

If the result is in pixels, where do you specify the Font Size? I never specified a font point size in my example code, and obvious different font sizes will produce different pixel sizes for text width.

Code: Pascal  [Select]
  1. program project1;
  2.  
  3. {$mode objfpc}{$h+}
  4.  
  5. uses
  6.   Classes,
  7.   SysUtils,
  8.   EasyLazFreeType,
  9.   LazFreeTypeFontCollection;
  10.  
  11. var
  12.   s: string;
  13.   fc: TFreeTypeFontCollection;
  14.   lTTF: TFreeTypeFont;
  15. begin
  16.   s := 'abc';
  17.  
  18.   fc := TFreeTypeFontCollection.Create;
  19.   if not fc.AddFile('/data/devel/Wisa/fpcpackages/fppdf/tests/fonts/DejaVuSans.ttf') then
  20.     writeln('Failed to load font');
  21.  
  22.   lTTF := fc.Family['DejaVu Sans'].Font[0].CreateFont;
  23.   // returns 8
  24.   writeln(Trunc(lTTF.TextWidth('$')));
  25.   // returns 23
  26.   writeln(Trunc(lTTF.TextWidth('abc')));
  27.  
  28.  
  29.   lTTF.Free;
  30.   fc.Free;
  31. end.
  32.  

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

FTurtle

  • Sr. Member
  • ****
  • Posts: 261
Re: EasyLazFreeType - what does TextWidth() return?
« Reply #1 on: January 21, 2016, 05:21:48 pm »
If the result is in pixels, where do you specify the Font Size? I never specified a font point size in my example code, and obvious different font sizes will produce different pixel sizes for text width.

I think should be some default value.
Why not specify your own value?

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: EasyLazFreeType - what does TextWidth() return?
« Reply #2 on: January 22, 2016, 10:24:03 am »
Thanks FTurtle. Only afterwards I delved into the code and noticed that the constructor sets a default PointSize of 10, and that there is an actually PointSize and PixelSize property.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/