Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c783604

Browse files
committed
Warn on freetype missing glyphs.
There is an additional call site to FT_Get_Char_Index in PyFT2Font_get_char_index but that value is directly returned to Python so it would be easier to check for the value there and handle it accordingly (plus, in practice, the same warning will be triggered at the other two call sites at some point anyways). But I can replace that by a warning too.
1 parent d02024e commit c783604

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/ft2font.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ inline double conv(long v)
164164
return double(v) / 64.0;
165165
}
166166

167+
FT_UInt ft_get_char_index_or_warn(FT_Face face, FT_ULong charcode)
168+
{
169+
FT_UInt glyph_index = FT_Get_Char_Index(face, charcode);
170+
if (!glyph_index) {
171+
PyErr_WarnEx(NULL, "Required glyph missing from current font.", 1);
172+
}
173+
return glyph_index;
174+
}
175+
176+
167177
int FT2Font::get_path_count()
168178
{
169179
// get the glyph as a path, a list of (COMMAND, *args) as desribed in matplotlib.path
@@ -614,7 +624,7 @@ void FT2Font::set_text(
614624
FT_BBox glyph_bbox;
615625
FT_Pos last_advance;
616626

617-
glyph_index = FT_Get_Char_Index(face, codepoints[n]);
627+
glyph_index = ft_get_char_index_or_warn(face, codepoints[n]);
618628

619629
// retrieve kerning distance and move pen position
620630
if (use_kerning && previous && glyph_index) {
@@ -667,7 +677,8 @@ void FT2Font::set_text(
667677

668678
void FT2Font::load_char(long charcode, FT_Int32 flags)
669679
{
670-
int error = FT_Load_Char(face, (unsigned long)charcode, flags);
680+
FT_UInt glyph_index = ft_get_char_index_or_warn(face, (FT_ULong)charcode);
681+
int error = FT_Load_Glyph(face, glyph_index, flags);
671682

672683
if (error) {
673684
throw "Could not load charcode";

src/ft2font_wrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,8 +995,8 @@ static PyObject *PyFT2Font_get_char_index(PyFT2Font *self, PyObject *args, PyObj
995995
const char *PyFT2Font_get_sfnt__doc__ =
996996
"get_sfnt(name)\n"
997997
"\n"
998-
"Get all values from the SFNT names table. Result is a dictionary whose"
999-
"key is the platform-ID, ISO-encoding-scheme, language-code, and"
998+
"Get all values from the SFNT names table. Result is a dictionary whose "
999+
"key is the platform-ID, ISO-encoding-scheme, language-code, and "
10001000
"description.\n";
10011001

10021002
static PyObject *PyFT2Font_get_sfnt(PyFT2Font *self, PyObject *args, PyObject *kwds)

0 commit comments

Comments
 (0)