Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45a1654

Browse files
committed
Clean up some ttconv replacements
De-wrap a few lines now that we allow longer lines.
1 parent 9fa0b2b commit 45a1654

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,9 +1270,7 @@ def embedTTFType42(font, characters, descriptor):
12701270

12711271
subset_str = "".join(chr(c) for c in characters)
12721272
_log.debug("SUBSET %s characters: %s", filename, subset_str)
1273-
with _backend_pdf_ps.get_glyphs_subset(
1274-
filename, subset_str
1275-
) as subset:
1273+
with _backend_pdf_ps.get_glyphs_subset(filename, subset_str) as subset:
12761274
fontdata = _backend_pdf_ps.font_as_file(subset)
12771275
_log.debug(
12781276
"SUBSET %s %d -> %d", filename,

lib/matplotlib/backends/backend_ps.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ def _font_to_ps_type42(font_path, chars, fh):
191191
# https://github.com/matplotlib/matplotlib/issues/3135#issuecomment-571085541
192192
if font_path.endswith('.ttc'):
193193
kw['fontNumber'] = 0
194-
with fontTools.ttLib.TTFont(font_path, **kw) as font, \
195-
_backend_pdf_ps.get_glyphs_subset(font_path, subset_str) as subset:
194+
with (fontTools.ttLib.TTFont(font_path, **kw) as font,
195+
_backend_pdf_ps.get_glyphs_subset(font_path, subset_str) as subset):
196196
fontdata = _backend_pdf_ps.font_as_file(subset).getvalue()
197197
_log.debug(
198198
"SUBSET %s %d -> %d", font_path, os.stat(font_path).st_size,
@@ -201,8 +201,8 @@ def _font_to_ps_type42(font_path, chars, fh):
201201
fh.write(_serialize_type42(font, subset, fontdata))
202202
except RuntimeError:
203203
_log.warning(
204-
"The PostScript backend does not currently "
205-
"support the selected font (%s).", font_path)
204+
"The PostScript backend does not currently support the selected font (%s).",
205+
font_path)
206206
raise
207207

208208

@@ -225,32 +225,33 @@ def _serialize_type42(font, subset, fontdata):
225225
The Type-42 formatted font
226226
"""
227227
version, breakpoints = _version_and_breakpoints(font.get('loca'), fontdata)
228-
post, name = font['post'], font['name']
229-
fmt = textwrap.dedent(f"""
230-
%%!PS-TrueTypeFont-{version[0]}.{version[1]}-{font['head'].fontRevision:.7f}
231-
10 dict begin
232-
/FontType 42 def
233-
/FontMatrix [1 0 0 1 0 0] def
234-
/FontName /{name.getDebugName(6)} def
235-
/FontInfo 7 dict dup begin
236-
/FullName ({name.getDebugName(4)}) def
237-
/FamilyName ({name.getDebugName(1)}) def
238-
/Version ({name.getDebugName(5)}) def
239-
/ItalicAngle {post.italicAngle} def
240-
/isFixedPitch {'true' if post.isFixedPitch else 'false'} def
241-
/UnderlinePosition {post.underlinePosition} def
242-
/UnderlineThickness {post.underlineThickness} def
243-
end readonly def
244-
/Encoding StandardEncoding def
245-
/FontBBox [{' '.join(str(x) for x in _bounds(font))}] def
246-
/PaintType 0 def
247-
/CIDMap 0 def
248-
%s
249-
%s
250-
FontName currentdict end definefont pop
251-
""")
252-
253-
return fmt % (_charstrings(subset), _sfnts(fontdata, subset, breakpoints))
228+
post = font['post']
229+
name = font['name']
230+
chars = _generate_charstrings(subset)
231+
sfnts = _generate_sfnts(fontdata, subset, breakpoints)
232+
return textwrap.dedent(f"""
233+
%%!PS-TrueTypeFont-{version[0]}.{version[1]}-{font['head'].fontRevision:.7f}
234+
10 dict begin
235+
/FontType 42 def
236+
/FontMatrix [1 0 0 1 0 0] def
237+
/FontName /{name.getDebugName(6)} def
238+
/FontInfo 7 dict dup begin
239+
/FullName ({name.getDebugName(4)}) def
240+
/FamilyName ({name.getDebugName(1)}) def
241+
/Version ({name.getDebugName(5)}) def
242+
/ItalicAngle {post.italicAngle} def
243+
/isFixedPitch {'true' if post.isFixedPitch else 'false'} def
244+
/UnderlinePosition {post.underlinePosition} def
245+
/UnderlineThickness {post.underlineThickness} def
246+
end readonly def
247+
/Encoding StandardEncoding def
248+
/FontBBox [{_nums_to_str(*_bounds(font))}] def
249+
/PaintType 0 def
250+
/CIDMap 0 def
251+
{chars}
252+
{sfnts}
253+
FontName currentdict end definefont pop
254+
""")
254255

255256

256257
def _version_and_breakpoints(loca, fontdata):
@@ -321,7 +322,7 @@ def _bounds(font):
321322
return pen.bounds or (0, 0, 0, 0)
322323

323324

324-
def _charstrings(font):
325+
def _generate_charstrings(font):
325326
"""
326327
Transform font glyphs into CharStrings
327328
@@ -345,7 +346,7 @@ def _charstrings(font):
345346
return s
346347

347348

348-
def _sfnts(fontdata, font, breakpoints):
349+
def _generate_sfnts(fontdata, font, breakpoints):
349350
"""
350351
Transform font data into PostScript sfnts format.
351352

0 commit comments

Comments
 (0)