Skip to content

Commit

Permalink
Restoring ability to disable text shaping - fix #1287
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Oct 17, 2024
1 parent ab0bd4d commit c6e230d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ in order to get warned about deprecated features used in your code.
This can also be enabled programmatically with `warnings.simplefilter('default', DeprecationWarning)`.

## [2.8.2] - Not released yet
### Fixed
* `FPDF.set_text_shaping(False)` was broken since version 2.7.8 and is now working properly - [issue #1287](https://github.com/py-pdf/fpdf2/issues/1287)

## [2.8.1] - 2024-10-04
### Added
Expand Down
24 changes: 12 additions & 12 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,6 @@ def set_display_mode(self, zoom, layout="continuous"):
raise FPDFException(f"Incorrect zoom display mode: {zoom}")
self.page_layout = LAYOUT_ALIASES.get(layout, layout)

# Disabling this check - importing outside toplevel to check module is present
# pylint: disable=import-outside-toplevel, unused-import
def set_text_shaping(
self,
use_shaping_engine: bool = True,
Expand All @@ -601,16 +599,18 @@ def set_text_shaping(
script: a valid OpenType script tag like "arab" or "latn"
language: a valid OpenType language tag like "eng" or "fra"
"""
if use_shaping_engine:
try:
import uharfbuzz
except ImportError as exc:
raise FPDFException(
"The uharfbuzz package could not be imported, but is required for text shaping. Try: pip install uharfbuzz"
) from exc
else:
if not use_shaping_engine:
self.text_shaping = None
return

try:
# pylint: disable=import-outside-toplevel, unused-import
import uharfbuzz
except ImportError as exc:
raise FPDFException(
"The uharfbuzz package could not be imported, but is required for text shaping. Try: pip install uharfbuzz"
) from exc

#
# Features must be a dictionary contaning opentype features and a boolean flag
# stating whether the feature should be enabled or disabled.
Expand Down Expand Up @@ -4149,8 +4149,8 @@ def image(
bytes, an io.BytesIO, or a instance of `PIL.Image.Image`
x (float, fpdf.enums.Align): optional horizontal position where to put the image on the page.
If not specified or equal to None, the current abscissa is used.
`Align.C` can also be passed to center the image horizontally;
and `Align.R` to place it along the right page margin
`fpdf.enums.Align.C` can also be passed to center the image horizontally;
and `fpdf.enums.Align.R` to place it along the right page margin
y (float): optional vertical position where to put the image on the page.
If not specified or equal to None, the current ordinate is used.
After the call, the current ordinate is moved to the bottom of the image
Expand Down
3 changes: 1 addition & 2 deletions fpdf/graphics_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,7 @@ def text_shaping(self):

@text_shaping.setter
def text_shaping(self, v):
if v:
self.__statestack[-1]["text_shaping"] = v
self.__statestack[-1]["text_shaping"] = v

def font_face(self):
"""
Expand Down
Binary file added test/text_shaping/disabling_text_shaping.pdf
Binary file not shown.
34 changes: 24 additions & 10 deletions test/text_shaping/test_text_shaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
FONTS_DIR = HERE.parent / "fonts"


def test_indi_text(tmp_path):
# issue #365
def test_indi_text(tmp_path): # issue #365
pdf = FPDF()
pdf.add_page()
pdf.add_font(family="Mangal", fname=HERE / "Mangal 400.ttf")
Expand Down Expand Up @@ -55,8 +54,7 @@ def test_text_replacement(tmp_path):
assert_pdf_equal(pdf, HERE / "text_replacement.pdf", tmp_path)


def test_kerning(tmp_path):
# issue #812
def test_kerning(tmp_path): # issue #812
pdf = FPDF()
pdf.add_page()
pdf.add_font(family="Dumbledor3Thin", fname=HERE / "Dumbledor3Thin.ttf")
Expand All @@ -70,8 +68,7 @@ def test_kerning(tmp_path):
assert_pdf_equal(pdf, HERE / "kerning.pdf", tmp_path)


def test_hebrew_diacritics(tmp_path):
# issue #549
def test_hebrew_diacritics(tmp_path): # issue #549
pdf = FPDF()
pdf.add_page()
pdf.add_font(family="SBL_Hbrw", fname=HERE / "SBL_Hbrw.ttf")
Expand Down Expand Up @@ -99,8 +96,7 @@ def test_ligatures(tmp_path):
assert_pdf_equal(pdf, HERE / "ligatures.pdf", tmp_path)


def test_arabic_right_to_left(tmp_path):
# issue #549
def test_arabic_right_to_left(tmp_path): # issue #549
pdf = FPDF()
pdf.add_page()
pdf.add_font(
Expand Down Expand Up @@ -171,8 +167,7 @@ def test_text_with_parentheses(tmp_path):
assert_pdf_equal(pdf, HERE / "text_with_parentheses.pdf", tmp_path)


def test_text_shaping_and_offset_rendering(tmp_path):
# issue #1075
def test_text_shaping_and_offset_rendering(tmp_path): # issue #1075
pdf = FPDF()
pdf.add_font("Garuda", fname=FONTS_DIR / "Garuda.ttf")
pdf.set_font("Garuda", size=16)
Expand Down Expand Up @@ -365,3 +360,22 @@ def test_unicode_script_codes():
for index, char in enumerate(char_list):
assert get_unicode_script(char) == UnicodeScript(index)
get_unicode_script.cache_clear()


def test_disabling_text_shaping(tmp_path): # issue #1287
pdf = FPDF()
pdf.add_font(fname=FONTS_DIR / "Garuda.ttf")
pdf.set_font("Garuda", size=24)
pdf.add_page()
assert not pdf.text_shaping
pdf.write(text="Pages {nb}")
pdf.ln()
pdf.set_text_shaping(True)
assert pdf.text_shaping
pdf.write(text="Pages {nb}")
pdf.ln()
pdf.set_text_shaping(False)
assert not pdf.text_shaping
print(f"{pdf.text_shaping=}")
pdf.write(text="Pages {nb}")
assert_pdf_equal(pdf, HERE / "disabling_text_shaping.pdf", tmp_path)

0 comments on commit c6e230d

Please sign in to comment.