Skip to content

Commit f7e5e35

Browse files
committed
Minor tweaks to usetex font loading.
1 parent f535930 commit f7e5e35

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

ext/_mplcairo.cpp

+20-21
Original file line numberDiff line numberDiff line change
@@ -1828,8 +1828,7 @@ void MathtextBackend::draw(
18281828
auto ft_face =
18291829
static_cast<FT_Face>(
18301830
cairo_font_face_get_user_data(face, &detail::FT_KEY));
1831-
auto index = FT_UInt{};
1832-
std::visit(overloaded {
1831+
auto index = std::visit(overloaded {
18331832
[&](char32_t codepoint) {
18341833
// The last unicode charmap is the FreeType-synthesized one.
18351834
auto i = ft_face->num_charmaps - 1;
@@ -1842,41 +1841,41 @@ void MathtextBackend::draw(
18421841
if (i < 0) {
18431842
throw std::runtime_error{"no unicode charmap found"};
18441843
}
1845-
index = FT_Get_Char_Index(ft_face, codepoint);
1846-
if (!index) {
1847-
warn_on_missing_glyph("#" + std::to_string(index));
1848-
}
1844+
return FT_Get_Char_Index(ft_face, codepoint);
18491845
},
18501846
[&](std::string name) {
1851-
index = FT_Get_Name_Index(ft_face, name.data());
1852-
if (!index) {
1853-
warn_on_missing_glyph(name);
1854-
}
1847+
return FT_Get_Name_Index(ft_face, name.data());
18551848
},
18561849
[&](FT_ULong idx) {
1857-
// For the usetex case, look up the "native" font charmap,
1858-
// which typically has a TT_ENCODING_ADOBE_STANDARD or
1859-
// TT_ENCODING_ADOBE_CUSTOM encoding, unlike the FreeType-synthesized
1860-
// one which has a TT_ENCODING_UNICODE encoding.
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.
18611853
auto found = false;
18621854
for (auto i = 0; i < ft_face->num_charmaps; ++i) {
1863-
if (ft_face->charmaps[i]->encoding != FT_ENCODING_UNICODE) {
1855+
auto const& cmap = ft_face->charmaps[i];
1856+
if (cmap->encoding == FT_ENCODING_ADOBE_STANDARD
1857+
|| cmap->encoding == FT_ENCODING_ADOBE_CUSTOM) {
18641858
if (found) {
1865-
throw std::runtime_error{"multiple non-unicode charmaps found"};
1859+
throw std::runtime_error{"multiple Adobe charmaps found"};
18661860
}
1867-
FT_CHECK(FT_Set_Charmap, ft_face, ft_face->charmaps[i]);
1861+
FT_CHECK(FT_Set_Charmap, ft_face, cmap);
18681862
found = true;
18691863
}
18701864
}
18711865
if (!found) {
18721866
throw std::runtime_error{"no builtin charmap found"};
18731867
}
1874-
index = FT_Get_Char_Index(ft_face, idx);
1875-
if (!index) {
1876-
warn_on_missing_glyph("#" + std::to_string(index));
1877-
}
1868+
return FT_Get_Char_Index(ft_face, idx);
18781869
}
18791870
}, glyph.codepoint_or_name_or_index);
1871+
if (!index) {
1872+
auto glyph_ref = std::visit(overloaded {
1873+
[&](char32_t codepoint) { return "#" + std::to_string(codepoint); },
1874+
[&](std::string name) { return name; },
1875+
[&](FT_ULong idx) { return "#" + std::to_string(idx); }
1876+
}, glyph.codepoint_or_name_or_index);
1877+
warn_on_missing_glyph(glyph_ref);
1878+
}
18801879
auto const& raw_glyph = cairo_glyph_t{index, glyph.x, glyph.y};
18811880
cairo_show_glyphs(cr, &raw_glyph, 1);
18821881
}

0 commit comments

Comments
 (0)