diff --git a/CHANGELOG.md b/CHANGELOG.md index 8debbc870..6aef3cc8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/docs/Text.md b/docs/Text.md index 77689ae4f..2260cd27c 100644 --- a/docs/Text.md +++ b/docs/Text.md @@ -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. diff --git a/fpdf/line_break.py b/fpdf/line_break.py index 258c72933..bbab8eb42 100644 --- a/fpdf/line_break.py +++ b/fpdf/line_break.py @@ -18,6 +18,7 @@ SOFT_HYPHEN = "\u00ad" HYPHEN = "\u002d" SPACE = " " +NBSP = "\u00a0" NEWLINE = "\n" @@ -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, diff --git a/test/fonts/charmap_first_999_chars-DejaVuSans-Oblique.pdf b/test/fonts/charmap_first_999_chars-DejaVuSans-Oblique.pdf index 1ff12f6c9..9d39d523e 100644 Binary files a/test/fonts/charmap_first_999_chars-DejaVuSans-Oblique.pdf and b/test/fonts/charmap_first_999_chars-DejaVuSans-Oblique.pdf differ diff --git a/test/fonts/charmap_first_999_chars-DejaVuSans.pdf b/test/fonts/charmap_first_999_chars-DejaVuSans.pdf index 1857ad928..1a68de14d 100644 Binary files a/test/fonts/charmap_first_999_chars-DejaVuSans.pdf and b/test/fonts/charmap_first_999_chars-DejaVuSans.pdf differ diff --git a/test/fonts/charmap_first_999_chars-DejaVuSansMono.pdf b/test/fonts/charmap_first_999_chars-DejaVuSansMono.pdf index 502be9397..a42d4fbef 100644 Binary files a/test/fonts/charmap_first_999_chars-DejaVuSansMono.pdf and b/test/fonts/charmap_first_999_chars-DejaVuSansMono.pdf differ diff --git a/test/fonts/charmap_first_999_chars-DroidSansFallback.pdf b/test/fonts/charmap_first_999_chars-DroidSansFallback.pdf index 40806c3f0..7b3caa3d9 100644 Binary files a/test/fonts/charmap_first_999_chars-DroidSansFallback.pdf and b/test/fonts/charmap_first_999_chars-DroidSansFallback.pdf differ diff --git a/test/fonts/charmap_first_999_chars-Garuda.pdf b/test/fonts/charmap_first_999_chars-Garuda.pdf index a3c0c4b1c..5cf1471bc 100644 Binary files a/test/fonts/charmap_first_999_chars-Garuda.pdf and b/test/fonts/charmap_first_999_chars-Garuda.pdf differ diff --git a/test/fonts/charmap_first_999_chars-Quicksand-Regular.pdf b/test/fonts/charmap_first_999_chars-Quicksand-Regular.pdf index 9b59c9876..d6388180e 100644 Binary files a/test/fonts/charmap_first_999_chars-Quicksand-Regular.pdf and b/test/fonts/charmap_first_999_chars-Quicksand-Regular.pdf differ diff --git a/test/fonts/charmap_first_999_chars-Roboto-Regular.pdf b/test/fonts/charmap_first_999_chars-Roboto-Regular.pdf index 4f515ed40..0006c68b5 100644 Binary files a/test/fonts/charmap_first_999_chars-Roboto-Regular.pdf and b/test/fonts/charmap_first_999_chars-Roboto-Regular.pdf differ diff --git a/test/fonts/charmap_first_999_chars-Waree.pdf b/test/fonts/charmap_first_999_chars-Waree.pdf index fccf3900f..f7f54a507 100644 Binary files a/test/fonts/charmap_first_999_chars-Waree.pdf and b/test/fonts/charmap_first_999_chars-Waree.pdf differ diff --git a/test/fonts/charmap_first_999_chars-cmss12.pdf b/test/fonts/charmap_first_999_chars-cmss12.pdf index ca5bbce4e..180f36e96 100644 Binary files a/test/fonts/charmap_first_999_chars-cmss12.pdf and b/test/fonts/charmap_first_999_chars-cmss12.pdf differ diff --git a/test/html/html_customize_ul.pdf b/test/html/html_customize_ul.pdf index 4f48d561f..b3fc788b9 100644 Binary files a/test/html/html_customize_ul.pdf and b/test/html/html_customize_ul.pdf differ diff --git a/test/html/html_description.pdf b/test/html/html_description.pdf index e260238f3..e07836d13 100644 Binary files a/test/html/html_description.pdf and b/test/html/html_description.pdf differ diff --git a/test/html/html_features.pdf b/test/html/html_features.pdf index 32adf21a8..2bcb2fbac 100644 Binary files a/test/html/html_features.pdf and b/test/html/html_features.pdf differ diff --git a/test/html/html_whitespace_handling.pdf b/test/html/html_whitespace_handling.pdf index 94be62640..21f87eff6 100644 Binary files a/test/html/html_whitespace_handling.pdf and b/test/html/html_whitespace_handling.pdf differ