Skip to content

Commit 263f4e2

Browse files
committed
Support luatex-generated dvi.
To be used together with the same-named branch on Matplotlib.
1 parent f7e5e35 commit 263f4e2

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

ext/_mplcairo.cpp

+21-16
Original file line numberDiff line numberDiff line change
@@ -1847,25 +1847,30 @@ void MathtextBackend::draw(
18471847
return FT_Get_Name_Index(ft_face, name.data());
18481848
},
18491849
[&](FT_ULong idx) {
1850-
// For classic fonts, the index maps to the "native" font charmap,
1851-
// which typically has an ADOBE_STANDARD or ADOBE_CUSTOM encoding,
1852-
// unlike the FreeType-synthesized one which has a UNICODE encoding.
1853-
auto found = false;
1854-
for (auto i = 0; i < ft_face->num_charmaps; ++i) {
1855-
auto const& cmap = ft_face->charmaps[i];
1856-
if (cmap->encoding == FT_ENCODING_ADOBE_STANDARD
1857-
|| cmap->encoding == FT_ENCODING_ADOBE_CUSTOM) {
1858-
if (found) {
1859-
throw std::runtime_error{"multiple Adobe charmaps found"};
1850+
if (FT_IS_SFNT(ft_face)) {
1851+
// For OpenType fonts, luatex directly outputs glyph indices.
1852+
return FT_UInt(idx);
1853+
} else {
1854+
// For classic fonts, the index maps to the "native" font charmap,
1855+
// which typically has an ADOBE_STANDARD or ADOBE_CUSTOM encoding,
1856+
// unlike the FreeType-synthesized one which has a UNICODE encoding.
1857+
auto found = false;
1858+
for (auto i = 0; i < ft_face->num_charmaps; ++i) {
1859+
auto const& cmap = ft_face->charmaps[i];
1860+
if (cmap->encoding == FT_ENCODING_ADOBE_STANDARD
1861+
|| cmap->encoding == FT_ENCODING_ADOBE_CUSTOM) {
1862+
if (found) {
1863+
throw std::runtime_error{"multiple Adobe charmaps found"};
1864+
}
1865+
FT_CHECK(FT_Set_Charmap, ft_face, cmap);
1866+
found = true;
18601867
}
1861-
FT_CHECK(FT_Set_Charmap, ft_face, cmap);
1862-
found = true;
18631868
}
1869+
if (!found) {
1870+
throw std::runtime_error{"no Adobe charmap found"};
1871+
}
1872+
return FT_Get_Char_Index(ft_face, idx);
18641873
}
1865-
if (!found) {
1866-
throw std::runtime_error{"no builtin charmap found"};
1867-
}
1868-
return FT_Get_Char_Index(ft_face, idx);
18691874
}
18701875
}, glyph.codepoint_or_name_or_index);
18711876
if (!index) {

src/mplcairo/base.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,24 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!", mtext=None):
142142
page = next(iter(dvi))
143143
mb = _mplcairo.MathtextBackendCairo()
144144
for text in page.text:
145-
texfont = _util.get_tex_font_map()[text.font.texname]
146-
if texfont.filename is None:
147-
# Not TypeError:
148-
# :mpltest:`test_backend_svg.test_missing_psfont`.
149-
raise ValueError(f"No font file found for {texfont.psname} "
150-
f"({texfont.texname!a})")
151-
mb.add_usetex_glyph(
152-
text.x, -text.y,
153-
texfont.filename, text.font.size,
154-
_util.get_glyph_name(text) or text.glyph,
155-
texfont.effects.get("slant", 0),
156-
texfont.effects.get("extend", 1))
145+
if text.font.texname.startswith(b"["):
146+
mb.add_usetex_glyph(
147+
text.x, -text.y,
148+
text.font.texname[1:-1].decode("latin-1"), text.font.size,
149+
text.glyph, 0, 1)
150+
else:
151+
texfont = _util.get_tex_font_map()[text.font.texname]
152+
if texfont.filename is None:
153+
# Not TypeError:
154+
# :mpltest:`test_backend_svg.test_missing_psfont`.
155+
raise ValueError(f"No font file found for {texfont.psname} "
156+
f"({texfont.texname!a})")
157+
mb.add_usetex_glyph(
158+
text.x, -text.y,
159+
texfont.filename, text.font.size,
160+
_util.get_glyph_name(text) or text.glyph,
161+
texfont.effects.get("slant", 0),
162+
texfont.effects.get("extend", 1))
157163
for x1, y1, h, w in page.boxes:
158164
mb.add_rect(x1, -y1, x1 + w, -(y1 + h))
159165
mb.draw(self, x, y, angle)

0 commit comments

Comments
 (0)