@xlfont/load 2.0.2 → 2.1.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/CHANGELOG.md CHANGED
@@ -1,8 +1,15 @@
1
1
  # Change Logs
2
2
 
3
+ ## v2.1.0
4
+
5
+ - fix bug: format of css generation in fetch is not correct
6
+ - export xlfont class
7
+
8
+
3
9
  ## v2.0.2
4
10
 
5
11
  - fix bug: default value for input dir and `output` directory should be `font` and `output`.
12
+ - remove `dist` folder since it's flatten in root dir
6
13
 
7
14
 
8
15
  ## v2.0.1
package/README.md CHANGED
@@ -73,6 +73,14 @@ Sync font based on given text:
73
73
  font.sync("含有這些字的字型子集會被載入。Subset containgin glyphs from these chars will be loaded.");
74
74
  });
75
75
 
76
+ Use `font.className` for font related CSS rules:
77
+
78
+ xfl
79
+ .load( ... )
80
+ .then(function(font) {
81
+ font.sync( ... ).then(function() { document.body.classList.add(font.className); });
82
+ })
83
+
76
84
 
77
85
  ## xfl API
78
86
 
@@ -107,6 +115,7 @@ Besides loading from `xfl.load`, you can also manually construct a `xlfont` obje
107
115
 
108
116
  - `init()`: init this font object. `xfl.load` will do this job for you.
109
117
  - `sync(text)`: load fonts based on given text.
118
+ - return a Promise. this will also call `update`
110
119
  - `getotf()`: return a promise, resolved to `opentype.Font` from `opentype.js`.
111
120
  - `opentype.js` is required for this method
112
121
  - `fetchAll()` will be called by this method.
package/dist/index.js CHANGED
@@ -87,7 +87,7 @@ xlfMerger = function(arg$){
87
87
  });
88
88
  };
89
89
  xlfont = function(opt){
90
- var that, this$ = this;
90
+ var this$ = this;
91
91
  opt == null && (opt = {});
92
92
  this.opt = opt;
93
93
  this.sub = {
@@ -105,12 +105,7 @@ xlfont = function(opt){
105
105
  this.style = opt.style || 'normal';
106
106
  this.weight = opt.weight || '400';
107
107
  this.ext = opt.ext || (/\.(ttf|otf|woff2|woff)$/.exec(this.path) || [])[1] || '';
108
- this.format = (that = this.ext.toLowerCase()) ? that === 'ttf'
109
- ? 'truetype'
110
- : that === 'otf' ? 'truetype' : that : '';
111
- if (this.format) {
112
- this.format = "format('" + this.format + "')";
113
- }
108
+ this.format = this._format(this.ext);
114
109
  this.className = "xfl-" + (this.name || '').replace(/\s+/g, '_') + "-" + Math.random().toString(36).substring(2);
115
110
  this.isXl = opt.isXl != null
116
111
  ? opt.isXl
@@ -129,6 +124,14 @@ xlfont = function(opt){
129
124
  return this;
130
125
  };
131
126
  xlfont.prototype = import$(Object.create(Object.prototype), {
127
+ _format: function(f){
128
+ f == null && (f = '');
129
+ f = f.toLowerCase();
130
+ f = f === 'ttf'
131
+ ? 'truetype'
132
+ : f === 'otf' ? 'truetype' : f;
133
+ return "format('" + f + "')";
134
+ },
132
135
  _init: function(){
133
136
  var this$ = this;
134
137
  return Promise.resolve().then(function(){
@@ -317,18 +320,17 @@ xlfont.prototype = import$(Object.create(Object.prototype), {
317
320
  }).concat([font]);
318
321
  });
319
322
  }).then(function(subfonts){
320
- var css, k, ref$, f, i$, len$;
323
+ var css, i$, len$, f, format;
321
324
  if (!subfonts.length) {
322
325
  return;
323
326
  }
324
- css = "." + this$.className + " { font-family: \"" + this$.name + "\"; }";
325
- for (k in ref$ = this$.sub.font) {
326
- f = ref$[k];
327
- }
327
+ css = "";
328
328
  for (i$ = 0, len$ = subfonts.length; i$ < len$; ++i$) {
329
329
  f = subfonts[i$];
330
- 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}";
330
+ format = this$._format(f.type);
331
+ css += "@font-face {\n font-family: \"" + this$.name + "\";\n src: url(\"" + f.url + "\") " + format + ";\n font-style: " + this$.style + ";\n font-weight: " + this$.weight + ";\n}";
331
332
  }
333
+ css += "." + this$.className + " { font-family: \"" + this$.name + "\"; }";
332
334
  return this$.css.push({
333
335
  content: css
334
336
  });
@@ -474,6 +476,7 @@ xlfont.prototype = import$(Object.create(Object.prototype), {
474
476
  }
475
477
  });
476
478
  xfl = {
479
+ xlfont: xlfont,
477
480
  range: {
478
481
  CJK: [[0x3040, 0x30ff], [0x3400, 0x4dbf], [0x4e00, 0x9fff], [0xf900, 0xfaff], [0xff66, 0xff9f], [0x3131, 0xD79D]]
479
482
  },
package/dist/index.min.js CHANGED
@@ -1 +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)}();
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=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=this._format(this.ext),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 t._init()}),this.init(),this}).prototype=l(Object.create(Object.prototype),{_format:function(e){return"format('"+(e="ttf"===(e=(e=null==e?"":e).toLowerCase())||"otf"===e?"truetype":e)+"')"},_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,i=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!i.sub.font[e]}).map(function(e,t){var n,r;return i.sub.font[e]=n={key:e},r=i.isXl?"woff":"ttf",n.needMerge=1!=+e,i._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:i.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;if(e.length){for(t="",n=0,r=e.length;n<r;++n)o=e[n],s=i._format(o.type),t+='@font-face {\n font-family: "'+i.name+'";\n src: url("'+o.url+'") '+s+";\n font-style: "+i.style+";\n font-weight: "+i.weight+";\n}";return t+="."+i.className+' { font-family: "'+i.name+'"; }',i.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={xlfont:o,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/index.js CHANGED
@@ -87,7 +87,7 @@ xlfMerger = function(arg$){
87
87
  });
88
88
  };
89
89
  xlfont = function(opt){
90
- var that, this$ = this;
90
+ var this$ = this;
91
91
  opt == null && (opt = {});
92
92
  this.opt = opt;
93
93
  this.sub = {
@@ -105,12 +105,7 @@ xlfont = function(opt){
105
105
  this.style = opt.style || 'normal';
106
106
  this.weight = opt.weight || '400';
107
107
  this.ext = opt.ext || (/\.(ttf|otf|woff2|woff)$/.exec(this.path) || [])[1] || '';
108
- this.format = (that = this.ext.toLowerCase()) ? that === 'ttf'
109
- ? 'truetype'
110
- : that === 'otf' ? 'truetype' : that : '';
111
- if (this.format) {
112
- this.format = "format('" + this.format + "')";
113
- }
108
+ this.format = this._format(this.ext);
114
109
  this.className = "xfl-" + (this.name || '').replace(/\s+/g, '_') + "-" + Math.random().toString(36).substring(2);
115
110
  this.isXl = opt.isXl != null
116
111
  ? opt.isXl
@@ -129,6 +124,14 @@ xlfont = function(opt){
129
124
  return this;
130
125
  };
131
126
  xlfont.prototype = import$(Object.create(Object.prototype), {
127
+ _format: function(f){
128
+ f == null && (f = '');
129
+ f = f.toLowerCase();
130
+ f = f === 'ttf'
131
+ ? 'truetype'
132
+ : f === 'otf' ? 'truetype' : f;
133
+ return "format('" + f + "')";
134
+ },
132
135
  _init: function(){
133
136
  var this$ = this;
134
137
  return Promise.resolve().then(function(){
@@ -317,18 +320,17 @@ xlfont.prototype = import$(Object.create(Object.prototype), {
317
320
  }).concat([font]);
318
321
  });
319
322
  }).then(function(subfonts){
320
- var css, k, ref$, f, i$, len$;
323
+ var css, i$, len$, f, format;
321
324
  if (!subfonts.length) {
322
325
  return;
323
326
  }
324
- css = "." + this$.className + " { font-family: \"" + this$.name + "\"; }";
325
- for (k in ref$ = this$.sub.font) {
326
- f = ref$[k];
327
- }
327
+ css = "";
328
328
  for (i$ = 0, len$ = subfonts.length; i$ < len$; ++i$) {
329
329
  f = subfonts[i$];
330
- 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}";
330
+ format = this$._format(f.type);
331
+ css += "@font-face {\n font-family: \"" + this$.name + "\";\n src: url(\"" + f.url + "\") " + format + ";\n font-style: " + this$.style + ";\n font-weight: " + this$.weight + ";\n}";
331
332
  }
333
+ css += "." + this$.className + " { font-family: \"" + this$.name + "\"; }";
332
334
  return this$.css.push({
333
335
  content: css
334
336
  });
@@ -474,6 +476,7 @@ xlfont.prototype = import$(Object.create(Object.prototype), {
474
476
  }
475
477
  });
476
478
  xfl = {
479
+ xlfont: xlfont,
477
480
  range: {
478
481
  CJK: [[0x3040, 0x30ff], [0x3400, 0x4dbf], [0x4e00, 0x9fff], [0xf900, 0xfaff], [0xff66, 0xff9f], [0x3131, 0xD79D]]
479
482
  },
package/index.min.js CHANGED
@@ -1 +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)}();
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=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=this._format(this.ext),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 t._init()}),this.init(),this}).prototype=l(Object.create(Object.prototype),{_format:function(e){return"format('"+(e="ttf"===(e=(e=null==e?"":e).toLowerCase())||"otf"===e?"truetype":e)+"')"},_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,i=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!i.sub.font[e]}).map(function(e,t){var n,r;return i.sub.font[e]=n={key:e},r=i.isXl?"woff":"ttf",n.needMerge=1!=+e,i._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:i.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;if(e.length){for(t="",n=0,r=e.length;n<r;++n)o=e[n],s=i._format(o.type),t+='@font-face {\n font-family: "'+i.name+'";\n src: url("'+o.url+'") '+s+";\n font-style: "+i.style+";\n font-weight: "+i.weight+";\n}";return t+="."+i.className+' { font-family: "'+i.name+'"; }',i.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={xlfont:o,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 +1 @@
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"]}}
1
+ {"author":"zbryikt","name":"@xlfont/load","license":"MIT","description":"XL font generator and loader","version":"2.1.0","browser":"index.min.js","main":"index.min.js","homepage":"https://github.com/xlfont/load","repository":{"type":"git","url":"https://github.com/xlfont/load"},"engines":{"node":">=10.14.1","npm":">=6.4.1"},"scripts":{"start":"npx server -r web -o true"},"bin":{"xfl":"./bin/build.js","xfl-meta":"./bin/meta.js"},"dependencies":{"@plotdb/colors":"^0.0.1","@plotdb/opentype.js":"^1.3.5","fs-extra":"^9.1.0","progress":"^2.0.3","ttf2woff":"^2.0.2","ttf2woff2":"^4.0.0","yargs":"^16.2.0"},"devDependencies":{"@loadingio/bootstrap.ext":"^0.0.4","@loadingio/debounce.js":"^1.0.1","@loadingio/ldquery":"^3.0.1","@loadingio/loading.css":"^3.0.0","@zbryikt/deploy":"^0.0.1","@zbryikt/template":"^2.3.33","bootstrap":"^4.5.3","bootstrap.native":"^3.0.13","fedep":"^1.1.1","glob":"^7.2.0","ldbutton":"^2.0.0","ldcover":"^3.0.1","ldloader":"^3.0.0","ldview":"^1.0.0","livescript":"^1.6.0","stylus":"^0.55.0","uglify-js":"^3.13.1","uglifycss":"^0.0.29"},"peerDependencies":{"@plotdb/opentype.js":"^1.3.5","proxise":"^1.0.1"},"frontendDependencies":{"root":"web/static/assets/lib","modules":["bootstrap","bootstrap.native","@loadingio/bootstrap.ext","@loadingio/ldquery","@loadingio/debounce.js","@loadingio/loading.css","@plotdb/opentype.js","ldview","ldcover","ldloader","ldbutton","proxise"]}}