|
8 | 8 |
|
9 | 9 | #include "ft2font.h"
|
10 | 10 | #include "mplutils.h"
|
| 11 | +#include "py_exceptions.h" |
11 | 12 |
|
12 | 13 | #ifndef M_PI
|
13 | 14 | #define M_PI 3.14159265358979323846264338328
|
@@ -162,9 +163,24 @@ FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1,
|
162 | 163 |
|
163 | 164 | inline double conv(long v)
|
164 | 165 | {
|
165 |
| - return double(v) / 64.0; |
| 166 | + return v / 64.; |
166 | 167 | }
|
167 | 168 |
|
| 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 | + |
168 | 184 | int FT2Font::get_path_count()
|
169 | 185 | {
|
170 | 186 | // get the glyph as a path, a list of (COMMAND, *args) as described in matplotlib.path
|
@@ -610,7 +626,7 @@ void FT2Font::set_text(
|
610 | 626 | FT_BBox glyph_bbox;
|
611 | 627 | FT_Pos last_advance;
|
612 | 628 |
|
613 |
| - glyph_index = FT_Get_Char_Index(face, codepoints[n]); |
| 629 | + glyph_index = ft_get_char_index_or_warn(face, codepoints[n]); |
614 | 630 |
|
615 | 631 | // retrieve kerning distance and move pen position
|
616 | 632 | if (use_kerning && previous && glyph_index) {
|
@@ -663,7 +679,8 @@ void FT2Font::set_text(
|
663 | 679 |
|
664 | 680 | void FT2Font::load_char(long charcode, FT_Int32 flags)
|
665 | 681 | {
|
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); |
667 | 684 |
|
668 | 685 | if (error) {
|
669 | 686 | throw std::runtime_error("Could not load charcode");
|
|
0 commit comments