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 bidirectional text processing on get_string_width #1233

Merged
merged 3 commits into from
Jul 31, 2024
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 @@ -40,6 +40,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
* default values for `top_margin` and `bottom_margin` in `HTML2FPDF._new_paragraph()` calls are now correctly converted into chosen document units.
* In [text_columns()](https://py-pdf.github.io/fpdf2/extColumns.html), paragraph top/bottom margins didn't correctly trigger column breaks; [issue #1214](https://github.com/py-pdf/fpdf2/issues/1214)
* [`fpdf.drawing.color_from_hex_string`](https://py-pdf.github.io/fpdf2/fpdf/drawing.html#fpdf.drawing.color_from_hex_string) did not test or mention accepting lowercase hex values.
* handling of bidirectional text on `FPDF.get_string_width()` [issue #1231]
### Removed
* an obscure and undocumented [feature](https://github.com/py-pdf/fpdf2/issues/1198) of [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html), which used to magically pass instance attributes as arguments.
### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ def get_string_width(self, s, normalized=False, markdown=False):
# normalized is parameter for internal use
s = s if normalized else self.normalize_text(s)
w = 0
for frag in self._parse_chars(s, markdown):
for frag in self._preload_bidirectional_text(s, markdown):
w += frag.get_width()
return w

Expand Down
Binary file added test/text_shaping/bidi_get_string_width.pdf
Binary file not shown.
33 changes: 33 additions & 0 deletions test/text_shaping/test_bidirectional.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,36 @@ def test_bidi_paragraph_direction(tmp_path):
HERE / "bidi_paragraph_direction.pdf",
tmp_path,
)


def test_bidi_get_string_width(tmp_path):
# issue 1231
teststrings = [
"الملوك",
"الملوك",
"test",
"الملوك",
"الملوك",
"الملوك",
"test",
"الملوك",
"test",
]
pdf = FPDF()
pdf.add_page()
pdf.c_margin = 0
pdf.add_font("noto", style="", fname=HERE / "NotoNaskhArabic-Regular.ttf")
pdf.set_text_shaping(use_shaping_engine=True)

pdf.set_font("noto", size=24)
pdf.set_draw_color(160)
pdf.set_line_width(0.3)
for string in teststrings:
pdf.set_x(110 - pdf.get_string_width(string))
pdf.rect(
pdf.get_x(), pdf.get_y() + 2, pdf.get_string_width(string), 13, style="D"
)
pdf.cell(h=17, text=string)
pdf.ln()
pdf.ln()
assert_pdf_equal(pdf, HERE / "bidi_get_string_width.pdf", tmp_path)
Loading