Skip to content

Conversation

@BharathPESU
Copy link

@BharathPESU BharathPESU commented Nov 4, 2025

e.g. Fixes #0

Checklist:

  • A unit test is covering the code added / modified by this PR

  • In case of a new feature, docstrings have been added, with also some documentation in the docs/ folder

  • A mention of the change is present in CHANGELOG.md

  • This PR is ready to be merged

By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.

@BharathPESU
Copy link
Author

inform me if there is any problem

@andersonhc andersonhc changed the title Fix contextmanager imports Support WOFF and WOFF2 fonts Nov 5, 2025
Copy link
Collaborator

@andersonhc andersonhc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR.
I like the solution you found for harfbuzz.
Did you try producing a PDF with a WOFF font? Does the subset at output already decompress the font without any additional parameter?
Please create some tests on test/fonts/test_add_fonts.py. You can use WOFF and WOFF2 fonts, with and without text shaping.
Let us know if you have any question.

Comment on lines 26 to 31
if TYPE_CHECKING: # Help static type checkers / language servers locate optional deps
try: # pragma: no cover - typing-only
import uharfbuzz as hb # type: ignore
except Exception:
hb = None # type: ignore

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you added this block of code, since right below the code already does the exact same thing (import uharfbuzz as hb or default to None if there is an error)

fpdf/fpdf.py Outdated
Comment on lines 48 to 57
if TYPE_CHECKING: # Help static type checkers / language servers locate optional deps
try: # pragma: no cover - typing-only
import endesive # type: ignore
except Exception:
pass
try: # pragma: no cover - typing-only
import uharfbuzz # type: ignore
except Exception:
pass

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will soon add the optional dependencies on the pyproject.toml
I am not convinced this block of code is really needed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok ,I will try to fix it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir I am just beginner for contributing for open source, if any mistakes tell me I will try fix that

- fpdf/fonts.py:
  * Added explanatory comment for TYPE_CHECKING block clarifying it's for static type checkers only
  * Optimized hbfont property to avoid performance hit for non-WOFF fonts by checking file extension
  * For WOFF/WOFF2: uses byte buffer decompression (required for HarfBuzz)
  * For TTF/OTF: loads directly from file path (faster, no extra serialization)
  * Removed invalid fallback for WOFF/WOFF2 that would fail anyway
  * Added logging for failed temp file cleanup to track orphaned files

- fpdf/fpdf.py:
  * Removed unnecessary TYPE_CHECKING block for optional dependencies
  * Fixed table() method return type from Iterator[Table] to ContextManager[Table]
Copy link
Author

@BharathPESU BharathPESU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have changed some part of code,review it and inform me ,if any changes required

@andersonhc
Copy link
Collaborator

i have changed some part of code,review it and inform me ,if any changes required

Can you please create some tests on test/fonts/test_add_fonts.py?
I suggest downloading at lest the WOFF and WOFF2 versions of NOTO (it's OFL licensed) and place them on test/fonts:

  • https://gwfh.mranftl.com/api/fonts/noto-sans?download=zip&subsets=latin&variants=regular&formats=woff2,woff

Look the test_add_font_otf() method on test_add_fonts.py. Create new methods test_add_font_woff() and test_add_font_woff2() - they can look the same as test_add_font_otf() except you can add only style="" and you don't need to have markdown on your text
Change the assert_pdf_equal() call adding generate=True so the test suite will generate the reference PDF for you.

Comment on lines +318 to +321
# If the user passed a WOFF2 file but brotli is not installed, fontTools
# raises an ImportError/RuntimeError during parsing. Provide a clearer hint
# only for that specific situation. Allow other exceptions (e.g. FileNotFoundError,
# OSError, parsing errors) to propagate normally so they aren't masked here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you manage to create a unit test for this case, please?

@andersonhc
Copy link
Collaborator

Hey @BharathPESU
Just checking in to see if you are still interested in working on this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants