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

Skip to content

Commit 3c0c9db

Browse files
authored
Merge pull request #12759 from anntzer/ftmissingglyphs
Warn on FreeType missing glyphs.
2 parents 56243e9 + cc744f5 commit 3c0c9db

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import time
1818
import types
19+
import warnings
1920
import zlib
2021

2122
import numpy as np
@@ -937,8 +938,13 @@ def get_char_width(charcode):
937938
s, flags=LOAD_NO_SCALE | LOAD_NO_HINTING).horiAdvance
938939
return cvt(width)
939940

940-
widths = [get_char_width(charcode)
941-
for charcode in range(firstchar, lastchar+1)]
941+
with warnings.catch_warnings():
942+
# Ignore 'Required glyph missing from current font' warning
943+
# from ft2font: here we're just builting the widths table, but
944+
# the missing glyphs may not even be used in the actual string.
945+
warnings.filterwarnings("ignore")
946+
widths = [get_char_width(charcode)
947+
for charcode in range(firstchar, lastchar+1)]
942948
descriptor['MaxWidth'] = max(widths)
943949

944950
# Make the "Differences" array, sort the ccodes < 255 from

src/ft2font.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "ft2font.h"
1010
#include "mplutils.h"
11+
#include "py_exceptions.h"
1112

1213
#ifndef M_PI
1314
#define M_PI 3.14159265358979323846264338328
@@ -162,9 +163,24 @@ FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1,
162163

163164
inline double conv(long v)
164165
{
165-
return double(v) / 64.0;
166+
return v / 64.;
166167
}
167168

169+
static FT_UInt ft_get_char_index_or_warn(FT_Face face, FT_ULong charcode)
170+
{
171+
FT_UInt glyph_index = FT_Get_Char_Index(face, charcode);
172+
if (!glyph_index) {
173+
PyErr_WarnFormat(NULL, 1, "Glyph %lu missing from current font.", charcode);
174+
// Apparently PyErr_WarnFormat returns 0 even if the exception propagates
175+
// due to running with -Werror, so check the error flag directly instead.
176+
if (PyErr_Occurred()) {
177+
throw py::exception();
178+
}
179+
}
180+
return glyph_index;
181+
}
182+
183+
168184
int FT2Font::get_path_count()
169185
{
170186
// get the glyph as a path, a list of (COMMAND, *args) as described in matplotlib.path
@@ -610,7 +626,7 @@ void FT2Font::set_text(
610626
FT_BBox glyph_bbox;
611627
FT_Pos last_advance;
612628

613-
glyph_index = FT_Get_Char_Index(face, codepoints[n]);
629+
glyph_index = ft_get_char_index_or_warn(face, codepoints[n]);
614630

615631
// retrieve kerning distance and move pen position
616632
if (use_kerning && previous && glyph_index) {
@@ -663,7 +679,8 @@ void FT2Font::set_text(
663679

664680
void FT2Font::load_char(long charcode, FT_Int32 flags)
665681
{
666-
int error = FT_Load_Char(face, (unsigned long)charcode, flags);
682+
FT_UInt glyph_index = ft_get_char_index_or_warn(face, (FT_ULong)charcode);
683+
int error = FT_Load_Glyph(face, glyph_index, flags);
667684

668685
if (error) {
669686
throw std::runtime_error("Could not load charcode");

src/ft2font_wrapper.cpp

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

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

0 commit comments

Comments
 (0)