#1 2021-02-01 08:09:34

Alalaki
Member
Registered: 2021-02-01
Posts: 4

Chinese Font bold style does not work, when EmbeddedTTF is true

Hello


As stated in the code, I set the font to bold, but it doesn't work. If I comment out the code "lPDF.EmbeddedTTF:= True;", it does work. But I do need to be bold. Here, I sincerely hope to get your help.

{ Delphi 2010;
synpdf version 1.18; }

procedure TForm1.Button3Click(Sender: TObject);
var
  lPdf: TPdfDocumentGDI;
begin
  lPdf := TPdfDocumentGDI.Create;
  try
    lPdf.ScreenLogPixels := 96;
    lPdf.DefaultPaperSize := TPDFPaperSize.psA4;
    lPDF.EmbeddedTTF := True;
    lPDF.AddPage;

    lPdf.VCLCanvas.Brush.Style := bsClear;
    lPdf.VCLCanvas.Font.Size := 32;
    lPdf.VCLCanvas.Font.Name := '宋体';
    //lPdf.VCLCanvas.TextOut(20, 60, '中文宋体');
    lPdf.VCLCanvas.Font.Style := [fsBold];
    lPdf.VCLCanvas.TextOut(20, 110, '中文宋体');

    lPdf.SaveToFile('C:\test\ttest.pdf');
  finally
    lPdf.Free;
  end;
end;

Thanks very much.

Last edited by Alalaki (2021-02-01 09:13:22)

Offline

#2 2021-02-01 13:51:28

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

Re: Chinese Font bold style does not work, when EmbeddedTTF is true

I suspect it works on non-Chinese fonts, right?

Could you debug a little more, and try to find out why the main font, not the bold font is referred and embedded?

Offline

#3 2021-02-01 20:58:36

5c4f394a
Member
Registered: 2013-03-22
Posts: 11

Re: Chinese Font bold style does not work, when EmbeddedTTF is true

(D7, mORMot 1.18.6222)
--
https://paste.ee/p/12VCO
--
orjUkBu.png
--
GZGkDrs.png
--
ctsfJXE.png
--
APTIrt5.png
--

Offline

#4 2021-02-02 07:03:27

Alalaki
Member
Registered: 2021-02-01
Posts: 4

Re: Chinese Font bold style does not work, when EmbeddedTTF is true

Sorry, I can't see your photo, and the site is unavailable. Can you describe it in words? Thank you.(Please forgive me for reporting your post by mistake. I just want to reply to you, but the first time I use this forum and my English is not good.)

Last edited by Alalaki (2021-02-02 07:09:54)

Offline

#5 2021-02-02 08:16:52

Alalaki
Member
Registered: 2021-02-01
Posts: 4

Re: Chinese Font bold style does not work, when EmbeddedTTF is true

ab wrote:

I suspect it works on non-Chinese fonts, right?

Could you debug a little more, and try to find out why the main font, not the bold font is referred and embedded?

Yes, the bold style works on non-Chinese fonts. I heard that Song Ti(SimSun) does not have the original bold font, but I find this in the generated file :

……
6 0 obj
<</Type/Font/BaseFont/SimSun,Bold/Subtype/TrueType/Encoding/WinAnsiEncoding/FontDescriptor 7 0 R/Name/F0>>
endobj
7 0 obj
<</Type/FontDescriptor/FontName/SimSun,Bold/Ascent 859/CapHeight 666/Descent -141/ItalicAngle 0/StemV 105/StemH 45/FontWeight 700/Flags 262178/FontBBox[-8 -141 1000 859]/FontFile2 9 0 R>>
endobj
8 0 obj
<</Type/Font/BaseFont/SimSun,Bold/Subtype/Type0/Encoding/Identity-H/Name/F1/DescendantFonts[10 0 R]/ToUnicode 11 0 R>>
endobj
……

It seems that the bold style has been selected in the file, but the bold style does not take effect.
By the way, my computers don't have a font named 'SimSun,Bold'. Even so, if I don't embed the font, I can still see the bold style in the generated file.
I tracked the PrepareForSaving function, but this function uses the WinApi function to embed fonts. I have no way of knowing whether it has embedded bold formatting.

{synpdf.pas version 1.18; }
……
procedure TPdfFontTrueType.PrepareForSaving;
var
  ……
begin
      ……
           if CreateFontPackage(pointer(ttf),ttfSize,
                    SubSetData,SubSetMem,SubSetSize,
                    usFlags,ttcIndex,TTFMFP_SUBSET,0,
                    TTFCFP_MS_PLATFORMID,TTFCFP_UNICODE_CHAR_SET,
                    pointer(Used.Values),Used.Count,
                    @lpfnAllocate,@lpfnReAllocate,@lpfnFree,nil)=0 then begin
                  // subset was created successfully -> save to PDF file
                  SetString(ttf,SubSetData,SubSetSize);
                  FreeMem(SubSetData);
                end;
              end;
            end;
            fFontFile2.Writer.Add(ttf);
            fFontFile2.FAttributes.AddItem('Length1',length(ttf));
            // /FontDescriptor is common to WinAnsi and Unicode fonts
            fFontDescriptor.AddItem('FontFile2',fFontFile2);
    ……
end;

Thanks.

Offline

#6 2021-02-02 19:22:48

5c4f394a
Member
Registered: 2013-03-22
Posts: 11

Re: Chinese Font bold style does not work, when EmbeddedTTF is true

Alalaki wrote:

I can't see your photo, and the site is unavailable. Can you describe it in words?

It's just the result of executing your code.

I can't help you specifically, but I think only the fonts shown below apply the style properly.

// SynPdf.pas

const // see PDF ref 9.6.2.2: Standard Type 1 Fonts
...
  STANDARDFONTS: array[0..11] of record
    Name: PDFString;
    Widths: PSmallIntArray;
  end = (
    (Name: 'Times-Roman'; Widths: @TIMES_ROMAN_W_ARRAY),
    (Name: 'Times-Bold'; Widths: @TIMES_BOLD_W_ARRAY),
    (Name: 'Times-Italic'; Widths: @TIMES_ITALIC_W_ARRAY),
    (Name: 'Times-BoldItalic'; Widths: @TIMES_BOLDITALIC_W_ARRAY),
    (Name: 'Helvetica'; Widths: @ARIAL_W_ARRAY),
    (Name: 'Helvetica-Bold'; Widths: @ARIAL_BOLD_W_ARRAY),
    (Name: 'Helvetica-Oblique'; Widths: @ARIAL_ITALIC_W_ARRAY),
    (Name: 'Helvetica-BoldOblique'; Widths: @ARIAL_BOLDITALIC_W_ARRAY),
    (Name: 'Courier'; Widths: nil), // Widths:nil -> set all widths to 600
    (Name: 'Courier-Bold'; Widths: nil),
    (Name: 'Courier-Oblique'; Widths: nil),
    (Name: 'Courier-BoldOblique'; Widths: nil));

...

const
  STAND_FONTS_PDF: array[TPdfFontStandard] of RawUTF8 = ('Times','Helvetica','Courier');
  STAND_FONTS_WIN: array[TPdfFontStandard] of RawUTF8 = ('Times New Roman','Arial','Courier New');

Last edited by 5c4f394a (2021-02-02 19:28:30)

Offline

#7 2021-02-04 09:13:12

Alalaki
Member
Registered: 2021-02-01
Posts: 4

Re: Chinese Font bold style does not work, when EmbeddedTTF is true

I used a terrible method to show bold fonts.

{SynPdf version 1.18}
procedure TPdfEnum.TextOut(var R: TEMRExtTextOut);
……
       if Canvas.FDoc.EmbeddedTTF then //如果需要嵌入字体
          with Canvas do
          begin
          if FContents<>nil then
            if isBold then
            begin
              begin
                FContents.Writer.Add('2 Tr'#10);
//                FContents.Writer.Add('0 g'#10);
                FContents.Writer.Add('0.5 w'#10);
              end;
            end else
              begin
                  FContents.Writer.Add('0 Tr'#10);
              end
          end;
        Canvas.ShowText(pointer(tmp));
      end;
    Canvas.EndText;
……

I found another problem.
The generated file cannot display the character of '∅' correctly, when I set font.name to 'Simsun'. I suspect the function named 'TPdfTTF.Create(aUnicodeTTF: TPdfFontTrueType)' can't get the Correct glyph accurately.

{SynPdf version 1.18}
{ TPdfTTF }

constructor TPdfTTF.Create(aUnicodeTTF: TPdfFontTrueType);
……
 for i := 0 to n-1 do begin
    idDeltai := idDelta[i];
    glyphi := idRangeOffset[i];
    if glyphi<>0 then
      glyphi := glyphi shr 1+i-n-startCode[i];
    for code := startCode[i] to endCode[i] do begin
      aUnicodeTTF.fUsedWideChar.Values[ndx] := code;
      if glyphi=0 then
        glyphIndex := code+idDeltai else begin
        glyphIndex := glyphIndexArray[glyphi+code];
        if glyphIndex<>0 then
          inc(glyphIndex,idDeltai);
      end;
      aUnicodeTTF.fUsedWide[ndx].Glyph := glyphIndex;
      inc(ndx);
    end;
  end;
……

Offline

Board footer

Powered by FluxBB