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

New feature: Handle bidirectional text #882

Closed
andersonhc opened this issue Aug 11, 2023 · 5 comments
Closed

New feature: Handle bidirectional text #882

andersonhc opened this issue Aug 11, 2023 · 5 comments

Comments

@andersonhc
Copy link
Collaborator

Since text shaping was implemented, fpdf2 can correctly render text in left to right (LTR) or right to left (RTL) direction.
However, if the text provided has both RTL and LTR on the same string, fpdf2 is not applying the bidirectional rules to render the text, as pointed on #549
The problem happens because Harfbuzz will shape the text in one direction only.
If we apply the BIDI algorithm to the input string and break the direction changes into different fragments it should solve this issue as they will be rendered separately.

References:
https://www.w3.org/International/articles/inline-bidi-markup/uba-basics
https://en.wikipedia.org/wiki/Bidirectional_text

Implementations of the BIDI algorithm:
https://github.com/fribidi/fribidi
https://github.com/MeirKriheli/python-bidi

@mihir1739
Copy link

Can I work on this if possible please assign this to me.

@gmischler
Copy link
Collaborator

Welcome to fpdf2, @mihir1739 !

Can I work on this if possible please assign this to me.

This issue is currently being worked on in #972.
Maybe you can find another interesting issue?

@andersonhc
Copy link
Collaborator Author

@marcstober
I am closing this issue as #1096 is now merged and released on 2.7.8
Can you please take a look and do some tests with bidirectional text?

@marcstober
Copy link

@andersonhc it's working! Thank you for your long-standing help with this issue.

Would it be useful for me to add my test file somewhere (an updated version of what I'd posted in #549)?

I noticed that selecting the text in a BIDI, RTL paragraph in Acrobat is a bit funny with my test file, and more generally, I'm not sure how fpdf2 knows whether a paragraph is a LTR or RTL paragraph (something I often switch manually in word processing apps). But this might be something I just need to read up the docs or something, and I can always file a new issue for any edge case. Overall I'm happy to see this working and now can go back to some side projects I set aside back when I ran into this over a year ago!

@andersonhc
Copy link
Collaborator Author

@andersonhc it's working! Thank you for your long-standing help with this issue.

Would it be useful for me to add my test file somewhere (an updated version of what I'd posted in #549)?

I noticed that selecting the text in a BIDI, RTL paragraph in Acrobat is a bit funny with my test file, and more generally, I'm not sure how fpdf2 knows whether a paragraph is a LTR or RTL paragraph (something I often switch manually in word processing apps). But this might be something I just need to read up the docs or something, and I can always file a new issue for any edge case. Overall I'm happy to see this working and now can go back to some side projects I set aside back when I ran into this over a year ago!

I just did a small PR adding the option to force paragraph direction. Here is the test file:
https://github.com/py-pdf/fpdf2/blob/master/test/text_shaping/bidi_paragraph_direction.pdf

Also updated the Text Shaping documentation.

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

5 participants