Skip to content

Commit

Permalink
Fix line wrapping with justified content and unicode fonts - close #118
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Mar 22, 2021
1 parent 95873b5 commit d8e5b6a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and [PEP 440](https://www.python.org/dev/peps/pep-0440/).
## [2.3.2] - not released yet
### Added
- `FPDF.set_xmp_metadata`
### Fixed
- line wrapping with justified content and unicode fonts, _cf._ [#118](https://github.com/PyFPDF/fpdf2/issues/118)

## [2.3.1] - 2021-02-28
### Added
Expand Down
28 changes: 15 additions & 13 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ def add_font(self, family, style="", fname=None, uni=False):

# Check if font already added or one of the core fonts
if fontkey in self.fonts or fontkey in self.core_fonts:
warnings.warn("Core font or font already added: doing nothing")
warnings.warn(f"Core font or font already added '{fontkey}': doing nothing")
return
if uni:
for parent in (".", FPDF_FONT_DIR, SYSTEM_TTFONTS):
Expand Down Expand Up @@ -1082,6 +1082,11 @@ def rotation(self, angle, x=None, y=None):
The rotation affects all elements which are printed inside the indented context
(with the exception of clickable areas).
Args:
angle (float): angle in degrees
x (float): abscissa of the center of the rotation
y (float): ordinate of the center of the rotation
Notes
-----
Expand Down Expand Up @@ -1214,18 +1219,15 @@ def cell(self, w, h=0, txt="", border=0, ln=0, align="", fill=False, link=""):
s += (
f"BT 0 Tw {(self.x + dx) * k:.2F} "
f"{(self.h - self.y - 0.5 * h - 0.3 * self.font_size) * k:.2F} "
f"Td ["
"Td ["
)

t = txt.split(" ")
numt = len(t)
for i in range(numt):
tx = t[i]
tx = enclose_in_parens(
escape_parens(tx.encode("UTF-16BE").decode("latin-1"))
)
s += f"{tx} "
if (i + 1) < numt:
words = txt.split(" ")
for i, word in enumerate(words):
word = escape_parens(word.encode("UTF-16BE").decode("latin-1"))
s += f"({word}) "
is_last_word = (i + 1) == len(words)
if not is_last_word:
adj = -(self.ws * self.k) * 1000 / self.font_size_pt
s += f"{adj}({space}) "
s += "] TJ"
Expand Down Expand Up @@ -1326,8 +1328,8 @@ def multi_cell(
(in any order):
`L`: left ; `T`: top ; `R`: right ; `B`: bottom. Default value: 0.
align (str): Allows to center or align the text. Possible values are:
`L` or empty string: left align (default value) ; `C`: center ;
`R`: right align
`J`: justify (default value); `L` or empty string: left align ;
`C`: center ; `R`: right align
fill (bool): Indicates if the cell background must be painted (`True`)
or transparent (`False`). Default value: False.
split_only (bool): if `True`, does not output anything, only perform
Expand Down
14 changes: 14 additions & 0 deletions test/cells/test_multi_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ def test_multi_cell_table_unbreakable(tmp_path): # issue 111
assert_pdf_equal(pdf, HERE / "multi_cell_table_unbreakable.pdf", tmp_path)


def test_multi_cell_justified_with_unicode_font(tmp_path): # issue 118
pdf = fpdf.FPDF()
pdf.add_page()
pdf.add_font(
"DejaVu", "", HERE / "../end_to_end_legacy/charmap/DejaVuSans.ttf", uni=True
)
pdf.set_font("DejaVu", "", 14)
text = 'Justified line containing "()" that is long enough to trigger wrapping and a line jump'
pdf.multi_cell(w=0, h=8, txt=text, ln=1)
assert_pdf_equal(
pdf, HERE / "test_multi_cell_justified_with_unicode_font.pdf", tmp_path
)


## Code used to create test
# doc = fpdf.FPDF(format = 'letter', unit = 'pt')
# set_doc_date_0(doc)
Expand Down
Binary file not shown.

0 comments on commit d8e5b6a

Please sign in to comment.