#1 2012-01-27 14:50:35

Gigo
Member
From: Split, Croatia
Registered: 2012-01-27
Posts: 14

Non-ANSI Fonts

SynPDF 1.15

I'm trying to write some text using EASTEUROPE_CHARSET (which is defaut on my computer). Output PDF renders fine in Acrobat reader, but Ghostscript has problems with CIDFontType2 font substitution when using non-ANSI chars.
Same problem on both TPdfDocument and TPdfDocumentGDI component. I'm using Delphi7. If I set EmbeddedTTF or PDF1A to true there's no problem, fonts are embedded fine.


example 1:

procedure TForm1.TestPDFDoc;
var
  PDF : TPdfDocument;
begin
  PDF := TPdfDocument.Create;
  try
    PDF.DefaultPaperSize := psA4;
    PDF.AddPage;
    PDF.Canvas.SetFont('Arial',30, []);     // default CP is 1250
    PDF.Canvas.TextOut(100,100, #$8A#$D0#$C8#$C6#$8E);
    PDF.SaveToFile('Test.PDF');              // GS will render first char only
  finally
    PDF.Free;
  end;
end;

example 2:

procedure TForm1.TestPDFDocGDI;
var
  PDF : TPdfDocumentGDI;
begin
  PDF := TPdfDocumentGDI.Create;
  try
    PDF.DefaultPaperSize := psA4;
    PDF.AddPage;
    PDF.VCLCanvas.Font.Name := 'Arial';
    PDF.VCLCanvas.Font.Size := 30;
    PDF.VCLCanvas.Font.Charset := EASTEUROPE_CHARSET;  // same as DEFAULT_CHARSET on my computer
    PDF.VCLCanvas.TextOut(100,100,#$8A#$D0#$C8#$C6#$8E);
    PDF.SaveToFile('TestGDI.PDF');                                    // GS will render first char only
  finally
    PDF.Free;
  end;
end;

For a quick fix I modified two procedures in SynPDF.pas to force embedding font when non-ANSI chars used :

// fixed TPdfDocument issue

procedure TPdfCanvas.ShowText(const text: PDFString; NextLine: boolean);
begin
  if (FContents<>nil) and (text<>'') then
    if (fDoc.FCharSet=ANSI_CHARSET) or IsAnsiCompatible(PAnsiChar(pointer(text))) then begin
      if FPage.Font.Unicode and (FPage.FFont.FTrueTypeFontsIndex<>0) then
        SetPDFFont(TPdfFontTrueType(FPage.Font).WinAnsiFont,FPage.FontSize);
      FContents.Writer.Add('(').AddEscapeText(pointer(text),FPage.Font).Add(')').
        Add(SHOWTEXTCMD[NextLine])
    end else begin
      // gigo
      Doc.EmbeddedTTF := true;
      if FPage.FFont.FTrueTypeFontsIndex<>0 then
        // write TrueType text after conversion to unicode
        FContents.Writer.AddToUnicodeHexText(text,NextLine,self) else
        // this standard font should expect MBCS encoding
        FContents.Writer.Add('<').AddHex(text).Add('>').Add(SHOWTEXTCMD[NextLine]);
    end;
end;

// fixed TPdfDocumentGDI issue

procedure TPdfEnum.TextOut(var R: TEMRExtTextOut);
...
at line 7742
...
begin
  with DC[nDC] do begin
    SetLength(tmp,R.emrtext.nChars+1); // faster than WideString for our purpose
    move(pointer(PtrUInt(@R)+R.emrtext.offString)^,tmp[0],R.emrtext.nChars*2);

    // gigo
    if not IsAnsiCompatible(PWideChar(tmp)) then
      Canvas.Doc.EmbeddedTTF := true;

    // guess the font size
    if font.LogFont.lfHeight<0 then
      ASize := -font.LogFont.lfHeight*Canvas.fFactorY else
      ASize := font.spec.cell*Canvas.fFactorY;
    ...

The question is : Am I on right track here, or should I just avoid Ghostscript for rendering ? If this is SynPDF issue, Id like to see these (or proper) changes in future SynPDF versions.

Offline

#2 2012-01-28 09:14:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,243
Website

Re: Non-ANSI Fonts

I guess this is an issue, but mostly to on the Ghoscript part.
Output PDF renders fine in Acrobat reader, as you stated.

The easier fix is not to change the SynPdf source code here, but just change your general use code by setting EmbeddedTTF := true:

procedure TForm1.TestPDFDoc;
var
  PDF : TPdfDocument;
begin
  PDF := TPdfDocument.Create;
  try
    PDF.DefaultPaperSize := psA4;
    PDF.EmbeddedTTF := true; // this will fix the Ghostscript issue here
    PDF.AddPage;
    PDF.Canvas.SetFont('Arial',30, []);     // default CP is 1250
    PDF.Canvas.TextOut(100,100, #$8A#$D0#$C8#$C6#$8E);
    PDF.SaveToFile('Test.PDF');              // GS will render first char only
  finally
    PDF.Free;
  end;
end;

Offline

Board footer

Powered by FluxBB