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

Restoring ability to disable text shaping - fix #1287 #1288

Merged
merged 1 commit into from
Oct 17, 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
3 changes: 0 additions & 3 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ e.g. Fixes #0 <!-- This will automatically close issue #0 once the PR is merged:
<!-- To check an item, place an "x" in the box like so: "- [x] Item description"
Add "N/A" to the end of each line that's irrelevant to your changes -->

- [ ] The GitHub pipeline is OK (green), <!-- The maintainers will trigger it if this is your 1st contribution -->
meaning that both `pylint` (static code analyzer) and `black` (code formatter) are happy with the changes of this PR.

- [ ] A unit test is covering the code added / modified by this PR

- [ ] This PR is ready to be merged <!-- In your opinion, can this be merged as soon as it's reviewed? Else, this can be turned into a Draft PR -->
Expand Down
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)
Loading