prawnpdf / prawn

Fast, Nimble PDF Writer for Ruby
https://prawnpdf.org
Other
4.68k stars 688 forks source link

Preserve soft hyphens when advancing line to the next page #1133

Open mojavelinux opened 5 years ago

mojavelinux commented 5 years ago

When there's not enough height for a line of text on the current page, Prawn advances that line to the next page (handled by Prawn::Text::Formatted::Wrap). When this happens, the soft hyphens in that line are stripped. As a result, soft hyphens are absent when the line is rendered. This affects hyphenation. The soft hyphens should be preserved.

mojavelinux commented 5 years ago

The problematic line is the call to Fragment#include_trailing_white_space! in Arranger#repack_unretrieved. (See https://github.com/prawnpdf/prawn/blob/c5842a27b15f912f2f0ad5818a9ef38992978b3c/lib/prawn/text/formatted/arranger.rb#L213). That call, in turn, calls Fragment#process_text, which removes soft hyphens.

If we look at the implementation for Fragment#include_trailing_white_space!, we see that it calls @format_state.delete(:exclude_trailing_white_space). So it seems like it was an oversight not to also call @format_state.delete(:normalized_soft_hyphen). Adding that line fixes the problem.

I ran all tests in Prawn and all 750+ tests in Asciidoctor PDF and can report that adding this line does not break any of the tests.

mojavelinux commented 5 years ago

Here's the interim patch I've applied to Asciidoctor PDF: https://github.com/asciidoctor/asciidoctor-pdf/pull/1384/files