Skip to content

Commit e7291c5

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

File tree

5 files changed

+13
-87
lines changed

5 files changed

+13
-87
lines changed

ext/_mplcairo.cpp

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,11 +1670,11 @@ void GraphicsContextRenderer::restore_region(Region& region)
16701670

16711671
MathtextBackend::Glyph::Glyph(
16721672
std::string path, double size,
1673-
std::variant<char32_t, std::string, FT_ULong> codepoint_or_name_or_index,
1673+
std::variant<char32_t, FT_ULong> codepoint_or_index,
16741674
double x, double y,
16751675
double slant, double extend) :
16761676
path{path}, size{size},
1677-
codepoint_or_name_or_index{codepoint_or_name_or_index},
1677+
codepoint_or_index{codepoint_or_index},
16781678
x{x}, y{y},
16791679
slant{slant}, extend{extend}
16801680
{}
@@ -1689,16 +1689,9 @@ void MathtextBackend::add_glyph(
16891689

16901690
void MathtextBackend::add_usetex_glyph(
16911691
double ox, double oy, std::string filename, double size,
1692-
std::variant<std::string, FT_ULong> name_or_index,
1693-
double slant, double extend)
1692+
FT_ULong index, double slant, double extend)
16941693
{
1695-
auto codepoint_or_name_or_index =
1696-
std::variant<char32_t, std::string, FT_ULong>{};
1697-
std::visit(
1698-
[&](auto name_or_index) { codepoint_or_name_or_index = name_or_index; },
1699-
name_or_index);
1700-
glyphs_.emplace_back(
1701-
filename, size, codepoint_or_name_or_index, ox, oy, slant, extend);
1694+
glyphs_.emplace_back(filename, size, index, ox, oy, slant, extend);
17021695
}
17031696

17041697
void MathtextBackend::add_rect(
@@ -1746,37 +1739,16 @@ void MathtextBackend::draw(
17461739
}
17471740
return FT_Get_Char_Index(ft_face, codepoint);
17481741
},
1749-
[&](std::string name) {
1750-
return FT_Get_Name_Index(ft_face, name.data());
1751-
},
17521742
[&](FT_ULong idx) {
1753-
// For classic fonts, the index maps to the "native" font charmap,
1754-
// which typically has an ADOBE_STANDARD or ADOBE_CUSTOM encoding,
1755-
// unlike the FreeType-synthesized one which has a UNICODE encoding.
1756-
auto found = false;
1757-
for (auto i = 0; i < ft_face->num_charmaps; ++i) {
1758-
auto const& cmap = ft_face->charmaps[i];
1759-
if (cmap->encoding == FT_ENCODING_ADOBE_STANDARD
1760-
|| cmap->encoding == FT_ENCODING_ADOBE_CUSTOM) {
1761-
if (found) {
1762-
throw std::runtime_error{"multiple Adobe charmaps found"};
1763-
}
1764-
FT_CHECK(FT_Set_Charmap, ft_face, cmap);
1765-
found = true;
1766-
}
1767-
}
1768-
if (!found) {
1769-
throw std::runtime_error{"no builtin charmap found"};
1770-
}
1771-
return FT_Get_Char_Index(ft_face, idx);
1743+
return FT_UInt(idx);
17721744
}
1773-
}, glyph.codepoint_or_name_or_index);
1745+
}, glyph.codepoint_or_index);
17741746
if (!index) {
17751747
auto glyph_ref = std::visit(overloaded {
17761748
[&](char32_t codepoint) { return "#" + std::to_string(codepoint); },
17771749
[&](std::string name) { return name; },
17781750
[&](FT_ULong idx) { return "#" + std::to_string(idx); }
1779-
}, glyph.codepoint_or_name_or_index);
1751+
}, glyph.codepoint_or_index);
17801752
warn_on_missing_glyph(glyph_ref);
17811753
}
17821754
auto const& raw_glyph = cairo_glyph_t{index, glyph.x, glyph.y};

ext/_mplcairo.h

Lines changed: 3 additions & 4 deletions
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/_backports.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/mplcairo/_util.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import matplotlib as mpl
77
import numpy as np
88

9-
from . import _backports
10-
119

1210
def detect_buffer_format(buf):
1311
return {
@@ -22,17 +20,6 @@ def detect_buffer_format(buf):
2220
}[buf.dtype.type, buf.shape[2:]]
2321

2422

25-
@functools.lru_cache(1)
26-
def get_tex_font_map():
27-
return mpl.dviread.PsfontsMap(mpl.dviread.find_tex_file("pdftex.map"))
28-
29-
30-
def get_glyph_name(dvitext):
31-
ps_font = get_tex_font_map()[dvitext.font.texname]
32-
return (_backports._parse_enc(ps_font.encoding)[dvitext.glyph]
33-
if ps_font.encoding is not None else None)
34-
35-
3623
def get_matplotlib_gtk_backend():
3724
import gi
3825
required = gi.get_required_version("Gtk")

src/mplcairo/base.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,10 @@ 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(
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))
146+
text.x, -text.y, text.font.path, text.font.size, text.index,
147+
text.font.effects.get("slant", 0),
148+
text.font.effects.get("extend", 1))
157149
for x1, y1, h, w in page.boxes:
158150
mb.add_rect(x1, -y1, x1 + w, -(y1 + h))
159151
mb.draw(self, x, y, angle)

0 commit comments

Comments
 (0)