43
43
44
44
FT_Library _ft2Library;
45
45
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
-
55
- void throw_ft_error (std::string message, FT_Error error) {
56
- char const * s = ft_error_string (error);
57
- std::ostringstream os (" " );
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
- }
63
- throw std::runtime_error (os.str ());
64
- }
65
-
66
46
FT2Image::FT2Image (unsigned long width, unsigned long height)
67
47
: m_buffer((unsigned char *)calloc(width * height, 1 )), m_width(width), m_height(height)
68
48
{
@@ -237,26 +217,16 @@ FT2Font::FT2Font(FT_Open_Args &open_args,
237
217
kerning_factor(0 )
238
218
{
239
219
clear ();
240
-
241
- FT_Error error = FT_Open_Face (_ft2Library, &open_args, 0 , &face);
242
- if (error) {
243
- throw_ft_error (" Can not load face" , error);
244
- }
245
-
246
- // set a default fontsize 12 pt at 72dpi
247
- error = FT_Set_Char_Size (face, 12 * 64 , 0 , 72 * (unsigned int )hinting_factor, 72 );
248
- if (error) {
249
- FT_Done_Face (face);
250
- throw_ft_error (" Could not set the fontsize" , error);
251
- }
252
-
220
+ FT_CHECK (FT_Open_Face, _ft2Library, &open_args, 0 , &face);
253
221
if (open_args.stream != nullptr ) {
254
222
face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
255
223
}
256
-
257
- FT_Matrix transform = { 65536 / hinting_factor, 0 , 0 , 65536 };
258
- FT_Set_Transform (face, &transform, nullptr );
259
-
224
+ try {
225
+ set_size (12 ., 72 .); // Set a default fontsize 12 pt at 72dpi.
226
+ } catch (...) {
227
+ FT_Done_Face (face);
228
+ throw ;
229
+ }
260
230
// Set fallbacks
261
231
std::copy (fallback_list.begin (), fallback_list.end (), std::back_inserter (fallbacks));
262
232
}
@@ -293,11 +263,9 @@ void FT2Font::clear()
293
263
294
264
void FT2Font::set_size (double ptsize, double dpi)
295
265
{
296
- FT_Error error = FT_Set_Char_Size (
266
+ FT_CHECK (
267
+ FT_Set_Char_Size,
297
268
face, (FT_F26Dot6)(ptsize * 64 ), 0 , (FT_UInt)(dpi * hinting_factor), (FT_UInt)dpi);
298
- if (error) {
299
- throw_ft_error (" Could not set the fontsize" , error);
300
- }
301
269
FT_Matrix transform = { 65536 / hinting_factor, 0 , 0 , 65536 };
302
270
FT_Set_Transform (face, &transform, nullptr );
303
271
@@ -311,17 +279,12 @@ void FT2Font::set_charmap(int i)
311
279
if (i >= face->num_charmaps ) {
312
280
throw std::runtime_error (" i exceeds the available number of char maps" );
313
281
}
314
- FT_CharMap charmap = face->charmaps [i];
315
- if (FT_Error error = FT_Set_Charmap (face, charmap)) {
316
- throw_ft_error (" Could not set the charmap" , error);
317
- }
282
+ FT_CHECK (FT_Set_Charmap, face, face->charmaps [i]);
318
283
}
319
284
320
285
void FT2Font::select_charmap (unsigned long i)
321
286
{
322
- if (FT_Error error = FT_Select_Charmap (face, (FT_Encoding)i)) {
323
- throw_ft_error (" Could not set the charmap" , error);
324
- }
287
+ FT_CHECK (FT_Select_Charmap, face, (FT_Encoding)i);
325
288
}
326
289
327
290
int FT2Font::get_kerning (FT_UInt left, FT_UInt right, FT_Kerning_Mode mode,
@@ -477,10 +440,10 @@ void FT2Font::load_char(long charcode, FT_Int32 flags, FT2Font *&ft_object, bool
477
440
if (!was_found) {
478
441
ft_glyph_warn (charcode, glyph_seen_fonts);
479
442
if (charcode_error) {
480
- throw_ft_error ( " Could not load charcode" , charcode_error);
443
+ THROW_FT_ERROR ( " charcode loading " , charcode_error);
481
444
}
482
445
else if (glyph_error) {
483
- throw_ft_error ( " Could not load charcode" , glyph_error);
446
+ THROW_FT_ERROR ( " charcode loading " , glyph_error);
484
447
}
485
448
} else if (ft_object_with_glyph->warn_if_used ) {
486
449
ft_glyph_warn (charcode, glyph_seen_fonts);
@@ -494,13 +457,9 @@ void FT2Font::load_char(long charcode, FT_Int32 flags, FT2Font *&ft_object, bool
494
457
glyph_seen_fonts.insert ((face != nullptr )?face->family_name : nullptr );
495
458
ft_glyph_warn ((FT_ULong)charcode, glyph_seen_fonts);
496
459
}
497
- if (FT_Error error = FT_Load_Glyph (face, glyph_index, flags)) {
498
- throw_ft_error (" Could not load charcode" , error);
499
- }
460
+ FT_CHECK (FT_Load_Glyph, face, glyph_index, flags);
500
461
FT_Glyph thisGlyph;
501
- if (FT_Error error = FT_Get_Glyph (face->glyph , &thisGlyph)) {
502
- throw_ft_error (" Could not get glyph" , error);
503
- }
462
+ FT_CHECK (FT_Get_Glyph, face->glyph , &thisGlyph);
504
463
glyphs.push_back (thisGlyph);
505
464
}
506
465
}
@@ -600,13 +559,9 @@ void FT2Font::load_glyph(FT_UInt glyph_index,
600
559
601
560
void FT2Font::load_glyph (FT_UInt glyph_index, FT_Int32 flags)
602
561
{
603
- if (FT_Error error = FT_Load_Glyph (face, glyph_index, flags)) {
604
- throw_ft_error (" Could not load glyph" , error);
605
- }
562
+ FT_CHECK (FT_Load_Glyph, face, glyph_index, flags);
606
563
FT_Glyph thisGlyph;
607
- if (FT_Error error = FT_Get_Glyph (face->glyph , &thisGlyph)) {
608
- throw_ft_error (" Could not get glyph" , error);
609
- }
564
+ FT_CHECK (FT_Get_Glyph, face->glyph , &thisGlyph);
610
565
glyphs.push_back (thisGlyph);
611
566
}
612
567
@@ -651,13 +606,10 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
651
606
image = py::array_t <uint8_t >{{height, width}};
652
607
std::memset (image.mutable_data (0 ), 0 , image.nbytes ());
653
608
654
- for (auto & glyph : glyphs) {
655
- FT_Error error = FT_Glyph_To_Bitmap (
609
+ for (auto & glyph: glyphs) {
610
+ FT_CHECK (
611
+ FT_Glyph_To_Bitmap,
656
612
&glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, nullptr , 1 );
657
- if (error) {
658
- throw_ft_error (" Could not convert glyph to bitmap" , error);
659
- }
660
-
661
613
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
662
614
// now, draw to our target surface (convert position)
663
615
@@ -681,16 +633,12 @@ void FT2Font::draw_glyph_to_bitmap(
681
633
throw std::runtime_error (" glyph num is out of range" );
682
634
}
683
635
684
- FT_Error error = FT_Glyph_To_Bitmap (
685
- &glyphs[glyphInd],
686
- antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO,
687
- &sub_offset, // additional translation
688
- 1 // destroy image
689
- );
690
- if (error) {
691
- throw_ft_error (" Could not convert glyph to bitmap" , error);
692
- }
693
-
636
+ FT_CHECK (
637
+ FT_Glyph_To_Bitmap,
638
+ &glyphs[glyphInd],
639
+ antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO,
640
+ &sub_offset, // additional translation
641
+ 1 ); // destroy image
694
642
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[glyphInd];
695
643
696
644
draw_bitmap (im, &bitmap->bitmap , x + bitmap->left , y);
@@ -715,9 +663,7 @@ void FT2Font::get_glyph_name(unsigned int glyph_number, std::string &buffer,
715
663
throw std::runtime_error (" Failed to convert glyph to standard name" );
716
664
}
717
665
} else {
718
- if (FT_Error error = FT_Get_Glyph_Name (face, glyph_number, buffer.data (), buffer.size ())) {
719
- throw_ft_error (" Could not get glyph names" , error);
720
- }
666
+ FT_CHECK (FT_Get_Glyph_Name, face, glyph_number, buffer.data (), buffer.size ());
721
667
auto len = buffer.find (' \0 ' );
722
668
if (len != buffer.npos ) {
723
669
buffer.resize (len);
0 commit comments