-
Notifications
You must be signed in to change notification settings - Fork 269
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: Only include fonts effectively used in the final PDF document #1387
base: master
Are you sure you want to change the base?
Conversation
hi @Lucas-C thanks for your detailed instruction and this is the PR. I cannot add reviewer so I could only mention you here. Thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are several problems with modified documents:
- text disapeared in
test/barcodes/barcodes_code39.pdf
- card sizes changed in
test/fonts/fonts_emoji_glyph.pdf
- text disapeared in
test/fonts/fonts_set_builtin_font.pdf
- first text line in black disapeared in
test/free_text_annotation_all_parameters.pdf
- etc.
This must be fixed before merging this PR.
fpdf/fpdf.py
Outdated
@@ -3427,6 +3415,7 @@ def _render_styled_text_line( | |||
self._resource_catalog.add( | |||
PDFResourceType.FONT, current_font.i, self.page | |||
) | |||
current_font_is_added = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shoudn't that be reset to False at some point?
What happens when a text fragment with a different font is parsed?
Is it correctly added to the content stream?
reproduce.py
Outdated
@@ -0,0 +1 @@ | |||
a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This script should be removed from the PR
test/linearization.pdf
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should not be added.
The corresponding unit test fail has @pytest.mark.xfail
, it should not produce a PDF reference file.
Could you rebase your branch on this repo Once you have done that, you could check PDF differences using |
…et_font_size_for_page and use in needed places
@Lucas-C thanks for your quick review. I have changed my code. def set_font_size_for_page(self):
"""
Set font and size for current page. This step is needed before adding text into page and not needed in set_font and set_font_size.
"""
if self.page > 0 and self.current_font and not self.font_size_set_for_page:
self._out(f"BT /F{self.current_font.i} {self.font_size_pt:.2f} Tf ET")
self._resource_catalog.add(
PDFResourceType.FONT, self.current_font.i, self.page
)
self.font_size_set_for_page = True This method should be only called when My assumption about font is simple: In a page, if a <font,size> is set with method However, after I refresh all unit test pdfs. I noticed a lot of difference in
Also some text disappeared in test/html/html_blockquote_indent.pdf and test/cell_skew_text.pdf
I think my assumption about font is incorrect. In a page, after a <font,size> is set, when some code is added, the font will disappear. But I don't know what exactly made the font gone. If I know, I could add |
I added a fix but these files still miss text, let me try to fix them: |
Just confirmed all pdfs are the same as master branch. Let me double check all code is good. |
Co-authored-by: Lucas Cimon <[email protected]>
self._push_local_stack() | ||
self.add_page(same=True) | ||
self._pop_local_stack() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
previously, the sequence looks quite confusing:
pop_gs -> push_local -> add_page -> push_gs -> pop_local
so here I changed it to correct sequence:
pop_gs -> push_local -> add_page -> pop_local -> push_gs
This makes code more readable
@Lucas-C
@Lucas-C I have checked the pdfs with the script you mentioned. I think the code looks much better now. |
Thank you for all your work on this @yuyiz67 👍 I may not have the time to review until Friday, |
@Lucas-C Thanks for running workflow. I tried to run it locally and found error :
I will fix all errors and let you know after that. |
Font Optimization: Only include fonts effectively used in the final PDF document. Fonts added via set_font() or add_page() but not actually used in the document are no longer included in the final output. This reduces file size and improves performance, especially for documents with many fallback fonts
Fixes Issue #1382
Checklist:
A unit test is covering the code added / modified by this PR
This PR is ready to be merged
In case of a new feature, docstrings have been added, with also some documentation in the
docs/
folderA mention of the change is present in
CHANGELOG.md
By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.