Recent

Author Topic: Chinese Fonts in fpPDF  (Read 3520 times)

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: Chinese Fonts in fpPDF
« Reply #15 on: September 28, 2024, 09:06:56 am »
@Davo
- I made the fonts available in a 7z archive because they take up much less space, which also helps with a poor internet connection
- if you disable built-in fonts in the options, the poSubsetFont option disables automatically, but then you need to have all the fonts installed for the pdf to display properly
Best regards / Pozdrawiam
paweld

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Chinese Fonts in fpPDF
« Reply #16 on: September 28, 2024, 11:45:29 am »
Right, attached is my demo based on what I have learnt here.

It builds and runs on both Linux and Windows (and probably MacOS). Just needs fpc to build from the command line, runs from the command line. If you have an appropriate font installed or in the same dir (or below) as the executable it does show the Chinese characters.

However, neither my 'out of the box' Linux or Windows have appropriate fonts installed. You need a Chinese specialist font, either a .ttf or .otf file. Not a .ttc file. I suggest using a .ttf font from https://github.com/notofonts/noto-cjk

I'd expect Chinese speakers will have more choice and it would be good to hear if that is the case.

paweld - yes, I realised how silly I was being wrt the 7z files, once I realised I was not using them !

Yes, its necessary to embed fonts IMHO. Only exception is if you are using the so called 14 PDF fonts and they are really not suitable. fpPDF default is to embed fonts, you turn it off by asserting poNoEmbeddedFonts. The default for poSubsetFont is off but the examples I found were on. I should have looked closer. Its not totally clear to me what it does, but it sure breaks .otf fonts and, I suspect, .otf fonts masquerading as .ttf.

I am pretty happy with what we have now !  Some notes on the wiki will be a good idea !

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jianwt

  • Full Member
  • ***
  • Posts: 125
Re: Chinese Fonts in fpPDF
« Reply #17 on: September 29, 2024, 03:31:03 am »
Unfortunately, I put a Chinese font "simfang.ttf" in the fonts directory, which is the "中文仿宋" font, and put it in the "FontsCh: TStringArray = ('simfang','Noto Sans CJK SC', 'Noto Serif CJK SC', 'Source Han Serif SC VF'{, 'Simsun-ExtB'}); " The name is added, but when the PDF file is generated, the Chinese is still not displayed, only the box is displayed.The program prompts that the Chinese font cannot be found.
« Last Edit: September 29, 2024, 03:35:53 am by jianwt »

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: Chinese Fonts in fpPDF
« Reply #18 on: September 29, 2024, 06:11:27 am »
@jianwt Because you have to specify the font name instead of the file name. For “simfang.ttf” it is “FangSong” (case sensitive).

To check the font name you can modify the code attached by @dbannon as follows:
Code: Pascal  [Select][+][-]
  1. function TPDFTestApp.SetupFonts(Doc: TPDFDocument): boolean;
  2.   var
  3.     i: integer;
  4.   begin
  5.     //...
  6.     for i := 0 to gTTFontCache.Count - 1 do
  7.     begin
  8.       if pos('CJK', gTTFontCache.Items[i].HumanFriendlyName) > 0 then
  9.         writeln('FONT-' + gTTFontCache.Items[i].FileName + ' HF=' + gTTFontCache.Items[i].HumanFriendlyName)
  10.       else if pos('simfang.ttf', gTTFontCache.Items[i].FileName) > 0 then
  11.         WriteLn('!!! for file "simfang.ttf" font name is: ', gTTFontCache.Items[i].FamilyName);
  12.     end;
  13.     //...
  14.   end;    
And then after defining the list of Chinese fonts to:
Code: Pascal  [Select][+][-]
  1. FontsCh: TStringArray = ('FangSong', 'Noto Sans CJK SC', 'Noto Serif CJK SC', 'Source Han Serif SC VF'{, 'Simsun-ExtB'});  
we get the result as below
Best regards / Pozdrawiam
paweld

jianwt

  • Full Member
  • ***
  • Posts: 125
Re: Chinese Fonts in fpPDF
« Reply #19 on: September 29, 2024, 07:21:40 am »
@paweld 

I did. It's just a mistake. Can you send up your project? Thank you very much!

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: Chinese Fonts in fpPDF
« Reply #20 on: September 29, 2024, 09:16:40 am »
this is a reworked design by @dbannon
I downloaded the font from: https://github.com/21wmd/ttf-simfang/blob/master/usr/share/fonts/truetype/sim/simfang.ttf and uploaded it to the project directory
Best regards / Pozdrawiam
paweld

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Chinese Fonts in fpPDF
« Reply #21 on: September 29, 2024, 12:11:27 pm »
this is a reworked design ...

Yep, definitely works fine for me.  A pain searching for FileName reather than Human Friendly Name but only way to find whats needed. 

jianwt, unless you have a reason for using FangSong, you might be better using "Noto Sans CJK SC". It, and its friends are pretty well documented and available in a range of 'weights', that is Light..Black, Mono spaced, with and without Serif.

https://github.com/notofonts/noto-cjk

https://github.com/notofonts/noto-cjk/tree/main/Serif#downloading-noto-serif-cjk

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jianwt

  • Full Member
  • ***
  • Posts: 125
Re: Chinese Fonts in fpPDF
« Reply #22 on: September 29, 2024, 03:05:35 pm »
@paweld。

“FontsCh:  TStringArray = ('FangSong', 'Noto Sans CJK SC', 'Noto Serif CJK SC', 'Source Han Serif SC VF'{, 'Simsun-ExtB'}); ” From what did you get the name 'FangSong'? Is it Chinese pinyin?

Why can't I just change it to “FontsCh:  TStringArray = ('仿宋', 'Noto Sans CJK SC', 'Noto Serif CJK SC', 'Source Han Serif SC VF'{, 'Simsun-ExtB'}); ” in Chinese.
Thanks to paweld AND dbannon  for their help.

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Chinese Fonts in fpPDF
« Reply #23 on: September 30, 2024, 01:34:44 am »
Hi jianwt,

The word "FangSong" is hard wired into the font, maybe "仿宋" is too, I don't know.

But those fonts are read and indexed by the Font Cache, in the fpttf unit and its very likely that it looks, when trying to find what you want, at only the "English" name. That could be because all fonts have an English name, maybe its quicker and more reliable to search for that English name ?  I really don't know.

It could be something like the specification for TTF fonts defines the name of the font as having only Latin characters, that is the 7 bit English alphabet. I have never seen a font, for example that uses even a European accented character in its name. Again, I don't know. The specification for TTF must be pretty old now....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jianwt

  • Full Member
  • ***
  • Posts: 125
Re: Chinese Fonts in fpPDF
« Reply #24 on: September 30, 2024, 03:23:13 am »
@paweld AND @ dbannon

I use more Chinese fonts, such as "simfang.ttf" Chinese font "FontsCh" = "fangsong", this name, with what method or software can be found? "FontsCh: TStringArray = ('fangsong', 'Noto Sans CJK SC', 'Noto Serif CJK SC', 'Source Han Serif SC VF'{, 'simsun-ExtB'});" . I'm only guessing that 'fangsong' is the Chinese pinyin of the font.

jianwt

  • Full Member
  • ***
  • Posts: 125
Re: Chinese Fonts in fpPDF
« Reply #25 on: September 30, 2024, 03:46:58 am »
Using the font viewer, it turns out that's the English name of the font. We can find out. Got it. Thank you.

jianwt

  • Full Member
  • ***
  • Posts: 125
Re: Chinese Fonts in fpPDF
« Reply #26 on: September 30, 2024, 05:04:23 am »
@ dbannon
In lazarUS3.4, linux 64 system under the compilation report overflow error "项目testfppdf提引异常类"EListError'带有信息:Listindex (-1) outof bounds  在地址431C69", the code did not change anything. Please take a look.

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Chinese Fonts in fpPDF
« Reply #27 on: September 30, 2024, 09:15:12 am »
Sorry, cannot uncompress a .rar file. Could you send a zip or tgz file please ?

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

440bx

  • Hero Member
  • *****
  • Posts: 4727
Re: Chinese Fonts in fpPDF
« Reply #28 on: September 30, 2024, 09:30:02 am »
just FYI, 7zip can decompress most .rar files. 
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jianwt

  • Full Member
  • ***
  • Posts: 125
Re: Chinese Fonts in fpPDF
« Reply #29 on: September 30, 2024, 10:07:48 am »
@ dbannon
zip

 

TinyPortal © 2005-2018