8000 Improve ft2font error reporting. · matplotlib/matplotlib@e62ccfe · GitHub
[go: up one dir, main page]

Skip to content

Commit e62ccfe

Browse files
committed
Improve ft2font error reporting.
This prints out human-readable error messages for failures. As an example, `FT2Font("setup.py")` now raises ``` RuntimeError: In FT2Font: Can not load face (unknown file format; error code 0x2) ```
1 parent e2e703b commit e62ccfe

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/ft2font.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,23 @@
4343

4444
FT_Library _ft2Library;
4545

46+
// FreeType error codes; loaded as per fterror.h.
47+
static char const* ft_error_string(FT_Error error) {
48+
#undef __FTERRORS_H__
49+
#define FT_ERROR_START_LIST switch (error) {
50+
#define FT_ERRORDEF( e, v, s ) case v: return s;
51+
#define FT_ERROR_END_LIST default: return NULL; }
52+
#include FT_ERRORS_H
53+
}
54+
4655
void throw_ft_error(std::string message, FT_Error error) {
56+
char const* s = ft_error_string(error);
4757
std::ostringstream os("");
48-
os << message << " (error code 0x" << std::hex << error << ")";
58+
if (s) {
59+
os << message << " (" << s << "; error code 0x" << std::hex << error << ")";
60+
} else { // Should not occur, but don't add another error from failed lookup.
61+
os << message << " (error code 0x" << std::hex << error << ")";
62+
}
4963
throw std::runtime_error(os.str());
5064
}
5165

@@ -324,14 +338,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
324338
clear();
325339

326340
FT_Error error = FT_Open_Face(_ft2Library, &open_args, 0, &face);
327-
328-
if (error == FT_Err_Unknown_File_Format) {
329-
throw std::runtime_error("Can not load face. Unknown file format.");
330-
} else if (error == FT_Err_Cannot_Open_Resource) {
331-
throw std::runtime_error("Can not load face. Can not open resource.");
332-
} else if (error == FT_Err_Invalid_File_Format) {
333-
throw std::runtime_error("Can not load face. Invalid file format.");
334-
} else if (error) {
341+
if (error) {
335342
throw_ft_error("Can not load face", error);
336343
}
337344

0 commit comments

Comments
 (0)
0