1
2
3
4
5
6
|
import{E as q,d as B,v as k,T as O,f as j,b as E,C as P,R as H,c as $,I as J,g as K,e as V,h as L,i as Z,j as Q,k as ee,l as Y,m as te,n as se,o as C,w as ie,p as ne}from"./index-DvAPfvr6.js";class D extends q{constructor(){super(...arguments),this.chars=Object.create(null),this.lineHeight=0,this.fontFamily="",this.fontMetrics={fontSize:0,ascent:0,descent:0},this.baseLineOffset=0,this.distanceField={type:"none",range:0},this.pages=[],this.applyFillAsTint=!0,this.baseMeasurementFontSize=100,this.baseRenderedFontSize=100}get font(){return B(k,"BitmapFont.font is deprecated, please use BitmapFont.fontFamily instead."),this.fontFamily}get pageTextures(){return B(k,"BitmapFont.pageTextures is deprecated, please use BitmapFont.pages instead."),this.pages}get size(){return B(k,"BitmapFont.size is deprecated, please use BitmapFont.fontMetrics.fontSize instead."),this.fontMetrics.fontSize}get distanceFieldRange(){return B(k,"BitmapFont.distanceFieldRange is deprecated, please use BitmapFont.distanceField.range instead."),this.distanceField.range}get distanceFieldType(){return B(k,"BitmapFont.distanceFieldType is deprecated, please use BitmapFont.distanceField.type instead."),this.distanceField.type}destroy(e=!1){var t;this.emit("destroy",this),this.removeAllListeners();for(const s in this.chars)(t=this.chars[s].texture)==null||t.destroy();this.chars=null,e&&(this.pages.forEach(s=>s.texture.destroy(!0)),this.pages=null)}}const U=class G extends D{constructor(e){super(),this.resolution=1,this.pages=[],this._padding=0,this._measureCache=Object.create(null),this._currentChars=[],this._currentX=0,this._currentY=0,this._currentMaxCharHeight=0,this._currentPageIndex=-1,this._skipKerning=!1;const t={...G.defaultOptions,...e};this._textureSize=t.textureSize,this._mipmap=t.mipmap;const s=t.style.clone();t.overrideFill&&(s._fill.color=16777215,s._fill.alpha=1,s._fill.texture=O.WHITE,s._fill.fill=null),this.applyFillAsTint=t.overrideFill;const i=s.fontSize;s.fontSize=this.baseMeasurementFontSize;const n=j(s);t.overrideSize?(s._stroke&&(s._stroke.width*=this.baseRenderedFontSize/i),s.dropShadow&&(s.dropShadow.blur*=this.baseRenderedFontSize/i,s.dropShadow.distance*=this.baseRenderedFontSize/i)):s.fontSize=this.baseRenderedFontSize=i,this._style=s,this._skipKerning=t.skipKerning??!1,this.resolution=t.resolution??1,this._padding=t.padding??4,t.textureStyle&&(this._textureStyle=t.textureStyle instanceof E?t.textureStyle:new E(t.textureStyle)),this.fontMetrics=P.measureFont(n),this.lineHeight=s.lineHeight||this.fontMetrics.fontSize||s.fontSize}ensureCharacters(e){var _,v;const t=P.graphemeSegmenter(e).filter(y=>!this._currentChars.includes(y)).filter((y,m,d)=>d.indexOf(y)===m);if(!t.length)return;this._currentChars=[...this._currentChars,...t];let s;this._currentPageIndex===-1?s=this._nextPage():s=this.pages[this._currentPageIndex];let{canvas:i,context:n}=s.canvasAndContext,a=s.texture.source;const o=this._style;let r=this._currentX,h=this._currentY,l=this._currentMaxCharHeight;const f=this.baseRenderedFontSize/this.baseMeasurementFontSize,w=(((_=o.dropShadow)==null?void 0:_.distance)??0)+(((v=o._stroke)==null?void 0:v.width)??0),F=this._padding+w;let x=!1;const z=i.width/this.resolution,b=i.height/this.resolution;for(let y=0;y<t.length;y++){const m=t[y],d=P.measureText(m,o,i,!1);d.lineHeight=d.height;const p=d.width*f,g=Math.ceil((o.fontStyle==="italic"?2:1)*p),u=d.height*f,W=g+F*2,M=u+F*2;if(x=!1,m!==`
`&&m!=="\r"&&m!==" "&&m!==" "&&(x=!0,l=Math.ceil(Math.max(M,l))),r+W>z&&(h+=l,l=M,r=0,h+l>b)){a.update();const S=this._nextPage();i=S.canvasAndContext.canvas,n=S.canvasAndContext.context,a=S.texture.source,r=0,h=0,l=0}const T=n.measureText(m).width/f;if(this.chars[m]={id:m.codePointAt(0),xOffset:-(F/f),yOffset:-(F/f),xAdvance:T,kerning:{}},x){this._drawGlyph(n,d,r+F,h+F,f,o);const S=a.width*f,I=a.height*f,R=new H(r/S*a.width,h/I*a.height,W/S*a.width,M/I*a.height);this.chars[m].texture=new O({source:a,frame:R}),r+=Math.ceil(W)}}a.update(),this._currentX=r,this._currentY=h,this._currentMaxCharHeight=l,this._skipKerning||this._applyKerning(t,n,f)}get pageTextures(){return B(k,"BitmapFont.pageTextures is deprecated, please use BitmapFont.pages instead."),this.pages}_applyKerning(e,t,s){const i=this._measureCache;for(let n=0;n<e.length;n++){const a=e[n];for(let o=0;o<this._currentChars.length;o++){const r=this._currentChars[o];let h=i[a];h||(h=i[a]=t.measureText(a).width);let l=i[r];l||(l=i[r]=t.measureText(r).width);let f=t.measureText(a+r).width,w=f-(h+l);w&&this.chars[a]&&(this.chars[a].kerning[r]=w/s),f=t.measureText(a+r).width,w=f-(h+l),w&&this.chars[r]&&(this.chars[r].kerning[a]=w/s)}}}_nextPage(){this._currentPageIndex++;const e=this.resolution,t=$.getOptimalCanvasAndContext(this._textureSize,this._textureSize,e);this._setupContext(t.context,this._style,e);const s=e*(this.baseRenderedFontSize/this.baseMeasurementFontSize),i=new O({source:new J({resource:t.canvas,resolution:s,alphaMode:"premultiply-alpha-on-upload",autoGenerateMipmaps:this._mipmap})});this._textureStyle&&(i.source.style=this._textureStyle);const n={canvasAndContext:t,texture:i};return this.pages[this._currentPageIndex]=n,n}_setupContext(e,t,s){t.fontSize=this.baseRenderedFontSize,e.scale(s,s),e.font=j(t),t.fontSize=this.baseMeasurementFontSize,e.textBaseline=t.textBaseline;const i=t._stroke,n=(i==null?void 0:i.width)??0;if(i&&(e.lineWidth=n,e.lineJoin=i.join,e.miterLimit=i.miterLimit,e.strokeStyle=K(i,e)),t._fill&&(e.fillStyle=K(t._fill,e)),t.dropShadow){const a=t.dropShadow,o=V.shared.setValue(a.color).toArray(),r=a.blur*s,h=a.distance*s;e.shadowColor=`rgba(${o[0]*255},${o[1]*255},${o[2]*255},${a.alpha})`,e.shadowBlur=r,e.shadowOffsetX=Math.cos(a.angle)*h,e.shadowOffsetY=Math.sin(a.angle)*h}else e.shadowColor="black",e.shadowBlur=0,e.shadowOffsetX=0,e.shadowOffsetY=0}_drawGlyph(e,t,s,i,n,a){const o=t.text,r=t.fontProperties,h=a._stroke,l=((h==null?void 0:h.width)??0)*n,f=s+l/2,w=i-l/2,F=r.descent*n,x=t.lineHeight*n;let z=!1;a.stroke&&l&&(z=!0,e.strokeText(o,f,w+x-F));const{shadowBlur:b,shadowOffsetX:_,shadowOffsetY:v}=e;a._fill&&(z&&(e.shadowBlur=0,e.shadowOffsetX=0,e.shadowOffsetY=0),e.fillText(o,f,w+x-F)),z&&(e.shadowBlur=b,e.shadowOffsetX=_,e.shadowOffsetY=v)}destroy(){super.destroy();for(let e=0;e<this.pages.length;e++){const{canvasAndContext:t,texture:s}=this.pages[e];$.returnCanvasAndContext(t),s.destroy(!0)}this.pages=null}};U.defaultOptions={textureSize:512,style:new L,mipmap:!0};let N=U;function ae(c,e,t,s){var y,m;const i={width:0,height:0,offsetY:0,scale:e.fontSize/t.baseMeasurementFontSize,lines:[{width:0,charPositions:[],spaceWidth:0,spacesIndex:[],chars:[]}]};i.offsetY=t.baseLineOffset;let n=i.lines[0],a=null,o=!0;const r={width:0,start:0,index:0,positions:[],chars:[]},h=t.baseMeasurementFontSize/e.fontSize,l=e.letterSpacing*h,f=e.wordWrapWidth*h,w=e.lineHeight?e.lineHeight*h:t.lineHeight,F=e.wordWrap&&e.breakWords,x=Z(e.whiteSpace),z=Q(e.whiteSpace);if(x||z){const d=[];let p=x;for(let g=0;g<c.length;g++){let u=c[g];if(u==="\r"||u===`
`)if(z)u==="\r"&&c[g+1]===`
`&&g++,u=" ";else{x&&(p=!0),d.push(u);continue}if(ee(u))if(x&&Y(u)){if(p)continue;p=!0,d.push(" ")}else p=!1,d.push(u);else p=!1,d.push(u)}c=d}const b=d=>{const p=n.width;for(let g=0;g<r.index;g++){const u=d.positions[g];n.chars.push(d.chars[g]),n.charPositions.push(u+p)}n.width+=d.width,(r.index>0||!x)&&(o=!1),r.width=0,r.index=0,r.chars.length=0},_=()=>{let d=n.chars.length-1;if(s){let p=n.chars[d];for(;Y(p);)n.width-=t.chars[p].xAdvance,n.spacesIndex.pop(),p=n.chars[--d]}i.width=Math.max(i.width,n.width),n={width:0,charPositions:[],chars:[],spaceWidth:0,spacesIndex:[]},o=!0,i.lines.push(n),i.height+=w},v=d=>d-l>f;for(let d=0;d<c.length+1;d++){let p;const g=d===c.length;g||(p=c[d]);const u=t.chars[p];if(/(?:\s)/.test(p)||p==="\r"||p===`
`||g){if(!o&&e.wordWrap&&v(n.width+r.width)?(_(),b(r),!g&&u&&n.charPositions.push(0)):(r.start=n.width,b(r),!g&&u&&n.charPositions.push(0)),p==="\r"||p===`
`)_();else if(!g&&u){const S=u.xAdvance+(((y=u.kerning)==null?void 0:y[a])||0)+l;n.width+=S,n.spaceWidth=S,n.spacesIndex.push(n.charPositions.length),n.chars.push(p)}}else if(u){const T=((m=u.kerning)==null?void 0:m[a])||0,S=u.xAdvance+T+l;F&&v(r.width+S)&&(o||_(),b(r),_()),r.positions[r.index++]=r.width+T,r.chars.push(p),r.width+=S,te(p)&&(!o&&e.wordWrap&&v(n.width+r.width)&&_(),b(r))}a=p}return _(),e.align==="center"?re(i):e.align==="right"?oe(i):e.align==="justify"&&he(i),i}function re(c){for(let e=0;e<c.lines.length;e++){const t=c.lines[e],s=c.width/2-t.width/2;for(let i=0;i<t.charPositions.length;i++)t.charPositions[i]+=s}}function oe(c){for(let e=0;e<c.lines.length;e++){const t=c.lines[e],s=c.width-t.width;for(let i=0;i<t.charPositions.length;i++)t.charPositions[i]+=s}}function he(c){const e=c.width;for(let t=0;t<c.lines.length-2;t++){const s=c.lines[t];let i=0,n=s.spacesIndex[i++],a=0;const o=s.spacesIndex.length,h=(e-s.width)/o;for(let l=0;l<s.charPositions.length;l++)l===n&&(n=s.spacesIndex[i++],a+=h),s.charPositions[l]+=a}}function le(c){if(c==="")return[];typeof c=="string"&&(c=[c]);const e=[];for(let t=0,s=c.length;t<s;t++){const i=c[t];if(Array.isArray(i)){if(i.length!==2)throw new Error(`[BitmapFont]: Invalid character range length, expecting 2 got ${i.length}.`);if(i[0].length===0||i[1].length===0)throw new Error("[BitmapFont]: Invalid character delimiter.");const n=i[0].charCodeAt(0),a=i[1].charCodeAt(0);if(a<n)throw new Error("[BitmapFont]: Invalid character range.");for(let o=n,r=a;o<=r;o++)e.push(String.fromCharCode(o))}else e.push(...Array.from(i))}if(e.length===0)throw new Error("[BitmapFont]: Empty set when resolving characters.");return e}let A=0;class ce{constructor(){this.ALPHA=[["a","z"],["A","Z"]," "],this.NUMERIC=[["0","9"]],this.ALPHANUMERIC=[["a","z"],["A","Z"],["0","9"]," "],this.ASCII=[[" ","~"]],this.defaultOptions={chars:this.ALPHANUMERIC,resolution:1,padding:4,skipKerning:!1,textureStyle:null},this.measureCache=se(1e3)}getFont(e,t){var a,o;let s=`${t.fontFamily}-bitmap`,i=!0;if(C.has(s)){const r=C.get(s);return(a=r.ensureCharacters)==null||a.call(r,e),r}if(t._fill.fill&&!t._stroke?(s+=t._fill.fill.styleKey,i=!1):(t._stroke||t.dropShadow)&&(s=`${t.styleKey}-bitmap`,i=!1),s+=`-${t.fontStyle}`,s+=`-${t.fontVariant}`,s+=`-${t.fontWeight}`,!C.has(s)){const r=Object.create(t);r._lineHeight=0;const h=new N({style:r,overrideFill:i,overrideSize:!0,...this.defaultOptions});A++,A>50&&ie("BitmapText",`You have dynamically created ${A} bitmap fonts, this can be inefficient. Try pre installing your font styles using \`BitmapFont.install({name:"style1", style})\``),h.once("destroy",()=>{A--,C.remove(s)}),C.set(s,h)}const n=C.get(s);return(o=n.ensureCharacters)==null||o.call(n,e),n}getLayout(e,t,s=!0){const i=this.getFont(e,t),n=`${e}-${t.styleKey}-${s}`;if(this.measureCache.has(n))return this.measureCache.get(n);const a=P.graphemeSegmenter(e),o=ae(a,t,i,s);return this.measureCache.set(n,o),o}measureText(e,t,s=!0){return this.getLayout(e,t,s)}install(...e){var h,l,f,w;let t=e[0];typeof t=="string"&&(t={name:t,style:e[1],chars:(h=e[2])==null?void 0:h.chars,resolution:(l=e[2])==null?void 0:l.resolution,padding:(f=e[2])==null?void 0:f.padding,skipKerning:(w=e[2])==null?void 0:w.skipKerning},B(k,"BitmapFontManager.install(name, style, options) is deprecated, use BitmapFontManager.install({name, style, ...options})"));const s=t==null?void 0:t.name;if(!s)throw new Error("[BitmapFontManager] Property `name` is required.");t={...this.defaultOptions,...t};const i=t.style,n=i instanceof L?i:new L(i),a=t.dynamicFill??this._canUseTintForStyle(n),o=new N({style:n,overrideFill:a,skipKerning:t.skipKerning,padding:t.padding,resolution:t.resolution,overrideSize:!1,textureStyle:t.textureStyle}),r=le(t.chars);return o.ensureCharacters(r.join("")),C.set(`${s}-bitmap`,o),o.once("destroy",()=>C.remove(`${s}-bitmap`)),o}uninstall(e){const t=`${e}-bitmap`,s=C.get(t);s&&s.destroy()}_canUseTintForStyle(e){return!e._stroke&&(!e.dropShadow||e.dropShadow.color===0)&&!e._fill.fill&&e._fill.color===16777215}}const X=new ce;class pe extends D{constructor(e,t){super();const{textures:s,data:i}=e;Object.keys(i.pages).forEach(n=>{const a=i.pages[parseInt(n,10)],o=s[a.id];this.pages.push({texture:o})}),Object.keys(i.chars).forEach(n=>{const a=i.chars[n],{frame:o,source:r,rotate:h}=s[a.page],l=ne.transformRectCoords(a,o,h,new H),f=new O({frame:l,orig:new H(0,0,a.width,a.height),source:r,rotate:h});this.chars[n]={id:n.codePointAt(0),xOffset:a.xOffset,yOffset:a.yOffset,xAdvance:a.xAdvance,kerning:a.kerning??{},texture:f}}),this.baseRenderedFontSize=i.fontSize,this.baseMeasurementFontSize=i.fontSize,this.fontMetrics={ascent:0,descent:0,fontSize:i.fontSize},this.baseLineOffset=i.baseLineOffset,this.lineHeight=i.lineHeight,this.fontFamily=i.fontFamily,this.distanceField=i.distanceField??{type:"none",range:0},this.url=t}destroy(){super.destroy();for(let e=0;e<this.pages.length;e++){const{texture:t}=this.pages[e];t.destroy(!0)}this.pages=null}static install(e){X.install(e)}static uninstall(e){X.uninstall(e)}}export{pe as BitmapFont};
|