@xlfont/load 2.0.0 → 2.0.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,119 @@
1
+ # Change Logs
2
+
3
+ ## v2.0.1
4
+
5
+ - publish module with files under `dist` in root too.
6
+
7
+
8
+ ## v2.0.0
9
+
10
+ - remove deprecated `xfl.js` and `xfl.min.js`
11
+ - further minimize generated js file with mangling and compression
12
+ - update `main`, add `browser` field in `package.json`.
13
+ - upgrade modules
14
+ - patch test code to make it work with upgraded modules
15
+ - fix bug: opentype-url doesn't work
16
+ - use Object URL for worker loading so we don't have to specify its path by ourselves
17
+
18
+
19
+ ## v1.1.1
20
+
21
+ - correctly support web worker by accepting options for worker and dependency url
22
+
23
+
24
+ ## v1.1.0
25
+
26
+ - support merging font locally by opentype.js
27
+ - support web worker ( TBD )
28
+ - suppoty `style` and `weight` information
29
+ - wait explicitly until fonts loaded according to document.fonts in `xfl.update`
30
+ - accept `use-worker` as an option, but still dont enable it
31
+ - normalize font name if it's derived from url
32
+ - load xlfont with woff instead of ttf.
33
+ - support `hasChar(c)`
34
+ - support `getPath(opt)` as xlfont method, which check chars for availability before rendering.
35
+ - let misschar also keep tracks of supported char (by setting it to false)
36
+
37
+
38
+ ## v1.0.13
39
+
40
+ - null check sub.font[0] before using it
41
+ - support string param in `load` api
42
+ - point entry point from `xfl.js` to `index.js`
43
+
44
+
45
+ ## v1.0.12
46
+
47
+ - add back proxise since we can't actually remove it for now
48
+
49
+
50
+ ## v1.0.11
51
+
52
+ - remove dependency to proxise by inlining `once` function
53
+ - rename `xfl.js` while keeping `index.js`, which will be removed in future minor update
54
+ - rename `xfont` to `xlfont` to reflect package name.
55
+
56
+
57
+ ## v1.0.10
58
+
59
+ - escape space in class name
60
+ - quote name in font family name that didn't quote in the last version
61
+
62
+
63
+ ## v1.0.9
64
+
65
+ - escape space in font name
66
+ - quote name and url in font family styles
67
+ - call update for non-xl fonts
68
+ - fix incorrectly assignment in xfl.update
69
+
70
+
71
+ ## v1.0.8
72
+
73
+ - fix bug: `-a` option doesn't work in `xfl-meta`
74
+
75
+
76
+ ## v1.0.7
77
+
78
+ - fix incorrect versioning issue
79
+
80
+
81
+ ## v1.0.5 / v1.0.6
82
+
83
+ - skip notdef by reference instead of by name. This fixed `Taipei Sans TC Beta` generation issue.
84
+
85
+
86
+ ## v1.0.4
87
+
88
+ - handle newlines in metadata
89
+
90
+
91
+ ## v1.0.3
92
+
93
+ - support subsetting only single font in xfl command
94
+ - better formatting of xfl-meta output
95
+
96
+
97
+ ## v1.0.2
98
+
99
+ - add `xfl-meta` command
100
+ - add missing dependency in `build.js`
101
+ - update `package.json` for some dependencies that should be for production
102
+
103
+
104
+ ## v1.0.1
105
+
106
+ - remove npm-shrinkwrap.json
107
+
108
+
109
+ ## v1.0.0
110
+
111
+ - completely rewrite xfl.ls
112
+ - support font merging based on opentype.js
113
+ - non-xl-font aware
114
+ - better support for concurrent fetching
115
+ - support promise in all api
116
+ - use SIL-Open Licensed fonts only for demonstration
117
+ - remove fontmin / gulp-rename dependency
118
+ - add `woff` format output
119
+ - use `@plotdb/opentype.js` for bug fixing
package/index.js ADDED
@@ -0,0 +1,590 @@
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;
14
+ opt == null && (opt = {});
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
+ });
55
+ });
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: {}
97
+ };
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;
158
+ });
159
+ });
160
+ return res();
161
+ });
162
+ xhr.open('GET', this$.path + "/charmap.txt");
163
+ return xhr.send();
164
+ });
165
+ }
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
+ }
182
+ }
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);
199
+ }
200
+ xhr = new XMLHttpRequest();
201
+ xhr.addEventListener('readystatechange', function(){
202
+ if (xhr.readyState !== XMLHttpRequest.DONE) {
203
+ return;
204
+ }
205
+ if (xhr.status !== 200) {
206
+ return rej(err({
207
+ code: xhr.status,
208
+ message: xhr.responseText
209
+ }));
210
+ }
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);
216
+ });
217
+ if (this$.isXl) {
218
+ xhr.open('GET', this$.path + "/" + f.key + "." + type);
219
+ } else {
220
+ xhr.open('GET', this$.path);
221
+ }
222
+ xhr.responseType = 'blob';
223
+ return xhr.send();
224
+ });
225
+ return p['finally'](function(){
226
+ return f.running = false;
227
+ });
228
+ });
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) {
238
+ f = ref$[k];
239
+ results$.push(f);
240
+ }
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
+ });
304
+ }
305
+ font = subfonts.filter(function(it){
306
+ return it.needMerge;
307
+ })[0];
308
+ blob = new Blob([ab], {
309
+ type: "font/" + (font.type || 'ttf')
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]);
319
+ });
320
+ }).then(function(subfonts){
321
+ var css, k, ref$, f, i$, len$;
322
+ if (!subfonts.length) {
323
+ return;
324
+ }
325
+ css = "." + this$.className + " { font-family: \"" + this$.name + "\"; }";
326
+ for (k in ref$ = this$.sub.font) {
327
+ f = ref$[k];
328
+ }
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);
389
+ }
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
+ });
399
+ }
400
+ });
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;
407
+ }
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$) {
412
+ i = i$;
413
+ results$.push(glyphs.glyphs[i]);
414
+ }
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;
445
+ }
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;
450
+ }
451
+ }
452
+ misscodes = (function(){
453
+ var results$ = [];
454
+ for (k in misscodes) {
455
+ results$.push(k);
456
+ }
457
+ return results$;
458
+ }()).filter(function(it){
459
+ return misscodes[it];
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$);
495
+ }
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$ = [];
504
+ for (k in ref$ = this.fonts) {
505
+ f = ref$[k];
506
+ results$.push(f);
507
+ }
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
+ }));
562
+ }
563
+ path = path.replace(/\/$/, '');
564
+ if (that = this$.fonts[path]) {
565
+ return res(that);
566
+ }
567
+ if (!this$.proxy[path]) {
568
+ this$.proxy[path] = proxise(function(it){
569
+ return this$._load(it);
570
+ });
571
+ }
572
+ return this$.proxy[path]((ref$ = import$({}, opt), ref$.path = path, ref$)).then(function(it){
573
+ return res(it);
574
+ })['catch'](function(it){
575
+ return rej(it);
576
+ });
577
+ });
578
+ }
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
+ }());
package/index.min.js ADDED
@@ -0,0 +1 @@
1
+ !function(){var i,n,t,o,c;function l(e,t){var n,r={}.hasOwnProperty;for(n in t)r.call(t,n)&&(e[n]=t[n]);return e}i=function(e){return null==e&&(e={}),l(new Error,l({name:"lderror"},e))},n={key:0,worker:null,queue:[],init:function(e){var r=this;return null==e&&(e={}),Promise.resolve().then(function(){var e;if(!r.worker)return e=URL.createObjectURL(new Blob(['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})})};'],{type:"text/javascript"})),r.worker=new Worker(e),URL.revokeObjectURL(e),r.worker.onmessage=function(e){var t=e.data,e=t.buf,n=t.key;if(t=r.queue.filter(function(e){return n===e.key})[0])return r.queue.splice(r.queue.indexOf(t),1),t.res(e)}})},run:function(r,o){var s=this;return new Promise(function(t,n){return s.init(o).then(function(){var e;return s.queue.push(e={res:t,rej:n,key:s.key++}),console.log(">",o),s.worker.postMessage({bufs:r,key:e.key,lib:o.opentypeUrl})})})}},t=function(e){var t=e.bufs,e=e.useWorker;return t.length?e?n.run(t,e):Promise.resolve().then(function(){return t.map(function(e){return opentype.parse(e)})}).then(function(e){for(var t,n,r,o,s,i=[],u=0,f=e.length;u<f;++u)for(n=0,r=(t=e[u]).glyphs.length;n<r;++n)o=n,i.push(t.glyphs.glyphs[o]);return t=e[0],new opentype.Font(((s={glyphs:i,familyName:t.names.fontFamily.en,styleName:t.names.fontSubfamily.en}).unitsPerEm=t.unitsPerEm,s.ascender=t.ascender,s.descender=t.descender,s)).toArrayBuffer()}):Promise.resolve()},(o=function(e){var t,n=this;return this.opt=e=null==e?{}:e,this.sub={set:{},font:{}},this.cjkOnly=e.cjkOnly||!1,this.codemap={},this.otf={font:null,dirty:!0},this.path=e.path,this.name=e.name||this.path.replace(/\.[a-zA-Z0-9]+$/,"").replace(/^https?:\/\/[^/]+\//g,"").replace(/\s/g,"-").replace(/\//g,"-").toLowerCase(),this.style=e.style||"normal",this.weight=e.weight||"400",this.ext=e.ext||(/\.(ttf|otf|woff2|woff)$/.exec(this.path)||[])[1]||"",this.format=(t=this.ext.toLowerCase())?"ttf"===t||"otf"===t?"truetype":t:"",this.format&&(this.format="format('"+this.format+"')"),this.className="xfl-"+(this.name||"").replace(/\s+/g,"_")+"-"+Math.random().toString(36).substring(2),this.isXl=null!=e.isXl?e.isXl:!this.ext,this.ext||(this.ext="woff"),this.doMerge=null!=e.doMerge&&e.doMerge,this.useWorker=null!=e.useWorker&&e.useWorker,this.misschar={},this.css=[],this.init=proxise.once(function(){return n._init()}),this.init(),this}).prototype=l(Object.create(Object.prototype),{_init:function(){var r=this;return Promise.resolve().then(function(){return r.isXl?new Promise(function(e,t){var n=new XMLHttpRequest;return n.addEventListener("readystatechange",function(){if(n.readyState===XMLHttpRequest.DONE)return 200!==n.status?t(i({code:n.status,message:n.responseText})):((n.responseText||"").split("\n").map(function(e,n){return e.split(" ").map(function(e,t){return r.codemap[e]=n+1})}),e())}),n.open("GET",r.path+"/charmap.txt"),n.send()}):(r.css=[{content:'@font-face {\n font-family: "'+r.name+'";\n src: url("'+r.path+'") '+r.format+";\n font-style: "+r.style+";\n font-weight: "+r.weight+";\n}\n."+r.className+' { font-family: "'+r.name+'"; }'}],c.update())})},_fetch:function(e,t,o){var s=this;return null==e&&(e={}),null==o&&(o="ttf"),(t=null!=t&&t)?e.blob?Promise.resolve(e):(e.proxy||(e.proxy=proxise(function(r){if(!r.running)return r.running=!0,new Promise(function(e,t){var n;return r.blob?e(r):((n=new XMLHttpRequest).addEventListener("readystatechange",function(){if(n.readyState===XMLHttpRequest.DONE)return 200!==n.status?t(i({code:n.status,message:n.responseText})):(s.otf.dirty=!0,r.url=URL.createObjectURL(n.response),r.blob=n.response,r.type=s.ext.toLowerCase()||o,e(r))}),s.isXl?n.open("GET",s.path+"/"+r.key+"."+o):n.open("GET",s.path),n.responseType="blob",n.send())}).finally(function(){return r.running=!1})})),e.proxy(e)):(e.url||(this.isXl?(e.url=this.path+"/"+e.key+"."+o,e.type=o):(e.url=this.path,e.type=this.ext.toLowerCase()||o)),Promise.resolve(e))},fetchAll:function(){var n,r,t=this;return this.isXl?Promise.all(function(){var e,t=[];for(n in e=this.sub.font)r=e[n],t.push(r);return t}.call(this).map(function(e){return t._fetch(e,!0,"woff")})):this._fetch(this.sub.font[0],!0,"ttf")},fetch:function(e,o){var s,u=this;if(null==e&&(e=[]),null==o&&(o=!1),!this.isXl){if(this.sub.font[0]&&this.sub.font[0].blob)return Promise.resolve();e=[0]}return s=this.doMerge,e=Array.from(new Set(e.map(function(e){return e}))).filter(function(e){return!u.sub.font[e]}).map(function(e,t){var n,r;return u.sub.font[e]=n={key:e},r=u.isXl?"woff":"ttf",n.needMerge=1!=+e,u._fetch(n,!!s||o,r)}),Promise.all(e).then(function(n){var e;return s?(e=n.filter(function(e){return e.needMerge}).map(function(r){return new Promise(function(e,t){var n=new FileReader;return n.onerror=function(e){return t(e)},n.onload=function(){return e(n.result)},n.readAsArrayBuffer(r.blob)})}),Promise.all(e).then(function(e){return t({bufs:e,useWorker:u.useWorker})}).then(function(e){var t;return e?(t=n.filter(function(e){return e.needMerge})[0],e=new Blob([e],{type:"font/"+(t.type||"ttf")}),t={url:URL.createObjectURL(e),blob:e,type:t.type||"ttf"},n.filter(function(e){return!e.needMerge}).concat([t])):n.filter(function(e){return!e.needMerge})})):n}).then(function(e){var t,n,r,o,s,i;if(e.length){for(n in t="."+u.className+' { font-family: "'+u.name+'"; }',r=u.sub.font)o=r[n];for(s=0,i=e.length;s<i;++s)o=e[s],t+='@font-face {\n font-family: "'+u.name+'";\n src: url("'+o.url+"\") format('"+o.type+"');\n font-style: "+u.style+";\n font-weight: "+u.weight+";\n}";return u.css.push({content:t})}})},hasChar:function(e){return void 0===this.misschar[e]?void 0:!this.misshcar[e]},getPath:function(e){var s=this,e=l({text:"",x:0,y:0,fontSize:48},e=null==e?{}:e),i=e.text,u=e.x,f=e.y,a=e.fontSize;return this.sync(i).then(function(){return s.getotf()}).then(function(e){for(var t,n="",r=0,o=i.length;r<o;++r)t=r,n+=s.misschar[i[t]]?" ":i[t];return e.getPath(n,u,f,a)})},getotf:function(){var o=this;return"undefined"==typeof opentype||null===opentype?Promise.reject(i({id:1022,message:"[@plotdb/xfl] need opentype.js to merge subfonts"})):this.otf.dirty?Promise.resolve().then(function(){return o.isXl?o.fetchAll():o.fetch()}).then(function(){var n,r,e=function(){var e,t=[];for(n in e=this.sub.font)r=e[n],t.push(r);return t}.call(o).map(function(t){return t.otf?Promise.resolve(t):opentype.load(t.url).then(function(e){return t.otf=e,t})});return Promise.all(e)}).then(function(e){var t;return 1===(e=null==e?[]:e).length?e[0].otf:(t=e.map(function(e){for(var t,n=[],r=e.otf.glyphs,o=1,s=r.length;o<=s;++o)t=o,n.push(r.glyphs[t]);return n}).reduce(function(e,t){return e.concat(t)},[]).filter(function(e){return e}),o.otf.font=new opentype.Font(((t={familyName:o.name,styleName:o.style||"normal",glyphs:t}).unitsPerEm=(e=e[0].otf).unitsPerEm,t.ascender=e.ascender,t.descender=e.descender,t)),o.otf.font.kerningPairs={},o.otf.font)}):Promise.resolve(this.otf.font)},sync:function(a){var l=this;return null==a&&(a=""),this.isXl?Promise.resolve().then(function(){for(var e,t,n,r,o=[{},{}],s=o[0],i=o[1],u=0,f=a.length;u<f;++u)e=u,t=a.charCodeAt(e),l.cjkOnly&&!c.isCJK(t)||(t=l.codemap[t.toString(16)],l.misschar[a[e]]=s[a[e]]=!t,t&&!l.sub.set[t]&&(l.sub.set[t]=i[t]=!0));for(n in(s=function(){var e=[];for(n in s)e.push(n);return e}().filter(function(e){return s[e]})).length&&console.log("[@plotdb/xfl] sync xl-font with following chars unsupported: "+s.join("")),r=[],i)r.push(n);if((o=r).length)return l.fetch(o)}).then(function(){return c.update()}):(c.update(),Promise.resolve())}}),c={range:{CJK:[[12352,12543],[13312,19903],[19968,40959],[63744,64255],[65382,65439],[12593,55197]]},fonts:{},running:{},isCJK:function(t){return this.range.CJK.filter(function(e){return t>=e[0]&&t<=e[1]}).length},proxy:{},update:function(){var n,e,t,r,o,s="";for(n in e=this.fonts)((t=e[n]).css||[]).filter(i).map(u);return s&&((r=document.createElement("style")).textContent=s,r.setAttribute("type","text/css"),document.body.appendChild(r)),Promise.all(function(){var e,t=[];for(n in e=this.fonts)o=e[n],t.push(o);return t}.call(this).map(function(){return document.fonts.load("16px "+t.name)})).then(function(){return new Promise(function(e,t){return setTimeout(function(){return e()},350)})});function i(e){return!e.rendered}function u(e){return e.rendered=!0,s+=e.content}},_load:function(t){var n=this,r=(t=null==t?{}:t).path;if(!this.running[r])return this.running[r]=!0,Promise.resolve().then(function(){var e;return n.fonts[r]=e=new o(t),e.init()}).finally(function(){return n.running[r]=!1}).then(function(){return n.proxy[r].resolve(n.fonts[r]),n.fonts[r]}).catch(function(e){return n.proxy[r].reject(e),Promise.reject(e)})},load:function(o){var s=this;return"string"==typeof(o=null==o?{}:o)&&(o={path:o}),new Promise(function(t,n){var e,r;return(e=o.path)?(e=e.replace(/\/$/,""),(r=s.fonts[e])?t(r):(s.proxy[e]||(s.proxy[e]=proxise(function(e){return s._load(e)})),s.proxy[e](((r=l({},o)).path=e,r)).then(function(e){return t(e)}).catch(function(e){return n(e)}))):n(i({id:400}))})}},"undefined"!=typeof module&&null!==module?module.exports=c:"undefined"!=typeof window&&null!==window&&(window.xfl=c)}();
package/package.json CHANGED
@@ -1,78 +1 @@
1
- {
2
- "author": "zbryikt",
3
- "name": "@xlfont/load",
4
- "license": "MIT",
5
- "description": "XL font generator and loader",
6
- "version": "2.0.0",
7
- "browser": "dist/index.min.js",
8
- "main": "dist/index.min.js",
9
- "homepage": "https://github.com/xlfont/load",
10
- "files": [
11
- "dist/**/*",
12
- "tool/data/*"
13
- ],
14
- "repository": {
15
- "type": "git",
16
- "url": "https://github.com/xlfont/load"
17
- },
18
- "engines": {
19
- "node": ">=10.14.1",
20
- "npm": ">=6.4.1"
21
- },
22
- "scripts": {
23
- "start": "npx server -r web -o true"
24
- },
25
- "bin": {
26
- "xfl": "./bin/build.js",
27
- "xfl-meta": "./bin/meta.js"
28
- },
29
- "dependencies": {
30
- "@plotdb/opentype.js": "^1.3.5",
31
- "@plotdb/colors": "^0.0.1",
32
- "fs-extra": "^9.1.0",
33
- "progress": "^2.0.3",
34
- "ttf2woff": "^2.0.2",
35
- "ttf2woff2": "^4.0.0",
36
- "yargs": "^16.2.0"
37
- },
38
- "devDependencies": {
39
- "@loadingio/bootstrap.ext": "^0.0.4",
40
- "@loadingio/debounce.js": "^1.0.1",
41
- "@loadingio/ldquery": "^3.0.1",
42
- "@loadingio/loading.css": "^3.0.0",
43
- "@zbryikt/deploy": "^0.0.1",
44
- "@zbryikt/template": "^2.3.33",
45
- "bootstrap": "^4.5.3",
46
- "bootstrap.native": "^3.0.13",
47
- "fedep": "^1.1.0",
48
- "ldbutton": "^2.0.0",
49
- "ldcover": "^3.0.1",
50
- "ldloader": "^3.0.0",
51
- "ldview": "^1.0.0",
52
- "livescript": "^1.6.0",
53
- "stylus": "^0.55.0",
54
- "uglify-js": "^3.13.1",
55
- "uglifycss": "^0.0.29"
56
- },
57
- "peerDependencies": {
58
- "@plotdb/opentype.js": "^1.3.5",
59
- "proxise": "^1.0.1"
60
- },
61
- "frontendDependencies": {
62
- "root": "web/static/assets/lib",
63
- "modules": [
64
- "bootstrap",
65
- "bootstrap.native",
66
- "@loadingio/bootstrap.ext",
67
- "@loadingio/ldquery",
68
- "@loadingio/debounce.js",
69
- "@loadingio/loading.css",
70
- "@plotdb/opentype.js",
71
- "ldview",
72
- "ldcover",
73
- "ldloader",
74
- "ldbutton",
75
- "proxise"
76
- ]
77
- }
78
- }
1
+ {"author":"zbryikt","name":"@xlfont/load","license":"MIT","description":"XL font generator and loader","version":"2.0.1","browser":"index.min.js","main":"index.min.js","homepage":"https://github.com/xlfont/load","repository":{"type":"git","url":"https://github.com/xlfont/load"},"engines":{"node":">=10.14.1","npm":">=6.4.1"},"scripts":{"start":"npx server -r web -o true"},"bin":{"xfl":"./bin/build.js","xfl-meta":"./bin/meta.js"},"dependencies":{"@plotdb/colors":"^0.0.1","@plotdb/opentype.js":"^1.3.5","fs-extra":"^9.1.0","progress":"^2.0.3","ttf2woff":"^2.0.2","ttf2woff2":"^4.0.0","yargs":"^16.2.0"},"devDependencies":{"@loadingio/bootstrap.ext":"^0.0.4","@loadingio/debounce.js":"^1.0.1","@loadingio/ldquery":"^3.0.1","@loadingio/loading.css":"^3.0.0","@zbryikt/deploy":"^0.0.1","@zbryikt/template":"^2.3.33","bootstrap":"^4.5.3","bootstrap.native":"^3.0.13","fedep":"^1.1.1","glob":"^7.2.0","ldbutton":"^2.0.0","ldcover":"^3.0.1","ldloader":"^3.0.0","ldview":"^1.0.0","livescript":"^1.6.0","stylus":"^0.55.0","uglify-js":"^3.13.1","uglifycss":"^0.0.29"},"peerDependencies":{"@plotdb/opentype.js":"^1.3.5","proxise":"^1.0.1"},"frontendDependencies":{"root":"web/static/assets/lib","modules":["bootstrap","bootstrap.native","@loadingio/bootstrap.ext","@loadingio/ldquery","@loadingio/debounce.js","@loadingio/loading.css","@plotdb/opentype.js","ldview","ldcover","ldloader","ldbutton","proxise"]}}
package/worker.js ADDED
@@ -0,0 +1,36 @@
1
+ var local, onmessage;
2
+ local = {};
3
+ onmessage = function(e){
4
+ var ref$, bufs, key, lib;
5
+ ref$ = e.data || {}, bufs = ref$.bufs, key = ref$.key, lib = ref$.lib;
6
+ if (!local.inited) {
7
+ console.log(lib);
8
+ importScripts(lib || "opentype.js");
9
+ local.inited = true;
10
+ }
11
+ return Promise.resolve().then(function(){
12
+ return bufs.map(function(it){
13
+ return opentype.parse(it);
14
+ });
15
+ }).then(function(fonts){
16
+ var glyphs, i$, len$, font, j$, to$, i, merged, ref$;
17
+ glyphs = [];
18
+ for (i$ = 0, len$ = fonts.length; i$ < len$; ++i$) {
19
+ font = fonts[i$];
20
+ for (j$ = 0, to$ = font.glyphs.length; j$ < to$; ++j$) {
21
+ i = j$;
22
+ glyphs.push(font.glyphs.glyphs[i]);
23
+ }
24
+ }
25
+ font = fonts[0];
26
+ merged = new opentype.Font((ref$ = {
27
+ glyphs: glyphs,
28
+ familyName: font.names.fontFamily.en,
29
+ styleName: font.names.fontSubfamily.en
30
+ }, ref$.unitsPerEm = font.unitsPerEm, ref$.ascender = font.ascender, ref$.descender = font.descender, ref$));
31
+ return postMessage({
32
+ buf: merged.toArrayBuffer(),
33
+ key: key
34
+ });
35
+ });
36
+ };
package/worker.min.js ADDED
@@ -0,0 +1 @@
1
+ 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})})};