Skip to content

Commit e460971

Browse files
authored
Fix text with parentheses on text shaping (close #884) (#889)
1 parent 5451b3b commit e460971

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
2121
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) now supports heading colors defined as attributes (_e.g._ `<h2 color="#00ff00">...`)
2222
### Fixed
2323
* [`FPDF.image()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.image), when provided a `BytesIO` instance, does not close it anymore - _cf._ issue [#881](https://github.com/py-pdf/fpdf2/issues/881)
24+
* Fix invalid characters being generated when a string contains parentheses - _cf._ issue [#884](https://github.com/py-pdf/fpdf2/issues/884)
2425

2526
## [2.7.5] - 2023-08-04
2627
### Added

fpdf/line_break.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def adjust_pos(pos):
275275
char = chr(ti["mapped_char"]).encode("utf-16-be").decode("latin-1")
276276
if ti["x_offset"] != 0 or ti["y_offset"] != 0:
277277
if text:
278-
ret += f"({text}) Tj "
278+
ret += f"({escape_parens(text)}) Tj "
279279
text = ""
280280
offsetx = pos_x + adjust_pos(ti["x_offset"])
281281
offsety = pos_y - adjust_pos(ti["y_offset"])
@@ -293,12 +293,12 @@ def adjust_pos(pos):
293293
word_spacing and ti["mapped_char"] == space_mapped_code
294294
):
295295
if text:
296-
ret += f"({text}) Tj "
296+
ret += f"({escape_parens(text)}) Tj "
297297
text = ""
298298
ret += f"1 0 0 1 {(pos_x) * self.k:.2f} {(h - pos_y) * self.k:.2f} Tm "
299299

300300
if text:
301-
ret += f"({text}) Tj"
301+
ret += f"({escape_parens(text)}) Tj"
302302
return ret
303303

304304
def render_pdf_text_core(self, frag_ws, current_ws):

test/text_shaping/test_text_shaping.py

+12
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,15 @@ def test_features(tmp_path):
139139
pdf.ln()
140140

141141
assert_pdf_equal(pdf, HERE / "features.pdf", tmp_path)
142+
143+
144+
def test_text_with_parentheses(tmp_path):
145+
pdf = FPDF()
146+
pdf.add_page()
147+
pdf.add_font(family="SBL_Hbrw", fname=HERE / "SBL_Hbrw.ttf")
148+
pdf.set_font("SBL_Hbrw", size=30)
149+
pdf.set_text_shaping(30)
150+
pdf.cell(txt="אנגלית (באנגלית: English) ה", new_x="LEFT", new_y="NEXT")
151+
pdf.ln()
152+
pdf.cell(txt="אנגלית (באנגלית: English) ", new_y="NEXT")
153+
assert_pdf_equal(pdf, HERE / "text_with_parentheses.pdf", tmp_path)
16.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)