Skip to content

Support WOFF and WOFF2 fonts#1660

Merged
andersonhc merged 7 commits intopy-pdf:masterfrom
BharathPESU:fix-contextmanager-imports
Jan 26, 2026
Merged

Support WOFF and WOFF2 fonts#1660
andersonhc merged 7 commits intopy-pdf:masterfrom
BharathPESU:fix-contextmanager-imports

Conversation

@BharathPESU
Copy link
Copy Markdown

@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
Copy Markdown
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
Copy Markdown
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 thread fpdf/fonts.py Outdated
Comment thread fpdf/fonts.py Outdated
Comment thread fpdf/fpdf.py Outdated
Copy link
Copy Markdown
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
Copy Markdown
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 thread fpdf/fonts.py
@andersonhc
Copy link
Copy Markdown
Collaborator

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

BharathPESU and others added 5 commits January 26, 2026 10:45
- 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]
@andersonhc andersonhc force-pushed the fix-contextmanager-imports branch from db7c521 to 3a187ee Compare January 26, 2026 17:56
@andersonhc
Copy link
Copy Markdown
Collaborator

I didn't have an answer in over 3 months, so I am completing the necessary changes to merge this PR.

@andersonhc andersonhc merged commit 893ed2d into py-pdf:master Jan 26, 2026
23 checks passed
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