Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita0278f6

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 listedin the file (see section 2.2 of Type1 reference linked below); thischarmap needed when decoding dvi files for usetex mode (as dvi representglyphs with these indices). Usually, this charmap is tagged as"ADOBE_STANDARD" or "ADOBE_CUSTOM", which is the heuristic we previouslyused 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'sType1-specific API. (The choice to return an mapping of Type1 indicesto FreeType-internal indices, rather than Type1 indices to glyph names,is motivated by upcoming changes for {xe,lua}tex support, which also useFreeType-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 cancontain arbitrary ligatures that are not representable in unicode),which is (one of the reasons) why fonts provide their own indexingmethods.
1 parent7d5d027 commita0278f6

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
importfontTools.agl
11331133

11341134
frommatplotlib.ft2fontimportFT2Font
1135-
frommatplotlib.textpathimportTextToPath
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
fortextingroup:
11611159
ifpsfont.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
ifpage.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
fortextinpage.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
elifisinstance(glyph_name_or_index,int):
246-
self._select_native_charmap(font)
247+
iffontnotint1_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
raiseTypeError(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-
forcharmap_codein [
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<unsignedchar>(dims, im.get_buffer());
14321432
}
14331433

1434+
constchar *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[]>{newchar[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
staticconstchar *
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)

[8]ページ先頭

©2009-2025 Movatter.jp