-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Type42 subsetting in PS/PDF #20391
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
Type42 subsetting in PS/PDF #20391
Conversation
I'm Mr. Meeseek, @aitikgupta, Look at meee ! |
The last commit subsets and embeds Type 42 PS/EPS, here's the size difference in outputs:
(To test it out on your machine, apply this patch for diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py
index 2a3ab64a1c..ed0af4ea85 100644
--- a/lib/matplotlib/backends/backend_ps.py
+++ b/lib/matplotlib/backends/backend_ps.py
@@ -997,12 +997,8 @@ class FigureCanvasPS(FigureCanvasBase):
) as tmp:
tmp.write(fontdata)
tmp.seek(0, 0)
- font = FT2Font(tmp.name)
- glyph_ids = [
- font.get_char_index(c) for c in chars
- ]
convert_ttf_to_ps(
- os.fsencode(tmp.name),
+ os.fsencode(font_path),
fh,
fonttype,
glyph_ids, The script: import matplotlib.pyplot as plt
plt.rcParams["ps.fonttype"] = 42
plt.figtext(0.5, 0.5, "hello")
# plt.savefig('sub-dejavu.ps') # before applying the patch
# plt.savefig('nosub-dejavu.ps') # after applying the patch |
I think it's difficult to test Is there any other way to test this? edit: @jklymak this isn't a draft anymore, just needed to address review comments 😄 |
You can mark as ready-to-review at any point. I just kick things to Draft so they don't show up in the queue if the PR requires action on the part of the author. (this one is still not passing the tests). |
Yeah, CI isn't installing fonttools, which is why |
Install is done with |
We ship DejaVu ourselves, so that should always be available for testing.
A file with just 'A' embedded should be smaller than one with the full alphabet, say? |
Oh, if we just test the subset being 'smaller' yeah we can do it, but not with the exact number glyphs, since that will definitely vary. (we also set But even so, isn't that the 'full-time job' of the fonttools library itself? or in other words wouldn't testing the |
Oh, and one more thing (sorry for not remembering this earlier): the PDF specification has some extra requirements for font subsets in section 9.6.4. The PostScript name of subsetted fonts needs to have a prepended tag of six random uppercase letters and a plus sign, e.g. |
I think we don't do this even for Type 3 subsetting? |
4974261
to
2702656
Compare
This breaks the matplotlib/lib/matplotlib/tests/test_determinism.py Lines 85 to 88 in 593de35
This is for obvious reasons, since we have random string inside font postscript name. (due to the specification) |
Then the string shouldn't be random, but perhaps something derived from the subset of glyphs? Take The PDF specification only requires the tag for Type 1 and TrueType fonts. It probably doesn't hurt with Type 3 fonts either. |
That totally makes sense! Let me try this out 👍🏼 |
@sauerburger can you suggest a test for that, or is it already in #20633? |
The test is already in, and that is exactly what is breaking the CI for this PR. |
42f57bb
to
85f4377
Compare
It seems to me that the requested changes have been made. |
PR Summary
This PR is a fresh rebase from #18143.
fonttools
to handle font subsetting for us(we already have an external ttconv dependency, which does not handle subsetting)
getSubset
utility to get file-like objects containing subsetted font dataPossibly fixes #11303 (large file sizes)
Fixes #18191.
PR Checklist
pytest
passes).flake8
on changed files to check).flake8-docstrings
and runflake8 --docstring-convention=all
).doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).