Skip to content

Commit 8397908

Browse files
committed
Split font opening/closing out of FT2Font constructor/destructor
This makes it easier to do later refactors.
1 parent 99d357f commit 8397908

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/ft2font.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <cstdio>
88
#include <iterator>
99
#include <set>
10-
#include <sstream>
1110
#include <stdexcept>
1211
#include <string>
1312
#include <vector>
@@ -207,32 +206,44 @@ FT2Font::get_path(std::vector<double> &vertices, std::vector<unsigned char> &cod
207206
codes.push_back(CLOSEPOLY);
208207
}
209208

210-
FT2Font::FT2Font(FT_Open_Args &open_args,
211-
long hinting_factor_,
212-
std::vector<FT2Font *> &fallback_list,
209+
FT2Font::FT2Font(long hinting_factor_, std::vector<FT2Font *> &fallback_list,
213210
FT2Font::WarnFunc warn, bool warn_if_used)
214211
: ft_glyph_warn(warn), warn_if_used(warn_if_used), image({1, 1}), face(nullptr),
215212
hinting_factor(hinting_factor_),
216213
// set default kerning factor to 0, i.e., no kerning manipulation
217214
kerning_factor(0)
218215
{
219216
clear();
217+
// Set fallbacks
218+
std::copy(fallback_list.begin(), fallback_list.end(), std::back_inserter(fallbacks));
219+
}
220+
221+
FT2Font::~FT2Font()
222+
{
223+
close();
224+
}
225+
226+
void FT2Font::open(FT_Open_Args &open_args)
227+
{
220228
FT_CHECK(FT_Open_Face, _ft2Library, &open_args, 0, &face);
221229
if (open_args.stream != nullptr) {
222230
face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
223231
}
224-
// Set fallbacks
225-
std::copy(fallback_list.begin(), fallback_list.end(), std::back_inserter(fallbacks));
226232
}
227233

228-
FT2Font::~FT2Font()
234+
void FT2Font::close()
229235
{
236+
// This should be idempotent, in case a user manually calls close before the
237+
// destructor does.
238+
230239
for (auto & glyph : glyphs) {
231240
FT_Done_Glyph(glyph);
232241
}
242+
glyphs.clear();
233243

234244
if (face) {
235245
FT_Done_Face(face);
246+
face = nullptr;
236247
}
237248
}
238249

src/ft2font.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ class FT2Font
9999
typedef void (*WarnFunc)(FT_ULong charcode, std::set<FT_String*> family_names);
100100

101101
public:
102-
FT2Font(FT_Open_Args &open_args, long hinting_factor,
103-
std::vector<FT2Font *> &fallback_list,
102+
FT2Font(long hinting_factor, std::vector<FT2Font *> &fallback_list,
104103
WarnFunc warn, bool warn_if_used);
105104
virtual ~FT2Font();
105+
void open(FT_Open_Args &open_args);
106+
void close();
106107
void clear();
107108
void set_size(double ptsize, double dpi);
108109
void set_charmap(int i);

src/ft2font_wrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,10 @@ PyFT2Font_init(py::object filename, long hinting_factor = 8,
499499
self->stream.close = nullptr;
500500
}
501501

502-
self->x = new FT2Font(open_args, hinting_factor, fallback_fonts, ft_glyph_warn,
502+
self->x = new FT2Font(hinting_factor, fallback_fonts, ft_glyph_warn,
503503
warn_if_used);
504-
505504
self->x->set_kerning_factor(*kerning_factor);
505+
self->x->open(open_args);
506506

507507
return self;
508508
}

0 commit comments

Comments
 (0)