Skip to content

Commit

Permalink
Fix NBSP support. Closes: #834. (#851)
Browse files Browse the repository at this point in the history
* Fix NBSP support. Closes: #834.

* Update docs about NBSP (#834)

* Update unittest for #834 (test_charmap)

* Update unittest for #834 (test_html_whitespace_handling)

* Update unittest for #834 (test_html)
  • Loading branch information
rysson committed Oct 11, 2023
1 parent e918119 commit 1546f70
Show file tree
Hide file tree
Showing 16 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This release is the first performed from the [@py-pdf GitHub org](https://github
### Changed
* The formatting output by `write_html()` has changed in some aspects. Vertical spacing around headings and paragraphs may be slightly different, and elements at the top of the page don't have any extra spacing above anymore.
* [`FPDF.table()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): If the height of a row is governed by an image, then the default vertical alignment of the other cells is "center". This was "top".
* variable-width non-breaking space (NBSP) support [issue #834](https://github.com/PyFPDF/fpdf2/issues/834)
This change was made for consistency between row-height governed by text or images. The old behaviour can be enforced using the new vertical alignment parameter.
### Fixed
* In multi_cells and table cells with horizontal padding, the text was not given quite enough space.
Expand Down
5 changes: 3 additions & 2 deletions docs/Text.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ There are a few advanced typesetting features that fpdf doesn't currently suppor
* Vertical writing - Some writing systems are meant to be written vertically. Doing so is not directly supported. In cases where this just means to stack characters on top of each other (eg. Chinese, Japanese, etc.), client software can implement this by placing each character individuall at the correct location. In cases where the characters are connected with each other (eg. Mongolian), this may be more difficult, if possible at all.

### Character or Word Based Line Wrapping
By default, `multi_line()` and `write()` will wrap lines based on words, using space characters and soft hyphens as seperators.
For languages like Chinese and Japanese, that don't usually seperate their words, character based wrapping is more appropriate.
By default, `multi_cell()` and `write()` will wrap lines based on words, using space characters and soft hyphens as separators.
Non-breaking spaces (\U00a0) do not trigger a word wrap, but are otherwise treated exactly as a normal space character.
For languages like Chinese and Japanese, that don't usually separate their words, character based wrapping is more appropriate.
In such a case, the argument `wrapmode="CHAR"` can be used (the default is "WORD"), and each line will get broken right before the
character that doesn't fit anymore.

Expand Down
5 changes: 5 additions & 0 deletions fpdf/line_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SOFT_HYPHEN = "\u00ad"
HYPHEN = "\u002d"
SPACE = " "
NBSP = "\u00a0"
NEWLINE = "\n"


Expand Down Expand Up @@ -409,6 +410,10 @@ def add_character(
self.number_of_spaces,
)
self.number_of_spaces += 1
elif character == NBSP:
# PDF viewers ignore NBSP for word spacing with "Tw".
character = SPACE
self.number_of_spaces += 1
elif character == SOFT_HYPHEN and not self.print_sh:
self.hyphen_break_hint = HyphenHint(
original_fragment_index,
Expand Down
Binary file modified test/fonts/charmap_first_999_chars-DejaVuSans-Oblique.pdf
Binary file not shown.
Binary file modified test/fonts/charmap_first_999_chars-DejaVuSans.pdf
Binary file not shown.
Binary file modified test/fonts/charmap_first_999_chars-DejaVuSansMono.pdf
Binary file not shown.
Binary file modified test/fonts/charmap_first_999_chars-DroidSansFallback.pdf
Binary file not shown.
Binary file modified test/fonts/charmap_first_999_chars-Garuda.pdf
Binary file not shown.
Binary file modified test/fonts/charmap_first_999_chars-Quicksand-Regular.pdf
Binary file not shown.
Binary file modified test/fonts/charmap_first_999_chars-Roboto-Regular.pdf
Binary file not shown.
Binary file modified test/fonts/charmap_first_999_chars-Waree.pdf
Binary file not shown.
Binary file modified test/fonts/charmap_first_999_chars-cmss12.pdf
Binary file not shown.
Binary file modified test/html/html_customize_ul.pdf
Binary file not shown.
Binary file modified test/html/html_description.pdf
Binary file not shown.
Binary file modified test/html/html_features.pdf
Binary file not shown.
Binary file modified test/html/html_whitespace_handling.pdf
Binary file not shown.

0 comments on commit 1546f70

Please sign in to comment.