@xlfont/load 1.0.12 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,404 +1,590 @@
1
- (function(){
2
- var err, xlfont, xfl;
3
- err = function(e){
4
- e == null && (e = {});
5
- return import$(new Error(), import$({
6
- name: 'lderror'
7
- }, e));
8
- };
9
- xlfont = function(opt){
10
- var ref$, that, this$ = this;
1
+ (function() { var xlfWorkerCode = 'var local={},onmessage=function(e){var e=e.data||{},n=e.bufs,p=e.key,e=e.lib;return local.inited||(console.log(e),importScripts(e||"opentype.js"),local.inited=!0),Promise.resolve().then(function(){return n.map(function(e){return opentype.parse(e)})}).then(function(e){for(var n,t,r,s,o,a=[],l=0,i=e.length;l<i;++l)for(t=0,r=(n=e[l]).glyphs.length;t<r;++t)s=t,a.push(n.glyphs.glyphs[s]);return n=e[0],o=new opentype.Font(((o={glyphs:a,familyName:n.names.fontFamily.en,styleName:n.names.fontSubfamily.en}).unitsPerEm=n.unitsPerEm,o.ascender=n.ascender,o.descender=n.descender,o)),postMessage({buf:o.toArrayBuffer(),key:p})})};'; var err, xlfWorker, xlfMerger, xlfont, xfl;
2
+ err = function(e){
3
+ e == null && (e = {});
4
+ return import$(new Error(), import$({
5
+ name: 'lderror'
6
+ }, e));
7
+ };
8
+ xlfWorker = {
9
+ key: 0,
10
+ worker: null,
11
+ queue: [],
12
+ init: function(opt){
13
+ var this$ = this;
11
14
  opt == null && (opt = {});
12
- this.opt = opt;
13
- this.sub = {
14
- set: {},
15
- font: {}
16
- };
17
- this.cjkOnly = opt.cjkOnly || false;
18
- this.codemap = {};
19
- this.otf = {
20
- font: null,
21
- dirty: true
22
- };
23
- this.path = opt.path;
24
- this.name = opt.name || (ref$ = this.path.replace(/\.[a-zA-Z0-9]+$/, '').split("/").filter(function(it){
25
- return it;
26
- }))[ref$.length - 1];
27
- this.style = 'normal';
28
- this.ext = opt.ext || (/\.(ttf|otf|woff2|woff)$/.exec(this.path) || [])[1] || '';
29
- this.format = (that = this.ext.toLowerCase()) ? that === 'ttf'
30
- ? 'truetype'
31
- : that === 'otf' ? 'truetype' : that : '';
32
- if (this.format) {
33
- this.format = "format('" + this.format + "')";
34
- }
35
- this.className = "xfl-" + (this.name || '').replace(/\s+/g, '_') + "-" + Math.random().toString(36).substring(2);
36
- this.isXl = !this.ext;
37
- this.css = [];
38
- this.init = proxise.once(function(){
39
- return this$._init();
15
+ return Promise.resolve().then(function(){
16
+ var url;
17
+ if (this$.worker) {
18
+ return;
19
+ }
20
+ url = URL.createObjectURL(new Blob([xlfWorkerCode], {
21
+ type: 'text/javascript'
22
+ }));
23
+ this$.worker = new Worker(url);
24
+ URL.revokeObjectURL(url);
25
+ return this$.worker.onmessage = function(e){
26
+ var ref$, buf, key, item;
27
+ ref$ = e.data, buf = ref$.buf, key = ref$.key;
28
+ if (!(item = this$.queue.filter(function(q){
29
+ return key === q.key;
30
+ })[0])) {
31
+ return;
32
+ }
33
+ this$.queue.splice(this$.queue.indexOf(item), 1);
34
+ return item.res(buf);
35
+ };
36
+ });
37
+ },
38
+ run: function(abs, opt){
39
+ var this$ = this;
40
+ return new Promise(function(res, rej){
41
+ return this$.init(opt).then(function(){
42
+ var item;
43
+ this$.queue.push(item = {
44
+ res: res,
45
+ rej: rej,
46
+ key: this$.key++
47
+ });
48
+ console.log(">", opt);
49
+ return this$.worker.postMessage({
50
+ bufs: abs,
51
+ key: item.key,
52
+ lib: opt.opentypeUrl
53
+ });
54
+ });
40
55
  });
41
- this.init();
42
- return this;
56
+ }
57
+ };
58
+ xlfMerger = function(arg$){
59
+ var bufs, useWorker;
60
+ bufs = arg$.bufs, useWorker = arg$.useWorker;
61
+ if (!bufs.length) {
62
+ return Promise.resolve();
63
+ }
64
+ if (useWorker) {
65
+ return xlfWorker.run(bufs, useWorker);
66
+ }
67
+ return Promise.resolve().then(function(){
68
+ return bufs.map(function(it){
69
+ return opentype.parse(it);
70
+ });
71
+ }).then(function(fonts){
72
+ var glyphs, i$, len$, font, j$, to$, i, merged, ref$;
73
+ glyphs = [];
74
+ for (i$ = 0, len$ = fonts.length; i$ < len$; ++i$) {
75
+ font = fonts[i$];
76
+ for (j$ = 0, to$ = font.glyphs.length; j$ < to$; ++j$) {
77
+ i = j$;
78
+ glyphs.push(font.glyphs.glyphs[i]);
79
+ }
80
+ }
81
+ font = fonts[0];
82
+ merged = new opentype.Font((ref$ = {
83
+ glyphs: glyphs,
84
+ familyName: font.names.fontFamily.en,
85
+ styleName: font.names.fontSubfamily.en
86
+ }, ref$.unitsPerEm = font.unitsPerEm, ref$.ascender = font.ascender, ref$.descender = font.descender, ref$));
87
+ return merged.toArrayBuffer();
88
+ });
89
+ };
90
+ xlfont = function(opt){
91
+ var that, this$ = this;
92
+ opt == null && (opt = {});
93
+ this.opt = opt;
94
+ this.sub = {
95
+ set: {},
96
+ font: {}
43
97
  };
44
- xlfont.prototype = import$(Object.create(Object.prototype), {
45
- _init: function(){
46
- var this$ = this;
47
- return Promise.resolve().then(function(){
48
- if (!this$.isXl) {
49
- this$.css = [{
50
- content: "@font-face {\n font-family: \"" + this$.name + "\";\n src: url(\"" + this$.path + "\") " + this$.format + ";\n}\n." + this$.className + " { font-family: \"" + this$.name + "\"; }"
51
- }];
52
- return xfl.update();
53
- } else {
54
- return new Promise(function(res, rej){
55
- var xhr;
56
- xhr = new XMLHttpRequest();
57
- xhr.addEventListener('readystatechange', function(){
58
- if (xhr.readyState !== XMLHttpRequest.DONE) {
59
- return;
60
- }
61
- if (xhr.status !== 200) {
62
- return rej(err({
63
- code: xhr.status,
64
- message: xhr.responseText
65
- }));
66
- }
67
- (xhr.responseText || '').split('\n').map(function(d, i){
68
- return d.split(' ').map(function(e, j){
69
- return this$.codemap[e] = i + 1;
70
- });
98
+ this.cjkOnly = opt.cjkOnly || false;
99
+ this.codemap = {};
100
+ this.otf = {
101
+ font: null,
102
+ dirty: true
103
+ };
104
+ this.path = opt.path;
105
+ this.name = opt.name || this.path.replace(/\.[a-zA-Z0-9]+$/, '').replace(/^https?:\/\/[^/]+\//g, '').replace(/\s/g, '-').replace(/\//g, '-').toLowerCase();
106
+ this.style = opt.style || 'normal';
107
+ this.weight = opt.weight || '400';
108
+ this.ext = opt.ext || (/\.(ttf|otf|woff2|woff)$/.exec(this.path) || [])[1] || '';
109
+ this.format = (that = this.ext.toLowerCase()) ? that === 'ttf'
110
+ ? 'truetype'
111
+ : that === 'otf' ? 'truetype' : that : '';
112
+ if (this.format) {
113
+ this.format = "format('" + this.format + "')";
114
+ }
115
+ this.className = "xfl-" + (this.name || '').replace(/\s+/g, '_') + "-" + Math.random().toString(36).substring(2);
116
+ this.isXl = opt.isXl != null
117
+ ? opt.isXl
118
+ : !this.ext;
119
+ if (!this.ext) {
120
+ this.ext = 'woff';
121
+ }
122
+ this.doMerge = opt.doMerge != null ? opt.doMerge : false;
123
+ this.useWorker = opt.useWorker != null ? opt.useWorker : false;
124
+ this.misschar = {};
125
+ this.css = [];
126
+ this.init = proxise.once(function(){
127
+ return this$._init();
128
+ });
129
+ this.init();
130
+ return this;
131
+ };
132
+ xlfont.prototype = import$(Object.create(Object.prototype), {
133
+ _init: function(){
134
+ var this$ = this;
135
+ return Promise.resolve().then(function(){
136
+ if (!this$.isXl) {
137
+ this$.css = [{
138
+ content: "@font-face {\n font-family: \"" + this$.name + "\";\n src: url(\"" + this$.path + "\") " + this$.format + ";\n font-style: " + this$.style + ";\n font-weight: " + this$.weight + ";\n}\n." + this$.className + " { font-family: \"" + this$.name + "\"; }"
139
+ }];
140
+ return xfl.update();
141
+ } else {
142
+ return new Promise(function(res, rej){
143
+ var xhr;
144
+ xhr = new XMLHttpRequest();
145
+ xhr.addEventListener('readystatechange', function(){
146
+ if (xhr.readyState !== XMLHttpRequest.DONE) {
147
+ return;
148
+ }
149
+ if (xhr.status !== 200) {
150
+ return rej(err({
151
+ code: xhr.status,
152
+ message: xhr.responseText
153
+ }));
154
+ }
155
+ (xhr.responseText || '').split('\n').map(function(d, i){
156
+ return d.split(' ').map(function(e, j){
157
+ return this$.codemap[e] = i + 1;
71
158
  });
72
- return res();
73
159
  });
74
- xhr.open('GET', this$.path + "/charmap.txt");
75
- return xhr.send();
160
+ return res();
76
161
  });
77
- }
78
- });
79
- },
80
- _fetch: function(f, dofetch){
81
- var this$ = this;
82
- f == null && (f = {});
83
- dofetch == null && (dofetch = false);
84
- if (!dofetch) {
85
- if (!f.url) {
86
- if (this.isXl) {
87
- f.url = this.path + "/" + f.key + ".woff2";
88
- f.type = 'woff2';
89
- } else {
90
- f.url = this.path;
91
- f.type = this.ext.toLowerCase();
92
- }
93
- }
94
- return Promise.resolve(f);
162
+ xhr.open('GET', this$.path + "/charmap.txt");
163
+ return xhr.send();
164
+ });
95
165
  }
96
- if (f.blob) {
97
- return Promise.resolve(f);
166
+ });
167
+ },
168
+ _fetch: function(f, dofetch, type){
169
+ var this$ = this;
170
+ f == null && (f = {});
171
+ dofetch == null && (dofetch = false);
172
+ type == null && (type = 'ttf');
173
+ if (!dofetch) {
174
+ if (!f.url) {
175
+ if (this.isXl) {
176
+ f.url = this.path + "/" + f.key + "." + type;
177
+ f.type = type;
178
+ } else {
179
+ f.url = this.path;
180
+ f.type = this.ext.toLowerCase() || type;
181
+ }
98
182
  }
99
- if (!f.proxy) {
100
- f.proxy = proxise(function(f){
101
- var p;
102
- if (f.running) {
103
- return;
183
+ return Promise.resolve(f);
184
+ }
185
+ if (f.blob) {
186
+ return Promise.resolve(f);
187
+ }
188
+ if (!f.proxy) {
189
+ f.proxy = proxise(function(f){
190
+ var p;
191
+ if (f.running) {
192
+ return;
193
+ }
194
+ f.running = true;
195
+ p = new Promise(function(res, rej){
196
+ var xhr;
197
+ if (f.blob) {
198
+ return res(f);
104
199
  }
105
- f.running = true;
106
- p = new Promise(function(res, rej){
107
- var xhr;
108
- if (f.blob) {
109
- return res(f);
200
+ xhr = new XMLHttpRequest();
201
+ xhr.addEventListener('readystatechange', function(){
202
+ if (xhr.readyState !== XMLHttpRequest.DONE) {
203
+ return;
110
204
  }
111
- xhr = new XMLHttpRequest();
112
- xhr.addEventListener('readystatechange', function(){
113
- if (xhr.readyState !== XMLHttpRequest.DONE) {
114
- return;
115
- }
116
- if (xhr.status !== 200) {
117
- return rej(err({
118
- code: xhr.status,
119
- message: xhr.responseText
120
- }));
121
- }
122
- this$.otf.dirty = true;
123
- f.url = URL.createObjectURL(xhr.response);
124
- f.blob = xhr.response;
125
- f.type = this$.ext.toLowerCase() || 'ttf';
126
- return res(f);
127
- });
128
- if (this$.isXl) {
129
- xhr.open('GET', this$.path + "/" + f.key + ".ttf");
130
- } else {
131
- xhr.open('GET', this$.path);
205
+ if (xhr.status !== 200) {
206
+ return rej(err({
207
+ code: xhr.status,
208
+ message: xhr.responseText
209
+ }));
132
210
  }
133
- xhr.responseType = 'blob';
134
- return xhr.send();
135
- });
136
- return p['finally'](function(){
137
- return f.running = false;
211
+ this$.otf.dirty = true;
212
+ f.url = URL.createObjectURL(xhr.response);
213
+ f.blob = xhr.response;
214
+ f.type = this$.ext.toLowerCase() || type;
215
+ return res(f);
138
216
  });
139
- });
140
- }
141
- return f.proxy(f);
142
- },
143
- fetchAll: function(){
144
- var k, f, this$ = this;
145
- if (this.isXl) {
146
- return Promise.all((function(){
147
- var ref$, results$ = [];
148
- for (k in ref$ = this.sub.font) {
149
- f = ref$[k];
150
- results$.push(f);
217
+ if (this$.isXl) {
218
+ xhr.open('GET', this$.path + "/" + f.key + "." + type);
219
+ } else {
220
+ xhr.open('GET', this$.path);
151
221
  }
152
- return results$;
153
- }.call(this)).map(function(it){
154
- return this$._fetch(it, true);
155
- }));
156
- } else {
157
- return this._fetch(this.sub.font[0], true);
158
- }
159
- },
160
- fetch: function(list, dofetch){
161
- var ps, this$ = this;
162
- list == null && (list = []);
163
- dofetch == null && (dofetch = false);
164
- if (!this.isXl) {
165
- if (this.sub.font[0].blob) {
166
- return Promise.resolve();
167
- }
168
- list = [0];
169
- }
170
- ps = Array.from(new Set(list.map(function(it){
171
- return it;
172
- }))).filter(function(it){
173
- return !this$.sub.font[it];
174
- }).map(function(it){
175
- var f;
176
- this$.sub.font[it] = f = {
177
- key: it
178
- };
179
- return this$._fetch(f, dofetch);
222
+ xhr.responseType = 'blob';
223
+ return xhr.send();
224
+ });
225
+ return p['finally'](function(){
226
+ return f.running = false;
227
+ });
180
228
  });
181
- return Promise.all(ps).then(function(subfonts){
182
- var css, k, ref$, f, i$, len$;
183
- if (!subfonts.length) {
184
- return;
185
- }
186
- css = "." + this$.className + " { font-family: \"" + this$.name + "\"; }";
187
- for (k in ref$ = this$.sub.font) {
229
+ }
230
+ return f.proxy(f);
231
+ },
232
+ fetchAll: function(){
233
+ var k, f, this$ = this;
234
+ if (this.isXl) {
235
+ return Promise.all((function(){
236
+ var ref$, results$ = [];
237
+ for (k in ref$ = this.sub.font) {
188
238
  f = ref$[k];
239
+ results$.push(f);
189
240
  }
190
- for (i$ = 0, len$ = subfonts.length; i$ < len$; ++i$) {
191
- f = subfonts[i$];
192
- css += "@font-face {\n font-family: \"" + this$.name + "\";\n src: url(\"" + f.url + "\") format('" + f.type + "');\n}";
241
+ return results$;
242
+ }.call(this)).map(function(it){
243
+ return this$._fetch(it, true, 'woff');
244
+ }));
245
+ } else {
246
+ return this._fetch(this.sub.font[0], true, 'ttf');
247
+ }
248
+ },
249
+ fetch: function(list, dofetch){
250
+ var doMerge, ps, this$ = this;
251
+ list == null && (list = []);
252
+ dofetch == null && (dofetch = false);
253
+ if (!this.isXl) {
254
+ if (this.sub.font[0] && this.sub.font[0].blob) {
255
+ return Promise.resolve();
256
+ }
257
+ list = [0];
258
+ }
259
+ doMerge = this.doMerge;
260
+ ps = Array.from(new Set(list.map(function(it){
261
+ return it;
262
+ }))).filter(function(it){
263
+ return !this$.sub.font[it];
264
+ }).map(function(d, i){
265
+ var f, type;
266
+ this$.sub.font[d] = f = {
267
+ key: d
268
+ };
269
+ type = !this$.isXl ? 'ttf' : 'woff';
270
+ f.needMerge = +d !== 1;
271
+ return this$._fetch(f, !doMerge ? dofetch : true, type);
272
+ });
273
+ return Promise.all(ps).then(function(subfonts){
274
+ var ps;
275
+ if (!doMerge) {
276
+ return subfonts;
277
+ }
278
+ ps = subfonts.filter(function(it){
279
+ return it.needMerge;
280
+ }).map(function(font){
281
+ return new Promise(function(res, rej){
282
+ var fr;
283
+ fr = new FileReader();
284
+ fr.onerror = function(it){
285
+ return rej(it);
286
+ };
287
+ fr.onload = function(){
288
+ return res(fr.result);
289
+ };
290
+ return fr.readAsArrayBuffer(font.blob);
291
+ });
292
+ });
293
+ return Promise.all(ps).then(function(it){
294
+ return xlfMerger({
295
+ bufs: it,
296
+ useWorker: this$.useWorker
297
+ });
298
+ }).then(function(ab){
299
+ var font, blob;
300
+ if (!ab) {
301
+ return subfonts.filter(function(it){
302
+ return !it.needMerge;
303
+ });
193
304
  }
194
- return this$.css.push({
195
- content: css
305
+ font = subfonts.filter(function(it){
306
+ return it.needMerge;
307
+ })[0];
308
+ blob = new Blob([ab], {
309
+ type: "font/" + (font.type || 'ttf')
196
310
  });
311
+ font = {
312
+ url: URL.createObjectURL(blob),
313
+ blob: blob,
314
+ type: font.type || 'ttf'
315
+ };
316
+ return subfonts.filter(function(it){
317
+ return !it.needMerge;
318
+ }).concat([font]);
197
319
  });
198
- },
199
- getotf: function(){
200
- var this$ = this;
201
- if (!(typeof opentype != 'undefined' && opentype !== null)) {
202
- return Promise.reject(err({
203
- id: 1022,
204
- message: "[@plotdb/xfl] need opentype.js to merge subfonts"
205
- }));
320
+ }).then(function(subfonts){
321
+ var css, k, ref$, f, i$, len$;
322
+ if (!subfonts.length) {
323
+ return;
206
324
  }
207
- if (!this.otf.dirty) {
208
- return Promise.resolve(this.otf.font);
325
+ css = "." + this$.className + " { font-family: \"" + this$.name + "\"; }";
326
+ for (k in ref$ = this$.sub.font) {
327
+ f = ref$[k];
209
328
  }
210
- return Promise.resolve().then(function(){
211
- if (!this$.isXl) {
212
- return this$.fetch();
213
- } else {
214
- return this$.fetchAll();
329
+ for (i$ = 0, len$ = subfonts.length; i$ < len$; ++i$) {
330
+ f = subfonts[i$];
331
+ css += "@font-face {\n font-family: \"" + this$.name + "\";\n src: url(\"" + f.url + "\") format('" + f.type + "');\n font-style: " + this$.style + ";\n font-weight: " + this$.weight + ";\n}";
332
+ }
333
+ return this$.css.push({
334
+ content: css
335
+ });
336
+ });
337
+ },
338
+ hasChar: function(c){
339
+ return typeof this.misschar[c] === 'undefined'
340
+ ? undefined
341
+ : !this.misshcar[c];
342
+ },
343
+ getPath: function(opt){
344
+ var ref$, text, x, y, fontSize, this$ = this;
345
+ opt == null && (opt = {});
346
+ ref$ = import$({
347
+ text: '',
348
+ x: 0,
349
+ y: 0,
350
+ fontSize: 48
351
+ }, opt), text = ref$.text, x = ref$.x, y = ref$.y, fontSize = ref$.fontSize;
352
+ return this.sync(text).then(function(){
353
+ return this$.getotf();
354
+ }).then(function(otf){
355
+ var txt, i$, to$, i, path;
356
+ txt = '';
357
+ for (i$ = 0, to$ = text.length; i$ < to$; ++i$) {
358
+ i = i$;
359
+ txt += !this$.misschar[text[i]] ? text[i] : ' ';
360
+ }
361
+ path = otf.getPath(txt, x, y, fontSize);
362
+ return path;
363
+ });
364
+ },
365
+ getotf: function(){
366
+ var this$ = this;
367
+ if (!(typeof opentype != 'undefined' && opentype !== null)) {
368
+ return Promise.reject(err({
369
+ id: 1022,
370
+ message: "[@plotdb/xfl] need opentype.js to merge subfonts"
371
+ }));
372
+ }
373
+ if (!this.otf.dirty) {
374
+ return Promise.resolve(this.otf.font);
375
+ }
376
+ return Promise.resolve().then(function(){
377
+ if (!this$.isXl) {
378
+ return this$.fetch();
379
+ } else {
380
+ return this$.fetchAll();
381
+ }
382
+ }).then(function(){
383
+ var ps, k, f;
384
+ ps = (function(){
385
+ var ref$, results$ = [];
386
+ for (k in ref$ = this.sub.font) {
387
+ f = ref$[k];
388
+ results$.push(f);
215
389
  }
216
- }).then(function(){
217
- var ps, k, f;
218
- ps = (function(){
219
- var ref$, results$ = [];
220
- for (k in ref$ = this.sub.font) {
221
- f = ref$[k];
222
- results$.push(f);
223
- }
224
- return results$;
225
- }.call(this$)).map(function(f){
226
- if (f.otf) {
227
- return Promise.resolve(f);
228
- } else {
229
- return opentype.load(f.url).then(function(it){
230
- f.otf = it;
231
- return f;
232
- });
233
- }
234
- });
235
- return Promise.all(ps);
236
- }).then(function(list){
237
- var glyphs, ref$, ref1$;
238
- list == null && (list = []);
239
- if (list.length === 1) {
240
- return list[0].otf;
390
+ return results$;
391
+ }.call(this$)).map(function(f){
392
+ if (f.otf) {
393
+ return Promise.resolve(f);
394
+ } else {
395
+ return opentype.load(f.url).then(function(it){
396
+ f.otf = it;
397
+ return f;
398
+ });
241
399
  }
242
- glyphs = list.map(function(f){
243
- var glyphs, i$, to$, i, results$ = [];
244
- glyphs = f.otf.glyphs;
245
- for (i$ = 1, to$ = glyphs.length; i$ <= to$; ++i$) {
246
- i = i$;
247
- results$.push(glyphs.glyphs[i]);
248
- }
249
- return results$;
250
- }).reduce(function(a, b){
251
- return a.concat(b);
252
- }, []).filter(function(it){
253
- return it;
254
- });
255
- this$.otf.font = new opentype.Font((ref1$ = {
256
- familyName: this$.name,
257
- styleName: this$.style || 'normal',
258
- glyphs: glyphs
259
- }, ref1$.unitsPerEm = (ref$ = list[0].otf).unitsPerEm, ref1$.ascender = ref$.ascender, ref1$.descender = ref$.descender, ref1$));
260
- this$.otf.font.kerningPairs = {};
261
- return this$.otf.font;
262
400
  });
263
- },
264
- sync: function(txt){
265
- var ref$, misschar, missset, this$ = this;
266
- txt == null && (txt = "");
267
- if (!this.isXl) {
268
- xfl.update();
269
- return Promise.resolve();
401
+ return Promise.all(ps);
402
+ }).then(function(list){
403
+ var glyphs, ref$, ref1$;
404
+ list == null && (list = []);
405
+ if (list.length === 1) {
406
+ return list[0].otf;
270
407
  }
271
- ref$ = [{}, {}], misschar = ref$[0], missset = ref$[1];
272
- return Promise.resolve().then(function(){
273
- var i$, to$, i, code, setIdx, k, list, res$;
274
- for (i$ = 0, to$ = txt.length; i$ < to$; ++i$) {
408
+ glyphs = list.map(function(f){
409
+ var glyphs, i$, to$, i, results$ = [];
410
+ glyphs = f.otf.glyphs;
411
+ for (i$ = 1, to$ = glyphs.length; i$ <= to$; ++i$) {
275
412
  i = i$;
276
- code = txt.charCodeAt(i);
277
- if (this$.cjkOnly && !xfl.isCJK(code)) {
278
- continue;
279
- }
280
- setIdx = this$.codemap[code.toString(16)];
281
- if (!setIdx) {
282
- misschar[txt[i]] = true;
283
- } else if (!this$.sub.set[setIdx]) {
284
- this$.sub.set[setIdx] = missset[setIdx] = true;
285
- }
413
+ results$.push(glyphs.glyphs[i]);
286
414
  }
287
- misschar = (function(){
288
- var results$ = [];
289
- for (k in misschar) {
290
- results$.push(k);
291
- }
292
- return results$;
293
- }()).filter(function(it){
294
- return it.trim();
295
- });
296
- if (misschar.length) {
297
- console.log("[@plotdb/xfl] sync xl-font with following chars unsupported: " + misschar.join(''));
415
+ return results$;
416
+ }).reduce(function(a, b){
417
+ return a.concat(b);
418
+ }, []).filter(function(it){
419
+ return it;
420
+ });
421
+ this$.otf.font = new opentype.Font((ref1$ = {
422
+ familyName: this$.name,
423
+ styleName: this$.style || 'normal',
424
+ glyphs: glyphs
425
+ }, ref1$.unitsPerEm = (ref$ = list[0].otf).unitsPerEm, ref1$.ascender = ref$.ascender, ref1$.descender = ref$.descender, ref1$));
426
+ this$.otf.font.kerningPairs = {};
427
+ return this$.otf.font;
428
+ });
429
+ },
430
+ sync: function(txt){
431
+ var this$ = this;
432
+ txt == null && (txt = "");
433
+ if (!this.isXl) {
434
+ xfl.update();
435
+ return Promise.resolve();
436
+ }
437
+ return Promise.resolve().then(function(){
438
+ var ref$, misscodes, missset, i$, to$, i, code, setIdx, k, list, res$;
439
+ ref$ = [{}, {}], misscodes = ref$[0], missset = ref$[1];
440
+ for (i$ = 0, to$ = txt.length; i$ < to$; ++i$) {
441
+ i = i$;
442
+ code = txt.charCodeAt(i);
443
+ if (this$.cjkOnly && !xfl.isCJK(code)) {
444
+ continue;
298
445
  }
299
- res$ = [];
300
- for (k in missset) {
301
- res$.push(k);
446
+ setIdx = this$.codemap[code.toString(16)];
447
+ this$.misschar[txt[i]] = misscodes[txt[i]] = !setIdx ? true : false;
448
+ if (setIdx && !this$.sub.set[setIdx]) {
449
+ this$.sub.set[setIdx] = missset[setIdx] = true;
302
450
  }
303
- list = res$;
304
- if (list.length) {
305
- return this$.fetch(list);
451
+ }
452
+ misscodes = (function(){
453
+ var results$ = [];
454
+ for (k in misscodes) {
455
+ results$.push(k);
306
456
  }
307
- }).then(function(){
308
- return xfl.update();
457
+ return results$;
458
+ }()).filter(function(it){
459
+ return misscodes[it];
309
460
  });
461
+ if (misscodes.length) {
462
+ console.log("[@plotdb/xfl] sync xl-font with following chars unsupported: " + misscodes.join(''));
463
+ }
464
+ res$ = [];
465
+ for (k in missset) {
466
+ res$.push(k);
467
+ }
468
+ list = res$;
469
+ if (list.length) {
470
+ return this$.fetch(list);
471
+ }
472
+ }).then(function(){
473
+ return xfl.update();
474
+ });
475
+ }
476
+ });
477
+ xfl = {
478
+ range: {
479
+ CJK: [[0x3040, 0x30ff], [0x3400, 0x4dbf], [0x4e00, 0x9fff], [0xf900, 0xfaff], [0xff66, 0xff9f], [0x3131, 0xD79D]]
480
+ },
481
+ fonts: {},
482
+ running: {},
483
+ isCJK: function(code){
484
+ return this.range.CJK.filter(function(it){
485
+ return code >= it[0] && code <= it[1];
486
+ }).length;
487
+ },
488
+ proxy: {},
489
+ update: function(){
490
+ var css, k, ref$, v, node, f;
491
+ css = "";
492
+ for (k in ref$ = this.fonts) {
493
+ v = ref$[k];
494
+ (v.css || []).filter(fn$).map(fn1$);
310
495
  }
311
- });
312
- xfl = {
313
- range: {
314
- CJK: [[0x3040, 0x30ff], [0x3400, 0x4dbf], [0x4e00, 0x9fff], [0xf900, 0xfaff], [0xff66, 0xff9f], [0x3131, 0xD79D]]
315
- },
316
- fonts: {},
317
- running: {},
318
- isCJK: function(code){
319
- return this.range.CJK.filter(function(it){
320
- return code >= it[0] && code <= it[1];
321
- }).length;
322
- },
323
- proxy: {},
324
- update: function(){
325
- var css, k, ref$, v, node;
326
- css = "";
496
+ if (css) {
497
+ node = document.createElement("style");
498
+ node.textContent = css;
499
+ node.setAttribute('type', 'text/css');
500
+ document.body.appendChild(node);
501
+ }
502
+ return Promise.all((function(){
503
+ var ref$, results$ = [];
327
504
  for (k in ref$ = this.fonts) {
328
- v = ref$[k];
329
- (v.css || []).filter(fn$).map(fn1$);
505
+ f = ref$[k];
506
+ results$.push(f);
330
507
  }
331
- if (css) {
332
- node = document.createElement("style");
333
- node.textContent = css;
334
- node.setAttribute('type', 'text/css');
335
- return document.body.appendChild(node);
336
- }
337
- function fn$(it){
338
- return !it.rendered;
508
+ return results$;
509
+ }.call(this)).map(function(){
510
+ return document.fonts.load("16px " + v.name);
511
+ })).then(function(){
512
+ return new Promise(function(res, rej){
513
+ return setTimeout(function(){
514
+ return res();
515
+ }, 350);
516
+ });
517
+ });
518
+ function fn$(it){
519
+ return !it.rendered;
520
+ }
521
+ function fn1$(it){
522
+ it.rendered = true;
523
+ return css = css + it.content;
524
+ }
525
+ },
526
+ _load: function(opt){
527
+ var path, this$ = this;
528
+ opt == null && (opt = {});
529
+ path = opt.path;
530
+ if (this.running[path]) {
531
+ return;
532
+ }
533
+ this.running[path] = true;
534
+ return Promise.resolve().then(function(){
535
+ var fobj;
536
+ this$.fonts[path] = fobj = new xlfont(opt);
537
+ return fobj.init();
538
+ })['finally'](function(){
539
+ return this$.running[path] = false;
540
+ }).then(function(){
541
+ this$.proxy[path].resolve(this$.fonts[path]);
542
+ return this$.fonts[path];
543
+ })['catch'](function(it){
544
+ this$.proxy[path].reject(it);
545
+ return Promise.reject(it);
546
+ });
547
+ },
548
+ load: function(opt){
549
+ var this$ = this;
550
+ opt == null && (opt = {});
551
+ if (typeof opt === 'string') {
552
+ opt = {
553
+ path: opt
554
+ };
555
+ }
556
+ return new Promise(function(res, rej){
557
+ var path, that, ref$;
558
+ if (!(path = opt.path)) {
559
+ return rej(err({
560
+ id: 400
561
+ }));
339
562
  }
340
- function fn1$(it){
341
- it.rendered = true;
342
- return css = css + it.content;
563
+ path = path.replace(/\/$/, '');
564
+ if (that = this$.fonts[path]) {
565
+ return res(that);
343
566
  }
344
- },
345
- _load: function(opt){
346
- var path, this$ = this;
347
- opt == null && (opt = {});
348
- path = opt.path;
349
- if (this.running[path]) {
350
- return;
567
+ if (!this$.proxy[path]) {
568
+ this$.proxy[path] = proxise(function(it){
569
+ return this$._load(it);
570
+ });
351
571
  }
352
- this.running[path] = true;
353
- return Promise.resolve().then(function(){
354
- var fobj;
355
- this$.fonts[path] = fobj = new xlfont(opt);
356
- return fobj.init();
357
- })['finally'](function(){
358
- return this$.running[path] = false;
359
- }).then(function(){
360
- this$.proxy[path].resolve(this$.fonts[path]);
361
- return this$.fonts[path];
572
+ return this$.proxy[path]((ref$ = import$({}, opt), ref$.path = path, ref$)).then(function(it){
573
+ return res(it);
362
574
  })['catch'](function(it){
363
- this$.proxy[path].reject(it);
364
- return Promise.reject(it);
575
+ return rej(it);
365
576
  });
366
- },
367
- load: function(opt){
368
- var this$ = this;
369
- opt == null && (opt = {});
370
- return new Promise(function(res, rej){
371
- var path, that, ref$;
372
- if (!(path = opt.path)) {
373
- return rej(err({
374
- id: 400
375
- }));
376
- }
377
- path = path.replace(/\/$/, '');
378
- if (that = this$.fonts[path]) {
379
- return res(that);
380
- }
381
- if (!this$.proxy[path]) {
382
- this$.proxy[path] = proxise(function(it){
383
- return this$._load(it);
384
- });
385
- }
386
- return this$.proxy[path]((ref$ = import$({}, opt), ref$.path = path, ref$)).then(function(it){
387
- return res(it);
388
- })['catch'](function(it){
389
- return rej(it);
390
- });
391
- });
392
- }
393
- };
394
- if (typeof module != 'undefined' && module !== null) {
395
- module.exports = xfl;
396
- } else if (typeof window != 'undefined' && window !== null) {
397
- window.xfl = xfl;
398
- }
399
- function import$(obj, src){
400
- var own = {}.hasOwnProperty;
401
- for (var key in src) if (own.call(src, key)) obj[key] = src[key];
402
- return obj;
577
+ });
403
578
  }
404
- }).call(this);
579
+ };
580
+ if (typeof module != 'undefined' && module !== null) {
581
+ module.exports = xfl;
582
+ } else if (typeof window != 'undefined' && window !== null) {
583
+ window.xfl = xfl;
584
+ }
585
+ function import$(obj, src){
586
+ var own = {}.hasOwnProperty;
587
+ for (var key in src) if (own.call(src, key)) obj[key] = src[key];
588
+ return obj;
589
+ }
590
+ }());