Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shaping Thai text support #679

Closed
mrchoke opened this issue Feb 4, 2023 · 11 comments
Closed

Shaping Thai text support #679

mrchoke opened this issue Feb 4, 2023 · 11 comments

Comments

@mrchoke
Copy link

mrchoke commented Feb 4, 2023

Please explain your intent
Now fpdf2 can display Thai text but it not pretty.

Describe the solution you'd like
Reference for shaping thai text.

Additional context
Screenshot 2023-02-04 at 15 19 22

@Lucas-C
Copy link
Member

Lucas-C commented Feb 6, 2023

Have you read this section from the documentation?

Notes on non-latin languages
Some users may encounter a problem where some characters displayed incorrectly. For example, using Thai language in the picture below

Page: https://pyfpdf.github.io/fpdf2/Unicode.html

We also have an existing issue about supporting ligatures: #540

@andersonhc
Copy link
Collaborator

@mrchoke can you please give the Thai text so I can copy/paste it? I am doing some tests integrating harfbuzz on fpdf2 and I'd like to know if it solves your problem too.

@mrchoke
Copy link
Author

mrchoke commented Feb 7, 2023

https://pyfpdf.github.io/fpdf2/Unicode.html

Have you read this section from the documentation?

Yes, I read it before. Now FPDF2 can display thai text at sample level. Thai writing system too complex.
I used WeasyPrint pdf generator it use Pango for thai text rendering it so pretty and it can manage thai word/line break too.

@mrchoke
Copy link
Author

mrchoke commented Feb 7, 2023

@mrchoke can you please give the Thai text so I can copy/paste it? I am doing some tests integrating harfbuzz on fpdf2 and I'd like to know if it solves your problem too.

Good news @andersonhc.

ok, wait me a while, I'll do it for you as soon as possible.

@mrchoke
Copy link
Author

mrchoke commented Feb 7, 2023

Sample Thai text:

  • ป ปี ปี่ ปี้ ปี๊ ปี๋ ปา ป่า ป้า ป๊า ป๋า ปำ ป่ำ ปุ ปู ปฺ ปํ ปิํ
  • บ บี บี่ บี้่ บี๊ บี๋ บา บ่า บ้า บ๊า บ๋า บำ บ่ำ บุ บู บฺ บํ บิํ
  • ฬ ฬี ฬี่ ฬี้ ฬี๊ ฬี๋ ฬา ฬ่า ฬ้า ฬ๊า ฬ๋า ฬำ ฬ่ำ ฬุ ฬู ฬฺ ฬํ ฬิํ
  • ญ ญี ญี่ ญี้ ญี๊ ญี๋ ญา ญ่า ญ้า ญ๊า ญ๋า ญำ ญ่ำ ญุ ญู ญฺ ญํ ญิํ
  • ฐ ฐี ฐี่ ฐี้ ฐี๊ ฐี๋ ฐา ฐ่า ฐ้า ฐ๊า ฐ๋า ฐำ ฐ่ำ ฐุ ฐู ฐฺ ฐํ ฐิํ
  • ฎ ฎี ฎี่ ฎี้ ฎี๊ ฎี๋ ฎา ฎ่า ฎ้า ฎ๊า ฎ๋า ฎำ ฎ่ำ ฎุ ฎู ฎฺ ฎํ ฎิํ
  • ฏ ฏี ฏี่ ฏี้ ฏี๊ ฏี๋ ฏา ฏ่า ฏ้า ฏ๊า ฏ๋า ฏำ ฏ่ำ ฏุ ฏู ฏฺ ฏํ ฏิํ

Current FPDF2 Render:

Screenshot 2023-02-08 at 01 28 05

Expected:

Screenshot 2023-02-08 at 01 29 50

Reference:
https://www.nectec.or.th/it-standards/thaistd.pdf
Page 5
Screenshot 2023-02-07 at 23 12 11
Screenshot 2023-02-07 at 23 12 46

@andersonhc

@Lucas-C
Copy link
Member

Lucas-C commented Feb 14, 2023

@mrchoke can you please give the Thai text so I can copy/paste it? I am doing some tests integrating harfbuzz on fpdf2 and I'd like to know if it solves your problem too.

Wow, that sounds promising!
I'd be glad to know how those tests went: what kind of problems have you faced?
Are there missing bricks in fpdf2 to plug it with harfbuzz?

@eroux
Copy link

eroux commented Feb 14, 2023

I do have an example with Tibetan, using the NotoSerifTibetan font:

from fpdf import FPDF

pdf = FPDF()
pdf.add_page(format=(200,200))
pdf.add_font("NotoSerifTibetan", fname='NotoSerifTibetan-Regular.ttf')
pdf.set_font("NotoSerifTibetan", '', 56)
pdf.write(40, "༄༅། །སྒྲུབ།")
pdf.output("test-tib.pdf")

result :

test-tib.pdf

expected result (produced with LibreOffice Writer):

test-tib-lo.pdf

@andersonhc
Copy link
Collaborator

@mrchoke can you please give the Thai text so I can copy/paste it? I am doing some tests integrating harfbuzz on fpdf2 and I'd like to know if it solves your problem too.

Wow, that sounds promising! I'd be glad to know how those tests went: what kind of problems have you faced? Are there missing bricks in fpdf2 to plug it with harfbuzz?

My biggest problem is that fpdf2 maps 1 unicode character to 1 glyph on the font, and that doesn't work with font shapers that supports ligatures (multiple characters will render 1 glyph).

I changed the SubsetMap to work with Glyphs and did several changes on the "font" code (using classes instead of dictionary).

I have some more things to fix, but as soon as I get a working prototype version I'll open a discussion.

@Lucas-C
Copy link
Member

Lucas-C commented Feb 15, 2023

I changed the SubsetMap to work with Glyphs and did several changes on the "font" code (using classes instead of dictionary).
I have some more things to fix, but as soon as I get a working prototype version I'll open a discussion.

Awesome! Best of luck with this project 😊
Your contribution would be very welcome.

@Lucas-C
Copy link
Member

Lucas-C commented Aug 2, 2023

@andersonhc PR #820 has been merged today.

Could you test if that solved your issue @mrchoke?

You can install fpdf2 directly from the master branch of this repo with this command:

pip install git+https://github.com/PyFPDF/fpdf2.git@master

The documentation is there: https://pyfpdf.github.io/fpdf2/TextShaping.html

@Lucas-C
Copy link
Member

Lucas-C commented Aug 17, 2023

Since this has been released in 2.7.5, I'm going to close this issue.

Anyone is free to reopen it or open another one thare are still issues with thai text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants