@@ -63,51 +63,23 @@ void throw_ft_error(std::string message, FT_Error error) {
6363 throw std::runtime_error (os.str ());
6464}
6565
66- FT2Image::FT2Image () : m_buffer (nullptr ), m_width (0 ), m_height (0 )
67- {
68- }
69-
7066FT2Image::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 )
7268{
73- resize (width, height);
7469}
7570
7671FT2Image::~FT2Image ()
7772{
78- delete[] m_buffer;
73+ free ( m_buffer) ;
7974}
8075
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)
8278{
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 );
9780
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 );
11183 FT_Int char_width = bitmap->width ;
11284 FT_Int char_height = bitmap->rows ;
11385
@@ -121,14 +93,14 @@ void FT2Image::draw_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y)
12193
12294 if (bitmap->pixel_mode == FT_PIXEL_MODE_GRAY) {
12395 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);
12597 unsigned char *src = bitmap->buffer + (((i - y_offset) * bitmap->pitch ) + x_start);
12698 for (FT_Int j = x1; j < x2; ++j, ++dst, ++src)
12799 *dst |= *src;
128100 }
129101 } else if (bitmap->pixel_mode == FT_PIXEL_MODE_MONO) {
130102 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);
132104 unsigned char *src = bitmap->buffer + ((i - y_offset) * bitmap->pitch );
133105 for (FT_Int j = x1; j < x2; ++j, ++dst) {
134106 int x = (j - x1 + x_start);
@@ -259,7 +231,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args,
259231 long hinting_factor_,
260232 std::vector<FT2Font *> &fallback_list,
261233 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 ),
263235 hinting_factor (hinting_factor_),
264236 // set default kerning factor to 0, i.e., no kerning manipulation
265237 kerning_factor (0 )
@@ -676,7 +648,8 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
676648 long width = (bbox.xMax - bbox.xMin ) / 64 + 2 ;
677649 long height = (bbox.yMax - bbox.yMin ) / 64 + 2 ;
678650
679- image.resize (width, height);
651+ image = py::array_t <uint8_t >{{height, width}};
652+ std::memset (image.mutable_data (0 ), 0 , image.nbytes ());
680653
681654 for (auto & glyph : glyphs) {
682655 FT_Error error = FT_Glyph_To_Bitmap (
@@ -692,11 +665,13 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
692665 FT_Int x = (FT_Int)(bitmap->left - (bbox.xMin * (1 . / 64 .)));
693666 FT_Int y = (FT_Int)((bbox.yMax * (1 . / 64 .)) - bitmap->top + 1 );
694667
695- image. draw_bitmap (&bitmap->bitmap , x, y);
668+ draw_bitmap (image, &bitmap->bitmap , x, y);
696669 }
697670}
698671
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)
700675{
701676 FT_Vector sub_offset;
702677 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,
718693
719694 FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[glyphInd];
720695
721- im. draw_bitmap (&bitmap->bitmap , x + bitmap->left , y);
696+ draw_bitmap (im, &bitmap->bitmap , x + bitmap->left , y);
722697}
723698
724699void FT2Font::get_glyph_name (unsigned int glyph_number, std::string &buffer,
0 commit comments