From 209b759d556950b5af7a880e9e226b956dd9f095 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 22 Jun 2017 23:45:01 -0700 Subject: [PATCH] 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. --- src/ft2font.cpp | 17 ++++++++++++++--- src/ft2font_wrapper.cpp | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ft2font.cpp b/src/ft2font.cpp index ef622b2e9cac..4e30da77ef46 100644 --- a/src/ft2font.cpp +++ b/src/ft2font.cpp @@ -162,9 +162,19 @@ FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1, inline double conv(long v) { - return double(v) / 64.0; + return v / 64.; } +FT_UInt ft_get_char_index_or_warn(FT_Face face, FT_ULong charcode) +{ + FT_UInt glyph_index = FT_Get_Char_Index(face, charcode); + if (!glyph_index) { + PyErr_WarnEx(NULL, "Required glyph missing from current font.", 1); + } + return glyph_index; +} + + int FT2Font::get_path_count() { // get the glyph as a path, a list of (COMMAND, *args) as described in matplotlib.path @@ -611,7 +621,7 @@ void FT2Font::set_text( FT_BBox glyph_bbox; FT_Pos last_advance; - glyph_index = FT_Get_Char_Index(face, codepoints[n]); + glyph_index = ft_get_char_index_or_warn(face, codepoints[n]); // retrieve kerning distance and move pen position if (use_kerning && previous && glyph_index) { @@ -664,7 +674,8 @@ void FT2Font::set_text( void FT2Font::load_char(long charcode, FT_Int32 flags) { - int error = FT_Load_Char(face, (unsigned long)charcode, flags); + FT_UInt glyph_index = ft_get_char_index_or_warn(face, (FT_ULong)charcode); + int error = FT_Load_Glyph(face, glyph_index, flags); if (error) { throw std::runtime_error("Could not load charcode"); diff --git a/src/ft2font_wrapper.cpp b/src/ft2font_wrapper.cpp index a90c7b115e0e..3ebaffffb9d2 100644 --- a/src/ft2font_wrapper.cpp +++ b/src/ft2font_wrapper.cpp @@ -998,8 +998,8 @@ static PyObject *PyFT2Font_get_char_index(PyFT2Font *self, PyObject *args, PyObj const char *PyFT2Font_get_sfnt__doc__ = "get_sfnt(name)\n" "\n" - "Get all values from the SFNT names table. Result is a dictionary whose" - "key is the platform-ID, ISO-encoding-scheme, language-code, and" + "Get all values from the SFNT names table. Result is a dictionary whose " + "key is the platform-ID, ISO-encoding-scheme, language-code, and " "description.\n"; static PyObject *PyFT2Font_get_sfnt(PyFT2Font *self, PyObject *args, PyObject *kwds)