Skip to content

Commit 1cd2728

Browse files
committed
faster fill/strokeText by holding FT_Face
I noticed that cairo_show_glyphs was slower vs when Pango calls it. Pango holds the FT_Face until the process ends. node-canvas now keeps it until the thread/InstanceData is deleted. Seems not great to me, but if Pango does it that way, let's wait until it becomes a problem.
1 parent 673d12f commit 1cd2728

4 files changed

Lines changed: 49 additions & 39 deletions

File tree

‎src/CanvasRenderingContext2d.cc‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,33 +2457,7 @@ Context2d::paintText(const Napi::CallbackInfo& info, bool stroke) {
24572457
double fontSize = state->fontProperties.size;
24582458
double toPx = fontSize / 1000;
24592459

2460-
FT_Face ftface;
2461-
FT_Error newFaceResult = FT_New_Memory_Face(
2462-
data->ft,
2463-
reinterpret_cast<const FT_Byte *>(run.face->data.get()),
2464-
run.face->data_len,
2465-
run.face->index,
2466-
&ftface
2467-
);
2468-
2469-
if (newFaceResult != 0) continue;
2470-
2471-
cairo_font_face_t* crface = cairo_ft_font_face_create_for_ft_face(ftface, 0);
2472-
2473-
static const cairo_user_data_key_t key{0};
2474-
cairo_status_t setDataResult = cairo_font_face_set_user_data(
2475-
crface,
2476-
&key,
2477-
ftface,
2478-
(cairo_destroy_func_t) FT_Done_Face
2479-
);
2480-
2481-
if (setDataResult) {
2482-
cairo_font_face_destroy(crface);
2483-
FT_Done_Face (ftface);
2484-
continue;
2485-
}
2486-
2460+
cairo_font_face_t* crface = cairo_ft_font_face_create_for_ft_face(run.face->ftface.get(), 0);
24872461
cairo_set_font_face(context(), crface);
24882462
cairo_set_font_size(context(), fontSize);
24892463

‎src/Font.h‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <hb.h>
1010
#include <hb-ot.h>
11+
#include <freetype/freetype.h>
12+
#include <type_traits>
1113

1214
enum class FontStyle {
1315
Normal,
@@ -42,6 +44,14 @@ struct HbFontDeleter {
4244
}
4345
};
4446

47+
struct FT_FaceDeleter {
48+
void operator()(FT_Face face) {
49+
FT_Done_Face(face);
50+
}
51+
};
52+
53+
using FT_Face_Type = std::remove_pointer<FT_Face>::type;
54+
4555
// Descriptors describe real fonts on the OS
4656
struct FontDescriptor : FontBase {
4757
std::unique_ptr<char[]> family;
@@ -53,6 +63,7 @@ struct FontDescriptor : FontBase {
5363
std::unique_ptr<file_char[]> url = nullptr;
5464
std::unique_ptr<uint8_t[]> data = nullptr;
5565
std::unique_ptr<hb_font_t, HbFontDeleter> hbfont = nullptr;
66+
std::unique_ptr<FT_Face_Type, FT_FaceDeleter> ftface = nullptr;
5667
size_t data_len = 0;
5768
size_t index = 0;
5869
FontStatus status = FontStatus::Unloaded;
@@ -95,6 +106,22 @@ struct FontDescriptor : FontBase {
95106
hb_font_set_scale(hbfont.get(), 1000, 1000);
96107
}
97108

109+
void loadFtFont(FT_Library ft) {
110+
FT_Face ftface;
111+
FT_Error newFaceResult = FT_New_Memory_Face(
112+
ft,
113+
reinterpret_cast<const FT_Byte *>(data.get()),
114+
data_len,
115+
index,
116+
&ftface
117+
);
118+
if (newFaceResult) {
119+
status = FontStatus::Error;
120+
} else {
121+
this->ftface = std::unique_ptr<FT_Face_Type, FT_FaceDeleter>(ftface);
122+
}
123+
}
124+
98125
void loadData() {
99126
FILE* file = nullptr;
100127
long file_size = 0;
@@ -134,9 +161,12 @@ struct FontDescriptor : FontBase {
134161
if (file) fclose(file);
135162
}
136163

137-
void load() {
164+
void load(FT_Library ft) {
138165
if (status == FontStatus::Unloaded) loadData();
139-
if (status == FontStatus::Loaded && hbfont == nullptr) loadHbFont();
166+
if (status == FontStatus::Loaded && hbfont == nullptr) {
167+
loadHbFont();
168+
loadFtFont(ft);
169+
}
140170
}
141171
};
142172

‎src/FontLayout.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ layoutText(
417417

418418
while (shapingWorkList.size() && matchIndex < matches.size()) {
419419
auto face = matches[matchIndex];
420-
face->load();
420+
face->load(data->ft());
421421
if (!face->hbfont) {
422422
matchIndex++;
423423
continue;

‎src/InstanceData.h‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ using PlatformFontManager = FontManagerMacos;
1515
using PlatformFontManager = FontManagerLinux;
1616
#endif
1717

18+
struct FT_Wrapper {
19+
FT_Library ft;
20+
FT_Wrapper() {
21+
FT_Init_FreeType(&ft);
22+
}
23+
~FT_Wrapper() {
24+
FT_Done_FreeType(ft);
25+
}
26+
};
27+
1828
struct InstanceData {
1929
Napi::FunctionReference CanvasCtor;
2030
Napi::FunctionReference CanvasGradientCtor;
@@ -27,14 +37,10 @@ struct InstanceData {
2737
Napi::FunctionReference FontFaceCtor;
2838
Napi::ObjectReference jsFontSet;
2939
FontFaceSet* cppFontSet;
30-
FT_Library ft;
40+
// FT_Wrapper exists because, and it is located here so that, FT_Done_FreeType
41+
// is called after ~PlatformFontManager. FT_Faces are all freed in
42+
// FT_Done_FreeType. That can't happen before their backing buffers are freed.
43+
FT_Wrapper _ft;
44+
inline FT_Library ft() { return _ft.ft; }
3145
PlatformFontManager fontManager;
32-
33-
InstanceData() {
34-
FT_Init_FreeType(&ft);
35-
}
36-
37-
~InstanceData() {
38-
FT_Done_FreeType(ft);
39-
}
4046
};

0 commit comments

Comments
 (0)