3
3
#define NO_IMPORT_ARRAY
4
4
5
5
#include < algorithm>
6
+ #include < sstream>
6
7
#include < stdexcept>
7
8
#include < string>
8
9
41
42
42
43
FT_Library _ft2Library;
43
44
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
+
44
51
FT2Image::FT2Image () : m_dirty(true ), m_buffer(NULL ), m_width(0 ), m_height(0 )
45
52
{
46
53
}
@@ -506,7 +513,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
506
513
{
507
514
clear ();
508
515
509
- int error = FT_Open_Face (_ft2Library, &open_args, 0 , &face);
516
+ FT_Error error = FT_Open_Face (_ft2Library, &open_args, 0 , &face);
510
517
511
518
if (error == FT_Err_Unknown_File_Format) {
512
519
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(
515
522
} else if (error == FT_Err_Invalid_File_Format) {
516
523
throw std::runtime_error (" Can not load face. Invalid file format." );
517
524
} else if (error) {
518
- throw std::runtime_error (" Can not load face. " );
525
+ throw_ft_error (" Can not load face" , error );
519
526
}
520
527
521
528
// set a default fontsize 12 pt at 72dpi
@@ -524,7 +531,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
524
531
error = FT_Set_Char_Size (face, 12 * 64 , 0 , 72 * (unsigned int )hinting_factor, 72 );
525
532
if (error) {
526
533
FT_Done_Face (face);
527
- throw std::runtime_error (" Could not set the fontsize" );
534
+ throw_ft_error (" Could not set the fontsize" , error );
528
535
}
529
536
530
537
if (open_args.stream != NULL ) {
@@ -560,14 +567,13 @@ void FT2Font::clear()
560
567
561
568
void FT2Font::set_size (double ptsize, double dpi)
562
569
{
563
- int error = FT_Set_Char_Size (
570
+ FT_Error error = FT_Set_Char_Size (
564
571
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
-
568
572
if (error) {
569
- throw std::runtime_error (" Could not set the fontsize" );
573
+ throw_ft_error (" Could not set the fontsize" , error );
570
574
}
575
+ FT_Matrix transform = { 65536 / hinting_factor, 0 , 0 , 65536 };
576
+ FT_Set_Transform (face, &transform, 0 );
571
577
}
572
578
573
579
void FT2Font::set_charmap (int i)
@@ -576,15 +582,15 @@ void FT2Font::set_charmap(int i)
576
582
throw std::runtime_error (" i exceeds the available number of char maps" );
577
583
}
578
584
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 );
581
587
}
582
588
}
583
589
584
590
void FT2Font::select_charmap (unsigned long i)
585
591
{
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 );
588
594
}
589
595
}
590
596
@@ -634,19 +640,16 @@ void FT2Font::set_text(
634
640
FT_Get_Kerning (face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
635
641
pen.x += (delta.x << 10 ) / (hinting_factor << 16 );
636
642
}
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);
640
645
}
641
646
// ignore errors, jump to next glyph
642
647
643
648
// extract glyph image and store it in our table
644
649
645
650
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);
650
653
}
651
654
// ignore errors, jump to next glyph
652
655
@@ -680,37 +683,25 @@ void FT2Font::set_text(
680
683
void FT2Font::load_char (long charcode, FT_Int32 flags)
681
684
{
682
685
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);
687
688
}
688
-
689
689
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);
694
692
}
695
-
696
693
glyphs.push_back (thisGlyph);
697
694
}
698
695
699
696
void FT2Font::load_glyph (FT_UInt glyph_index, FT_Int32 flags)
700
697
{
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);
705
700
}
706
-
707
701
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);
712
704
}
713
-
714
705
glyphs.push_back (thisGlyph);
715
706
}
716
707
@@ -739,10 +730,10 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
739
730
image.resize (width, height);
740
731
741
732
for (size_t n = 0 ; n < glyphs.size (); n++) {
742
- error = FT_Glyph_To_Bitmap (
733
+ FT_Error error = FT_Glyph_To_Bitmap (
743
734
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0 , 1 );
744
735
if (error) {
745
- throw std::runtime_error (" Could not convert glyph to bitmap" );
736
+ throw_ft_error (" Could not convert glyph to bitmap" , error );
746
737
}
747
738
748
739
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
@@ -760,10 +751,10 @@ void FT2Font::get_xys(bool antialiased, std::vector<double> &xys)
760
751
{
761
752
for (size_t n = 0 ; n < glyphs.size (); n++) {
762
753
763
- error = FT_Glyph_To_Bitmap (
754
+ FT_Error error = FT_Glyph_To_Bitmap (
764
755
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0 , 1 );
765
756
if (error) {
766
- throw std::runtime_error (" Could not convert glyph to bitmap" );
757
+ throw_ft_error (" Could not convert glyph to bitmap" , error );
767
758
}
768
759
769
760
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,
789
780
throw std::runtime_error (" glyph num is out of range" );
790
781
}
791
782
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
+ );
797
789
if (error) {
798
- throw std::runtime_error (" Could not convert glyph to bitmap" );
790
+ throw_ft_error (" Could not convert glyph to bitmap" , error );
799
791
}
800
792
801
793
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[glyphInd];
@@ -810,8 +802,8 @@ void FT2Font::get_glyph_name(unsigned int glyph_number, char *buffer)
810
802
is generated by ttconv in ttfont_CharStrings_getname. */
811
803
PyOS_snprintf (buffer, 128 , " uni%08x" , glyph_number);
812
804
} 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 );
815
807
}
816
808
}
817
809
}
0 commit comments