UNPKG

phaser3-bitmapfont-factory

Version:

Creates bitmapfonts in Phaser3 at runtime, using available browser fonts

2 lines (1 loc) 11.6 kB
function e(e,t){if(!{}.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e}var t=0;function r(e){return"__private_"+t+++"_"+e}var n=[" A"," T"," Y","11","A ","AT","AV","AW","AY","Av","Aw","Ay","F,","F.","FA","L ","LT","LV","LW","LY","Ly","P ","P,","P.","PA","RT","RV","RW","RY","T ","T,","T-","T.","T:","T;","TA","TO","Ta","Tc","Te","Ti","To","Tr","Ts","Tu","Tw","Ty","V,","V-","V.","V:","V;","VA","Va","Ve","Vi","Vo","Vr","Vu","Vy","W,","W-","W.","W:","W;","WA","Wa","We","Wi","Wo","Wr","Wu","Wy","Y ","Y,","Y-","Y.","Y:","Y;","YA","Ya","Ye","Yi","Yo","Yp","Yq","Yu","Yv","ff","r,","r.","v,","v.","w,","w.","y,","y."],i=["Arial","Calibri","Helvetica","Roboto","Trebuchet MS","Verdana"],a=["Garamond","Georgia","serif","Times","Times New Roman"],o=["Consolas","Courier","Courier New","monospace"];function s(e,t){return parseInt(e.getAttribute(t),10)}var l=function(e,t,r,n,i){void 0===r&&(r=0),void 0===n&&(n=0);var a=t.cutX,o=t.cutY,l=t.source.width,c=t.source.height,u=t.sourceIndex,h={},f=e.getElementsByTagName("info")[0],v=e.getElementsByTagName("common")[0];h.font=f.getAttribute("face"),h.size=s(f,"size"),h.lineHeight=s(v,"lineHeight")+n,h.chars={};var d=e.getElementsByTagName("char"),g=void 0!==t&&t.trimmed;if(g)var m=t.height,x=t.width;for(var y=0;y<d.length;y++){var p=d[y],b=s(p,"id"),P=String.fromCharCode(b),w=s(p,"x"),B=s(p,"y"),T=s(p,"width"),O=s(p,"height");g&&(w<x&&(x=w),B<m&&(m=B)),g&&0!==m&&0!==x&&(w-=t.x,B-=t.y);var j=(a+w)/l,Y=(o+B)/c,W=(a+w+T)/l,A=(o+B+O)/c;if(h.chars[b]={x:w,y:B,width:T,height:O,centerX:Math.floor(T/2),centerY:Math.floor(O/2),xOffset:s(p,"xoffset"),yOffset:s(p,"yoffset"),xAdvance:s(p,"xadvance")+r,data:{},kerning:{},u0:j,v0:Y,u1:W,v1:A},i&&0!==T&&0!==O){var k=i.add(P,u,w,B,T,O);k&&k.setUVs(T,O,j,Y,W,A)}}var X=e.getElementsByTagName("kerning");for(y=0;y<X.length;y++){var C=X[y],L=s(C,"first"),V=s(C,"second"),H=s(C,"amount");h.chars[V].kerning[L]=H}return h};function c(e,t,r){if(!e.s){if(r instanceof h){if(!r.s)return void(r.o=c.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(c.bind(null,e,t),c.bind(null,e,2));e.s=t,e.v=r;var n=e.o;n&&n(e)}}var u=/*#__PURE__*/r("ctx"),h=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(t,r){var n=new e,i=this.s;if(i){var a=1&i?t:r;if(a){try{c(n,1,a(this.v))}catch(e){c(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?c(n,1,t?t(i):i):r?c(n,1,r(i)):c(n,2,i)}catch(e){c(n,2,e)}},n},e}();function f(e,t,r){var n,i,a=-1;return function o(s){try{for(;++a<e.length&&(!r||!r());)if((s=t(a))&&s.then){if(!((l=s)instanceof h&&1&l.s))return void s.then(o,i||(i=c.bind(null,n=new h,2)));s=s.v}n?c(n,1,s):n=s}catch(e){c(n||(n=new h),2,e)}var l}(),n}var v=/*#__PURE__*/r("currentPendingSteps"),d=/*#__PURE__*/r("currentTexture"),g=/*#__PURE__*/r("currentXMLs"),m=/*#__PURE__*/r("currentHash"),x=/*#__PURE__*/r("isOnCache"),y=/*#__PURE__*/r("onComplete"),p=/*#__PURE__*/r("padding"),b=/*#__PURE__*/r("tasks"),P=/*#__PURE__*/r("textureWidth"),w=/*#__PURE__*/r("textureHeight"),B=/*#__PURE__*/r("totalGlyphs"),T=/*#__PURE__*/r("totalHeight"),O=/*#__PURE__*/r("totalProgress"),j=/*#__PURE__*/r("totalWidth"),Y=/*#__PURE__*/r("calcBounds"),W=/*#__PURE__*/r("calcKernings"),A=/*#__PURE__*/r("finish"),k=/*#__PURE__*/r("getKerningPairs"),X=/*#__PURE__*/r("getTextureWidth"),C=/*#__PURE__*/r("getValidFont"),L=/*#__PURE__*/r("hash"),V=/*#__PURE__*/r("makeGlyphs"),H=/*#__PURE__*/r("makeTexture"),S=/*#__PURE__*/r("makeXMLs"),R=/*#__PURE__*/r("step");module.exports=/*#__PURE__*/function(){function t(t,r,s){var c=this,h=this,F=this,M=this,N=this;void 0===s&&(s={PoT:!1,disableCache:!1}),this.onProgress=void 0,this.PoT=!1,this.scene=void 0,this.disableCache=!1,this.defaultFonts={sansSerif:i,serif:a,monospace:o},Object.defineProperty(this,u,{writable:!0,value:void 0}),Object.defineProperty(this,v,{writable:!0,value:0}),Object.defineProperty(this,d,{writable:!0,value:null}),Object.defineProperty(this,g,{writable:!0,value:[]}),Object.defineProperty(this,m,{writable:!0,value:void 0}),Object.defineProperty(this,x,{writable:!0,value:!1}),Object.defineProperty(this,y,{writable:!0,value:function(){}}),Object.defineProperty(this,p,{writable:!0,value:1}),Object.defineProperty(this,b,{writable:!0,value:[]}),Object.defineProperty(this,P,{writable:!0,value:0}),Object.defineProperty(this,w,{writable:!0,value:0}),Object.defineProperty(this,B,{writable:!0,value:0}),Object.defineProperty(this,T,{writable:!0,value:0}),Object.defineProperty(this,O,{writable:!0,value:0}),Object.defineProperty(this,j,{writable:!0,value:0}),Object.defineProperty(this,Y,{writable:!0,value:function(){var t,r=e(N,b)[b],n=0,i=e(N,p)[p],a=e(N,p)[p];t=e(N,X)[X](e(N,B)[B],e(N,T)[T],e(N,j)[j]);for(var o=[],s=0;s<r.length;s++)o.push.apply(o,r[s].glyphs);o.sort(function(e,t){return t.xmlHeight-e.xmlHeight});var l=o[0],c=l.xmlHeight+e(N,p)[p];o.forEach(function(r){r.xmlX=a,r.xmlY=i,(a=r.xmlX+r.xmlWidth+e(N,p)[p])>t&&(r.xmlX=e(N,p)[p],r.xmlY=c+i,l=r,a=r.xmlX+r.xmlWidth+e(N,p)[p],i+=c,c=r.xmlHeight+e(N,p)[p]),r.printX=r.xmlX+r.actualBoundingBoxLeft,r.printY=r.xmlY+r.actualBoundingBoxAscent}),n=l.xmlY+l.xmlHeight+e(N,p)[p],N.PoT&&(n=Phaser.Math.Pow2.GetNext(n)),e(N,P)[P]=t,e(N,w)[w]=n}}),Object.defineProperty(this,W,{writable:!0,value:function(){try{var t=e(c,b)[b],r=e(c,u)[u];return Promise.resolve(f(t,function(n){var i=t[n];if(i.getKernings){var a=i.kernings,o=i.chars,s=i.glyphs,l=e(c,k)[k](i);r.font=i.font;for(var u=0;u<l.length;u++){var h=l[u],f=s[o.indexOf(h[0])],v=s[o.indexOf(h[1])],d=f.xmlWidth+v.xmlWidth,g=r.measureText(l[u]),m=Math.ceil(g.actualBoundingBoxRight+g.actualBoundingBoxLeft-d);0!=m&&a.push({first:f.id,second:v.id,amount:m})}var x=function(){if(c.onProgress){e(c,O)[O]+=1/e(c,b)[b].length*.5;var t=c;return Promise.resolve(new Promise(function(r){t.onProgress(e(t,O)[O]),t.scene.events.once("preupdate",r)})).then(function(){})}}();return x&&x.then?x.then(function(){}):void 0}}))}catch(e){return Promise.reject(e)}}}),Object.defineProperty(this,A,{writable:!0,value:function(){for(var t=e(N,d)[d],r=e(N,g)[g],n=e(N,b)[b][0].key,i=N.scene.textures.getFrame(n),a=0;a<r.length;a++){var o=l(r[a],i,0,0,t);N.scene.cache.bitmapFont.add(e(N,b)[b][a].key,{data:o,texture:n,frame:null})}if(!e(N,x)[x]&&!N.disableCache){var s={tasks:e(N,b)[b],textureW:e(N,P)[P],textureH:e(N,w)[w]};localStorage.setItem(e(N,m)[m],JSON.stringify(s))}e(N,d)[d]=null,e(N,g)[g]=[],e(N,b)[b]=[],e(N,y)[y]()}}),Object.defineProperty(this,k,{writable:!0,value:function(e){for(var t=[],r=0;r<n.length;r++){var i=n[r];-1!=e.chars.indexOf(i[0])&&-1!=e.chars.indexOf(i[1])&&t.push(n[r])}return t}}),Object.defineProperty(this,X,{writable:!0,value:function(t,r,n){var i=Math.max(r/t,n/t),a=Math.ceil(Math.sqrt(i*i*t));return N.PoT?Phaser.Math.Pow2.GetNext(a):a+2*e(N,p)[p]}}),Object.defineProperty(this,C,{writable:!0,value:function(e){for(var t="",r=0;r<e.length;r++)if(N.check(e[r])){t=e[r];break}return t}}),Object.defineProperty(this,L,{writable:!0,value:function(e){for(var t=2166136261,r=0;r<e.length;r++)t=16777619*(t^=e.charCodeAt(r))>>>0;return t.toString()}}),Object.defineProperty(this,V,{writable:!0,value:function(){try{var t=e(h,b)[b],r=e(h,u)[u];return Promise.resolve(f(t,function(n){var i=t[n],a=i.chars,o=a.length;e(h,B)[B]+=o,r.font=i.font;for(var s=0;s<o;s++){var l=a[s],c={actualBoundingBoxAscent:0,actualBoundingBoxLeft:0,id:l.charCodeAt(0),letter:l,printX:0,printY:0,xmlX:0,xmlY:0,xmlXoffset:0,xmlYoffset:0,xmlHeight:0,xmlWidth:0,xmlXadvance:0},u=r.measureText(l);c.xmlXoffset=-u.actualBoundingBoxLeft,c.xmlYoffset=u.actualBoundingBoxDescent,c.xmlWidth=u.actualBoundingBoxRight+u.actualBoundingBoxLeft,c.xmlHeight=u.actualBoundingBoxDescent+u.actualBoundingBoxAscent,c.xmlXadvance=u.width,c.actualBoundingBoxAscent=u.actualBoundingBoxAscent,c.actualBoundingBoxLeft=u.actualBoundingBoxLeft,i.glyphs.push(c),e(h,T)[T]+=c.xmlHeight+e(h,p)[p],e(h,j)[j]+=c.xmlWidth+e(h,p)[p]}var f=function(){if(h.onProgress){e(h,O)[O]+=1/e(h,b)[b].length*.5;var t=h;return Promise.resolve(new Promise(function(r){t.onProgress(e(t,O)[O]),t.scene.events.once("preupdate",r)})).then(function(){})}}();if(f&&f.then)return f.then(function(){})}))}catch(e){return Promise.reject(e)}}}),Object.defineProperty(this,H,{writable:!0,value:function(){try{return Promise.resolve(function(e,t,r,n){try{var i=t[0].key,a=document.createElement("canvas"),o=a.getContext("2d",{willReadFrequently:!0});a.width=r,a.height=n;for(var s=0;s<t.length;s++){var l=t[s],c=l.glyphs;o.font=l.font,o.fillStyle=l.style.color?l.style.color:"white";for(var u=0;u<c.length;u++){var h=c[u];o.fillText(h.letter,h.printX,h.printY)}}return Promise.resolve(new Promise(function(t){a.toBlob(function(r){var n=document.createElement("img"),a=URL.createObjectURL(r);n.onload=function(){e.textures.addImage(i,n);var r=e.textures.get(i);URL.revokeObjectURL(a),t(r)},n.src=a},"image/png")}))}catch(e){return Promise.reject(e)}}(F.scene,e(F,b)[b],e(F,P)[P],e(F,w)[w])).then(function(t){e(F,d)[d]=t,e(F,R)[R](null)})}catch(e){return Promise.reject(e)}}}),Object.defineProperty(this,S,{writable:!0,value:function(){try{return e(M,g)[g]=function(e){for(var t=[],r=0;r<e.length;r++){for(var n=e[r],i=n.chars.length,a=n.fontFamily,o=n.style.fontSize.replace("px",""),s=n.glyphs,l=Number.parseInt(o),c='<?xml version="1.0"?><font><info face="'+a+'" size="'+o+'"></info><common lineHeight="'+o+'"></common><chars count="'+i+'">',u="",h=0;h<i;h++){var f=s[h];u+='<char id="'+f.id+'" x="'+f.xmlX+'" y="'+f.xmlY+'" width="'+f.xmlWidth+'" height="'+f.xmlHeight+'" xoffset="'+f.xmlXoffset+'" yoffset="'+(l-(f.printY-f.xmlY))+'" xadvance="'+f.xmlXadvance+'"/>'}u+="</chars>";var v="";if(n.getKernings){var d=n.kernings.length,g=n.kernings;v+='<kernings count="'+d+'">';for(var m=0;m<d;m++){var x=g[m];v+='<kerning first="'+x.first+'" second="'+x.second+'" amount="'+x.amount+'" />'}v+="</kernings>"}var y=c+u+v+"</font>",p=(new DOMParser).parseFromString(y,"application/xml");t.push(p)}return t}(e(M,b)[b]),e(M,R)[R](null),Promise.resolve()}catch(e){return Promise.reject(e)}}}),Object.defineProperty(this,R,{writable:!0,value:function(t){e(N,v)[v]-=1,0==e(N,v)[v]&&e(N,A)[A]()}}),this.scene=t,this.PoT=s.PoT,this.disableCache=s.disableCache,this.onProgress=s.onProgress,e(this,y)[y]=r,e(this,u)[u]=document.createElement("canvas").getContext("2d")}var r=t.prototype;return r.check=function(t){var r=e(this,u)[u];r.font="12px default";var n=r.measureText("0");r.font="12px "+t;var i=r.measureText("0");return n.actualBoundingBoxAscent!=i.actualBoundingBoxAscent&&n.actualBoundingBoxRight!=i.actualBoundingBoxRight},r.exec=function(){try{var t=function(){e(r,v)[v]=2,e(r,H)[H](),e(r,S)[S]()},r=this;if(e(r,B)[B]=0,e(r,T)[T]=0,e(r,j)[j]=0,!r.disableCache){var n=e(r,L)[L](JSON.stringify(e(r,b)[b]));try{var i=localStorage.getItem(n);if(e(r,m)[m]=n,i){var a=JSON.parse(i);e(r,b)[b]=a.tasks,e(r,P)[P]=a.textureW,e(r,w)[w]=a.textureH,e(r,x)[x]=!0,e(r,O)[O]=1,r.onProgress&&r.onProgress(1)}}catch(e){r.disableCache=!0}}var o=function(){if(0==e(r,P)[P])return Promise.resolve(e(r,V)[V]()).then(function(){return e(r,Y)[Y](),Promise.resolve(e(r,W)[W]()).then(function(){})})}();return Promise.resolve(o&&o.then?o.then(t):t())}catch(e){return Promise.reject(e)}},r.make=function(t,r,n,i,a){var o;void 0===a&&(a=!0),null==i.fontSize&&(i.fontSize="32px"),o="string"==typeof r?r:e(this,C)[C](r);var s="";s+=i.fontStyle?i.fontStyle+" ":"",s+=i.fontSize+" ";var l={chars:n,font:s+='"'+o+'"',fontFamily:o,glyphs:[],getKernings:a,kernings:[],key:t,style:i};l.style.fontFamily=l.fontFamily,e(this,b)[b].push(l)},t}();