-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathglyphs.cpp
More file actions
404 lines (346 loc) · 15.8 KB
/
Copy pathglyphs.cpp
File metadata and controls
404 lines (346 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// fontnik
#include "glyphs.hpp"
// node
#include <limits>
#include <nan.h>
#include <node_buffer.h>
// sdf-glyph-foundry
#include <mapbox/glyph_foundry.hpp>
#include <mapbox/glyph_foundry_impl.hpp>
namespace node_fontnik {
struct FaceMetadata {
// non copyable
FaceMetadata(FaceMetadata const&) = delete;
FaceMetadata& operator=(FaceMetadata const&) = delete;
// movaable only for highest efficiency
FaceMetadata& operator=(FaceMetadata&& c) = default;
FaceMetadata(FaceMetadata&& c) = default;
std::string family_name{};
std::string style_name{};
std::vector<int> points{};
FaceMetadata(std::string const& _family_name,
std::string const& _style_name,
std::vector<int>&& _points) : family_name(_family_name),
style_name(_style_name),
points(std::move(_points)) {}
FaceMetadata(std::string const& _family_name,
std::vector<int>&& _points) : family_name(_family_name),
points(std::move(_points)) {}
};
struct LoadBaton {
// We explicitly delete the copy constructor and assignment operator below
// This allows us to have the `const char* font_data` without needing to define copy semantics
// and avoids a g++ warning of ‘struct node_fontnik::LoadBaton’ has pointer data members [-Weffc++]
// but does not override ‘node_fontnik::LoadBaton(const node_fontnik::LoadBaton&)’'
LoadBaton(LoadBaton const&) = delete;
LoadBaton& operator=(LoadBaton const&) = delete;
Nan::Persistent<v8::Function> callback;
Nan::Persistent<v8::Object> buffer;
const char* font_data;
std::size_t font_size;
std::string error_name;
std::vector<FaceMetadata> faces;
uv_work_t request;
LoadBaton(v8::Local<v8::Object> buf,
v8::Local<v8::Value> cb) : font_data(node::Buffer::Data(buf)),
font_size(node::Buffer::Length(buf)),
error_name(),
faces(),
request() {
request.data = this;
callback.Reset(cb.As<v8::Function>());
buffer.Reset(buf.As<v8::Object>());
}
~LoadBaton() {
callback.Reset();
buffer.Reset();
}
};
struct RangeBaton {
// We explicitly delete the copy constructor and assignment operator below
// This allows us to have the `const char* font_data` without needing to define copy semantics
// and avoids a g++ warning of ‘struct node_fontnik::LoadBaton’ has pointer data members [-Weffc++]
// but does not override ‘node_fontnik::LoadBaton(const node_fontnik::LoadBaton&)’'
RangeBaton(RangeBaton const&) = delete;
RangeBaton& operator=(RangeBaton const&) = delete;
Nan::Persistent<v8::Function> callback;
Nan::Persistent<v8::Object> buffer;
const char* font_data;
std::size_t font_size;
std::string error_name;
std::uint32_t start;
std::uint32_t end;
std::vector<std::uint32_t> chars;
std::string message;
uv_work_t request;
RangeBaton(v8::Local<v8::Object> buf,
v8::Local<v8::Value> cb,
std::uint32_t _start,
std::uint32_t _end) : font_data(node::Buffer::Data(buf)),
font_size(node::Buffer::Length(buf)),
error_name(),
start(_start),
end(_end),
chars(),
message(),
request() {
request.data = this;
callback.Reset(cb.As<v8::Function>());
buffer.Reset(buf.As<v8::Object>());
}
~RangeBaton() {
callback.Reset();
buffer.Reset();
}
};
NAN_METHOD(Load) {
// Validate arguments.
if (!info[0]->IsObject()) {
return Nan::ThrowTypeError("First argument must be a font buffer");
}
v8::Local<v8::Object> obj = info[0]->ToObject();
if (obj->IsNull() || obj->IsUndefined() || !node::Buffer::HasInstance(obj)) {
return Nan::ThrowTypeError("First argument must be a font buffer");
}
if (info.Length() < 2 || !info[1]->IsFunction()) {
return Nan::ThrowTypeError("Callback must be a function");
}
LoadBaton* baton = new LoadBaton(obj, info[1]);
uv_queue_work(uv_default_loop(), &baton->request, LoadAsync, reinterpret_cast<uv_after_work_cb>(AfterLoad));
}
NAN_METHOD(Range) {
// Validate arguments.
if (info.Length() < 1 || !info[0]->IsObject()) {
return Nan::ThrowTypeError("First argument must be an object of options");
}
v8::Local<v8::Object> options = info[0].As<v8::Object>();
v8::Local<v8::Value> font_buffer = options->Get(Nan::New<v8::String>("font").ToLocalChecked());
if (!font_buffer->IsObject()) {
return Nan::ThrowTypeError("Font buffer is not an object");
}
v8::Local<v8::Object> obj = font_buffer->ToObject();
v8::Local<v8::Value> start = options->Get(Nan::New<v8::String>("start").ToLocalChecked());
v8::Local<v8::Value> end = options->Get(Nan::New<v8::String>("end").ToLocalChecked());
if (obj->IsNull() || obj->IsUndefined() || !node::Buffer::HasInstance(obj)) {
return Nan::ThrowTypeError("First argument must be a font buffer");
}
if (!start->IsNumber() || start->IntegerValue() < 0) {
return Nan::ThrowTypeError("option `start` must be a number from 0-65535");
}
if (!end->IsNumber() || end->IntegerValue() > 65535) {
return Nan::ThrowTypeError("option `end` must be a number from 0-65535");
}
if (end->IntegerValue() < start->IntegerValue()) {
return Nan::ThrowTypeError("`start` must be less than or equal to `end`");
}
if (info.Length() < 2 || !info[1]->IsFunction()) {
return Nan::ThrowTypeError("Callback must be a function");
}
RangeBaton* baton = new RangeBaton(obj,
info[1],
start->Uint32Value(),
end->Uint32Value());
uv_queue_work(uv_default_loop(), &baton->request, RangeAsync, reinterpret_cast<uv_after_work_cb>(AfterRange));
}
struct ft_library_guard {
// non copyable
ft_library_guard(ft_library_guard const&) = delete;
ft_library_guard& operator=(ft_library_guard const&) = delete;
ft_library_guard(FT_Library* lib) : library_(lib) {}
~ft_library_guard() {
if (library_) FT_Done_FreeType(*library_);
}
FT_Library* library_;
};
struct ft_face_guard {
// non copyable
ft_face_guard(ft_face_guard const&) = delete;
ft_face_guard& operator=(ft_face_guard const&) = delete;
ft_face_guard(FT_Face* f) : face_(f) {}
~ft_face_guard() {
if (face_) {
FT_Done_Face(*face_);
}
}
FT_Face* face_;
};
void LoadAsync(uv_work_t* req) {
LoadBaton* baton = static_cast<LoadBaton*>(req->data);
try {
FT_Library library = nullptr;
ft_library_guard library_guard(&library);
FT_Error error = FT_Init_FreeType(&library);
if (error) {
/* LCOV_EXCL_START */
baton->error_name = std::string("could not open FreeType library");
return;
/* LCOV_EXCL_END */
}
FT_Face ft_face = 0;
FT_Long num_faces = 0;
for (int i = 0; ft_face == 0 || i < num_faces; ++i) {
ft_face_guard face_guard(&ft_face);
FT_Error face_error = FT_New_Memory_Face(library, reinterpret_cast<FT_Byte const*>(baton->font_data), static_cast<FT_Long>(baton->font_size), i, &ft_face);
if (face_error) {
baton->error_name = std::string("could not open font file");
return;
}
if (num_faces == 0) {
num_faces = ft_face->num_faces;
}
if (ft_face->family_name) {
std::set<int> points;
FT_ULong charcode;
FT_UInt gindex;
charcode = FT_Get_First_Char(ft_face, &gindex);
while (gindex != 0) {
charcode = FT_Get_Next_Char(ft_face, charcode, &gindex);
if (charcode != 0) points.emplace(charcode);
}
std::vector<int> points_vec(points.begin(), points.end());
if (ft_face->style_name) {
baton->faces.emplace_back(ft_face->family_name, ft_face->style_name, std::move(points_vec));
} else {
baton->faces.emplace_back(ft_face->family_name, std::move(points_vec));
}
} else {
baton->error_name = std::string("font does not have family_name or style_name");
return;
}
}
} catch (std::exception const& ex) {
baton->error_name = ex.what();
}
}
void AfterLoad(uv_work_t* req) {
Nan::HandleScope scope;
LoadBaton* baton = static_cast<LoadBaton*>(req->data);
if (!baton->error_name.empty()) {
v8::Local<v8::Value> argv[1] = {Nan::Error(baton->error_name.c_str())};
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(baton->callback), 1, argv);
} else {
v8::Local<v8::Array> js_faces = Nan::New<v8::Array>(baton->faces.size());
unsigned idx = 0;
for (auto const& face : baton->faces) {
v8::Local<v8::Object> js_face = Nan::New<v8::Object>();
js_face->Set(Nan::New("family_name").ToLocalChecked(), Nan::New(face.family_name).ToLocalChecked());
if (!face.style_name.empty()) js_face->Set(Nan::New("style_name").ToLocalChecked(), Nan::New(face.style_name).ToLocalChecked());
v8::Local<v8::Array> js_points = Nan::New<v8::Array>(face.points.size());
unsigned p_idx = 0;
for (auto const& pt : face.points) {
js_points->Set(p_idx++, Nan::New(pt));
}
js_face->Set(Nan::New("points").ToLocalChecked(), js_points);
js_faces->Set(idx++, js_face);
}
v8::Local<v8::Value> argv[2] = {Nan::Null(), js_faces};
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(baton->callback), 2, argv);
}
delete baton;
}
void RangeAsync(uv_work_t* req) {
RangeBaton* baton = static_cast<RangeBaton*>(req->data);
try {
unsigned array_size = baton->end - baton->start;
baton->chars.reserve(array_size);
for (unsigned i = baton->start; i <= baton->end; i++) {
baton->chars.emplace_back(i);
}
FT_Library library = nullptr;
ft_library_guard library_guard(&library);
FT_Error error = FT_Init_FreeType(&library);
if (error) {
/* LCOV_EXCL_START */
baton->error_name = std::string("could not open FreeType library");
return;
/* LCOV_EXCL_END */
}
llmr::glyphs::glyphs glyphs;
FT_Face ft_face = 0;
FT_Long num_faces = 0;
for (int i = 0; ft_face == 0 || i < num_faces; ++i) {
ft_face_guard face_guard(&ft_face);
FT_Error face_error = FT_New_Memory_Face(library, reinterpret_cast<FT_Byte const*>(baton->font_data), static_cast<FT_Long>(baton->font_size), i, &ft_face);
if (face_error) {
baton->error_name = std::string("could not open font");
return;
}
if (num_faces == 0) {
num_faces = ft_face->num_faces;
}
if (ft_face->family_name) {
llmr::glyphs::fontstack* mutable_fontstack = glyphs.add_stacks();
if (ft_face->style_name) {
mutable_fontstack->set_name(std::string(ft_face->family_name) + " " + std::string(ft_face->style_name));
} else {
mutable_fontstack->set_name(std::string(ft_face->family_name));
}
mutable_fontstack->set_range(std::to_string(baton->start) + "-" + std::to_string(baton->end));
const double scale_factor = 1.0;
// Set character sizes.
double size = 24 * scale_factor;
FT_Set_Char_Size(ft_face, 0, static_cast<FT_F26Dot6>(size * (1 << 6)), 0, 0);
// Set ascender and descender in 26.6 fractional pixels.
mutable_fontstack->set_ascender(ft_face->size->metrics.ascender / 64);
mutable_fontstack->set_descender(ft_face->size->metrics.descender / 64);
for (std::vector<uint32_t>::size_type x = 0; x != baton->chars.size(); x++) {
FT_ULong char_code = baton->chars[x];
sdf_glyph_foundry::glyph_info glyph;
// Get FreeType face from face_ptr.
FT_UInt char_index = FT_Get_Char_Index(ft_face, char_code);
if (!char_index) continue;
glyph.glyph_index = char_index;
sdf_glyph_foundry::RenderSDF(glyph, 24, 3, 0.25, ft_face);
// Add glyph to fontstack.
llmr::glyphs::glyph* mutable_glyph = mutable_fontstack->add_glyphs();
// direct type conversions, no need for checking or casting
mutable_glyph->set_width(glyph.width);
mutable_glyph->set_height(glyph.height);
mutable_glyph->set_left(glyph.left);
// conversions requiring checks, for safety and correctness
// shortening conversion
if (char_code > std::numeric_limits<FT_ULong>::max()) {
throw std::runtime_error("Invalid value for char_code: too large");
} else {
mutable_glyph->set_id(static_cast<std::uint32_t>(char_code));
}
// double to int
double top = static_cast<double>(glyph.top) - glyph.ascender;
if (top < std::numeric_limits<std::int32_t>::min() || top > std::numeric_limits<std::int32_t>::max()) {
throw std::runtime_error("Invalid value for glyph.top-glyph.ascender");
} else {
mutable_glyph->set_top(static_cast<std::int32_t>(top));
}
// double to uint
if (glyph.advance < std::numeric_limits<std::uint32_t>::min() || glyph.advance > std::numeric_limits<std::uint32_t>::max()) {
throw std::runtime_error("Invalid value for glyph.top-glyph.ascender");
} else {
mutable_glyph->set_advance(static_cast<std::uint32_t>(glyph.advance));
}
if (glyph.width > 0) {
mutable_glyph->set_bitmap(glyph.bitmap);
}
}
} else {
baton->error_name = std::string("font does not have family_name");
return;
}
}
baton->message = glyphs.SerializeAsString();
} catch (std::exception const& ex) {
baton->error_name = ex.what();
}
}
void AfterRange(uv_work_t* req) {
Nan::HandleScope scope;
RangeBaton* baton = static_cast<RangeBaton*>(req->data);
if (!baton->error_name.empty()) {
v8::Local<v8::Value> argv[1] = {Nan::Error(baton->error_name.c_str())};
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(baton->callback), 1, argv);
} else {
v8::Local<v8::Value> argv[2] = {Nan::Null(), Nan::CopyBuffer(baton->message.data(), static_cast<std::uint32_t>(baton->message.size())).ToLocalChecked()};
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(baton->callback), 2, argv);
}
delete baton;
}
} // namespace node_fontnik