-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathFontManager.cc
224 lines (180 loc) · 6.49 KB
/
FontManager.cc
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
#include <stdlib.h>
#include <node.h>
#include <uv.h>
#include <v8.h>
#include <nan.h>
#include "FontDescriptor.h"
using namespace v8;
// these functions are implemented by the platform
ResultSet *getAvailableFonts();
ResultSet *findFonts(FontDescriptor *);
FontDescriptor *findFont(FontDescriptor *);
FontDescriptor *substituteFont(char *, char *);
// converts a ResultSet to a JavaScript array
Local<Array> collectResults(ResultSet *results) {
Nan::EscapableHandleScope scope;
Local<Array> res = Nan::New<Array>(results->size());
int i = 0;
for (ResultSet::iterator it = results->begin(); it != results->end(); it++) {
Nan::Set(res, i++, (*it)->toJSObject());
}
delete results;
return scope.Escape(res);
}
// converts a FontDescriptor to a JavaScript object
Local<Value> wrapResult(FontDescriptor *result) {
Nan::EscapableHandleScope scope;
if (result == NULL)
return scope.Escape(Nan::Null());
Local<Object> res = result->toJSObject();
delete result;
return scope.Escape(res);
}
// holds data about an operation that will be
// performed on a background thread
struct AsyncRequest {
uv_work_t work;
FontDescriptor *desc; // used by findFont and findFonts
char *postscriptName; // used by substituteFont
char *substitutionString; // ditto
FontDescriptor *result; // for functions with a single result
ResultSet *results; // for functions with multiple results
Nan::Callback *callback; // the actual JS callback to call when we are done
AsyncRequest(Local<Value> v) {
work.data = (void *)this;
callback = new Nan::Callback(v.As<Function>());
desc = NULL;
postscriptName = NULL;
substitutionString = NULL;
result = NULL;
results = NULL;
}
~AsyncRequest() {
delete callback;
if (desc)
delete desc;
if (postscriptName)
delete postscriptName;
if (substitutionString)
delete substitutionString;
// result/results deleted by wrapResult/collectResults respectively
}
};
// calls the JavaScript callback for a request
void asyncCallback(uv_work_t *work) {
Nan::HandleScope scope;
AsyncRequest *req = (AsyncRequest *) work->data;
Nan::AsyncResource async("asyncCallback");
Local<Value> info[1];
if (req->results) {
info[0] = collectResults(req->results);
} else if (req->result) {
info[0] = wrapResult(req->result);
} else {
info[0] = Nan::Null();
}
req->callback->Call(1, info, &async);
delete req;
}
void getAvailableFontsAsync(uv_work_t *work) {
AsyncRequest *req = (AsyncRequest *) work->data;
req->results = getAvailableFonts();
}
template<bool async>
NAN_METHOD(getAvailableFonts) {
if (async) {
if (info.Length() < 1 || !info[0]->IsFunction())
return Nan::ThrowTypeError("Expected a callback");
AsyncRequest *req = new AsyncRequest(info[0]);
uv_queue_work(uv_default_loop(), &req->work, getAvailableFontsAsync, (uv_after_work_cb) asyncCallback);
return;
} else {
info.GetReturnValue().Set(collectResults(getAvailableFonts()));
}
}
void findFontsAsync(uv_work_t *work) {
AsyncRequest *req = (AsyncRequest *) work->data;
req->results = findFonts(req->desc);
}
template<bool async>
NAN_METHOD(findFonts) {
if (info.Length() < 1 || !info[0]->IsObject() || info[0]->IsFunction())
return Nan::ThrowTypeError("Expected a font descriptor");
Local<Object> desc = info[0].As<Object>();
FontDescriptor *descriptor = new FontDescriptor(desc);
if (async) {
if (info.Length() < 2 || !info[1]->IsFunction())
return Nan::ThrowTypeError("Expected a callback");
AsyncRequest *req = new AsyncRequest(info[1]);
req->desc = descriptor;
uv_queue_work(uv_default_loop(), &req->work, findFontsAsync, (uv_after_work_cb) asyncCallback);
return;
} else {
Local<Object> res = collectResults(findFonts(descriptor));
delete descriptor;
info.GetReturnValue().Set(res);
}
}
void findFontAsync(uv_work_t *work) {
AsyncRequest *req = (AsyncRequest *) work->data;
req->result = findFont(req->desc);
}
template<bool async>
NAN_METHOD(findFont) {
if (info.Length() < 1 || !info[0]->IsObject() || info[0]->IsFunction())
return Nan::ThrowTypeError("Expected a font descriptor");
Local<Object> desc = info[0].As<Object>();
FontDescriptor *descriptor = new FontDescriptor(desc);
if (async) {
if (info.Length() < 2 || !info[1]->IsFunction())
return Nan::ThrowTypeError("Expected a callback");
AsyncRequest *req = new AsyncRequest(info[1]);
req->desc = descriptor;
uv_queue_work(uv_default_loop(), &req->work, findFontAsync, (uv_after_work_cb) asyncCallback);
return;
} else {
Local<Value> res = wrapResult(findFont(descriptor));
delete descriptor;
info.GetReturnValue().Set(res);
}
}
void substituteFontAsync(uv_work_t *work) {
AsyncRequest *req = (AsyncRequest *) work->data;
req->result = substituteFont(req->postscriptName, req->substitutionString);
}
template<bool async>
NAN_METHOD(substituteFont) {
if (info.Length() < 1 || !info[0]->IsString())
return Nan::ThrowTypeError("Expected postscript name");
if (info.Length() < 2 || !info[1]->IsString())
return Nan::ThrowTypeError("Expected substitution string");
Nan::Utf8String postscriptName(info[0]);
Nan::Utf8String substitutionString(info[1]);
if (async) {
if (info.Length() < 3 || !info[2]->IsFunction())
return Nan::ThrowTypeError("Expected a callback");
// copy the strings since the JS garbage collector might run before the async request is finished
char *ps = new char[postscriptName.length() + 1];
strcpy(ps, *postscriptName);
char *sub = new char[substitutionString.length() + 1];
strcpy(sub, *substitutionString);
AsyncRequest *req = new AsyncRequest(info[2]);
req->postscriptName = ps;
req->substitutionString = sub;
uv_queue_work(uv_default_loop(), &req->work, substituteFontAsync, (uv_after_work_cb) asyncCallback);
return;
} else {
info.GetReturnValue().Set(wrapResult(substituteFont(*postscriptName, *substitutionString)));
}
}
NAN_MODULE_INIT(Init) {
Nan::Export(target, "getAvailableFonts", getAvailableFonts<true>);
Nan::Export(target, "getAvailableFontsSync", getAvailableFonts<false>);
Nan::Export(target, "findFonts", findFonts<true>);
Nan::Export(target, "findFontsSync", findFonts<false>);
Nan::Export(target, "findFont", findFont<true>);
Nan::Export(target, "findFontSync", findFont<false>);
Nan::Export(target, "substituteFont", substituteFont<true>);
Nan::Export(target, "substituteFontSync", substituteFont<false>);
}
NODE_MODULE(fontmanager, Init)