Skip to content

Commit 501414b

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

File tree

3 files changed

+14
-49
lines changed

3 files changed

+14
-49
lines changed

ext/_mplcairo.cpp

+7-35
Original file line numberDiff line numberDiff line change
@@ -1767,11 +1767,11 @@ void GraphicsContextRenderer::restore_region(Region& region)
17671767

17681768
MathtextBackend::Glyph::Glyph(
17691769
std::string path, double size,
1770-
std::variant<char32_t, std::string, FT_ULong> codepoint_or_name_or_index,
1770+
std::variant<char32_t, FT_ULong> codepoint_or_index,
17711771
double x, double y,
17721772
double slant, double extend) :
17731773
path{path}, size{size},
1774-
codepoint_or_name_or_index{codepoint_or_name_or_index},
1774+
codepoint_or_index{codepoint_or_index},
17751775
x{x}, y{y},
17761776
slant{slant}, extend{extend}
17771777
{}
@@ -1786,16 +1786,9 @@ void MathtextBackend::add_glyph(
17861786

17871787
void MathtextBackend::add_usetex_glyph(
17881788
double ox, double oy, std::string filename, double size,
1789-
std::variant<std::string, FT_ULong> name_or_index,
1790-
double slant, double extend)
1789+
FT_ULong index, double slant, double extend)
17911790
{
1792-
auto codepoint_or_name_or_index =
1793-
std::variant<char32_t, std::string, FT_ULong>{};
1794-
std::visit(
1795-
[&](auto name_or_index) { codepoint_or_name_or_index = name_or_index; },
1796-
name_or_index);
1797-
glyphs_.emplace_back(
1798-
filename, size, codepoint_or_name_or_index, ox, oy, slant, extend);
1791+
glyphs_.emplace_back(filename, size, index, ox, oy, slant, extend);
17991792
}
18001793

18011794
void MathtextBackend::add_rect(
@@ -1843,37 +1836,16 @@ void MathtextBackend::draw(
18431836
}
18441837
return FT_Get_Char_Index(ft_face, codepoint);
18451838
},
1846-
[&](std::string name) {
1847-
return FT_Get_Name_Index(ft_face, name.data());
1848-
},
18491839
[&](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"};
1860-
}
1861-
FT_CHECK(FT_Set_Charmap, ft_face, cmap);
1862-
found = true;
1863-
}
1864-
}
1865-
if (!found) {
1866-
throw std::runtime_error{"no builtin charmap found"};
1867-
}
1868-
return FT_Get_Char_Index(ft_face, idx);
1840+
return FT_UInt(idx);
18691841
}
1870-
}, glyph.codepoint_or_name_or_index);
1842+
}, glyph.codepoint_or_index);
18711843
if (!index) {
18721844
auto glyph_ref = std::visit(overloaded {
18731845
[&](char32_t codepoint) { return "#" + std::to_string(codepoint); },
18741846
[&](std::string name) { return name; },
18751847
[&](FT_ULong idx) { return "#" + std::to_string(idx); }
1876-
}, glyph.codepoint_or_name_or_index);
1848+
}, glyph.codepoint_or_index);
18771849
warn_on_missing_glyph(glyph_ref);
18781850
}
18791851
auto const& raw_glyph = cairo_glyph_t{index, glyph.x, glyph.y};

ext/_mplcairo.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ class MathtextBackend {
175175
// that will wait for the ft2 rewrite in Matplotlib itself.
176176
std::string path;
177177
double size;
178-
std::variant<char32_t, std::string, FT_ULong> codepoint_or_name_or_index;
178+
std::variant<char32_t, FT_ULong> codepoint_or_index;
179179
double x, y;
180180
double slant;
181181
double extend;
182182

183183
Glyph(
184184
std::string path,
185185
double size,
186-
std::variant<char32_t, std::string, FT_ULong> codepoint_or_name_or_index,
186+
std::variant<char32_t, FT_ULong> codepoint_or_index,
187187
double x, double y,
188188
double slant = 0, double extend = 1);
189189
};
@@ -200,8 +200,7 @@ class MathtextBackend {
200200
char32_t codepoint);
201201
void add_usetex_glyph(
202202
double ox, double oy, std::string filename, double size,
203-
std::variant<std::string, FT_ULong> name_or_index,
204-
double slant, double extend);
203+
FT_ULong index, double slant, double extend);
205204
void add_rect(double x1, double y1, double x2, double y2);
206205
void draw(
207206
GraphicsContextRenderer& gcr, double x, double y, double angle) const;

src/mplcairo/base.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,12 @@ 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})")
151145
mb.add_usetex_glyph(
152146
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))
147+
text.font.path, text.font.size,
148+
text.font._index_dvi_to_freetype(text.glyph),
149+
text.font.effects.get("slant", 0),
150+
text.font.effects.get("extend", 1))
157151
for x1, y1, h, w in page.boxes:
158152
mb.add_rect(x1, -y1, x1 + w, -(y1 + h))
159153
mb.draw(self, x, y, angle)

0 commit comments

Comments
 (0)