The Wayback Machine - https://web.archive.org/web/20240218131239/https://github.com/matplotlib/matplotlib/pull/20866
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

Remove ttconv and implement Type-42 embedding using fontTools #20866

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

jkseppan
Copy link
Member

@jkseppan jkseppan commented Aug 20, 2021

PR Summary

ttconv is now only being used for outputting fonts in Type-42 format in PostScript files, and with fontTools this turns out to be quite doable in pure Python.

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • [N/A] New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@jkseppan
Copy link
Member Author

Now this works with almost all TrueType fonts on my Mac, with the exception of CFF fonts (#20870) and Apple Color Emoji.ttc and NISC18030.ttf both of which cause a crash in FT2Font (#7305).

@jkseppan jkseppan marked this pull request as ready for review August 21, 2021 16:21
@jkseppan jkseppan force-pushed the remove-ttconv branch 2 times, most recently from 016713a to 7cea2d4 Compare August 22, 2021 14:20
@@ -24,28 +25,59 @@ def get_glyphs_subset(fontfile, characters):
Subset a TTF font

Reads the named fontfile and restricts the font to the characters.
Returns a serialization of the subset font as file-like object.
Returns a TTFont object.
Copy link
Member

Choose a reason for hiding this comment

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

Just remove the line; you have a Returns section.

if fontfile.endswith('ttc'):
# fix this once we support loading more fonts from a collection
# https://github.com/matplotlib/matplotlib/issues/3135#issuecomment-571085541
options.font_number = 0
Copy link
Member

Choose a reason for hiding this comment

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

I think we test a ttc in CI already, so it could be used to test this line?

The Type-42 formatted font
"""
version, breakpoints = _version_and_breakpoints(font.get('loca'), fontdata)
post, name = font['post'], font['name']
Copy link
Member

Choose a reason for hiding this comment

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

Are these tables guaranteed to exist?

Comment on lines +296 to +301
Read the version number of the font and determine sfnts breakpoints.
When a TrueType font file is written as a Type 42 font, it has to be
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Read the version number of the font and determine sfnts breakpoints.
When a TrueType font file is written as a Type 42 font, it has to be
Read the version number of the font and determine sfnts breakpoints.
When a TrueType font file is written as a Type 42 font, it has to be

Comment on lines +314 to +322
tuple
((v1, v2), breakpoints) where v1 is the major version number,
v2 is the minor version number and breakpoints is a sorted list
of offsets into fontdata; if loca is not available, just the table
boundaries
Copy link
Member

Choose a reason for hiding this comment

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

For multiple return, you should list the entries separately, like Parameters.

Comment on lines +336 to +342
breakpoints = sorted(
set(tables.values()) | glyf_breakpoints | {len(fontdata)}
)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
breakpoints = sorted(
set(tables.values()) | glyf_breakpoints | {len(fontdata)}
)
breakpoints = sorted({*tables.values(), *glyf_breakpoints, len(fontdata)})

Comment on lines +384 to +394
s = StringIO()
go = font.getGlyphOrder()
s.write(f'/CharStrings {len(go)} dict dup begin\n')
for i, name in enumerate(go):
s.write(f'/{name} {i} def\n')
s.write('end readonly def')
return s.getvalue()
Copy link
Member

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 need StringIO for this, as there is no file IO necessary.

Suggested change
s = StringIO()
go = font.getGlyphOrder()
s.write(f'/CharStrings {len(go)} dict dup begin\n')
for i, name in enumerate(go):
s.write(f'/{name} {i} def\n')
s.write('end readonly def')
return s.getvalue()
go = font.getGlyphOrder()
s = f'/CharStrings {len(go)} dict dup begin\n'
for i, name in enumerate(go):
s += f'/{name} {i} def\n'
s += 'end readonly def'
return s

Comment on lines +414 to +432
b = BytesIO()
b.write(b'/sfnts[')
pos = 0
while pos < len(fontdata):
i = bisect.bisect_left(breakpoints, pos + 65534)
newpos = breakpoints[i-1]
if newpos <= pos:
# have to accept a larger string
newpos = breakpoints[-1]
b.write(b'<')
b.write(binascii.hexlify(fontdata[pos:newpos]))
b.write(b'00>') # need an extra zero byte on every string
pos = newpos
b.write(b']def')
s = b.getvalue().decode('ascii')
Copy link
Member

Choose a reason for hiding this comment

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

Also doesn't appear to need BytesIO:

Suggested change
b = BytesIO()
b.write(b'/sfnts[')
pos = 0
while pos < len(fontdata):
i = bisect.bisect_left(breakpoints, pos + 65534)
newpos = breakpoints[i-1]
if newpos <= pos:
# have to accept a larger string
newpos = breakpoints[-1]
b.write(b'<')
b.write(binascii.hexlify(fontdata[pos:newpos]))
b.write(b'00>') # need an extra zero byte on every string
pos = newpos
b.write(b']def')
s = b.getvalue().decode('ascii')
s = '/sfnts['
pos = 0
while pos < len(fontdata):
i = bisect.bisect_left(breakpoints, pos + 65534)
newpos = breakpoints[i-1]
if newpos <= pos:
# have to accept a larger string
newpos = breakpoints[-1]
s += f'<{fontdata[pos:newpos].hex()}00>' # Always NULL terminate.
pos = newpos
s += ']def'

Comment on lines +240 to +261
fontfiles = (font_manager.findfont(fp) for fp in fps)
if len(set(fontfiles)) < 6:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fontfiles = (font_manager.findfont(fp) for fp in fps)
if len(set(fontfiles)) < 6:
fontfiles = {font_manager.findfont(fp) for fp in fps}
if len(fontfiles) < 6:

@tacaswell
Copy link
Member

Wow, I missed that this came in. 💯 on dropping an extension module!

@jklymak jklymak marked this pull request as draft February 1, 2022 11:52
@jklymak
Copy link
Member

jklymak commented Feb 1, 2022

Dropping to draft until the comments and rebase are addressed. Anyone should feel free to move back to active...

@QuLogic
Copy link
Member

QuLogic commented Mar 15, 2022

Ping @jkseppan?


Returns
-------
fontTools.ttLib.ttFont.TTFont
Copy link
Contributor

@oscargus oscargus Mar 15, 2022

Choose a reason for hiding this comment

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

Long term, it could make sense to link to the documentation? (Although fontTools is not in the intersphinx_mapping yet.)

@QuLogic QuLogic modified the milestones: v3.6.0, v3.7.0 Jul 5, 2022
@tacaswell tacaswell modified the milestones: v3.7.0, v3.8.0 Dec 9, 2022
@tacaswell
Copy link
Member

We want to try and do mpl 3.7 feature freeze around Jan 1 so I think it is unlikely we will have the bandwidth to get this rebased and reviewed by then, pushing to 3.8.

Move the test in lib/matplotlib/tests/test_ttconv.py to
lib/matplotlib/tests/test_backend_pdf.py. Actually it has not been
a test of ttconv for a while, since type-3 conversion has not been
using ttconv.
Split _backend_pdf_ps.get_glyphs_subset into two functions:
get_glyphs_subset returns the font object (which needs to be
closed after using) and font_as_file serializes this object
into a BytesIO file-like object.
Fonttools cannot subset these and drops them, avoid the warning
Also don't end up in an infinite loop if there is a larger gap
between breakpoints than we would like.
FT2Font throws RuntimeError for some fonts
Not the subsetted one, since in some cases fontTools.subset
does not produce a good name table.

Import functions from _backend_pdf_ps to help keep lines
shorter.
@jklymak
Copy link
Member

jklymak commented Feb 21, 2023

Do we have someone who can pick this up?

@QuLogic QuLogic linked an issue Jun 7, 2023 that may be closed by this pull request
@ksunden ksunden modified the milestones: v3.8.0, future releases Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

replace ttconv for ps/pdf
6 participants