8000 Fix loading of Type1 "native" charmap. · matplotlib/matplotlib@a0278f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit a0278f6

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 a0278f6

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

lib/matplotlib/dviread.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,6 @@ def _fontfile(cls, suffix, texname):
11321132
import fontTools.agl
11331133

11341134
from matplotlib.ft2font import FT2Font
1135-
from matplotlib.textpath import TextToPath
11361135

11371136
parser = ArgumentParser()
11381137
parser.add_argument("filename")
@@ -1155,14 +1154,13 @@ def _print_fields(*args):
11551154
print(f"font: {font.texname.decode('latin-1')} "
11561155
f"(scale: {font._scale / 2 ** 20}) at {fontpath}")
11571156
face = FT2Font(fontpath)
1158-
TextToPath._select_native_charmap(face)
11591157
_print_fields("x", "y", "glyph", "chr", "w")
11601158
for text in group:
11611159
if psfont.encoding:
11621160
glyph_name = _parse_enc(psfont.encoding)[text.glyph]
11631161
else:
1164-
glyph_name = face.get_glyph_name(
1165-
face.get_char_index(text.glyph))
1162+
encoding_vector = face._get_type1_encoding_vector()
1163+
glyph_name = face.get_glyph_name(encoding_vector[text.glyph])
11661164
glyph_str = fontTools.agl.toUnicode(glyph_name)
11671165
_print_fields(text.x, text.y, text.glyph, glyph_str, text.width)
11681166
if page.boxes:

lib/matplotlib/textpath.py

Lines changed: 5 additions & 19 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)
@@ -243,9 +244,11 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
243244
index = font.get_name_index(glyph_name_or_index)
244245
font.load_glyph(index, flags=LoadFlags.TARGET_LIGHT)
245246
elif isinstance(glyph_name_or_index, int):
246-
self._select_native_charmap(font)
247+
if font not in t1_encodings:
248+
t1_encodings[font] = font._get_type1_encoding_vector()
247249
font.load_char(
248-
glyph_name_or_index, flags=LoadFlags.TARGET_LIGHT)
250+
t1_encodings[font][glyph_name_or_index],
251+
flags=LoadFlags.TARGET_LIGHT)
249252
else: # Should not occur.
250253
raise TypeError(f"Glyph spec of unexpected type: "
251254
f"{glyph_name_or_index!r}")
@@ -269,23 +272,6 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
269272
return (list(zip(glyph_ids, xpositions, ypositions, sizes)),
270273
glyph_map_new, myrects)
271274

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-
289275

290276
text_to_path = TextToPath()
291277

src/ft2font_wrapper.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,32 @@ PyFT2Font_get_image(PyFT2Font *self)
14311431
return py::array_t<unsigned char>(dims, im.get_buffer());
14321432
}
14331433

1434+
const char *PyFT2Font__get_type1_encoding_vector__doc__ = R"""(
1435+
Return a list mapping CharString indices of a Type 1 font to FreeType glyph indices.
1436+
1437+
Returns
1438+
-------
1439+
list[int]
1440+
)""";
1441+
1442+
static std::array<FT_UInt, 256>
1443+
PyFT2Font__get_type1_encoding_vector(PyFT2Font *self)
1444+
{
1445+
auto face = self->x->get_face();
1446+
auto indices = std::array<FT_UInt, 256>{};
1447+
for (auto i = 0; i < indices.size(); ++i) {
1448+
auto len = FT_Get_PS_Font_Value(face, PS_DICT_ENCODING_ENTRY, i, nullptr, 0);
1449+
if (len == -1) {
1450+
throw std::runtime_error{
1451+
"FT_Get_PS_Font_Value tried to access a non-existent value"};
1452+
}
1453+
auto buf = std::unique_ptr<char[]>{new char[len]};
1454+
FT_Get_PS_Font_Value(face, PS_DICT_ENCODING_ENTRY, i, buf.get(), len);
1455+
indices[i] = FT_Get_Name_Index(face, buf.get());
1456+
}
1457+
return indices;
1458+
}
1459+
14341460
static const char *
14351461
PyFT2Font_postscript_name(PyFT2Font *self)
14361462
{
@@ -1761,6 +1787,8 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
17611787
PyFT2Font_get_sfnt_table__doc__)
17621788
.def("get_path", &PyFT2Font_get_path, PyFT2Font_get_path__doc__)
17631789
.def("get_image", &PyFT2Font_get_image, PyFT2Font_get_image__doc__)
1790+
.def("_get_type1_encoding_vector", &PyFT2Font__get_type1_encoding_vector,
1791+
PyFT2Font__get_type1_encoding_vector__doc__)
17641792

17651793
.def_property_readonly("postscript_name", &PyFT2Font_postscript_name,
17661794
"PostScript name of the font.")

0 commit comments

Comments
 (0)
0