@xlfont/load 1.1.0 → 2.0.2
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 +124 -0
- package/README.md +38 -10
- package/bin/build.js +4 -4
- package/bin/meta.js +1 -1
- package/dist/index.js +41 -31
- package/dist/index.min.js +1 -1
- package/dist/worker.js +8 -4
- package/dist/worker.min.js +1 -1
- package/{dist/xfl.js → index.js} +41 -31
- package/index.min.js +1 -0
- package/package.json +1 -76
- package/worker.js +35 -0
- package/worker.min.js +1 -0
- package/dist/xfl.min.js +0 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Change Logs
|
|
2
|
+
|
|
3
|
+
## v2.0.2
|
|
4
|
+
|
|
5
|
+
- fix bug: default value for input dir and `output` directory should be `font` and `output`.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## v2.0.1
|
|
9
|
+
|
|
10
|
+
- publish module with files under `dist` in root too.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## v2.0.0
|
|
14
|
+
|
|
15
|
+
- remove deprecated `xfl.js` and `xfl.min.js`
|
|
16
|
+
- further minimize generated js file with mangling and compression
|
|
17
|
+
- update `main`, add `browser` field in `package.json`.
|
|
18
|
+
- upgrade modules
|
|
19
|
+
- patch test code to make it work with upgraded modules
|
|
20
|
+
- fix bug: opentype-url doesn't work
|
|
21
|
+
- use Object URL for worker loading so we don't have to specify its path by ourselves
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## v1.1.1
|
|
25
|
+
|
|
26
|
+
- correctly support web worker by accepting options for worker and dependency url
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## v1.1.0
|
|
30
|
+
|
|
31
|
+
- support merging font locally by opentype.js
|
|
32
|
+
- support web worker ( TBD )
|
|
33
|
+
- suppoty `style` and `weight` information
|
|
34
|
+
- wait explicitly until fonts loaded according to document.fonts in `xfl.update`
|
|
35
|
+
- accept `use-worker` as an option, but still dont enable it
|
|
36
|
+
- normalize font name if it's derived from url
|
|
37
|
+
- load xlfont with woff instead of ttf.
|
|
38
|
+
- support `hasChar(c)`
|
|
39
|
+
- support `getPath(opt)` as xlfont method, which check chars for availability before rendering.
|
|
40
|
+
- let misschar also keep tracks of supported char (by setting it to false)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## v1.0.13
|
|
44
|
+
|
|
45
|
+
- null check sub.font[0] before using it
|
|
46
|
+
- support string param in `load` api
|
|
47
|
+
- point entry point from `xfl.js` to `index.js`
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## v1.0.12
|
|
51
|
+
|
|
52
|
+
- add back proxise since we can't actually remove it for now
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
## v1.0.11
|
|
56
|
+
|
|
57
|
+
- remove dependency to proxise by inlining `once` function
|
|
58
|
+
- rename `xfl.js` while keeping `index.js`, which will be removed in future minor update
|
|
59
|
+
- rename `xfont` to `xlfont` to reflect package name.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## v1.0.10
|
|
63
|
+
|
|
64
|
+
- escape space in class name
|
|
65
|
+
- quote name in font family name that didn't quote in the last version
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
## v1.0.9
|
|
69
|
+
|
|
70
|
+
- escape space in font name
|
|
71
|
+
- quote name and url in font family styles
|
|
72
|
+
- call update for non-xl fonts
|
|
73
|
+
- fix incorrectly assignment in xfl.update
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
## v1.0.8
|
|
77
|
+
|
|
78
|
+
- fix bug: `-a` option doesn't work in `xfl-meta`
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
## v1.0.7
|
|
82
|
+
|
|
83
|
+
- fix incorrect versioning issue
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
## v1.0.5 / v1.0.6
|
|
87
|
+
|
|
88
|
+
- skip notdef by reference instead of by name. This fixed `Taipei Sans TC Beta` generation issue.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
## v1.0.4
|
|
92
|
+
|
|
93
|
+
- handle newlines in metadata
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
## v1.0.3
|
|
97
|
+
|
|
98
|
+
- support subsetting only single font in xfl command
|
|
99
|
+
- better formatting of xfl-meta output
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
## v1.0.2
|
|
103
|
+
|
|
104
|
+
- add `xfl-meta` command
|
|
105
|
+
- add missing dependency in `build.js`
|
|
106
|
+
- update `package.json` for some dependencies that should be for production
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
## v1.0.1
|
|
110
|
+
|
|
111
|
+
- remove npm-shrinkwrap.json
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
## v1.0.0
|
|
115
|
+
|
|
116
|
+
- completely rewrite xfl.ls
|
|
117
|
+
- support font merging based on opentype.js
|
|
118
|
+
- non-xl-font aware
|
|
119
|
+
- better support for concurrent fetching
|
|
120
|
+
- support promise in all api
|
|
121
|
+
- use SIL-Open Licensed fonts only for demonstration
|
|
122
|
+
- remove fontmin / gulp-rename dependency
|
|
123
|
+
- add `woff` format output
|
|
124
|
+
- use `@plotdb/opentype.js` for bug fixing
|
package/README.md
CHANGED
|
@@ -15,11 +15,11 @@ This is a javascript project based on nodeJS / npm. After installing node / npm,
|
|
|
15
15
|
npm install --save @xlfont/load
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
##
|
|
18
|
+
## xlfont subsetting generator
|
|
19
19
|
|
|
20
20
|
use `npx xfl` to subset fonts with `xfl`:
|
|
21
21
|
|
|
22
|
-
npx xfl font-dir [-f word-frequency-csv] [-
|
|
22
|
+
npx xfl font-dir [o output-dir] [-f word-frequency-csv] [-c major-subset-size] [-s subset-size]
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
where:
|
|
@@ -54,11 +54,11 @@ Generally speaking, the steps of font subsetting by `xfl` are as following:
|
|
|
54
54
|
- dump unicode / subset mapping to `charmap.txt`. also keep the original font as `all.ttf`.
|
|
55
55
|
|
|
56
56
|
|
|
57
|
-
##
|
|
57
|
+
## xlfont subset loader
|
|
58
58
|
|
|
59
59
|
include the locally installed dist files or from cdn:
|
|
60
60
|
|
|
61
|
-
<script src="path-to/xfl
|
|
61
|
+
<script src="path-to/xfl-js"></script>
|
|
62
62
|
|
|
63
63
|
|
|
64
64
|
Then, load desired font:
|
|
@@ -74,8 +74,36 @@ Sync font based on given text:
|
|
|
74
74
|
});
|
|
75
75
|
|
|
76
76
|
|
|
77
|
+
## xfl API
|
|
77
78
|
|
|
78
|
-
|
|
79
|
+
- `load(opt)`: load specified font. cache used fonts to save bandwidth and memory.
|
|
80
|
+
- `opt` can either be:
|
|
81
|
+
- a string to the specified font, or object as option
|
|
82
|
+
- an object passed directly to `xlfont` constructor.
|
|
83
|
+
- return a promise resolving to a `xlfont` object.
|
|
84
|
+
- `update()`: refresh necessary CSS in document to reflect font status changes.
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
## xlfont Constructor
|
|
88
|
+
|
|
89
|
+
Besides loading from `xfl.load`, you can also manually construct a `xlfont` object. `xlfont` is the interface to accessing specific font ( including normal font or subsetted xlfont ), with constructor options as below:
|
|
90
|
+
|
|
91
|
+
- `path`: path to font.
|
|
92
|
+
- `ext`: optional. font file extension ( `ttf`, `woff`, etc )
|
|
93
|
+
- if omitted, calculated from `path`.
|
|
94
|
+
- if not available ( either in `ext` or `path` ), default to `woff` and consider this font as a xlfont.
|
|
95
|
+
- `name`: font name. if omitted, use normalized `path` as font name.
|
|
96
|
+
- `isXl`: true if this is a xlfont. if omitted, decided by `ext`.
|
|
97
|
+
- `doMerge`: should subsetted merged by frontend JS before using to enhacen UX. default false
|
|
98
|
+
- `useWorker`: use web worker for font merging. default false. When specified, can be an object with following:
|
|
99
|
+
- `opentype-url`: required `opentype.js` url.
|
|
100
|
+
- context of worker is in Object URL, so relative URL is not support.
|
|
101
|
+
- use `document.location` to prevent from hardcoding domain.
|
|
102
|
+
- `style`: font style ( e.g., `normal`, `italic` ). default `normal`.
|
|
103
|
+
- `weight`: font weight ( e.g., `400`, `500`, etc ). default `400`.
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
## xlfont Object API
|
|
79
107
|
|
|
80
108
|
- `init()`: init this font object. `xfl.load` will do this job for you.
|
|
81
109
|
- `sync(text)`: load fonts based on given text.
|
|
@@ -95,12 +123,11 @@ Font object API:
|
|
|
95
123
|
- `className`: a CSS class name which its `font-family` is assigned to this font.
|
|
96
124
|
|
|
97
125
|
|
|
98
|
-
## availabel
|
|
126
|
+
## availabel xlfonts
|
|
99
127
|
|
|
100
|
-
While this is a tool for composing and using
|
|
101
|
-
|
|
102
|
-
https://github.com/xlfont/cjk/
|
|
128
|
+
While this is a tool for composing and using xlfont, we also prepare a set of xlfonts so you can use them directly with `xfl.js`. All fonts are released under `SIL-Open Font License 1.1` or similar open licenses. Checkout repo list of the Github organization of `@xlfont` for more information:
|
|
103
129
|
|
|
130
|
+
https://github.com/xlfont/
|
|
104
131
|
|
|
105
132
|
|
|
106
133
|
## Todo
|
|
@@ -123,9 +150,10 @@ While this is a tool for composing and using xl-font, we also prepare a set of x
|
|
|
123
150
|
- parsed from content of `https://www.ling.upenn.edu/courses/Spring_2003/ling538/UnicodeRanges.html`
|
|
124
151
|
- content of the above link is from `http://www.unicode.org/charts/`
|
|
125
152
|
|
|
153
|
+
|
|
126
154
|
## Font Subsetting Consideration
|
|
127
155
|
|
|
128
|
-
According to *
|
|
156
|
+
According to [*SIL's OFL FAQ*](https://scripts.sil.org/cms/scripts/page.php?item_id=OFL-FAQ_web#b4b7db21) and [*Web Fonts and Reserved Font Names*](https://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL_web_fonts_and_RFNs#33301a9c), Subsetting a font is considered *modification* of that font. This is permitted by *Open Font License (OFL)*, but OFL requires that modified versions must not use RFNs (Reserved Font Names) without separate permission.
|
|
129
157
|
|
|
130
158
|
However, once FE (Functional Equivalence) is preserved, then it's very likely that the original author would feel that the optimized font is a good and reasonable equivalent, and that the main purposes of the RFN mechanism - avoids collisions, protects authors. minimizes support, encourages derivatives - continue to be met.
|
|
131
159
|
|
package/bin/build.js
CHANGED
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
opentype = require('@plotdb/opentype.js');
|
|
8
8
|
yargs = require('yargs');
|
|
9
9
|
path = require('path');
|
|
10
|
-
colors = require('colors');
|
|
10
|
+
colors = require('@plotdb/colors');
|
|
11
11
|
progress = require('progress');
|
|
12
12
|
ttf2woff2 = require('ttf2woff2');
|
|
13
13
|
ttf2woff = require('ttf2woff');
|
|
14
14
|
argv = yargs.usage("usage: npx xfl font-dir [-o output-dir] [-c major-subset-size] [-s subset-size]").option('output', {
|
|
15
15
|
alias: 'o',
|
|
16
|
-
description: "output directory. default
|
|
16
|
+
description: "output directory. default `output`",
|
|
17
17
|
type: 'string'
|
|
18
18
|
}).option('major-subset-size', {
|
|
19
19
|
alias: 'c',
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
commonRanges = [[0, 0xff], [0xff00, 0xffef]];
|
|
38
38
|
commonSize = argv.c || 1500;
|
|
39
39
|
setSize = argv.s || 100;
|
|
40
|
-
fontDir = argv._[0] || "
|
|
41
|
-
outDir = argv.o || "
|
|
40
|
+
fontDir = argv._[0] || "fonts";
|
|
41
|
+
outDir = argv.o || "output";
|
|
42
42
|
defaultFrequencyFile = path.join(libdir, '..', 'tool', 'data', 'word-frequency.csv');
|
|
43
43
|
if (!fs.existsSync(fontDir)) {
|
|
44
44
|
console.log(fontDir + " directory not found.");
|
package/bin/meta.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
var fs, path, colors, yargs, opentype, lib, allRanges, argv, file, all, multiline;
|
|
4
4
|
fs = require('fs');
|
|
5
5
|
path = require('path');
|
|
6
|
-
colors = require('colors');
|
|
6
|
+
colors = require('@plotdb/colors');
|
|
7
7
|
yargs = require('yargs');
|
|
8
8
|
opentype = require('@plotdb/opentype.js');
|
|
9
9
|
lib = path.dirname(fs.realpathSync(__filename));
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var err, xlfWorker, xlfMerger, xlfont, xfl;
|
|
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||(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,a,o=[],l=0,i=e.length;l<i;++l)for(t=0,r=(n=e[l]).glyphs.length;t<r;++t)s=t,o.push(n.glyphs.glyphs[s]);return n=e[0],a=new opentype.Font(((a={glyphs:o,familyName:n.names.fontFamily.en,styleName:n.names.fontSubfamily.en}).unitsPerEm=n.unitsPerEm,a.ascender=n.ascender,a.descender=n.descender,a)),postMessage({buf:a.toArrayBuffer(),key:p})})};'; var err, xlfWorker, xlfMerger, xlfont, xfl;
|
|
2
2
|
err = function(e){
|
|
3
3
|
e == null && (e = {});
|
|
4
4
|
return import$(new Error(), import$({
|
|
@@ -9,37 +9,47 @@ xlfWorker = {
|
|
|
9
9
|
key: 0,
|
|
10
10
|
worker: null,
|
|
11
11
|
queue: [],
|
|
12
|
-
init: function(){
|
|
12
|
+
init: function(opt){
|
|
13
13
|
var this$ = this;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return this.worker.onmessage = function(e){
|
|
19
|
-
var ref$, buf, key, item;
|
|
20
|
-
ref$ = e.data, buf = ref$.buf, key = ref$.key;
|
|
21
|
-
if (!(item = this$.queue.filter(function(q){
|
|
22
|
-
return key === q.key;
|
|
23
|
-
})[0])) {
|
|
14
|
+
opt == null && (opt = {});
|
|
15
|
+
return Promise.resolve().then(function(){
|
|
16
|
+
var url;
|
|
17
|
+
if (this$.worker) {
|
|
24
18
|
return;
|
|
25
19
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
+
});
|
|
29
37
|
},
|
|
30
|
-
run: function(abs){
|
|
38
|
+
run: function(abs, opt){
|
|
31
39
|
var this$ = this;
|
|
32
40
|
return new Promise(function(res, rej){
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
+
return this$.worker.postMessage({
|
|
49
|
+
bufs: abs,
|
|
50
|
+
key: item.key,
|
|
51
|
+
lib: opt.opentypeUrl
|
|
52
|
+
});
|
|
43
53
|
});
|
|
44
54
|
});
|
|
45
55
|
}
|
|
@@ -51,10 +61,7 @@ xlfMerger = function(arg$){
|
|
|
51
61
|
return Promise.resolve();
|
|
52
62
|
}
|
|
53
63
|
if (useWorker) {
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
if (useWorker) {
|
|
57
|
-
return xlfWorker.run(bufs);
|
|
64
|
+
return xlfWorker.run(bufs, useWorker);
|
|
58
65
|
}
|
|
59
66
|
return Promise.resolve().then(function(){
|
|
60
67
|
return bufs.map(function(it){
|
|
@@ -105,7 +112,9 @@ xlfont = function(opt){
|
|
|
105
112
|
this.format = "format('" + this.format + "')";
|
|
106
113
|
}
|
|
107
114
|
this.className = "xfl-" + (this.name || '').replace(/\s+/g, '_') + "-" + Math.random().toString(36).substring(2);
|
|
108
|
-
this.isXl =
|
|
115
|
+
this.isXl = opt.isXl != null
|
|
116
|
+
? opt.isXl
|
|
117
|
+
: !this.ext;
|
|
109
118
|
if (!this.ext) {
|
|
110
119
|
this.ext = 'woff';
|
|
111
120
|
}
|
|
@@ -577,3 +586,4 @@ function import$(obj, src){
|
|
|
577
586
|
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
|
|
578
587
|
return obj;
|
|
579
588
|
}
|
|
589
|
+
}());
|
package/dist/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var err,xlfWorker,xlfMerger,xlfont,xfl;err=function(e){e==null&&(e={});return import$(new Error,import$({name:"lderror"},e))};xlfWorker={key:0,worker:null,queue:[],init:function(){var this$=this;if(this.worker){return}this.worker=new Worker("/assets/lib/@xlfont/load/dev/worker.min.js");return this.worker.onmessage=function(e){var ref$,buf,key,item;ref$=e.data,buf=ref$.buf,key=ref$.key;if(!(item=this$.queue.filter(function(q){return key===q.key})[0])){return}this$.queue.splice(this$.queue.indexOf(item),1);return item.res(buf)}},run:function(abs){var this$=this;return new Promise(function(res,rej){var item;this$.init();this$.queue.push(item={res:res,rej:rej,key:this$.key++});return this$.worker.postMessage({bufs:abs,key:item.key})})}};xlfMerger=function(arg$){var bufs,useWorker;bufs=arg$.bufs,useWorker=arg$.useWorker;if(!bufs.length){return Promise.resolve()}if(useWorker){return console.error("[@xlfont/load] worker is not yet supported.")}if(useWorker){return xlfWorker.run(bufs)}return Promise.resolve().then(function(){return bufs.map(function(it){return opentype.parse(it)})}).then(function(fonts){var glyphs,i$,len$,font,j$,to$,i,merged,ref$;glyphs=[];for(i$=0,len$=fonts.length;i$<len$;++i$){font=fonts[i$];for(j$=0,to$=font.glyphs.length;j$<to$;++j$){i=j$;glyphs.push(font.glyphs.glyphs[i])}}font=fonts[0];merged=new opentype.Font((ref$={glyphs:glyphs,familyName:font.names.fontFamily.en,styleName:font.names.fontSubfamily.en},ref$.unitsPerEm=font.unitsPerEm,ref$.ascender=font.ascender,ref$.descender=font.descender,ref$));return merged.toArrayBuffer()})};xlfont=function(opt){var that,this$=this;opt==null&&(opt={});this.opt=opt;this.sub={set:{},font:{}};this.cjkOnly=opt.cjkOnly||false;this.codemap={};this.otf={font:null,dirty:true};this.path=opt.path;this.name=opt.name||this.path.replace(/\.[a-zA-Z0-9]+$/,"").replace(/^https?:\/\/[^/]+\//g,"").replace(/\s/g,"-").replace(/\//g,"-").toLowerCase();this.style=opt.style||"normal";this.weight=opt.weight||"400";this.ext=opt.ext||(/\.(ttf|otf|woff2|woff)$/.exec(this.path)||[])[1]||"";this.format=(that=this.ext.toLowerCase())?that==="ttf"?"truetype":that==="otf"?"truetype":that:"";if(this.format){this.format="format('"+this.format+"')"}this.className="xfl-"+(this.name||"").replace(/\s+/g,"_")+"-"+Math.random().toString(36).substring(2);this.isXl=!this.ext;if(!this.ext){this.ext="woff"}this.doMerge=opt.doMerge!=null?opt.doMerge:false;this.useWorker=opt.useWorker!=null?opt.useWorker:false;this.misschar={};this.css=[];this.init=proxise.once(function(){return this$._init()});this.init();return this};xlfont.prototype=import$(Object.create(Object.prototype),{_init:function(){var this$=this;return Promise.resolve().then(function(){if(!this$.isXl){this$.css=[{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+'"; }'}];return xfl.update()}else{return new Promise(function(res,rej){var xhr;xhr=new XMLHttpRequest;xhr.addEventListener("readystatechange",function(){if(xhr.readyState!==XMLHttpRequest.DONE){return}if(xhr.status!==200){return rej(err({code:xhr.status,message:xhr.responseText}))}(xhr.responseText||"").split("\n").map(function(d,i){return d.split(" ").map(function(e,j){return this$.codemap[e]=i+1})});return res()});xhr.open("GET",this$.path+"/charmap.txt");return xhr.send()})}})},_fetch:function(f,dofetch,type){var this$=this;f==null&&(f={});dofetch==null&&(dofetch=false);type==null&&(type="ttf");if(!dofetch){if(!f.url){if(this.isXl){f.url=this.path+"/"+f.key+"."+type;f.type=type}else{f.url=this.path;f.type=this.ext.toLowerCase()||type}}return Promise.resolve(f)}if(f.blob){return Promise.resolve(f)}if(!f.proxy){f.proxy=proxise(function(f){var p;if(f.running){return}f.running=true;p=new Promise(function(res,rej){var xhr;if(f.blob){return res(f)}xhr=new XMLHttpRequest;xhr.addEventListener("readystatechange",function(){if(xhr.readyState!==XMLHttpRequest.DONE){return}if(xhr.status!==200){return rej(err({code:xhr.status,message:xhr.responseText}))}this$.otf.dirty=true;f.url=URL.createObjectURL(xhr.response);f.blob=xhr.response;f.type=this$.ext.toLowerCase()||type;return res(f)});if(this$.isXl){xhr.open("GET",this$.path+"/"+f.key+"."+type)}else{xhr.open("GET",this$.path)}xhr.responseType="blob";return xhr.send()});return p["finally"](function(){return f.running=false})})}return f.proxy(f)},fetchAll:function(){var k,f,this$=this;if(this.isXl){return Promise.all(function(){var ref$,results$=[];for(k in ref$=this.sub.font){f=ref$[k];results$.push(f)}return results$}.call(this).map(function(it){return this$._fetch(it,true,"woff")}))}else{return this._fetch(this.sub.font[0],true,"ttf")}},fetch:function(list,dofetch){var doMerge,ps,this$=this;list==null&&(list=[]);dofetch==null&&(dofetch=false);if(!this.isXl){if(this.sub.font[0]&&this.sub.font[0].blob){return Promise.resolve()}list=[0]}doMerge=this.doMerge;ps=Array.from(new Set(list.map(function(it){return it}))).filter(function(it){return!this$.sub.font[it]}).map(function(d,i){var f,type;this$.sub.font[d]=f={key:d};type=!this$.isXl?"ttf":"woff";f.needMerge=+d!==1;return this$._fetch(f,!doMerge?dofetch:true,type)});return Promise.all(ps).then(function(subfonts){var ps;if(!doMerge){return subfonts}ps=subfonts.filter(function(it){return it.needMerge}).map(function(font){return new Promise(function(res,rej){var fr;fr=new FileReader;fr.onerror=function(it){return rej(it)};fr.onload=function(){return res(fr.result)};return fr.readAsArrayBuffer(font.blob)})});return Promise.all(ps).then(function(it){return xlfMerger({bufs:it,useWorker:this$.useWorker})}).then(function(ab){var font,blob;if(!ab){return subfonts.filter(function(it){return!it.needMerge})}font=subfonts.filter(function(it){return it.needMerge})[0];blob=new Blob([ab],{type:"font/"+(font.type||"ttf")});font={url:URL.createObjectURL(blob),blob:blob,type:font.type||"ttf"};return subfonts.filter(function(it){return!it.needMerge}).concat([font])})}).then(function(subfonts){var css,k,ref$,f,i$,len$;if(!subfonts.length){return}css="."+this$.className+' { font-family: "'+this$.name+'"; }';for(k in ref$=this$.sub.font){f=ref$[k]}for(i$=0,len$=subfonts.length;i$<len$;++i$){f=subfonts[i$];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}"}return this$.css.push({content:css})})},hasChar:function(c){return typeof this.misschar[c]==="undefined"?undefined:!this.misshcar[c]},getPath:function(opt){var ref$,text,x,y,fontSize,this$=this;opt==null&&(opt={});ref$=import$({text:"",x:0,y:0,fontSize:48},opt),text=ref$.text,x=ref$.x,y=ref$.y,fontSize=ref$.fontSize;return this.sync(text).then(function(){return this$.getotf()}).then(function(otf){var txt,i$,to$,i,path;txt="";for(i$=0,to$=text.length;i$<to$;++i$){i=i$;txt+=!this$.misschar[text[i]]?text[i]:" "}path=otf.getPath(txt,x,y,fontSize);return path})},getotf:function(){var this$=this;if(!(typeof opentype!="undefined"&&opentype!==null)){return Promise.reject(err({id:1022,message:"[@plotdb/xfl] need opentype.js to merge subfonts"}))}if(!this.otf.dirty){return Promise.resolve(this.otf.font)}return Promise.resolve().then(function(){if(!this$.isXl){return this$.fetch()}else{return this$.fetchAll()}}).then(function(){var ps,k,f;ps=function(){var ref$,results$=[];for(k in ref$=this.sub.font){f=ref$[k];results$.push(f)}return results$}.call(this$).map(function(f){if(f.otf){return Promise.resolve(f)}else{return opentype.load(f.url).then(function(it){f.otf=it;return f})}});return Promise.all(ps)}).then(function(list){var glyphs,ref$,ref1$;list==null&&(list=[]);if(list.length===1){return list[0].otf}glyphs=list.map(function(f){var glyphs,i$,to$,i,results$=[];glyphs=f.otf.glyphs;for(i$=1,to$=glyphs.length;i$<=to$;++i$){i=i$;results$.push(glyphs.glyphs[i])}return results$}).reduce(function(a,b){return a.concat(b)},[]).filter(function(it){return it});this$.otf.font=new opentype.Font((ref1$={familyName:this$.name,styleName:this$.style||"normal",glyphs:glyphs},ref1$.unitsPerEm=(ref$=list[0].otf).unitsPerEm,ref1$.ascender=ref$.ascender,ref1$.descender=ref$.descender,ref1$));this$.otf.font.kerningPairs={};return this$.otf.font})},sync:function(txt){var this$=this;txt==null&&(txt="");if(!this.isXl){xfl.update();return Promise.resolve()}return Promise.resolve().then(function(){var ref$,misscodes,missset,i$,to$,i,code,setIdx,k,list,res$;ref$=[{},{}],misscodes=ref$[0],missset=ref$[1];for(i$=0,to$=txt.length;i$<to$;++i$){i=i$;code=txt.charCodeAt(i);if(this$.cjkOnly&&!xfl.isCJK(code)){continue}setIdx=this$.codemap[code.toString(16)];this$.misschar[txt[i]]=misscodes[txt[i]]=!setIdx?true:false;if(setIdx&&!this$.sub.set[setIdx]){this$.sub.set[setIdx]=missset[setIdx]=true}}misscodes=function(){var results$=[];for(k in misscodes){results$.push(k)}return results$}().filter(function(it){return misscodes[it]});if(misscodes.length){console.log("[@plotdb/xfl] sync xl-font with following chars unsupported: "+misscodes.join(""))}res$=[];for(k in missset){res$.push(k)}list=res$;if(list.length){return this$.fetch(list)}}).then(function(){return xfl.update()})}});xfl={range:{CJK:[[12352,12543],[13312,19903],[19968,40959],[63744,64255],[65382,65439],[12593,55197]]},fonts:{},running:{},isCJK:function(code){return this.range.CJK.filter(function(it){return code>=it[0]&&code<=it[1]}).length},proxy:{},update:function(){var css,k,ref$,v,node,f;css="";for(k in ref$=this.fonts){v=ref$[k];(v.css||[]).filter(fn$).map(fn1$)}if(css){node=document.createElement("style");node.textContent=css;node.setAttribute("type","text/css");document.body.appendChild(node)}return Promise.all(function(){var ref$,results$=[];for(k in ref$=this.fonts){f=ref$[k];results$.push(f)}return results$}.call(this).map(function(){return document.fonts.load("16px "+v.name)})).then(function(){return new Promise(function(res,rej){return setTimeout(function(){return res()},350)})});function fn$(it){return!it.rendered}function fn1$(it){it.rendered=true;return css=css+it.content}},_load:function(opt){var path,this$=this;opt==null&&(opt={});path=opt.path;if(this.running[path]){return}this.running[path]=true;return Promise.resolve().then(function(){var fobj;this$.fonts[path]=fobj=new xlfont(opt);return fobj.init()})["finally"](function(){return this$.running[path]=false}).then(function(){this$.proxy[path].resolve(this$.fonts[path]);return this$.fonts[path]})["catch"](function(it){this$.proxy[path].reject(it);return Promise.reject(it)})},load:function(opt){var this$=this;opt==null&&(opt={});if(typeof opt==="string"){opt={path:opt}}return new Promise(function(res,rej){var path,that,ref$;if(!(path=opt.path)){return rej(err({id:400}))}path=path.replace(/\/$/,"");if(that=this$.fonts[path]){return res(that)}if(!this$.proxy[path]){this$.proxy[path]=proxise(function(it){return this$._load(it)})}return this$.proxy[path]((ref$=import$({},opt),ref$.path=path,ref$)).then(function(it){return res(it)})["catch"](function(it){return rej(it)})})}};if(typeof module!="undefined"&&module!==null){module.exports=xfl}else if(typeof window!="undefined"&&window!==null){window.xfl=xfl}function import$(obj,src){var own={}.hasOwnProperty;for(var key in src)if(own.call(src,key))obj[key]=src[key];return obj}
|
|
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||(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,a,o=[],l=0,i=e.length;l<i;++l)for(t=0,r=(n=e[l]).glyphs.length;t<r;++t)s=t,o.push(n.glyphs.glyphs[s]);return n=e[0],a=new opentype.Font(((a={glyphs:o,familyName:n.names.fontFamily.en,styleName:n.names.fontSubfamily.en}).unitsPerEm=n.unitsPerEm,a.ascender=n.ascender,a.descender=n.descender,a)),postMessage({buf:a.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++}),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/dist/worker.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
var onmessage;
|
|
2
|
-
|
|
1
|
+
var local, onmessage;
|
|
2
|
+
local = {};
|
|
3
3
|
onmessage = function(e){
|
|
4
|
-
var ref$, bufs, key;
|
|
5
|
-
ref$ = e.data || {}, bufs = ref$.bufs, key = ref$.key;
|
|
4
|
+
var ref$, bufs, key, lib;
|
|
5
|
+
ref$ = e.data || {}, bufs = ref$.bufs, key = ref$.key, lib = ref$.lib;
|
|
6
|
+
if (!local.inited) {
|
|
7
|
+
importScripts(lib || "opentype.js");
|
|
8
|
+
local.inited = true;
|
|
9
|
+
}
|
|
6
10
|
return Promise.resolve().then(function(){
|
|
7
11
|
return bufs.map(function(it){
|
|
8
12
|
return opentype.parse(it);
|
package/dist/worker.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var onmessage
|
|
1
|
+
var local={},onmessage=function(e){var e=e.data||{},n=e.bufs,p=e.key,e=e.lib;return local.inited||(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,a,o=[],l=0,i=e.length;l<i;++l)for(t=0,r=(n=e[l]).glyphs.length;t<r;++t)s=t,o.push(n.glyphs.glyphs[s]);return n=e[0],a=new opentype.Font(((a={glyphs:o,familyName:n.names.fontFamily.en,styleName:n.names.fontSubfamily.en}).unitsPerEm=n.unitsPerEm,a.ascender=n.ascender,a.descender=n.descender,a)),postMessage({buf:a.toArrayBuffer(),key:p})})};
|
package/{dist/xfl.js → index.js}
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var err, xlfWorker, xlfMerger, xlfont, xfl;
|
|
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||(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,a,o=[],l=0,i=e.length;l<i;++l)for(t=0,r=(n=e[l]).glyphs.length;t<r;++t)s=t,o.push(n.glyphs.glyphs[s]);return n=e[0],a=new opentype.Font(((a={glyphs:o,familyName:n.names.fontFamily.en,styleName:n.names.fontSubfamily.en}).unitsPerEm=n.unitsPerEm,a.ascender=n.ascender,a.descender=n.descender,a)),postMessage({buf:a.toArrayBuffer(),key:p})})};'; var err, xlfWorker, xlfMerger, xlfont, xfl;
|
|
2
2
|
err = function(e){
|
|
3
3
|
e == null && (e = {});
|
|
4
4
|
return import$(new Error(), import$({
|
|
@@ -9,37 +9,47 @@ xlfWorker = {
|
|
|
9
9
|
key: 0,
|
|
10
10
|
worker: null,
|
|
11
11
|
queue: [],
|
|
12
|
-
init: function(){
|
|
12
|
+
init: function(opt){
|
|
13
13
|
var this$ = this;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return this.worker.onmessage = function(e){
|
|
19
|
-
var ref$, buf, key, item;
|
|
20
|
-
ref$ = e.data, buf = ref$.buf, key = ref$.key;
|
|
21
|
-
if (!(item = this$.queue.filter(function(q){
|
|
22
|
-
return key === q.key;
|
|
23
|
-
})[0])) {
|
|
14
|
+
opt == null && (opt = {});
|
|
15
|
+
return Promise.resolve().then(function(){
|
|
16
|
+
var url;
|
|
17
|
+
if (this$.worker) {
|
|
24
18
|
return;
|
|
25
19
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
+
});
|
|
29
37
|
},
|
|
30
|
-
run: function(abs){
|
|
38
|
+
run: function(abs, opt){
|
|
31
39
|
var this$ = this;
|
|
32
40
|
return new Promise(function(res, rej){
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
+
return this$.worker.postMessage({
|
|
49
|
+
bufs: abs,
|
|
50
|
+
key: item.key,
|
|
51
|
+
lib: opt.opentypeUrl
|
|
52
|
+
});
|
|
43
53
|
});
|
|
44
54
|
});
|
|
45
55
|
}
|
|
@@ -51,10 +61,7 @@ xlfMerger = function(arg$){
|
|
|
51
61
|
return Promise.resolve();
|
|
52
62
|
}
|
|
53
63
|
if (useWorker) {
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
if (useWorker) {
|
|
57
|
-
return xlfWorker.run(bufs);
|
|
64
|
+
return xlfWorker.run(bufs, useWorker);
|
|
58
65
|
}
|
|
59
66
|
return Promise.resolve().then(function(){
|
|
60
67
|
return bufs.map(function(it){
|
|
@@ -105,7 +112,9 @@ xlfont = function(opt){
|
|
|
105
112
|
this.format = "format('" + this.format + "')";
|
|
106
113
|
}
|
|
107
114
|
this.className = "xfl-" + (this.name || '').replace(/\s+/g, '_') + "-" + Math.random().toString(36).substring(2);
|
|
108
|
-
this.isXl =
|
|
115
|
+
this.isXl = opt.isXl != null
|
|
116
|
+
? opt.isXl
|
|
117
|
+
: !this.ext;
|
|
109
118
|
if (!this.ext) {
|
|
110
119
|
this.ext = 'woff';
|
|
111
120
|
}
|
|
@@ -577,3 +586,4 @@ function import$(obj, src){
|
|
|
577
586
|
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
|
|
578
587
|
return obj;
|
|
579
588
|
}
|
|
589
|
+
}());
|
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||(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,a,o=[],l=0,i=e.length;l<i;++l)for(t=0,r=(n=e[l]).glyphs.length;t<r;++t)s=t,o.push(n.glyphs.glyphs[s]);return n=e[0],a=new opentype.Font(((a={glyphs:o,familyName:n.names.fontFamily.en,styleName:n.names.fontSubfamily.en}).unitsPerEm=n.unitsPerEm,a.ascender=n.ascender,a.descender=n.descender,a)),postMessage({buf:a.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++}),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,76 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"author": "zbryikt",
|
|
3
|
-
"name": "@xlfont/load",
|
|
4
|
-
"license": "MIT",
|
|
5
|
-
"description": "XL font generator and loader",
|
|
6
|
-
"version": "1.1.0",
|
|
7
|
-
"main": "dist/index.js",
|
|
8
|
-
"homepage": "https://github.com/xlfont/load",
|
|
9
|
-
"files": [
|
|
10
|
-
"dist/**/*",
|
|
11
|
-
"tool/data/*"
|
|
12
|
-
],
|
|
13
|
-
"repository": {
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "https://github.com/xlfont/load"
|
|
16
|
-
},
|
|
17
|
-
"engines": {
|
|
18
|
-
"node": ">=10.14.1",
|
|
19
|
-
"npm": ">=6.4.1"
|
|
20
|
-
},
|
|
21
|
-
"scripts": {
|
|
22
|
-
"start": "npx server -r web -o true"
|
|
23
|
-
},
|
|
24
|
-
"bin": {
|
|
25
|
-
"xfl": "./bin/build.js",
|
|
26
|
-
"xfl-meta": "./bin/meta.js"
|
|
27
|
-
},
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"@plotdb/opentype.js": "^1.3.4",
|
|
30
|
-
"colors": "^1.4.0",
|
|
31
|
-
"fs-extra": "^9.1.0",
|
|
32
|
-
"progress": "^2.0.3",
|
|
33
|
-
"ttf2woff": "^2.0.2",
|
|
34
|
-
"ttf2woff2": "^4.0.0",
|
|
35
|
-
"yargs": "^16.2.0"
|
|
36
|
-
},
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"@loadingio/bootstrap.ext": "^0.0.1",
|
|
39
|
-
"@loadingio/debounce.js": "^0.0.1",
|
|
40
|
-
"@loadingio/ldquery": "^2.2.1",
|
|
41
|
-
"@loadingio/loading.css": "^2.0.1",
|
|
42
|
-
"@zbryikt/template": "^2.3.22",
|
|
43
|
-
"bootstrap": "^4.5.3",
|
|
44
|
-
"bootstrap.native": "^3.0.13",
|
|
45
|
-
"fedep": "^0.0.17",
|
|
46
|
-
"ldbutton": "^1.0.2",
|
|
47
|
-
"ldcover": "^2.0.0",
|
|
48
|
-
"ldloader": "^2.1.0",
|
|
49
|
-
"ldview": "^0.2.7",
|
|
50
|
-
"livescript": "^1.6.0",
|
|
51
|
-
"stylus": "^0.55.0",
|
|
52
|
-
"uglify-js": "^3.2.0",
|
|
53
|
-
"uglifycss": "^0.0.29"
|
|
54
|
-
},
|
|
55
|
-
"peerDependencies": {
|
|
56
|
-
"@plotdb/opentype.js": "^1.3.4",
|
|
57
|
-
"proxise": "^0.1.3"
|
|
58
|
-
},
|
|
59
|
-
"frontendDependencies": {
|
|
60
|
-
"root": "web/static/assets/lib",
|
|
61
|
-
"modules": [
|
|
62
|
-
"bootstrap",
|
|
63
|
-
"bootstrap.native",
|
|
64
|
-
"@loadingio/bootstrap.ext",
|
|
65
|
-
"@loadingio/ldquery",
|
|
66
|
-
"@loadingio/debounce.js",
|
|
67
|
-
"@loadingio/loading.css",
|
|
68
|
-
"@plotdb/opentype.js",
|
|
69
|
-
"ldview",
|
|
70
|
-
"ldcover",
|
|
71
|
-
"ldloader",
|
|
72
|
-
"ldbutton",
|
|
73
|
-
"proxise"
|
|
74
|
-
]
|
|
75
|
-
}
|
|
76
|
-
}
|
|
1
|
+
{"author":"zbryikt","name":"@xlfont/load","license":"MIT","description":"XL font generator and loader","version":"2.0.2","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","publish":"npx fedep publish -d 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,35 @@
|
|
|
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
|
+
importScripts(lib || "opentype.js");
|
|
8
|
+
local.inited = true;
|
|
9
|
+
}
|
|
10
|
+
return Promise.resolve().then(function(){
|
|
11
|
+
return bufs.map(function(it){
|
|
12
|
+
return opentype.parse(it);
|
|
13
|
+
});
|
|
14
|
+
}).then(function(fonts){
|
|
15
|
+
var glyphs, i$, len$, font, j$, to$, i, merged, ref$;
|
|
16
|
+
glyphs = [];
|
|
17
|
+
for (i$ = 0, len$ = fonts.length; i$ < len$; ++i$) {
|
|
18
|
+
font = fonts[i$];
|
|
19
|
+
for (j$ = 0, to$ = font.glyphs.length; j$ < to$; ++j$) {
|
|
20
|
+
i = j$;
|
|
21
|
+
glyphs.push(font.glyphs.glyphs[i]);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
font = fonts[0];
|
|
25
|
+
merged = new opentype.Font((ref$ = {
|
|
26
|
+
glyphs: glyphs,
|
|
27
|
+
familyName: font.names.fontFamily.en,
|
|
28
|
+
styleName: font.names.fontSubfamily.en
|
|
29
|
+
}, ref$.unitsPerEm = font.unitsPerEm, ref$.ascender = font.ascender, ref$.descender = font.descender, ref$));
|
|
30
|
+
return postMessage({
|
|
31
|
+
buf: merged.toArrayBuffer(),
|
|
32
|
+
key: key
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
};
|
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||(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,a,o=[],l=0,i=e.length;l<i;++l)for(t=0,r=(n=e[l]).glyphs.length;t<r;++t)s=t,o.push(n.glyphs.glyphs[s]);return n=e[0],a=new opentype.Font(((a={glyphs:o,familyName:n.names.fontFamily.en,styleName:n.names.fontSubfamily.en}).unitsPerEm=n.unitsPerEm,a.ascender=n.ascender,a.descender=n.descender,a)),postMessage({buf:a.toArrayBuffer(),key:p})})};
|
package/dist/xfl.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var err,xlfWorker,xlfMerger,xlfont,xfl;err=function(e){e==null&&(e={});return import$(new Error,import$({name:"lderror"},e))};xlfWorker={key:0,worker:null,queue:[],init:function(){var this$=this;if(this.worker){return}this.worker=new Worker("/assets/lib/@xlfont/load/dev/worker.min.js");return this.worker.onmessage=function(e){var ref$,buf,key,item;ref$=e.data,buf=ref$.buf,key=ref$.key;if(!(item=this$.queue.filter(function(q){return key===q.key})[0])){return}this$.queue.splice(this$.queue.indexOf(item),1);return item.res(buf)}},run:function(abs){var this$=this;return new Promise(function(res,rej){var item;this$.init();this$.queue.push(item={res:res,rej:rej,key:this$.key++});return this$.worker.postMessage({bufs:abs,key:item.key})})}};xlfMerger=function(arg$){var bufs,useWorker;bufs=arg$.bufs,useWorker=arg$.useWorker;if(!bufs.length){return Promise.resolve()}if(useWorker){return console.error("[@xlfont/load] worker is not yet supported.")}if(useWorker){return xlfWorker.run(bufs)}return Promise.resolve().then(function(){return bufs.map(function(it){return opentype.parse(it)})}).then(function(fonts){var glyphs,i$,len$,font,j$,to$,i,merged,ref$;glyphs=[];for(i$=0,len$=fonts.length;i$<len$;++i$){font=fonts[i$];for(j$=0,to$=font.glyphs.length;j$<to$;++j$){i=j$;glyphs.push(font.glyphs.glyphs[i])}}font=fonts[0];merged=new opentype.Font((ref$={glyphs:glyphs,familyName:font.names.fontFamily.en,styleName:font.names.fontSubfamily.en},ref$.unitsPerEm=font.unitsPerEm,ref$.ascender=font.ascender,ref$.descender=font.descender,ref$));return merged.toArrayBuffer()})};xlfont=function(opt){var that,this$=this;opt==null&&(opt={});this.opt=opt;this.sub={set:{},font:{}};this.cjkOnly=opt.cjkOnly||false;this.codemap={};this.otf={font:null,dirty:true};this.path=opt.path;this.name=opt.name||this.path.replace(/\.[a-zA-Z0-9]+$/,"").replace(/^https?:\/\/[^/]+\//g,"").replace(/\s/g,"-").replace(/\//g,"-").toLowerCase();this.style=opt.style||"normal";this.weight=opt.weight||"400";this.ext=opt.ext||(/\.(ttf|otf|woff2|woff)$/.exec(this.path)||[])[1]||"";this.format=(that=this.ext.toLowerCase())?that==="ttf"?"truetype":that==="otf"?"truetype":that:"";if(this.format){this.format="format('"+this.format+"')"}this.className="xfl-"+(this.name||"").replace(/\s+/g,"_")+"-"+Math.random().toString(36).substring(2);this.isXl=!this.ext;if(!this.ext){this.ext="woff"}this.doMerge=opt.doMerge!=null?opt.doMerge:false;this.useWorker=opt.useWorker!=null?opt.useWorker:false;this.misschar={};this.css=[];this.init=proxise.once(function(){return this$._init()});this.init();return this};xlfont.prototype=import$(Object.create(Object.prototype),{_init:function(){var this$=this;return Promise.resolve().then(function(){if(!this$.isXl){this$.css=[{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+'"; }'}];return xfl.update()}else{return new Promise(function(res,rej){var xhr;xhr=new XMLHttpRequest;xhr.addEventListener("readystatechange",function(){if(xhr.readyState!==XMLHttpRequest.DONE){return}if(xhr.status!==200){return rej(err({code:xhr.status,message:xhr.responseText}))}(xhr.responseText||"").split("\n").map(function(d,i){return d.split(" ").map(function(e,j){return this$.codemap[e]=i+1})});return res()});xhr.open("GET",this$.path+"/charmap.txt");return xhr.send()})}})},_fetch:function(f,dofetch,type){var this$=this;f==null&&(f={});dofetch==null&&(dofetch=false);type==null&&(type="ttf");if(!dofetch){if(!f.url){if(this.isXl){f.url=this.path+"/"+f.key+"."+type;f.type=type}else{f.url=this.path;f.type=this.ext.toLowerCase()||type}}return Promise.resolve(f)}if(f.blob){return Promise.resolve(f)}if(!f.proxy){f.proxy=proxise(function(f){var p;if(f.running){return}f.running=true;p=new Promise(function(res,rej){var xhr;if(f.blob){return res(f)}xhr=new XMLHttpRequest;xhr.addEventListener("readystatechange",function(){if(xhr.readyState!==XMLHttpRequest.DONE){return}if(xhr.status!==200){return rej(err({code:xhr.status,message:xhr.responseText}))}this$.otf.dirty=true;f.url=URL.createObjectURL(xhr.response);f.blob=xhr.response;f.type=this$.ext.toLowerCase()||type;return res(f)});if(this$.isXl){xhr.open("GET",this$.path+"/"+f.key+"."+type)}else{xhr.open("GET",this$.path)}xhr.responseType="blob";return xhr.send()});return p["finally"](function(){return f.running=false})})}return f.proxy(f)},fetchAll:function(){var k,f,this$=this;if(this.isXl){return Promise.all(function(){var ref$,results$=[];for(k in ref$=this.sub.font){f=ref$[k];results$.push(f)}return results$}.call(this).map(function(it){return this$._fetch(it,true,"woff")}))}else{return this._fetch(this.sub.font[0],true,"ttf")}},fetch:function(list,dofetch){var doMerge,ps,this$=this;list==null&&(list=[]);dofetch==null&&(dofetch=false);if(!this.isXl){if(this.sub.font[0]&&this.sub.font[0].blob){return Promise.resolve()}list=[0]}doMerge=this.doMerge;ps=Array.from(new Set(list.map(function(it){return it}))).filter(function(it){return!this$.sub.font[it]}).map(function(d,i){var f,type;this$.sub.font[d]=f={key:d};type=!this$.isXl?"ttf":"woff";f.needMerge=+d!==1;return this$._fetch(f,!doMerge?dofetch:true,type)});return Promise.all(ps).then(function(subfonts){var ps;if(!doMerge){return subfonts}ps=subfonts.filter(function(it){return it.needMerge}).map(function(font){return new Promise(function(res,rej){var fr;fr=new FileReader;fr.onerror=function(it){return rej(it)};fr.onload=function(){return res(fr.result)};return fr.readAsArrayBuffer(font.blob)})});return Promise.all(ps).then(function(it){return xlfMerger({bufs:it,useWorker:this$.useWorker})}).then(function(ab){var font,blob;if(!ab){return subfonts.filter(function(it){return!it.needMerge})}font=subfonts.filter(function(it){return it.needMerge})[0];blob=new Blob([ab],{type:"font/"+(font.type||"ttf")});font={url:URL.createObjectURL(blob),blob:blob,type:font.type||"ttf"};return subfonts.filter(function(it){return!it.needMerge}).concat([font])})}).then(function(subfonts){var css,k,ref$,f,i$,len$;if(!subfonts.length){return}css="."+this$.className+' { font-family: "'+this$.name+'"; }';for(k in ref$=this$.sub.font){f=ref$[k]}for(i$=0,len$=subfonts.length;i$<len$;++i$){f=subfonts[i$];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}"}return this$.css.push({content:css})})},hasChar:function(c){return typeof this.misschar[c]==="undefined"?undefined:!this.misshcar[c]},getPath:function(opt){var ref$,text,x,y,fontSize,this$=this;opt==null&&(opt={});ref$=import$({text:"",x:0,y:0,fontSize:48},opt),text=ref$.text,x=ref$.x,y=ref$.y,fontSize=ref$.fontSize;return this.sync(text).then(function(){return this$.getotf()}).then(function(otf){var txt,i$,to$,i,path;txt="";for(i$=0,to$=text.length;i$<to$;++i$){i=i$;txt+=!this$.misschar[text[i]]?text[i]:" "}path=otf.getPath(txt,x,y,fontSize);return path})},getotf:function(){var this$=this;if(!(typeof opentype!="undefined"&&opentype!==null)){return Promise.reject(err({id:1022,message:"[@plotdb/xfl] need opentype.js to merge subfonts"}))}if(!this.otf.dirty){return Promise.resolve(this.otf.font)}return Promise.resolve().then(function(){if(!this$.isXl){return this$.fetch()}else{return this$.fetchAll()}}).then(function(){var ps,k,f;ps=function(){var ref$,results$=[];for(k in ref$=this.sub.font){f=ref$[k];results$.push(f)}return results$}.call(this$).map(function(f){if(f.otf){return Promise.resolve(f)}else{return opentype.load(f.url).then(function(it){f.otf=it;return f})}});return Promise.all(ps)}).then(function(list){var glyphs,ref$,ref1$;list==null&&(list=[]);if(list.length===1){return list[0].otf}glyphs=list.map(function(f){var glyphs,i$,to$,i,results$=[];glyphs=f.otf.glyphs;for(i$=1,to$=glyphs.length;i$<=to$;++i$){i=i$;results$.push(glyphs.glyphs[i])}return results$}).reduce(function(a,b){return a.concat(b)},[]).filter(function(it){return it});this$.otf.font=new opentype.Font((ref1$={familyName:this$.name,styleName:this$.style||"normal",glyphs:glyphs},ref1$.unitsPerEm=(ref$=list[0].otf).unitsPerEm,ref1$.ascender=ref$.ascender,ref1$.descender=ref$.descender,ref1$));this$.otf.font.kerningPairs={};return this$.otf.font})},sync:function(txt){var this$=this;txt==null&&(txt="");if(!this.isXl){xfl.update();return Promise.resolve()}return Promise.resolve().then(function(){var ref$,misscodes,missset,i$,to$,i,code,setIdx,k,list,res$;ref$=[{},{}],misscodes=ref$[0],missset=ref$[1];for(i$=0,to$=txt.length;i$<to$;++i$){i=i$;code=txt.charCodeAt(i);if(this$.cjkOnly&&!xfl.isCJK(code)){continue}setIdx=this$.codemap[code.toString(16)];this$.misschar[txt[i]]=misscodes[txt[i]]=!setIdx?true:false;if(setIdx&&!this$.sub.set[setIdx]){this$.sub.set[setIdx]=missset[setIdx]=true}}misscodes=function(){var results$=[];for(k in misscodes){results$.push(k)}return results$}().filter(function(it){return misscodes[it]});if(misscodes.length){console.log("[@plotdb/xfl] sync xl-font with following chars unsupported: "+misscodes.join(""))}res$=[];for(k in missset){res$.push(k)}list=res$;if(list.length){return this$.fetch(list)}}).then(function(){return xfl.update()})}});xfl={range:{CJK:[[12352,12543],[13312,19903],[19968,40959],[63744,64255],[65382,65439],[12593,55197]]},fonts:{},running:{},isCJK:function(code){return this.range.CJK.filter(function(it){return code>=it[0]&&code<=it[1]}).length},proxy:{},update:function(){var css,k,ref$,v,node,f;css="";for(k in ref$=this.fonts){v=ref$[k];(v.css||[]).filter(fn$).map(fn1$)}if(css){node=document.createElement("style");node.textContent=css;node.setAttribute("type","text/css");document.body.appendChild(node)}return Promise.all(function(){var ref$,results$=[];for(k in ref$=this.fonts){f=ref$[k];results$.push(f)}return results$}.call(this).map(function(){return document.fonts.load("16px "+v.name)})).then(function(){return new Promise(function(res,rej){return setTimeout(function(){return res()},350)})});function fn$(it){return!it.rendered}function fn1$(it){it.rendered=true;return css=css+it.content}},_load:function(opt){var path,this$=this;opt==null&&(opt={});path=opt.path;if(this.running[path]){return}this.running[path]=true;return Promise.resolve().then(function(){var fobj;this$.fonts[path]=fobj=new xlfont(opt);return fobj.init()})["finally"](function(){return this$.running[path]=false}).then(function(){this$.proxy[path].resolve(this$.fonts[path]);return this$.fonts[path]})["catch"](function(it){this$.proxy[path].reject(it);return Promise.reject(it)})},load:function(opt){var this$=this;opt==null&&(opt={});if(typeof opt==="string"){opt={path:opt}}return new Promise(function(res,rej){var path,that,ref$;if(!(path=opt.path)){return rej(err({id:400}))}path=path.replace(/\/$/,"");if(that=this$.fonts[path]){return res(that)}if(!this$.proxy[path]){this$.proxy[path]=proxise(function(it){return this$._load(it)})}return this$.proxy[path]((ref$=import$({},opt),ref$.path=path,ref$)).then(function(it){return res(it)})["catch"](function(it){return rej(it)})})}};if(typeof module!="undefined"&&module!==null){module.exports=xfl}else if(typeof window!="undefined"&&window!==null){window.xfl=xfl}function import$(obj,src){var own={}.hasOwnProperty;for(var key in src)if(own.call(src,key))obj[key]=src[key];return obj}
|