Skip to content

Commit a5d314f

Browse files
committed
print fontname in missing glyph warning
1 parent 21ce592 commit a5d314f

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

lib/matplotlib/_text_helpers.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
"LayoutItem", ["ft_object", "char", "glyph_idx", "x", "prev_kern"])
1313

1414

15-
def warn_on_missing_glyph(codepoint):
15+
def warn_on_missing_glyph(codepoint, font_name):
1616
_api.warn_external(
17-
"Glyph {} ({}) missing from current font.".format(
17+
"Glyph {} ({}) missing from current font {}.".format(
1818
codepoint,
19-
chr(codepoint).encode("ascii", "namereplace").decode("ascii")))
19+
chr(codepoint).encode("ascii", "namereplace").decode("ascii"),
20+
font_name))
2021
block = ("Hebrew" if 0x0590 <= codepoint <= 0x05ff else
2122
"Arabic" if 0x0600 <= codepoint <= 0x06ff else
2223
"Devanagari" if 0x0900 <= codepoint <= 0x097f else

lib/matplotlib/tests/test_text.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -817,12 +817,14 @@ def test_pdf_kerning():
817817

818818
def test_unsupported_script(recwarn):
819819
fig = plt.figure()
820-
fig.text(.5, .5, "\N{BENGALI DIGIT ZERO}")
820+
t = fig.text(.5, .5, "\N{BENGALI DIGIT ZERO}")
821+
print(t.get_fontname())
821822
fig.canvas.draw()
822823
assert all(isinstance(warn.message, UserWarning) for warn in recwarn)
823824
assert (
824825
[warn.message.args for warn in recwarn] ==
825-
[(r"Glyph 2534 (\N{BENGALI DIGIT ZERO}) missing from current font.",),
826+
[(r"Glyph 2534 (\N{BENGALI DIGIT ZERO}) missing from current font "
827+
+ f"{t.get_fontname()}.",),
826828
(r"Matplotlib currently does not support Bengali natively.",)])
827829

828830

src/ft2font.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "ft2font.h"
1212
#include "mplutils.h"
1313
#include "numpy_cpp.h"
14+
#include "py_converters.h"
1415
#include "py_exceptions.h"
1516

1617
#ifndef M_PI
@@ -184,11 +185,13 @@ FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1,
184185
m_dirty = true;
185186
}
186187

187-
static void ft_glyph_warn(FT_ULong charcode)
188+
static void ft_glyph_warn(FT_ULong charcode, FT_String * family_name)
188189
{
190+
const char* name = (family_name != NULL)?family_name:"UNAVAILABLE";
189191
PyObject *text_helpers = NULL, *tmp = NULL;
192+
190193
if (!(text_helpers = PyImport_ImportModule("matplotlib._text_helpers")) ||
191-
!(tmp = PyObject_CallMethod(text_helpers, "warn_on_missing_glyph", "k", charcode))) {
194+
!(tmp = PyObject_CallMethod(text_helpers, "warn_on_missing_glyph", "(k, s)", charcode, name))){
192195
goto exit;
193196
}
194197
exit:
@@ -207,7 +210,7 @@ ft_get_char_index_or_warn(FT_Face face, FT_ULong charcode, bool warn = true)
207210
return glyph_index;
208211
}
209212
if (warn) {
210-
ft_glyph_warn(charcode);
213+
ft_glyph_warn(charcode, face->family_name);
211214
}
212215
return 0;
213216
}
@@ -515,7 +518,7 @@ void FT2Font::set_text(
515518
char_to_font, glyph_to_font, codepoints[n], flags,
516519
charcode_error, glyph_error, false);
517520
if (!was_found) {
518-
ft_glyph_warn((FT_ULong)codepoints[n]);
521+
ft_glyph_warn((FT_ULong)codepoints[n], (ft_object_with_glyph->face)->family_name);
519522

520523
// render missing glyph tofu
521524
// come back to top-most font
@@ -582,7 +585,7 @@ void FT2Font::load_char(long charcode, FT_Int32 flags, FT2Font *&ft_object, bool
582585
bool was_found = load_char_with_fallback(ft_object_with_glyph, final_glyph_index, glyphs, char_to_font,
583586
glyph_to_font, charcode, flags, charcode_error, glyph_error, true);
584587
if (!was_found) {
585-
ft_glyph_warn(charcode);
588+
ft_glyph_warn(charcode, (ft_object_with_glyph->face)->family_name);
586589
if (charcode_error) {
587590
throw_ft_error("Could not load charcode", charcode_error);
588591
}

0 commit comments

Comments
 (0)