Recent

Author Topic: Get Text Width using "fcl-pdf" Unit  (Read 1659 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Get Text Width using "fcl-pdf" Unit
« on: August 03, 2022, 11:17:41 am »
Hello,

i am trying to get textwidth of my String and then convert it from pixel to mm

Here is my Code
Code: Pascal  [Select][+][-]
  1. procedure TVGraph.Command2Click(Sender: TObject);
  2. Var
  3.    P: TPDFPage;
  4.    Doc: TPDFDocument;
  5.    Section: TPDFSection;
  6.    IDLogo: Integer;
  7.    Pos1, Pos2: TPDFCoord;
  8.    HInMM, WInMM, PosTemp: TPDFFloat;
  9.    Tx: String;
  10.    lFC: TFPFontCacheItem;
  11. Begin
  12.    { ----- Document erstellen ----- }
  13.    Doc:= TPDFDocument.Create(Nil);
  14.    Doc.Infos.Title:= Application.Title;
  15.    //Doc.Infos.Author := '';
  16.    Doc.Infos.Producer:= 'test';
  17.    Doc.Infos.CreationDate:= Now;
  18.    Doc.StartDocument;
  19.    Section:= Doc.Sections.AddSection; //always needed
  20.  
  21.    { ----- Seite erstellen ----- }
  22.    P:= Doc.Pages.AddPage;
  23.    P.PaperType:= ptA4;
  24.    P.UnitOfMeasure:= uomMillimeters;
  25.    Section.AddPage(P); //add to section
  26.  
  27.    { ----- Seitenkopf ----- }
  28.    Doc.AddFont('Helvetica');
  29.    P.SetFont(Doc.Fonts.FindFont('Helvetica'), fFontSize);
  30.  
  31.    Doc.FontDirectory:= ExtractFilePath(ParamStr(0))+'fonts';
  32.    gTTFontCache.SearchPath.Add(Doc.FontDirectory);
  33.    gTTFontCache.BuildFontCache;
  34.  
  35.    lFC:= gTTFontCache.Find('Helvetica', False, False);
  36.  
  37.    //Darwing some Lines and Pictures here, works fine
  38.  
  39.    { -- Build up some String--}
  40.    Tx:= 'I wanna know my Length';
  41.    //get length in mm
  42.    PosTemp:= PDFTomm(lFC.TextWidth(Tx, fFontSize)); //Access Violation (Font Size is 12)
  43.    //check if Length was correctly calculated
  44.    P.WriteText(10, 20, Tx);
  45.    P.WriteText(10 + PosTemp, 40, Tx);
  46.   //save
  47.   if FileExists('C:\Users\Admin\Desktop\TEST.pdf') then DeleteFile('C:\Users\Admin\Desktop\TEST.pdf');
  48.   D.SaveToFile('C:\Users\Admin\Desktop\TEST.pdf');
  49. end;
  50.  

when i run the Application i get Access Violation in this Line:

Quote
   PosTemp:= PDFTomm(lFC.TextWidth(Tx, fFontSize)); //font size is 12

i am not realy sure if i did initalize this the correct way:

Code: Pascal  [Select][+][-]
  1.    Doc.FontDirectory:= ExtractFilePath(ParamStr(0))+'fonts';
  2.    gTTFontCache.SearchPath.Add(Doc.FontDirectory);
  3.    gTTFontCache.BuildFontCache;
  4.  

i tried to follow this Tutorial: https://www.freepascal.org/~michael/articles/lazpdf/lazpdf.pdf
and the tutorial in :  fcl-pdf/examples/testfppdf.lpr

Can someone help me out ?

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: Get Text Width using "fcl-pdf" Unit
« Reply #1 on: August 03, 2022, 11:57:16 am »
I'm not that familiar with fpc-pdf but...

You might begin with building in some error-checking.
Code: Pascal  [Select][+][-]
  1. lFC:= gTTFontCache.Find('Helvetica', False, False);
  2. if not Assigned(lFC) then raise EReportFontNotFound.CreateFmt(SErrFontNotFound, ['Helvetica']);
In that case it throws a sane error.

Next... list all fonts to see if your font is there:
Code: Pascal  [Select][+][-]
  1.    for i := 0 to gTTFontCache.Count -1 do
  2.       Showmessage(gTTFontCache.Items[i].FamilyName);
If it is not, you can go from there.

Doing a gTTFontCache.ReadStandardFonts; will read in all Windows fonts (assuming you are on Windows).

But Helvetica isn't one of the standard Windows fonts.
You do a Doc.AddFont('Helvetica');. Where is that font located???
Check if Fonts.FindFont('Helvetica'); returns a correct index. (Look at the source for TPDFDocument.AddFont, it does the same).

My guess is that the font is not added correctly.
(if you are under Windows try to use one of the standard fonts first)

BTW, the last line is D.SaveToFile. Where does D come from ???


Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: Get Text Width using "fcl-pdf" Unit
« Reply #2 on: August 03, 2022, 12:47:42 pm »
D should be DOC, but that is not the problem. (was just trying to show the problem i had and this is a result of Strg C / strg V  :-[)

i get the error since adding this line:   
PosTemp:= PDFTomm(lFC.TextWidth(Tx, fFontSize));

and working with TFPFontCacheItem.

in the Examples they said that i should use one of 14 Adobe PDF standard fonts:
Quote
The standard 14 PDF fonts – that can be referenced by name in a PDF document – are:

Times-Roman
Times-Bold
Time-Italic
Time-BoldItalic
Courier
Courier
Courier-Bold
Courier-Oblique
Helvetica
Helvetica-Bold
Helvetica-Oblique
Helvetica-BoldOblique
Symbol
ZapfDingbats
so i don't think The font is creating problems, but i will check it.

if i can find a Solution i will post it here. :)
« Last Edit: August 03, 2022, 12:51:01 pm by Weitentaaal »

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: Get Text Width using "fcl-pdf" Unit
« Reply #3 on: August 03, 2022, 01:28:29 pm »
You were right i did mistake the path so it could not find the Font.  :-[

Thank you !

 

TinyPortal © 2005-2018