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

Fix NBSP support. Closes: #834. #851

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.