[Lazarus] PDF generator: please test

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


On 2016-04-10 00:02, Graeme Geldenhuys wrote:
> Here is verbosely commented code that draws a rectangle around the Page
> Title text.

Apologies, here is the correct code.

=================================
  { Page title }
  P.SetFont(FtText1, 23);
  P.SetColor(clBlack, false);
  P.WriteUTF8Text(25, 20, 'Sample Text');

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

  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) / gTTFontCache.DPI;

  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) / gTTFontCache.DPI;

  { adjust the Y coordinate for the font Descender, because
    WriteUTF8Text draws on the baseline. Also adjust the TextHeight
    because CapHeight doesn't take into account the Descender. }
  P.SetColor(clRed, true);
  P.DrawRect(lPt1.X, lPt1.Y+lDescenderHeightInMM, lTextWidthInMM,
      lTextHeightInMM+lDescenderHeightInMM, 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




More information about the Lazarus mailing list