-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Change wrapping lines algorithm #783
Comments
I would definitely recommend doing this somewhere higher in the stack than PDFKit. PDFKit basically immediately writes content to the file as you are adding it, but for justification, especially when multiple styles or fonts can be inlined, you need a multi-pass layout algorithm. You'll need to do the layout first, and then once the final glyphs and their positions are known, send them to PDFKit for rendering. Text layout is actually a really hard problem - way harder than it seems at first glance. Getting the details right in a way that works for all languages is crazy challenging. Here's the basic text layout algorithm most text systems (like word processors, operating systems, web browsers, etc.) follow:
As you can see, there are a lot of steps here to do text layout correctly in a Unicode friendly way. PDFKit currently implements an extremely basic version of this without a lot of the steps. It basically only works well for unidirectional text in a single font, and you'll hit that limitation very quickly for anything complicated. I worked on this problem a bit in https://github.com/devongovett/textkit a while ago. I've been meaning to clean that up and release it, but I don't really have time. It's not really finished or well tested at the moment, but if you feel like taking a look at it feel free! Seems like it might be useful for react-pdf and other similar libraries that want to do text layout. Happy to help out - let me know if you have questions or if you want to help take over that code! |
Thanks for your answer @devongovett . Was very informative and helpful. As I explained, I really need to implement the |
The |
Hi @devongovett @alafr !
Im building react-pdf and I'm using pdfkit under the hood. The library got a lot of popularity so far and there are plenty of users, so I would like to thank you for also making this possible.
However, we now need to support justify paragraphs and words wrapping (support soft-hyphens, non breakable spaces, etc). I know you already support the first point, but not the second, and there are also people asking for it. Also the current paragraph justification is not optimal.
I started to work on a fix to this using the Breaking paragraphs into lines algorithm, by Donald E. Knuth and Michael F. Plass which I think it brings an excellent solution for this. There is already a JS library who implements it here. However, it's crucial to us to know if this library it's still maintained, and if someone will be able to test this implementation and eventually merge it into master. If now, we would be forced to come up with another solution 😄
Thanks for your time and work!
The text was updated successfully, but these errors were encountered: