UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 1.52 kB
import{math as t}from"../../core/math/math.js";import{Texture as e}from"../../platform/graphics/texture.js";import{FILTER_NEAREST as a}from"../../platform/graphics/constants.js";class i{constructor(i,o){const h=t=>{t.font='10px "Lucida Console", Monaco, monospace',t.textAlign="left",t.textBaseline="alphabetic"},l=document.createElement("canvas"),n=l.getContext("2d",{alpha:!0});h(n);const r=new Map;let s=5,c=5;o.forEach((t=>{const e=n.measureText(t),a=Math.ceil(-e.actualBoundingBoxLeft),i=Math.ceil(e.actualBoundingBoxRight),o=Math.ceil(e.actualBoundingBoxAscent),h=Math.ceil(e.actualBoundingBoxDescent),l=a+i,d=o+h;s+l+5>=512&&(s=5,c+=16),r.set(t,{l:a,r:i,a:o,d:h,w:l,h:d,x:s,y:c}),s+=l+5})),l.width=512,l.height=t.nextPowerOfTwo(c+16+5),h(n),n.fillStyle="rgb(0, 0, 0)",n.fillRect(0,0,l.width,l.height),r.forEach(((t,e)=>{n.fillStyle=(t=>"."===t||1===t.length&&t.charCodeAt(0)>=48&&t.charCodeAt(0)<=57)(e)?"rgb(255, 255, 255)":"rgb(170, 170, 170)",n.fillText(e,t.x-t.l,t.y+t.a)})),this.placements=r;const d=n.getImageData(0,0,l.width,l.height).data;for(let t=0;t<d.length;t+=4)d[t+3]=d[t+0],d[t+0]=255,d[t+1]=255,d[t+2]=255;this.texture=new e(i,{name:"mini-stats-word-atlas",width:l.width,height:l.height,mipmaps:!1,minFilter:a,magFilter:a,levels:[d]})}destroy(){this.texture.destroy(),this.texture=null}render(t,e,a,i){const o=this.placements.get(e);if(o){const e=1;return t.quad(a+o.l-e,i-o.d+e,o.w+2*e,o.h+2*e,o.x-e,this.texture.height-o.y-o.h-e,void 0,void 0,this.texture,1),o.w}return 0}}export{i as WordAtlas};