Fix loading of Type1 "native" charmap. · matplotlib/matplotlib@662d675 · GitHub
Skip to content

Commit 662d675

Browse files
committed
Fix loading of Type1 "native" charmap.
Type1 fonts have a "native" charmap (mapping of indices to glyphs (\*)), which is simply the order in which glyphs are physically listed in the file (see section 2.2 of Type1 reference linked below); this charmap needed when decoding dvi files for usetex mode (as dvi represent glyphs with these indices). Usually, this charmap is tagged as "ADOBE_STANDARD" or "ADOBE_CUSTOM", which is the heuristic we previously used to load it, but it is unclear to me whether this is guaranteed (reference section 10.3), and FreeType may supply its own reencodings (it already does so to try to provide a unicode charmap). Instead, directly read and return the encoding vector via FreeType's Type1-specific API. (The choice to return an mapping of Type1 indices to FreeType-internal indices, rather than Type1 indices to glyph names, is motivated by upcoming changes for {xe,lua}tex support, which also use FreeType-internal indices.) Type1 reference: https://adobe-type-tools.github.io/font-tech-notes/pdfs/T1_SPEC.pdf (\*) Not all glyphs correspond to a unicode codepoint (e.g. a font can contain arbitrary ligatures that are not representable in unicode), which is (one of the reasons) why fonts provide their own indexing methods.
1 parent 7d5d027 commit 662d675

3 files changed

Lines changed: 35 additions & 25 deletions

File tree

lib/matplotlib/dviread.py

Lines changed: 2 additions & 4 deletions

lib/matplotlib/textpath.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
232232

233233
# Gather font information and do some setup for combining
234234
# characters into strings.
235+
t1_encodings = {}
235236
for text in page.text:
236237
font = get_font(text.font_path)
237238
char_id = self._get_char_id(font, text.glyph)
@@ -241,14 +242,14 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
241242
glyph_name_or_index = text.glyph_name_or_index
242243
if isinstance(glyph_name_or_index, str):
243244
index = font.get_name_index(glyph_name_or_index)
244-
font.load_glyph(index, flags=LoadFlags.TARGET_LIGHT)
245245
elif isinstance(glyph_name_or_index, int):
246-
self._select_native_charmap(font)
247-
font.load_char(
248-
glyph_name_or_index, flags=LoadFlags.TARGET_LIGHT)
246+
if font not in t1_encodings:
247+
t1_encodings[font] = font._get_type1_encoding_vector()
248+
index = t1_encodings[font][glyph_name_or_index]
249249
else: # Should not occur.
250250
raise TypeError(f"Glyph spec of unexpected type: "
251251
f"{glyph_name_or_index!r}")
252+
font.load_glyph(index, flags=LoadFlags.TARGET_LIGHT)
252253
glyph_map_new[char_id] = font.get_path()
253254

254255
glyph_ids.append(char_id)
@@ -269,23 +270,6 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
269270
return (list(zip(glyph_ids, xpositions, ypositions, sizes)),
270271
glyph_map_new, myrects)
271272

272-
@staticmethod
273-
def _select_native_charmap(font):
274-
# Select the native charmap. (we can't directly identify it but it's
275-
# typically an Adobe charmap).
276-
for charmap_code in [
277-
1094992451, # ADOBE_CUSTOM.
278-
1094995778, # ADOBE_STANDARD.
279-
]:
280-
try:
281-
font.select_charmap(charmap_code)
282-
except (ValueError, RuntimeError):
283-
pass
284-
else:
285-
break
286-
else:
287-
_log.warning("No supported encoding in font (%s).", font.fname)
288-
289273

290274
text_to_path = TextToPath()
291275

src/ft2font_wrapper.cpp

Lines changed: 28 additions & 0 deletions

0 commit comments

Comments
 (0)