Skip to content

Commit 08fd9f2

Browse files
authored
Merge pull request matplotlib#13730 from anntzer/fterrors
Include FreeType error codes in FreeType exception messages.
2 parents 30454d7 + 2a8f6df commit 08fd9f2

File tree

2 files changed

+43
-52
lines changed

2 files changed

+43
-52
lines changed

src/ft2font.cpp

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define NO_IMPORT_ARRAY
44

55
#include <algorithm>
6+
#include <sstream>
67
#include <stdexcept>
78
#include <string>
89

@@ -41,6 +42,12 @@
4142

4243
FT_Library _ft2Library;
4344

45+
void throw_ft_error(std::string message, FT_Error error) {
46+
std::ostringstream os;
47+
os << message << " (error code 0x" << std::hex << error << ")";
48+
throw std::runtime_error(os.str());
49+
}
50+
4451
FT2Image::FT2Image() : m_dirty(true), m_buffer(NULL), m_width(0), m_height(0)
4552
{
4653
}
@@ -506,7 +513,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
506513
{
507514
clear();
508515

509-
int error = FT_Open_Face(_ft2Library, &open_args, 0, &face);
516+
FT_Error error = FT_Open_Face(_ft2Library, &open_args, 0, &face);
510517

511518
if (error == FT_Err_Unknown_File_Format) {
512519
throw std::runtime_error("Can not load face. Unknown file format.");
@@ -515,7 +522,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
515522
} else if (error == FT_Err_Invalid_File_Format) {
516523
throw std::runtime_error("Can not load face. Invalid file format.");
517524
} else if (error) {
518-
throw std::runtime_error("Can not load face.");
525+
throw_ft_error("Can not load face", error);
519526
}
520527

521528
// set a default fontsize 12 pt at 72dpi
@@ -524,7 +531,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
524531
error = FT_Set_Char_Size(face, 12 * 64, 0, 72 * (unsigned int)hinting_factor, 72);
525532
if (error) {
526533
FT_Done_Face(face);
527-
throw std::runtime_error("Could not set the fontsize");
534+
throw_ft_error("Could not set the fontsize", error);
528535
}
529536

530537
if (open_args.stream != NULL) {
@@ -560,14 +567,13 @@ void FT2Font::clear()
560567

561568
void FT2Font::set_size(double ptsize, double dpi)
562569
{
563-
int error = FT_Set_Char_Size(
570+
FT_Error error = FT_Set_Char_Size(
564571
face, (long)(ptsize * 64), 0, (unsigned int)(dpi * hinting_factor), (unsigned int)dpi);
565-
FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
566-
FT_Set_Transform(face, &transform, 0);
567-
568572
if (error) {
569-
throw std::runtime_error("Could not set the fontsize");
573+
throw_ft_error("Could not set the fontsize", error);
570574
}
575+
FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
576+
FT_Set_Transform(face, &transform, 0);
571577
}
572578

573579
void FT2Font::set_charmap(int i)
@@ -576,15 +582,15 @@ void FT2Font::set_charmap(int i)
576582
throw std::runtime_error("i exceeds the available number of char maps");
577583
}
578584
FT_CharMap charmap = face->charmaps[i];
579-
if (FT_Set_Charmap(face, charmap)) {
580-
throw std::runtime_error("Could not set the charmap");
585+
if (FT_Error error = FT_Set_Charmap(face, charmap)) {
586+
throw_ft_error("Could not set the charmap", error);
581587
}
582588
}
583589

584590
void FT2Font::select_charmap(unsigned long i)
585591
{
586-
if (FT_Select_Charmap(face, (FT_Encoding)i)) {
587-
throw std::runtime_error("Could not set the charmap");
592+
if (FT_Error error = FT_Select_Charmap(face, (FT_Encoding)i)) {
593+
throw_ft_error("Could not set the charmap", error);
588594
}
589595
}
590596

@@ -634,19 +640,16 @@ void FT2Font::set_text(
634640
FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
635641
pen.x += (delta.x << 10) / (hinting_factor << 16);
636642
}
637-
error = FT_Load_Glyph(face, glyph_index, flags);
638-
if (error) {
639-
throw std::runtime_error("could not load glyph");
643+
if (FT_Error error = FT_Load_Glyph(face, glyph_index, flags)) {
644+
throw_ft_error("Could not load glyph", error);
640645
}
641646
// ignore errors, jump to next glyph
642647

643648
// extract glyph image and store it in our table
644649

645650
FT_Glyph thisGlyph;
646-
error = FT_Get_Glyph(face->glyph, &thisGlyph);
647-
648-
if (error) {
649-
throw std::runtime_error("could not get glyph");
651+
if (FT_Error error = FT_Get_Glyph(face->glyph, &thisGlyph)) {
652+
throw_ft_error("Could not get glyph", error);
650653
}
651654
// ignore errors, jump to next glyph
652655

@@ -680,37 +683,25 @@ void FT2Font::set_text(
680683
void FT2Font::load_char(long charcode, FT_Int32 flags)
681684
{
682685
FT_UInt glyph_index = ft_get_char_index_or_warn(face, (FT_ULong)charcode);
683-
int error = FT_Load_Glyph(face, glyph_index, flags);
684-
685-
if (error) {
686-
throw std::runtime_error("Could not load charcode");
686+
if (FT_Error error = FT_Load_Glyph(face, glyph_index, flags)) {
687+
throw_ft_error("Could not load charcode", error);
687688
}
688-
689689
FT_Glyph thisGlyph;
690-
error = FT_Get_Glyph(face->glyph, &thisGlyph);
691-
692-
if (error) {
693-
throw std::runtime_error("Could not get glyph");
690+
if (FT_Error error = FT_Get_Glyph(face->glyph, &thisGlyph)) {
691+
throw_ft_error("Could not get glyph", error);
694692
}
695-
696693
glyphs.push_back(thisGlyph);
697694
}
698695

699696
void FT2Font::load_glyph(FT_UInt glyph_index, FT_Int32 flags)
700697
{
701-
int error = FT_Load_Glyph(face, glyph_index, flags);
702-
703-
if (error) {
704-
throw std::runtime_error("Could not load glyph");
698+
if (FT_Error error = FT_Load_Glyph(face, glyph_index, flags)) {
699+
throw_ft_error("Could not load glyph", error);
705700
}
706-
707701
FT_Glyph thisGlyph;
708-
error = FT_Get_Glyph(face->glyph, &thisGlyph);
709-
710-
if (error) {
711-
throw std::runtime_error("Could not load glyph");
702+
if (FT_Error error = FT_Get_Glyph(face->glyph, &thisGlyph)) {
703+
throw_ft_error("Could not get glyph", error);
712704
}
713-
714705
glyphs.push_back(thisGlyph);
715706
}
716707

@@ -739,10 +730,10 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
739730
image.resize(width, height);
740731

741732
for (size_t n = 0; n < glyphs.size(); n++) {
742-
error = FT_Glyph_To_Bitmap(
733+
FT_Error error = FT_Glyph_To_Bitmap(
743734
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0, 1);
744735
if (error) {
745-
throw std::runtime_error("Could not convert glyph to bitmap");
736+
throw_ft_error("Could not convert glyph to bitmap", error);
746737
}
747738

748739
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
@@ -760,10 +751,10 @@ void FT2Font::get_xys(bool antialiased, std::vector<double> &xys)
760751
{
761752
for (size_t n = 0; n < glyphs.size(); n++) {
762753

763-
error = FT_Glyph_To_Bitmap(
754+
FT_Error error = FT_Glyph_To_Bitmap(
764755
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0, 1);
765756
if (error) {
766-
throw std::runtime_error("Could not convert glyph to bitmap");
757+
throw_ft_error("Could not convert glyph to bitmap", error);
767758
}
768759

769760
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
@@ -789,13 +780,14 @@ void FT2Font::draw_glyph_to_bitmap(FT2Image &im, int x, int y, size_t glyphInd,
789780
throw std::runtime_error("glyph num is out of range");
790781
}
791782

792-
error = FT_Glyph_To_Bitmap(&glyphs[glyphInd],
793-
antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO,
794-
&sub_offset, // additional translation
795-
1 // destroy image
796-
);
783+
FT_Error error = FT_Glyph_To_Bitmap(
784+
&glyphs[glyphInd],
785+
antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO,
786+
&sub_offset, // additional translation
787+
1 // destroy image
788+
);
797789
if (error) {
798-
throw std::runtime_error("Could not convert glyph to bitmap");
790+
throw_ft_error("Could not convert glyph to bitmap", error);
799791
}
800792

801793
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[glyphInd];
@@ -810,8 +802,8 @@ void FT2Font::get_glyph_name(unsigned int glyph_number, char *buffer)
810802
is generated by ttconv in ttfont_CharStrings_getname. */
811803
PyOS_snprintf(buffer, 128, "uni%08x", glyph_number);
812804
} else {
813-
if (FT_Get_Glyph_Name(face, glyph_number, buffer, 128)) {
814-
throw std::runtime_error("Could not get glyph names.");
805+
if (FT_Error error = FT_Get_Glyph_Name(face, glyph_number, buffer, 128)) {
806+
throw_ft_error("Could not get glyph names", error);
815807
}
816808
}
817809
}

src/ft2font.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class FT2Font
119119
FT_Face face;
120120
FT_Matrix matrix; /* transformation matrix */
121121
FT_Vector pen; /* untransformed origin */
122-
FT_Error error;
123122
std::vector<FT_Glyph> glyphs;
124123
std::vector<FT_Vector> pos;
125124
FT_BBox bbox;

0 commit comments

Comments
 (0)