@xlfont/load 1.1.1 → 2.0.0

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