[Lazarus] PDF generator: please test

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Sun Apr 10 01:02:01 CEST 2016


On 2016-04-09 09:09, Ondrej Pokorny wrote:
>    P.WriteText(25, 0, 'Sample Text'); // DOESN'T WORK !!!

Indeed. As I mentioned, the Write*Text() names are a bit inaccurate and
will be improved. At the moment WriteText() is meant for the Standard
PDF Fonts, and WriteUTF8Text() for any TTF fonts used.


>    P.WriteUTF8Text(25, 20, 'Sample Text');
> 
>    xFontCache := TFPFontCacheList.Create;
>    try
>      xFont := TFPFontCacheItem.Create('fonts\FreeSans.ttf');
>      xFontCache.Add(xFont);
>      xWidth := xFont.TextWidth('Sample Text', cFontSize) * 25.4 / 72; // 
> 25.4 / 72 = conversion PDFTomm (?)

I believe here is your mistake. Your system is probably 96 DPI, not 72.
If you use gTTFontCache.DPI it would have given you the correct result.
At the moment the FontCache object defaults to the most common desktop
DPI, which is 96, but in your actual application you can query it with
whatever GUI you have and set the value yourself.


Here is verbosely commented code that draws a rectangle around the Page
Title text. Screenshot of the output is attached.

==========================
  { draw a rectangle around the Page Title text }
  lPt1.X := 25;  // units in MM
  lPt1.Y := 20;  // units in MM
  lFntPtSize := 23;

  { a rough calculation but not 100% accurate for a specific font. }
//  lTextHeightInMM := ((gTTFontCache.PointSizeInPixels(lFntPtSize) *
25.4) / 72) / 2;

  lFC := gTTFontCache.Find('FreeSans', False, False);
  if not Assigned(lFC) then
    raise Exception.Create('FreeSans font not found');
  lFntInfo := lFC.GetFontData;

  { result is in pixels }
  lHeight := lFntInfo.CapHeight * lFntPtSize * gTTFontCache.DPI / (72 *
lFntInfo.Head.UnitsPerEm);
  { convert pixels to mm as our PDFPage.UnitOfMeasure is set to mm. }
  lTextHeightInMM :=  (lHeight * 25.4) / 72;

  lWidth := lFC.TextWidth('Sample Text', lFntPtSize);
  { convert the Font Units to Millimeters }
  lTextWidthInMM := (lWidth * 25.4) / gTTFontCache.DPI;

  { result is in pixels }
  lHeight := Abs(lFntInfo.Descender) * lFntPtSize * gTTFontCache.DPI /
(72 * lFntInfo.Head.UnitsPerEm);
  { convert pixels to mm as you PDFPage.UnitOfMeasure is set to mm. }
  lDescenderHeightInMM :=  (lHeight * 25.4) / 72;

  { adjust the Y coordinate for the font Descender, because
WriteUTF8Text draws on the baseline. }
  lPt1.Y := lPt1.Y + lDescenderHeightInMM;

  P.SetColor(clRed, true);
  P.DrawRect(lPt1.X, lPt1.Y, lTextWidthInMM, lTextHeightInMM, 1, false,
true);
  lFntInfo.Free;
==========================



Regards,
  - Graeme -

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

My public PGP key:  http://tinyurl.com/graeme-pgp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot.png
Type: image/png
Size: 10204 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160410/f422988d/attachment-0003.png>


More information about the Lazarus mailing list