textmode.js 0.2.0-beta.3 → 0.2.0-beta.5

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.
@@ -1,28 +1,28 @@
1
- var te=Object.defineProperty;var ee=(b,v,L)=>v in b?te(b,v,{enumerable:!0,configurable:!0,writable:!0,value:L}):b[v]=L;var u=(b,v,L)=>ee(b,typeof v!="symbol"?v+"":v,L);var t,e;t=this,e=function(b){class v extends Error{constructor(r,i={}){super(v.i(r,i)),this.name="TextmodeError"}static i(r,i){let s=r;if(i&&Object.keys(i).length>0){s+=`
1
+ var te=Object.defineProperty;var ee=(b,v,L)=>v in b?te(b,v,{enumerable:!0,configurable:!0,writable:!0,value:L}):b[v]=L;var u=(b,v,L)=>ee(b,typeof v!="symbol"?v+"":v,L);var e,s;e=this,s=function(b){class v extends Error{constructor(t,r={}){super(v.i(t,r)),this.name="TextmodeError"}static i(t,r){let i=t;if(r&&Object.keys(r).length>0){i+=`
2
2
 
3
- 📋 Context:`;for(const[n,o]of Object.entries(i))s+=`
4
- - ${n}: ${v.o(o)}`}return s+=`
3
+ 📋 Context:`;for(const[n,o]of Object.entries(r))i+=`
4
+ - ${n}: ${v.o(o)}`}return i+=`
5
5
 
6
- `,s+="↓".repeat(24)+`
7
- `,s}static o(r){if(r===null)return"null";if(r===void 0)return"undefined";if(typeof r=="string")return`"${r}"`;if(typeof r=="number"||typeof r=="boolean")return r+"";if(Array.isArray(r))return r.length===0?"[]":r.length<=5?`[${r.map(i=>v.o(i)).join(", ")}]`:`[${r.slice(0,3).map(i=>v.o(i)).join(", ")}, ... +${r.length-3} more]`;if(typeof r=="object"){const i=Object.keys(r);return i.length===0?"{}":i.length<=3?`{ ${i.map(s=>`${s}: ${v.o(r[s])}`).join(", ")} }`:`{ ${i.slice(0,2).map(s=>`${s}: ${v.o(r[s])}`).join(", ")}, ... +${i.length-2} more }`}return r+""}}var L=(c=>(c[c.SILENT=0]="SILENT",c[c.WARNING=1]="WARNING",c[c.ERROR=2]="ERROR",c[c.THROW=3]="THROW",c))(L||{});const I=class I{constructor(){u(this,"u",{globalLevel:3})}static p(){return I.l||(I.l=new I),I.l}_(r,i){const s="%c[textmode.js] Oops! (╯°□°)╯︵ Something went wrong in your code.",n="color: #f44336; font-weight: bold; background: #ffebee; padding: 2px 6px; border-radius: 3px;";switch(this.u.globalLevel){case 0:return!1;case 1:return console.group(s,n),console.warn(v.i(r,i)),console.groupEnd(),!1;case 2:return console.group(s,n),console.error(v.i(r,i)),console.groupEnd(),!1;default:throw new v(r,i)}}m(r,i,s){return!!r||(this._(i,s),!1)}v(r){this.u.globalLevel=r}};u(I,"l",null);let k=I;const z=k.p(),H=new WeakMap;function G(c,r){H.set(c,r)}function D(c){return H.get(c)}class N{constructor(r,i,s=i,n=1,o={}){u(this,"C");u(this,"$");u(this,"u");u(this,"R",null);u(this,"S");u(this,"M");u(this,"F",[]);u(this,"G");u(this,"D",null);u(this,"P",[]);this.C=i,this.$=s,this.u={filter:"nearest",wrap:"clamp",format:"rgba",type:"unsigned_byte",...o},this.S=r,this.G=Math.min(Math.max(1,n),8);const a=r.getParameter(r.MAX_DRAW_BUFFERS),h=r.getParameter(r.MAX_COLOR_ATTACHMENTS);this.G=Math.min(this.G,a,h),this.M=r.createFramebuffer(),this.A(),this.I(),this.P=Array(this.G).fill(null)}A(){const r=this.S,i=this.u.filter==="linear"?r.LINEAR:r.NEAREST,s=this.u.wrap==="repeat"?r.REPEAT:r.CLAMP_TO_EDGE,n=this.u.type==="float"?r.FLOAT:r.UNSIGNED_BYTE;for(let o=0;o<this.G;o++){const a=r.createTexture();r.bindTexture(r.TEXTURE_2D,a),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,i),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,i),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,s),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,s),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,this.C,this.$,0,r.RGBA,n,null),this.F.push(a)}r.bindTexture(r.TEXTURE_2D,null)}I(){const r=this.S;if(r.bindFramebuffer(r.FRAMEBUFFER,this.M),this.G===1)r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,this.F[0],0);else{const s=[];for(let n=0;n<this.G;n++){const o=r.COLOR_ATTACHMENT0+n;r.framebufferTexture2D(r.FRAMEBUFFER,o,r.TEXTURE_2D,this.F[n],0),s.push(o)}r.drawBuffers(s)}const i=r.checkFramebufferStatus(r.FRAMEBUFFER);i!==r.FRAMEBUFFER_COMPLETE&&console.error("GLFramebuffer is not complete:",i),r.bindFramebuffer(r.FRAMEBUFFER,null)}L(r){const i=this.S;i.bindTexture(i.TEXTURE_2D,this.F[0]),i.pixelStorei(i.UNPACK_FLIP_Y_WEBGL,1),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,i.RGBA,i.UNSIGNED_BYTE,r),i.bindTexture(i.TEXTURE_2D,null)}W(r,i){this.C=r,this.$=i,this.R=null,this.P=Array(this.G).fill(null);const s=this.S,n=this.u.type==="float"?s.FLOAT:s.UNSIGNED_BYTE;for(const o of this.F)s.bindTexture(s.TEXTURE_2D,o),s.texImage2D(s.TEXTURE_2D,0,s.RGBA,this.C,this.$,0,s.RGBA,n,null);s.bindTexture(s.TEXTURE_2D,null)}k(r){const i=this.S;if(r<0||r>=this.G)throw Error(`GLFramebuffer: attachment index ${r} out of range (count=${this.G})`);const s=this.P[r];if(s)return s;const n=this.C,o=this.$,a=new Uint8Array(n*o*4),h=i.getParameter(i.READ_FRAMEBUFFER_BINDING);i.bindFramebuffer(i.READ_FRAMEBUFFER,this.M),i.readBuffer(i.COLOR_ATTACHMENT0+r),i.readPixels(0,0,n,o,i.RGBA,i.UNSIGNED_BYTE,a),i.bindFramebuffer(i.READ_FRAMEBUFFER,h);const l=4*n,f=new Uint8Array(a.length);for(let g=0;g<o;g++){const d=(o-1-g)*l,p=g*l;f.set(a.subarray(d,d+l),p)}return this.P[r]=f,f}U(){for(let r=0;r<this.G;r++)this.k(r)}V(){const r=this.S;this.D={framebuffer:r.getParameter(r.FRAMEBUFFER_BINDING),viewport:r.getParameter(r.VIEWPORT)},r.bindFramebuffer(r.FRAMEBUFFER,this.M),this.P=Array(this.G).fill(null);for(let i=0;i<this.G;i++)r.clearBufferfv(r.COLOR,i,new Float32Array([0,0,0,0]));r.viewport(0,0,this.C,this.$),G(r,[0,0,this.C,this.$])}O(){if(!this.D)return;const r=this.S;r.bindFramebuffer(r.FRAMEBUFFER,this.D.framebuffer),r.viewport(...this.D.viewport),G(r,this.D.viewport),this.D=null}j(){const r=this.S;this.M&&r.deleteFramebuffer(this.M);for(const i of this.F)r.deleteTexture(i);this.F=[],this.P=[]}get width(){return this.C}get height(){return this.$}get pixels(){return this.R}get options(){return{...this.u}}get framebuffer(){return this.M}get texture(){return this.F[0]}get textures(){return[...this.F]}get attachmentCount(){return this.G}getAttachmentPixels(r){return this.P[r]??null}}class V{constructor(r,i,s){u(this,"S");u(this,"H");u(this,"X",new Map);u(this,"Y",new Map);u(this,"q",0);u(this,"Z");this.S=r,this.H=this.N(i,s),this.Z=r.getParameter(r.MAX_TEXTURE_IMAGE_UNITS),this.J()}J(){const r=this.S.getProgramParameter(this.H,this.S.ACTIVE_UNIFORMS);for(let i=0;i<r;i++){const s=this.S.getActiveUniform(this.H,i);if(s){const n=this.S.getUniformLocation(this.H,s.name);n&&(this.X.set(s.name,n),this.Y.set(s.name,s.type))}}}N(r,i){const s=this.K(this.S.VERTEX_SHADER,r),n=this.K(this.S.FRAGMENT_SHADER,i),o=this.S.createProgram();if(this.S.attachShader(o,s),this.S.attachShader(o,n),this.S.linkProgram(o),!this.S.getProgramParameter(o,this.S.LINK_STATUS)){const a=this.S.getProgramInfoLog(o);throw Error("Shader program link error: "+a)}return this.S.deleteShader(s),this.S.deleteShader(n),o}K(r,i){const s=this.S.createShader(r);if(this.S.shaderSource(s,i),this.S.compileShader(s),!this.S.getShaderParameter(s,this.S.COMPILE_STATUS)){const n=this.S.getShaderInfoLog(s);throw this.S.deleteShader(s),Error("Shader compilation error: "+n)}return s}tt(){this.S.useProgram(this.H),this.et()}et(){this.q=0}st(r){for(const[i,s]of Object.entries(r))this.it(i,s)}rt(r){return this.X.has(r)}it(r,i){if(this.S.getParameter(this.S.CURRENT_PROGRAM)!==this.H)return void console.warn(`Attempting to set uniform '${r}' on shader that is not currently bound`);const s=this.X.get(r);if(s)if(typeof i=="number")this.S.uniform1f(s,i);else if(typeof i=="boolean")this.S.uniform1i(s,i?1:0);else if(Array.isArray(i))switch(i.length){case 2:this.S.uniform2f(s,i[0],i[1]);break;case 3:this.S.uniform3f(s,i[0],i[1],i[2]);break;case 4:this.S.uniform4f(s,i[0],i[1],i[2],i[3]);break;default:console.warn(`Unsupported array length ${i.length} for uniform '${r}'`)}else if(i instanceof WebGLTexture){const n=this.nt();this.S.uniform1i(s,n),this.S.activeTexture(this.S.TEXTURE0+n),this.S.bindTexture(this.S.TEXTURE_2D,i)}else if(i instanceof N){const n=this.nt();this.S.uniform1i(s,n),this.S.activeTexture(this.S.TEXTURE0+n),this.S.bindTexture(this.S.TEXTURE_2D,i.texture)}else if(typeof i=="object"&&"texture"in i){const n=this.nt();this.S.uniform1i(s,n),this.S.activeTexture(this.S.TEXTURE0+n),this.S.bindTexture(this.S.TEXTURE_2D,i.texture)}else console.warn(`Unsupported uniform type for '${r}':`,typeof i)}nt(){return this.q>=this.Z&&console.warn(`Exceeded maximum texture units (${this.Z}). Texture may not render correctly.`),this.q++}get ot(){return this.H}j(){this.S.deleteProgram(this.H)}}class st{constructor(){u(this,"ht",1);u(this,"ct",0);u(this,"lt",0);u(this,"ut",0);u(this,"ft",[0,0,0]);u(this,"dt",[1,1,1,1]);u(this,"_t",[0,0,0,1]);u(this,"gt",!1);u(this,"vt",!1);u(this,"xt",!1);u(this,"Ct",[0,0]);u(this,"yt",[0,0,0,1]);u(this,"wt",[])}$t(){this.wt.push({lineWeight:this.ht,rotationX:this.ct,rotationY:this.lt,rotationZ:this.ut,charRotation:[...this.Ct],flipHorizontally:this.gt,flipVertically:this.vt,invert:this.xt,character:[...this.ft],charColor:[...this.dt],cellColor:[...this._t]})}bt(){const r=this.wt.pop();r?(this.ht=r.lineWeight,this.ct=r.rotationX,this.lt=r.rotationY,this.ut=r.rotationZ,this.Ct=r.charRotation,this.gt=r.flipHorizontally,this.vt=r.flipVertically,this.xt=r.invert,this.ft=r.character,this.dt=r.charColor,this._t=r.cellColor):console.warn("pop() called without matching push()")}zt(){this.wt=[],this.ct=0,this.lt=0,this.ut=0}Rt(r){r.lineWeight=this.ht,r.rotationX=this.ct,r.rotationY=this.lt,r.rotationZ=this.ut,r.character[0]=this.ft[0],r.character[1]=this.ft[1],r.character[2]=this.ft[2],r.charColor[0]=this.dt[0],r.charColor[1]=this.dt[1],r.charColor[2]=this.dt[2],r.charColor[3]=this.dt[3],r.bgColor[0]=this._t[0],r.bgColor[1]=this._t[1],r.bgColor[2]=this._t[2],r.bgColor[3]=this._t[3],r.flipHorizontally=this.gt,r.flipVertically=this.vt,r.invert=this.xt,r.charRotation[0]=this.Ct[0],r.charRotation[1]=this.Ct[1]}get lineWeight(){return this.ht}get canvasBackgroundColor(){return this.yt}Tt(r){this.ht=Math.abs(r)}St(r){this.ct=r}Mt(r){this.lt=r}Ft(r){this.ut=r}Gt(r){this.ft=r}Dt(r,i,s,n=255){this.dt=[r/255,i/255,s/255,n/255]}Pt(r,i,s,n=255){this._t=[r/255,i/255,s/255,n/255]}At(r){this.gt=r}Bt(r){this.vt=r}It(r){this.xt=r}Lt(r){const i=255*r/360,s=Math.floor(i)/255,n=Math.round(i-Math.floor(i));this.Ct=[s,n]}Et(r,i,s,n){this.yt=[r/255,i/255,s/255,n/255]}}var j=`#version 300 es
8
- in vec2 a_position;in vec2 a_texCoord;in vec2 a_instancePosition;in vec2 a_instanceSize;in vec3 a_instanceCharacter;in vec4 a_instancePrimaryColor;in vec4 a_instanceSecondaryColor;in vec2 a_instanceRotation;in vec3 a_instanceTransform;in vec3 a_instanceGlobalRotation;in vec2 a_instanceRotationCenter;in vec2 a_instanceBezierCP1;in vec2 a_instanceBezierCP2;in vec2 a_instanceBezierStart;in vec2 a_instanceBezierEnd;in vec2 a_instanceArcAngles;uniform float u_aspectRatio;uniform vec2 u_viewportSize;out vec2 v_uv;out vec3 v_character;out vec4 v_primaryColor;out vec4 v_secondaryColor;out vec2 v_rotation;out vec3 v_transform;mat3 rotateX(float a){float s=sin(a),c=cos(a);return mat3(1,0,0,0,c,-s,0,s,c);}mat3 rotateY(float a){float s=sin(a),c=cos(a);return mat3(c,0,s,0,1,0,-s,0,c);}mat3 rotateZ(float a){float s=sin(a),c=cos(a);return mat3(c,-s,0,s,c,0,0,0,1);}vec2 evaluateBezier(float t,vec2 p0,vec2 p1,vec2 p2,vec2 p3){float u=1.-t,u2=u*u,t2=t*t;return u2*u*p0+3.*u2*t*p1+3.*u*t2*p2+t2*t*p3;}vec2 evaluateBezierDerivative(float t,vec2 p0,vec2 p1,vec2 p2,vec2 p3){float u=1.-t,u2=u*u,t2=t*t;return-3.*u2*p0+3.*u2*p1-6.*u*t*p1+6.*u*t*p2-3.*t2*p2+3.*t2*p3;}void main(){v_uv=a_texCoord;v_character=a_instanceCharacter;v_primaryColor=a_instancePrimaryColor;v_secondaryColor=a_instanceSecondaryColor;v_rotation=a_instanceRotation;v_transform=a_instanceTransform;vec2 worldPosition;bool isBezier=length(a_instanceBezierCP1)+length(a_instanceBezierCP2)+length(a_instanceBezierStart)+length(a_instanceBezierEnd)>0.;bool isArc=a_instanceArcAngles.x!=0.||a_instanceArcAngles.y!=0.;if(isBezier){float t=a_position.x;vec2 curvePoint=evaluateBezier(t,a_instanceBezierStart,a_instanceBezierCP1,a_instanceBezierCP2,a_instanceBezierEnd);vec2 tangent=evaluateBezierDerivative(t,a_instanceBezierStart,a_instanceBezierCP1,a_instanceBezierCP2,a_instanceBezierEnd);float tLen=length(tangent);tangent=tLen>0.?tangent/tLen:vec2(1,0);worldPosition=curvePoint+vec2(-tangent.y,tangent.x)*a_position.y*a_instanceSize.y;}else if(isArc){float s=a_instanceArcAngles.x,e=a_instanceArcAngles.y;s=mod(s,6.28318530718);if(s<0.)s+=6.28318530718;e=mod(e,6.28318530718);if(e<0.)e+=6.28318530718;float d=s-e;if(d<=0.)d+=6.28318530718;float angle=s-a_position.x*d;vec2 local=vec2(cos(angle),sin(angle))*a_position.y;worldPosition=local*a_instanceSize*.5+a_instanceSize*.5+a_instancePosition;}else{worldPosition=a_position*a_instanceSize+a_instancePosition;}vec2 ndc=(worldPosition/u_viewportSize)*2.-1.;ndc.y=-ndc.y;if(length(a_instanceGlobalRotation)>0.){vec3 pos3D=vec3(ndc-a_instanceRotationCenter,0);pos3D.x*=u_aspectRatio;if(a_instanceGlobalRotation.x!=0.)pos3D=rotateX(-a_instanceGlobalRotation.x)*pos3D;if(a_instanceGlobalRotation.y!=0.)pos3D=rotateY(-a_instanceGlobalRotation.y)*pos3D;if(a_instanceGlobalRotation.z!=0.)pos3D=rotateZ(-a_instanceGlobalRotation.z)*pos3D;pos3D.x/=u_aspectRatio;ndc=pos3D.xy+a_instanceRotationCenter;}gl_Position=vec4(ndc,0,1);}`,C=(c=>(c.RECTANGLE="rectangle",c.LINE="line",c.ELLIPSE="ellipse",c.ARC="arc",c.TRIANGLE="triangle",c.BEZIER_CURVE="bezier_curve",c.CUSTOM="custom",c))(C||{});class nt{constructor(r){u(this,"S");u(this,"Wt",new Map);this.S=r}kt(r,i,s,n){const o=this.S;let a=this.Wt.get(r);a||(a=new Map,this.Wt.set(r,a));let h=a.get(i)||null;if(!h){h=o.createVertexArray(),a.set(i,h),o.bindVertexArray(h),o.bindBuffer(o.ARRAY_BUFFER,n);const l=o.getAttribLocation(r,"a_position");l!==-1&&(o.enableVertexAttribArray(l),o.vertexAttribPointer(l,s.attributes.position.size,o.FLOAT,!1,s.stride,s.attributes.position.offset),o.vertexAttribDivisor(l,0));const f=o.getAttribLocation(r,"a_texCoord");f!==-1&&(o.enableVertexAttribArray(f),o.vertexAttribPointer(f,s.attributes.texCoord.size,o.FLOAT,!1,s.stride,s.attributes.texCoord.offset),o.vertexAttribDivisor(f,0))}o.bindVertexArray(h)}Ut(){this.S.bindVertexArray(null)}j(){const r=this.S;for(const[,i]of this.Wt)for(const[,s]of i)s&&r.deleteVertexArray(s);this.Wt.clear()}}class ot{constructor(r){u(this,"Vt");u(this,"S");u(this,"Ot",null);u(this,"jt",null);u(this,"Ht",null);this.S=r,this.Vt=new nt(r)}Xt(r,i,s){const{shader:n}=r,o=D(this.S)||this.S.getParameter(this.S.VIEWPORT),a={u_aspectRatio:o[2]/o[3],u_viewportSize:[o[2],o[3]]},h={};for(const[d,p]of Object.entries(a))n.rt(d)&&(h[d]=p);Object.keys(h).length>0&&n.st(h);const l=d=>{if(!d||!d.Yt())return;const p=d.unitGeometry,x=d.unitBuffer;try{this.Vt.kt(n.ot,d.type+"",p,x),d.batch.qt(n),d.batch.Zt(p.primitiveType,p.vertexCount)}finally{d.batch.Nt(n),this.Vt.Ut(),d.Qt()}};let f=null,g=null;for(const d of i){if(d.type===C.CUSTOM){g&&(l(g),f=null,g=null),this.Jt(r,d.params,d.state,s.get(C.RECTANGLE)||null);continue}f!==null&&d.type!==f&&(l(g),f=null,g=null);let p=g;p&&d.type===f||(p=s.get(d.type)||null,g=p,f=d.type),p&&p.Kt(d.params,d.state)}l(g)}Jt(r,i,s,n){if(!n)return;const{x:o,y:a,width:h,height:l,shader:f,uniforms:g}=i,d=this.S;f.tt(),n.Qt();const p=this.te(Math.max(1,Math.floor(h)),Math.max(1,Math.floor(l)));p.V(),f.tt(),g&&Object.keys(g).length&&f.st(g);{const R=D(d)||d.getParameter(d.VIEWPORT);f.rt("u_aspectRatio")&&f.it("u_aspectRatio",R[2]/R[3]),f.rt("u_viewportSize")&&f.it("u_viewportSize",[R[2],R[3]])}const x={...s,rotationX:0,rotationY:0,rotationZ:0};if(n.Kt({x:0,y:0,width:p.width,height:p.height},x),n.Yt()){const R=n.unitGeometry,B=n.unitBuffer;try{this.Vt.kt(f.ot,n.type+"",R,B),n.batch.qt(f),n.batch.Zt(R.primitiveType,R.vertexCount)}finally{n.batch.Nt(f),this.Vt.Ut(),n.Qt()}}p.O();const m=this.ee();m.tt(),m.st({u_src0:p.textures[0],u_src1:p.textures[1],u_src2:p.textures[2],u_src3:p.textures[3],u_src4:p.textures[4],u_srcSize:[p.width,p.height]});const y=D(d)||d.getParameter(d.VIEWPORT);m.rt("u_aspectRatio")&&m.it("u_aspectRatio",y[2]/y[3]),m.rt("u_viewportSize")&&m.it("u_viewportSize",[y[2],y[3]]);const w=Math.floor(o),E=Math.floor(a),T=Math.max(1,Math.floor(h)),A=Math.max(1,Math.floor(l));if(n.Kt({x:w,y:E,width:T,height:A},s),n.Yt()){const R=n.unitGeometry,B=n.unitBuffer;try{this.Vt.kt(m.ot,n.type+"",R,B),n.batch.qt(m),n.batch.Zt(R.primitiveType,R.vertexCount)}finally{n.batch.Nt(m),this.Vt.Ut(),n.Qt()}}r.shader.tt()}ee(){return this.Ot||(this.Ot=new V(this.S,j,`#version 300 es
9
- precision highp float;in vec2 v_uv;uniform sampler2D u_src0;uniform sampler2D u_src1;uniform sampler2D u_src2;uniform sampler2D u_src3;uniform sampler2D u_src4;uniform vec2 u_srcSize;layout(location=0)out vec4 o_character;layout(location=1)out vec4 o_primaryColor;layout(location=2)out vec4 o_secondaryColor;layout(location=3)out vec4 o_rotation;layout(location=4)out vec4 o_transform;void main(){vec2 uvTex=v_uv*u_srcSize;vec2 uvQ=(floor(uvTex)+0.5f)/u_srcSize;o_character=texture(u_src0,uvQ);o_primaryColor=texture(u_src1,uvQ);o_secondaryColor=texture(u_src2,uvQ);o_rotation=texture(u_src3,uvQ);o_transform=texture(u_src4,uvQ);}`)),this.Ot}te(r,i){return this.jt&&this.Ht&&this.Ht.w===r&&this.Ht.h===i||(this.jt&&this.jt.j(),this.jt=new N(this.S,r,i,5),this.Ht={w:r,h:i}),this.jt}}class at{constructor(){u(this,"se",[]);u(this,"ie",1);u(this,"re",0)}ne(r){if(this.re>=this.se.length){const s={id:this.ie++,type:r,params:{},state:{lineWeight:1,rotationX:0,rotationY:0,rotationZ:0,character:[0,0,0],charColor:[1,1,1,1],bgColor:[0,0,0,1],flipHorizontally:!1,flipVertically:!1,invert:!1,charRotation:[0,0]}};this.se.push(s)}const i=this.se[this.re];switch(i.id=this.ie++,i.type=r,r){case C.RECTANGLE:case C.ELLIPSE:i.params&&"width"in i.params||(i.params={x:0,y:0,width:0,height:0});break;case C.CUSTOM:i.params&&"shader"in i.params||(i.params={x:0,y:0,width:0,height:0,shader:void 0,uniforms:{}});break;case C.ARC:i.params&&"start"in i.params||(i.params={x:0,y:0,width:0,height:0,start:0,stop:0});break;case C.LINE:i.params&&"x2"in i.params||(i.params={x1:0,y1:0,x2:0,y2:0,thickness:void 0});break;case C.TRIANGLE:i.params&&"x3"in i.params||(i.params={x1:0,y1:0,x2:0,y2:0,x3:0,y3:0});break;case C.BEZIER_CURVE:i.params&&"cp2y"in i.params||(i.params={x1:0,y1:0,cp1x:0,cp1y:0,cp2x:0,cp2y:0,x2:0,y2:0,thickness:void 0});break;default:i.params||(i.params={})}return this.re++,i}oe(r,i,s,n,o){const a=this.ne(C.RECTANGLE);return a.params.x=r,a.params.y=i,a.params.width=s,a.params.height=n,o.Rt(a.state),a.id}ae(r,i,s,n,o,a,h){const l=this.ne(C.CUSTOM);return l.params.x=r,l.params.y=i,l.params.width=s,l.params.height=n,l.params.shader=o,l.params.uniforms=a,h.Rt(l.state),l.id}he(r,i,s,n,o,a){const h=this.ne(C.LINE);return h.params.x1=r,h.params.y1=i,h.params.x2=s,h.params.y2=n,h.params.thickness=o,a.Rt(h.state),h.id}ce(r,i,s,n,o){const a=this.ne(C.ELLIPSE);return a.params.x=r,a.params.y=i,a.params.width=s,a.params.height=n,o.Rt(a.state),a.id}le(r,i,s,n,o,a,h){const l=this.ne(C.ARC);return l.params.x=r,l.params.y=i,l.params.width=s,l.params.height=n,l.params.start=o,l.params.stop=a,h.Rt(l.state),l.id}ue(r,i,s,n,o,a,h){const l=this.ne(C.TRIANGLE);return l.params.x1=r,l.params.y1=i,l.params.x2=s,l.params.y2=n,l.params.x3=o,l.params.y3=a,h.Rt(l.state),l.id}fe(r,i,s,n,o,a,h,l,f,g){const d=this.ne(C.BEZIER_CURVE);return d.params.x1=r,d.params.y1=i,d.params.cp1x=s,d.params.cp1y=n,d.params.cp2x=o,d.params.cp2y=a,d.params.x2=h,d.params.y2=l,d.params.thickness=f,g.Rt(d.state),d.id}get length(){return this.re}get isEmpty(){return this.re===0}de(){this.re=0}[Symbol.iterator](){let r=0;const i=this.re,s=this.se;return{next:()=>r<i?{value:s[r++],done:!1}:{value:void 0,done:!0}}}}const U=class U{static pe(r,i,s=0){var a,h,l,f,g,d,p,x,m,y;const n=i||new Float32Array(U.FLOATS_PER_INSTANCE);let o=s;return n[o++]=r.position[0],n[o++]=r.position[1],n[o++]=r.size[0],n[o++]=r.size[1],n[o++]=r.character[0],n[o++]=r.character[1],n[o++]=r.character[2],n[o++]=r.primaryColor[0],n[o++]=r.primaryColor[1],n[o++]=r.primaryColor[2],n[o++]=r.primaryColor[3],n[o++]=r.secondaryColor[0],n[o++]=r.secondaryColor[1],n[o++]=r.secondaryColor[2],n[o++]=r.secondaryColor[3],n[o++]=r.rotation[0],n[o++]=r.rotation[1],n[o++]=r.transform[0],n[o++]=r.transform[1],n[o++]=r.transform[2],n[o++]=r.globalRotationX,n[o++]=r.globalRotationY,n[o++]=r.globalRotationZ,n[o++]=r.rotationCenter[0],n[o++]=r.rotationCenter[1],n[o++]=((a=r.arcAngles)==null?void 0:a[0])||0,n[o++]=((h=r.arcAngles)==null?void 0:h[1])||0,n[o++]=((l=r.bezierControlPoint1)==null?void 0:l[0])||0,n[o++]=((f=r.bezierControlPoint1)==null?void 0:f[1])||0,n[o++]=((g=r.bezierControlPoint2)==null?void 0:g[0])||0,n[o++]=((d=r.bezierControlPoint2)==null?void 0:d[1])||0,n[o++]=((p=r.bezierStartPoint)==null?void 0:p[0])||0,n[o++]=((x=r.bezierStartPoint)==null?void 0:x[1])||0,n[o++]=((m=r.bezierEndPoint)==null?void 0:m[0])||0,n[o++]=((y=r.bezierEndPoint)==null?void 0:y[1])||0,n}static _e(r){const i=r.length*U.FLOATS_PER_INSTANCE,s=new Float32Array(i);for(let n=0;n<r.length;n++){const o=n*U.FLOATS_PER_INSTANCE;U.pe(r[n],s,o)}return s}};u(U,"BYTES_PER_INSTANCE",140),u(U,"FLOATS_PER_INSTANCE",35);let P=U;const S=class S{};u(S,"STRIDE",P.BYTES_PER_INSTANCE),u(S,"ATTRIBUTES",{a_instancePosition:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:0,divisor:1},a_instanceSize:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:8,divisor:1},a_instanceCharacter:{location:-1,size:3,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:16,divisor:1},a_instancePrimaryColor:{location:-1,size:4,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:28,divisor:1},a_instanceSecondaryColor:{location:-1,size:4,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:44,divisor:1},a_instanceRotation:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:60,divisor:1},a_instanceTransform:{location:-1,size:3,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:68,divisor:1},a_instanceGlobalRotation:{location:-1,size:3,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:80,divisor:1},a_instanceRotationCenter:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:92,divisor:1},a_instanceArcAngles:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:100,divisor:1},a_instanceBezierCP1:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:108,divisor:1},a_instanceBezierCP2:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:116,divisor:1},a_instanceBezierStart:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:124,divisor:1},a_instanceBezierEnd:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:132,divisor:1}});let $=S;class ht{constructor(r,i=1e3,s=1.5){u(this,"S");u(this,"ge",[]);u(this,"me");u(this,"ve");u(this,"xe",null);u(this,"Ce",!0);u(this,"ye",0);u(this,"we",new Map);u(this,"$e",null);this.S=r,this.me=i,this.ve=s,this.be()}Kt(r){const i=this.ge.length;return this.ge.push(r),this.Ce=!0,i}get count(){return this.ge.length}get isEmpty(){return this.ge.length===0}clear(){this.ge.length=0,this.Ce=!0}ze(r){if(r<=this.me)return;const i=Math.ceil(r*this.ve);this.me=i,this.be()}be(){const r=this.S;this.xe&&r.deleteBuffer(this.xe),this.xe=r.createBuffer();const i=this.me*P.BYTES_PER_INSTANCE;r.bindBuffer(r.ARRAY_BUFFER,this.xe),r.bufferData(r.ARRAY_BUFFER,i,r.DYNAMIC_DRAW),r.bindBuffer(r.ARRAY_BUFFER,null),this.Ce=!0,this.ye=0}Re(){if(!this.Ce||this.ge.length===0)return;const r=this.S,i=this.ge.length;this.ze(i),(!this.$e||this.$e.length<i*P.FLOATS_PER_INSTANCE)&&(this.$e=new Float32Array(i*P.FLOATS_PER_INSTANCE));const s=P._e(this.ge);r.bindBuffer(r.ARRAY_BUFFER,this.xe),i<=this.ye?r.bufferSubData(r.ARRAY_BUFFER,0,s):r.bufferData(r.ARRAY_BUFFER,s,r.DYNAMIC_DRAW),r.bindBuffer(r.ARRAY_BUFFER,null),this.Ce=!1,this.ye=i}Te(r){let i=this.we.get(r);if(!i){i=new Map;const s=this.S;for(const n in $.ATTRIBUTES){const o=s.getAttribLocation(r,n);o!==-1&&i.set(n,o)}this.we.set(r,i)}return i}qt(r){if(!this.xe||this.ge.length===0)return;const i=this.S,s=r.ot;this.Re();const n=this.Te(s);i.bindBuffer(i.ARRAY_BUFFER,this.xe);for(const[o,a]of n){const h=$.ATTRIBUTES[o];h&&(i.enableVertexAttribArray(a),i.vertexAttribPointer(a,h.size,h.type,h.normalized,h.stride,h.offset),i.vertexAttribDivisor(a,h.divisor))}}Nt(r){const i=this.S,s=this.Te(r.ot);for(const[,n]of s)i.disableVertexAttribArray(n),i.vertexAttribDivisor(n,0)}Zt(r,i){this.ge.length!==0&&this.S.drawArraysInstanced(r,0,i,this.ge.length)}j(){const r=this.S;this.xe&&(r.deleteBuffer(this.xe),this.xe=null),this.ge.length=0,this.we.clear(),this.$e=null}}class M{constructor(r,i,s,n){u(this,"S");u(this,"Se");u(this,"Me");u(this,"Fe");u(this,"Ge",null);this.S=r,this.Se=i,this.Me=s,this.Fe=n;const o=this.S.createBuffer();if(!o)throw Error("Failed to create unit geometry buffer");this.S.bindBuffer(this.S.ARRAY_BUFFER,o),this.S.bufferData(this.S.ARRAY_BUFFER,this.Fe.vertices,this.S.STATIC_DRAW),this.S.bindBuffer(this.S.ARRAY_BUFFER,null),this.Ge=o}get type(){return this.Me}get unitGeometry(){return this.Fe}get unitBuffer(){return this.Ge}get batch(){return this.Se}Qt(){this.Se.clear()}Yt(){return!this.Se.isEmpty}j(){this.Se.j(),this.Ge&&(this.S.deleteBuffer(this.Ge),this.Ge=null)}De(r,i,s,n,o){const a=this.Pe(r,i,s,n,o.rotationX||0,o.rotationY||0,o.rotationZ||0);return{position:[r,i],size:[s,n],character:o.character||[0,0,0],primaryColor:o.charColor||[1,1,1,1],secondaryColor:o.bgColor||[0,0,0,1],rotation:o.charRotation||[0,0],transform:[o.invert?1:0,o.flipHorizontally?1:0,o.flipVertically?1:0],globalRotationX:a.radiansX,globalRotationY:a.radiansY,globalRotationZ:a.radiansZ,rotationCenter:[a.centerX,a.centerY]}}Ae(r,i){const s=D(this.S)||[0,0,this.S.canvas.width,this.S.canvas.height];return{nx:r/s[2]*2-1,ny:1-i/s[3]*2}}Be(r,i,s){const n=this.Ae(i,s);r.rotationCenter=[n.nx,n.ny]}Pe(r,i,s,n,o,a,h){const l=D(this.S)||[0,0,this.S.canvas.width,this.S.canvas.height],f=l[2],g=l[3];return{centerX:(r+s/2)/f*2-1,centerY:1-(i+n/2)/g*2,radiansX:-o*Math.PI/180,radiansY:-a*Math.PI/180,radiansZ:-h*Math.PI/180,aspectRatio:f/g}}}const ct={vertices:new Float32Array([0,0,0,0,1,0,1,0,0,1,0,1,0,1,0,1,1,0,1,0,1,1,1,1]),vertexCount:6,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class lt extends M{constructor(r,i){super(r,i,C.RECTANGLE,ct)}Kt(r,i){const s=this.De(r.x,r.y,r.width,r.height,i);return this.Se.Kt(s)}}const ut={vertices:new Float32Array([0,-.5,0,0,1,-.5,1,0,0,.5,0,1,0,.5,0,1,1,-.5,1,0,1,.5,1,1]),vertexCount:6,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class ft extends M{constructor(r,i){super(r,i,C.LINE,ut)}Kt(r,i){const s=r.x2-r.x1,n=r.y2-r.y1,o=Math.hypot(s,n),a=Math.atan2(n,s),h=r.thickness||i.lineWeight||1,l=r.x1+s/2,f=r.y1+n/2,g=l-o/2,d=f,p={character:i.character,charColor:i.charColor,bgColor:i.bgColor,charRotation:i.charRotation,flipHorizontally:i.flipHorizontally,flipVertically:i.flipVertically,invert:i.invert,rotationX:i.rotationX||0,rotationY:i.rotationY||0,rotationZ:(i.rotationZ||0)+180*a/Math.PI,lineWeight:h},x=this.De(g,d,o,h,p);return this.Be(x,l,f),this.Se.Kt(x)}}const dt={vertices:function(c=32){const r=[],i=2*Math.PI/c;for(let s=0;s<c;s++){const n=s*i,o=(s+1)%c*i,a=Math.cos(n),h=Math.sin(n),l=.5*(a+1),f=.5*(h+1),g=Math.cos(o),d=Math.sin(o),p=.5*(g+1),x=.5*(d+1);r.push(0,0,.5,.5,a,h,l,f,g,d,p,x)}return new Float32Array(r)}(32),vertexCount:96,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class pt extends M{constructor(r,i){super(r,i,C.ELLIPSE,dt)}Kt(r,i){const s=this.De(r.x,r.y,r.width,r.height,i);return this.Be(s,r.x,r.y),this.Se.Kt(s)}}let gt={vertices:function(c){const r=[];for(let i=0;i<c;i++){const s=i/c,n=(i+1)/c;r.push(s,0,s,0,s,1,s,1,n,1,n,1)}return new Float32Array(r)}(32),vertexCount:96,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class mt extends M{constructor(r,i){super(r,i,C.ARC,gt)}Kt(r,i){const s=r.x-r.width/2,n=r.y-r.height/2,o=r.start*Math.PI/180,a=r.stop*Math.PI/180,h=this.De(s,n,r.width,r.height,i);return this.Be(h,r.x,r.y),h.arcAngles=[o,a],this.Se.Kt(h)}}const xt={vertices:new Float32Array([0,0,0,0,1,0,1,0,.5,1,.5,1]),vertexCount:3,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class yt extends M{constructor(r,i){super(r,i,C.TRIANGLE,xt)}Kt(r,i){const s=Math.min(r.x1,r.x2,r.x3),n=Math.max(r.x1,r.x2,r.x3),o=Math.min(r.y1,r.y2,r.y3),a=n-s,h=Math.max(r.y1,r.y2,r.y3)-o,l=this.De(s,o,a,h,i),f=s+.5*a,g=o+h*(1/3);return this.Be(l,f,g),this.Se.Kt(l)}}function Q(c,r,i,s,n){const o=1-c,a=o*o,h=c*c;return a*o*r+3*a*c*i+3*o*h*s+h*c*n}const Ct={vertices:function(c=16){const r=[];for(let i=0;i<c;i++){const s=i/c,n=(i+1)/c;r.push(s,-.5,s,0),r.push(n,-.5,n,0),r.push(s,.5,s,1),r.push(s,.5,s,1),r.push(n,-.5,n,0),r.push(n,.5,n,1)}return new Float32Array(r)}(16),vertexCount:96,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class vt extends M{constructor(r,i){super(r,i,C.BEZIER_CURVE,Ct)}Kt(r,i){const s=i.lineWeight||1,n=Q(.5,r.x1,r.cp1x,r.cp2x,r.x2),o=Q(.5,r.y1,r.cp1y,r.cp2y,r.y2),a={character:i.character,charColor:i.charColor,bgColor:i.bgColor,charRotation:i.charRotation,flipHorizontally:i.flipHorizontally,flipVertically:i.flipVertically,invert:i.invert,rotationX:i.rotationX||0,rotationY:i.rotationY||0,rotationZ:i.rotationZ||0,lineWeight:s},h=this.De(0,0,1,s,a);return this.Be(h,n,o),h.bezierStartPoint=[r.x1,r.y1],h.bezierControlPoint1=[r.cp1x,r.cp1y],h.bezierControlPoint2=[r.cp2x,r.cp2y],h.bezierEndPoint=[r.x2,r.y2],this.Se.Kt(h)}}class St{constructor(r){u(this,"S");u(this,"Ie",null);u(this,"Le",null);u(this,"Ee",{});u(this,"We",null);u(this,"ke",new Map);u(this,"Ue");u(this,"Ve");u(this,"Oe");this.S=r,this.Oe=new st,this.Ue=new ot(r),this.Ve=new at,this.We=r.createBuffer(),G(this.S,[0,0,this.S.canvas.width,this.S.canvas.height])}je(r){let i=this.ke.get(r);if(i)return i;const s=new ht(this.S);return i=(0,{[C.RECTANGLE]:()=>new lt(this.S,s),[C.LINE]:()=>new ft(this.S,s),[C.ELLIPSE]:()=>new pt(this.S,s),[C.ARC]:()=>new mt(this.S,s),[C.TRIANGLE]:()=>new yt(this.S,s),[C.BEZIER_CURVE]:()=>new vt(this.S,s)}[r])(),this.ke.set(r,i),i}He(r){this.Ie!==r&&(this.Ie=r,r.tt())}Xe(r,i){return new V(this.S,r,i)}Ye(r){this.Le=r,r&&(this.Ee={})}qe(r,i){this.Ee[r]=i}Ze(r){Object.assign(this.Ee,r)}Ne(r){return new V(this.S,j,r)}Qe(r,i,s,n){var y;const o=this.S,a=o.canvas.width,h=o.canvas.height,l=r/a*2-1,f=(r+s)/a*2-1,g=1-i/h*2,d=1-(i+n)/h*2,p=new Float32Array([l,d,f,d,l,g,f,d,f,g,l,g]);o.bindBuffer(o.ARRAY_BUFFER,this.We),o.bufferData(o.ARRAY_BUFFER,p,o.DYNAMIC_DRAW);const x=((y=this.Ie)==null?void 0:y.ot)||o.getParameter(o.CURRENT_PROGRAM),m=x?o.getAttribLocation(x,"a_position"):-1;m!==-1&&(o.enableVertexAttribArray(m),o.vertexAttribPointer(m,2,o.FLOAT,!1,8,0)),o.drawArrays(o.TRIANGLES,0,6),m!==-1&&o.disableVertexAttribArray(m)}Je(r,i,s,n){this.Le?(this.Ve.ae(r,i,s,n,this.Le,{...this.Ee},this.Oe),this.Le=null,this.Ee={}):this.Ve.oe(r,i,s,n,this.Oe)}Ke(r,i,s,n){this.Ve.he(r,i,s,n,this.Oe.lineWeight,this.Oe)}ts(r,i,s,n){this.Ve.ce(r,i,s,n,this.Oe)}es(r,i,s,n,o,a){this.Ve.ue(r,i,s,n,o,a,this.Oe)}ss(r,i,s,n,o,a,h,l){const f=this.Oe.lineWeight;this.Ve.fe(r,i,s,n,o,a,h,l,f,this.Oe)}rs(r,i,s=1,n={}){return new N(this.S,r,i,s,n)}ns(r,i,s,n,o,a){this.Ve.le(r,i,s,n,o,a,this.Oe)}hs(r,i=r,s=r,n=255){this.state.Et(r,i,s,n),this.de(r/255,i/255,s/255,n/255)}de(r=0,i=0,s=0,n=0){this.S.clearColor(r,i,s,n),this.S.clear(this.S.COLOR_BUFFER_BIT)}cs(){this.S.viewport(0,0,this.S.canvas.width,this.S.canvas.height),G(this.S,[0,0,this.S.canvas.width,this.S.canvas.height])}get context(){return this.S}get state(){return this.Oe}ls(r){const i=r,s=D(this.S)??this.S.getParameter(this.S.VIEWPORT),n={shader:i,gl:this.S,viewport:s};this.He(i);const o=new Set;for(const a of this.Ve)a.type===C.CUSTOM?o.add(C.RECTANGLE):o.add(a.type);for(const a of o)a!==C.CUSTOM&&this.je(a);this.Ue.Xt(n,this.Ve,this.ke),this.Ve.de()}j(){this.S.deleteBuffer(this.We),this.Ve.de();for(const r of this.ke.values())r.j()}}const _={readShort:(c,r)=>(_.t.uint16[0]=c[r]<<8|c[r+1],_.t.int16[0]),readUshort:(c,r)=>c[r]<<8|c[r+1],readUshorts(c,r,i){const s=[];for(let n=0;n<i;n++)s.push(_.readUshort(c,r+2*n));return s},readUint(c,r){const i=_.t.uint8;return i[3]=c[r],i[2]=c[r+1],i[1]=c[r+2],i[0]=c[r+3],_.t.uint32[0]},readASCII(c,r,i){let s="";for(let n=0;n<i;n++)s+=String.fromCharCode(c[r+n]);return s},t:(()=>{const c=new ArrayBuffer(8);return{uint8:new Uint8Array(c),int16:new Int16Array(c),uint16:new Uint16Array(c),uint32:new Uint32Array(c)}})()},Rt={parseTab(c,r,i){const s={tables:[],ids:{},off:r};c=new Uint8Array(c.buffer,r,i),r=0;const n=_,o=n.readUshort,a=o(c,r+=2);r+=2;const h=[];for(let l=0;l<a;l++){const f=o(c,r),g=o(c,r+=2);r+=2;const d=n.readUint(c,r);r+=4;const p=`p${f}e${g}`;let x=h.indexOf(d);if(x===-1){let m;x=s.tables.length,h.push(d);const y=o(c,d);m=y===4?this.parse4(c,d):y===12?this.parse12(c,d):{format:y},s.tables.push(m)}s.ids[p]!=null&&console.warn("Multiple tables for one platform+encoding: "+p),s.ids[p]=x}return s},parse4(c,r){const i=_,s=i.readUshort,n=i.readUshorts,o=r,a=s(c,r+=2);r+=2;const h=s(c,r+=2)>>>1,l={format:4,searchRange:s(c,r+=2),entrySelector:0,rangeShift:0,endCount:[],startCount:[],idDelta:[],idRangeOffset:[],glyphIdArray:[]};r+=2,l.entrySelector=s(c,r),r+=2,l.rangeShift=s(c,r),r+=2,l.endCount=n(c,r,h),r+=2*h,r+=2,l.startCount=n(c,r,h),r+=2*h;for(let f=0;f<h;f++)l.idDelta.push(i.readShort(c,r)),r+=2;return l.idRangeOffset=n(c,r,h),r+=2*h,l.glyphIdArray=n(c,r,o+a-r>>1),l},parse12(c,r){const i=_.readUint;i(c,r+=4),i(c,r+=4);const s=i(c,r+=4);r+=4;const n=new Uint32Array(3*s);for(let o=0;o<3*s;o+=3)n[o]=i(c,r+(o<<2)),n[o+1]=i(c,r+(o<<2)+4),n[o+2]=i(c,r+(o<<2)+8);return{format:12,groups:n}}},bt={parseTab(c,r,i){const s=_;r+=18;const n=s.readUshort(c,r);r+=2,r+=16;const o=s.readShort(c,r);r+=2;const a=s.readShort(c,r);r+=2;const h=s.readShort(c,r);r+=2;const l=s.readShort(c,r);return r+=2,r+=6,{unitsPerEm:n,xMin:o,yMin:a,xMax:h,yMax:l,indexToLocFormat:s.readShort(c,r)}}},Et={parseTab(c,r,i){const s=_;r+=4;const n=["ascender","descender","lineGap","advanceWidthMax","minLeftSideBearing","minRightSideBearing","xMaxExtent","caretSlopeRise","caretSlopeRun","caretOffset","res0","res1","res2","res3","metricDataFormat","numberOfHMetrics"],o={};for(let a=0;a<n.length;a++){const h=n[a],l=h==="advanceWidthMax"||h==="numberOfHMetrics"?s.readUshort:s.readShort;o[h]=l(c,r+2*a)}return o}},_t={parseTab(c,r,i,s){const n=_,o=[],a=[],h=s.maxp.numGlyphs,l=s.hhea.numberOfHMetrics;let f=0,g=0,d=0;for(;d<l;)f=n.readUshort(c,r+(d<<2)),g=n.readShort(c,r+(d<<2)+2),o.push(f),a.push(g),d++;for(;d<h;)o.push(f),a.push(g),d++;return{aWidth:o,lsBearing:a}}},Z={cmap:Rt,head:bt,hhea:Et,maxp:{parseTab(c,r,i){const s=_;return s.readUint(c,r),r+=4,{numGlyphs:s.readUshort(c,r)}}},hmtx:_t,loca:{parseTab(c,r,i,s){const n=_,o=[],a=s.head.indexToLocFormat,h=s.maxp.numGlyphs+1;if(a===0)for(let l=0;l<h;l++)o.push(n.readUshort(c,r+(l<<1))<<1);else if(a===1)for(let l=0;l<h;l++)o.push(n.readUint(c,r+(l<<2)));return o}},glyf:{parseTab(c,r,i,s){const n=[],o=s.maxp.numGlyphs;for(let a=0;a<o;a++)n.push(null);return n},us(c,r){const i=_,s=c.fs,n=c.loca;if(n[r]===n[r+1])return null;const o=F.findTable(s,"glyf",c.ds);if(!o)return null;let a=o[0]+n[r];const h={};if(h.noc=i.readShort(s,a),a+=2,h.xMin=i.readShort(s,a),a+=2,h.yMin=i.readShort(s,a),a+=2,h.xMax=i.readShort(s,a),a+=2,h.yMax=i.readShort(s,a),a+=2,h.xMin>=h.xMax||h.yMin>=h.yMax)return null;if(h.noc>0){h.endPts=[];for(let p=0;p<h.noc;p++)h.endPts.push(i.readUshort(s,a)),a+=2;const l=i.readUshort(s,a);if(a+=2,s.length-a<l)return null;a+=l;const f=h.endPts[h.noc-1]+1;h.flags=[];for(let p=0;p<f;p++){const x=s[a];if(a++,h.flags.push(x),8&x){const m=s[a];a++;for(let y=0;y<m;y++)h.flags.push(x),p++}}h.xs=[];for(let p=0;p<f;p++){const x=h.flags[p],m=!!(16&x);2&x?(h.xs.push(m?s[a]:-s[a]),a++):m?h.xs.push(0):(h.xs.push(i.readShort(s,a)),a+=2)}h.ys=[];for(let p=0;p<f;p++){const x=h.flags[p],m=!!(32&x);4&x?(h.ys.push(m?s[a]:-s[a]),a++):m?h.ys.push(0):(h.ys.push(i.readShort(s,a)),a+=2)}let g=0,d=0;for(let p=0;p<f;p++)g+=h.xs[p],d+=h.ys[p],h.xs[p]=g,h.ys[p]=d}else h.parts=[],h.endPts=[],h.flags=[],h.xs=[],h.ys=[];return h}}},F={parse:c=>[((r,i,s,n)=>{const o=Z,a={fs:r,ps:i,ds:s};for(const h in o){const l=h,f=F.findTable(r,l,s);if(f){const[g,d]=f;let p=n[g];p==null&&(p=o[l].parseTab(r,g,d,a),n[g]=p),a[l]=p}}return a})(new Uint8Array(c),0,0,{})],findTable(c,r,i){const s=_,n=s.readUshort(c,i+4);let o=i+12;for(let a=0;a<n;a++){const h=s.readASCII(c,o,4);s.readUint(c,o+4);const l=s.readUint(c,o+8),f=s.readUint(c,o+12);if(h===r)return[l,f];o+=16}return null},T:Z,B:_};class O{constructor(){u(this,"_s",new Map);u(this,"gs",new Map)}vs(r,i){const s=`${this.Cs(r)}_${i}`;if(this._s.has(s))return this._s.get(s);const n=r.cmap;if(!n||!n.tables)return this._s.set(s,0),0;let o=0;for(const a of n.tables)if(a.format===4?o=this.ws(i,a):a.format===12&&(o=this.$s(i,a)),o>0)break;return this._s.set(s,o),o}bs(r,i){const s=i.codePointAt(0);return s===void 0?0:this.vs(r,s)}zs(r,i){const s=r.hmtx;return s&&s.aWidth&&s.aWidth.length!==0?i<s.aWidth.length?s.aWidth[i]:s.aWidth[s.aWidth.length-1]:0}Rs(r,i){const s=i/r.head.unitsPerEm,n=r.hhea.ascender*s,o=r.hhea.descender*s,a=r.hhea.lineGap*s;return{ascender:n,descender:o,lineGap:a,lineHeight:n-o+a,unitsPerEm:r.head.unitsPerEm,scale:s}}Ts(){this._s.clear(),this.gs.clear()}Cs(r){return`${r.ds}_${r.fs.length}`}ws(r,i){const s=i.endCount.length;let n=-1;for(let o=0;o<s;o++)if(r<=i.endCount[o]){n=o;break}if(n===-1||r<i.startCount[n])return 0;if(i.idRangeOffset[n]===0)return r+i.idDelta[n]&65535;{const o=i.idRangeOffset[n]/2+(r-i.startCount[n])-(s-n);if(o>=0&&o<i.glyphIdArray.length){const a=i.glyphIdArray[o];return a===0?0:a+i.idDelta[n]&65535}}return 0}$s(r,i){const s=i.groups.length/3;for(let n=0;n<s;n++){const o=i.groups[3*n],a=i.groups[3*n+1],h=i.groups[3*n+2];if(r>=o&&r<=a)return h+(r-o)}return 0}}class wt{constructor(r){u(this,"Ss");this.Ss=r}Ms(r){var s;const i=[];return(s=r.cmap)!=null&&s.tables?(r.cmap.tables.forEach(n=>{if(n.format===4){const o=this.Fs(n);i.push(...o)}else if(n.format===12){const o=this.Gs(n);i.push(...o)}}),[...new Set(i)]):[]}Ds(r,i){return this.Ss.bs(r,i)>0}Ps(r,i){for(const s of i)if(!this.Ds(r,s))return!1;return!0}As(r,i){return i.filter(s=>this.Ds(r,s))}Bs(r){return r.filter(i=>this.Is(i))}Fs(r){const i=[];if(!(r.startCount&&r.endCount&&r.idRangeOffset&&r.idDelta))return i;for(let s=0;s<r.startCount.length;s++){const n=r.startCount[s],o=r.endCount[s];if(n!==65535||o!==65535){for(let a=n;a<=o;a++)if(this.Ls(r,a,s)>0)try{const h=String.fromCodePoint(a);i.push(h)}catch{}}}return i}Gs(r){const i=[];if(!r.groups)return i;for(let s=0;s<r.groups.length;s+=3){const n=r.groups[s],o=r.groups[s+1],a=r.groups[s+2];for(let h=n;h<=o;h++)if(a+(h-n)>0)try{const l=String.fromCodePoint(h);i.push(l)}catch{}}return i}Ls(r,i,s){if(r.idRangeOffset[s]===0)return i+r.idDelta[s]&65535;{const n=r.idRangeOffset[s]/2+(i-r.startCount[s])-(r.startCount.length-s);if(n>=0&&r.glyphIdArray&&n<r.glyphIdArray.length){const o=r.glyphIdArray[n];if(o!==0)return o+r.idDelta[s]&65535}}return 0}Is(r){const i=r.codePointAt(0)||0;return!(i>=0&&i<=31&&i!==9&&i!==10&&i!==13||i>=127&&i<=159)}}class Tt{constructor(){u(this,"Es");const r=new O;this.Es=new wt(r)}extractCharacters(r){return this.Es.Ms(r)}filterProblematicCharacters(r){return this.Es.Bs(r)}characterExists(r,i){return this.Es.Ds(r,i)}allCharactersExist(r,i){return this.Es.Ps(r,i)}}class At{constructor(r){u(this,"Ws");u(this,"ks");u(this,"Us");u(this,"Vs");this.Us=r,this.Vs=new O,this.Ws=document.createElement("canvas"),this.ks=this.Ws.getContext("2d",{willReadFrequently:!0,alpha:!1})}createTextureAtlas(r,i,s,n){const o=r.length,a=Math.ceil(Math.sqrt(o)),h=Math.ceil(o/a),l=i.width*a,f=i.height*h,g=typeof n=="object"?n:null;this.Os(l,f),this.js(r,i,a,s,g);const d=this.Us.rs(l,f,1,{filter:"nearest"});return d.L(this.Ws),{framebuffer:d,columns:a,rows:h}}Os(r,i){this.Ws.width=r,this.Ws.height=i,this.Ws.style.width=r+"px",this.Ws.style.height=r+"px",this.ks.imageSmoothingEnabled=!1,this.Ws.style.imageRendering="pixelated",this.ks.fillStyle="black",this.ks.fillRect(0,0,r,i),this.ks.textBaseline="top",this.ks.textAlign="left",this.ks.fillStyle="white"}js(r,i,s,n,o){const a=n/o.head.unitsPerEm;for(let h=0;h<r.length;h++){const l=h%s,f=Math.floor(h/s),g=r[h].character,d=this.Hs(o,g);if(!d)continue;const p=g.codePointAt(0)||0,x=this.Vs.vs(o,p),m=this.Xs(o,x)*a,y=l*i.width,w=f*i.height,E=y+.5*i.width,T=w+.5*i.height,A=Math.round(E-.5*i.width),R=Math.round(T-.5*n),B=A+.5*(i.width-m),Jt=R+o.hhea.ascender*a;this.Ys(d,B,Jt,a)}}Hs(r,i){const s=i.codePointAt(0)||0,n=this.Vs.vs(r,s);if(n===0)return null;if(r.glyf&&r.glyf[n]!==null)return r.glyf[n];if(F&&F.T&&F.T.glyf){const o=F.T.glyf.us(r,n);return r.glyf&&o&&(r.glyf[n]=o),o}return null}Xs(r,i){const s=r.hmtx;return s&&s.aWidth?i<s.aWidth.length?s.aWidth[i]:s.aWidth[s.aWidth.length-1]:0}Ys(r,i,s,n){if(!r||!r.xs||r.noc===0)return;const{xs:o,ys:a,endPts:h,flags:l}=r;if(!(o&&a&&h&&l))return;this.ks.beginPath();let f=0;for(let g=0;g<h.length;g++){const d=h[g];if(!(d<f)){if(d>=f){const p=i+o[f]*n,x=s-a[f]*n;this.ks.moveTo(p,x);let m=f+1;for(;m<=d;)if(1&l[m]){const y=i+o[m]*n,w=s-a[m]*n;this.ks.lineTo(y,w),m++}else{const y=i+o[m]*n,w=s-a[m]*n;let E=m+1>d?f:m+1;if(1&l[E]){const T=i+o[E]*n,A=s-a[E]*n;this.ks.quadraticCurveTo(y,w,T,A),m=E+1}else{const T=(y+(i+o[E]*n))/2,A=(w+(s-a[E]*n))/2;this.ks.quadraticCurveTo(y,w,T,A),m=E}}this.ks.closePath()}f=d+1}}this.ks.fill()}}class Ft{constructor(){u(this,"Ss");this.Ss=new O}calculateMaxGlyphDimensions(r,i,s){let n=0;const o=this.Ss.Rs(s,i),a=o.lineHeight;for(const h of r){const l=this.Ss.bs(s,h);if(l===0)continue;const f=this.Ss.zs(s,l)*o.scale;n=Math.max(n,f)}return{width:Math.ceil(n),height:Math.ceil(a)}}getCharacterAdvanceWidth(r,i,s){const n=this.Ss.Rs(s,i),o=this.Ss.bs(s,r);return this.Ss.zs(s,o)*n.scale}getFontMetrics(r,i){return this.Ss.Rs(i,r)}Ts(){this.Ss.Ts()}}class Ut{constructor(){u(this,"Vs");this.Vs=new O}createCharacterObjects(r,i){return r.map((s,n)=>{const o=s.codePointAt(0)||0,a=this.qs(n);let h=0;if(i.hmtx&&i.hmtx.aWidth){const l=this.Vs.vs(i,o);l>0&&i.hmtx.aWidth[l]!==void 0&&(h=i.hmtx.aWidth[l])}return{character:s,unicode:o,color:a,advanceWidth:h}})}qs(r){return[r%256/255,Math.floor(r/256)%256/255,Math.floor(r/65536)%256/255]}Zs(r,i){if(!z.m(typeof r=="string","Character must be a string.",{method:"getCharacterColor",providedValue:r}))return[0,0,0];const s=i.find(n=>n.character===r);return s?s.color:[0,0,0]}Ns(r,i){return z.m(typeof r=="string"&&r.length>0,"Characters must be a string with at least one character.",{method:"getCharacterColors",providedValue:r})?Array.from(r).map(s=>this.Zs(s,i)||[0,0,0]):[[0,0,0]]}}class K{constructor(r,i=16){u(this,"Qs");u(this,"Js",[]);u(this,"Ks");u(this,"ti",16);u(this,"ei",0);u(this,"si",0);u(this,"ii",{width:0,height:0});u(this,"ri");u(this,"ni");u(this,"oi");u(this,"ai");u(this,"hi");this.ti=i,this.ni=new Tt,this.oi=new At(r),this.ai=new Ft,this.hi=new Ut}async ci(r){let i;if(!r)throw new v("Embedded font not available. This appears to be a minified build - please provide `fontSource`.");{const s=await fetch(r);if(!s.ok)throw new v(`Failed to load font file: ${s.status} ${s.statusText}`);i=await s.arrayBuffer()}await this.li(i),this.Qs=F.parse(i)[0],await this.ui()}fi(r){if(r===void 0)return this.ti;this.ti=r,this.ii=this.ai.calculateMaxGlyphDimensions(this.Js.map(s=>s.character),this.ti,this.Qs);const i=this.oi.createTextureAtlas(this.Js,this.ii,this.ti,this.Qs);this.Ks=i.framebuffer,this.ei=i.columns,this.si=i.rows}async di(r){try{const i=await fetch(r);if(!i.ok)throw new v(`Failed to load font file: ${i.status} ${i.statusText}`);const s=await i.arrayBuffer();await this.li(s);const n=F.parse(s);if(!n||n.length===0)throw Error("Failed to parse font file");this.Qs=n[0],await this.ui()}catch(i){throw new v("Failed to load font: "+(i instanceof Error?i.message:"Unknown error"),i)}}async li(r){const i=Date.now();this.ri=new FontFace("CustomFont_"+i,r),await this.ri.load(),document.fonts.add(this.ri)}async ui(){const r=this.ni.extractCharacters(this.Qs),i=this.ni.filterProblematicCharacters(r);this.Js=this.hi.createCharacterObjects(i,this.Qs),this.ii=this.ai.calculateMaxGlyphDimensions(i,this.ti,this.Qs);const s=this.oi.createTextureAtlas(this.Js,this.ii,this.ti,this.Qs);this.Ks=s.framebuffer,this.ei=s.columns,this.si=s.rows}Zs(r){return this.hi.Zs(r,this.Js)}Ns(r){return this.hi.Ns(r,this.Js)}j(){this.Ks.j(),document.fonts.delete(this.ri)}get fontFramebuffer(){return this.Ks}get characters(){return this.Js}get textureColumns(){return this.ei}get textureRows(){return this.si}get maxGlyphDimensions(){return this.ii}get fontSize(){return this.ti}get font(){return this.Qs}}class q{constructor(r,i,s){u(this,"pi");u(this,"_i");u(this,"C");u(this,"$");u(this,"gi");u(this,"mi");u(this,"xi");u(this,"Ci");u(this,"yi");this.xi=r,this.Ci=i,this.yi=s,this.zt()}zt(){this.pi=Math.floor(this.xi.width/this.Ci),this._i=Math.floor(this.xi.height/this.yi),this.C=this.pi*this.Ci,this.$=this._i*this.yi,this.gi=Math.floor((this.xi.width-this.C)/2),this.mi=Math.floor((this.xi.height-this.$)/2)}wi(r,i){this.Ci=r,this.yi=i,this.zt()}get cellWidth(){return this.Ci}get cellHeight(){return this.yi}get cols(){return this.pi}get rows(){return this._i}get width(){return this.C}get height(){return this.$}get offsetX(){return this.gi}get offsetY(){return this.mi}}class J{constructor(r={}){u(this,"xi");u(this,"$i");u(this,"bi");r.canvas?(this.xi=r.canvas,this.bi=!1):(this.xi=this.zi(r.width,r.height),this.bi=!0),this.xi.style.imageRendering="pixelated"}zi(r,i){const s=document.createElement("canvas");return s.className="textmodeCanvas",s.style.imageRendering="pixelated",s.width=r||800,s.height=i||600,document.body.appendChild(s),s}W(r,i){this.xi.width=r??this.xi.width,this.xi.height=i??this.xi.height}Ri(){const r=this.xi.getContext("webgl2",{alpha:!0,premultipliedAlpha:!1,preserveDrawingBuffer:!0,antialias:!1,depth:!1,stencil:!1,powerPreference:"high-performance"});if(!r)throw new v("`textmode.js` requires WebGL2 support.");return r}j(){this.$i&&this.$i.disconnect();const r=this.xi.getContext("webgl")||this.xi.getContext("webgl2");if(r){const i=r.getExtension("WEBGL_lose_context");i&&i.loseContext()}this.bi&&this.xi.parentNode&&this.xi.parentNode.removeChild(this.xi)}get canvas(){return this.xi}get width(){return this.xi.width}get height(){return this.xi.height}}class Lt{constructor(r=60){u(this,"Ti");u(this,"Si");u(this,"Mi",null);u(this,"Fi",0);u(this,"Gi",!0);u(this,"Di",0);u(this,"Pi",0);u(this,"Ai",[]);u(this,"Bi",10);u(this,"Ii",0);this.Ti=r,this.Si=1e3/r}start(r){if(!this.Gi)return;this.Fi=performance.now();const i=s=>{if(!this.Gi)return void(this.Mi=null);const n=s-this.Fi;n>=this.Si&&(r(),this.Fi=s-n%this.Si),this.Gi&&(this.Mi=requestAnimationFrame(i))};this.Mi=requestAnimationFrame(i)}stop(){this.Mi&&(cancelAnimationFrame(this.Mi),this.Mi=null)}pause(){this.Gi&&(this.Gi=!1,this.stop())}resume(r){this.Gi||(this.Gi=!0,this.start(r))}frameRate(r,i){if(r===void 0)return this.Di;this.Ti=r,this.Si=1e3/r,this.Gi&&i&&(this.stop(),this.start(i))}measureFrameRate(){const r=performance.now();if(this.Pi>0){const i=r-this.Pi;this.Ai.push(i),this.Ai.length>this.Bi&&this.Ai.shift();const s=this.Ai.reduce((n,o)=>n+o,0)/this.Ai.length;this.Di=1e3/s}this.Pi=r}get isLooping(){return this.Gi}get frameRateLimit(){return this.Ti}get currentFrameRate(){return this.Di}get frameCount(){return this.Ii}set frameCount(r){this.Ii=r}incrementFrame(){this.Ii++}resetFrameCount(){this.Ii=0}}const Pt=c=>class extends c{rotate(r=0,i=0,s=0){this.Us.state.St(r),this.Us.state.Mt(i),this.Us.state.Ft(s)}rotateX(r){this.Us.state.St(r)}rotateY(r){this.Us.state.Mt(r)}rotateZ(r){this.Us.state.Ft(r)}push(){this.Us.state.$t()}pop(){this.Us.state.bt()}rect(r,i,s=1,n=1){this.Us.Je(r,i,s,n)}line(r,i,s,n){this.Us.Ke(r,i,s,n)}lineWeight(r){this.Us.state.Tt(r)}background(r,i=r,s=r,n=255){this.Us.hs(r,i,s,n)}char(r){this.Us.state.Gt(this.Qs.Zs(r))}charColor(r,i,s){this.Us.state.Dt(r,i,s)}cellColor(r,i,s){this.Us.state.Pt(r,i,s)}flipHorizontally(r){this.Us.state.At(r)}flipVertically(r){this.Us.state.Bt(r)}charRotation(r){this.Us.state.Lt(r)}invert(r){this.Us.state.It(r)}clear(){this.Us.hs(0,0,0,0)}ellipse(r,i,s,n){this.Us.ts(r,i,s/2,n/2)}triangle(r,i,s,n,o,a){this.Us.es(r,i,s,n,o,a)}bezierCurve(r,i,s,n,o,a,h,l){this.Us.ss(r,i,s,n,o,a,h,l)}arc(r,i,s,n,o,a){this.Us.ns(r,i,s,n,o,a)}shader(r){this.Us.Ye(r)}setUniform(r,i){this.Us.qe(r,i)}setUniforms(r){this.Us.Ze(r)}createFilterShader(r){return this.Us.Ne(r)}};class X{Li(r){const i=r.k(0),s=r.k(1),n=r.k(2),o=r.k(3);return{characterPixels:i,primaryColorPixels:s,secondaryColorPixels:n,transformPixels:r.k(4),rotationPixels:o}}Ei(r,i){return r[i]+(r[i+1]<<8)}Wi(r,i){return{r:r[i],g:r[i+1],b:r[i+2],a:r[i+3]}}}class Y{ki(r,i){return new Blob([r],{type:i})}Ui(r,i,s){try{const n=this.ki(r,s),o=URL.createObjectURL(n),a=document.createElement("a");a.href=o,a.download=i,a.style.display="none",a.rel="noopener",document.body.appendChild(a),a.click(),document.body.removeChild(a),URL.revokeObjectURL(o)}catch(n){throw console.error("Failed to download file:",n),Error("File download failed: "+(n instanceof Error?n.message:"Unknown error"))}}Vi(){return new Date().toISOString().slice(0,19).replace(/:/g,"-")}Oi(){const r=new Date;return{date:r.toISOString().split("T")[0],time:r.toTimeString().split(" ")[0].replace(/:/g,"-")}}ji(r){return r.replace(/[<>:"/\\|?*]/g,"_").replace(/\s+/g,"_").replace(/_{2,}/g,"_").replace(/^_+|_+$/g,"").substring(0,255)}Hi(){return"textmode-export-"+this.Vi()}}class It extends X{Xi(r,i,s){const n=r[s]===255,o=r[s+1]===255,a=r[s+2]===255,h=i[s],l=i[s+1];return{isInverted:n,flipHorizontal:o,flipVertical:a,rotation:Math.round(360*(h+l/255)/255*100)/100}}Yi(r,i,s){return{x:r,y:i,cellX:r*s.cellWidth,cellY:i*s.cellHeight}}qi(r,i){const s=[];let n=0;for(let o=0;o<i.rows;o++)for(let a=0;a<i.cols;a++){const h=4*n,l=this.Ei(r.characterPixels,h);let f=this.Wi(r.primaryColorPixels,h),g=this.Wi(r.secondaryColorPixels,h);const d=this.Xi(r.transformPixels,r.rotationPixels,h);if(d.isInverted){const x=f;f=g,g=x}const p=this.Yi(a,o,i);s.push({charIndex:l,primaryColor:f,secondaryColor:g,transform:d,position:p}),n++}return s}}class Dt{Zi(r,i){const s=r.cmap;for(const n of s.tables)if(n.format===4){const o=n;for(let a=0;a<o.startCount.length;a++)if(i>=o.startCount[a]&&i<=o.endCount[a]){if(o.idRangeOffset[a]===0)return i+o.idDelta[a]&65535;{const h=o.idRangeOffset[a]/2+(i-o.startCount[a])-(o.startCount.length-a);if(h>=0&&h<o.glyphIdArray.length){const l=o.glyphIdArray[h];if(l!==0)return l+o.idDelta[a]&65535}}}}else if(n.format===12){const o=n;for(let a=0;a<o.groups.length;a+=3){const h=o.groups[a],l=o.groups[a+1],f=o.groups[a+2];if(i>=h&&i<=l)return f+(i-h)}}return 0}Ni(r,i,s,n,o){const a=o/r.head.unitsPerEm;return{getBoundingBox:()=>({x1:s+i.xMin*a,y1:n+-i.yMax*a,x2:s+i.xMax*a,y2:n+-i.yMin*a}),toSVG:()=>this.Qi(i,s,n,a)}}Qi(r,i,s,n){if(!r||!r.xs)return"";const{xs:o,ys:a,endPts:h,flags:l}=r;if(!(o&&a&&h&&l))return"";let f="",g=0;for(let d=0;d<h.length;d++){const p=h[d];if(!(p<g)){if(p>=g){const x=i+o[g]*n,m=s-a[g]*n;f+=`M${x.toFixed(2)},${m.toFixed(2)}`;let y=g+1;for(;y<=p;)if(1&l[y]){const w=i+o[y]*n,E=s-a[y]*n;f+=`L${w.toFixed(2)},${E.toFixed(2)}`,y++}else{const w=i+o[y]*n,E=s-a[y]*n;let T=y+1>p?g:y+1;if(1&l[T]){const A=i+o[T]*n,R=s-a[T]*n;f+=`Q${w.toFixed(2)},${E.toFixed(2)} ${A.toFixed(2)},${R.toFixed(2)}`,y=T+1}else{const A=(w+(i+o[T]*n))/2,R=(E+(s-a[T]*n))/2;f+=`Q${w.toFixed(2)},${E.toFixed(2)} ${A.toFixed(2)},${R.toFixed(2)}`,y=T}}f+="Z"}g=p+1}}return f}Ji(r,i,s,n,o){const a=r.codePointAt(0)||0,h=this.Zi(i,a);let l=null;return i.glyf&&i.glyf[h]!==null?l=i.glyf[h]:(l=F.T.glyf.us(i,h),i.glyf[h]=l),this.Ni(i,l,s,n,o)}Ki(r,i,s,n,o,a,h,l){const f=s+(o-l*(h/i.head.unitsPerEm))/2,g=n+(a+.7*h)/2;return this.Ji(r,i,f,g,h).toSVG()||null}}class Mt{constructor(){u(this,"tr");this.tr=new Dt}er(r){return`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
6
+ `,i+="↓".repeat(24)+`
7
+ `,i}static o(t){if(t===null)return"null";if(t===void 0)return"undefined";if(typeof t=="string")return`"${t}"`;if(typeof t=="number"||typeof t=="boolean")return t+"";if(Array.isArray(t))return t.length===0?"[]":t.length<=5?`[${t.map(r=>v.o(r)).join(", ")}]`:`[${t.slice(0,3).map(r=>v.o(r)).join(", ")}, ... +${t.length-3} more]`;if(typeof t=="object"){const r=Object.keys(t);return r.length===0?"{}":r.length<=3?`{ ${r.map(i=>`${i}: ${v.o(t[i])}`).join(", ")} }`:`{ ${r.slice(0,2).map(i=>`${i}: ${v.o(t[i])}`).join(", ")}, ... +${r.length-2} more }`}return t+""}}var L=(c=>(c[c.SILENT=0]="SILENT",c[c.WARNING=1]="WARNING",c[c.ERROR=2]="ERROR",c[c.THROW=3]="THROW",c))(L||{});const I=class I{constructor(){u(this,"u",{globalLevel:3})}static p(){return I.l||(I.l=new I),I.l}_(t,r){const i="%c[textmode.js] Oops! (╯°□°)╯︵ Something went wrong in your code.",n="color: #f44336; font-weight: bold; background: #ffebee; padding: 2px 6px; border-radius: 3px;";switch(this.u.globalLevel){case 0:return!1;case 1:return console.group(i,n),console.warn(v.i(t,r)),console.groupEnd(),!1;case 2:return console.group(i,n),console.error(v.i(t,r)),console.groupEnd(),!1;default:throw new v(t,r)}}m(t,r,i){return!!t||(this._(r,i),!1)}v(t){this.u.globalLevel=t}};u(I,"l",null);let k=I;const z=k.p(),H=new WeakMap;function G(c,t){H.set(c,t)}function D(c){return H.get(c)}class N{constructor(t,r,i=r,n=1,o={}){u(this,"C");u(this,"$");u(this,"u");u(this,"R",null);u(this,"S");u(this,"M");u(this,"F",[]);u(this,"G");u(this,"D",null);u(this,"P",[]);this.C=r,this.$=i,this.u={filter:"nearest",wrap:"clamp",format:"rgba",type:"unsigned_byte",...o},this.S=t,this.G=Math.min(Math.max(1,n),8);const a=t.getParameter(t.MAX_DRAW_BUFFERS),h=t.getParameter(t.MAX_COLOR_ATTACHMENTS);this.G=Math.min(this.G,a,h),this.M=t.createFramebuffer(),this.A(),this.I(),this.P=Array(this.G).fill(null)}A(){const t=this.S,r=this.u.filter==="linear"?t.LINEAR:t.NEAREST,i=this.u.wrap==="repeat"?t.REPEAT:t.CLAMP_TO_EDGE,n=this.u.type==="float"?t.FLOAT:t.UNSIGNED_BYTE;for(let o=0;o<this.G;o++){const a=t.createTexture();t.bindTexture(t.TEXTURE_2D,a),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,r),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,r),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,i),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,i),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,this.C,this.$,0,t.RGBA,n,null),this.F.push(a)}t.bindTexture(t.TEXTURE_2D,null)}I(){const t=this.S;if(t.bindFramebuffer(t.FRAMEBUFFER,this.M),this.G===1)t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,this.F[0],0);else{const i=[];for(let n=0;n<this.G;n++){const o=t.COLOR_ATTACHMENT0+n;t.framebufferTexture2D(t.FRAMEBUFFER,o,t.TEXTURE_2D,this.F[n],0),i.push(o)}t.drawBuffers(i)}const r=t.checkFramebufferStatus(t.FRAMEBUFFER);r!==t.FRAMEBUFFER_COMPLETE&&console.error("GLFramebuffer is not complete:",r),t.bindFramebuffer(t.FRAMEBUFFER,null)}L(t){const r=this.S;r.bindTexture(r.TEXTURE_2D,this.F[0]),r.pixelStorei(r.UNPACK_FLIP_Y_WEBGL,1),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,t),r.bindTexture(r.TEXTURE_2D,null)}W(t,r){this.C=t,this.$=r,this.R=null,this.P=Array(this.G).fill(null);const i=this.S,n=this.u.type==="float"?i.FLOAT:i.UNSIGNED_BYTE;for(const o of this.F)i.bindTexture(i.TEXTURE_2D,o),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,this.C,this.$,0,i.RGBA,n,null);i.bindTexture(i.TEXTURE_2D,null)}k(t){const r=this.S;if(t<0||t>=this.G)throw Error(`GLFramebuffer: attachment index ${t} out of range (count=${this.G})`);const i=this.P[t];if(i)return i;const n=this.C,o=this.$,a=new Uint8Array(n*o*4),h=r.getParameter(r.READ_FRAMEBUFFER_BINDING);r.bindFramebuffer(r.READ_FRAMEBUFFER,this.M),r.readBuffer(r.COLOR_ATTACHMENT0+t),r.readPixels(0,0,n,o,r.RGBA,r.UNSIGNED_BYTE,a),r.bindFramebuffer(r.READ_FRAMEBUFFER,h);const l=4*n,f=new Uint8Array(a.length);for(let g=0;g<o;g++){const d=(o-1-g)*l,p=g*l;f.set(a.subarray(d,d+l),p)}return this.P[t]=f,f}U(){for(let t=0;t<this.G;t++)this.k(t)}V(){const t=this.S;this.D={framebuffer:t.getParameter(t.FRAMEBUFFER_BINDING),viewport:t.getParameter(t.VIEWPORT)},t.bindFramebuffer(t.FRAMEBUFFER,this.M),this.P=Array(this.G).fill(null);for(let r=0;r<this.G;r++)t.clearBufferfv(t.COLOR,r,new Float32Array([0,0,0,0]));t.viewport(0,0,this.C,this.$),G(t,[0,0,this.C,this.$])}O(){if(!this.D)return;const t=this.S;t.bindFramebuffer(t.FRAMEBUFFER,this.D.framebuffer),t.viewport(...this.D.viewport),G(t,this.D.viewport),this.D=null}j(){const t=this.S;this.M&&t.deleteFramebuffer(this.M);for(const r of this.F)t.deleteTexture(r);this.F=[],this.P=[]}get width(){return this.C}get height(){return this.$}get pixels(){return this.R}get options(){return{...this.u}}get framebuffer(){return this.M}get texture(){return this.F[0]}get textures(){return[...this.F]}get attachmentCount(){return this.G}getAttachmentPixels(t){return this.P[t]??null}}class V{constructor(t,r,i){u(this,"S");u(this,"H");u(this,"X",new Map);u(this,"Y",new Map);u(this,"q",0);u(this,"Z");this.S=t,this.H=this.N(r,i),this.Z=t.getParameter(t.MAX_TEXTURE_IMAGE_UNITS),this.J()}J(){const t=this.S.getProgramParameter(this.H,this.S.ACTIVE_UNIFORMS);for(let r=0;r<t;r++){const i=this.S.getActiveUniform(this.H,r);if(i){const n=this.S.getUniformLocation(this.H,i.name);n&&(this.X.set(i.name,n),this.Y.set(i.name,i.type))}}}N(t,r){const i=this.K(this.S.VERTEX_SHADER,t),n=this.K(this.S.FRAGMENT_SHADER,r),o=this.S.createProgram();if(this.S.attachShader(o,i),this.S.attachShader(o,n),this.S.linkProgram(o),!this.S.getProgramParameter(o,this.S.LINK_STATUS)){const a=this.S.getProgramInfoLog(o);throw Error("Shader program link error: "+a)}return this.S.deleteShader(i),this.S.deleteShader(n),o}K(t,r){const i=this.S.createShader(t);if(this.S.shaderSource(i,r),this.S.compileShader(i),!this.S.getShaderParameter(i,this.S.COMPILE_STATUS)){const n=this.S.getShaderInfoLog(i);throw this.S.deleteShader(i),Error("Shader compilation error: "+n)}return i}tt(){this.S.useProgram(this.H),this.et()}et(){this.q=0}st(t){for(const[r,i]of Object.entries(t))this.it(r,i)}rt(t){return this.X.has(t)}it(t,r){if(this.S.getParameter(this.S.CURRENT_PROGRAM)!==this.H)return void console.warn(`Attempting to set uniform '${t}' on shader that is not currently bound`);const i=this.X.get(t);if(i)if(typeof r=="number")this.S.uniform1f(i,r);else if(typeof r=="boolean")this.S.uniform1i(i,r?1:0);else if(Array.isArray(r))switch(r.length){case 2:this.S.uniform2f(i,r[0],r[1]);break;case 3:this.S.uniform3f(i,r[0],r[1],r[2]);break;case 4:this.S.uniform4f(i,r[0],r[1],r[2],r[3]);break;default:console.warn(`Unsupported array length ${r.length} for uniform '${t}'`)}else if(r instanceof WebGLTexture){const n=this.nt();this.S.uniform1i(i,n),this.S.activeTexture(this.S.TEXTURE0+n),this.S.bindTexture(this.S.TEXTURE_2D,r)}else if(r instanceof N){const n=this.nt();this.S.uniform1i(i,n),this.S.activeTexture(this.S.TEXTURE0+n),this.S.bindTexture(this.S.TEXTURE_2D,r.texture)}else if(typeof r=="object"&&"texture"in r){const n=this.nt();this.S.uniform1i(i,n),this.S.activeTexture(this.S.TEXTURE0+n),this.S.bindTexture(this.S.TEXTURE_2D,r.texture)}else console.warn(`Unsupported uniform type for '${t}':`,typeof r)}nt(){return this.q>=this.Z&&console.warn(`Exceeded maximum texture units (${this.Z}). Texture may not render correctly.`),this.q++}get ot(){return this.H}j(){this.S.deleteProgram(this.H)}}class st{constructor(){u(this,"ht",1);u(this,"ct",0);u(this,"lt",0);u(this,"ut",0);u(this,"ft",[0,0,0]);u(this,"dt",[1,1,1,1]);u(this,"_t",[0,0,0,1]);u(this,"gt",!1);u(this,"vt",!1);u(this,"xt",!1);u(this,"Ct",[0,0]);u(this,"yt",[0,0,0,1]);u(this,"wt",[])}$t(){this.wt.push({lineWeight:this.ht,rotationX:this.ct,rotationY:this.lt,rotationZ:this.ut,charRotation:[...this.Ct],flipHorizontally:this.gt,flipVertically:this.vt,invert:this.xt,character:[...this.ft],charColor:[...this.dt],cellColor:[...this._t]})}bt(){const t=this.wt.pop();t?(this.ht=t.lineWeight,this.ct=t.rotationX,this.lt=t.rotationY,this.ut=t.rotationZ,this.Ct=t.charRotation,this.gt=t.flipHorizontally,this.vt=t.flipVertically,this.xt=t.invert,this.ft=t.character,this.dt=t.charColor,this._t=t.cellColor):console.warn("pop() called without matching push()")}zt(){this.wt=[],this.ct=0,this.lt=0,this.ut=0}Rt(t){t.lineWeight=this.ht,t.rotationX=this.ct,t.rotationY=this.lt,t.rotationZ=this.ut,t.character[0]=this.ft[0],t.character[1]=this.ft[1],t.character[2]=this.ft[2],t.charColor[0]=this.dt[0],t.charColor[1]=this.dt[1],t.charColor[2]=this.dt[2],t.charColor[3]=this.dt[3],t.bgColor[0]=this._t[0],t.bgColor[1]=this._t[1],t.bgColor[2]=this._t[2],t.bgColor[3]=this._t[3],t.flipHorizontally=this.gt,t.flipVertically=this.vt,t.invert=this.xt,t.charRotation[0]=this.Ct[0],t.charRotation[1]=this.Ct[1]}get lineWeight(){return this.ht}get canvasBackgroundColor(){return this.yt}Tt(t){this.ht=Math.abs(t)}St(t){this.ct=t}Mt(t){this.lt=t}Ft(t){this.ut=t}Gt(t){this.ft=t}Dt(t,r,i,n=255){this.dt=[t/255,r/255,i/255,n/255]}Pt(t,r,i,n=255){this._t=[t/255,r/255,i/255,n/255]}At(t){this.gt=t}Bt(t){this.vt=t}It(t){this.xt=t}Lt(t){const r=255*t/360,i=Math.floor(r)/255,n=Math.round(r-Math.floor(r));this.Ct=[i,n]}Et(t,r,i,n){this.yt=[t/255,r/255,i/255,n/255]}}var X=`#version 300 es
8
+ in vec2 a_position;in vec2 a_texCoord;in vec2 a_instancePosition;in vec2 a_instanceSize;in vec3 a_instanceCharacter;in vec4 a_instancePrimaryColor;in vec4 a_instanceSecondaryColor;in vec2 a_instanceRotation;in vec3 a_instanceTransform;in vec3 a_instanceGlobalRotation;in vec2 a_instanceRotationCenter;in vec2 a_instanceBezierCP1;in vec2 a_instanceBezierCP2;in vec2 a_instanceBezierStart;in vec2 a_instanceBezierEnd;in vec2 a_instanceArcAngles;uniform float u_aspectRatio;uniform vec2 u_viewportSize;out vec2 v_uv;out vec3 v_character;out vec4 v_primaryColor;out vec4 v_secondaryColor;out vec2 v_rotation;out vec3 v_transform;mat3 rotateX(float a){float s=sin(a),c=cos(a);return mat3(1,0,0,0,c,-s,0,s,c);}mat3 rotateY(float a){float s=sin(a),c=cos(a);return mat3(c,0,s,0,1,0,-s,0,c);}mat3 rotateZ(float a){float s=sin(a),c=cos(a);return mat3(c,-s,0,s,c,0,0,0,1);}vec2 evaluateBezier(float t,vec2 p0,vec2 p1,vec2 p2,vec2 p3){float u=1.-t,u2=u*u,t2=t*t;return u2*u*p0+3.*u2*t*p1+3.*u*t2*p2+t2*t*p3;}vec2 evaluateBezierDerivative(float t,vec2 p0,vec2 p1,vec2 p2,vec2 p3){float u=1.-t,u2=u*u,t2=t*t;return-3.*u2*p0+3.*u2*p1-6.*u*t*p1+6.*u*t*p2-3.*t2*p2+3.*t2*p3;}void main(){v_uv=a_texCoord;v_character=a_instanceCharacter;v_primaryColor=a_instancePrimaryColor;v_secondaryColor=a_instanceSecondaryColor;v_rotation=a_instanceRotation;v_transform=a_instanceTransform;vec2 worldPosition;bool isBezier=length(a_instanceBezierCP1)+length(a_instanceBezierCP2)+length(a_instanceBezierStart)+length(a_instanceBezierEnd)>0.;bool isArc=a_instanceArcAngles.x!=0.||a_instanceArcAngles.y!=0.;if(isBezier){float t=a_position.x;vec2 curvePoint=evaluateBezier(t,a_instanceBezierStart,a_instanceBezierCP1,a_instanceBezierCP2,a_instanceBezierEnd);vec2 tangent=evaluateBezierDerivative(t,a_instanceBezierStart,a_instanceBezierCP1,a_instanceBezierCP2,a_instanceBezierEnd);float tLen=length(tangent);tangent=tLen>0.?tangent/tLen:vec2(1,0);worldPosition=curvePoint+vec2(-tangent.y,tangent.x)*a_position.y*a_instanceSize.y;}else if(isArc){float s=a_instanceArcAngles.x,e=a_instanceArcAngles.y;s=mod(s,6.28318530718);if(s<0.)s+=6.28318530718;e=mod(e,6.28318530718);if(e<0.)e+=6.28318530718;float d=s-e;if(d<=0.)d+=6.28318530718;float angle=s-a_position.x*d;vec2 local=vec2(cos(angle),sin(angle))*a_position.y;worldPosition=local*a_instanceSize*.5+a_instanceSize*.5+a_instancePosition;}else{worldPosition=a_position*a_instanceSize+a_instancePosition;}vec2 ndc=(worldPosition/u_viewportSize)*2.-1.;ndc.y=-ndc.y;if(length(a_instanceGlobalRotation)>0.){vec3 pos3D=vec3(ndc-a_instanceRotationCenter,0);pos3D.x*=u_aspectRatio;if(a_instanceGlobalRotation.x!=0.)pos3D=rotateX(-a_instanceGlobalRotation.x)*pos3D;if(a_instanceGlobalRotation.y!=0.)pos3D=rotateY(-a_instanceGlobalRotation.y)*pos3D;if(a_instanceGlobalRotation.z!=0.)pos3D=rotateZ(-a_instanceGlobalRotation.z)*pos3D;pos3D.x/=u_aspectRatio;ndc=pos3D.xy+a_instanceRotationCenter;}gl_Position=vec4(ndc,0,1);}`,C=(c=>(c.RECTANGLE="rectangle",c.LINE="line",c.ELLIPSE="ellipse",c.ARC="arc",c.TRIANGLE="triangle",c.BEZIER_CURVE="bezier_curve",c.CUSTOM="custom",c))(C||{});class nt{constructor(t){u(this,"S");u(this,"Wt",new Map);this.S=t}kt(t,r,i,n){const o=this.S;let a=this.Wt.get(t);a||(a=new Map,this.Wt.set(t,a));let h=a.get(r)||null;if(!h){h=o.createVertexArray(),a.set(r,h),o.bindVertexArray(h),o.bindBuffer(o.ARRAY_BUFFER,n);const l=o.getAttribLocation(t,"a_position");l!==-1&&(o.enableVertexAttribArray(l),o.vertexAttribPointer(l,i.attributes.position.size,o.FLOAT,!1,i.stride,i.attributes.position.offset),o.vertexAttribDivisor(l,0));const f=o.getAttribLocation(t,"a_texCoord");f!==-1&&(o.enableVertexAttribArray(f),o.vertexAttribPointer(f,i.attributes.texCoord.size,o.FLOAT,!1,i.stride,i.attributes.texCoord.offset),o.vertexAttribDivisor(f,0))}o.bindVertexArray(h)}Ut(){this.S.bindVertexArray(null)}j(){const t=this.S;for(const[,r]of this.Wt)for(const[,i]of r)i&&t.deleteVertexArray(i);this.Wt.clear()}}class ot{constructor(t){u(this,"Vt");u(this,"S");u(this,"Ot",null);u(this,"jt",null);u(this,"Ht",null);this.S=t,this.Vt=new nt(t)}Xt(t,r,i){const{shader:n}=t,o=D(this.S)||this.S.getParameter(this.S.VIEWPORT),a={u_aspectRatio:o[2]/o[3],u_viewportSize:[o[2],o[3]]},h={};for(const[d,p]of Object.entries(a))n.rt(d)&&(h[d]=p);Object.keys(h).length>0&&n.st(h);const l=d=>{if(!d||!d.Yt())return;const p=d.unitGeometry,x=d.unitBuffer;try{this.Vt.kt(n.ot,d.type+"",p,x),d.batch.qt(n),d.batch.Zt(p.primitiveType,p.vertexCount)}finally{d.batch.Nt(n),this.Vt.Ut(),d.Qt()}};let f=null,g=null;for(const d of r){if(d.type===C.CUSTOM){g&&(l(g),f=null,g=null),this.Jt(t,d.params,d.state,i.get(C.RECTANGLE)||null);continue}f!==null&&d.type!==f&&(l(g),f=null,g=null);let p=g;p&&d.type===f||(p=i.get(d.type)||null,g=p,f=d.type),p&&p.Kt(d.params,d.state)}l(g)}Jt(t,r,i,n){if(!n)return;const{x:o,y:a,width:h,height:l,shader:f,uniforms:g}=r,d=this.S;f.tt(),n.Qt();const p=this.te(Math.max(1,Math.floor(h)),Math.max(1,Math.floor(l)));p.V(),f.tt(),g&&Object.keys(g).length&&f.st(g);{const R=D(d)||d.getParameter(d.VIEWPORT);f.rt("u_aspectRatio")&&f.it("u_aspectRatio",R[2]/R[3]),f.rt("u_viewportSize")&&f.it("u_viewportSize",[R[2],R[3]])}const x={...i,rotationX:0,rotationY:0,rotationZ:0};if(n.Kt({x:0,y:0,width:p.width,height:p.height},x),n.Yt()){const R=n.unitGeometry,B=n.unitBuffer;try{this.Vt.kt(f.ot,n.type+"",R,B),n.batch.qt(f),n.batch.Zt(R.primitiveType,R.vertexCount)}finally{n.batch.Nt(f),this.Vt.Ut(),n.Qt()}}p.O();const m=this.ee();m.tt(),m.st({u_src0:p.textures[0],u_src1:p.textures[1],u_src2:p.textures[2],u_src3:p.textures[3],u_src4:p.textures[4],u_srcSize:[p.width,p.height]});const y=D(d)||d.getParameter(d.VIEWPORT);m.rt("u_aspectRatio")&&m.it("u_aspectRatio",y[2]/y[3]),m.rt("u_viewportSize")&&m.it("u_viewportSize",[y[2],y[3]]);const w=Math.floor(o),E=Math.floor(a),T=Math.max(1,Math.floor(h)),A=Math.max(1,Math.floor(l));if(n.Kt({x:w,y:E,width:T,height:A},i),n.Yt()){const R=n.unitGeometry,B=n.unitBuffer;try{this.Vt.kt(m.ot,n.type+"",R,B),n.batch.qt(m),n.batch.Zt(R.primitiveType,R.vertexCount)}finally{n.batch.Nt(m),this.Vt.Ut(),n.Qt()}}t.shader.tt()}ee(){return this.Ot||(this.Ot=new V(this.S,X,`#version 300 es
9
+ precision highp float;in vec2 v_uv;uniform sampler2D u_src0;uniform sampler2D u_src1;uniform sampler2D u_src2;uniform sampler2D u_src3;uniform sampler2D u_src4;uniform vec2 u_srcSize;layout(location=0)out vec4 o_character;layout(location=1)out vec4 o_primaryColor;layout(location=2)out vec4 o_secondaryColor;layout(location=3)out vec4 o_rotation;layout(location=4)out vec4 o_transform;void main(){vec2 uvTex=v_uv*u_srcSize;vec2 uvQ=(floor(uvTex)+0.5f)/u_srcSize;o_character=texture(u_src0,uvQ);o_primaryColor=texture(u_src1,uvQ);o_secondaryColor=texture(u_src2,uvQ);o_rotation=texture(u_src3,uvQ);o_transform=texture(u_src4,uvQ);}`)),this.Ot}te(t,r){return this.jt&&this.Ht&&this.Ht.w===t&&this.Ht.h===r||(this.jt&&this.jt.j(),this.jt=new N(this.S,t,r,5),this.Ht={w:t,h:r}),this.jt}}class at{constructor(){u(this,"se",[]);u(this,"ie",1);u(this,"re",0)}ne(t){if(this.re>=this.se.length){const i={id:this.ie++,type:t,params:{},state:{lineWeight:1,rotationX:0,rotationY:0,rotationZ:0,character:[0,0,0],charColor:[1,1,1,1],bgColor:[0,0,0,1],flipHorizontally:!1,flipVertically:!1,invert:!1,charRotation:[0,0]}};this.se.push(i)}const r=this.se[this.re];switch(r.id=this.ie++,r.type=t,t){case C.RECTANGLE:case C.ELLIPSE:r.params&&"width"in r.params||(r.params={x:0,y:0,width:0,height:0});break;case C.CUSTOM:r.params&&"shader"in r.params||(r.params={x:0,y:0,width:0,height:0,shader:void 0,uniforms:{}});break;case C.ARC:r.params&&"start"in r.params||(r.params={x:0,y:0,width:0,height:0,start:0,stop:0});break;case C.LINE:r.params&&"x2"in r.params||(r.params={x1:0,y1:0,x2:0,y2:0,thickness:void 0});break;case C.TRIANGLE:r.params&&"x3"in r.params||(r.params={x1:0,y1:0,x2:0,y2:0,x3:0,y3:0});break;case C.BEZIER_CURVE:r.params&&"cp2y"in r.params||(r.params={x1:0,y1:0,cp1x:0,cp1y:0,cp2x:0,cp2y:0,x2:0,y2:0,thickness:void 0});break;default:r.params||(r.params={})}return this.re++,r}oe(t,r,i,n,o){const a=this.ne(C.RECTANGLE);return a.params.x=t,a.params.y=r,a.params.width=i,a.params.height=n,o.Rt(a.state),a.id}ae(t,r,i,n,o,a,h){const l=this.ne(C.CUSTOM);return l.params.x=t,l.params.y=r,l.params.width=i,l.params.height=n,l.params.shader=o,l.params.uniforms=a,h.Rt(l.state),l.id}he(t,r,i,n,o,a){const h=this.ne(C.LINE);return h.params.x1=t,h.params.y1=r,h.params.x2=i,h.params.y2=n,h.params.thickness=o,a.Rt(h.state),h.id}ce(t,r,i,n,o){const a=this.ne(C.ELLIPSE);return a.params.x=t,a.params.y=r,a.params.width=i,a.params.height=n,o.Rt(a.state),a.id}le(t,r,i,n,o,a,h){const l=this.ne(C.ARC);return l.params.x=t,l.params.y=r,l.params.width=i,l.params.height=n,l.params.start=o,l.params.stop=a,h.Rt(l.state),l.id}ue(t,r,i,n,o,a,h){const l=this.ne(C.TRIANGLE);return l.params.x1=t,l.params.y1=r,l.params.x2=i,l.params.y2=n,l.params.x3=o,l.params.y3=a,h.Rt(l.state),l.id}fe(t,r,i,n,o,a,h,l,f,g){const d=this.ne(C.BEZIER_CURVE);return d.params.x1=t,d.params.y1=r,d.params.cp1x=i,d.params.cp1y=n,d.params.cp2x=o,d.params.cp2y=a,d.params.x2=h,d.params.y2=l,d.params.thickness=f,g.Rt(d.state),d.id}get length(){return this.re}get isEmpty(){return this.re===0}de(){this.re=0}[Symbol.iterator](){let t=0;const r=this.re,i=this.se;return{next:()=>t<r?{value:i[t++],done:!1}:{value:void 0,done:!0}}}}const U=class U{static pe(t,r,i=0){var a,h,l,f,g,d,p,x,m,y;const n=r||new Float32Array(U.FLOATS_PER_INSTANCE);let o=i;return n[o++]=t.position[0],n[o++]=t.position[1],n[o++]=t.size[0],n[o++]=t.size[1],n[o++]=t.character[0],n[o++]=t.character[1],n[o++]=t.character[2],n[o++]=t.primaryColor[0],n[o++]=t.primaryColor[1],n[o++]=t.primaryColor[2],n[o++]=t.primaryColor[3],n[o++]=t.secondaryColor[0],n[o++]=t.secondaryColor[1],n[o++]=t.secondaryColor[2],n[o++]=t.secondaryColor[3],n[o++]=t.rotation[0],n[o++]=t.rotation[1],n[o++]=t.transform[0],n[o++]=t.transform[1],n[o++]=t.transform[2],n[o++]=t.globalRotationX,n[o++]=t.globalRotationY,n[o++]=t.globalRotationZ,n[o++]=t.rotationCenter[0],n[o++]=t.rotationCenter[1],n[o++]=((a=t.arcAngles)==null?void 0:a[0])||0,n[o++]=((h=t.arcAngles)==null?void 0:h[1])||0,n[o++]=((l=t.bezierControlPoint1)==null?void 0:l[0])||0,n[o++]=((f=t.bezierControlPoint1)==null?void 0:f[1])||0,n[o++]=((g=t.bezierControlPoint2)==null?void 0:g[0])||0,n[o++]=((d=t.bezierControlPoint2)==null?void 0:d[1])||0,n[o++]=((p=t.bezierStartPoint)==null?void 0:p[0])||0,n[o++]=((x=t.bezierStartPoint)==null?void 0:x[1])||0,n[o++]=((m=t.bezierEndPoint)==null?void 0:m[0])||0,n[o++]=((y=t.bezierEndPoint)==null?void 0:y[1])||0,n}static _e(t){const r=t.length*U.FLOATS_PER_INSTANCE,i=new Float32Array(r);for(let n=0;n<t.length;n++){const o=n*U.FLOATS_PER_INSTANCE;U.pe(t[n],i,o)}return i}};u(U,"BYTES_PER_INSTANCE",140),u(U,"FLOATS_PER_INSTANCE",35);let P=U;const S=class S{};u(S,"STRIDE",P.BYTES_PER_INSTANCE),u(S,"ATTRIBUTES",{a_instancePosition:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:0,divisor:1},a_instanceSize:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:8,divisor:1},a_instanceCharacter:{location:-1,size:3,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:16,divisor:1},a_instancePrimaryColor:{location:-1,size:4,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:28,divisor:1},a_instanceSecondaryColor:{location:-1,size:4,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:44,divisor:1},a_instanceRotation:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:60,divisor:1},a_instanceTransform:{location:-1,size:3,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:68,divisor:1},a_instanceGlobalRotation:{location:-1,size:3,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:80,divisor:1},a_instanceRotationCenter:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:92,divisor:1},a_instanceArcAngles:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:100,divisor:1},a_instanceBezierCP1:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:108,divisor:1},a_instanceBezierCP2:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:116,divisor:1},a_instanceBezierStart:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:124,divisor:1},a_instanceBezierEnd:{location:-1,size:2,type:WebGL2RenderingContext.FLOAT,normalized:!1,stride:S.STRIDE,offset:132,divisor:1}});let $=S;class ht{constructor(t,r=1e3,i=1.5){u(this,"S");u(this,"ge",[]);u(this,"me");u(this,"ve");u(this,"xe",null);u(this,"Ce",!0);u(this,"ye",0);u(this,"we",new Map);u(this,"$e",null);this.S=t,this.me=r,this.ve=i,this.be()}Kt(t){const r=this.ge.length;return this.ge.push(t),this.Ce=!0,r}get count(){return this.ge.length}get isEmpty(){return this.ge.length===0}clear(){this.ge.length=0,this.Ce=!0}ze(t){if(t<=this.me)return;const r=Math.ceil(t*this.ve);this.me=r,this.be()}be(){const t=this.S;this.xe&&t.deleteBuffer(this.xe),this.xe=t.createBuffer();const r=this.me*P.BYTES_PER_INSTANCE;t.bindBuffer(t.ARRAY_BUFFER,this.xe),t.bufferData(t.ARRAY_BUFFER,r,t.DYNAMIC_DRAW),t.bindBuffer(t.ARRAY_BUFFER,null),this.Ce=!0,this.ye=0}Re(){if(!this.Ce||this.ge.length===0)return;const t=this.S,r=this.ge.length;this.ze(r),(!this.$e||this.$e.length<r*P.FLOATS_PER_INSTANCE)&&(this.$e=new Float32Array(r*P.FLOATS_PER_INSTANCE));const i=P._e(this.ge);t.bindBuffer(t.ARRAY_BUFFER,this.xe),r<=this.ye?t.bufferSubData(t.ARRAY_BUFFER,0,i):t.bufferData(t.ARRAY_BUFFER,i,t.DYNAMIC_DRAW),t.bindBuffer(t.ARRAY_BUFFER,null),this.Ce=!1,this.ye=r}Te(t){let r=this.we.get(t);if(!r){r=new Map;const i=this.S;for(const n in $.ATTRIBUTES){const o=i.getAttribLocation(t,n);o!==-1&&r.set(n,o)}this.we.set(t,r)}return r}qt(t){if(!this.xe||this.ge.length===0)return;const r=this.S,i=t.ot;this.Re();const n=this.Te(i);r.bindBuffer(r.ARRAY_BUFFER,this.xe);for(const[o,a]of n){const h=$.ATTRIBUTES[o];h&&(r.enableVertexAttribArray(a),r.vertexAttribPointer(a,h.size,h.type,h.normalized,h.stride,h.offset),r.vertexAttribDivisor(a,h.divisor))}}Nt(t){const r=this.S,i=this.Te(t.ot);for(const[,n]of i)r.disableVertexAttribArray(n),r.vertexAttribDivisor(n,0)}Zt(t,r){this.ge.length!==0&&this.S.drawArraysInstanced(t,0,r,this.ge.length)}j(){const t=this.S;this.xe&&(t.deleteBuffer(this.xe),this.xe=null),this.ge.length=0,this.we.clear(),this.$e=null}}class M{constructor(t,r,i,n){u(this,"S");u(this,"Se");u(this,"Me");u(this,"Fe");u(this,"Ge",null);this.S=t,this.Se=r,this.Me=i,this.Fe=n;const o=this.S.createBuffer();if(!o)throw Error("Failed to create unit geometry buffer");this.S.bindBuffer(this.S.ARRAY_BUFFER,o),this.S.bufferData(this.S.ARRAY_BUFFER,this.Fe.vertices,this.S.STATIC_DRAW),this.S.bindBuffer(this.S.ARRAY_BUFFER,null),this.Ge=o}get type(){return this.Me}get unitGeometry(){return this.Fe}get unitBuffer(){return this.Ge}get batch(){return this.Se}Qt(){this.Se.clear()}Yt(){return!this.Se.isEmpty}j(){this.Se.j(),this.Ge&&(this.S.deleteBuffer(this.Ge),this.Ge=null)}De(t,r,i,n,o){const a=this.Pe(t,r,i,n,o.rotationX||0,o.rotationY||0,o.rotationZ||0);return{position:[t,r],size:[i,n],character:o.character||[0,0,0],primaryColor:o.charColor||[1,1,1,1],secondaryColor:o.bgColor||[0,0,0,1],rotation:o.charRotation||[0,0],transform:[o.invert?1:0,o.flipHorizontally?1:0,o.flipVertically?1:0],globalRotationX:a.radiansX,globalRotationY:a.radiansY,globalRotationZ:a.radiansZ,rotationCenter:[a.centerX,a.centerY]}}Ae(t,r){const i=D(this.S)||[0,0,this.S.canvas.width,this.S.canvas.height];return{nx:t/i[2]*2-1,ny:1-r/i[3]*2}}Be(t,r,i){const n=this.Ae(r,i);t.rotationCenter=[n.nx,n.ny]}Pe(t,r,i,n,o,a,h){const l=D(this.S)||[0,0,this.S.canvas.width,this.S.canvas.height],f=l[2],g=l[3];return{centerX:(t+i/2)/f*2-1,centerY:1-(r+n/2)/g*2,radiansX:-o*Math.PI/180,radiansY:-a*Math.PI/180,radiansZ:-h*Math.PI/180,aspectRatio:f/g}}}const ct={vertices:new Float32Array([0,0,0,0,1,0,1,0,0,1,0,1,0,1,0,1,1,0,1,0,1,1,1,1]),vertexCount:6,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class lt extends M{constructor(t,r){super(t,r,C.RECTANGLE,ct)}Kt(t,r){const i=this.De(t.x,t.y,t.width,t.height,r);return this.Se.Kt(i)}}const ut={vertices:new Float32Array([0,-.5,0,0,1,-.5,1,0,0,.5,0,1,0,.5,0,1,1,-.5,1,0,1,.5,1,1]),vertexCount:6,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class ft extends M{constructor(t,r){super(t,r,C.LINE,ut)}Kt(t,r){const i=t.x2-t.x1,n=t.y2-t.y1,o=Math.hypot(i,n),a=Math.atan2(n,i),h=t.thickness||r.lineWeight||1,l=t.x1+i/2,f=t.y1+n/2,g=l-o/2,d=f,p={character:r.character,charColor:r.charColor,bgColor:r.bgColor,charRotation:r.charRotation,flipHorizontally:r.flipHorizontally,flipVertically:r.flipVertically,invert:r.invert,rotationX:r.rotationX||0,rotationY:r.rotationY||0,rotationZ:(r.rotationZ||0)+180*a/Math.PI,lineWeight:h},x=this.De(g,d,o,h,p);return this.Be(x,l,f),this.Se.Kt(x)}}const dt={vertices:function(c=32){const t=[],r=2*Math.PI/c;for(let i=0;i<c;i++){const n=i*r,o=(i+1)%c*r,a=Math.cos(n),h=Math.sin(n),l=.5*(a+1),f=.5*(h+1),g=Math.cos(o),d=Math.sin(o),p=.5*(g+1),x=.5*(d+1);t.push(0,0,.5,.5,a,h,l,f,g,d,p,x)}return new Float32Array(t)}(32),vertexCount:96,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class pt extends M{constructor(t,r){super(t,r,C.ELLIPSE,dt)}Kt(t,r){const i=this.De(t.x,t.y,t.width,t.height,r);return this.Be(i,t.x,t.y),this.Se.Kt(i)}}let gt={vertices:function(c){const t=[];for(let r=0;r<c;r++){const i=r/c,n=(r+1)/c;t.push(i,0,i,0,i,1,i,1,n,1,n,1)}return new Float32Array(t)}(32),vertexCount:96,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class mt extends M{constructor(t,r){super(t,r,C.ARC,gt)}Kt(t,r){const i=t.x-t.width/2,n=t.y-t.height/2,o=t.start*Math.PI/180,a=t.stop*Math.PI/180,h=this.De(i,n,t.width,t.height,r);return this.Be(h,t.x,t.y),h.arcAngles=[o,a],this.Se.Kt(h)}}const xt={vertices:new Float32Array([0,0,0,0,1,0,1,0,.5,1,.5,1]),vertexCount:3,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class yt extends M{constructor(t,r){super(t,r,C.TRIANGLE,xt)}Kt(t,r){const i=Math.min(t.x1,t.x2,t.x3),n=Math.max(t.x1,t.x2,t.x3),o=Math.min(t.y1,t.y2,t.y3),a=n-i,h=Math.max(t.y1,t.y2,t.y3)-o,l=this.De(i,o,a,h,r),f=i+.5*a,g=o+h*(1/3);return this.Be(l,f,g),this.Se.Kt(l)}}function Q(c,t,r,i,n){const o=1-c,a=o*o,h=c*c;return a*o*t+3*a*c*r+3*o*h*i+h*c*n}const Ct={vertices:function(c=16){const t=[];for(let r=0;r<c;r++){const i=r/c,n=(r+1)/c;t.push(i,-.5,i,0),t.push(n,-.5,n,0),t.push(i,.5,i,1),t.push(i,.5,i,1),t.push(n,-.5,n,0),t.push(n,.5,n,1)}return new Float32Array(t)}(16),vertexCount:96,primitiveType:WebGL2RenderingContext.TRIANGLES,stride:16,attributes:{position:{size:2,offset:0},texCoord:{size:2,offset:8}}};class vt extends M{constructor(t,r){super(t,r,C.BEZIER_CURVE,Ct)}Kt(t,r){const i=r.lineWeight||1,n=Q(.5,t.x1,t.cp1x,t.cp2x,t.x2),o=Q(.5,t.y1,t.cp1y,t.cp2y,t.y2),a={character:r.character,charColor:r.charColor,bgColor:r.bgColor,charRotation:r.charRotation,flipHorizontally:r.flipHorizontally,flipVertically:r.flipVertically,invert:r.invert,rotationX:r.rotationX||0,rotationY:r.rotationY||0,rotationZ:r.rotationZ||0,lineWeight:i},h=this.De(0,0,1,i,a);return this.Be(h,n,o),h.bezierStartPoint=[t.x1,t.y1],h.bezierControlPoint1=[t.cp1x,t.cp1y],h.bezierControlPoint2=[t.cp2x,t.cp2y],h.bezierEndPoint=[t.x2,t.y2],this.Se.Kt(h)}}class St{constructor(t){u(this,"S");u(this,"Ie",null);u(this,"Le",null);u(this,"Ee",{});u(this,"We",null);u(this,"ke",new Map);u(this,"Ue");u(this,"Ve");u(this,"Oe");this.S=t,this.Oe=new st,this.Ue=new ot(t),this.Ve=new at,this.We=t.createBuffer(),G(this.S,[0,0,this.S.canvas.width,this.S.canvas.height])}je(t){let r=this.ke.get(t);if(r)return r;const i=new ht(this.S);return r=(0,{[C.RECTANGLE]:()=>new lt(this.S,i),[C.LINE]:()=>new ft(this.S,i),[C.ELLIPSE]:()=>new pt(this.S,i),[C.ARC]:()=>new mt(this.S,i),[C.TRIANGLE]:()=>new yt(this.S,i),[C.BEZIER_CURVE]:()=>new vt(this.S,i)}[t])(),this.ke.set(t,r),r}He(t){this.Ie!==t&&(this.Ie=t,t.tt())}Xe(t,r){return new V(this.S,t,r)}Ye(t){this.Le=t,t&&(this.Ee={})}qe(t,r){this.Ee[t]=r}Ze(t){Object.assign(this.Ee,t)}Ne(t){return new V(this.S,X,t)}Qe(t,r,i,n){var y;const o=this.S,a=o.canvas.width,h=o.canvas.height,l=t/a*2-1,f=(t+i)/a*2-1,g=1-r/h*2,d=1-(r+n)/h*2,p=new Float32Array([l,d,f,d,l,g,f,d,f,g,l,g]);o.bindBuffer(o.ARRAY_BUFFER,this.We),o.bufferData(o.ARRAY_BUFFER,p,o.DYNAMIC_DRAW);const x=((y=this.Ie)==null?void 0:y.ot)||o.getParameter(o.CURRENT_PROGRAM),m=x?o.getAttribLocation(x,"a_position"):-1;m!==-1&&(o.enableVertexAttribArray(m),o.vertexAttribPointer(m,2,o.FLOAT,!1,8,0)),o.drawArrays(o.TRIANGLES,0,6),m!==-1&&o.disableVertexAttribArray(m)}Je(t,r,i,n){this.Le?(this.Ve.ae(t,r,i,n,this.Le,{...this.Ee},this.Oe),this.Le=null,this.Ee={}):this.Ve.oe(t,r,i,n,this.Oe)}Ke(t,r,i,n){this.Ve.he(t,r,i,n,this.Oe.lineWeight,this.Oe)}ts(t,r,i,n){this.Ve.ce(t,r,i,n,this.Oe)}es(t,r,i,n,o,a){this.Ve.ue(t,r,i,n,o,a,this.Oe)}ss(t,r,i,n,o,a,h,l){const f=this.Oe.lineWeight;this.Ve.fe(t,r,i,n,o,a,h,l,f,this.Oe)}rs(t,r,i=1,n={}){return new N(this.S,t,r,i,n)}ns(t,r,i,n,o,a){this.Ve.le(t,r,i,n,o,a,this.Oe)}hs(t,r=t,i=t,n=255){this.state.Et(t,r,i,n),this.de(t/255,r/255,i/255,n/255)}de(t=0,r=0,i=0,n=0){this.S.clearColor(t,r,i,n),this.S.clear(this.S.COLOR_BUFFER_BIT)}cs(){this.S.viewport(0,0,this.S.canvas.width,this.S.canvas.height),G(this.S,[0,0,this.S.canvas.width,this.S.canvas.height])}get context(){return this.S}get state(){return this.Oe}ls(t){const r=t,i=D(this.S)??this.S.getParameter(this.S.VIEWPORT),n={shader:r,gl:this.S,viewport:i};this.He(r);const o=new Set;for(const a of this.Ve)a.type===C.CUSTOM?o.add(C.RECTANGLE):o.add(a.type);for(const a of o)a!==C.CUSTOM&&this.je(a);this.Ue.Xt(n,this.Ve,this.ke),this.Ve.de()}j(){this.S.deleteBuffer(this.We),this.Ve.de();for(const t of this.ke.values())t.j()}}const _={readShort:(c,t)=>(_.t.uint16[0]=c[t]<<8|c[t+1],_.t.int16[0]),readUshort:(c,t)=>c[t]<<8|c[t+1],readUshorts(c,t,r){const i=[];for(let n=0;n<r;n++)i.push(_.readUshort(c,t+2*n));return i},readUint(c,t){const r=_.t.uint8;return r[3]=c[t],r[2]=c[t+1],r[1]=c[t+2],r[0]=c[t+3],_.t.uint32[0]},readASCII(c,t,r){let i="";for(let n=0;n<r;n++)i+=String.fromCharCode(c[t+n]);return i},t:(()=>{const c=new ArrayBuffer(8);return{uint8:new Uint8Array(c),int16:new Int16Array(c),uint16:new Uint16Array(c),uint32:new Uint32Array(c)}})()},Rt={parseTab(c,t,r){const i={tables:[],ids:{},off:t};c=new Uint8Array(c.buffer,t,r),t=0;const n=_,o=n.readUshort,a=o(c,t+=2);t+=2;const h=[];for(let l=0;l<a;l++){const f=o(c,t),g=o(c,t+=2);t+=2;const d=n.readUint(c,t);t+=4;const p=`p${f}e${g}`;let x=h.indexOf(d);if(x===-1){let m;x=i.tables.length,h.push(d);const y=o(c,d);m=y===4?this.parse4(c,d):y===12?this.parse12(c,d):{format:y},i.tables.push(m)}i.ids[p]!=null&&console.warn("Multiple tables for one platform+encoding: "+p),i.ids[p]=x}return i},parse4(c,t){const r=_,i=r.readUshort,n=r.readUshorts,o=t,a=i(c,t+=2);t+=2;const h=i(c,t+=2)>>>1,l={format:4,searchRange:i(c,t+=2),entrySelector:0,rangeShift:0,endCount:[],startCount:[],idDelta:[],idRangeOffset:[],glyphIdArray:[]};t+=2,l.entrySelector=i(c,t),t+=2,l.rangeShift=i(c,t),t+=2,l.endCount=n(c,t,h),t+=2*h,t+=2,l.startCount=n(c,t,h),t+=2*h;for(let f=0;f<h;f++)l.idDelta.push(r.readShort(c,t)),t+=2;return l.idRangeOffset=n(c,t,h),t+=2*h,l.glyphIdArray=n(c,t,o+a-t>>1),l},parse12(c,t){const r=_.readUint;r(c,t+=4),r(c,t+=4);const i=r(c,t+=4);t+=4;const n=new Uint32Array(3*i);for(let o=0;o<3*i;o+=3)n[o]=r(c,t+(o<<2)),n[o+1]=r(c,t+(o<<2)+4),n[o+2]=r(c,t+(o<<2)+8);return{format:12,groups:n}}},bt={parseTab(c,t,r){const i=_;t+=18;const n=i.readUshort(c,t);t+=2,t+=16;const o=i.readShort(c,t);t+=2;const a=i.readShort(c,t);t+=2;const h=i.readShort(c,t);t+=2;const l=i.readShort(c,t);return t+=2,t+=6,{unitsPerEm:n,xMin:o,yMin:a,xMax:h,yMax:l,indexToLocFormat:i.readShort(c,t)}}},Et={parseTab(c,t,r){const i=_;t+=4;const n=["ascender","descender","lineGap","advanceWidthMax","minLeftSideBearing","minRightSideBearing","xMaxExtent","caretSlopeRise","caretSlopeRun","caretOffset","res0","res1","res2","res3","metricDataFormat","numberOfHMetrics"],o={};for(let a=0;a<n.length;a++){const h=n[a],l=h==="advanceWidthMax"||h==="numberOfHMetrics"?i.readUshort:i.readShort;o[h]=l(c,t+2*a)}return o}},_t={parseTab(c,t,r,i){const n=_,o=[],a=[],h=i.maxp.numGlyphs,l=i.hhea.numberOfHMetrics;let f=0,g=0,d=0;for(;d<l;)f=n.readUshort(c,t+(d<<2)),g=n.readShort(c,t+(d<<2)+2),o.push(f),a.push(g),d++;for(;d<h;)o.push(f),a.push(g),d++;return{aWidth:o,lsBearing:a}}},Z={cmap:Rt,head:bt,hhea:Et,maxp:{parseTab(c,t,r){const i=_;return i.readUint(c,t),t+=4,{numGlyphs:i.readUshort(c,t)}}},hmtx:_t,loca:{parseTab(c,t,r,i){const n=_,o=[],a=i.head.indexToLocFormat,h=i.maxp.numGlyphs+1;if(a===0)for(let l=0;l<h;l++)o.push(n.readUshort(c,t+(l<<1))<<1);else if(a===1)for(let l=0;l<h;l++)o.push(n.readUint(c,t+(l<<2)));return o}},glyf:{parseTab(c,t,r,i){const n=[],o=i.maxp.numGlyphs;for(let a=0;a<o;a++)n.push(null);return n},us(c,t){const r=_,i=c.fs,n=c.loca;if(n[t]===n[t+1])return null;const o=F.findTable(i,"glyf",c.ds);if(!o)return null;let a=o[0]+n[t];const h={};if(h.noc=r.readShort(i,a),a+=2,h.xMin=r.readShort(i,a),a+=2,h.yMin=r.readShort(i,a),a+=2,h.xMax=r.readShort(i,a),a+=2,h.yMax=r.readShort(i,a),a+=2,h.xMin>=h.xMax||h.yMin>=h.yMax)return null;if(h.noc>0){h.endPts=[];for(let p=0;p<h.noc;p++)h.endPts.push(r.readUshort(i,a)),a+=2;const l=r.readUshort(i,a);if(a+=2,i.length-a<l)return null;a+=l;const f=h.endPts[h.noc-1]+1;h.flags=[];for(let p=0;p<f;p++){const x=i[a];if(a++,h.flags.push(x),8&x){const m=i[a];a++;for(let y=0;y<m;y++)h.flags.push(x),p++}}h.xs=[];for(let p=0;p<f;p++){const x=h.flags[p],m=!!(16&x);2&x?(h.xs.push(m?i[a]:-i[a]),a++):m?h.xs.push(0):(h.xs.push(r.readShort(i,a)),a+=2)}h.ys=[];for(let p=0;p<f;p++){const x=h.flags[p],m=!!(32&x);4&x?(h.ys.push(m?i[a]:-i[a]),a++):m?h.ys.push(0):(h.ys.push(r.readShort(i,a)),a+=2)}let g=0,d=0;for(let p=0;p<f;p++)g+=h.xs[p],d+=h.ys[p],h.xs[p]=g,h.ys[p]=d}else h.parts=[],h.endPts=[],h.flags=[],h.xs=[],h.ys=[];return h}}},F={parse:c=>[((t,r,i,n)=>{const o=Z,a={fs:t,ps:r,ds:i};for(const h in o){const l=h,f=F.findTable(t,l,i);if(f){const[g,d]=f;let p=n[g];p==null&&(p=o[l].parseTab(t,g,d,a),n[g]=p),a[l]=p}}return a})(new Uint8Array(c),0,0,{})],findTable(c,t,r){const i=_,n=i.readUshort(c,r+4);let o=r+12;for(let a=0;a<n;a++){const h=i.readASCII(c,o,4);i.readUint(c,o+4);const l=i.readUint(c,o+8),f=i.readUint(c,o+12);if(h===t)return[l,f];o+=16}return null},T:Z,B:_};class O{constructor(){u(this,"_s",new Map);u(this,"gs",new Map)}vs(t,r){const i=`${this.Cs(t)}_${r}`;if(this._s.has(i))return this._s.get(i);const n=t.cmap;if(!n||!n.tables)return this._s.set(i,0),0;let o=0;for(const a of n.tables)if(a.format===4?o=this.ws(r,a):a.format===12&&(o=this.$s(r,a)),o>0)break;return this._s.set(i,o),o}bs(t,r){const i=r.codePointAt(0);return i===void 0?0:this.vs(t,i)}zs(t,r){const i=t.hmtx;return i&&i.aWidth&&i.aWidth.length!==0?r<i.aWidth.length?i.aWidth[r]:i.aWidth[i.aWidth.length-1]:0}Rs(t,r){const i=r/t.head.unitsPerEm,n=t.hhea.ascender*i,o=t.hhea.descender*i,a=t.hhea.lineGap*i;return{ascender:n,descender:o,lineGap:a,lineHeight:n-o+a,unitsPerEm:t.head.unitsPerEm,scale:i}}Ts(){this._s.clear(),this.gs.clear()}Cs(t){return`${t.ds}_${t.fs.length}`}ws(t,r){const i=r.endCount.length;let n=-1;for(let o=0;o<i;o++)if(t<=r.endCount[o]){n=o;break}if(n===-1||t<r.startCount[n])return 0;if(r.idRangeOffset[n]===0)return t+r.idDelta[n]&65535;{const o=r.idRangeOffset[n]/2+(t-r.startCount[n])-(i-n);if(o>=0&&o<r.glyphIdArray.length){const a=r.glyphIdArray[o];return a===0?0:a+r.idDelta[n]&65535}}return 0}$s(t,r){const i=r.groups.length/3;for(let n=0;n<i;n++){const o=r.groups[3*n],a=r.groups[3*n+1],h=r.groups[3*n+2];if(t>=o&&t<=a)return h+(t-o)}return 0}}class wt{constructor(t){u(this,"Ss");this.Ss=t}Ms(t){var i;const r=[];return(i=t.cmap)!=null&&i.tables?(t.cmap.tables.forEach(n=>{if(n.format===4){const o=this.Fs(n);r.push(...o)}else if(n.format===12){const o=this.Gs(n);r.push(...o)}}),[...new Set(r)]):[]}Ds(t,r){return this.Ss.bs(t,r)>0}Ps(t,r){for(const i of r)if(!this.Ds(t,i))return!1;return!0}As(t,r){return r.filter(i=>this.Ds(t,i))}Bs(t){return t.filter(r=>this.Is(r))}Fs(t){const r=[];if(!(t.startCount&&t.endCount&&t.idRangeOffset&&t.idDelta))return r;for(let i=0;i<t.startCount.length;i++){const n=t.startCount[i],o=t.endCount[i];if(n!==65535||o!==65535){for(let a=n;a<=o;a++)if(this.Ls(t,a,i)>0)try{const h=String.fromCodePoint(a);r.push(h)}catch{}}}return r}Gs(t){const r=[];if(!t.groups)return r;for(let i=0;i<t.groups.length;i+=3){const n=t.groups[i],o=t.groups[i+1],a=t.groups[i+2];for(let h=n;h<=o;h++)if(a+(h-n)>0)try{const l=String.fromCodePoint(h);r.push(l)}catch{}}return r}Ls(t,r,i){if(t.idRangeOffset[i]===0)return r+t.idDelta[i]&65535;{const n=t.idRangeOffset[i]/2+(r-t.startCount[i])-(t.startCount.length-i);if(n>=0&&t.glyphIdArray&&n<t.glyphIdArray.length){const o=t.glyphIdArray[n];if(o!==0)return o+t.idDelta[i]&65535}}return 0}Is(t){const r=t.codePointAt(0)||0;return!(r>=0&&r<=31&&r!==9&&r!==10&&r!==13||r>=127&&r<=159)}}class Tt{constructor(){u(this,"Es");const t=new O;this.Es=new wt(t)}extractCharacters(t){return this.Es.Ms(t)}filterProblematicCharacters(t){return this.Es.Bs(t)}characterExists(t,r){return this.Es.Ds(t,r)}allCharactersExist(t,r){return this.Es.Ps(t,r)}}class At{constructor(t){u(this,"Ws");u(this,"ks");u(this,"Us");u(this,"Vs");this.Us=t,this.Vs=new O,this.Ws=document.createElement("canvas"),this.ks=this.Ws.getContext("2d",{willReadFrequently:!0,alpha:!1})}createTextureAtlas(t,r,i,n){const o=t.length,a=Math.ceil(Math.sqrt(o)),h=Math.ceil(o/a),l=r.width*a,f=r.height*h,g=typeof n=="object"?n:null;this.Os(l,f),this.js(t,r,a,i,g);const d=this.Us.rs(l,f,1,{filter:"nearest"});return d.L(this.Ws),{framebuffer:d,columns:a,rows:h}}Os(t,r){this.Ws.width=t,this.Ws.height=r,this.Ws.style.width=t+"px",this.Ws.style.height=t+"px",this.ks.imageSmoothingEnabled=!1,this.Ws.style.imageRendering="pixelated",this.ks.fillStyle="black",this.ks.fillRect(0,0,t,r),this.ks.textBaseline="top",this.ks.textAlign="left",this.ks.fillStyle="white"}js(t,r,i,n,o){const a=n/o.head.unitsPerEm;for(let h=0;h<t.length;h++){const l=h%i,f=Math.floor(h/i),g=t[h].character,d=this.Hs(o,g);if(!d)continue;const p=g.codePointAt(0)||0,x=this.Vs.vs(o,p),m=this.Xs(o,x)*a,y=l*r.width,w=f*r.height,E=y+.5*r.width,T=w+.5*r.height,A=Math.round(E-.5*r.width),R=Math.round(T-.5*n),B=A+.5*(r.width-m),Jt=R+o.hhea.ascender*a;this.Ys(d,B,Jt,a)}}Hs(t,r){const i=r.codePointAt(0)||0,n=this.Vs.vs(t,i);if(n===0)return null;if(t.glyf&&t.glyf[n]!==null)return t.glyf[n];if(F&&F.T&&F.T.glyf){const o=F.T.glyf.us(t,n);return t.glyf&&o&&(t.glyf[n]=o),o}return null}Xs(t,r){const i=t.hmtx;return i&&i.aWidth?r<i.aWidth.length?i.aWidth[r]:i.aWidth[i.aWidth.length-1]:0}Ys(t,r,i,n){if(!t||!t.xs||t.noc===0)return;const{xs:o,ys:a,endPts:h,flags:l}=t;if(!(o&&a&&h&&l))return;this.ks.beginPath();let f=0;for(let g=0;g<h.length;g++){const d=h[g];if(!(d<f)){if(d>=f){const p=r+o[f]*n,x=i-a[f]*n;this.ks.moveTo(p,x);let m=f+1;for(;m<=d;)if(1&l[m]){const y=r+o[m]*n,w=i-a[m]*n;this.ks.lineTo(y,w),m++}else{const y=r+o[m]*n,w=i-a[m]*n;let E=m+1>d?f:m+1;if(1&l[E]){const T=r+o[E]*n,A=i-a[E]*n;this.ks.quadraticCurveTo(y,w,T,A),m=E+1}else{const T=(y+(r+o[E]*n))/2,A=(w+(i-a[E]*n))/2;this.ks.quadraticCurveTo(y,w,T,A),m=E}}this.ks.closePath()}f=d+1}}this.ks.fill()}}class Ft{constructor(){u(this,"Ss");this.Ss=new O}calculateMaxGlyphDimensions(t,r,i){let n=0;const o=this.Ss.Rs(i,r),a=o.lineHeight;for(const h of t){const l=this.Ss.bs(i,h);if(l===0)continue;const f=this.Ss.zs(i,l)*o.scale;n=Math.max(n,f)}return{width:Math.ceil(n),height:Math.ceil(a)}}getCharacterAdvanceWidth(t,r,i){const n=this.Ss.Rs(i,r),o=this.Ss.bs(i,t);return this.Ss.zs(i,o)*n.scale}getFontMetrics(t,r){return this.Ss.Rs(r,t)}Ts(){this.Ss.Ts()}}class Ut{constructor(){u(this,"Vs");this.Vs=new O}createCharacterObjects(t,r){return t.map((i,n)=>{const o=i.codePointAt(0)||0,a=this.qs(n);let h=0;if(r.hmtx&&r.hmtx.aWidth){const l=this.Vs.vs(r,o);l>0&&r.hmtx.aWidth[l]!==void 0&&(h=r.hmtx.aWidth[l])}return{character:i,unicode:o,color:a,advanceWidth:h}})}qs(t){return[t%256/255,Math.floor(t/256)%256/255,Math.floor(t/65536)%256/255]}Zs(t,r){if(!z.m(typeof t=="string","Character must be a string.",{method:"getCharacterColor",providedValue:t}))return[0,0,0];const i=r.find(n=>n.character===t);return i?i.color:[0,0,0]}Ns(t,r){return z.m(typeof t=="string"&&t.length>0,"Characters must be a string with at least one character.",{method:"getCharacterColors",providedValue:t})?Array.from(t).map(i=>this.Zs(i,r)||[0,0,0]):[[0,0,0]]}}class K{constructor(t,r=16){u(this,"Qs");u(this,"Js",[]);u(this,"Ks");u(this,"ti",16);u(this,"ei",0);u(this,"si",0);u(this,"ii",{width:0,height:0});u(this,"ri");u(this,"ni");u(this,"oi");u(this,"ai");u(this,"hi");this.ti=r,this.ni=new Tt,this.oi=new At(t),this.ai=new Ft,this.hi=new Ut}async ci(t){let r;if(!t)throw new v("Embedded font not available. This appears to be a minified build - please provide `fontSource`.");{const i=await fetch(t);if(!i.ok)throw new v(`Failed to load font file: ${i.status} ${i.statusText}`);r=await i.arrayBuffer()}await this.li(r),this.Qs=F.parse(r)[0],await this.ui()}fi(t){if(t===void 0)return this.ti;this.ti=t,this.ii=this.ai.calculateMaxGlyphDimensions(this.Js.map(i=>i.character),this.ti,this.Qs);const r=this.oi.createTextureAtlas(this.Js,this.ii,this.ti,this.Qs);this.Ks=r.framebuffer,this.ei=r.columns,this.si=r.rows}async di(t){try{const r=await fetch(t);if(!r.ok)throw new v(`Failed to load font file: ${r.status} ${r.statusText}`);const i=await r.arrayBuffer();await this.li(i);const n=F.parse(i);if(!n||n.length===0)throw Error("Failed to parse font file");this.Qs=n[0],await this.ui()}catch(r){throw new v("Failed to load font: "+(r instanceof Error?r.message:"Unknown error"),r)}}async li(t){const r=Date.now();this.ri=new FontFace("CustomFont_"+r,t),await this.ri.load(),document.fonts.add(this.ri)}async ui(){const t=this.ni.extractCharacters(this.Qs),r=this.ni.filterProblematicCharacters(t);this.Js=this.hi.createCharacterObjects(r,this.Qs),this.ii=this.ai.calculateMaxGlyphDimensions(r,this.ti,this.Qs);const i=this.oi.createTextureAtlas(this.Js,this.ii,this.ti,this.Qs);this.Ks=i.framebuffer,this.ei=i.columns,this.si=i.rows}Zs(t){return this.hi.Zs(t,this.Js)}Ns(t){return this.hi.Ns(t,this.Js)}j(){this.Ks.j(),document.fonts.delete(this.ri)}get fontFramebuffer(){return this.Ks}get characters(){return this.Js}get textureColumns(){return this.ei}get textureRows(){return this.si}get maxGlyphDimensions(){return this.ii}get fontSize(){return this.ti}get font(){return this.Qs}}class q{constructor(t,r,i){u(this,"pi");u(this,"_i");u(this,"C");u(this,"$");u(this,"gi");u(this,"mi");u(this,"xi");u(this,"Ci");u(this,"yi");this.xi=t,this.Ci=r,this.yi=i,this.zt()}zt(){this.pi=Math.floor(this.xi.width/this.Ci),this._i=Math.floor(this.xi.height/this.yi),this.C=this.pi*this.Ci,this.$=this._i*this.yi,this.gi=Math.floor((this.xi.width-this.C)/2),this.mi=Math.floor((this.xi.height-this.$)/2)}wi(t,r){this.Ci=t,this.yi=r,this.zt()}get cellWidth(){return this.Ci}get cellHeight(){return this.yi}get cols(){return this.pi}get rows(){return this._i}get width(){return this.C}get height(){return this.$}get offsetX(){return this.gi}get offsetY(){return this.mi}}class J{constructor(t={}){u(this,"xi");u(this,"$i");u(this,"bi");t.canvas?(this.xi=t.canvas,this.bi=!1):(this.xi=this.zi(t.width,t.height),this.bi=!0),this.xi.style.imageRendering="pixelated"}zi(t,r){const i=document.createElement("canvas");return i.className="textmodeCanvas",i.style.imageRendering="pixelated",i.width=t||800,i.height=r||600,document.body.appendChild(i),i}W(t,r){this.xi.width=t??this.xi.width,this.xi.height=r??this.xi.height}Ri(){const t=this.xi.getContext("webgl2",{alpha:!0,premultipliedAlpha:!1,preserveDrawingBuffer:!0,antialias:!1,depth:!1,stencil:!1,powerPreference:"high-performance"});if(!t)throw new v("`textmode.js` requires WebGL2 support.");return t}j(){this.$i&&this.$i.disconnect();const t=this.xi.getContext("webgl")||this.xi.getContext("webgl2");if(t){const r=t.getExtension("WEBGL_lose_context");r&&r.loseContext()}this.bi&&this.xi.parentNode&&this.xi.parentNode.removeChild(this.xi)}get canvas(){return this.xi}get width(){return this.xi.width}get height(){return this.xi.height}}class Lt{constructor(t=60){u(this,"Ti");u(this,"Si");u(this,"Mi",null);u(this,"Fi",0);u(this,"Gi",!0);u(this,"Di",0);u(this,"Pi",0);u(this,"Ai",[]);u(this,"Bi",10);u(this,"Ii",0);this.Ti=t,this.Si=1e3/t}start(t){if(!this.Gi)return;this.Fi=performance.now();const r=i=>{if(!this.Gi)return void(this.Mi=null);const n=i-this.Fi;n>=this.Si&&(t(),this.Fi=i-n%this.Si),this.Gi&&(this.Mi=requestAnimationFrame(r))};this.Mi=requestAnimationFrame(r)}stop(){this.Mi&&(cancelAnimationFrame(this.Mi),this.Mi=null)}pause(){this.Gi&&(this.Gi=!1,this.stop())}resume(t){this.Gi||(this.Gi=!0,this.start(t))}frameRate(t,r){if(t===void 0)return this.Di;this.Ti=t,this.Si=1e3/t,this.Gi&&r&&(this.stop(),this.start(r))}measureFrameRate(){const t=performance.now();if(this.Pi>0){const r=t-this.Pi;this.Ai.push(r),this.Ai.length>this.Bi&&this.Ai.shift();const i=this.Ai.reduce((n,o)=>n+o,0)/this.Ai.length;this.Di=1e3/i}this.Pi=t}get isLooping(){return this.Gi}get frameRateLimit(){return this.Ti}get currentFrameRate(){return this.Di}get frameCount(){return this.Ii}set frameCount(t){this.Ii=t}incrementFrame(){this.Ii++}resetFrameCount(){this.Ii=0}}const Pt=c=>class extends c{rotate(t=0,r=0,i=0){this.Us.state.St(t),this.Us.state.Mt(r),this.Us.state.Ft(i)}rotateX(t){this.Us.state.St(t)}rotateY(t){this.Us.state.Mt(t)}rotateZ(t){this.Us.state.Ft(t)}push(){this.Us.state.$t()}pop(){this.Us.state.bt()}rect(t,r,i=1,n=1){this.Us.Je(t,r,i,n)}line(t,r,i,n){this.Us.Ke(t,r,i,n)}lineWeight(t){this.Us.state.Tt(t)}background(t,r=t,i=t,n=255){this.Us.hs(t,r,i,n)}char(t){this.Us.state.Gt(this.Qs.Zs(t))}charColor(t,r,i){this.Us.state.Dt(t,r,i)}cellColor(t,r,i){this.Us.state.Pt(t,r,i)}flipX(t){this.Us.state.At(t)}flipY(t){this.Us.state.Bt(t)}charRotation(t){this.Us.state.Lt(t)}invert(t){this.Us.state.It(t)}clear(){this.Us.hs(0,0,0,0)}ellipse(t,r,i,n){this.Us.ts(t,r,i/2,n/2)}triangle(t,r,i,n,o,a){this.Us.es(t,r,i,n,o,a)}bezierCurve(t,r,i,n,o,a,h,l){this.Us.ss(t,r,i,n,o,a,h,l)}arc(t,r,i,n,o,a){this.Us.ns(t,r,i,n,o,a)}shader(t){this.Us.Ye(t)}setUniform(t,r){this.Us.qe(t,r)}setUniforms(t){this.Us.Ze(t)}createFilterShader(t){return this.Us.Ne(t)}};class j{Li(t){const r=t.k(0),i=t.k(1),n=t.k(2),o=t.k(3);return{characterPixels:r,primaryColorPixels:i,secondaryColorPixels:n,transformPixels:t.k(4),rotationPixels:o}}Ei(t,r){return t[r]+(t[r+1]<<8)}Wi(t,r){return{r:t[r],g:t[r+1],b:t[r+2],a:t[r+3]}}}class Y{ki(t,r){return new Blob([t],{type:r})}Ui(t,r,i){try{const n=this.ki(t,i),o=URL.createObjectURL(n),a=document.createElement("a");a.href=o,a.download=r,a.style.display="none",a.rel="noopener",document.body.appendChild(a),a.click(),document.body.removeChild(a),URL.revokeObjectURL(o)}catch(n){throw console.error("Failed to download file:",n),Error("File download failed: "+(n instanceof Error?n.message:"Unknown error"))}}Vi(){return new Date().toISOString().slice(0,19).replace(/:/g,"-")}Oi(){const t=new Date;return{date:t.toISOString().split("T")[0],time:t.toTimeString().split(" ")[0].replace(/:/g,"-")}}ji(t){return t.replace(/[<>:"/\\|?*]/g,"_").replace(/\s+/g,"_").replace(/_{2,}/g,"_").replace(/^_+|_+$/g,"").substring(0,255)}Hi(){return"textmode-export-"+this.Vi()}}class It extends j{Xi(t,r,i){const n=t[i]===255,o=t[i+1]===255,a=t[i+2]===255,h=r[i],l=r[i+1];return{isInverted:n,flipHorizontal:o,flipVertical:a,rotation:Math.round(360*(h+l/255)/255*100)/100}}Yi(t,r,i){return{x:t,y:r,cellX:t*i.cellWidth,cellY:r*i.cellHeight}}qi(t,r){const i=[];let n=0;for(let o=0;o<r.rows;o++)for(let a=0;a<r.cols;a++){const h=4*n,l=this.Ei(t.characterPixels,h);let f=this.Wi(t.primaryColorPixels,h),g=this.Wi(t.secondaryColorPixels,h);const d=this.Xi(t.transformPixels,t.rotationPixels,h);if(d.isInverted){const x=f;f=g,g=x}const p=this.Yi(a,o,r);i.push({charIndex:l,primaryColor:f,secondaryColor:g,transform:d,position:p}),n++}return i}}class Dt{Zi(t,r){const i=t.cmap;for(const n of i.tables)if(n.format===4){const o=n;for(let a=0;a<o.startCount.length;a++)if(r>=o.startCount[a]&&r<=o.endCount[a]){if(o.idRangeOffset[a]===0)return r+o.idDelta[a]&65535;{const h=o.idRangeOffset[a]/2+(r-o.startCount[a])-(o.startCount.length-a);if(h>=0&&h<o.glyphIdArray.length){const l=o.glyphIdArray[h];if(l!==0)return l+o.idDelta[a]&65535}}}}else if(n.format===12){const o=n;for(let a=0;a<o.groups.length;a+=3){const h=o.groups[a],l=o.groups[a+1],f=o.groups[a+2];if(r>=h&&r<=l)return f+(r-h)}}return 0}Ni(t,r,i,n,o){const a=o/t.head.unitsPerEm;return{getBoundingBox:()=>({x1:i+r.xMin*a,y1:n+-r.yMax*a,x2:i+r.xMax*a,y2:n+-r.yMin*a}),toSVG:()=>this.Qi(r,i,n,a)}}Qi(t,r,i,n){if(!t||!t.xs)return"";const{xs:o,ys:a,endPts:h,flags:l}=t;if(!(o&&a&&h&&l))return"";let f="",g=0;for(let d=0;d<h.length;d++){const p=h[d];if(!(p<g)){if(p>=g){const x=r+o[g]*n,m=i-a[g]*n;f+=`M${x.toFixed(2)},${m.toFixed(2)}`;let y=g+1;for(;y<=p;)if(1&l[y]){const w=r+o[y]*n,E=i-a[y]*n;f+=`L${w.toFixed(2)},${E.toFixed(2)}`,y++}else{const w=r+o[y]*n,E=i-a[y]*n;let T=y+1>p?g:y+1;if(1&l[T]){const A=r+o[T]*n,R=i-a[T]*n;f+=`Q${w.toFixed(2)},${E.toFixed(2)} ${A.toFixed(2)},${R.toFixed(2)}`,y=T+1}else{const A=(w+(r+o[T]*n))/2,R=(E+(i-a[T]*n))/2;f+=`Q${w.toFixed(2)},${E.toFixed(2)} ${A.toFixed(2)},${R.toFixed(2)}`,y=T}}f+="Z"}g=p+1}}return f}Ji(t,r,i,n,o){const a=t.codePointAt(0)||0,h=this.Zi(r,a);let l=null;return r.glyf&&r.glyf[h]!==null?l=r.glyf[h]:(l=F.T.glyf.us(r,h),r.glyf[h]=l),this.Ni(r,l,i,n,o)}Ki(t,r,i,n,o,a,h,l){const f=i+(o-l*(h/r.head.unitsPerEm))/2,g=n+(a+.7*h)/2;return this.Ji(t,r,f,g,h).toSVG()||null}}class Mt{constructor(){u(this,"tr");this.tr=new Dt}er(t){return`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
10
10
  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
11
- <svg width="${r.width}" height="${r.height}" viewBox="0 0 ${r.width} ${r.height}"
11
+ <svg width="${t.width}" height="${t.height}" viewBox="0 0 ${t.width} ${t.height}"
12
12
  xmlns="http://www.w3.org/2000/svg" version="1.1">
13
13
  <title>textmode art generated via textmode.js</title>
14
14
  <desc>textmode art visualization generated by textmode.js library</desc>`}sr(){return`
15
15
  </g>
16
- </svg>`}ir(r,i){if(!i.includeBackgroundRectangles)return"";const s=i.backgroundColor,n=`rgba(${s[0]},${s[1]},${s[2]},${s[3]/255})`;return`
17
- <rect width="${r.width}" height="${r.height}" fill="${n}" />`}rr(r){return`rgba(${r.r},${r.g},${r.b},${r.a/255})`}nr(r,i){const{transform:s,position:n}=r,o=n.cellX+i.cellWidth/2,a=n.cellY+i.cellHeight/2,h=[];if(s.flipHorizontal||s.flipVertical){const l=s.flipHorizontal?-1:1,f=s.flipVertical?-1:1;h.push(`translate(${o} ${a})`),h.push(`scale(${l} ${f})`),h.push(`translate(${-o} ${-a})`)}return s.rotation&&h.push(`rotate(${s.rotation} ${o} ${a})`),h.length?` transform="${h.join(" ")}"`:""}ar(r,i,s){if(!s.includeBackgroundRectangles||r.secondaryColor.a===0)return"";const{position:n}=r,o=this.rr(r.secondaryColor);return s.drawMode==="stroke"?`
18
- <rect x="${n.cellX}" y="${n.cellY}" width="${i.cellWidth}" height="${i.cellHeight}" stroke="${o}" fill="none" stroke-width="${s.strokeWidth}" />`:`
19
- <rect x="${n.cellX}" y="${n.cellY}" width="${i.cellWidth}" height="${i.cellHeight}" fill="${o}" />`}Ji(r,i,s,n){const o=s.characters[r.charIndex];if(!o)return"";const a=this.tr.Ki(o.character,s.font,r.position.cellX,r.position.cellY,i.cellWidth,i.cellHeight,s.fontSize,o.advanceWidth);if(!a)return"";const h=this.rr(r.primaryColor);return n.drawMode==="stroke"?`
20
- <path id="${`path-${r.charIndex}-${r.position.cellX}-${r.position.cellY}`.replace(/\./g,"-")}" d="${a}" stroke="${h}" stroke-width="${n.strokeWidth}" fill="none" />`:`
21
- <path d="${a}" fill="${h}" />`}hr(r,i,s,n){let o="";o+=this.ar(r,i,n);const a=this.nr(r,i),h=this.Ji(r,i,s,n);return h&&(a?(o+=`
16
+ </svg>`}ir(t,r){if(!r.includeBackgroundRectangles)return"";const i=r.backgroundColor,n=`rgba(${i[0]},${i[1]},${i[2]},${i[3]/255})`;return`
17
+ <rect width="${t.width}" height="${t.height}" fill="${n}" />`}rr(t){return`rgba(${t.r},${t.g},${t.b},${t.a/255})`}nr(t,r){const{transform:i,position:n}=t,o=n.cellX+r.cellWidth/2,a=n.cellY+r.cellHeight/2,h=[];if(i.flipHorizontal||i.flipVertical){const l=i.flipHorizontal?-1:1,f=i.flipVertical?-1:1;h.push(`translate(${o} ${a})`),h.push(`scale(${l} ${f})`),h.push(`translate(${-o} ${-a})`)}return i.rotation&&h.push(`rotate(${i.rotation} ${o} ${a})`),h.length?` transform="${h.join(" ")}"`:""}ar(t,r,i){if(!i.includeBackgroundRectangles||t.secondaryColor.a===0)return"";const{position:n}=t,o=this.rr(t.secondaryColor);return i.drawMode==="stroke"?`
18
+ <rect x="${n.cellX}" y="${n.cellY}" width="${r.cellWidth}" height="${r.cellHeight}" stroke="${o}" fill="none" stroke-width="${i.strokeWidth}" />`:`
19
+ <rect x="${n.cellX}" y="${n.cellY}" width="${r.cellWidth}" height="${r.cellHeight}" fill="${o}" />`}Ji(t,r,i,n){const o=i.characters[t.charIndex];if(!o)return"";const a=this.tr.Ki(o.character,i.font,t.position.cellX,t.position.cellY,r.cellWidth,r.cellHeight,i.fontSize,o.advanceWidth);if(!a)return"";const h=this.rr(t.primaryColor);return n.drawMode==="stroke"?`
20
+ <path id="${`path-${t.charIndex}-${t.position.cellX}-${t.position.cellY}`.replace(/\./g,"-")}" d="${a}" stroke="${h}" stroke-width="${n.strokeWidth}" fill="none" />`:`
21
+ <path d="${a}" fill="${h}" />`}hr(t,r,i,n){let o="";o+=this.ar(t,r,n);const a=this.nr(t,r),h=this.Ji(t,r,i,n);return h&&(a?(o+=`
22
22
  <g${a}>`,o+=h,o+=`
23
- </g>`):o+=h),o}cr(r,i,s,n){let o=this.er(i);o+=this.ir(i,n),o+=`
24
- <g id="ascii-cells">`;for(const a of r)o+=this.hr(a,i,s,n);return o+=this.sr(),o}lr(r){return r.replace(/<path[^>]*d=""[^>]*\/>/g,"").replace(/\n\s*\n/g,`
25
- `).replace(/[ \t]+$/gm,"")}}class zt extends Y{ur(r){return this.ki(r,"image/svg+xml;charset=utf-8")}dr(r,i){this.Ui(r,this.ji(i)+".svg","image/svg+xml;charset=utf-8")}pr(r,i){this.dr(r,i||this.Hi())}}class tt{constructor(){u(this,"_r");u(this,"gr");u(this,"mr");this._r=new It,this.gr=new Mt,this.mr=new zt}vr(r){return{includeBackgroundRectangles:r.includeBackgroundRectangles??!0,drawMode:r.drawMode??"fill",strokeWidth:r.strokeWidth??1,backgroundColor:r.backgroundColor??[0,0,0,0],filename:r.filename||this.mr.Hi()}}Cr(r,i={}){const s=this._r.qi(this._r.Li(r.pipeline),r.grid),n=this.gr.cr(s,r.grid,r.font,this.vr(i));return this.gr.lr(n)}pr(r,i={}){this.mr.pr(this.Cr(r,i),i.filename)}}class Bt extends X{yr(r,i,s,n=" "){var h;const o=[];let a=0;for(let l=0;l<i.rows;l++){const f=[];for(let g=0;g<i.cols;g++){const d=4*a,p=this.Ei(r.characterPixels,d),x=((h=s.characters[p])==null?void 0:h.character)||n;f.push(x),a++}o.push(f)}return o}}class Gt{wr(r,i){const s=[];for(const o of r){let a=o.join("");i.preserveTrailingSpaces||(a=a.replace(/\s+$/,"")),s.push(a)}const n=i.lineEnding==="crlf"?`\r
23
+ </g>`):o+=h),o}cr(t,r,i,n){let o=this.er(r);o+=this.ir(r,n),o+=`
24
+ <g id="ascii-cells">`;for(const a of t)o+=this.hr(a,r,i,n);return o+=this.sr(),o}lr(t){return t.replace(/<path[^>]*d=""[^>]*\/>/g,"").replace(/\n\s*\n/g,`
25
+ `).replace(/[ \t]+$/gm,"")}}class zt extends Y{ur(t){return this.ki(t,"image/svg+xml;charset=utf-8")}dr(t,r){this.Ui(t,this.ji(r)+".svg","image/svg+xml;charset=utf-8")}pr(t,r){this.dr(t,r||this.Hi())}}class tt{constructor(){u(this,"_r");u(this,"gr");u(this,"mr");this._r=new It,this.gr=new Mt,this.mr=new zt}vr(t){return{includeBackgroundRectangles:t.includeBackgroundRectangles??!0,drawMode:t.drawMode??"fill",strokeWidth:t.strokeWidth??1,backgroundColor:t.backgroundColor??[0,0,0,0],filename:t.filename||this.mr.Hi()}}Cr(t,r={}){const i=this._r.qi(this._r.Li(t.pipeline),t.grid),n=this.gr.cr(i,t.grid,t.font,this.vr(r));return this.gr.lr(n)}pr(t,r={}){this.mr.pr(this.Cr(t,r),r.filename)}}class Bt extends j{yr(t,r,i,n=" "){var h;const o=[];let a=0;for(let l=0;l<r.rows;l++){const f=[];for(let g=0;g<r.cols;g++){const d=4*a,p=this.Ei(t.characterPixels,d),x=((h=i.characters[p])==null?void 0:h.character)||n;f.push(x),a++}o.push(f)}return o}}class Gt{wr(t,r){const i=[];for(const o of t){let a=o.join("");r.preserveTrailingSpaces||(a=a.replace(/\s+$/,"")),i.push(a)}const n=r.lineEnding==="crlf"?`\r
26
26
  `:`
27
- `;return s.join(n)}}class $t extends Y{$r(r,i){const s=this.br(i);this.Ui(r,s,"text/plain;charset=utf-8")}br(r){let i=this.ji(r);return i===".txt"||i.length<=4?this.Hi():i}}class et{constructor(){u(this,"_r");u(this,"gr");u(this,"mr");this._r=new Bt,this.gr=new Gt,this.mr=new $t}vr(r){return{preserveTrailingSpaces:r.preserveTrailingSpaces??!1,lineEnding:r.lineEnding??"lf",emptyCharacter:r.emptyCharacter??" ",filename:r.filename||this.mr.Hi()}}zr(r,i={}){const s=this.vr(i),n=this._r.yr(this._r.Li(r.pipeline),r.grid,r.font,s.emptyCharacter);return this.gr.wr(n,s)}$r(r,i={}){this.mr.$r(this.zr(r,i),i.filename)}}class Ot extends X{Rr(r,i=1,s="transparent"){const n=r.canvas;if(i===1&&s==="transparent")return n;const o=document.createElement("canvas"),a=o.getContext("2d"),h=Math.round(n.width*i),l=Math.round(n.height*i);return o.width=h,o.height=l,s!=="transparent"&&(a.fillStyle=s,a.fillRect(0,0,h,l)),a.imageSmoothingEnabled=!1,a.drawImage(n,0,0,n.width,n.height,0,0,h,l),o}}class Wt{Tr(r,i){const s=this.Sr(i.format);return i.format==="png"?r.toDataURL(s):r.toDataURL(s,i.quality)}async Mr(r,i){return new Promise((s,n)=>{const o=this.Sr(i.format),a=h=>{h?s(h):n(Error(`Failed to generate ${i.format.toUpperCase()} blob`))};i.format==="png"?r.toBlob(a,o):r.toBlob(a,o,i.quality)})}Sr(r){switch(r){case"png":return"image/png";case"jpg":return"image/jpeg";case"webp":return"image/webp";default:throw Error("Unsupported image format: "+r)}}}const kt={png:"image/png",jpg:"image/jpeg",webp:"image/webp"},rt={png:".png",jpg:".jpg",webp:".webp"};class Nt extends Y{Fr(r,i,s){this.Gr(r,this.ji(i)+rt[s])}Gr(r,i){const s=URL.createObjectURL(r);try{const n=document.createElement("a");n.href=s,n.download=i,n.style.display="none",n.rel="noopener",document.body.appendChild(n),n.click(),document.body.removeChild(n)}finally{URL.revokeObjectURL(s)}}Dr(r){return r in kt&&r in rt}}class Vt{constructor(){u(this,"_r");u(this,"gr");u(this,"mr");this._r=new Ot,this.gr=new Wt,this.mr=new Nt}vr(r){return{format:r.format??"png",quality:r.quality??1,scale:r.scale??1,backgroundColor:r.backgroundColor??"transparent",filename:r.filename||this.mr.Hi()}}Pr(r){if(!this.mr.Dr(r.format))throw Error(`Saving '${r.format}' files is not supported`);if(r.quality<0||r.quality>1)throw Error("Image quality must be between 0.0 and 1.0");if(r.scale<=0)throw Error("Scale factor must be greater than 0");r.format==="jpg"&&r.backgroundColor==="transparent"&&(r.backgroundColor="black")}async Mr(r,i){if(i.scale===1&&i.backgroundColor==="transparent")return await this.gr.Mr(r.canvas,i);const s=this._r.Rr(r,i.scale,i.backgroundColor);return await this.gr.Mr(s,i)}async Fr(r,i={}){const s=this.vr(i);this.Pr(s);const n=await this.Mr(r,s);this.mr.Fr(n,s.filename,s.format)}}const jt=c=>class extends c{Ar(){this.Us.ls(this.Br)}toString(r={}){return this.Ar(),new et().zr({pipeline:this.Ir,grid:this.Lr,font:this.Qs},r)}saveStrings(r={}){this.Ar(),new et().$r({pipeline:this.Ir,grid:this.Lr,font:this.Qs},r)}toSVG(r={}){return this.Ar(),new tt().Cr({pipeline:this.Ir,grid:this.Lr,font:this.Qs},r)}saveSVG(r={}){this.Ar(),new tt().pr({pipeline:this.Ir,grid:this.Lr,font:this.Qs},r)}async saveCanvas(r={}){await new Vt().Fr(this.xi,r)}},Xt=c=>class extends c{async loadFont(r){return this.Qs.di(r).then(()=>{const i=this.Qs.maxGlyphDimensions;this.Lr.wi(i.width,i.height),this.Ir.W(this.Lr.cols,this.Lr.rows),this.Us.cs()})}fontSize(r){if(!z.m(typeof r=="number"&&r>0,"Font size must be a positive number greater than 0.",{method:"fontSize",providedValue:r})||this.Qs.fontSize===r)return;this.Qs.fi(r);const i=this.Qs.maxGlyphDimensions;this.Lr.wi(i.width,i.height),this.Ir.W(this.Lr.cols,this.Lr.rows),this.Us.cs()}},Yt=c=>class extends c{get frameCount(){return this.Er.frameCount}set frameCount(r){this.Er.frameCount=r}frameRate(r){return r===void 0?this.Er.currentFrameRate:this.Er.frameRate(r,()=>this.Wr())}noLoop(){this.Er.pause()}loop(){this.Er.resume(()=>this.Wr())}redraw(r=1){if(z.m(typeof r=="number"&&r>0&&Number.isInteger(r),"Redraw count must be a positive integer.",{method:"redraw",providedValue:r}))for(let i=0;i<r;i++)this.Wr()}isLooping(){return this.Er.isLooping}};class Ht{constructor(){u(this,"Us");u(this,"Qs");u(this,"xi");u(this,"Lr");u(this,"Er");u(this,"Br");u(this,"Ir");u(this,"kr")}Wr(){}}class it extends function(i,...s){return s.reduce((n,o)=>o(n),i)}(Ht,Pt,jt,Xt,Yt){constructor(i={}){super();u(this,"Ur",!1);u(this,"Vr",()=>{});u(this,"Or",()=>{});u(this,"jr");this.xi=new J(i),this.Us=new St(this.xi.Ri()),this.Qs=new K(this.Us,i.fontSize??16),this.Er=new Lt(i.frameRate??60),this.Br=this.Us.Xe(j,`#version 300 es
28
- precision highp float;in vec2 v_uv;in vec3 v_character;in vec4 v_primaryColor;in vec4 v_secondaryColor;in vec2 v_rotation;in vec3 v_transform;layout(location=0)out vec4 o_character;layout(location=1)out vec4 o_primaryColor;layout(location=2)out vec4 o_secondaryColor;layout(location=3)out vec4 o_rotation;layout(location=4)out vec4 o_transform;void main(){o_character=vec4(v_character,1.0);o_primaryColor=v_primaryColor;o_secondaryColor=v_secondaryColor;o_rotation=vec4(v_rotation,0.0,1.0);o_transform=vec4(v_transform,1.0);}`),this.kr=this.Us.Xe("attribute vec2 a_position;attribute vec2 a_texCoord;varying vec2 v_uv;void main(){v_uv=a_texCoord;gl_Position=vec4(a_position,0.0,1.0);}","precision mediump float;uniform sampler2D u_characterTexture;uniform vec2 u_charsetDimensions;uniform sampler2D u_primaryColorTexture;uniform sampler2D u_secondaryColorTexture;uniform sampler2D u_transformTexture;uniform sampler2D u_asciiCharacterTexture;uniform sampler2D u_rotationTexture;uniform vec2 u_gridCellDimensions;uniform vec2 u_gridPixelDimensions;uniform vec2 u_gridOffsetPixels;mat2 rotate2D(float angle){float s=sin(angle);float c=cos(angle);return mat2(c,-s,s,c);}void main(){vec2 adjustedCoord=(gl_FragCoord.xy-u_gridOffsetPixels)/u_gridPixelDimensions;vec2 gridCoord=adjustedCoord*u_gridCellDimensions;vec2 cellCoord=floor(gridCoord);vec2 charIndexTexCoord=(cellCoord+0.5)/u_gridCellDimensions;vec4 primaryColor=texture2D(u_primaryColorTexture,charIndexTexCoord);vec4 secondaryColor=texture2D(u_secondaryColorTexture,charIndexTexCoord);vec4 transformColor=texture2D(u_transformTexture,charIndexTexCoord);bool isInverted=transformColor.r>0.5;bool flipHorizontal=transformColor.g>0.5;bool flipVertical=transformColor.b>0.5;vec4 encodedIndexVec=texture2D(u_asciiCharacterTexture,charIndexTexCoord);int charIndex=int(encodedIndexVec.r*255.0+0.5)+int(encodedIndexVec.g*255.0+0.5)*256;int charCol=int(mod(float(charIndex),u_charsetDimensions.x));int charRow=charIndex/int(u_charsetDimensions.x);float flippedRow=(u_charsetDimensions.y-1.0)-float(charRow);vec2 charCoord=vec2(float(charCol),flippedRow)/u_charsetDimensions;vec4 rotationColor=texture2D(u_rotationTexture,charIndexTexCoord);float scaledAngle=rotationColor.r*255.0+rotationColor.g;float rotationAngle=-(scaledAngle*360.0/255.0)*0.017453292;vec2 fractionalPart=fract(gridCoord)-0.5;if(flipHorizontal)fractionalPart.x=-fractionalPart.x;if(flipVertical)fractionalPart.y=-fractionalPart.y;fractionalPart=rotate2D(rotationAngle)*fractionalPart+0.5;vec2 cellSize=1.0/u_charsetDimensions;vec2 texCoord=charCoord+fractionalPart*cellSize;vec2 cellMax=charCoord+cellSize;if(any(lessThan(texCoord,charCoord))||any(greaterThan(texCoord,cellMax))){gl_FragColor=isInverted ? primaryColor : secondaryColor;return;}vec4 charTexel=texture2D(u_characterTexture,texCoord);if(isInverted)charTexel.rgb=1.0-charTexel.rgb;gl_FragColor=mix(secondaryColor,primaryColor,charTexel);}")}static async create(i={}){const s=new this(i);await s.Qs.ci(i.fontSource);const n=s.Qs.maxGlyphDimensions;return s.Lr=new q(s.xi.canvas,n.width,n.height),s.Ir=s.Us.rs(s.Lr.cols,s.Lr.rows,5),s.Hr(),s.Er.start(()=>s.Wr()),s}Hr(){this.jr=()=>{this.Or()},window.addEventListener("resize",this.jr)}Wr(){if(this.Er.measureFrameRate(),this.Er.incrementFrame(),this.Ur)return;this.Ir.V(),this.Us.He(this.Br),this.Vr(),this.Us.ls(this.Br),this.Ir.O();const i=this.Us.state.canvasBackgroundColor;this.Us.de(i[0],i[1],i[2],i[3]),this.Us.He(this.kr),this.kr.st({u_characterTexture:this.Qs.fontFramebuffer,u_charsetDimensions:[this.Qs.textureColumns,this.Qs.textureRows],u_asciiCharacterTexture:this.Ir.textures[0],u_primaryColorTexture:this.Ir.textures[1],u_secondaryColorTexture:this.Ir.textures[2],u_transformTexture:this.Ir.textures[4],u_rotationTexture:this.Ir.textures[3],u_gridCellDimensions:[this.Lr.cols,this.Lr.rows],u_gridPixelDimensions:[this.Lr.width,this.Lr.height],u_gridOffsetPixels:[this.Lr.offsetX,this.Lr.offsetY],u_aspectRatio:this.Lr.width/this.Lr.height}),this.Us.Qe(this.Lr.offsetX,this.Lr.offsetY,this.Lr.width,this.Lr.height)}draw(i){this.Vr=i}windowResized(i){this.Or=i}resizeCanvas(i,s){this.xi.W(i,s),this.Lr.zt(),this.Ir.W(this.Lr.cols,this.Lr.rows),this.Us.cs(),this.Wr()}destroy(){this.Ur||(this.Er.stop(),window.removeEventListener("resize",this.jr),this.Qs.j(),this.Us.j(),this.Ur=!0)}get grid(){return this.Lr}get font(){return this.Qs}get width(){return this.xi.width}get height(){return this.xi.height}get canvas(){return this.xi.canvas}get isDisposed(){return this.Ur}get drawFramebuffer(){return this.Ir}}class W{constructor(){throw new v("Textmode is a static class and cannot be instantiated.")}static async create(r){return it.create(r)}static setErrorLevel(r){z.v(r)}static get version(){return"0.2.0-beta.3"}}const Qt=Object.freeze(Object.defineProperty({__proto__:null},Symbol.toStringTag,{value:"Module"})),Zt=W.create,Kt=W.setErrorLevel,qt=W.version;b.TextmodeCanvas=J,b.TextmodeErrorLevel=L,b.TextmodeFont=K,b.TextmodeGrid=q,b.Textmodifier=it,b.create=Zt,b.export=Qt,b.setErrorLevel=Kt,b.textmode=W,b.version=qt,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})},typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):e((t=typeof globalThis<"u"?globalThis:t||self).textmode={});
27
+ `;return i.join(n)}}class $t extends Y{$r(t,r){const i=this.br(r);this.Ui(t,i,"text/plain;charset=utf-8")}br(t){let r=this.ji(t);return r===".txt"||r.length<=4?this.Hi():r}}class et{constructor(){u(this,"_r");u(this,"gr");u(this,"mr");this._r=new Bt,this.gr=new Gt,this.mr=new $t}vr(t){return{preserveTrailingSpaces:t.preserveTrailingSpaces??!1,lineEnding:t.lineEnding??"lf",emptyCharacter:t.emptyCharacter??" ",filename:t.filename||this.mr.Hi()}}zr(t,r={}){const i=this.vr(r),n=this._r.yr(this._r.Li(t.pipeline),t.grid,t.font,i.emptyCharacter);return this.gr.wr(n,i)}$r(t,r={}){this.mr.$r(this.zr(t,r),r.filename)}}class Ot extends j{Rr(t,r=1,i="transparent"){const n=t.canvas;if(r===1&&i==="transparent")return n;const o=document.createElement("canvas"),a=o.getContext("2d"),h=Math.round(n.width*r),l=Math.round(n.height*r);return o.width=h,o.height=l,i!=="transparent"&&(a.fillStyle=i,a.fillRect(0,0,h,l)),a.imageSmoothingEnabled=!1,a.drawImage(n,0,0,n.width,n.height,0,0,h,l),o}}class Wt{Tr(t,r){const i=this.Sr(r.format);return r.format==="png"?t.toDataURL(i):t.toDataURL(i,r.quality)}async Mr(t,r){return new Promise((i,n)=>{const o=this.Sr(r.format),a=h=>{h?i(h):n(Error(`Failed to generate ${r.format.toUpperCase()} blob`))};r.format==="png"?t.toBlob(a,o):t.toBlob(a,o,r.quality)})}Sr(t){switch(t){case"png":return"image/png";case"jpg":return"image/jpeg";case"webp":return"image/webp";default:throw Error("Unsupported image format: "+t)}}}const kt={png:"image/png",jpg:"image/jpeg",webp:"image/webp"},rt={png:".png",jpg:".jpg",webp:".webp"};class Nt extends Y{Fr(t,r,i){this.Gr(t,this.ji(r)+rt[i])}Gr(t,r){const i=URL.createObjectURL(t);try{const n=document.createElement("a");n.href=i,n.download=r,n.style.display="none",n.rel="noopener",document.body.appendChild(n),n.click(),document.body.removeChild(n)}finally{URL.revokeObjectURL(i)}}Dr(t){return t in kt&&t in rt}}class Vt{constructor(){u(this,"_r");u(this,"gr");u(this,"mr");this._r=new Ot,this.gr=new Wt,this.mr=new Nt}vr(t){return{format:t.format??"png",quality:t.quality??1,scale:t.scale??1,backgroundColor:t.backgroundColor??"transparent",filename:t.filename||this.mr.Hi()}}Pr(t){if(!this.mr.Dr(t.format))throw Error(`Saving '${t.format}' files is not supported`);if(t.quality<0||t.quality>1)throw Error("Image quality must be between 0.0 and 1.0");if(t.scale<=0)throw Error("Scale factor must be greater than 0");t.format==="jpg"&&t.backgroundColor==="transparent"&&(t.backgroundColor="black")}async Mr(t,r){if(r.scale===1&&r.backgroundColor==="transparent")return await this.gr.Mr(t.canvas,r);const i=this._r.Rr(t,r.scale,r.backgroundColor);return await this.gr.Mr(i,r)}async Fr(t,r={}){const i=this.vr(r);this.Pr(i);const n=await this.Mr(t,i);this.mr.Fr(n,i.filename,i.format)}}const Xt=c=>class extends c{Ar(){this.Us.ls(this.Br)}toString(t={}){return this.Ar(),new et().zr({pipeline:this.Ir,grid:this.Lr,font:this.Qs},t)}saveStrings(t={}){this.Ar(),new et().$r({pipeline:this.Ir,grid:this.Lr,font:this.Qs},t)}toSVG(t={}){return this.Ar(),new tt().Cr({pipeline:this.Ir,grid:this.Lr,font:this.Qs},t)}saveSVG(t={}){this.Ar(),new tt().pr({pipeline:this.Ir,grid:this.Lr,font:this.Qs},t)}async saveCanvas(t={}){await new Vt().Fr(this.xi,t)}},jt=c=>class extends c{async loadFont(t){return this.Qs.di(t).then(()=>{const r=this.Qs.maxGlyphDimensions;this.Lr.wi(r.width,r.height),this.Ir.W(this.Lr.cols,this.Lr.rows),this.Us.cs()})}fontSize(t){if(!z.m(typeof t=="number"&&t>0,"Font size must be a positive number greater than 0.",{method:"fontSize",providedValue:t})||this.Qs.fontSize===t)return;this.Qs.fi(t);const r=this.Qs.maxGlyphDimensions;this.Lr.wi(r.width,r.height),this.Ir.W(this.Lr.cols,this.Lr.rows),this.Us.cs()}},Yt=c=>class extends c{get frameCount(){return this.Er.frameCount}set frameCount(t){this.Er.frameCount=t}frameRate(t){return t===void 0?this.Er.currentFrameRate:this.Er.frameRate(t,()=>this.Wr())}noLoop(){this.Er.pause()}loop(){this.Er.resume(()=>this.Wr())}redraw(t=1){if(z.m(typeof t=="number"&&t>0&&Number.isInteger(t),"Redraw count must be a positive integer.",{method:"redraw",providedValue:t}))for(let r=0;r<t;r++)this.Wr()}isLooping(){return this.Er.isLooping}};class Ht{constructor(){u(this,"Us");u(this,"Qs");u(this,"xi");u(this,"Lr");u(this,"Er");u(this,"Br");u(this,"Ir");u(this,"kr")}Wr(){}}class it extends function(r,...i){return i.reduce((n,o)=>o(n),r)}(Ht,Pt,Xt,jt,Yt){constructor(r={}){super();u(this,"Ur",!1);u(this,"Vr",()=>{});u(this,"Or",()=>{});u(this,"jr",()=>{});u(this,"Hr");this.xi=new J(r),this.Us=new St(this.xi.Ri()),this.Qs=new K(this.Us,r.fontSize??16),this.Er=new Lt(r.frameRate??60),this.Br=this.Us.Xe(X,`#version 300 es
28
+ precision highp float;in vec2 v_uv;in vec3 v_character;in vec4 v_primaryColor;in vec4 v_secondaryColor;in vec2 v_rotation;in vec3 v_transform;layout(location=0)out vec4 o_character;layout(location=1)out vec4 o_primaryColor;layout(location=2)out vec4 o_secondaryColor;layout(location=3)out vec4 o_rotation;layout(location=4)out vec4 o_transform;void main(){o_character=vec4(v_character,1.0);o_primaryColor=v_primaryColor;o_secondaryColor=v_secondaryColor;o_rotation=vec4(v_rotation,0.0,1.0);o_transform=vec4(v_transform,1.0);}`),this.kr=this.Us.Xe("attribute vec2 a_position;attribute vec2 a_texCoord;varying vec2 v_uv;void main(){v_uv=a_texCoord;gl_Position=vec4(a_position,0.0,1.0);}","precision mediump float;uniform sampler2D u_characterTexture;uniform vec2 u_charsetDimensions;uniform sampler2D u_primaryColorTexture;uniform sampler2D u_secondaryColorTexture;uniform sampler2D u_transformTexture;uniform sampler2D u_asciiCharacterTexture;uniform sampler2D u_rotationTexture;uniform vec2 u_gridCellDimensions;uniform vec2 u_gridPixelDimensions;uniform vec2 u_gridOffsetPixels;mat2 rotate2D(float angle){float s=sin(angle);float c=cos(angle);return mat2(c,-s,s,c);}void main(){vec2 adjustedCoord=(gl_FragCoord.xy-u_gridOffsetPixels)/u_gridPixelDimensions;vec2 gridCoord=adjustedCoord*u_gridCellDimensions;vec2 cellCoord=floor(gridCoord);vec2 charIndexTexCoord=(cellCoord+0.5)/u_gridCellDimensions;vec4 primaryColor=texture2D(u_primaryColorTexture,charIndexTexCoord);vec4 secondaryColor=texture2D(u_secondaryColorTexture,charIndexTexCoord);vec4 transformColor=texture2D(u_transformTexture,charIndexTexCoord);bool isInverted=transformColor.r>0.5;bool flipHorizontal=transformColor.g>0.5;bool flipVertical=transformColor.b>0.5;vec4 encodedIndexVec=texture2D(u_asciiCharacterTexture,charIndexTexCoord);int charIndex=int(encodedIndexVec.r*255.0+0.5)+int(encodedIndexVec.g*255.0+0.5)*256;int charCol=int(mod(float(charIndex),u_charsetDimensions.x));int charRow=charIndex/int(u_charsetDimensions.x);float flippedRow=(u_charsetDimensions.y-1.0)-float(charRow);vec2 charCoord=vec2(float(charCol),flippedRow)/u_charsetDimensions;vec4 rotationColor=texture2D(u_rotationTexture,charIndexTexCoord);float scaledAngle=rotationColor.r*255.0+rotationColor.g;float rotationAngle=-(scaledAngle*360.0/255.0)*0.017453292;vec2 fractionalPart=fract(gridCoord)-0.5;if(flipHorizontal)fractionalPart.x=-fractionalPart.x;if(flipVertical)fractionalPart.y=-fractionalPart.y;fractionalPart=rotate2D(rotationAngle)*fractionalPart+0.5;vec2 cellSize=1.0/u_charsetDimensions;vec2 texCoord=charCoord+fractionalPart*cellSize;vec2 cellMax=charCoord+cellSize;if(any(lessThan(texCoord,charCoord))||any(greaterThan(texCoord,cellMax))){gl_FragColor=isInverted ? primaryColor : secondaryColor;return;}vec4 charTexel=texture2D(u_characterTexture,texCoord);if(isInverted)charTexel.rgb=1.0-charTexel.rgb;gl_FragColor=mix(secondaryColor,primaryColor,charTexel);}"),this.Xr(r)}async Xr(r){await this.Qs.ci(r.fontSource);const i=this.Qs.maxGlyphDimensions;this.Lr=new q(this.xi.canvas,i.width,i.height),this.Ir=this.Us.rs(this.Lr.cols,this.Lr.rows,5),this.Yr(),this.Vr(),this.Er.start(()=>this.Wr())}Yr(){this.Hr=()=>{this.jr()},window.addEventListener("resize",this.Hr)}Wr(){if(this.Er.measureFrameRate(),this.Er.incrementFrame(),this.Ur)return;this.Ir.V(),this.Us.He(this.Br),this.Or(),this.Us.ls(this.Br),this.Ir.O();const r=this.Us.state.canvasBackgroundColor;this.Us.de(r[0],r[1],r[2],r[3]),this.Us.He(this.kr),this.kr.st({u_characterTexture:this.Qs.fontFramebuffer,u_charsetDimensions:[this.Qs.textureColumns,this.Qs.textureRows],u_asciiCharacterTexture:this.Ir.textures[0],u_primaryColorTexture:this.Ir.textures[1],u_secondaryColorTexture:this.Ir.textures[2],u_transformTexture:this.Ir.textures[4],u_rotationTexture:this.Ir.textures[3],u_gridCellDimensions:[this.Lr.cols,this.Lr.rows],u_gridPixelDimensions:[this.Lr.width,this.Lr.height],u_gridOffsetPixels:[this.Lr.offsetX,this.Lr.offsetY],u_aspectRatio:this.Lr.width/this.Lr.height}),this.Us.Qe(this.Lr.offsetX,this.Lr.offsetY,this.Lr.width,this.Lr.height)}setup(r){this.Vr=r}draw(r){this.Or=r}windowResized(r){this.jr=r}resizeCanvas(r,i){this.xi.W(r,i),this.Lr.zt(),this.Ir.W(this.Lr.cols,this.Lr.rows),this.Us.cs(),this.Wr()}destroy(){this.Ur||(this.Er.stop(),window.removeEventListener("resize",this.Hr),this.Qs.j(),this.Us.j(),this.Ur=!0)}get grid(){return this.Lr}get font(){return this.Qs}get width(){return this.xi.width}get height(){return this.xi.height}get canvas(){return this.xi.canvas}get isDisposed(){return this.Ur}get drawFramebuffer(){return this.Ir}}class W{constructor(){throw new v("Textmode is a static class and cannot be instantiated.")}static create(t={}){return new it(t)}static setErrorLevel(t){z.v(t)}static get version(){return"0.2.0-beta.5"}}const Qt=Object.freeze(Object.defineProperty({__proto__:null},Symbol.toStringTag,{value:"Module"})),Zt=W.create,Kt=W.setErrorLevel,qt=W.version;b.TextmodeCanvas=J,b.TextmodeErrorLevel=L,b.TextmodeFont=K,b.TextmodeGrid=q,b.Textmodifier=it,b.create=Zt,b.export=Qt,b.setErrorLevel=Kt,b.textmode=W,b.version=qt,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})},typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):s((e=typeof globalThis<"u"?globalThis:e||self).textmode={});
@@ -10,23 +10,35 @@ export declare class Textmode {
10
10
  /**
11
11
  * Create a new Textmodifier instance with optional configuration.
12
12
  *
13
+ * This method now returns immediately and handles font loading asynchronously.
14
+ * Use the `setup()` callback to execute code when initialization is complete.
15
+ *
13
16
  * @param opts Configuration options for the Textmodifier instance
14
- * @returns Promise that resolves to a new Textmodifier instance
17
+ * @returns A new Textmodifier instance
15
18
  *
16
19
  * @example
17
20
  * ```javascript
18
21
  * // Create with default canvas
19
- * const textmodifier = await textmode.create();
22
+ * const textmodifier = textmode.create();
23
+ *
24
+ * textmodifier.setup(() => {
25
+ * // Called when font is loaded and ready
26
+ * console.log(`Grid size: ${textmodifier.grid.cols}x${textmodifier.grid.rows}`);
27
+ * });
20
28
  *
29
+ * textmodifier.draw(() => {
30
+ * textmodifier.background(128);
31
+ * textmodifier.rect(10, 10, 20, 20);
32
+ * });
21
33
  *
22
34
  * // Create with options
23
- * const textmodifier = await textmode.create({ width: 1920, height: 1080 });
35
+ * const textmodifier2 = textmode.create({ width: 1920, height: 1080 });
24
36
  *
25
37
  * // Create with canvas and options
26
- * const textmodifier = await textmode.create({ canvas: canvas, fontSize: 20 });
38
+ * const textmodifier3 = textmode.create({ canvas: canvas, fontSize: 20 });
27
39
  * ```
28
40
  */
29
- static create(opts: TextmodeOptions): Promise<Textmodifier>;
41
+ static create(opts?: TextmodeOptions): Textmodifier;
30
42
  /**
31
43
  * Set the global error handling level for the library. This applies to all {@link Textmodifier} instances present.
32
44
  *
@@ -55,6 +55,7 @@ declare const Textmodifier_base: typeof TextmodifierCore;
55
55
  */
56
56
  export declare class Textmodifier extends Textmodifier_base {
57
57
  private _isDisposed;
58
+ private _setupCallback;
58
59
  private _drawCallback;
59
60
  private _resizedCallback;
60
61
  private _windowResizeListener;
@@ -65,11 +66,10 @@ export declare class Textmodifier extends Textmodifier_base {
65
66
  */
66
67
  constructor(opts?: TextmodeOptions);
67
68
  /**
68
- * Static factory method for creating and initializing a Textmodifier instance.
69
- * @param opts Optional configuration options for the `Textmodifier` instance.
70
- * @ignore
69
+ * Internal async initialization method.
70
+ * @param opts Configuration options
71
71
  */
72
- static create(opts?: TextmodeOptions): Promise<Textmodifier>;
72
+ private _initialize;
73
73
  /**
74
74
  * Setup event listeners for resize handling.
75
75
  * @ignore
@@ -81,6 +81,41 @@ export declare class Textmodifier extends Textmodifier_base {
81
81
  * @ignore
82
82
  */
83
83
  $render(): void;
84
+ /**
85
+ * Set a setup callback function that will be executed once when initialization is complete.
86
+ *
87
+ * This callback is called after font loading and grid initialization, allowing access to
88
+ * properties like `textmodifier.grid.cols` for calculating layout or setup variables.
89
+ *
90
+ * @param callback The function to call when setup is complete
91
+ *
92
+ * @example
93
+ * ```javascript
94
+ * const textmodifier = textmode.create({
95
+ * width: 800,
96
+ * height: 600,
97
+ * fontSize: 16
98
+ * });
99
+ *
100
+ * // Setup callback - called once when ready
101
+ * textmodifier.setup(() => {
102
+ * // Now you can access grid properties
103
+ * const cols = textmodifier.grid.cols;
104
+ * const rows = textmodifier.grid.rows;
105
+ *
106
+ * // Initialize any variables that depend on grid size
107
+ * cellWidth = Math.floor(cols / 3);
108
+ * cellHeight = Math.floor(rows / 2);
109
+ * });
110
+ *
111
+ * // Draw callback - called every frame
112
+ * textmodifier.draw(() => {
113
+ * textmodifier.background(128);
114
+ * textmodifier.rect(0, 0, cellWidth, cellHeight);
115
+ * });
116
+ * ```
117
+ */
118
+ setup(callback: () => void): void;
84
119
  /**
85
120
  * Set a draw callback function that will be executed before each render.
86
121
  *
@@ -481,12 +481,12 @@ export interface RenderingCapabilities {
481
481
  *
482
482
  * t.draw(() => {
483
483
  * t.background(0);
484
- * t.flipHorizontally(true);
484
+ * t.flipX(true);
485
485
  * t.rect(10, 10, 5, 5);
486
486
  * });
487
487
  * ```
488
488
  */
489
- flipHorizontally(toggle: boolean): void;
489
+ flipX(toggle: boolean): void;
490
490
  /**
491
491
  * Toggle vertical flipping for subsequent character rendering.
492
492
  * @param toggle Whether to flip vertically
@@ -500,12 +500,12 @@ export interface RenderingCapabilities {
500
500
  *
501
501
  * t.draw(() => {
502
502
  * t.background(0);
503
- * t.flipVertically(true);
503
+ * t.flipY(true);
504
504
  * t.rect(10, 10, 5, 5);
505
505
  * });
506
506
  * ```
507
507
  */
508
- flipVertically(toggle: boolean): void;
508
+ flipY(toggle: boolean): void;
509
509
  /**
510
510
  * Set the character rotation angle for subsequent character rendering.
511
511
  * @param degrees The rotation angle in degrees
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "textmode.js",
3
- "version": "0.2.0-beta.3",
3
+ "version": "0.2.0-beta.5",
4
4
  "description": "Apply real-time ASCII conversion to any HTML canvas.",
5
5
  "type": "module",
6
6
  "types": "./dist/types/index.d.ts",