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

Skip to content

Fix loading of Type1 "native" charmap. #29843

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

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,6 @@ def _fontfile(cls, suffix, texname):
import fontTools.agl

from matplotlib.ft2font import FT2Font
from matplotlib.textpath import TextToPath

parser = ArgumentParser()
parser.add_argument("filename")
Expand All @@ -1155,14 +1154,13 @@ def _print_fields(*args):
print(f"font: {font.texname.decode('latin-1')} "
f"(scale: {font._scale / 2 ** 20}) at {fontpath}")
face = FT2Font(fontpath)
TextToPath._select_native_charmap(face)
_print_fields("x", "y", "glyph", "chr", "w")
for text in group:
if psfont.encoding:
glyph_name = _parse_enc(psfont.encoding)[text.glyph]
else:
glyph_name = face.get_glyph_name(
face.get_char_index(text.glyph))
encoding_vector = face._get_type1_encoding_vector()
glyph_name = face.get_glyph_name(encoding_vector[text.glyph])
glyph_str = fontTools.agl.toUnicode(glyph_name)
_print_fields(text.x, text.y, text.glyph, glyph_str, text.width)
if page.boxes:
Expand Down
26 changes: 5 additions & 21 deletions lib/matplotlib/textpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,

# Gather font information and do some setup for combining
# characters into strings.
t1_encodings = {}
for text in page.text:
font = get_font(text.font_path)
char_id = self._get_char_id(font, text.glyph)
Expand All @@ -241,14 +242,14 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
glyph_name_or_index = text.glyph_name_or_index
if isinstance(glyph_name_or_index, str):
index = font.get_name_index(glyph_name_or_index)
font.load_glyph(index, flags=LoadFlags.TARGET_LIGHT)
elif isinstance(glyph_name_or_index, int):
self._select_native_charmap(font)
font.load_char(
glyph_name_or_index, flags=LoadFlags.TARGET_LIGHT)
if font not in t1_encodings:
t1_encodings[font] = font._get_type1_encoding_vector()
index = t1_encodings[font][glyph_name_or_index]
else: # Should not occur.
raise TypeError(f"Glyph spec of unexpected type: "
f"{glyph_name_or_index!r}")
font.load_glyph(index, flags=LoadFlags.TARGET_LIGHT)
glyph_map_new[char_id] = font.get_path()

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

@staticmethod
def _select_native_charmap(font):
# Select the native charmap. (we can't directly identify it but it's
# typically an Adobe charmap).
for charmap_code in [
1094992451, # ADOBE_CUSTOM.
1094995778, # ADOBE_STANDARD.
]:
try:
font.select_charmap(charmap_code)
except (ValueError, RuntimeError):
pass
else:
break
else:
_log.warning("No supported encoding in font (%s).", font.fname)


text_to_path = TextToPath()

Expand Down
48 changes: 38 additions & 10 deletions src/ft2font_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,16 @@
return self;
}

static py::str
PyFT2Font_fname(PyFT2Font *self)
{
if (self->stream.close) { // Called passed a filename to the constructor.
return self->py_file.attr("name");
} else {
return py::cast<py::str>(self->py_file);

Check warning on line 507 in src/ft2font_wrapper.cpp

View check run for this annotation

Codecov / codecov/patch

src/ft2font_wrapper.cpp#L507

Added line #L507 was not covered by tests
}
}

const char *PyFT2Font_clear__doc__ =
"Clear all the glyphs, reset for a new call to `.set_text`.";

Expand Down Expand Up @@ -1431,6 +1441,32 @@
return py::array_t<unsigned char>(dims, im.get_buffer());
}

const char *PyFT2Font__get_type1_encoding_vector__doc__ = R"""(
Return a list mapping CharString indices of a Type 1 font to FreeType glyph indices.

Returns
-------
list[int]
)""";

static std::array<FT_UInt, 256>
PyFT2Font__get_type1_encoding_vector(PyFT2Font *self)
{
auto face = self->x->get_face();
auto indices = std::array<FT_UInt, 256>{};
for (auto i = 0u; i < indices.size(); ++i) {
auto len = FT_Get_PS_Font_Value(face, PS_DICT_ENCODING_ENTRY, i, nullptr, 0);
if (len == -1) {
// Explicitly ignore missing entries (mapped to glyph 0 = .notdef).
continue;
}
auto buf = std::make_unique<char[]>(len);
FT_Get_PS_Font_Value(face, PS_DICT_ENCODING_ENTRY, i, buf.get(), len);
indices[i] = FT_Get_Name_Index(face, buf.get());
}
return indices;
}

static const char *
PyFT2Font_postscript_name(PyFT2Font *self)
{
Expand Down Expand Up @@ -1569,16 +1605,6 @@
return self->x->get_face()->underline_thickness;
}

static py::str
PyFT2Font_fname(PyFT2Font *self)
{
if (self->stream.close) { // Called passed a filename to the constructor.
return self->py_file.attr("name");
} else {
return py::cast<py::str>(self->py_file);
}
}

static py::object
ft2font__getattr__(std::string name) {
auto api = py::module_::import("matplotlib._api");
Expand Down Expand Up @@ -1761,6 +1787,8 @@
PyFT2Font_get_sfnt_table__doc__)
.def("get_path", &PyFT2Font_get_path, PyFT2Font_get_path__doc__)
.def("get_image", &PyFT2Font_get_image, PyFT2Font_get_image__doc__)
.def("_get_type1_encoding_vector", &PyFT2Font__get_type1_encoding_vector,
PyFT2Font__get_type1_encoding_vector__doc__)

.def_property_readonly("postscript_name", &PyFT2Font_postscript_name,
"PostScript name of the font.")
Expand Down
Loading