program TextMeasure;
{$I ..\..\..\Lib\PDFlibDefines.inc}
{$APPTYPE CONSOLE}
{$R *.res}

uses
{$IFDEF XE2+} System.SysUtils,
{$ELSE} SysUtils,
{$ENDIF} PDFlibrary;

var
  PDF: TPDFlib;
  s: string;
  x, y, tf, ta, td, tb, ts, tw: double;

begin
  PDF := TPDFlib.Create;
  PDF.SetMeasurementUnits(1); // millimeters
  PDF.SetPageDimensions(297, 210); // A4
  PDF.SetOrigin(0); // bottom left
  x := 8;
  y := 150;
  s := 'Ärger Cäsar Übermut ÉË áÃ üç ÎÑ';

  PDF.AddStandardFont(11);
  tf := 18 * 72 / 25.4; // 18 mm as points
  PDF.SetTextSize(tf);
  ta := PDF.GetTextAscent; // get all we can need
  td := PDF.GetTextDescent;
  tb := PDF.GetTextBound(2); // get the top bound
  ts := PDF.GetTextSize * 25.4 / 72; // points to mm
  tw := PDF.GetTextWidth(s);
  PDF.DrawText(x, y, s);

  tf := 4 * 72 / 25.4; // 4 mm as points
  PDF.SetTextSize(tf);

  PDF.SetLineWidth(0.1);
  PDF.SetLineColor(1, 0, 0); // Baseline
  PDF.DrawLine(x - 2, y, x + tw + 4, y);
  PDF.DrawText(x + tw + 6, y, 'Baseline');
  PDF.SetLineColor(0, 1, 0); // Ascent
  PDF.DrawLine(x - 2, y + ta, x + tw + 4, y + ta);
  PDF.DrawText(x + tw + 6, y + ta, 'Ascent');
  PDF.SetLineColor(0, 0, 1); // Descent = abs(Bottom)
  PDF.DrawLine(x - 2, y + td, x + tw + 4, y + td);
  PDF.DrawText(x + tw + 6, y + td, 'Descent');
  PDF.SetLineColor(0, 0.5, 0.5); // Top
  PDF.DrawLine(x - 2, y + tb, x + tw + 4, y + tb);
  PDF.DrawText(x + tw + 6, y + tb, 'Top');

  PDF.AddStandardFont(4);
  PDF.SetTextSize(tf);
  PDF.DrawText(100, y - 10, Format('Ascent=%.2f Descent=%.2f Top=%.2f Textsize=%.2f Delta=%.2f', [ta, td, tb, ts, tb - ta]));

  PDF.SetOrigin(1);
  tf := 20 * 72 / 25.4; // 20 mm as points
  PDF.AddStandardFont(11);
  PDF.SetTextSize(tf);
  PDF.DrawLine(5, 110, 287, 110);
  PDF.DrawText(25, 110 + PDF.GetTextAscent(), 'by text ascent');
  PDF.DrawText(150, 110 + PDF.GetTextHeight(), 'by text height');

  PDF.SaveToFile('TextMeasure.pdf')

end.
