@@ -63,51 +63,23 @@ void throw_ft_error(std::string message, FT_Error error) {
63
63
throw std::runtime_error (os.str ());
64
64
}
65
65
66
- FT2Image::FT2Image () : m_buffer (nullptr ), m_width (0 ), m_height (0 )
67
- {
68
- }
69
-
70
66
FT2Image::FT2Image (unsigned long width, unsigned long height)
71
- : m_buffer (nullptr ), m_width (0 ), m_height (0 )
67
+ : m_buffer (( unsigned char *) calloc (width * height, 1 )), m_width (width ), m_height (height )
72
68
{
73
- resize (width, height);
74
69
}
75
70
76
71
FT2Image::~FT2Image ()
77
72
{
78
- delete[] m_buffer;
73
+ free ( m_buffer) ;
79
74
}
80
75
81
- void FT2Image::resize (long width, long height)
76
+ void draw_bitmap (
77
+ py::array_t <uint8_t , py::array::c_style> im, FT_Bitmap *bitmap, FT_Int x, FT_Int y)
82
78
{
83
- if (width <= 0 ) {
84
- width = 1 ;
85
- }
86
- if (height <= 0 ) {
87
- height = 1 ;
88
- }
89
- size_t numBytes = width * height;
90
-
91
- if ((unsigned long )width != m_width || (unsigned long )height != m_height) {
92
- if (numBytes > m_width * m_height) {
93
- delete[] m_buffer;
94
- m_buffer = nullptr ;
95
- m_buffer = new unsigned char [numBytes];
96
- }
79
+ auto buf = im.mutable_data (0 );
97
80
98
- m_width = (unsigned long )width;
99
- m_height = (unsigned long )height;
100
- }
101
-
102
- if (numBytes && m_buffer) {
103
- memset (m_buffer, 0 , numBytes);
104
- }
105
- }
106
-
107
- void FT2Image::draw_bitmap (FT_Bitmap *bitmap, FT_Int x, FT_Int y)
108
- {
109
- FT_Int image_width = (FT_Int)m_width;
110
- FT_Int image_height = (FT_Int)m_height;
81
+ FT_Int image_width = (FT_Int)im.shape (1 );
82
+ FT_Int image_height = (FT_Int)im.shape (0 );
111
83
FT_Int char_width = bitmap->width ;
112
84
FT_Int char_height = bitmap->rows ;
113
85
@@ -121,14 +93,14 @@ void FT2Image::draw_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y)
121
93
122
94
if (bitmap->pixel_mode == FT_PIXEL_MODE_GRAY) {
123
95
for (FT_Int i = y1; i < y2; ++i) {
124
- unsigned char *dst = m_buffer + (i * image_width + x1);
96
+ unsigned char *dst = buf + (i * image_width + x1);
125
97
unsigned char *src = bitmap->buffer + (((i - y_offset) * bitmap->pitch ) + x_start);
126
98
for (FT_Int j = x1; j < x2; ++j, ++dst, ++src)
127
99
*dst |= *src;
128
100
}
129
101
} else if (bitmap->pixel_mode == FT_PIXEL_MODE_MONO) {
130
102
for (FT_Int i = y1; i < y2; ++i) {
131
- unsigned char *dst = m_buffer + (i * image_width + x1);
103
+ unsigned char *dst = buf + (i * image_width + x1);
132
104
unsigned char *src = bitmap->buffer + ((i - y_offset) * bitmap->pitch );
133
105
for (FT_Int j = x1; j < x2; ++j, ++dst) {
134
106
int x = (j - x1 + x_start);
@@ -259,7 +231,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args,
259
231
long hinting_factor_,
260
232
std::vector<FT2Font *> &fallback_list,
261
233
FT2Font::WarnFunc warn, bool warn_if_used)
262
- : ft_glyph_warn (warn), warn_if_used (warn_if_used), image (), face (nullptr ),
234
+ : ft_glyph_warn (warn), warn_if_used (warn_if_used), image ({ 1 , 1 } ), face (nullptr ),
263
235
hinting_factor (hinting_factor_),
264
236
// set default kerning factor to 0, i.e., no kerning manipulation
265
237
kerning_factor (0 )
@@ -676,7 +648,8 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
676
648
long width = (bbox.xMax - bbox.xMin ) / 64 + 2 ;
677
649
long height = (bbox.yMax - bbox.yMin ) / 64 + 2 ;
678
650
679
- image.resize (width, height);
651
+ image = py::array_t <uint8_t >{{height, width}};
652
+ std::memset (image.mutable_data (0 ), 0 , image.nbytes ());
680
653
681
654
for (auto & glyph : glyphs) {
682
655
FT_Error error = FT_Glyph_To_Bitmap (
@@ -692,11 +665,13 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
692
665
FT_Int x = (FT_Int)(bitmap->left - (bbox.xMin * (1 . / 64 .)));
693
666
FT_Int y = (FT_Int)((bbox.yMax * (1 . / 64 .)) - bitmap->top + 1 );
694
667
695
- image. draw_bitmap (&bitmap->bitmap , x, y);
668
+ draw_bitmap (image, &bitmap->bitmap , x, y);
696
669
}
697
670
}
698
671
699
- void FT2Font::draw_glyph_to_bitmap (FT2Image &im, int x, int y, size_t glyphInd, bool antialiased)
672
+ void FT2Font::draw_glyph_to_bitmap (
673
+ py::array_t <uint8_t , py::array::c_style> im,
674
+ int x, int y, size_t glyphInd, bool antialiased)
700
675
{
701
676
FT_Vector sub_offset;
702
677
sub_offset.x = 0 ; // int((xd - (double)x) * 64.0);
@@ -718,7 +693,7 @@ void FT2Font::draw_glyph_to_bitmap(FT2Image &im, int x, int y, size_t glyphInd,
718
693
719
694
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[glyphInd];
720
695
721
- im. draw_bitmap (&bitmap->bitmap , x + bitmap->left , y);
696
+ draw_bitmap (im, &bitmap->bitmap , x + bitmap->left , y);
722
697
}
723
698
724
699
void FT2Font::get_glyph_name (unsigned int glyph_number, std::string &buffer,
0 commit comments