Skip to content

Commit db17baf

Browse files
committed
Make PyFT2Font a subclass of FT2Font
1 parent fa62956 commit db17baf

File tree

3 files changed

+115
-197
lines changed

3 files changed

+115
-197
lines changed

src/ft2font.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,13 @@ FT2Font::get_path(std::vector<double> &vertices, std::vector<unsigned char> &cod
207207
}
208208

209209
FT2Font::FT2Font(long hinting_factor_, std::vector<FT2Font *> &fallback_list,
210-
FT2Font::WarnFunc warn, bool warn_if_used)
211-
: ft_glyph_warn(warn), warn_if_used(warn_if_used), image({1, 1}), face(nullptr),
210+
bool warn_if_used)
211+
: warn_if_used(warn_if_used), image({1, 1}), face(nullptr), fallbacks(fallback_list),
212212
hinting_factor(hinting_factor_),
213213
// set default kerning factor to 0, i.e., no kerning manipulation
214214
kerning_factor(0)
215215
{
216216
clear();
217-
// Set fallbacks
218-
std::copy(fallback_list.begin(), fallback_list.end(), std::back_inserter(fallbacks));
219217
}
220218

221219
FT2Font::~FT2Font()
@@ -234,7 +232,8 @@ void FT2Font::open(FT_Open_Args &open_args)
234232
void FT2Font::close()
235233
{
236234
// This should be idempotent, in case a user manually calls close before the
237-
// destructor does.
235+
// destructor does. Note for example, that PyFT2Font _does_ call this before the
236+
// base destructor to ensure internal pointers are cleared early enough.
238237

239238
for (auto & glyph : glyphs) {
240239
FT_Done_Glyph(glyph);
@@ -544,21 +543,19 @@ FT_UInt FT2Font::get_char_index(FT_ULong charcode, bool fallback = false)
544543
return FT_Get_Char_Index(ft_object->get_face(), charcode);
545544
}
546545

547-
void FT2Font::get_width_height(long *width, long *height)
546+
std::tuple<long, long> FT2Font::get_width_height()
548547
{
549-
*width = advance;
550-
*height = bbox.yMax - bbox.yMin;
548+
return {advance, bbox.yMax - bbox.yMin};
551549
}
552550

553551
long FT2Font::get_descent()
554552
{
555553
return -bbox.yMin;
556554
}
557555

558-
void FT2Font::get_bitmap_offset(long *x, long *y)
556+
std::tuple<long, long> FT2Font::get_bitmap_offset()
559557
{
560-
*x = bbox.xMin;
561-
*y = 0;
558+
return {bbox.xMin, 0};
562559
}
563560

564561
void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
@@ -607,8 +604,11 @@ void FT2Font::draw_glyph_to_bitmap(
607604
draw_bitmap(im, &bitmap->bitmap, x + bitmap->left, y);
608605
}
609606

610-
void FT2Font::get_glyph_name(unsigned int glyph_number, std::string &buffer)
607+
std::string FT2Font::get_glyph_name(unsigned int glyph_number)
611608
{
609+
std::string buffer;
610+
buffer.resize(128);
611+
612612
if (!FT_HAS_GLYPH_NAMES(face)) {
613613
/* Note that this generated name must match the name that
614614
is generated by ttconv in ttfont_CharStrings_getname. */
@@ -625,6 +625,8 @@ void FT2Font::get_glyph_name(unsigned int glyph_number, std::string &buffer)
625625
buffer.resize(len);
626626
}
627627
}
628+
629+
return buffer;
628630
}
629631

630632
long FT2Font::get_name_index(char *name)

src/ft2font.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <set>
1313
#include <string>
1414
#include <string_view>
15+
#include <tuple>
1516
#include <unordered_map>
1617
#include <vector>
1718

@@ -96,11 +97,9 @@ extern FT_Library _ft2Library;
9697

9798
class FT2Font
9899
{
99-
typedef void (*WarnFunc)(FT_ULong charcode, std::set<FT_String*> family_names);
100-
101100
public:
102101
FT2Font(long hinting_factor, std::vector<FT2Font *> &fallback_list,
103-
WarnFunc warn, bool warn_if_used);
102+
bool warn_if_used);
104103
virtual ~FT2Font();
105104
void open(FT_Open_Args &open_args);
106105
void close();
@@ -124,14 +123,14 @@ class FT2Font
124123
std::set<FT_String*> &glyph_seen_fonts,
125124
bool override);
126125
void load_glyph(FT_UInt glyph_index, FT_Int32 flags);
127-
void get_width_height(long *width, long *height);
128-
void get_bitmap_offset(long *x, long *y);
126+
std::tuple<long, long> get_width_height();
127+
std::tuple<long, long> get_bitmap_offset();
129128
long get_descent();
130129
void draw_glyphs_to_bitmap(bool antialiased);
131130
void draw_glyph_to_bitmap(
132131
py::array_t<uint8_t, py::array::c_style> im,
133132
int x, int y, size_t glyphInd, bool antialiased);
134-
void get_glyph_name(unsigned int glyph_number, std::string &buffer);
133+
std::string get_glyph_name(unsigned int glyph_number);
135134
long get_name_index(char *name);
136135
FT_UInt get_char_index(FT_ULong charcode, bool fallback);
137136
void get_path(std::vector<double> &vertices, std::vector<unsigned char> &codes);
@@ -167,8 +166,9 @@ class FT2Font
167166
return FT_HAS_KERNING(face);
168167
}
169168

169+
protected:
170+
virtual void ft_glyph_warn(FT_ULong charcode, std::set<FT_String*> family_names) = 0;
170171
private:
171-
WarnFunc ft_glyph_warn;
172172
bool warn_if_used;
173173
py::array_t<uint8_t, py::array::c_style> image;
174174
FT_Face face;

0 commit comments

Comments
 (0)