textmode.js 0.1.6-beta.4 → 0.1.6-beta.6

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.
Files changed (39) hide show
  1. package/dist/textmode.esm.js +1487 -4016
  2. package/dist/textmode.esm.min.js +1486 -4014
  3. package/dist/textmode.umd.js +19 -19
  4. package/dist/textmode.umd.min.js +18 -18
  5. package/dist/types/ColorPalette.d.ts +1 -5
  6. package/dist/types/Textmode.d.ts +0 -10
  7. package/dist/types/export/image/ImageDataExtractor.d.ts +1 -1
  8. package/dist/types/export/image/ImageExporter.d.ts +1 -1
  9. package/dist/types/export/image/ImageFileHandler.d.ts +1 -7
  10. package/dist/types/export/svg/SVGContentGenerator.d.ts +1 -1
  11. package/dist/types/export/svg/SVGDataExtractor.d.ts +1 -1
  12. package/dist/types/export/svg/SVGPathGenerator.d.ts +0 -5
  13. package/dist/types/export/txt/TXTDataExtractor.d.ts +1 -1
  14. package/dist/types/index.d.ts +0 -1
  15. package/dist/types/rendering/index.d.ts +0 -5
  16. package/dist/types/rendering/webgl/Framebuffer.d.ts +2 -9
  17. package/dist/types/rendering/webgl/Renderer.d.ts +2 -12
  18. package/dist/types/rendering/webgl/Shader.d.ts +5 -12
  19. package/dist/types/rendering/webgl/StateCache.d.ts +0 -2
  20. package/dist/types/rendering/webgl/geometries/BaseGeometry.d.ts +40 -0
  21. package/dist/types/rendering/webgl/geometries/Line.d.ts +6 -26
  22. package/dist/types/rendering/webgl/geometries/Rectangle.d.ts +8 -18
  23. package/dist/types/rendering/webgl/geometries/index.d.ts +3 -0
  24. package/dist/types/textmode/Canvas.d.ts +0 -11
  25. package/dist/types/textmode/ConversionPipeline.d.ts +12 -9
  26. package/dist/types/textmode/Grid.d.ts +1 -6
  27. package/dist/types/textmode/Textmodifier.d.ts +28 -610
  28. package/dist/types/textmode/converters/FeatureConverter.d.ts +24 -0
  29. package/dist/types/textmode/mixins/ConversionMixin.d.ts +63 -0
  30. package/dist/types/textmode/mixins/ExportMixin.d.ts +155 -0
  31. package/dist/types/textmode/mixins/FontMixin.d.ts +49 -0
  32. package/dist/types/textmode/mixins/RenderingMixin.d.ts +409 -0
  33. package/dist/types/textmode/mixins/TextmodifierMixin.d.ts +39 -0
  34. package/dist/types/textmode/mixins/index.d.ts +5 -0
  35. package/package.json +3 -1
  36. package/dist/types/rendering/webgl/shapes/LineShape.d.ts +0 -20
  37. package/dist/types/rendering/webgl/shapes/RectangleShape.d.ts +0 -20
  38. package/dist/types/rendering/webgl/shapes/Shape.d.ts +0 -21
  39. package/dist/types/types/index.d.ts +0 -4
@@ -1,25 +1,25 @@
1
- (function(v,_){typeof exports=="object"&&typeof module<"u"?_(exports):typeof define=="function"&&define.amd?define(["exports"],_):(v=typeof globalThis<"u"?globalThis:v||self,_(v.textmode={}))})(this,function(v){"use strict";var Ve=Object.defineProperty;var ze=(v,_,M)=>_ in v?Ve(v,_,{enumerable:!0,configurable:!0,writable:!0,value:M}):v[_]=M;var o=(v,_,M)=>ze(v,typeof _!="symbol"?_+"":_,M);class _ extends Error{constructor(t,r,i={}){const s=_.createFormattedMessage(t,i);super(s);o(this,"originalError");o(this,"context");this.name="TextmodeError",this.originalError=r,this.context=i}static createFormattedMessage(t,r){let i=t;if(r&&Object.keys(r).length>0){i+=`
1
+ var Yt=Object.defineProperty;var Pt=(v,x,L)=>x in v?Yt(v,x,{enumerable:!0,configurable:!0,writable:!0,value:L}):v[x]=L;var l=(v,x,L)=>Pt(v,typeof x!="symbol"?x+"":x,L);var t,e;t=this,e=function(v){class x extends Error{constructor(i,r,o={}){super(x.i(i,o));l(this,"t");l(this,"context");this.name="TextmodeError",this.t=r,this.context=o}static i(i,r){let o=i;if(r&&Object.keys(r).length>0){o+=`
2
2
 
3
- 📋 Context:`;for(const[s,a]of Object.entries(r)){const n=_.formatValue(a);i+=`
4
- - ${s}: ${n}`}}return i+=`
3
+ 📋 Context:`;for(const[n,h]of Object.entries(r))o+=`
4
+ - ${n}: ${x.o(h)}`}return o+=`
5
5
 
6
- `,i+="↓".repeat(24)+`
7
- `,i}static formatValue(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 String(t);if(Array.isArray(t))return t.length===0?"[]":t.length<=5?`[${t.map(r=>_.formatValue(r)).join(", ")}]`:`[${t.slice(0,3).map(r=>_.formatValue(r)).join(", ")}, ... +${t.length-3} more]`;if(typeof t=="object"){const r=Object.keys(t);return r.length===0?"{}":r.length<=3?`{ ${r.map(a=>`${a}: ${_.formatValue(t[a])}`).join(", ")} }`:`{ ${r.slice(0,2).map(s=>`${s}: ${_.formatValue(t[s])}`).join(", ")}, ... +${r.length-2} more }`}return String(t)}}var M=(h=>(h[h.SILENT=0]="SILENT",h[h.WARNING=1]="WARNING",h[h.ERROR=2]="ERROR",h[h.THROW=3]="THROW",h))(M||{});const A=class A{constructor(){o(this,"_options",{globalLevel:3})}static getInstance(){return A._instance||(A._instance=new A),A._instance}_handle(e,t,r){const i="[textmode.js]";switch(this._options.globalLevel){case 0:return!1;case 1:return console.group(`%c${i} Oops! (╯°□°)╯︵ Something went wrong in your code.`,"color: #f44336; font-weight: bold; background: #ffebee; padding: 2px 6px; border-radius: 3px;"),console.warn(_.createFormattedMessage(e,t)),console.groupEnd(),!1;case 2:return console.group(`%c${i} Oops! (╯°□°)╯︵ Something went wrong in your code.`,"color: #f44336; font-weight: bold; background: #ffebee; padding: 2px 6px; border-radius: 3px;"),console.error(_.createFormattedMessage(e,t)),console.groupEnd(),!1;case 3:default:const s=new _(e,r,t);throw console.group(`%c${i} Oops! (╯°□°)╯︵ Something went wrong in your code.`,"color: #d32f2f; font-weight: bold; background: #ffcdd2; padding: 2px 6px; border-radius: 3px;"),s}}validate(e,t,r){return e?!0:(this._handle(t,r),!1)}setGlobalLevel(e){this._options.globalLevel=e}};o(A,"_instance",null);let $=A;const b=$.getInstance(),j=new WeakMap,ae=new WeakMap;function V(h,e){j.set(h,e)}function q(h){return j.get(h)}function z(h,e){ae.set(h,e)}const k=class k{constructor(e,t,r=t,i={}){o(this,"gl");o(this,"_framebuffer");o(this,"_texture");o(this,"_width");o(this,"_height");o(this,"options");o(this,"previousState",null);o(this,"_pixels",null);o(this,"_isFromCanvasSource",!1);this.gl=e,this._width=t,this._height=r,this.options={filter:"nearest",wrap:"clamp",format:"rgba",type:"unsigned_byte",...i},this._texture=this.createTexture(),this._framebuffer=e.createFramebuffer(),this.attachTexture()}getMaxTextureUnits(){let e=k.maxTextureUnitsMap.get(this.gl);return e===void 0&&(e=this.gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),k.maxTextureUnitsMap.set(this.gl,e)),e}unbindTextureAcrossUnits(e){const t=this.gl,r=t.getParameter(t.ACTIVE_TEXTURE),i=this.getMaxTextureUnits();for(let s=0;s<i;s++)t.activeTexture(t.TEXTURE0+s),t.getParameter(t.TEXTURE_BINDING_2D)===e&&t.bindTexture(t.TEXTURE_2D,null);t.activeTexture(r)}createTexture(){const{gl:e}=this,t=e.createTexture();e.bindTexture(e.TEXTURE_2D,t);const r=this.options.filter==="linear"?e.LINEAR:e.NEAREST,i=this.options.wrap==="repeat"?e.REPEAT:e.CLAMP_TO_EDGE;return e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,r),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,r),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,i),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,i),this.updateTextureSize(),t}updateTextureSize(){const{gl:e}=this,t=e.RGBA,r=e.RGBA,i=this.options.type==="float"?e.FLOAT:e.UNSIGNED_BYTE;e.texImage2D(e.TEXTURE_2D,0,t,this._width,this._height,0,r,i,null)}attachTexture(){const{gl:e}=this;e.bindFramebuffer(e.FRAMEBUFFER,this._framebuffer),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,this._texture,0),e.bindFramebuffer(e.FRAMEBUFFER,null)}update(e){const{gl:t}=this;if(e instanceof HTMLVideoElement&&e.readyState<2)return;t.bindTexture(t.TEXTURE_2D,this._texture);const r=t.getParameter(t.UNPACK_FLIP_Y_WEBGL);t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,1),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,r?1:0),t.bindTexture(t.TEXTURE_2D,null),this._isFromCanvasSource=!1}updatePixels(e,t,r){const{gl:i}=this;i.bindTexture(i.TEXTURE_2D,this._texture),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,t,r,0,i.RGBA,i.UNSIGNED_BYTE,e),i.bindTexture(i.TEXTURE_2D,null),this._isFromCanvasSource=!1}resize(e,t){const{gl:r}=this;this._width=e,this._height=t,this._pixels=null,r.bindTexture(r.TEXTURE_2D,this._texture),this.updateTextureSize(),r.bindTexture(r.TEXTURE_2D,null)}begin(){const{gl:e}=this;this.previousState={framebuffer:e.getParameter(e.FRAMEBUFFER_BINDING),viewport:e.getParameter(e.VIEWPORT)},e.bindFramebuffer(e.FRAMEBUFFER,this._framebuffer),e.viewport(0,0,this._width,this._height),this.unbindTextureAcrossUnits(this._texture),z(e,!0),V(e,[0,0,this._width,this._height]),this._isFromCanvasSource=!1}end(){if(!this.previousState)return;const{gl:e}=this;e.bindFramebuffer(e.FRAMEBUFFER,this.previousState.framebuffer),e.viewport(...this.previousState.viewport),z(e,!!this.previousState.framebuffer),V(e,this.previousState.viewport),this.previousState=null}loadPixels(){const{gl:e}=this;this._pixels||(this._pixels=new Uint8Array(this._width*this._height*4));const t=e.getParameter(e.FRAMEBUFFER_BINDING);e.bindFramebuffer(e.FRAMEBUFFER,this._framebuffer),e.readPixels(0,0,this._width,this._height,e.RGBA,e.UNSIGNED_BYTE,this._pixels),e.bindFramebuffer(e.FRAMEBUFFER,t)}get(e,t,r,i){const{gl:s}=this;if(e===void 0&&t===void 0){const a=new Uint8Array(this._width*this._height*4),n=s.getParameter(s.FRAMEBUFFER_BINDING);return s.bindFramebuffer(s.FRAMEBUFFER,this._framebuffer),s.readPixels(0,0,this._width,this._height,s.RGBA,s.UNSIGNED_BYTE,a),s.bindFramebuffer(s.FRAMEBUFFER,n),a}else if(r===void 0&&i===void 0){(e<0||t<0||e>=this._width||t>=this._height)&&(console.warn("The x and y values passed to Framebuffer.get are outside of its range and will be clamped."),e=Math.max(0,Math.min(e,this._width-1)),t=Math.max(0,Math.min(t,this._height-1)));const a=new Uint8Array(4),n=s.getParameter(s.FRAMEBUFFER_BINDING);return s.bindFramebuffer(s.FRAMEBUFFER,this._framebuffer),s.readPixels(e,t,1,1,s.RGBA,s.UNSIGNED_BYTE,a),s.bindFramebuffer(s.FRAMEBUFFER,n),[a[0],a[1],a[2],a[3]]}else{e=Math.max(0,Math.min(e,this._width-1)),t=Math.max(0,Math.min(t,this._height-1)),r=Math.max(1,Math.min(r,this._width-e)),i=Math.max(1,Math.min(i,this._height-t));const a=new Uint8Array(r*i*4),n=s.getParameter(s.FRAMEBUFFER_BINDING);return s.bindFramebuffer(s.FRAMEBUFFER,this._framebuffer),s.readPixels(e,t,r,i,s.RGBA,s.UNSIGNED_BYTE,a),s.bindFramebuffer(s.FRAMEBUFFER,n),a}}dispose(){const{gl:e}=this;this._framebuffer&&(e.deleteFramebuffer(this._framebuffer),this._framebuffer=null),this._texture&&(e.deleteTexture(this._texture),this._texture=null),this._pixels=null}get framebuffer(){return this._framebuffer}get texture(){return this._texture}get width(){return this._width}get height(){return this._height}get pixels(){return this._pixels}get isFromCanvasSource(){return this._isFromCanvasSource}};o(k,"maxTextureUnitsMap",new WeakMap);let N=k;class y{constructor(e,t,r){o(this,"gl");o(this,"program");o(this,"uniformLocations",new Map);o(this,"attributeLocations",new Map);o(this,"textureUnitCounter",0);this.gl=e,this.program=this.createProgram(t,r),this.cacheLocations()}createProgram(e,t){const r=this.createShader(this.gl.VERTEX_SHADER,e),i=this.createShader(this.gl.FRAGMENT_SHADER,t),s=this.gl.createProgram();if(this.gl.attachShader(s,r),this.gl.attachShader(s,i),this.gl.linkProgram(s),!this.gl.getProgramParameter(s,this.gl.LINK_STATUS)){const a=this.gl.getProgramInfoLog(s);throw new Error(`Shader program link error: ${a}`)}return this.gl.deleteShader(r),this.gl.deleteShader(i),s}createShader(e,t){const r=this.gl.createShader(e);if(this.gl.shaderSource(r,t),this.gl.compileShader(r),!this.gl.getShaderParameter(r,this.gl.COMPILE_STATUS)){const i=this.gl.getShaderInfoLog(r);throw this.gl.deleteShader(r),new Error(`Shader compilation error: ${i}`)}return r}cacheLocations(){const e=this.gl.getProgramParameter(this.program,this.gl.ACTIVE_UNIFORMS);for(let r=0;r<e;r++){const i=this.gl.getActiveUniform(this.program,r);if(i){const s=this.gl.getUniformLocation(this.program,i.name);s&&this.uniformLocations.set(i.name,s)}}const t=this.gl.getProgramParameter(this.program,this.gl.ACTIVE_ATTRIBUTES);for(let r=0;r<t;r++){const i=this.gl.getActiveAttrib(this.program,r);if(i){const s=this.gl.getAttribLocation(this.program,i.name);this.attributeLocations.set(i.name,s)}}}use(){this.gl.useProgram(this.program),this.resetTextureUnits()}setUniform(e,t){const r=this.uniformLocations.get(e);if(!r)return;const i=this.getUniformInfo(e);if(typeof t=="number")i&&i.type===this.gl.INT?this.gl.uniform1i(r,Math.floor(t)):this.gl.uniform1f(r,t);else if(typeof t=="boolean")this.gl.uniform1i(r,t?1:0);else if(Array.isArray(t))if(i&&(i.type===this.gl.INT_VEC2||i.type===this.gl.INT_VEC3||i.type===this.gl.INT_VEC4)){const s=t.map(a=>Math.floor(a));switch(s.length){case 2:this.gl.uniform2iv(r,s);break;case 3:this.gl.uniform3iv(r,s);break;case 4:this.gl.uniform4iv(r,s);break;default:console.warn(`Unsupported array length ${s.length} for uniform '${e}'`)}}else switch(t.length){case 2:this.gl.uniform2f(r,t[0],t[1]);break;case 3:this.gl.uniform3f(r,t[0],t[1],t[2]);break;case 4:this.gl.uniform4f(r,t[0],t[1],t[2],t[3]);break;default:console.warn(`Unsupported array length ${t.length} for uniform '${e}'`)}else if(t instanceof WebGLTexture){const s=this.getNextTextureUnit();this.gl.uniform1i(r,s),this.gl.activeTexture(this.gl.TEXTURE0+s),this.gl.bindTexture(this.gl.TEXTURE_2D,t)}else if(t&&typeof t=="object"&&"texture"in t){const s=this.getNextTextureUnit();this.gl.uniform1i(r,s),this.gl.activeTexture(this.gl.TEXTURE0+s),this.gl.bindTexture(this.gl.TEXTURE_2D,t.texture)}else console.warn(`Unsupported uniform type for '${e}':`,typeof t)}getUniformInfo(e){const t=this.gl.getProgramParameter(this.program,this.gl.ACTIVE_UNIFORMS);for(let r=0;r<t;r++){const i=this.gl.getActiveUniform(this.program,r);if(i&&i.name===e)return i}return null}getNextTextureUnit(){return this.textureUnitCounter++}hasUniform(e){return this.uniformLocations.has(e)}hasAttribute(e){return this.attributeLocations.has(e)}get glProgram(){return this.program}dispose(){this.program&&(this.gl.deleteProgram(this.program),this.program=null),this.uniformLocations.clear(),this.attributeLocations.clear(),this.textureUnitCounter=0}resetTextureUnits(){this.textureUnitCounter=0}}var T="attribute vec2 a_position;attribute vec2 a_texCoord;varying vec2 v_uv;uniform float u_rotation;uniform vec2 u_center;uniform float u_aspectRatio;mat2 rotate2D(float angle){float s=sin(angle);float c=cos(angle);return mat2(c,-s,s,c);}void main(){v_uv=a_texCoord;vec2 pos=a_position;pos-=u_center;pos.x*=u_aspectRatio;pos=rotate2D(-u_rotation)*pos;pos.x/=u_aspectRatio;pos+=u_center;gl_Position=vec4(pos,0.0,1.0);}",ne="precision lowp float;uniform sampler2D u_texture;varying vec2 v_uv;void main(){gl_FragColor=texture2D(u_texture,v_uv);}",oe="precision lowp float;uniform vec4 u_color;void main(){gl_FragColor=u_color;}";class he{constructor(e){o(this,"gl");o(this,"imageShader");o(this,"solidColorShader");o(this,"currentShader",null);o(this,"attribCache");o(this,"currentFillColor",[1,1,1,1]);o(this,"fillMode",!0);o(this,"currentStrokeColor",[0,0,0,1]);o(this,"currentStrokeWeight",1);o(this,"strokeMode",!0);o(this,"currentRotation",0);o(this,"stateStack",[]);o(this,"unitBuffer",null);o(this,"bytesPerVertex",16);o(this,"scratchFramebuffer",null);this.gl=e,this.imageShader=new y(this.gl,T,ne),this.solidColorShader=new y(this.gl,T,oe),this.attribCache=new Map,this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ONE,this.gl.ONE_MINUS_SRC_ALPHA),V(this.gl,[0,0,this.gl.canvas.width,this.gl.canvas.height]),z(this.gl,!1)}shader(e){this.currentShader!==e&&(this.currentShader=e,e.use())}fill(e,t,r,i){if(this.fillMode=!0,t===void 0&&r===void 0&&i===void 0){const s=e/255;this.currentFillColor=[s,s,s,1]}else if(r!==void 0&&i===void 0)this.currentFillColor=[e/255,t/255,r/255,1];else if(r!==void 0&&i!==void 0)this.currentFillColor=[e/255,t/255,r/255,i/255];else throw new Error("Invalid fill parameters. Use fill(gray), fill(r,g,b), or fill(r,g,b,a)")}stroke(e,t,r,i){if(this.strokeMode=!0,t===void 0&&r===void 0&&i===void 0){const s=e/255;this.currentStrokeColor=[s,s,s,1]}else if(r!==void 0&&i===void 0)this.currentStrokeColor=[e/255,t/255,r/255,1];else if(r!==void 0&&i!==void 0)this.currentStrokeColor=[e/255,t/255,r/255,i/255];else throw new Error("Invalid stroke parameters. Use stroke(gray), stroke(r,g,b), or stroke(r,g,b,a)")}strokeWeight(e){if(e<0)throw new Error("Stroke weight must be non-negative");this.currentStrokeWeight=e}noStroke(){this.strokeMode=!1}noFill(){this.fillMode=!1}rotate(e){this.currentRotation=e}push(){this.stateStack.push({fillColor:[...this.currentFillColor],fillMode:this.fillMode,strokeColor:[...this.currentStrokeColor],strokeWeight:this.currentStrokeWeight,strokeMode:this.strokeMode,rotation:this.currentRotation})}pop(){const e=this.stateStack.pop();e?(this.currentFillColor=e.fillColor,this.fillMode=e.fillMode,this.currentStrokeColor=e.strokeColor,this.currentStrokeWeight=e.strokeWeight,this.strokeMode=e.strokeMode,this.currentRotation=e.rotation):console.warn("pop() called without matching push()")}reset(){this.currentShader=null,this.stateStack=[],this.currentRotation=0}createShader(e,t){return new y(this.gl,e,t)}createFilterShader(e){return new y(this.gl,T,e)}setUniform(e,t){this.currentShader.setUniform(e,t)}rect(e,t,r,i){if(this.currentShader!==null){if(this.currentRotation!==0){const{centerX:f,centerY:g,radians:x,aspectRatio:p}=this.calculateRotationParams(e,t,r,i);this.setUniform("u_rotation",x),this.setUniform("u_center",[f,g]),this.setUniform("u_aspectRatio",p)}else this.setUniform("u_rotation",0),this.setUniform("u_center",[0,0]),this.setUniform("u_aspectRatio",1);this.drawRectImmediate(e,t,r,i),this.currentShader=null;return}const a=this.solidColorShader,n=this.currentRotation!==0;let l=0,c=0,u=0,d=1;if(n){const f=this.calculateRotationParams(e,t,r,i);l=f.centerX,c=f.centerY,u=f.radians,d=f.aspectRatio}this.fillMode&&(this.shader(a),this.setUniform("u_color",this.currentFillColor),n&&(this.setUniform("u_rotation",u),this.setUniform("u_center",[l,c]),this.setUniform("u_aspectRatio",d)),this.drawRectImmediate(e,t,r,i)),this.strokeMode&&this.currentStrokeWeight>0&&(this.shader(a),this.setUniform("u_color",this.currentStrokeColor),n&&(this.setUniform("u_rotation",u),this.setUniform("u_center",[l,c]),this.setUniform("u_aspectRatio",d)),this.drawRectStrokeImmediate(e,t,r,i,this.currentStrokeWeight))}line(e,t,r,i){if(this.currentShader!==null){const C=(e+r)/2,S=(t+i)/2,w=Math.abs(r-e)||1,F=Math.abs(i-t)||1;if(this.currentRotation!==0){const{centerX:D,centerY:R,radians:E,aspectRatio:U}=this.calculateRotationParams(C-w/2,S-F/2,w,F);this.setUniform("u_rotation",E),this.setUniform("u_center",[D,R]),this.setUniform("u_aspectRatio",U)}else this.setUniform("u_rotation",0),this.setUniform("u_center",[0,0]),this.setUniform("u_aspectRatio",1);const L=this.currentStrokeWeight>0?this.currentStrokeWeight:1;this.drawLineImmediate(e,t,r,i,L),this.currentShader=null;return}if(!this.strokeMode||this.currentStrokeWeight<=0)return;const a=this.solidColorShader,n=(e+r)/2,l=(t+i)/2,c=Math.abs(r-e)||1,u=Math.abs(i-t)||1,d=this.currentRotation!==0;let f=0,g=0,x=0,p=1;if(d){const C=this.calculateRotationParams(n-c/2,l-u/2,c,u);f=C.centerX,g=C.centerY,x=C.radians,p=C.aspectRatio}this.shader(a),this.setUniform("u_color",this.currentStrokeColor),d&&(this.setUniform("u_rotation",x),this.setUniform("u_center",[f,g]),this.setUniform("u_aspectRatio",p)),this.drawLineImmediate(e,t,r,i,this.currentStrokeWeight)}calculateRotationParams(e,t,r,i){const s=q(this.gl)||[0,0,this.gl.canvas.width,this.gl.canvas.height],a=s[2],n=s[3],l=a/n,c=e+r/2,u=t+i/2,d=c/a*2-1,f=1-u/n*2,g=this.currentRotation*Math.PI/180;return{centerX:d,centerY:f,radians:g,aspectRatio:l}}createFramebuffer(e,t,r={}){return new N(this.gl,e,t,r)}background(e,t=e,r=e,i=255){this.clear(e/255,t/255,r/255,i/255)}clear(e=0,t=0,r=0,i=0){this.gl.clearColor(e,t,r,i),this.gl.clear(this.gl.COLOR_BUFFER_BIT)}resetViewport(){this.gl.viewport(0,0,this.gl.canvas.width,this.gl.canvas.height),V(this.gl,[0,0,this.gl.canvas.width,this.gl.canvas.height]),z(this.gl,!1)}get context(){return this.gl}dispose(){this.scratchFramebuffer&&(this.scratchFramebuffer.dispose(),this.scratchFramebuffer=null),this.imageShader.dispose(),this.solidColorShader.dispose(),this.imageShader=null,this.solidColorShader=null,this.currentShader=null,this.stateStack=[]}image(e,t,r,i,s){const a=this.gl,n=i??e.width,l=s??e.height,c=a.getParameter(a.FRAMEBUFFER_BINDING);if(c&&e.framebuffer===c){(!this.scratchFramebuffer||this.scratchFramebuffer.width!==n||this.scratchFramebuffer.height!==l)&&(this.scratchFramebuffer&&this.scratchFramebuffer.dispose(),this.scratchFramebuffer=this.createFramebuffer(n,l)),this.scratchFramebuffer.begin(),this.clear(0,0,0,0),this.shader(this.imageShader),this.setUniform("u_texture",e.texture),this.setUniform("u_rotation",0),this.setUniform("u_center",[0,0]),this.setUniform("u_aspectRatio",1),this.drawRectImmediate(0,0,n,l),this.scratchFramebuffer.end(),this.shader(this.imageShader);const d=this.currentRotation!==0?this.calculateRotationParams(t,r,n,l):{centerX:0,centerY:0,radians:0,aspectRatio:1};this.setUniform("u_texture",this.scratchFramebuffer.texture),this.setUniform("u_rotation",d.radians),this.setUniform("u_center",[d.centerX,d.centerY]),this.setUniform("u_aspectRatio",d.aspectRatio),this.drawRectImmediate(t,r,n,l),a.bindTexture(a.TEXTURE_2D,null),this.currentShader=null;return}this.shader(this.imageShader),this.setUniform("u_texture",e.texture);const u=this.currentRotation!==0?this.calculateRotationParams(t,r,n,l):{centerX:0,centerY:0,radians:0,aspectRatio:1};this.setUniform("u_rotation",u.radians),this.setUniform("u_center",[u.centerX,u.centerY]),this.setUniform("u_aspectRatio",u.aspectRatio),this.drawRectImmediate(t,r,n,l),a.bindTexture(a.TEXTURE_2D,null),this.currentShader=null}ensureUnitBuffer(){if(this.unitBuffer)return;const e=this.gl;this.unitBuffer=e.createBuffer(),e.bindBuffer(e.ARRAY_BUFFER,this.unitBuffer)}enableAttribs(){const e=this.gl,t=e.getParameter(e.CURRENT_PROGRAM);let r=this.attribCache.get(t);return r||(r={a_position:e.getAttribLocation(t,"a_position"),a_texCoord:e.getAttribLocation(t,"a_texCoord")},this.attribCache.set(t,r)),e.enableVertexAttribArray(r.a_position),e.vertexAttribPointer(r.a_position,2,e.FLOAT,!1,this.bytesPerVertex,0),e.enableVertexAttribArray(r.a_texCoord),e.vertexAttribPointer(r.a_texCoord,2,e.FLOAT,!1,this.bytesPerVertex,8),{positionLoc:r.a_position,texLoc:r.a_texCoord}}disableAttribs(e,t){const r=this.gl;r.disableVertexAttribArray(e),r.disableVertexAttribArray(t)}uploadQuadNDC(e,t,r,i){const s=this.gl;this.ensureUnitBuffer(),s.bindBuffer(s.ARRAY_BUFFER,this.unitBuffer);const a=0,n=0,l=1,c=1,u=new Float32Array([e,i,0,a,r,i,1,n,e,t,0,l,e,t,0,l,r,i,1,n,r,t,1,c]);s.bufferData(s.ARRAY_BUFFER,u,s.DYNAMIC_DRAW)}toNDC(e,t){const r=this.gl,i=q(r)||[0,0,r.canvas.width,r.canvas.height],s=i[2],a=i[3],n=e/s*2-1,l=1-t/a*2;return{nx:n,ny:l}}drawRectImmediate(e,t,r,i){const s=this.toNDC(e,t),a=this.toNDC(e+r,t+i);this.uploadQuadNDC(s.nx,s.ny,a.nx,a.ny);const n=this.enableAttribs();this.gl.drawArrays(this.gl.TRIANGLES,0,6),this.disableAttribs(n.positionLoc,n.texLoc)}drawRectStrokeImmediate(e,t,r,i,s){this.drawRectImmediate(e,t,r,s),this.drawRectImmediate(e+r-s,t,s,i),this.drawRectImmediate(e,t+i-s,r,s),this.drawRectImmediate(e,t,s,i)}drawLineImmediate(e,t,r,i,s){const a=r-e,n=i-t,l=Math.hypot(a,n);if(l===0){this.drawRectImmediate(e-s/2,t-s/2,s,s);return}const c=a/l,d=-(n/l),f=c,g=s/2,x=e+d*g,p=t+f*g,C=e-d*g,S=t-f*g,w=r+d*g,F=i+f*g,L=r-d*g,D=i-f*g,R=this.toNDC(x,p),E=this.toNDC(C,S),U=this.toNDC(w,F),ie=this.toNDC(L,D),I=this.gl;this.ensureUnitBuffer(),I.bindBuffer(I.ARRAY_BUFFER,this.unitBuffer);const ke=new Float32Array([R.nx,R.ny,0,0,E.nx,E.ny,0,1,U.nx,U.ny,1,0,E.nx,E.ny,0,1,ie.nx,ie.ny,1,1,U.nx,U.ny,1,0]);I.bufferData(I.ARRAY_BUFFER,ke,I.DYNAMIC_DRAW);const se=this.enableAttribs();I.drawArrays(I.TRIANGLES,0,6),this.disableAttribs(se.positionLoc,se.texLoc)}}var m={};m.parse=function(h){var e=function(s,a,n,l){var c=m.T,u={cmap:c.cmap,head:c.head,hhea:c.hhea,maxp:c.maxp,hmtx:c.hmtx,loca:c.loca,glyf:c.glyf},d={_data:s,_index:a,_offset:n};for(var f in u){var g=m.findTable(s,f,n);if(g){var x=g[0],p=l[x];p==null&&(p=u[f].parseTab(s,x,g[1],d)),d[f]=l[x]=p}}return d},t=new Uint8Array(h),r={},i=e(t,0,0,r);return[i]},m.findTable=function(h,e,t){for(var r=m.B,i=r.readUshort(h,t+4),s=t+12,a=0;a<i;a++){var n=r.readASCII(h,s,4);r.readUint(h,s+4);var l=r.readUint(h,s+8),c=r.readUint(h,s+12);if(n==e)return[l,c];s+=16}return null},m.T={},m.B={readShort:function(h,e){var t=m.B.t.uint16;return t[0]=h[e]<<8|h[e+1],m.B.t.int16[0]},readUshort:function(h,e){return h[e]<<8|h[e+1]},readUshorts:function(h,e,t){for(var r=[],i=0;i<t;i++)r.push(m.B.readUshort(h,e+i*2));return r},readUint:function(h,e){var t=m.B.t.uint8;return t[3]=h[e],t[2]=h[e+1],t[1]=h[e+2],t[0]=h[e+3],m.B.t.uint32[0]},readASCII:function(h,e,t){for(var r="",i=0;i<t;i++)r+=String.fromCharCode(h[e+i]);return r},t:function(){var h=new ArrayBuffer(8);return{uint8:new Uint8Array(h),int16:new Int16Array(h),uint16:new Uint16Array(h),uint32:new Uint32Array(h)}}()},m.T.cmap={parseTab:function(h,e,t){var r={tables:[],ids:{},off:e};h=new Uint8Array(h.buffer,e,t),e=0;var i=m.B,s=i.readUshort,a=m.T.cmap;s(h,e),e+=2;var n=s(h,e);e+=2;for(var l=[],c=0;c<n;c++){var u=s(h,e);e+=2;var d=s(h,e);e+=2;var f=i.readUint(h,e);e+=4;var g="p"+u+"e"+d,x=l.indexOf(f);if(x==-1){x=r.tables.length;var p={};l.push(f);var C=p.format=s(h,f);C==4?p=a.parse4(h,f,p):C==12&&(p=a.parse12(h,f,p)),r.tables.push(p)}r.ids[g]!=null&&console.log("multiple tables for one platform+encoding: "+g),r.ids[g]=x}return r},parse4:function(h,e,t){var r=m.B,i=r.readUshort,s=r.readUshorts,a=e;e+=2;var n=i(h,e);e+=2,i(h,e),e+=2;var l=i(h,e);e+=2;var c=l>>>1;t.searchRange=i(h,e),e+=2,t.entrySelector=i(h,e),e+=2,t.rangeShift=i(h,e),e+=2,t.endCount=s(h,e,c),e+=c*2,e+=2,t.startCount=s(h,e,c),e+=c*2,t.idDelta=[];for(var u=0;u<c;u++)t.idDelta.push(r.readShort(h,e)),e+=2;return t.idRangeOffset=s(h,e,c),e+=c*2,t.glyphIdArray=s(h,e,a+n-e>>1),t},parse12:function(h,e,t){var r=m.B,i=r.readUint;e+=4,i(h,e),e+=4,i(h,e),e+=4;var s=i(h,e)*3;e+=4;for(var a=t.groups=new Uint32Array(s),n=0;n<s;n+=3)a[n]=i(h,e+(n<<2)),a[n+1]=i(h,e+(n<<2)+4),a[n+2]=i(h,e+(n<<2)+8);return t}},m.T.head={parseTab:function(h,e,t){var r=m.B,i={};return e+=18,i.unitsPerEm=r.readUshort(h,e),e+=2,e+=16,i.xMin=r.readShort(h,e),e+=2,i.yMin=r.readShort(h,e),e+=2,i.xMax=r.readShort(h,e),e+=2,i.yMax=r.readShort(h,e),e+=2,e+=6,i.indexToLocFormat=r.readShort(h,e),i}},m.T.hhea={parseTab:function(h,e,t){var r=m.B,i={};e+=4;for(var s=["ascender","descender","lineGap","advanceWidthMax","minLeftSideBearing","minRightSideBearing","xMaxExtent","caretSlopeRise","caretSlopeRun","caretOffset","res0","res1","res2","res3","metricDataFormat","numberOfHMetrics"],a=0;a<s.length;a++){var n=s[a],l=n=="advanceWidthMax"||n=="numberOfHMetrics"?r.readUshort:r.readShort;i[n]=l(h,e+a*2)}return i}},m.T.hmtx={parseTab:function(h,e,t,r){for(var i=m.B,s=[],a=[],n=r.maxp.numGlyphs,l=r.hhea.numberOfHMetrics,c=0,u=0,d=0;d<l;)c=i.readUshort(h,e+(d<<2)),u=i.readShort(h,e+(d<<2)+2),s.push(c),a.push(u),d++;for(;d<n;)s.push(c),a.push(u),d++;return{aWidth:s,lsBearing:a}}},m.T.maxp={parseTab:function(h,e,t){var r=m.B,i=r.readUshort,s={};return r.readUint(h,e),e+=4,s.numGlyphs=i(h,e),e+=2,s}},m.T.loca={parseTab:function(h,e,t,r){var i=m.B,s=[],a=r.head.indexToLocFormat,n=r.maxp.numGlyphs+1;if(a==0)for(var l=0;l<n;l++)s.push(i.readUshort(h,e+(l<<1))<<1);if(a==1)for(var l=0;l<n;l++)s.push(i.readUint(h,e+(l<<2)));return s}},m.T.glyf={parseTab:function(h,e,t,r){for(var i=[],s=r.maxp.numGlyphs,a=0;a<s;a++)i.push(null);return i},_parseGlyf:function(h,e){var t=m.B,r=h._data,i=h.loca;if(i[e]==i[e+1])return null;var s=m.findTable(r,"glyf",h._offset)[0]+i[e],a={};if(a.noc=t.readShort(r,s),s+=2,a.xMin=t.readShort(r,s),s+=2,a.yMin=t.readShort(r,s),s+=2,a.xMax=t.readShort(r,s),s+=2,a.yMax=t.readShort(r,s),s+=2,a.xMin>=a.xMax||a.yMin>=a.yMax)return null;if(a.noc>0){a.endPts=[];for(var n=0;n<a.noc;n++)a.endPts.push(t.readUshort(r,s)),s+=2;var l=t.readUshort(r,s);if(s+=2,r.length-s<l)return null;s+=l;var c=a.endPts[a.noc-1]+1;a.flags=[];for(var n=0;n<c;n++){var u=r[s];if(s++,a.flags.push(u),u&8){var d=r[s];s++;for(var f=0;f<d;f++)a.flags.push(u),n++}}a.xs=[];for(var n=0;n<c;n++){var g=(a.flags[n]&2)!=0,x=(a.flags[n]&16)!=0;g?(a.xs.push(x?r[s]:-r[s]),s++):x?a.xs.push(0):(a.xs.push(t.readShort(r,s)),s+=2)}a.ys=[];for(var n=0;n<c;n++){var g=(a.flags[n]&4)!=0,x=(a.flags[n]&32)!=0;g?(a.ys.push(x?r[s]:-r[s]),s++):x?a.ys.push(0):(a.ys.push(t.readShort(r,s)),s+=2)}for(var p=0,C=0,n=0;n<c;n++)p+=a.xs[n],C+=a.ys[n],a.xs[n]=p,a.ys[n]=C}else a.parts=[];return a}},typeof module<"u"&&module.exports?module.exports=m:typeof window<"u"&&(window.Typr=m);class le{extractCharacters(e){var r;const t=[];return(r=e==null?void 0:e.cmap)!=null&&r.tables?(e.cmap.tables.forEach(i=>{if(i.format===4){const s=this._extractCharactersFromFormat4Table(i);t.push(...s)}else if(i.format===12){const s=this._extractCharactersFromFormat12Table(i);t.push(...s)}}),[...new Set(t)]):[]}_extractCharactersFromFormat4Table(e){const t=[];if(!e.startCount||!e.endCount||!e.idRangeOffset||!e.idDelta)return t;for(let r=0;r<e.startCount.length;r++){const i=e.startCount[r],s=e.endCount[r];if(!(i===65535&&s===65535)){for(let a=i;a<=s;a++)if(this._calculateGlyphIndexFormat4(e,a,r)>0){const l=String.fromCodePoint(a);t.push(l)}}}return t}_extractCharactersFromFormat12Table(e){const t=[];if(!e.groups)return t;for(let r=0;r<e.groups.length;r+=3){const i=e.groups[r],s=e.groups[r+1],a=e.groups[r+2];for(let n=i;n<=s;n++)if(a+(n-i)>0){const c=String.fromCodePoint(n);t.push(c)}}return t}_calculateGlyphIndexFormat4(e,t,r){if(e.idRangeOffset[r]===0)return t+e.idDelta[r]&65535;{const i=e.idRangeOffset[r]/2+(t-e.startCount[r])-(e.startCount.length-r);if(i>=0&&e.glyphIdArray&&i<e.glyphIdArray.length){const s=e.glyphIdArray[i];if(s!==0)return s+e.idDelta[r]&65535}}return 0}filterProblematicCharacters(e){return e.filter(t=>this._isValidCharacter(t))}_isValidCharacter(e){const t=e.codePointAt(0)||0;return!(t>=0&&t<=31&&t!==9&&t!==10&&t!==13||t>=127&&t<=159)}}class ce{constructor(e){o(this,"_textureCanvas");o(this,"_textureContext");o(this,"_renderer");this._renderer=e,this._textureCanvas=document.createElement("canvas"),this._textureContext=this._textureCanvas.getContext("2d",{willReadFrequently:!0,alpha:!1})}createTextureAtlas(e,t,r,i){const s=e.length,a=Math.ceil(Math.sqrt(s)),n=Math.ceil(s/a),l=t.width*a,c=t.height*n;this._setupCanvas(l,c,r,i),this._renderCharactersToCanvas(e,t,a,r),this._applyBlackWhiteThreshold();const u=this._renderer.createFramebuffer(l,c,{filter:"nearest"});return u.update(this._textureCanvas),{framebuffer:u,columns:a,rows:n}}_setupCanvas(e,t,r,i){this._textureCanvas.width=e,this._textureCanvas.height=t,this._textureCanvas.style.width=e+"px",this._textureCanvas.style.height=e+"px",this._textureContext.imageSmoothingEnabled=!1,this._textureCanvas.style.imageRendering="pixelated",this._textureContext.fillStyle="black",this._textureContext.fillRect(0,0,e,t),this._textureContext.font=`${r}px ${i}`,this._textureContext.textBaseline="top",this._textureContext.textAlign="left",this._textureContext.fillStyle="white"}_renderCharactersToCanvas(e,t,r,i){for(let s=0;s<e.length;s++){const a=s%r,n=Math.floor(s/r),l=a*t.width+t.width*.5,c=n*t.height+t.height*.5,u=Math.round(l-t.width*.5),d=Math.round(c-i*.5);this._textureContext.fillText(e[s].character,u,d)}}_applyBlackWhiteThreshold(e=128){const t=this._textureContext.getImageData(0,0,this._textureCanvas.width,this._textureCanvas.height),r=t.data;for(let i=0;i<r.length;i+=4){const s=.299*r[i]+.587*r[i+1]+.114*r[i+2],a=e+32,n=s>a?255:0;r[i]=n,r[i+1]=n,r[i+2]=n}this._textureContext.putImageData(t,0,0)}}class ue{constructor(){o(this,"_tempCanvas");o(this,"_tempContext");this._tempCanvas=document.createElement("canvas"),this._tempContext=this._tempCanvas.getContext("2d")}calculateMaxGlyphDimensions(e,t,r){this._tempContext.font=`${t}px ${r}`;let i=0,s=0;for(const a of e){const n=this._tempContext.measureText(a),l=n.width,c=n.actualBoundingBoxAscent+n.actualBoundingBoxDescent;l>0&&(i=Math.max(i,l),s=Math.max(s,c))}return{width:Math.ceil(i),height:Math.ceil(s)}}}class de{createCharacterObjects(e,t){return e.map((r,i)=>{const s=r.codePointAt(0)||0,a=this._generateCharacterColor(i);let n=0;if(t.hmtx&&t.hmtx.aWidth){const l=this._getGlyphIndex(t,s);l>0&&t.hmtx.aWidth[l]!==void 0&&(n=t.hmtx.aWidth[l])}return{character:r,unicode:s,color:a,advanceWidth:n}})}_getGlyphIndex(e,t){const r=e.cmap;if(!r||!r.tables)return 0;for(const i of r.tables)if(i.format===4){for(let s=0;s<i.startCount.length;s++)if(t>=i.startCount[s]&&t<=i.endCount[s]){if(i.idRangeOffset[s]===0)return t+i.idDelta[s]&65535;{const a=i.idRangeOffset[s]/2+(t-i.startCount[s])-(i.startCount.length-s);if(a>=0&&a<i.glyphIdArray.length){const n=i.glyphIdArray[a];if(n!==0)return n+i.idDelta[s]&65535}}}}return 0}_generateCharacterColor(e){const t=e%256,r=Math.floor(e/256)%256,i=Math.floor(e/65536)%256;return[t,r,i]}getCharacterColor(e,t){if(!b.validate(typeof e=="string"&&e.length===1,"Character must be a single character string.",{method:"getCharacterColor",providedValue:e}))return[0,0,0];const r=t.find(i=>i.character===e);return r?r.color:[0,0,0]}getCharacterColors(e,t){return b.validate(typeof e=="string"&&e.length>0,"Characters must be a string with at least one character.",{method:"getCharacterColors",providedValue:e})?e.split("").map(r=>this.getCharacterColor(r,t)||[0,0,0]):[[0,0,0]]}}class K{constructor(e,t=16){o(this,"_font");o(this,"_characters",[]);o(this,"_fontFramebuffer");o(this,"_fontSize",16);o(this,"_textureColumns",0);o(this,"_textureRows",0);o(this,"_maxGlyphDimensions",{width:0,height:0});o(this,"_fontFace");o(this,"_fontFamilyName","UrsaFont");o(this,"_characterExtractor");o(this,"_textureAtlas");o(this,"_metricsCalculator");o(this,"_characterColorMapper");this._fontSize=t,this._characterExtractor=new le,this._textureAtlas=new ce(e),this._metricsCalculator=new ue,this._characterColorMapper=new de}async initialize(e){let t;if(e){const r=await fetch(e);if(!r.ok)throw new _(`Failed to load font file: ${r.status} ${r.statusText}`);t=await r.arrayBuffer()}else throw new _("Embedded font not available. This appears to be a minified build - please provide `fontSource`.");await this._loadFontFace(t),this._font=m.parse(t)[0],await this._initializeFont()}setFontSize(e){if(e===void 0)return this._fontSize;this._fontSize=e,this._maxGlyphDimensions=this._metricsCalculator.calculateMaxGlyphDimensions(this._characters.map(r=>r.character),this._fontSize,this._fontFamilyName);const t=this._textureAtlas.createTextureAtlas(this._characters,this._maxGlyphDimensions,this._fontSize,this._fontFamilyName);this._fontFramebuffer=t.framebuffer,this._textureColumns=t.columns,this._textureRows=t.rows}async loadFont(e){try{const t=await fetch(e);if(!t.ok)throw new _(`Failed to load font file: ${t.status} ${t.statusText}`);const r=await t.arrayBuffer();await this._loadFontFace(r);const i=m.parse(r);if(!i||i.length===0)throw new Error("Failed to parse font file");this._font=i[0],await this._initializeFont()}catch(t){throw new _(`Failed to load font: ${t instanceof Error?t.message:"Unknown error"}`,t)}}async _loadFontFace(e){const t=Date.now();this._fontFamilyName=this._fontFamilyName==="UrsaFont"?"UrsaFont":`CustomFont_${t}`,this._fontFace=new FontFace(this._fontFamilyName,e),await this._fontFace.load(),document.fonts.add(this._fontFace)}async _initializeFont(){const e=this._characterExtractor.extractCharacters(this._font),t=this._characterExtractor.filterProblematicCharacters(e);this._characters=this._characterColorMapper.createCharacterObjects(t,this._font),this._maxGlyphDimensions=this._metricsCalculator.calculateMaxGlyphDimensions(t,this._fontSize,this._fontFamilyName);const r=this._textureAtlas.createTextureAtlas(this._characters,this._maxGlyphDimensions,this._fontSize,this._fontFamilyName);this._fontFramebuffer=r.framebuffer,this._textureColumns=r.columns,this._textureRows=r.rows}getCharacterColor(e){return this._characterColorMapper.getCharacterColor(e,this._characters)}getCharacterColors(e){return this._characterColorMapper.getCharacterColors(e,this._characters)}hasAllCharacters(e){if(typeof e!="string"||e.length===0)return!1;const t=new Set(this._characters.map(r=>r.character));for(const r of e)if(!t.has(r))return!1;return!0}get fontFramebuffer(){return this._fontFramebuffer}get characters(){return this._characters}get textureColumns(){return this._textureColumns}get textureRows(){return this._textureRows}get maxGlyphDimensions(){return this._maxGlyphDimensions}dispose(){this._fontFramebuffer.dispose(),document.fonts.delete(this._fontFace),this._fontFramebuffer=null,this._fontFace=null,this._font=null,this._characters=[],this._maxGlyphDimensions={width:0,height:0},this._textureColumns=0,this._textureRows=0}get fontSize(){return this._fontSize}get font(){return this._font}}class Q{constructor(e,t,r){o(this,"_cols");o(this,"_rows");o(this,"_width");o(this,"_height");o(this,"_offsetX");o(this,"_offsetY");o(this,"_fixedDimensions",!1);o(this,"_canvas");o(this,"_cellWidth");o(this,"_cellHeight");this._canvas=e,this._cellWidth=t,this._cellHeight=r,this.reset()}reset(){if(!this._fixedDimensions){const e=this._canvas.width,t=this._canvas.height;[this._cols,this._rows]=[Math.floor(e/this._cellWidth),Math.floor(t/this._cellHeight)]}this._resizeGrid()}_resizeGrid(){const e=this._canvas.width,t=this._canvas.height;this._width=this._cols*this._cellWidth,this._height=this._rows*this._cellHeight,this._offsetX=Math.floor((e-this._width)/2),this._offsetY=Math.floor((t-this._height)/2)}resizeCellPixelDimensions(e,t){[this._cellWidth,this._cellHeight]=[e,t],this.reset()}resizeGridDimensions(e,t){this._fixedDimensions=!0,[this._cols,this._rows]=[e,t],this._resizeGrid()}resetGridDimensions(){this._fixedDimensions=!1,this.reset()}resize(){this._fixedDimensions?this._resizeGrid():this.reset()}fixedDimensions(e){if(e===void 0)return this._fixedDimensions;this._fixedDimensions=e}get cellWidth(){return this._cellWidth}get cellHeight(){return this._cellHeight}dispose(){this._canvas=null,this._cols=0,this._rows=0,this._width=0,this._height=0,this._offsetX=0,this._offsetY=0,this._cellWidth=0,this._cellHeight=0}get cols(){return this._cols}get rows(){return this._rows}get width(){return this._width}get height(){return this._height}get offsetX(){return this._offsetX}get offsetY(){return this._offsetY}}class Z{constructor(e,t=!1,r={}){o(this,"_canvas");o(this,"captureSource");o(this,"_isStandalone");o(this,"resizeObserver");o(this,"onTransformChange");this.captureSource=e,this._isStandalone=t,this._canvas=this.createCanvas(r.width,r.height),t&&this.setupTransformObserver()}createCanvas(e,t){var i;const r=document.createElement("canvas");if(r.className="textmodeCanvas",r.style.imageRendering="pixelated",this._isStandalone)r.width=e||800,r.height=t||600,document.body.appendChild(r);else{const s=this.captureSource.getBoundingClientRect();let a=Math.round(s.width),n=Math.round(s.height);if(this.captureSource instanceof HTMLVideoElement){const u=this.captureSource;(a===0||n===0)&&u.videoWidth>0&&u.videoHeight>0&&(a=u.videoWidth,n=u.videoHeight)}r.width=a,r.height=n,r.style.position="absolute",r.style.pointerEvents="none";const l=window.getComputedStyle(this.captureSource);let c=parseInt(l.zIndex||"0",10);isNaN(c)&&(c=0),r.style.zIndex=(c+1).toString(),this.positionOverlayCanvas(r),(i=this.captureSource.parentNode)==null||i.insertBefore(r,this.captureSource.nextSibling)}return r}positionOverlayCanvas(e){const t=this.captureSource.getBoundingClientRect();let r=this.captureSource.offsetParent;if(r&&r!==document.body){const i=r.getBoundingClientRect();e.style.top=t.top-i.top+"px",e.style.left=t.left-i.left+"px"}else e.style.top=t.top+window.scrollY+"px",e.style.left=t.left+window.scrollX+"px"}resize(e,t){if(this._isStandalone)this._canvas.width=e??this._canvas.width,this._canvas.height=t??this._canvas.height;else{const r=this.captureSource.getBoundingClientRect();let i=Math.round(r.width),s=Math.round(r.height);if(this.captureSource instanceof HTMLVideoElement){const a=this.captureSource;(i===0||s===0)&&a.videoWidth>0&&a.videoHeight>0&&(i=a.videoWidth,s=a.videoHeight)}this._canvas.width=i,this._canvas.height=s,this.positionOverlayCanvas(this._canvas)}}getWebGLContext(){const e={alpha:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!0,antialias:!1,depth:!1,stencil:!1,powerPreference:"high-performance"},t=this._canvas.getContext("webgl2",e)||this._canvas.getContext("webgl",e);if(!t)throw new _("WebGL context could not be created. Ensure your browser supports WebGL.");return t}getEffectiveRenderingDimensions(){return this._canvas?{width:this._canvas.width,height:this._canvas.height}:{width:0,height:0}}isTransformed(){if(!this._canvas||!this._isStandalone)return!1;const e=this._canvas.getBoundingClientRect(),t=Math.round(e.width),r=Math.round(e.height);return t!==this._canvas.width||r!==this._canvas.height}setupTransformObserver(){typeof ResizeObserver>"u"||(this.resizeObserver=new ResizeObserver(e=>{for(const t of e){const r=t.contentRect,i=Math.round(r.width),s=Math.round(r.height);this.onTransformChange&&(i!==this._canvas.width||s!==this._canvas.height)&&this.onTransformChange()}}),this.resizeObserver.observe(this._canvas))}dispose(){if(this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=void 0),this._canvas){const e=this._canvas.getContext("webgl")||this._canvas.getContext("webgl2");if(e){const t=e.getExtension("WEBGL_lose_context");t&&t.loseContext()}this._canvas.parentNode&&this._canvas.parentNode.removeChild(this._canvas),this._canvas=null}this.captureSource=null}get canvas(){return this._canvas}get width(){return this._canvas.width}get height(){return this._canvas.height}}class G{constructor(e,t,r,i={}){o(this,"renderer");o(this,"fontManager");o(this,"grid");o(this,"_characterFramebuffer");o(this,"_primaryColorFramebuffer");o(this,"_secondaryColorFramebuffer");o(this,"_rotationFramebuffer");o(this,"_transformFramebuffer");o(this,"_options");this.renderer=e,this.fontManager=t,this.grid=r,this._options=i,this._characterFramebuffer=this.renderer.createFramebuffer(this.grid.cols,this.grid.rows),this._primaryColorFramebuffer=this.renderer.createFramebuffer(this.grid.cols,this.grid.rows),this._secondaryColorFramebuffer=this.renderer.createFramebuffer(this.grid.cols,this.grid.rows),this._rotationFramebuffer=this.renderer.createFramebuffer(this.grid.cols,this.grid.rows),this._transformFramebuffer=this.renderer.createFramebuffer(this.grid.cols,this.grid.rows)}resize(){this._characterFramebuffer.resize(this.grid.cols,this.grid.rows),this._primaryColorFramebuffer.resize(this.grid.cols,this.grid.rows),this._secondaryColorFramebuffer.resize(this.grid.cols,this.grid.rows),this._rotationFramebuffer.resize(this.grid.cols,this.grid.rows),this._transformFramebuffer.resize(this.grid.cols,this.grid.rows)}enabled(e){this._options.enabled=e}enable(){this.enabled(!0)}disable(){this.enabled(!1)}dispose(){this._characterFramebuffer.dispose(),this._primaryColorFramebuffer.dispose(),this._secondaryColorFramebuffer.dispose(),this._rotationFramebuffer.dispose(),this._transformFramebuffer.dispose(),this._characterFramebuffer=null,this._primaryColorFramebuffer=null,this._secondaryColorFramebuffer=null,this._rotationFramebuffer=null,this._transformFramebuffer=null}get characterFramebuffer(){return this._characterFramebuffer}get primaryColorFramebuffer(){return this._primaryColorFramebuffer}get secondaryColorFramebuffer(){return this._secondaryColorFramebuffer}get rotationFramebuffer(){return this._rotationFramebuffer}get transformFramebuffer(){return this._transformFramebuffer}get options(){return this._options}}class fe{constructor(e,t){o(this,"_framebuffer");o(this,"_renderer");o(this,"_colors");this._renderer=e,this._colors=t;const r=Math.max(this._colors.length,1);this._framebuffer=this._renderer.createFramebuffer(r,1),this._updateFramebuffer()}_updateFramebuffer(){if(!this._framebuffer)return;const e=Math.max(this._colors.length,1),t=1;this._framebuffer.width!==e&&this._framebuffer.resize(e,t);const r=new Uint8Array(e*t*4);for(let i=0;i<e;i++){const s=i<this._colors.length?this._colors[i]:[0,0,0],a=i*4;r[a]=s[0],r[a+1]=s[1],r[a+2]=s[2],r[a+3]=255}this._framebuffer.updatePixels(r,e,t)}setColors(e){this._colors=e,this._updateFramebuffer()}get colors(){return this._colors}get framebuffer(){return this._framebuffer}get texture(){return this._framebuffer.texture}}class H extends G{constructor(t,r,i,s={}){super(t,r,i,s);o(this,"palette");this.palette=new fe(this.renderer,this.fontManager.getCharacterColors(" .:-=+*%@#"))}characters(t){b.validate(this.fontManager.hasAllCharacters(t),"One or more characters do not exist in the current font.",{method:"characters",providedValue:t})&&(this._options.characters=t,this.palette.setColors(this.fontManager.getCharacterColors(t)))}characterColor(t,r,i,s=255){let a,n,l,c;if(typeof t=="string"){const u=this.parseHexColor(t);if(!u){b.validate(!1,"Invalid hex color format. Use '#FF0000', '#F00', 'FF0000', or 'F00'.",{method:"characterColor",providedValue:t});return}[a,n,l,c]=u}else if(a=t,n=r!==void 0?r:t,l=i!==void 0?i:t,c=s,!b.validate([a,n,l,c].every(u=>u>=0&&u<=255),"Character color values must be between 0 and 255",{method:"characterColor",providedValues:{r:a,g:n,b:l,a:c}}))return;this._options.characterColor=[a/255,n/255,l/255,c/255]}characterColorMode(t){b.validate(["sampled","fixed"].includes(t),"Invalid character color mode. Must be 'sampled' or 'fixed'.",{method:"characterColorMode",providedValue:t})&&(this._options.characterColorMode=t)}cellColor(t,r,i,s=255){let a,n,l,c;if(typeof t=="string"){const u=this.parseHexColor(t);if(!u){b.validate(!1,"Invalid hex color format. Use '#FF0000', '#F00', 'FF0000', or 'F00'.",{method:"cellColor",providedValue:t});return}[a,n,l,c]=u}else if(a=t,n=r!==void 0?r:t,l=i!==void 0?i:t,c=s,!b.validate([a,n,l,c].every(u=>u>=0&&u<=255),"Cell color values must be between 0 and 255",{method:"cellColor",providedValues:{r:a,g:n,b:l,a:c}}))return;this._options.cellColor=[a/255,n/255,l/255,c/255]}cellColorMode(t){b.validate(["sampled","fixed"].includes(t),"Invalid cell color mode. Must be 'sampled' or 'fixed'.",{method:"cellColorMode",providedValue:t})&&(this._options.cellColorMode=t)}invert(t){b.validate(typeof t=="boolean"||typeof t=="number"&&Number.isInteger(t),"Invert must be a boolean value or an integer (0 for false, any other number for true).",{method:"invert",providedValue:t})&&(this._options.invert=!!t)}rotation(t){if(!b.validate(typeof t=="number","Rotation angle must be a number.",{method:"rotation",providedValue:t}))return;t=t%360,t<0&&(t+=360);const r=t*255/360,i=Math.floor(r)/255,s=Math.round(r-i);this._options.rotation=[i,s,0,1]}flipHorizontally(t){b.validate(typeof t=="boolean"||typeof t=="number"&&Number.isInteger(t),"Flip horizontally must be a boolean value or an integer (0 for false, any other number for true).",{method:"flipHorizontally",providedValue:t})&&(this._options.flipHorizontally=!!t)}flipVertically(t){b.validate(typeof t=="boolean"||typeof t=="number"&&Number.isInteger(t),"Flip vertically must be a boolean value or an integer (0 for false, any other number for true).",{method:"flipVertically",providedValue:t})&&(this._options.flipVertically=!!t)}parseHexColor(t){if(t=t.replace(/^#/,""),!/^[0-9A-Fa-f]{3}$|^[0-9A-Fa-f]{6}$/.test(t))return null;t.length===3&&(t=t.split("").map(a=>a+a).join(""));const r=parseInt(t.slice(0,2),16),i=parseInt(t.slice(2,4),16),s=parseInt(t.slice(4,6),16);return[r,i,s,255]}}var me="precision lowp float;uniform sampler2D u_sketchTexture;uniform vec2 u_gridCellDimensions;uniform vec2 u_brightnessRange;varying vec2 v_uv;void main(){vec2 cellCenter=(floor(v_uv*u_gridCellDimensions)+vec2(0.5))/u_gridCellDimensions;vec4 color=texture2D(u_sketchTexture,cellCenter);float brightness=dot(color.rgb,vec3(0.299,0.587,0.114));float brightnessValue=brightness*255.0;if(brightnessValue>=u_brightnessRange.x&&brightnessValue<=u_brightnessRange.y){gl_FragColor=color;}else{gl_FragColor=vec4(0.0);}}",ge="precision lowp float;uniform sampler2D u_sampleTexture;uniform vec4 u_fillColor;uniform bool u_useFixedColor;varying vec2 v_uv;void main(){vec4 sampleColor=texture2D(u_sampleTexture,v_uv);if(sampleColor.a>0.0){if(u_useFixedColor){gl_FragColor=u_fillColor;}else{gl_FragColor=sampleColor;}}else{gl_FragColor=vec4(0.0);}}",pe="precision lowp float;uniform sampler2D u_sampleTexture;uniform bool u_invert;uniform bool u_flipHorizontally;uniform bool u_flipVertically;varying vec2 v_uv;void main(){vec4 sampleColor=texture2D(u_sampleTexture,v_uv);if(sampleColor.a>0.0){float invertValue=u_invert ? 1.0 : 0.0;float flipHValue=u_flipHorizontally ? 1.0 : 0.0;float flipVValue=u_flipVertically ? 1.0 : 0.0;gl_FragColor=vec4(invertValue,flipHValue,flipVValue,1.0);}else{gl_FragColor=vec4(0.0);}}",_e="precision lowp float;uniform sampler2D u_sampleTexture;uniform vec4 u_rotationColor;varying vec2 v_uv;void main(){vec4 sampleColor=texture2D(u_sampleTexture,v_uv);if(sampleColor.a>0.0){gl_FragColor=u_rotationColor;}else{gl_FragColor=vec4(0.0);}}",ve="precision lowp float;uniform sampler2D u_colorSampleFramebuffer;uniform sampler2D u_charPaletteTexture;uniform vec2 u_charPaletteSize;uniform vec2 u_brightnessRange;varying vec2 v_uv;void main(){vec4 color=texture2D(u_colorSampleFramebuffer,v_uv);if(color.a==0.0){gl_FragColor=vec4(0.0);return;}float brightness=dot(color.rgb,vec3(0.299,0.587,0.114))*255.0;vec2 range=u_brightnessRange;if(brightness<range.x||brightness>range.y){gl_FragColor=vec4(0.0);return;}float t=(brightness-range.x)/(range.y-range.x);float idx=clamp(floor(t*u_charPaletteSize.x),0.0,u_charPaletteSize.x-1.0);vec3 charColor=texture2D(u_charPaletteTexture,vec2((idx+0.5)/u_charPaletteSize.x,0.0)).rgb;gl_FragColor=vec4(charColor,1.0);}";const be={enabled:!0,characters:" .:-=+*%@#",characterColor:[1,1,1,1],characterColorMode:"sampled",cellColor:[0,0,0,1],cellColorMode:"fixed",invert:!1,rotation:[0,0,0,255],flipHorizontally:!1,flipVertically:!1,brightnessRange:[0,255]};class O extends H{constructor(t,r,i){super(t,r,i,{...be});o(this,"sampleShader");o(this,"colorFillShader");o(this,"charMappingShader");o(this,"transformFillShader");o(this,"rotationFillShader");o(this,"sampleFramebuffer");this.sampleShader=new y(t.context,T,me),this.colorFillShader=new y(t.context,T,ge),this.transformFillShader=new y(t.context,T,pe),this.rotationFillShader=new y(t.context,T,_e),this.charMappingShader=new y(t.context,T,ve),this.sampleFramebuffer=this.renderer.createFramebuffer(this.grid.cols,this.grid.rows)}convert(t){this.sampleFramebuffer.begin(),this.renderer.clear(),this.renderer.shader(this.sampleShader),this.renderer.setUniform("u_sketchTexture",t),this.renderer.setUniform("u_gridCellDimensions",[this.grid.cols,this.grid.rows]),this.renderer.setUniform("u_brightnessRange",this._options.brightnessRange),this.renderer.rect(0,0,this.grid.cols,this.grid.rows),this.sampleFramebuffer.end(),this._primaryColorFramebuffer.begin(),this.renderer.clear(),this.renderer.shader(this.colorFillShader),this.renderer.setUniform("u_sampleTexture",this.sampleFramebuffer.texture),this.renderer.setUniform("u_fillColor",this._options.characterColor),this.renderer.setUniform("u_useFixedColor",this._options.characterColorMode==="fixed"),this.renderer.rect(0,0,this.grid.cols,this.grid.rows),this._primaryColorFramebuffer.end(),this._secondaryColorFramebuffer.begin(),this.renderer.clear(),this.renderer.shader(this.colorFillShader),this.renderer.setUniform("u_sampleTexture",this.sampleFramebuffer.texture),this.renderer.setUniform("u_fillColor",this._options.cellColor),this.renderer.setUniform("u_useFixedColor",this._options.cellColorMode==="fixed"),this.renderer.rect(0,0,this.grid.cols,this.grid.rows),this._secondaryColorFramebuffer.end(),this._transformFramebuffer.begin(),this.renderer.clear(),this.renderer.shader(this.transformFillShader),this.renderer.setUniform("u_sampleTexture",this.sampleFramebuffer.texture),this.renderer.setUniform("u_invert",this._options.invert),this.renderer.setUniform("u_flipHorizontally",this._options.flipHorizontally),this.renderer.setUniform("u_flipVertically",this._options.flipVertically),this.renderer.rect(0,0,this.grid.cols,this.grid.rows),this._transformFramebuffer.end(),this._rotationFramebuffer.begin(),this.renderer.clear(),this.renderer.shader(this.rotationFillShader),this.renderer.setUniform("u_sampleTexture",this.sampleFramebuffer.texture),this.renderer.setUniform("u_rotationColor",this._options.rotation),this.renderer.rect(0,0,this.grid.cols,this.grid.rows),this._rotationFramebuffer.end(),this._characterFramebuffer.begin(),this.renderer.clear(0,0,0,0),this.renderer.shader(this.charMappingShader),this.renderer.setUniform("u_colorSampleFramebuffer",this.sampleFramebuffer.texture),this.renderer.setUniform("u_charPaletteTexture",this.palette.texture),this.renderer.setUniform("u_charPaletteSize",[this.palette.colors.length,1]),this.renderer.setUniform("u_brightnessRange",this._options.brightnessRange),this.renderer.rect(0,0,this.grid.cols,this.grid.rows),this._characterFramebuffer.end()}resize(){super.resize(),this.sampleFramebuffer.resize(this.grid.cols,this.grid.rows)}brightnessRange(t){b.validate(Array.isArray(t)&&t.length===2&&t.every(r=>typeof r=="number"&&r>=0&&r<=255),"Brightness range must be an array of two numbers between 0 and 255.",{method:"brightnessRange",providedValue:t})&&(this._options.brightnessRange=t)}}const xe=Object.freeze(Object.defineProperty({__proto__:null,TextmodeBrightnessConverter:O,TextmodeConverter:G,TextmodeFeatureConverter:H},Symbol.toStringTag,{value:"Module"}));var Ce="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 sampler2D u_captureTexture;uniform vec2 u_captureDimensions;uniform int u_backgroundMode;uniform vec2 u_gridCellDimensions;uniform vec2 u_gridPixelDimensions;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_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);if(encodedIndexVec.a<0.01){gl_FragColor=(u_backgroundMode==0)? vec4(0.0):texture2D(u_captureTexture,gl_FragCoord.xy/u_captureDimensions);return;}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);}";class J{constructor(e,t,r){o(this,"renderer");o(this,"font");o(this,"grid");o(this,"converters");o(this,"_resultFramebuffer");o(this,"_asciiShader");o(this,"_characterFramebuffer");o(this,"_primaryColorFramebuffer");o(this,"_secondaryColorFramebuffer");o(this,"_rotationFramebuffer");o(this,"_transformFramebuffer");this.renderer=e,this.font=t,this.grid=r,this._asciiShader=this.renderer.createShader(T,Ce),this.converters=[{name:"brightness",converter:new O(e,t,r)},{name:"custom",converter:new G(e,t,r)}],this._characterFramebuffer=this.renderer.createFramebuffer(r.cols,r.rows),this._primaryColorFramebuffer=this.renderer.createFramebuffer(r.cols,r.rows),this._secondaryColorFramebuffer=this.renderer.createFramebuffer(r.cols,r.rows),this._rotationFramebuffer=this.renderer.createFramebuffer(r.cols,r.rows),this._transformFramebuffer=this.renderer.createFramebuffer(r.cols,r.rows),this._resultFramebuffer=this.renderer.createFramebuffer(this.grid.width,this.grid.height)}render(e){for(const r of this.converters){const i=r.converter;i.options.enabled&&i instanceof H&&i.convert(e)}const t=(r,i)=>{r.begin(),this.renderer.clear();for(const s of this.converters){const a=s.converter;a.options.enabled&&this.renderer.image(i(a),0,0)}r.end()};t(this._characterFramebuffer,r=>r.characterFramebuffer),t(this._primaryColorFramebuffer,r=>r.primaryColorFramebuffer),t(this._secondaryColorFramebuffer,r=>r.secondaryColorFramebuffer),t(this._rotationFramebuffer,r=>r.rotationFramebuffer),t(this._transformFramebuffer,r=>r.transformFramebuffer),this._resultFramebuffer.begin(),this.renderer.clear(),this.renderer.shader(this._asciiShader),this.renderer.setUniform("u_characterTexture",this.font.fontFramebuffer),this.renderer.setUniform("u_charsetDimensions",[this.font.textureColumns,this.font.textureRows]),this.renderer.setUniform("u_asciiCharacterTexture",this._characterFramebuffer.texture),this.renderer.setUniform("u_primaryColorTexture",this._primaryColorFramebuffer.texture),this.renderer.setUniform("u_secondaryColorTexture",this._secondaryColorFramebuffer.texture),this.renderer.setUniform("u_transformTexture",this._transformFramebuffer.texture),this.renderer.setUniform("u_rotationTexture",this._rotationFramebuffer.texture),this.renderer.setUniform("u_captureTexture",e.texture),this.renderer.setUniform("u_backgroundMode",!1),this.renderer.setUniform("u_captureDimensions",[e.width,e.height]),this.renderer.setUniform("u_gridCellDimensions",[this.grid.cols,this.grid.rows]),this.renderer.setUniform("u_gridPixelDimensions",[this.grid.width,this.grid.height]),this.renderer.rect(0,0,this._resultFramebuffer.width,this._resultFramebuffer.height),this._resultFramebuffer.end()}get(e){if(!b.validate(typeof e=="string"&&e.length>0,"Converter name must be a non-empty string.",{method:"converter",providedValue:e}))return;const t=this.converters.find(i=>i.name===e),r=t==null?void 0:t.converter;if(b.validate(r instanceof G,`Converter "${e}" is not a valid TextmodeConverter.`,{method:"converter",providedValue:e,converterInstance:r}))return r}add(e,t){if(!b.validate(typeof e=="string"&&e.length>0,"Converter name must be a non-empty string.",{method:"add",providedValue:e})||!b.validate(t==="brightness"||t==="custom",`Converter type must be either "brightness" or "custom". Provided: ${t}`,{method:"add",providedValue:t}))return;let r;return t==="brightness"?r=new O(this.renderer,this.font,this.grid):r=new G(this.renderer,this.font,this.grid),this.converters.push({name:e,converter:r}),r}remove(e){if(!b.validate(typeof e=="string"||e instanceof G,"Parameter must be either a string (converter name) or a TextmodeConverter instance.",{method:"remove",providedValue:e}))return;let t=-1;if(typeof e=="string"){if(!b.validate(e.length>0,"Converter name must be a non-empty string.",{method:"remove",providedValue:e}))return;t=this.converters.findIndex(r=>r.name===e)}else t=this.converters.findIndex(r=>r.converter===e);b.validate(t!==-1,typeof e=="string"?`Converter with name "${e}" not found in pipeline.`:"Converter instance not found in pipeline.",{method:"remove",providedValue:e,convertersCount:this.converters.length})&&this.converters.splice(t,1)}get texture(){return this._resultFramebuffer}resize(){this._resultFramebuffer.resize(this.grid.width,this.grid.height),this._characterFramebuffer.resize(this.grid.cols,this.grid.rows),this._primaryColorFramebuffer.resize(this.grid.cols,this.grid.rows),this._secondaryColorFramebuffer.resize(this.grid.cols,this.grid.rows),this._rotationFramebuffer.resize(this.grid.cols,this.grid.rows),this._transformFramebuffer.resize(this.grid.cols,this.grid.rows);for(const e of this.converters)e.converter.resize()}hasEnabledConverters(){return this.converters.some(e=>e.converter.options.enabled)}disable(){for(const e of this.converters)e.converter.disable()}enable(){for(const e of this.converters)e.converter.enable()}dispose(){for(const e of this.converters)e.converter.dispose();this._characterFramebuffer.dispose(),this._primaryColorFramebuffer.dispose(),this._secondaryColorFramebuffer.dispose(),this._rotationFramebuffer.dispose(),this._transformFramebuffer.dispose(),this._resultFramebuffer.dispose(),this._asciiShader.dispose(),this.converters=[],this._characterFramebuffer=null,this._primaryColorFramebuffer=null,this._secondaryColorFramebuffer=null,this._rotationFramebuffer=null,this._transformFramebuffer=null,this._resultFramebuffer=null,this._asciiShader=null}get characterFramebuffer(){return this._characterFramebuffer}get primaryColorFramebuffer(){return this._primaryColorFramebuffer}get secondaryColorFramebuffer(){return this._secondaryColorFramebuffer}get rotationFramebuffer(){return this._rotationFramebuffer}get transformFramebuffer(){return this._transformFramebuffer}}class X{extractFramebufferData(e){const t=e.get("brightness"),r=t==null?void 0:t.characterFramebuffer,i=t==null?void 0:t.primaryColorFramebuffer,s=t==null?void 0:t.secondaryColorFramebuffer,a=t==null?void 0:t.transformFramebuffer,n=t==null?void 0:t.rotationFramebuffer;return r==null||r.loadPixels(),i==null||i.loadPixels(),s==null||s.loadPixels(),a==null||a.loadPixels(),n==null||n.loadPixels(),{characterPixels:(r==null?void 0:r.pixels)||new Uint8Array(0),primaryColorPixels:(i==null?void 0:i.pixels)||new Uint8Array(0),secondaryColorPixels:(s==null?void 0:s.pixels)||new Uint8Array(0),transformPixels:(a==null?void 0:a.pixels)||new Uint8Array(0),rotationPixels:(n==null?void 0:n.pixels)||new Uint8Array(0)}}getCharacterIndex(e,t){const r=e[t],i=e[t+1];return r+(i<<8)}pixelsToRGBA(e,t){return{r:e[t],g:e[t+1],b:e[t+2],a:e[t+3]}}}class W{createBlob(e,t){return new Blob([e],{type:t})}downloadFile(e,t,r){try{const i=this.createBlob(e,r),s=URL.createObjectURL(i),a=document.createElement("a");a.href=s,a.download=t,a.style.display="none",a.rel="noopener",document.body.appendChild(a),a.click(),document.body.removeChild(a),URL.revokeObjectURL(s)}catch(i){throw console.error("Failed to download file:",i),new Error(`File download failed: ${i instanceof Error?i.message:"Unknown error"}`)}}generateTimestamp(){return new Date().toISOString().slice(0,19).replace(/:/g,"-")}generateDateTimeString(){const e=new Date,t=e.toISOString().split("T")[0],r=e.toTimeString().split(" ")[0].replace(/:/g,"-");return{date:t,time:r}}sanitizeFilename(e){return e.replace(/[<>:"/\\|?*]/g,"_").replace(/\s+/g,"_").replace(/_{2,}/g,"_").replace(/^_+|_+$/g,"").substring(0,255)}generateDefaultFilename(){return`'textmode-export'-${this.generateTimestamp()}`}}class Fe extends X{extractTransformData(e,t,r){const i=e[r],s=e[r+1],a=e[r+2],n=i===255,l=s===255,c=a===255,u=t[r],d=t[r+1],f=u+d/255,g=Math.round(f*360/255*100)/100;return{isInverted:n,flipHorizontal:l,flipVertical:c,rotation:g}}calculateCellPosition(e,t,r){return{x:e,y:t,cellX:e*r.cellWidth,cellY:t*r.cellHeight}}extractSVGCellData(e,t){const r=[];let i=0;for(let s=0;s<t.rows;s++)for(let a=0;a<t.cols;a++){const n=i*4,l=this.getCharacterIndex(e.characterPixels,n);let c=this.pixelsToRGBA(e.primaryColorPixels,n),u=this.pixelsToRGBA(e.secondaryColorPixels,n);const d=this.extractTransformData(e.transformPixels,e.rotationPixels,n);if(d.isInverted){const g=c;c=u,u=g}const f=this.calculateCellPosition(a,s,t);r.push({charIndex:l,primaryColor:c,secondaryColor:u,transform:d,position:f}),i++}return r}}class we{getGlyphIndex(e,t){const r=e.cmap;if(!r||!r.tables)return 0;for(const i of r.tables)if(i.format===4){for(let s=0;s<i.startCount.length;s++)if(t>=i.startCount[s]&&t<=i.endCount[s]){if(i.idRangeOffset[s]===0)return t+i.idDelta[s]&65535;{const a=i.idRangeOffset[s]/2+(t-i.startCount[s])-(i.startCount.length-s);if(a>=0&&a<i.glyphIdArray.length){const n=i.glyphIdArray[a];if(n!==0)return n+i.idDelta[s]&65535}}}}return 0}createEmptyPath(){return{getBoundingBox:()=>({x1:0,y1:0,x2:0,y2:0}),toSVG:()=>""}}createGlyphPath(e,t,r,i,s){if(!t||!t.xs||t.xs.length===0)return this.createEmptyPath();const a=s/e.head.unitsPerEm;return{getBoundingBox:()=>({x1:r+t.xMin*a,y1:i+-t.yMax*a,x2:r+t.xMax*a,y2:i+-t.yMin*a}),toSVG:()=>this.glyphToSVGPath(t,r,i,a)}}glyphToSVGPath(e,t,r,i){if(!e||!e.xs)return"";const{xs:s,ys:a,endPts:n,flags:l}=e;if(!s||!a||!n||!l)return"";let c="",u=0;for(let d=0;d<n.length;d++){const f=n[d];if(!(f<u)){if(f>=u){const g=t+s[u]*i,x=r-a[u]*i;c+=`M${g.toFixed(2)},${x.toFixed(2)}`;let p=u+1;for(;p<=f;)if((l[p]&1)!==0){const S=t+s[p]*i,w=r-a[p]*i;c+=`L${S.toFixed(2)},${w.toFixed(2)}`,p++}else{const S=t+s[p]*i,w=r-a[p]*i;let F=p+1>f?u:p+1;if((l[F]&1)!==0){const D=t+s[F]*i,R=r-a[F]*i;c+=`Q${S.toFixed(2)},${w.toFixed(2)} ${D.toFixed(2)},${R.toFixed(2)}`,p=F+1}else{const D=t+s[F]*i,R=r-a[F]*i,E=(S+D)/2,U=(w+R)/2;c+=`Q${S.toFixed(2)},${w.toFixed(2)} ${E.toFixed(2)},${U.toFixed(2)}`,p=F}}c+="Z"}u=f+1}}return c}generateCharacterPath(e,t,r,i,s){try{const a=e.codePointAt(0)||0,n=this.getGlyphIndex(t,a);if(n===0)return this.createEmptyPath();let l=null;try{t.glyf&&t.glyf[n]!==null?l=t.glyf[n]:m&&m.T&&m.T.glyf&&m.T.glyf._parseGlyf&&(l=m.T.glyf._parseGlyf(t,n),t.glyf&&l&&(t.glyf[n]=l))}catch(c){console.warn(`Failed to parse glyph ${n}:`,c)}return l?this.createGlyphPath(t,l,r,i,s):this.createEmptyPath()}catch(a){return console.warn(`Failed to generate path for character "${e}":`,a),this.createEmptyPath()}}generatePositionedCharacterPath(e,t,r,i,s,a,n,l){try{const c=n/t.head.unitsPerEm,u=l*c,d=r+(s-u)/2,f=i+(a+n*.7)/2;return this.generateCharacterPath(e,t,d,f,n).toSVG()||null}catch(c){return console.warn(`Failed to generate positioned character path for "${e}":`,c),null}}}class ye{constructor(){o(this,"pathGenerator");this.pathGenerator=new we}generateSVGHeader(e){return`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
6
+ `,o+="↓".repeat(24)+`
7
+ `,o}static o(i){if(i===null)return"null";if(i===void 0)return"undefined";if(typeof i=="string")return`"${i}"`;if(typeof i=="number"||typeof i=="boolean")return i+"";if(Array.isArray(i))return i.length===0?"[]":i.length<=5?`[${i.map(r=>x.o(r)).join(", ")}]`:`[${i.slice(0,3).map(r=>x.o(r)).join(", ")}, ... +${i.length-3} more]`;if(typeof i=="object"){const r=Object.keys(i);return r.length===0?"{}":r.length<=3?`{ ${r.map(o=>`${o}: ${x.o(i[o])}`).join(", ")} }`:`{ ${r.slice(0,2).map(o=>`${o}: ${x.o(i[o])}`).join(", ")}, ... +${r.length-2} more }`}return i+""}}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 D=class D{constructor(){l(this,"l",{u:3})}static m(){return D.h||(D.h=new D),D.h}p(s,i,r){const o="%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.l.u){case 0:return!1;case 1:return console.group(o,n),console.warn(x.i(s,i)),console.groupEnd(),!1;case 2:return console.group(o,n),console.error(x.i(s,i)),console.groupEnd(),!1;default:throw new x(s,r,i)}}validate(s,i,r){return!!s||(this.p(i,r),!1)}v(s){this.l.u=s}};l(D,"h",null);let k=D;const y=k.m(),X=new WeakMap;function P(c,s){X.set(c,s)}function W(c){return X.get(c)}class q{constructor(s,i,r=i,o={}){l(this,"_");l(this,"C");l(this,"F");l(this,"$");l(this,"M");l(this,"options");l(this,"D",null);l(this,"S",null);this._=s,this.$=i,this.M=r,this.options={filter:"nearest",wrap:"clamp",format:"rgba",type:"unsigned_byte",...o},this.F=this.createTexture(),this.C=s.createFramebuffer(),this.V()}R(s){const{_:i}=this,r=i.getParameter(i.FRAMEBUFFER_BINDING);i.bindFramebuffer(i.FRAMEBUFFER,this.C);try{return s()}finally{i.bindFramebuffer(i.FRAMEBUFFER,r)}}createTexture(){const{_:s}=this,i=s.createTexture();s.bindTexture(s.TEXTURE_2D,i);const r=this.options.filter==="linear"?s.LINEAR:s.NEAREST,o=this.options.wrap==="repeat"?s.REPEAT:s.CLAMP_TO_EDGE;return s.texParameteri(s.TEXTURE_2D,s.TEXTURE_MIN_FILTER,r),s.texParameteri(s.TEXTURE_2D,s.TEXTURE_MAG_FILTER,r),s.texParameteri(s.TEXTURE_2D,s.TEXTURE_WRAP_S,o),s.texParameteri(s.TEXTURE_2D,s.TEXTURE_WRAP_T,o),this.k(),i}k(){const{_:s}=this,i=this.options.type==="float"?s.FLOAT:s.UNSIGNED_BYTE;s.texImage2D(s.TEXTURE_2D,0,s.RGBA,this.$,this.M,0,s.RGBA,i,null)}V(){const{_:s}=this;s.bindFramebuffer(s.FRAMEBUFFER,this.C),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,this.F,0),s.bindFramebuffer(s.FRAMEBUFFER,null)}update(s){const{_:i}=this;i.bindTexture(i.TEXTURE_2D,this.F),i.pixelStorei(i.UNPACK_FLIP_Y_WEBGL,1),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,i.RGBA,i.UNSIGNED_BYTE,s),i.bindTexture(i.TEXTURE_2D,null)}I(s,i,r){const{_:o}=this;o.bindTexture(o.TEXTURE_2D,this.F),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,i,r,0,o.RGBA,o.UNSIGNED_BYTE,s),o.bindTexture(o.TEXTURE_2D,null)}resize(s,i){const{_:r}=this;this.$=s,this.M=i,this.S=null,r.bindTexture(r.TEXTURE_2D,this.F),this.k(),r.bindTexture(r.TEXTURE_2D,null)}P(){const{_:s}=this;this.D={framebuffer:s.getParameter(s.FRAMEBUFFER_BINDING),viewport:s.getParameter(s.VIEWPORT)},s.bindFramebuffer(s.FRAMEBUFFER,this.C),s.viewport(0,0,this.$,this.M),P(s,[0,0,this.$,this.M])}end(){if(!this.D)return;const{_:s}=this;s.bindFramebuffer(s.FRAMEBUFFER,this.D.framebuffer),s.viewport(...this.D.viewport),P(s,this.D.viewport),this.D=null}A(){const{_:s}=this;this.S||(this.S=new Uint8Array(this.$*this.M*4)),this.R(()=>{s.readPixels(0,0,this.$,this.M,s.RGBA,s.UNSIGNED_BYTE,this.S)})}get(s,i,r,o){const{_:n}=this;if(s===void 0&&i===void 0){const h=new Uint8Array(this.$*this.M*4);return this.R(()=>(n.readPixels(0,0,this.$,this.M,n.RGBA,n.UNSIGNED_BYTE,h),h))}if(r===void 0&&o===void 0){(s<0||i<0||s>=this.$||i>=this.M)&&(console.warn("The x and y values passed to Framebuffer.get are outside of its range and will be clamped."),s=Math.max(0,Math.min(s,this.$-1)),i=Math.max(0,Math.min(i,this.M-1)));const h=new Uint8Array(4);return this.R(()=>(n.readPixels(s,i,1,1,n.RGBA,n.UNSIGNED_BYTE,h),[h[0],h[1],h[2],h[3]]))}{s=Math.max(0,Math.min(s,this.$-1)),i=Math.max(0,Math.min(i,this.M-1)),r=Math.max(1,Math.min(r,this.$-s)),o=Math.max(1,Math.min(o,this.M-i));const h=new Uint8Array(r*o*4);return this.R(()=>(n.readPixels(s,i,r,o,n.RGBA,n.UNSIGNED_BYTE,h),h))}}dispose(){this.C&&this._.deleteFramebuffer(this.C),this.F&&this._.deleteTexture(this.F)}get framebuffer(){return this.C}get texture(){return this.F}get width(){return this.$}get height(){return this.M}get U(){return this.S}}class E{constructor(s,i,r){l(this,"_");l(this,"G");l(this,"L",new Map);l(this,"O",new Map);l(this,"H",0);l(this,"j");this._=s,this.G=this.createProgram(i,r),this.j=s.getParameter(s.MAX_TEXTURE_IMAGE_UNITS),this.W()}W(){const s=this._.getProgramParameter(this.G,this._.ACTIVE_UNIFORMS);for(let i=0;i<s;i++){const r=this._.getActiveUniform(this.G,i);if(r){const o=this._.getUniformLocation(this.G,r.name);o&&(this.L.set(r.name,o),this.O.set(r.name,r.type))}}}createProgram(s,i){const r=this.createShader(this._.VERTEX_SHADER,s),o=this.createShader(this._.FRAGMENT_SHADER,i),n=this._.createProgram();if(this._.attachShader(n,r),this._.attachShader(n,o),this._.linkProgram(n),!this._.getProgramParameter(n,this._.LINK_STATUS)){const h=this._.getProgramInfoLog(n);throw Error("Shader program link error: "+h)}return this._.deleteShader(r),this._.deleteShader(o),n}createShader(s,i){const r=this._.createShader(s);if(this._.shaderSource(r,i),this._.compileShader(r),!this._.getShaderParameter(r,this._.COMPILE_STATUS)){const o=this._.getShaderInfoLog(r);throw this._.deleteShader(r),Error("Shader compilation error: "+o)}return r}N(){this._.useProgram(this.G),this.X()}Y(s,i){const r=this.L.get(s);if(r)if(typeof i=="number")this.q(s)?this._.uniform1i(r,Math.floor(i)):this._.uniform1f(r,i);else if(typeof i=="boolean")this._.uniform1i(r,i?1:0);else if(Array.isArray(i))switch(i.length){case 2:this._.uniform2f(r,i[0],i[1]);break;case 3:this._.uniform3f(r,i[0],i[1],i[2]);break;case 4:this._.uniform4f(r,i[0],i[1],i[2],i[3]);break;default:console.warn(`Unsupported array length ${i.length} for uniform '${s}'`)}else if(i instanceof WebGLTexture){const o=this.Z();this._.uniform1i(r,o),this._.activeTexture(this._.TEXTURE0+o),this._.bindTexture(this._.TEXTURE_2D,i)}else if(i instanceof q){const o=this.Z();this._.uniform1i(r,o),this._.activeTexture(this._.TEXTURE0+o),this._.bindTexture(this._.TEXTURE_2D,i.texture)}else console.warn(`Unsupported uniform type for '${s}':`,typeof i)}Z(){return this.H>=this.j&&console.warn(`Exceeded maximum texture units (${this.j}). Texture may not render correctly.`),this.H++}q(s){const i=this.O.get(s);return!!i&&(i===this._.INT||i===this._.INT_VEC2||i===this._.INT_VEC3||i===this._.INT_VEC4||i===this._.SAMPLER_2D||i===this._.SAMPLER_CUBE)}get J(){return this.G}dispose(){this._.deleteProgram(this.G)}X(){this.H=0}}class J{constructor(s){l(this,"_");l(this,"K",null);l(this,"tt",16);l(this,"et",new Map);this._=s}st(){if(this.K)return;const s=this._;this.K=s.createBuffer(),s.bindBuffer(s.ARRAY_BUFFER,this.K)}it(){const s=this._,i=s.getParameter(s.CURRENT_PROGRAM);let r=this.et.get(i);return r||(r={rt:s.getAttribLocation(i,"a_position"),ot:s.getAttribLocation(i,"a_texCoord")},this.et.set(i,r)),s.enableVertexAttribArray(r.rt),s.vertexAttribPointer(r.rt,2,s.FLOAT,!1,this.tt,0),s.enableVertexAttribArray(r.ot),s.vertexAttribPointer(r.ot,2,s.FLOAT,!1,this.tt,8),{nt:r.rt,ht:r.ot}}lt(s,i){const r=this._;r.disableVertexAttribArray(s),r.disableVertexAttribArray(i)}ct(s,i){const r=this._,o=W(r)||[0,0,r.canvas.width,r.canvas.height];return{ut:s/o[2]*2-1,ft:1-i/o[3]*2}}dt(s,i,r,o){const n=this._;this.st(),n.bindBuffer(n.ARRAY_BUFFER,this.K);const h=new Float32Array([s,o,0,0,r,o,1,0,s,i,0,1,s,i,0,1,r,o,1,0,r,i,1,1]);n.bufferData(n.ARRAY_BUFFER,h,n.DYNAMIC_DRAW)}dispose(){this.K&&this._.deleteBuffer(this.K)}}class ot extends J{constructor(s){super(s)}gt(s,i,r,o){const n=this.ct(s,i),h=this.ct(s+r,i+o);this.dt(n.ut,n.ft,h.ut,h.ft);const a=this.it();this._.drawArrays(this._.TRIANGLES,0,6),this.lt(a.nt,a.ht)}vt(s,i,r,o,n){this.gt(s,i,r,n),this.gt(s+r-n,i,n,o),this.gt(s,i+o-n,r,n),this.gt(s,i,n,o)}}class nt extends J{constructor(s){super(s)}draw(s,i,r,o,n){const h=r-s,a=o-i,u=Math.hypot(h,a);if(u===0){const d=n/2,g=this.ct(s-d,i-d),p=this.ct(s+d,i+d);this.dt(g.ut,g.ft,p.ut,p.ft)}else{const d=-a/u,g=h/u,p=n/2,w=s+d*p,b=i+g*p,_=s-d*p,T=i-g*p,R=r+d*p,C=o+g*p,M=r-d*p,$=o-g*p,N=this.ct(w,b),A=this.ct(_,T),S=this.ct(R,C),rt=this.ct(M,$),Y=this._;this.st(),Y.bindBuffer(Y.ARRAY_BUFFER,this.K);const Bt=new Float32Array([N.ut,N.ft,0,0,A.ut,A.ft,0,1,S.ut,S.ft,1,0,A.ut,A.ft,0,1,rt.ut,rt.ft,1,1,S.ut,S.ft,1,0]);Y.bufferData(Y.ARRAY_BUFFER,Bt,Y.DYNAMIC_DRAW)}const f=this.it();this._.drawArrays(this._.TRIANGLES,0,6),this.lt(f.nt,f.ht)}}var F="attribute vec2 a_position;attribute vec2 a_texCoord;varying vec2 v_uv;uniform float u_rotation;uniform vec2 u_center;uniform float u_aspectRatio;mat2 rotate2D(float angle){float s=sin(angle);float c=cos(angle);return mat2(c,-s,s,c);}void main(){v_uv=a_texCoord;vec2 pos=a_position;pos-=u_center;pos.x*=u_aspectRatio;pos=rotate2D(-u_rotation)*pos;pos.x/=u_aspectRatio;pos+=u_center;gl_Position=vec4(pos,0.0,1.0);}";class ht{constructor(s){l(this,"_");l(this,"_t");l(this,"xt");l(this,"bt",null);l(this,"wt");l(this,"Ct");l(this,"yt",[1,1,1,1]);l(this,"Ft",!0);l(this,"Tt",[0,0,0,1]);l(this,"$t",1);l(this,"Mt",!0);l(this,"Dt",0);l(this,"St",[]);this._=s,this._t=new E(this._,F,"precision lowp float;uniform sampler2D u_texture;varying vec2 v_uv;void main(){gl_FragColor=texture2D(u_texture,v_uv);}"),this.xt=new E(this._,F,"precision lowp float;uniform vec4 u_color;void main(){gl_FragColor=u_color;}"),this.wt=new ot(this._),this.Ct=new nt(this._),this._.enable(this._.BLEND),this._.blendEquation(this._.FUNC_ADD),this._.blendFunc(this._.ONE,this._.ONE_MINUS_SRC_ALPHA),P(this._,[0,0,this._.canvas.width,this._.canvas.height])}Vt(s){this.bt!==s&&(this.bt=s,s.N())}fill(s,i,r,o){if(this.Ft=!0,i===void 0&&r===void 0&&o===void 0){const n=s/255;this.yt=[n,n,n,1]}else if(r!==void 0&&o===void 0)this.yt=[s/255,i/255,r/255,1];else{if(r===void 0||o===void 0)throw Error("Invalid fill parameters. Use fill(gray), fill(r,g,b), or fill(r,g,b,a)");this.yt=[s/255,i/255,r/255,o/255]}}stroke(s,i,r,o){if(this.Mt=!0,i===void 0&&r===void 0&&o===void 0){const n=s/255;this.Tt=[n,n,n,1]}else if(r!==void 0&&o===void 0)this.Tt=[s/255,i/255,r/255,1];else{if(r===void 0||o===void 0)throw Error("Invalid stroke parameters. Use stroke(gray), stroke(r,g,b), or stroke(r,g,b,a)");this.Tt=[s/255,i/255,r/255,o/255]}}Rt(s){if(s<0)throw Error("Stroke weight must be non-negative");this.$t=s}kt(){this.Mt=!1}zt(){this.Ft=!1}rotate(s){this.Dt=s}push(){this.St.push({It:[...this.yt],Ft:this.Ft,Pt:[...this.Tt],Rt:this.$t,Mt:this.Mt,rotation:this.Dt})}pop(){const s=this.St.pop();s?(this.yt=s.It,this.Ft=s.Ft,this.Tt=s.Pt,this.$t=s.Rt,this.Mt=s.Mt,this.Dt=s.rotation):console.warn("pop() called without matching push()")}reset(){this.bt=null,this.St=[],this.Dt=0}createShader(s,i){return new E(this._,s,i)}At(s){return new E(this._,F,s)}Y(s,i){this.bt.Y(s,i)}rect(s,i,r,o){if(this.bt!==null){const{Ut:g,Gt:p,Et:w,aspectRatio:b}=this.Lt(s,i,r,o);return this.Y("u_rotation",w),this.Y("u_center",[g,p]),this.Y("u_aspectRatio",b),this.wt.gt(s,i,r,o),void(this.bt=null)}const n=this.xt;let h=0,a=0,u=0,f=1;const d=this.Lt(s,i,r,o);h=d.Ut,a=d.Gt,u=d.Et,f=d.aspectRatio,this.Ft&&(this.Vt(n),this.Y("u_color",this.yt),this.Y("u_rotation",u),this.Y("u_center",[h,a]),this.Y("u_aspectRatio",f),this.wt.gt(s,i,r,o)),this.Mt&&this.$t>0&&(this.Vt(n),this.Y("u_color",this.Tt),this.Y("u_rotation",u),this.Y("u_center",[h,a]),this.Y("u_aspectRatio",f),this.wt.vt(s,i,r,o,this.$t)),this.bt=null}line(s,i,r,o){if(this.bt!==null){const _=(s+r)/2,T=(i+o)/2,R=Math.abs(r-s)||1,C=Math.abs(o-i)||1,{Ut:M,Gt:$,Et:N,aspectRatio:A}=this.Lt(_-R/2,T-C/2,R,C);this.Y("u_rotation",N),this.Y("u_center",[M,$]),this.Y("u_aspectRatio",A);const S=this.$t>0?this.$t:1;return this.Ct.draw(s,i,r,o,S),void(this.bt=null)}if(!this.Mt||this.$t<=0)return;const n=this.xt,h=(s+r)/2,a=(i+o)/2,u=Math.abs(r-s)||1,f=Math.abs(o-i)||1,d=this.Dt!==0;let g=0,p=0,w=0,b=1;if(d){const _=this.Lt(h-u/2,a-f/2,u,f);g=_.Ut,p=_.Gt,w=_.Et,b=_.aspectRatio}this.Vt(n),this.Y("u_color",this.Tt),d&&(this.Y("u_rotation",w),this.Y("u_center",[g,p]),this.Y("u_aspectRatio",b)),this.Ct.draw(s,i,r,o,this.$t)}Lt(s,i,r,o){const n=W(this._)||[0,0,this._.canvas.width,this._.canvas.height],h=n[2],a=n[3],u=h/a;return{Ut:(s+r/2)/h*2-1,Gt:1-(i+o/2)/a*2,Et:this.Dt*Math.PI/180,aspectRatio:u}}createFramebuffer(s,i,r={}){return new q(this._,s,i,r)}background(s,i=s,r=s,o=255){this.clear(s/255,i/255,r/255,o/255)}clear(s=0,i=0,r=0,o=0){this._.clearColor(s,i,r,o),this._.clear(this._.COLOR_BUFFER_BIT)}Ot(){this._.viewport(0,0,this._.canvas.width,this._.canvas.height),P(this._,[0,0,this._.canvas.width,this._.canvas.height])}get context(){return this._}dispose(){this._t.dispose(),this.xt.dispose(),this.wt.dispose(),this.Ct.dispose()}image(s,i,r,o,n){const h=this._,a=o??s.width,u=n??s.height;this.Vt(this._t),this.Y("u_texture",s.texture);const f=this.Lt(i,r,a,u);this.Y("u_rotation",f.Et),this.Y("u_center",[f.Ut,f.Gt]),this.Y("u_aspectRatio",f.aspectRatio),this.wt.gt(i,r,a,u),h.bindTexture(h.TEXTURE_2D,null),this.bt=null}}var U,m={parse:function(c){var s=function(i,r,o,n){var h=m.T,a={cmap:h.cmap,head:h.head,hhea:h.hhea,maxp:h.maxp,hmtx:h.hmtx,loca:h.loca,glyf:h.glyf},u={_data:i,_index:r,_offset:o};for(var f in a){var d=m.findTable(i,f,o);if(d){var g=d[0],p=n[g];p==null&&(p=a[f].Bt(i,g,d[1],u)),u[f]=n[g]=p}}return u}(new Uint8Array(c),0,0,{});return[s]},findTable:function(c,s,i){for(var r=m.B,o=r.Ht(c,i+4),n=i+12,h=0;h<o;h++){var a=r.jt(c,n,4);r.Wt(c,n+4);var u=r.Wt(c,n+8),f=r.Wt(c,n+12);if(a==s)return[u,f];n+=16}return null},T:{}};m.B={Nt:function(c,s){return m.B.Xt.uint16[0]=c[s]<<8|c[s+1],m.B.Xt.int16[0]},Ht:function(c,s){return c[s]<<8|c[s+1]},Yt:function(c,s,i){for(var r=[],o=0;o<i;o++)r.push(m.B.Ht(c,s+2*o));return r},Wt:function(c,s){var i=m.B.Xt.uint8;return i[3]=c[s],i[2]=c[s+1],i[1]=c[s+2],i[0]=c[s+3],m.B.Xt.uint32[0]},jt:function(c,s,i){for(var r="",o=0;o<i;o++)r+=String.fromCharCode(c[s+o]);return r},Xt:(U=new ArrayBuffer(8),{uint8:new Uint8Array(U),int16:new Int16Array(U),uint16:new Uint16Array(U),uint32:new Uint32Array(U)})},m.T.cmap={Bt:function(c,s,i){var r={qt:[],Qt:{},off:s};c=new Uint8Array(c.buffer,s,i),s=0;var o=m.B,n=o.Ht,h=m.T.cmap;n(c,s);var a=n(c,s+=2);s+=2;for(var u=[],f=0;f<a;f++){var d=n(c,s),g=n(c,s+=2);s+=2;var p=o.Wt(c,s);s+=4;var w="p"+d+"e"+g,b=u.indexOf(p);if(b==-1){b=r.qt.length;var _={};u.push(p);var T=_.format=n(c,p);T==4?_=h.Zt(c,p,_):T==12&&(_=h.Jt(c,p,_)),r.qt.push(_)}r.Qt[w]!=null&&console.log("multiple tables for one platform+encoding: "+w),r.Qt[w]=b}return r},Zt:function(c,s,i){var r=m.B,o=r.Ht,n=r.Yt,h=s,a=o(c,s+=2);o(c,s+=2);var u=o(c,s+=2);s+=2;var f=u>>>1;i.Kt=o(c,s),s+=2,i.te=o(c,s),s+=2,i.ee=o(c,s),s+=2,i.se=n(c,s,f),s+=2*f,s+=2,i.ie=n(c,s,f),s+=2*f,i.re=[];for(var d=0;d<f;d++)i.re.push(r.Nt(c,s)),s+=2;return i.oe=n(c,s,f),s+=2*f,i.ne=n(c,s,h+a-s>>1),i},Jt:function(c,s,i){var r=m.B.Wt;r(c,s+=4),r(c,s+=4);var o=3*r(c,s+=4);s+=4;for(var n=i.groups=new Uint32Array(o),h=0;h<o;h+=3)n[h]=r(c,s+(h<<2)),n[h+1]=r(c,s+(h<<2)+4),n[h+2]=r(c,s+(h<<2)+8);return i}},m.T.head={Bt:function(c,s,i){var r=m.B,o={};return s+=18,o.unitsPerEm=r.Ht(c,s),s+=2,s+=16,o.xMin=r.Nt(c,s),s+=2,o.yMin=r.Nt(c,s),s+=2,o.xMax=r.Nt(c,s),s+=2,o.yMax=r.Nt(c,s),s+=2,s+=6,o.indexToLocFormat=r.Nt(c,s),o}},m.T.hhea={Bt:function(c,s,i){var r=m.B,o={};s+=4;for(var n=["ascender","descender","lineGap","advanceWidthMax","minLeftSideBearing","minRightSideBearing","xMaxExtent","caretSlopeRise","caretSlopeRun","caretOffset","res0","res1","res2","res3","metricDataFormat","numberOfHMetrics"],h=0;h<n.length;h++){var a=n[h],u=a=="advanceWidthMax"||a=="numberOfHMetrics"?r.Ht:r.Nt;o[a]=u(c,s+2*h)}return o}},m.T.hmtx={Bt:function(c,s,i,r){for(var o=m.B,n=[],h=[],a=r.maxp.numGlyphs,u=r.hhea.numberOfHMetrics,f=0,d=0,g=0;g<u;)f=o.Ht(c,s+(g<<2)),d=o.Nt(c,s+(g<<2)+2),n.push(f),h.push(d),g++;for(;g<a;)n.push(f),h.push(d),g++;return{he:n,ae:h}}},m.T.maxp={Bt:function(c,s,i){var r=m.B,o=r.Ht,n={};return r.Wt(c,s),s+=4,n.numGlyphs=o(c,s),s+=2,n}},m.T.loca={Bt:function(c,s,i,r){var o=m.B,n=[],h=r.head.indexToLocFormat,a=r.maxp.numGlyphs+1;if(h==0)for(var u=0;u<a;u++)n.push(o.Ht(c,s+(u<<1))<<1);if(h==1)for(u=0;u<a;u++)n.push(o.Wt(c,s+(u<<2)));return n}},m.T.glyf={Bt:function(c,s,i,r){for(var o=[],n=r.maxp.numGlyphs,h=0;h<n;h++)o.push(null);return o},le:function(c,s){var i=m.B,r=c._data,o=c.loca;if(o[s]==o[s+1])return null;var n=m.findTable(r,"glyf",c._offset)[0]+o[s],h={};if(h.ce=i.Nt(r,n),n+=2,h.xMin=i.Nt(r,n),n+=2,h.yMin=i.Nt(r,n),n+=2,h.xMax=i.Nt(r,n),n+=2,h.yMax=i.Nt(r,n),n+=2,h.xMin>=h.xMax||h.yMin>=h.yMax)return null;if(h.ce>0){h.ue=[];for(var a=0;a<h.ce;a++)h.ue.push(i.Ht(r,n)),n+=2;var u=i.Ht(r,n);if(n+=2,r.length-n<u)return null;n+=u;var f=h.ue[h.ce-1]+1;for(h.flags=[],a=0;a<f;a++){var d=r[n];if(n++,h.flags.push(d),8&d){var g=r[n];n++;for(var p=0;p<g;p++)h.flags.push(d),a++}}for(h.fe=[],a=0;a<f;a++){var w=!!(2&h.flags[a]),b=!!(16&h.flags[a]);w?(h.fe.push(b?r[n]:-r[n]),n++):b?h.fe.push(0):(h.fe.push(i.Nt(r,n)),n+=2)}for(h.de=[],a=0;a<f;a++)w=!!(4&h.flags[a]),b=!!(32&h.flags[a]),w?(h.de.push(b?r[n]:-r[n]),n++):b?h.de.push(0):(h.de.push(i.Nt(r,n)),n+=2);var _=0,T=0;for(a=0;a<f;a++)_+=h.fe[a],T+=h.de[a],h.fe[a]=_,h.de[a]=T}else h.me=[];return h}},typeof module<"u"&&module.exports?module.exports=m:typeof window<"u"&&(window.Typr=m);class at{ge(s){var r;const i=[];return(r=s==null?void 0:s.cmap)!=null&&r.qt?(s.cmap.qt.forEach(o=>{if(o.format===4){const n=this.pe(o);i.push(...n)}else if(o.format===12){const n=this.ve(o);i.push(...n)}}),[...new Set(i)]):[]}pe(s){const i=[];if(!(s.ie&&s.se&&s.oe&&s.re))return i;for(let r=0;r<s.ie.length;r++){const o=s.ie[r],n=s.se[r];if(o!==65535||n!==65535){for(let h=o;h<=n;h++)if(this._e(s,h,r)>0){const a=String.fromCodePoint(h);i.push(a)}}}return i}ve(s){const i=[];if(!s.groups)return i;for(let r=0;r<s.groups.length;r+=3){const o=s.groups[r],n=s.groups[r+1],h=s.groups[r+2];for(let a=o;a<=n;a++)if(h+(a-o)>0){const u=String.fromCodePoint(a);i.push(u)}}return i}_e(s,i,r){if(s.oe[r]===0)return i+s.re[r]&65535;{const o=s.oe[r]/2+(i-s.ie[r])-(s.ie.length-r);if(o>=0&&s.ne&&o<s.ne.length){const n=s.ne[o];if(n!==0)return n+s.re[r]&65535}}return 0}xe(s){return s.filter(i=>this.be(i))}be(s){const i=s.codePointAt(0)||0;return!(i>=0&&i<=31&&i!==9&&i!==10&&i!==13||i>=127&&i<=159)}}class lt{constructor(s){l(this,"we");l(this,"Ce");l(this,"ye");this.ye=s,this.we=document.createElement("canvas"),this.Ce=this.we.getContext("2d",{Fe:!0,alpha:!1})}Te(s,i,r,o){const n=s.length,h=Math.ceil(Math.sqrt(n)),a=Math.ceil(n/h),u=i.width*h,f=i.height*a;this.$e(u,f,r,o),this.Me(s,i,h,r),this.De();const d=this.ye.createFramebuffer(u,f,{filter:"nearest"});return d.update(this.we),{framebuffer:d,columns:h,rows:a}}$e(s,i,r,o){this.we.width=s,this.we.height=i,this.we.style.width=s+"px",this.we.style.height=s+"px",this.Ce.imageSmoothingEnabled=!1,this.we.style.imageRendering="pixelated",this.Ce.fillStyle="black",this.Ce.fillRect(0,0,s,i),this.Ce.font=`${r}px ${o}`,this.Ce.textBaseline="top",this.Ce.textAlign="left",this.Ce.fillStyle="white"}Me(s,i,r,o){for(let n=0;n<s.length;n++){const h=n%r,a=Math.floor(n/r),u=h*i.width+.5*i.width,f=a*i.height+.5*i.height,d=Math.round(u-.5*i.width),g=Math.round(f-.5*o);this.Ce.fillText(s[n].Se,d,g)}}De(s=128){const i=this.Ce.getImageData(0,0,this.we.width,this.we.height),r=i.data;for(let o=0;o<r.length;o+=4){const n=.299*r[o]+.587*r[o+1]+.114*r[o+2]>s+32?255:0;r[o]=n,r[o+1]=n,r[o+2]=n}this.Ce.putImageData(i,0,0)}}class ct{constructor(){l(this,"Ve");l(this,"Re");this.Ve=document.createElement("canvas"),this.Re=this.Ve.getContext("2d")}ke(s,i,r){this.Re.font=`${i}px ${r}`;let o=0,n=0;for(const h of s){const a=this.Re.measureText(h);o=Math.max(o,a.width),n=Math.max(n,a.actualBoundingBoxAscent+a.actualBoundingBoxDescent)}return{width:o,height:n}}}class ut{ze(s,i){return s.map((r,o)=>{const n=r.codePointAt(0)||0,h=this.Ie(o);let a=0;if(i.hmtx&&i.hmtx.he){const u=this.Pe(i,n);u>0&&i.hmtx.he[u]!==void 0&&(a=i.hmtx.he[u])}return{Se:r,unicode:n,color:h,Ae:a}})}Pe(s,i){const r=s.cmap;if(!r||!r.qt)return 0;for(const o of r.qt)if(o.format===4){for(let n=0;n<o.ie.length;n++)if(i>=o.ie[n]&&i<=o.se[n]){if(o.oe[n]===0)return i+o.re[n]&65535;{const h=o.oe[n]/2+(i-o.ie[n])-(o.ie.length-n);if(h>=0&&h<o.ne.length){const a=o.ne[h];if(a!==0)return a+o.re[n]&65535}}}}return 0}Ie(s){return[s%256,Math.floor(s/256)%256,Math.floor(s/65536)%256]}Ue(s,i){if(!y.validate(typeof s=="string","Character must be a string.",{method:"getCharacterColor",Ge:s}))return[0,0,0];const r=i.find(o=>o.Se===s);return r?r.color:[0,0,0]}Ee(s,i){return y.validate(typeof s=="string"&&s.length>0,"Characters must be a string with at least one character.",{method:"getCharacterColors",Ge:s})?s.split("").map(r=>this.Ue(r,i)||[0,0,0]):[[0,0,0]]}}class Z{constructor(s,i=16){l(this,"Le");l(this,"Oe",[]);l(this,"Be");l(this,"He",16);l(this,"je",0);l(this,"We",0);l(this,"Ne",{width:0,height:0});l(this,"Xe");l(this,"Ye","UrsaFont");l(this,"qe");l(this,"Qe");l(this,"Ze");l(this,"Je");this.He=i,this.qe=new at,this.Qe=new lt(s),this.Ze=new ct,this.Je=new ut}async initialize(s){let i;if(!s)throw new x("Embedded font not available. This appears to be a minified build - please provide `fontSource`.");{const r=await fetch(s);if(!r.ok)throw new x(`Failed to load font file: ${r.status} ${r.statusText}`);i=await r.arrayBuffer()}await this.Ke(i),this.Le=m.parse(i)[0],await this.ts()}es(s){if(s===void 0)return this.He;this.He=s,this.Ne=this.Ze.ke(this.Oe.map(r=>r.Se),this.He,this.Ye);const i=this.Qe.Te(this.Oe,this.Ne,this.He,this.Ye);this.Be=i.framebuffer,this.je=i.columns,this.We=i.rows}async ss(s){try{const i=await fetch(s);if(!i.ok)throw new x(`Failed to load font file: ${i.status} ${i.statusText}`);const r=await i.arrayBuffer();await this.Ke(r);const o=m.parse(r);if(!o||o.length===0)throw Error("Failed to parse font file");this.Le=o[0],await this.ts()}catch(i){throw new x("Failed to load font: "+(i instanceof Error?i.message:"Unknown error"),i)}}async Ke(s){const i=Date.now();this.Ye=this.Ye==="UrsaFont"?"UrsaFont":"CustomFont_"+i,this.Xe=new FontFace(this.Ye,s),await this.Xe.load(),document.fonts.add(this.Xe)}async ts(){const s=this.qe.ge(this.Le),i=this.qe.xe(s);this.Oe=this.Je.ze(i,this.Le),this.Ne=this.Ze.ke(i,this.He,this.Ye);const r=this.Qe.Te(this.Oe,this.Ne,this.He,this.Ye);this.Be=r.framebuffer,this.je=r.columns,this.We=r.rows}Ue(s){return this.Je.Ue(s,this.Oe)}Ee(s){return this.Je.Ee(s,this.Oe)}rs(s){if(typeof s!="string"||s.length===0)return!1;const i=new Set(this.Oe.map(r=>r.Se));for(const r of s)if(!i.has(r))return!1;return!0}get ns(){return this.Be}get hs(){return this.Oe}get ls(){return this.je}get cs(){return this.We}get us(){return this.Ne}dispose(){this.Be.dispose(),document.fonts.delete(this.Xe)}get fontSize(){return this.He}get font(){return this.Le}}class K{constructor(s,i,r){l(this,"fs");l(this,"ds");l(this,"$");l(this,"M");l(this,"gs");l(this,"ps");l(this,"vs",!1);l(this,"_s");l(this,"xs");l(this,"bs");this._s=s,this.xs=i,this.bs=r,this.reset()}reset(){this.vs||(this.fs=Math.floor(this._s.width/this.xs),this.ds=Math.floor(this._s.height/this.bs)),this.ws()}ws(){this.$=this.fs*this.xs,this.M=this.ds*this.bs,this.gs=Math.floor((this._s.width-this.$)/2),this.ps=Math.floor((this._s.height-this.M)/2)}Cs(s,i){this.xs=s,this.bs=i,this.reset()}ys(s,i){this.vs=!0,this.fs=s,this.ds=i,this.ws()}Fs(){this.vs=!1,this.reset()}resize(){this.vs?this.ws():this.reset()}Ts(s){if(s===void 0)return this.vs;this.vs=s}get $s(){return this.xs}get Ms(){return this.bs}get cols(){return this.fs}get rows(){return this.ds}get width(){return this.$}get height(){return this.M}get offsetX(){return this.gs}get offsetY(){return this.ps}}class Q{constructor(s,i=!1,r={}){l(this,"_s");l(this,"Ds");l(this,"Ss");l(this,"Vs");l(this,"Rs");this.Ds=s,this.Ss=i,this._s=this.ks(r.width,r.height),i&&this.zs()}ks(s,i){var o;const r=document.createElement("canvas");if(r.className="textmodeCanvas",r.style.imageRendering="pixelated",this.Ss)r.width=s||800,r.height=i||600,document.body.appendChild(r);else{const n=this.Ds.getBoundingClientRect();let h=Math.round(n.width),a=Math.round(n.height);if(this.Ds instanceof HTMLVideoElement){const d=this.Ds;(h===0||a===0)&&d.videoWidth>0&&d.videoHeight>0&&(h=d.videoWidth,a=d.videoHeight)}r.width=h,r.height=a,r.style.position="absolute",r.style.pointerEvents="none";const u=window.getComputedStyle(this.Ds);let f=parseInt(u.zIndex||"0",10);isNaN(f)&&(f=0),r.style.zIndex=""+(f+1),this.Is(r),(o=this.Ds.parentNode)==null||o.insertBefore(r,this.Ds.nextSibling)}return r}Is(s){const i=this.Ds.getBoundingClientRect();let r=this.Ds.offsetParent;if(r&&r!==document.body){const o=r.getBoundingClientRect();s.style.top=i.top-o.top+"px",s.style.left=i.left-o.left+"px"}else s.style.top=i.top+window.scrollY+"px",s.style.left=i.left+window.scrollX+"px"}resize(s,i){if(this.Ss)this._s.width=s??this._s.width,this._s.height=i??this._s.height;else{const r=this.Ds.getBoundingClientRect();let o=Math.round(r.width),n=Math.round(r.height);if(this.Ds instanceof HTMLVideoElement){const h=this.Ds;(o===0||n===0)&&h.videoWidth>0&&h.videoHeight>0&&(o=h.videoWidth,n=h.videoHeight)}this._s.width=o,this._s.height=n,this.Is(this._s)}}Ps(){const s={alpha:!1,premultipliedAlpha:!1,As:!0,antialias:!1,Us:!1,Gs:!1,powerPreference:"high-performance"},i=this._s.getContext("webgl2",s)||this._s.getContext("webgl",s);if(!i)throw new x("WebGL context could not be created. Ensure your browser supports WebGL.");return i}zs(){this.Vs=new ResizeObserver(s=>{for(const i of s){const r=i.contentRect;!this.Rs||Math.round(r.width)===this._s.width&&Math.round(r.height)===this._s.height||this.Rs()}}),this.Vs.observe(this._s)}dispose(){if(this.Vs&&this.Vs.disconnect(),this._s){const s=this._s.getContext("webgl")||this._s.getContext("webgl2");if(s){const i=s.getExtension("WEBGL_lose_context");i&&i.Es()}this._s.parentNode&&this._s.parentNode.removeChild(this._s)}}get canvas(){return this._s}get width(){return this._s.width}get height(){return this._s.height}}class B{constructor(s,i,r,o={}){l(this,"Ls");l(this,"Os");l(this,"grid");l(this,"Bs");l(this,"Hs");l(this,"js");l(this,"Ws");l(this,"Ns");l(this,"l");this.Ls=s,this.Os=i,this.grid=r,this.l=o;const n=this.grid.cols,h=this.grid.rows;this.Bs=this.Ls.createFramebuffer(n,h),this.Hs=this.Ls.createFramebuffer(n,h),this.js=this.Ls.createFramebuffer(n,h),this.Ws=this.Ls.createFramebuffer(n,h),this.Ns=this.Ls.createFramebuffer(n,h)}resize(){const s=this.grid.cols,i=this.grid.rows;this.Bs.resize(s,i),this.Hs.resize(s,i),this.js.resize(s,i),this.Ws.resize(s,i),this.Ns.resize(s,i)}enabled(s){this.l.enabled=s}enable(){this.enabled(!0)}disable(){this.enabled(!1)}dispose(){this.Bs.dispose(),this.Hs.dispose(),this.js.dispose(),this.Ws.dispose(),this.Ns.dispose()}get Xs(){return this.Bs}get Ys(){return this.Hs}get qs(){return this.js}get Qs(){return this.Ws}get Zs(){return this.Ns}get options(){return this.l}}class ft{constructor(s,i){l(this,"C");l(this,"ye");l(this,"Js");this.ye=s,this.Js=i;const r=Math.max(this.Js.length,1);this.C=this.ye.createFramebuffer(r,1),this.Ks()}Ks(){const s=this.Js.length;this.C.width!==s&&this.C.resize(s,1);const i=new Uint8Array(1*s*4);for(let r=0;r<s;r++){const o=this.Js[r],n=4*r;i[n]=o[0],i[n+1]=o[1],i[n+2]=o[2],i[n+3]=255}this.C.I(i,s,1)}ti(s){this.Js=s,this.Ks()}get ei(){return this.Js}get framebuffer(){return this.C}get texture(){return this.C.texture}}class z extends B{constructor(i,r,o,n={}){super(i,r,o,n);l(this,"palette");this.palette=new ft(this.Ls,this.Os.Ee(" .:-=+*%@#"))}hs(i){y.validate(this.Os.rs(i),"One or more characters do not exist in the current font.",{method:"characters",Ge:i})&&(this.l.hs=i,this.palette.ti(this.Os.Ee(i)))}si(i,r,o,n=255){const h=this.ii(i,"characterColor",r,o,n);h&&(this.l.si=h)}ri(i){this.oi(i,"characterColorMode")}ni(i,r,o,n=255){const h=this.ii(i,"cellColor",r,o,n);h&&(this.l.ni=h)}hi(i){this.oi(i,"cellColorMode")}ai(i){this.li(i,"invert","Invert")}rotation(i){if(!y.validate(typeof i=="number","Rotation angle must be a number.",{method:"rotation",Ge:i}))return;(i%=360)<0&&(i+=360);const r=255*i/360,o=Math.floor(r)/255,n=Math.round(r-o);this.l.rotation=[o,n,0,1]}ci(i){this.li(i,"flipHorizontally","Flip horizontally")}ui(i){this.li(i,"flipVertically","Flip vertically")}ii(i,r,o,n,h=255){let a,u,f,d;if(typeof i=="string"){const g=this.fi(i);if(!g)return y.validate(!1,"Invalid hex color format. Use '#FF0000', '#F00', 'FF0000', or 'F00'.",{method:r,Ge:i}),null;[a,u,f,d]=g}else if(a=i,u=o!==void 0?o:i,f=n!==void 0?n:i,d=h,!y.validate([a,u,f,d].every(g=>g>=0&&g<=255),r.charAt(0).toUpperCase()+r.slice(1)+" color values must be between 0 and 255",{method:r,di:{r:a,g:u,b:f,a:d}}))return null;return[a/255,u/255,f/255,d/255]}oi(i,r){y.validate(["sampled","fixed"].includes(i),"Invalid color mode. Must be 'sampled' or 'fixed'.",{method:r,Ge:i})&&(this.l[r]=i)}li(i,r,o){y.validate(typeof i=="boolean"||typeof i=="number"&&Number.isInteger(i),o+" must be a boolean value or an integer (0 for false, any other number for true).",{method:r,Ge:i})&&(this.l[r]=!!i)}fi(i){return i=i.replace(/^#/,""),/^[0-9A-Fa-f]{3}$|^[0-9A-Fa-f]{6}$/.test(i)?(i.length===3&&(i=i.split("").map(r=>r+r).join("")),[parseInt(i.slice(0,2),16),parseInt(i.slice(2,4),16),parseInt(i.slice(4,6),16),255]):null}}const dt={enabled:!0,hs:" .:-=+*%@#",si:[1,1,1,1],ri:"sampled",ni:[0,0,0,1],hi:"fixed",ai:!1,rotation:[0,0,0,255],ci:!1,ui:!1,mi:[0,255]};class V extends z{constructor(i,r,o){super(i,r,o,{...dt});l(this,"gi");l(this,"pi");l(this,"_i");l(this,"xi");l(this,"bi");l(this,"wi");this.gi=new E(i.context,F,"precision lowp float;uniform sampler2D u_sketchTexture;uniform vec2 u_gridCellDimensions;uniform vec2 u_brightnessRange;varying vec2 v_uv;void main(){vec2 cellCenter=(floor(v_uv*u_gridCellDimensions)+vec2(0.5))/u_gridCellDimensions;vec4 color=texture2D(u_sketchTexture,cellCenter);float brightness=dot(color.rgb,vec3(0.299,0.587,0.114));float brightnessValue=brightness*255.0;if(brightnessValue>=u_brightnessRange.x&&brightnessValue<=u_brightnessRange.y){gl_FragColor=color;}else{gl_FragColor=vec4(0.0);}}"),this.pi=new E(i.context,F,"precision lowp float;uniform sampler2D u_sampleTexture;uniform vec4 u_fillColor;uniform bool u_useFixedColor;varying vec2 v_uv;void main(){vec4 sampleColor=texture2D(u_sampleTexture,v_uv);if(sampleColor.a>0.0){if(u_useFixedColor){gl_FragColor=u_fillColor;}else{gl_FragColor=sampleColor;}}else{gl_FragColor=vec4(0.0);}}"),this.xi=new E(i.context,F,"precision lowp float;uniform sampler2D u_sampleTexture;uniform bool u_invert;uniform bool u_flipHorizontally;uniform bool u_flipVertically;varying vec2 v_uv;void main(){vec4 sampleColor=texture2D(u_sampleTexture,v_uv);if(sampleColor.a>0.0){float invertValue=u_invert ? 1.0 : 0.0;float flipHValue=u_flipHorizontally ? 1.0 : 0.0;float flipVValue=u_flipVertically ? 1.0 : 0.0;gl_FragColor=vec4(invertValue,flipHValue,flipVValue,1.0);}else{gl_FragColor=vec4(0.0);}}"),this.bi=new E(i.context,F,"precision lowp float;uniform sampler2D u_sampleTexture;uniform vec4 u_rotationColor;varying vec2 v_uv;void main(){vec4 sampleColor=texture2D(u_sampleTexture,v_uv);if(sampleColor.a>0.0){gl_FragColor=u_rotationColor;}else{gl_FragColor=vec4(0.0);}}"),this._i=new E(i.context,F,"precision lowp float;uniform sampler2D u_colorSampleFramebuffer;uniform sampler2D u_charPaletteTexture;uniform vec2 u_charPaletteSize;uniform vec2 u_brightnessRange;varying vec2 v_uv;void main(){vec4 color=texture2D(u_colorSampleFramebuffer,v_uv);if(color.a==0.0){gl_FragColor=vec4(0.0);return;}float brightness=dot(color.rgb,vec3(0.299,0.587,0.114))*255.0;vec2 range=u_brightnessRange;if(brightness<range.x||brightness>range.y){gl_FragColor=vec4(0.0);return;}float t=(brightness-range.x)/(range.y-range.x);float idx=clamp(floor(t*u_charPaletteSize.x),0.0,u_charPaletteSize.x-1.0);vec3 charColor=texture2D(u_charPaletteTexture,vec2((idx+0.5)/u_charPaletteSize.x,0.0)).rgb;gl_FragColor=vec4(charColor,1.0);}"),this.wi=this.Ls.createFramebuffer(this.grid.cols,this.grid.rows)}Ci(i){const r=this.grid.cols,o=this.grid.rows;this.wi.P(),this.Ls.clear(),this.Ls.Vt(this.gi),this.Ls.Y("u_sketchTexture",i),this.Ls.Y("u_gridCellDimensions",[r,o]),this.Ls.Y("u_brightnessRange",this.l.mi),this.Ls.rect(0,0,r,o),this.wi.end(),this.Hs.P(),this.Ls.clear(),this.Ls.Vt(this.pi),this.Ls.Y("u_sampleTexture",this.wi),this.Ls.Y("u_fillColor",this.l.si),this.Ls.Y("u_useFixedColor",this.l.ri==="fixed"),this.Ls.rect(0,0,r,o),this.Hs.end(),this.js.P(),this.Ls.clear(),this.Ls.Vt(this.pi),this.Ls.Y("u_sampleTexture",this.wi),this.Ls.Y("u_fillColor",this.l.ni),this.Ls.Y("u_useFixedColor",this.l.hi==="fixed"),this.Ls.rect(0,0,r,o),this.js.end(),this.Ns.P(),this.Ls.clear(),this.Ls.Vt(this.xi),this.Ls.Y("u_sampleTexture",this.wi),this.Ls.Y("u_invert",this.l.ai),this.Ls.Y("u_flipHorizontally",this.l.ci),this.Ls.Y("u_flipVertically",this.l.ui),this.Ls.rect(0,0,r,o),this.Ns.end(),this.Ws.P(),this.Ls.clear(),this.Ls.Vt(this.bi),this.Ls.Y("u_sampleTexture",this.wi),this.Ls.Y("u_rotationColor",this.l.rotation),this.Ls.rect(0,0,r,o),this.Ws.end(),this.Bs.P(),this.Ls.clear(),this.Ls.Vt(this._i),this.Ls.Y("u_colorSampleFramebuffer",this.wi),this.Ls.Y("u_charPaletteTexture",this.palette.texture),this.Ls.Y("u_charPaletteSize",[this.palette.ei.length,1]),this.Ls.Y("u_brightnessRange",this.l.mi),this.Ls.rect(0,0,r,o),this.Bs.end()}resize(){super.resize(),this.wi.resize(this.grid.cols,this.grid.rows)}mi(i){y.validate(Array.isArray(i)&&i.length===2&&i.every(r=>typeof r=="number"&&r>=0&&r<=255),"Brightness range must be an array of two numbers between 0 and 255.",{method:"brightnessRange",Ge:i})&&(this.l.mi=i)}}const gt=Object.freeze(Object.defineProperty({__proto__:null,yi:V,Fi:B,Ti:z},Symbol.toStringTag,{value:"Module"}));class tt{constructor(s,i,r){l(this,"Ls");l(this,"font");l(this,"grid");l(this,"$i");l(this,"Mi");l(this,"Di");l(this,"Si");l(this,"Vi");l(this,"Bs");l(this,"Hs");l(this,"js");l(this,"Ws");l(this,"Ns");this.Ls=s,this.font=i,this.grid=r,this.Vi=this.Ls.createShader(F,"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 sampler2D u_captureTexture;uniform vec2 u_captureDimensions;uniform int u_backgroundMode;uniform vec2 u_gridCellDimensions;uniform vec2 u_gridPixelDimensions;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_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);if(encodedIndexVec.a<0.01){gl_FragColor=(u_backgroundMode==0)? vec4(0.0):texture2D(u_captureTexture,gl_FragCoord.xy/u_captureDimensions);return;}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.Mi=new V(s,i,r),this.Di=new B(s,i,r),this.$i=[this.Mi,this.Di],this.Bs=this.Ls.createFramebuffer(r.cols,r.rows),this.Hs=this.Ls.createFramebuffer(r.cols,r.rows),this.js=this.Ls.createFramebuffer(r.cols,r.rows),this.Ws=this.Ls.createFramebuffer(r.cols,r.rows),this.Ns=this.Ls.createFramebuffer(r.cols,r.rows),this.Si=this.Ls.createFramebuffer(this.grid.width,this.grid.height)}Ri(s){for(const r of this.$i)r.options.enabled&&r instanceof z&&r.Ci(s);const i=(r,o)=>{r.P(),this.Ls.clear();for(const n of this.$i)n.options.enabled&&this.Ls.image(o(n),0,0);r.end()};i(this.Bs,r=>r.Xs),i(this.Hs,r=>r.Ys),i(this.js,r=>r.qs),i(this.Ws,r=>r.Qs),i(this.Ns,r=>r.Zs),this.Si.P(),this.Ls.clear(),this.Ls.Vt(this.Vi),this.Ls.Y("u_characterTexture",this.font.ns),this.Ls.Y("u_charsetDimensions",[this.font.ls,this.font.cs]),this.Ls.Y("u_asciiCharacterTexture",this.Bs.texture),this.Ls.Y("u_primaryColorTexture",this.Hs.texture),this.Ls.Y("u_secondaryColorTexture",this.js.texture),this.Ls.Y("u_transformTexture",this.Ns.texture),this.Ls.Y("u_rotationTexture",this.Ws.texture),this.Ls.Y("u_captureTexture",s.texture),this.Ls.Y("u_backgroundMode",!1),this.Ls.Y("u_captureDimensions",[s.width,s.height]),this.Ls.Y("u_gridCellDimensions",[this.grid.cols,this.grid.rows]),this.Ls.Y("u_gridPixelDimensions",[this.grid.width,this.grid.height]),this.Ls.rect(0,0,this.Si.width,this.Si.height),this.Si.end()}add(s){if(!y.validate(s==="brightness"||s==="custom",'Converter type must be either "brightness" or "custom". Provided: '+s,{method:"add",Ge:s}))return;let i;return i=s==="brightness"?new V(this.Ls,this.font,this.grid):new B(this.Ls,this.font,this.grid),this.$i.push(i),i}remove(s){if(!y.validate(s instanceof B,"Parameter must be a TextmodeConverter instance.",{method:"remove",Ge:s}))return;const i=this.$i.indexOf(s);y.validate(i!==-1,"Converter instance not found in pipeline.",{method:"remove",Ge:s,ki:this.$i.length})&&this.$i.splice(i,1)}get texture(){return this.Si}resize(){this.Si.resize(this.grid.width,this.grid.height);const s=this.grid.cols,i=this.grid.rows;this.Bs.resize(s,i),this.Hs.resize(s,i),this.js.resize(s,i),this.Ws.resize(s,i),this.Ns.resize(s,i);for(const r of this.$i)r.resize()}zi(){return this.$i.some(s=>s.options.enabled)}disable(){for(const s of this.$i)s.disable()}enable(){for(const s of this.$i)s.enable()}dispose(){for(const s of this.$i)s.dispose();this.Bs.dispose(),this.Hs.dispose(),this.js.dispose(),this.Ws.dispose(),this.Ns.dispose(),this.Si.dispose(),this.Vi.dispose()}get Xs(){return this.Bs}get Ys(){return this.Hs}get qs(){return this.js}get Qs(){return this.Ws}get Zs(){return this.Ns}}const pt=c=>class extends c{fill(s,i,r,o){this.ye.fill(s,i,r,o)}stroke(s,i,r,o){this.ye.stroke(s,i,r,o)}Rt(s){this.ye.Rt(s)}kt(){this.ye.kt()}zt(){this.ye.zt()}rotate(s){this.ye.rotate(s)}push(){this.ye.push()}pop(){this.ye.pop()}rect(s,i,r=1,o=1){this.ye.rect(s,i,r,o)}line(s,i,r,o){this.ye.line(s,i,r,o)}background(s,i=s,r=s,o=255){this.ye.background(s,i,r,o)}createShader(s,i){return this.ye.createShader(s,i)}At(s){return this.ye.At(s)}Vt(s){this.ye.Vt(s)}Y(s,i){this.ye.Y(s,i)}};class G{Ii(s){const i=s.get("brightness"),r=i==null?void 0:i.Xs,o=i==null?void 0:i.Ys,n=i==null?void 0:i.qs,h=i==null?void 0:i.Zs,a=i==null?void 0:i.Qs;return r==null||r.A(),o==null||o.A(),n==null||n.A(),h==null||h.A(),a==null||a.A(),{Pi:(r==null?void 0:r.U)||new Uint8Array(0),Ai:(o==null?void 0:o.U)||new Uint8Array(0),Ui:(n==null?void 0:n.U)||new Uint8Array(0),Gi:(h==null?void 0:h.U)||new Uint8Array(0),Ei:(a==null?void 0:a.U)||new Uint8Array(0)}}Li(s,i){return s[i]+(s[i+1]<<8)}Oi(s,i){return{r:s[i],g:s[i+1],b:s[i+2],a:s[i+3]}}}class H{Bi(s,i){return new Blob([s],{type:i})}Hi(s,i,r){try{const o=this.Bi(s,r),n=URL.createObjectURL(o),h=document.createElement("a");h.href=n,h.download=i,h.style.display="none",h.rel="noopener",document.body.appendChild(h),h.click(),document.body.removeChild(h),URL.revokeObjectURL(n)}catch(o){throw console.error("Failed to download file:",o),Error("File download failed: "+(o instanceof Error?o.message:"Unknown error"))}}ji(){return new Date().toISOString().slice(0,19).replace(/:/g,"-")}Wi(){const s=new Date;return{Ni:s.toISOString().split("T")[0],time:s.toTimeString().split(" ")[0].replace(/:/g,"-")}}Xi(s){return s.replace(/[<>:"/\\|?*]/g,"_").replace(/\s+/g,"_").replace(/_{2,}/g,"_").replace(/^_+|_+$/g,"").substring(0,255)}Yi(){return"'textmode-export'-"+this.ji()}}class mt extends G{qi(s,i,r){const o=s[r]===255,n=s[r+1]===255,h=s[r+2]===255,a=i[r],u=i[r+1];return{Qi:o,Zi:n,Ji:h,rotation:Math.round(360*(a+u/255)/255*100)/100}}Ki(s,i,r){return{x:s,y:i,tr:s*r.$s,er:i*r.Ms}}sr(s,i){const r=[];let o=0;for(let n=0;n<i.rows;n++)for(let h=0;h<i.cols;h++){const a=4*o,u=this.Li(s.Pi,a);let f=this.Oi(s.Ai,a),d=this.Oi(s.Ui,a);const g=this.qi(s.Gi,s.Ei,a);if(g.Qi){const w=f;f=d,d=w}const p=this.Ki(h,n,i);r.push({charIndex:u,ir:f,rr:d,transform:g,position:p}),o++}return r}}class _t{nr(s,i){const r=s.cmap;for(const o of r.qt)if(o.format===4){for(let n=0;n<o.ie.length;n++)if(i>=o.ie[n]&&i<=o.se[n]){if(o.oe[n]===0)return i+o.re[n]&65535;{const h=o.oe[n]/2+(i-o.ie[n])-(o.ie.length-n);if(h>=0&&h<o.ne.length){const a=o.ne[h];if(a!==0)return a+o.re[n]&65535}}}}return 0}hr(s,i,r,o,n){const h=n/s.head.unitsPerEm;return{ar:()=>({x1:r+i.xMin*h,y1:o+-i.yMax*h,x2:r+i.xMax*h,y2:o+-i.yMin*h}),lr:()=>this.cr(i,r,o,h)}}cr(s,i,r,o){if(!s||!s.fe)return"";const{fe:n,de:h,ue:a,flags:u}=s;if(!(n&&h&&a&&u))return"";let f="",d=0;for(let g=0;g<a.length;g++){const p=a[g];if(!(p<d)){if(p>=d){const w=i+n[d]*o,b=r-h[d]*o;f+=`M${w.toFixed(2)},${b.toFixed(2)}`;let _=d+1;for(;_<=p;)if(1&u[_]){const T=i+n[_]*o,R=r-h[_]*o;f+=`L${T.toFixed(2)},${R.toFixed(2)}`,_++}else{const T=i+n[_]*o,R=r-h[_]*o;let C=_+1>p?d:_+1;if(1&u[C]){const M=i+n[C]*o,$=r-h[C]*o;f+=`Q${T.toFixed(2)},${R.toFixed(2)} ${M.toFixed(2)},${$.toFixed(2)}`,_=C+1}else{const M=(T+(i+n[C]*o))/2,$=(R+(r-h[C]*o))/2;f+=`Q${T.toFixed(2)},${R.toFixed(2)} ${M.toFixed(2)},${$.toFixed(2)}`,_=C}}f+="Z"}d=p+1}}return f}ur(s,i,r,o,n){const h=s.codePointAt(0)||0,a=this.nr(i,h);let u=null;return i.glyf&&i.glyf[a]!==null?u=i.glyf[a]:m&&m.T&&m.T.glyf&&m.T.glyf.le&&(u=m.T.glyf.le(i,a),i.glyf&&u&&(i.glyf[a]=u)),this.hr(i,u,r,o,n)}dr(s,i,r,o,n,h,a,u){const f=r+(n-u*(a/i.head.unitsPerEm))/2,d=o+(h+.7*a)/2;return this.ur(s,i,f,d,a).lr()||null}}class xt{constructor(){l(this,"mr");this.mr=new _t}gr(s){return`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
8
8
  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
9
- <svg width="${e.width}" height="${e.height}" viewBox="0 0 ${e.width} ${e.height}"
9
+ <svg width="${s.width}" height="${s.height}" viewBox="0 0 ${s.width} ${s.height}"
10
10
  xmlns="http://www.w3.org/2000/svg" version="1.1">
11
11
  <title>textmode art generated via textmode.js</title>
12
- <desc>textmode art visualization generated by textmode.js library</desc>`}generateSVGFooter(){return`
12
+ <desc>textmode art visualization generated by textmode.js library</desc>`}pr(){return`
13
13
  </g>
14
- </svg>`}generateBackground(e,t){if(!t.includeBackgroundRectangles)return"";const r=t.backgroundColor,i=`rgba(${r[0]},${r[1]},${r[2]},${r[3]/255})`;return`
15
- <rect width="${e.width}" height="${e.height}" fill="${i}" />`}rgbaToColorString(e){return`rgba(${e.r},${e.g},${e.b},${e.a/255})`}generateTransformAttribute(e,t){const{transform:r,position:i}=e,s=i.cellX+t.cellWidth/2,a=i.cellY+t.cellHeight/2,n=[];if(r.flipHorizontal||r.flipVertical){const l=r.flipHorizontal?-1:1,c=r.flipVertical?-1:1;n.push(`translate(${s} ${a})`),n.push(`scale(${l} ${c})`),n.push(`translate(${-s} ${-a})`)}return r.rotation&&n.push(`rotate(${r.rotation} ${s} ${a})`),n.length?` transform="${n.join(" ")}"`:""}generateCellBackground(e,t,r){if(!r.includeBackgroundRectangles||e.secondaryColor.a===0)return"";const{position:i}=e,s=this.rgbaToColorString(e.secondaryColor);return r.drawMode==="stroke"?`
16
- <rect x="${i.cellX}" y="${i.cellY}" width="${t.cellWidth}" height="${t.cellHeight}" stroke="${s}" fill="none" stroke-width="${r.strokeWidth}" />`:`
17
- <rect x="${i.cellX}" y="${i.cellY}" width="${t.cellWidth}" height="${t.cellHeight}" fill="${s}" />`}generateCharacterPath(e,t,r,i){const s=r.characters[e.charIndex];if(!s)return"";const a=this.pathGenerator.generatePositionedCharacterPath(s.character,r.font,e.position.cellX,e.position.cellY,t.cellWidth,t.cellHeight,r.fontSize,s.advanceWidth);if(!a)return"";const n=this.rgbaToColorString(e.primaryColor);return i.drawMode==="stroke"?`
18
- <path id="${`path-${e.charIndex}-${e.position.cellX}-${e.position.cellY}`.replace(/\./g,"-")}" d="${a}" stroke="${n}" stroke-width="${i.strokeWidth}" fill="none" />`:`
19
- <path d="${a}" fill="${n}" />`}generateCellContent(e,t,r,i){let s="";s+=this.generateCellBackground(e,t,i);const a=this.generateTransformAttribute(e,t),n=this.generateCharacterPath(e,t,r,i);return n&&(a?(s+=`
20
- <g${a}>`,s+=n,s+=`
21
- </g>`):s+=n),s}generateSVGContent(e,t,r,i){let s=this.generateSVGHeader(t);s+=this.generateBackground(t,i),s+=`
22
- <g id="ascii-cells">`;for(const a of e)s+=this.generateCellContent(a,t,r,i);return s+=this.generateSVGFooter(),s}optimizeSVGContent(e){return e.replace(/<path[^>]*d=""[^>]*\/>/g,"").replace(/\n\s*\n/g,`
23
- `).replace(/[ \t]+$/gm,"")}}class Te extends W{createSVGBlob(e){return this.createBlob(e,"image/svg+xml;charset=utf-8")}downloadSVG(e,t){try{this.downloadFile(e,this.sanitizeFilename(t)+".svg","image/svg+xml;charset=utf-8")}catch(r){throw console.error("Failed to download SVG file:",r),new Error(`SVG download failed: ${r instanceof Error?r.message:"Unknown error"}`)}}saveSVG(e,t){this.downloadSVG(e,t||this.generateDefaultFilename())}}class ee{constructor(){o(this,"dataExtractor");o(this,"contentGenerator");o(this,"fileHandler");this.dataExtractor=new Fe,this.contentGenerator=new ye,this.fileHandler=new Te}applyDefaultOptions(e){return{includeBackgroundRectangles:e.includeBackgroundRectangles??!0,drawMode:e.drawMode??"fill",strokeWidth:e.strokeWidth??1,backgroundColor:e.backgroundColor??[0,0,0,0]}}generateSVG(e,t={}){const r=this.applyDefaultOptions(t),i=this.dataExtractor.extractFramebufferData(e.pipeline),s=this.dataExtractor.extractSVGCellData(i,e.grid),a=this.contentGenerator.generateSVGContent(s,e.grid,e.font,r);return this.contentGenerator.optimizeSVGContent(a)}saveSVG(e,t={}){try{const r=this.generateSVG(e,t),i=t.filename||this.fileHandler.generateDefaultFilename();this.fileHandler.saveSVG(r,i)}catch(r){throw console.error("Failed to save SVG:",r),new Error(`SVG save failed: ${r instanceof Error?r.message:"Unknown error"}`)}}}class Se extends X{extractCharacterGrid(e,t,r,i=" "){var n;const s=[];let a=0;for(let l=0;l<t.rows;l++){const c=[];for(let u=0;u<t.cols;u++){const d=a*4,f=this.getCharacterIndex(e.characterPixels,d),g=((n=r.characters[f])==null?void 0:n.character)||i;c.push(g),a++}s.push(c)}return s}}class Re{generateTXTContent(e,t){const r=[];for(const s of e){let a=s.join("");t.preserveTrailingSpaces||(a=a.replace(/\s+$/,"")),r.push(a)}const i=t.lineEnding==="crlf"?`\r
14
+ </svg>`}vr(s,i){if(!i._r)return"";const r=i.backgroundColor,o=`rgba(${r[0]},${r[1]},${r[2]},${r[3]/255})`;return`
15
+ <rect width="${s.width}" height="${s.height}" fill="${o}" />`}br(s){return`rgba(${s.r},${s.g},${s.b},${s.a/255})`}wr(s,i){const{transform:r,position:o}=s,n=o.tr+i.$s/2,h=o.er+i.Ms/2,a=[];if(r.Zi||r.Ji){const u=r.Zi?-1:1,f=r.Ji?-1:1;a.push(`translate(${n} ${h})`),a.push(`scale(${u} ${f})`),a.push(`translate(${-n} ${-h})`)}return r.rotation&&a.push(`rotate(${r.rotation} ${n} ${h})`),a.length?` transform="${a.join(" ")}"`:""}Cr(s,i,r){if(!r._r||s.rr.a===0)return"";const{position:o}=s,n=this.br(s.rr);return r.yr==="stroke"?`
16
+ <rect x="${o.tr}" y="${o.er}" width="${i.$s}" height="${i.Ms}" stroke="${n}" fill="none" stroke-width="${r.strokeWidth}" />`:`
17
+ <rect x="${o.tr}" y="${o.er}" width="${i.$s}" height="${i.Ms}" fill="${n}" />`}ur(s,i,r,o){const n=r.hs[s.charIndex];if(!n)return"";const h=this.mr.dr(n.Se,r.font,s.position.tr,s.position.er,i.$s,i.Ms,r.fontSize,n.Ae);if(!h)return"";const a=this.br(s.ir);return o.yr==="stroke"?`
18
+ <path id="${`path-${s.charIndex}-${s.position.tr}-${s.position.er}`.replace(/\./g,"-")}" d="${h}" stroke="${a}" stroke-width="${o.strokeWidth}" fill="none" />`:`
19
+ <path d="${h}" fill="${a}" />`}Fr(s,i,r,o){let n="";n+=this.Cr(s,i,o);const h=this.wr(s,i),a=this.ur(s,i,r,o);return a&&(h?(n+=`
20
+ <g${h}>`,n+=a,n+=`
21
+ </g>`):n+=a),n}Tr(s,i,r,o){let n=this.gr(i);n+=this.vr(i,o),n+=`
22
+ <g id="ascii-cells">`;for(const h of s)n+=this.Fr(h,i,r,o);return n+=this.pr(),n}$r(s){return s.replace(/<path[^>]*d=""[^>]*\/>/g,"").replace(/\n\s*\n/g,`
23
+ `).replace(/[ \t]+$/gm,"")}}class wt extends H{Mr(s){return this.Bi(s,"image/svg+xml;charset=utf-8")}Dr(s,i){this.Hi(s,this.Xi(i)+".svg","image/svg+xml;charset=utf-8")}Sr(s,i){this.Dr(s,i||this.Yi())}}class et{constructor(){l(this,"Vr");l(this,"Rr");l(this,"kr");this.Vr=new mt,this.Rr=new xt,this.kr=new wt}zr(s){return{_r:s._r??!0,yr:s.yr??"fill",strokeWidth:s.strokeWidth??1,backgroundColor:s.backgroundColor??[0,0,0,0]}}Ir(s,i={}){const r=this.zr(i),o=this.Vr.Ii(s.Pr),n=this.Vr.sr(o,s.grid),h=this.Rr.Tr(n,s.grid,s.font,r);return this.Rr.$r(h)}Sr(s,i={}){const r=this.Ir(s,i),o=i.filename||this.kr.Yi();this.kr.Sr(r,o)}}class vt extends G{Ar(s,i,r,o=" "){var a;const n=[];let h=0;for(let u=0;u<i.rows;u++){const f=[];for(let d=0;d<i.cols;d++){const g=4*h,p=this.Li(s.Pi,g),w=((a=r.hs[p])==null?void 0:a.Se)||o;f.push(w),h++}n.push(f)}return n}}class bt{Ur(s,i){const r=[];for(const n of s){let h=n.join("");i.Gr||(h=h.replace(/\s+$/,"")),r.push(h)}const o=i.Er==="crlf"?`\r
24
24
  `:`
25
- `;return r.join(i)}}class Ee extends W{saveTXT(e,t){try{const r=this.ensureValidFilename(t);this.downloadFile(e,r,"text/plain;charset=utf-8")}catch(r){throw console.error("Failed to save TXT file:",r),new Error(`TXT file save failed: ${r instanceof Error?r.message:"Unknown error"}`)}}ensureValidFilename(e){let t=this.sanitizeFilename(e);return t===".txt"||t.length<=4?this.generateDefaultFilename():t}}class te{constructor(){o(this,"dataExtractor");o(this,"contentGenerator");o(this,"fileHandler");this.dataExtractor=new Se,this.contentGenerator=new Re,this.fileHandler=new Ee}applyDefaultOptions(e){return{preserveTrailingSpaces:e.preserveTrailingSpaces??!1,lineEnding:e.lineEnding??"lf",emptyCharacter:e.emptyCharacter??" "}}generateTXT(e,t={}){const r=this.applyDefaultOptions(t),i=this.dataExtractor.extractFramebufferData(e.pipeline),s=this.dataExtractor.extractCharacterGrid(i,e.grid,e.font,r.emptyCharacter);return this.contentGenerator.generateTXTContent(s,r)}saveTXT(e,t={}){try{const r=this.generateTXT(e,t),i=t.filename||this.fileHandler.generateDefaultFilename();this.fileHandler.saveTXT(r,i)}catch(r){throw console.error("Failed to save TXT:",r),new Error(`TXT save failed: ${r instanceof Error?r.message:"Unknown error"}`)}}}class Ue extends X{captureCanvasData(e,t=1,r="transparent"){const i=e.canvas;if(t===1&&r==="transparent")return i;const s=document.createElement("canvas"),a=s.getContext("2d"),n=Math.round(i.width*t),l=Math.round(i.height*t);return s.width=n,s.height=l,r!=="transparent"&&(a.fillStyle=r,a.fillRect(0,0,n,l)),a.imageSmoothingEnabled=!1,a.drawImage(i,0,0,i.width,i.height,0,0,n,l),s}}class Me{generateImageData(e,t){const r=this.getMimeType(t.format);return t.format==="png"?e.toDataURL(r):e.toDataURL(r,t.quality)}async generateImageBlob(e,t){return new Promise((r,i)=>{const s=this.getMimeType(t.format),a=n=>{n?r(n):i(new Error(`Failed to generate ${t.format.toUpperCase()} blob`))};t.format==="png"?e.toBlob(a,s):e.toBlob(a,s,t.quality)})}getMimeType(e){switch(e){case"png":return"image/png";case"jpg":return"image/jpeg";case"webp":return"image/webp";default:throw new Error(`Unsupported image format: ${e}`)}}}const re={png:"image/png",jpg:"image/jpeg",webp:"image/webp"},Y={png:".png",jpg:".jpg",webp:".webp"};class Ae extends W{saveImage(e,t,r){try{const i=Y[r];typeof e=="string"?this.saveImageFromDataURL(e,this.sanitizeFilename(t)+i):this.saveImageFromBlob(e,this.sanitizeFilename(t)+i)}catch(i){throw console.error(`Failed to save ${r.toUpperCase()} image:`,i),new Error(`Image save failed: ${i instanceof Error?i.message:"Unknown error"}`)}}saveImageFromDataURL(e,t){const r=document.createElement("a");r.href=e,r.download=t,r.style.display="none",r.rel="noopener",document.body.appendChild(r),r.click(),document.body.removeChild(r)}saveImageFromBlob(e,t){const r=URL.createObjectURL(e);try{const i=document.createElement("a");i.href=r,i.download=t,i.style.display="none",i.rel="noopener",document.body.appendChild(i),i.click(),document.body.removeChild(i)}finally{URL.revokeObjectURL(r)}}validateSaveSupport(e){return e in re&&e in Y}getMimeType(e){return re[e]}getFileExtension(e){return Y[e]}}class De{constructor(){o(this,"dataExtractor");o(this,"contentGenerator");o(this,"fileHandler");this.dataExtractor=new Ue,this.contentGenerator=new Me,this.fileHandler=new Ae}applyDefaultOptions(e){return{format:e.format??"png",quality:e.quality??1,scale:e.scale??1,backgroundColor:e.backgroundColor??"transparent"}}validateOptions(e){if(console.log("Validating image export options:",e),!this.fileHandler.validateSaveSupport(e.format))throw new Error(`Saving '${e.format}' files is not supported`);if(e.quality<0||e.quality>1)throw new Error("Image quality must be between 0.0 and 1.0");if(e.scale<=0)throw new Error("Scale factor must be greater than 0");e.scale>10&&console.warn("Large scale factors may result in very large files and slow performance"),e.format==="jpg"&&e.backgroundColor==="transparent"&&(e.backgroundColor="black")}generateImage(e,t={}){const r=this.applyDefaultOptions(t);if(this.validateOptions(r),r.scale===1&&r.backgroundColor==="transparent")return this.contentGenerator.generateImageData(e.canvas,r);const i=this.dataExtractor.captureCanvasData(e,r.scale,r.backgroundColor);return this.contentGenerator.generateImageData(i,r)}async generateImageBlob(e,t={}){const r=this.applyDefaultOptions(t);if(this.validateOptions(r),r.scale===1&&r.backgroundColor==="transparent")return await this.contentGenerator.generateImageBlob(e.canvas,r);const i=this.dataExtractor.captureCanvasData(e,r.scale,r.backgroundColor);return await this.contentGenerator.generateImageBlob(i,r)}async saveImage(e,t={}){try{const r=await this.generateImageBlob(e,t),i=t.format??"png",s=t.filename||this.fileHandler.generateDefaultFilename();this.fileHandler.saveImage(r,s,i)}catch(r){throw console.error("Failed to save image:",r),new Error(`Image save failed: ${r instanceof Error?r.message:"Unknown error"}`)}}}class P{constructor(e=null,t={}){o(this,"captureSource");o(this,"textmodeCanvas");o(this,"_renderer");o(this,"_canvasFramebuffer");o(this,"_font");o(this,"_grid");o(this,"resizeObserver");o(this,"_mode");o(this,"_frameRateLimit");o(this,"animationFrameId",null);o(this,"lastFrameTime",0);o(this,"frameInterval");o(this,"_isLooping",!0);o(this,"_frameRate",0);o(this,"lastRenderTime",0);o(this,"_frameCount",0);o(this,"frameTimeHistory",[]);o(this,"frameTimeHistorySize",10);o(this,"_pipeline");o(this,"_isDisposed",!1);o(this,"_standalone",!1);o(this,"_drawCallback",()=>{});o(this,"_resizedCallback",()=>{});o(this,"_windowResizeListener",null);o(this,"_printDebug",!1);this.captureSource=e,this._standalone=e===null,this._mode=t.renderMode??"auto",this._frameRateLimit=t.frameRate??60,this.frameInterval=1e3/this._frameRateLimit}static async create(e=null,t={}){const r=new P(e,t),i=r._standalone?t:void 0;r.textmodeCanvas=new Z(r.captureSource,r._standalone,i),r._renderer=new he(r.textmodeCanvas.getWebGLContext());let s,a;r._standalone?(s=t.width||800,a=t.height||600):(s=r.textmodeCanvas.width||800,a=r.textmodeCanvas.height||600),r._canvasFramebuffer=r._renderer.createFramebuffer(s,a),r._font=new K(r._renderer,t.fontSize??16),await r._font.initialize(t.fontSource);const n=r._font.maxGlyphDimensions;return r._grid=new Q(r.textmodeCanvas.canvas,n.width,n.height),r._pipeline=new J(r._renderer,r._font,r._grid),r.setupEventListeners(),r.startAutoRendering(),r}setupEventListeners(){this._windowResizeListener=()=>{this._standalone?this._resizedCallback():this.resize()},window.addEventListener("resize",this._windowResizeListener),window.ResizeObserver&&this.captureSource&&!this._standalone&&(this.resizeObserver=new ResizeObserver(()=>{this.resize()}),this.resizeObserver.observe(this.captureSource))}toString(e={}){return new te().generateTXT(this,e)}saveStrings(e={}){new te().saveTXT(this,e)}toSVG(e={}){return new ee().generateSVG(this,e)}saveSVG(e={}){new ee().saveSVG(this,e)}async saveCanvas(e,t="png",r={}){await new De().saveImage(this.textmodeCanvas,{...r,filename:e,format:t})}async loadFont(e){return this._font.loadFont(e).then(()=>{const t=this._font.maxGlyphDimensions;this._grid.resizeCellPixelDimensions(t.width,t.height),this._pipeline.resize()})}render(){if(this.measureFrameRate(),this._frameCount++,this._isDisposed){console.warn("Cannot render: Required resources have been disposed");return}if(this._standalone?this._canvasFramebuffer&&(this._canvasFramebuffer.begin(),this._drawCallback(),this._renderer&&this._renderer.reset(),this._canvasFramebuffer&&this._canvasFramebuffer.end()):this.captureSource&&this._canvasFramebuffer&&this._canvasFramebuffer.update(this.captureSource),!this._pipeline||!this._renderer){console.warn("Cannot complete render: Pipeline or renderer has been disposed");return}if(this._pipeline.render(this._canvasFramebuffer),this._pipeline.hasEnabledConverters()?(this._renderer.background(0),this._renderer.image(this._pipeline.texture,this._grid.offsetX,this._grid.offsetY,this._pipeline.texture.width,this._pipeline.texture.height)):(this._renderer.clear(),this._renderer.image(this._canvasFramebuffer,this._grid.offsetX,this._grid.offsetY,this._canvasFramebuffer.width,this._canvasFramebuffer.height)),this._printDebug){let e=0;this._renderer.image(this._pipeline.characterFramebuffer,e,0,this._pipeline.characterFramebuffer.width,this._pipeline.characterFramebuffer.height),e+=this._pipeline.characterFramebuffer.width,this._renderer.image(this._pipeline.primaryColorFramebuffer,e,0,this._pipeline.primaryColorFramebuffer.width,this._pipeline.primaryColorFramebuffer.height),e+=this._pipeline.primaryColorFramebuffer.width,this._renderer.image(this._pipeline.secondaryColorFramebuffer,e,0,this._pipeline.secondaryColorFramebuffer.width,this._pipeline.secondaryColorFramebuffer.height),e+=this._pipeline.secondaryColorFramebuffer.width,this._renderer.image(this._pipeline.transformFramebuffer,e,0,this._pipeline.transformFramebuffer.width,this._pipeline.transformFramebuffer.height),e+=this._pipeline.transformFramebuffer.width,this._renderer.image(this._pipeline.rotationFramebuffer,e,0,this._pipeline.rotationFramebuffer.width,this._pipeline.rotationFramebuffer.height)}}resize(){this.textmodeCanvas.resize(),this._canvasFramebuffer.resize(this.textmodeCanvas.width,this.textmodeCanvas.height),this._grid.resize(),this._pipeline.resize(),this._renderer.resetViewport(),this._mode!=="manual"&&this.render()}startAutoRendering(){if(this._mode!=="auto"||!this._isLooping)return;this.lastFrameTime=performance.now();const e=t=>{if(!this._isLooping){this.animationFrameId=null;return}const r=t-this.lastFrameTime;r>=this.frameInterval&&(this.render(),this.lastFrameTime=t-r%this.frameInterval),this._isLooping&&(this.animationFrameId=requestAnimationFrame(e))};this.animationFrameId=requestAnimationFrame(e)}measureFrameRate(){const e=performance.now();if(this.lastRenderTime>0){const t=e-this.lastRenderTime;this.frameTimeHistory.push(t),this.frameTimeHistory.length>this.frameTimeHistorySize&&this.frameTimeHistory.shift();const r=this.frameTimeHistory.reduce((i,s)=>i+s,0)/this.frameTimeHistory.length;this._frameRate=1e3/r}this.lastRenderTime=e}stopAutoRendering(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}renderMode(e){this._mode!==e&&(this.stopAutoRendering(),this._mode=e,e==="auto"&&this._isLooping&&this.startAutoRendering())}frameRate(e){if(e===void 0)return this._frameRate;this._frameRateLimit=e,this.frameInterval=1e3/e,this._mode==="auto"&&this._isLooping&&(this.stopAutoRendering(),this.startAutoRendering())}noLoop(){this._isLooping&&(this._isLooping=!1,this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null))}loop(){this._isLooping||(this._isLooping=!0,this._mode==="auto"&&this.startAutoRendering())}redraw(e=1){if(b.validate(typeof e=="number"&&e>0&&Number.isInteger(e),"Redraw count must be a positive integer.",{method:"redraw",providedValue:e}))for(let t=0;t<e;t++)this.render()}isLooping(){return this._mode==="auto"&&this._isLooping}fontSize(e){b.validate(typeof e=="number"&&e>0,"Font size must be a positive number greater than 0.",{method:"fontSize",providedValue:e})&&this._font.fontSize!==e&&(this._font.setFontSize(e),this._grid.resizeCellPixelDimensions(this._font.maxGlyphDimensions.width,this._font.maxGlyphDimensions.height),this._pipeline.resize(),this._renderer.resetViewport())}draw(e){this._drawCallback=e}windowResized(e){this._resizedCallback=e}resizeCanvas(e,t){this.textmodeCanvas.resize(e,t),this._canvasFramebuffer.resize(this.textmodeCanvas.width,this.textmodeCanvas.height),this._grid.resize(),this._pipeline.resize(),this._renderer.resetViewport(),this._mode!=="manual"&&this.render()}converter(e){return this._pipeline.get(e)}fill(e,t,r,i){this._renderer.fill(e,t,r,i)}stroke(e,t,r,i){this._renderer.stroke(e,t,r,i)}strokeWeight(e){this._renderer.strokeWeight(e)}noStroke(){this._renderer.noStroke()}noFill(){this._renderer.noFill()}rotate(e){this._renderer.rotate(e)}push(){this._renderer.push()}pop(){this._renderer.pop()}rect(e,t,r=1,i=1){this._renderer.rect(e,t,r,i)}line(e,t,r,i){this._renderer.line(e,t,r,i)}background(e,t=e,r=e,i=255){this._renderer.background(e,t,r,i)}createShader(e,t){return this._renderer.createShader(e,t)}createFilterShader(e){return this._renderer.createFilterShader(e)}shader(e){this._renderer.shader(e)}setUniform(e,t){this._renderer.setUniform(e,t)}destroy(){this._isDisposed=!0,this.stopAutoRendering(),this._windowResizeListener&&(window.removeEventListener("resize",this._windowResizeListener),this._windowResizeListener=null),this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=null),this._pipeline.dispose(),this._font.dispose(),this._grid.dispose(),this._canvasFramebuffer.dispose(),this._renderer.dispose(),this.textmodeCanvas.dispose(),this.captureSource=null,this.textmodeCanvas=null,this._renderer=null,this._canvasFramebuffer=null,this._font=null,this._grid=null,this._pipeline=null,this.resizeObserver=null,this._drawCallback=()=>{},this._resizedCallback=()=>{},this.animationFrameId=null,this.lastFrameTime=0,this.lastRenderTime=0,this._frameCount=0,this._frameRate=0,this.frameTimeHistory=[],this._isLooping=!1}debug(e){this._printDebug=e}get grid(){return this._grid}get font(){return this._font}get mode(){return this._mode}get pipeline(){return this._pipeline}get frameCount(){return this._frameCount}set frameCount(e){this._frameCount=e}get width(){return this.textmodeCanvas.width}get height(){return this.textmodeCanvas.height}get isDisposed(){return this._isDisposed}}class B{static async create(e,t={}){if(e instanceof HTMLCanvasElement||e instanceof HTMLVideoElement)return P.create(e,t);{const r=e||{};return P.create(null,r)}}static setErrorLevel(e){b.setGlobalLevel(e)}static get version(){return"0.1.6-beta.4"}constructor(){throw new Error("Textmode is a static class and cannot be instantiated.")}}const Ie=Object.freeze(Object.defineProperty({__proto__:null},Symbol.toStringTag,{value:"Module"})),Ge=B.create,Pe=B.setErrorLevel,Be=B.version;v.TextmodeCanvas=Z,v.TextmodeConversionPipeline=J,v.TextmodeErrorLevel=M,v.TextmodeFont=K,v.TextmodeGrid=Q,v.Textmodifier=P,v.converters=xe,v.create=Ge,v.default=B,v.export=Ie,v.setErrorLevel=Pe,v.textmode=B,v.version=Be,Object.defineProperties(v,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
25
+ `;return r.join(o)}}class yt extends H{Lr(s,i){const r=this.Or(i);this.Hi(s,r,"text/plain;charset=utf-8")}Or(s){let i=this.Xi(s);return i===".txt"||i.length<=4?this.Yi():i}}class st{constructor(){l(this,"Vr");l(this,"Rr");l(this,"kr");this.Vr=new vt,this.Rr=new bt,this.kr=new yt}zr(s){return{Gr:s.Gr??!1,Er:s.Er??"lf",Br:s.Br??" "}}Hr(s,i={}){const r=this.zr(i),o=this.Vr.Ii(s.Pr),n=this.Vr.Ar(o,s.grid,s.font,r.Br);return this.Rr.Ur(n,r)}Lr(s,i={}){const r=this.Hr(s,i),o=i.filename||this.kr.Yi();this.kr.Lr(r,o)}}class Tt extends G{jr(s,i=1,r="transparent"){const o=s.canvas;if(i===1&&r==="transparent")return o;const n=document.createElement("canvas"),h=n.getContext("2d"),a=Math.round(o.width*i),u=Math.round(o.height*i);return n.width=a,n.height=u,r!=="transparent"&&(h.fillStyle=r,h.fillRect(0,0,a,u)),h.imageSmoothingEnabled=!1,h.drawImage(o,0,0,o.width,o.height,0,0,a,u),n}}class Ct{Wr(s,i){const r=this.Nr(i.format);return i.format==="png"?s.toDataURL(r):s.toDataURL(r,i.Xr)}async Yr(s,i){return new Promise((r,o)=>{const n=this.Nr(i.format),h=a=>{a?r(a):o(Error(`Failed to generate ${i.format.toUpperCase()} blob`))};i.format==="png"?s.toBlob(h,n):s.toBlob(h,n,i.Xr)})}Nr(s){switch(s){case"png":return"image/png";case"jpg":return"image/jpeg";case"webp":return"image/webp";default:throw Error("Unsupported image format: "+s)}}}const it={qr:"image/png",Qr:"image/jpeg",Zr:"image/webp"},O={qr:".png",Qr:".jpg",Zr:".webp"};class Rt extends H{Jr(s,i,r){this.Kr(s,this.Xi(i)+O[r])}Kr(s,i){const r=URL.createObjectURL(s);try{const o=document.createElement("a");o.href=r,o.download=i,o.style.display="none",o.rel="noopener",document.body.appendChild(o),o.click(),document.body.removeChild(o)}finally{URL.revokeObjectURL(r)}}eo(s){return s in it&&s in O}Nr(s){return it[s]}so(s){return O[s]}}class Et{constructor(){l(this,"Vr");l(this,"Rr");l(this,"kr");this.Vr=new Tt,this.Rr=new Ct,this.kr=new Rt}zr(s){return{format:s.format??"png",Xr:s.Xr??1,scale:s.scale??1,backgroundColor:s.backgroundColor??"transparent"}}io(s){if(console.log("Validating image export options:",s),!this.kr.eo(s.format))throw Error(`Saving '${s.format}' files is not supported`);if(s.Xr<0||s.Xr>1)throw Error("Image quality must be between 0.0 and 1.0");if(s.scale<=0)throw Error("Scale factor must be greater than 0");s.scale>10&&console.warn("Large scale factors may result in very large files and slow performance"),s.format==="jpg"&&s.backgroundColor==="transparent"&&(s.backgroundColor="black")}ro(s,i={}){const r=this.zr(i);if(this.io(r),r.scale===1&&r.backgroundColor==="transparent")return this.Rr.Wr(s.canvas,r);const o=this.Vr.jr(s,r.scale,r.backgroundColor);return this.Rr.Wr(o,r)}async Yr(s,i={}){const r=this.zr(i);if(this.io(r),r.scale===1&&r.backgroundColor==="transparent")return await this.Rr.Yr(s.canvas,r);const o=this.Vr.jr(s,r.scale,r.backgroundColor);return await this.Rr.Yr(o,r)}async Jr(s,i={}){const r=await this.Yr(s,i),o=i.format??"png",n=i.filename||this.kr.Yi();this.kr.Jr(r,n,o)}}const Ft=c=>class extends c{toString(s={}){return new st().Hr(this,s)}oo(s={}){new st().Lr(this,s)}lr(s={}){return new et().Ir(this,s)}Sr(s={}){new et().Sr(this,s)}async no(s,i="png",r={}){await new Et().Jr(this.ho,{...r,filename:s,format:i})}},Lt=c=>class extends c{async ss(s){return this.Le.ss(s).then(()=>{const i=this.Le.us;this.ao.Cs(i.width,i.height),this.lo.resize()})}fontSize(s){if(!y.validate(typeof s=="number"&&s>0,"Font size must be a positive number greater than 0.",{method:"fontSize",Ge:s})||this.Le.fontSize===s)return;this.Le.es(s);const i=this.Le.us;this.ao.Cs(i.width,i.height),this.lo.resize(),this.ye.Ot()}},Dt=c=>class extends c{co(s){return this.lo.add(s)}uo(s){this.lo.remove(s)}};class Mt{constructor(){l(this,"ye");l(this,"Le");l(this,"lo");l(this,"ho");l(this,"ao")}}class j extends function(i,...r){return r.reduce((o,n)=>n(o),i)}(Mt,pt,Ft,Lt,Dt){constructor(i=null,r={}){super();l(this,"Ds");l(this,"fo");l(this,"Vs");l(this,"do");l(this,"mo");l(this,"po",null);l(this,"vo",0);l(this,"_o");l(this,"xo",!0);l(this,"bo",0);l(this,"wo",0);l(this,"Co",0);l(this,"yo",[]);l(this,"Fo",10);l(this,"To",!1);l(this,"$o",!1);l(this,"Mo",()=>{});l(this,"Do",()=>{});l(this,"So");l(this,"Vo",!1);this.Ds=i,this.$o=i===null,this.do=r.Ro??"auto",this.mo=r.ko??60,this._o=1e3/this.mo}static async create(i=null,r={}){const o=new this(i,r),n=o.$o?r:void 0;let h,a;o.ho=new Q(o.Ds,o.$o,n),o.ye=new ht(o.ho.Ps()),o.$o?(h=r.width||800,a=r.height||600):(h=o.ho.width||800,a=o.ho.height||600),o.fo=o.ye.createFramebuffer(h,a),o.Le=new Z(o.ye,r.fontSize??16),await o.Le.initialize(r.zo);const u=o.Le.us;return o.ao=new K(o.ho.canvas,u.width,u.height),o.lo=new tt(o.ye,o.Le,o.ao),o.Io(),o.Po(),o}Io(){this.So=()=>{this.$o?this.Do():this.resize()},window.addEventListener("resize",this.So),window.ResizeObserver&&this.Ds&&!this.$o&&(this.Vs=new ResizeObserver(()=>{this.resize()}),this.Vs.observe(this.Ds))}Ri(){if(this.Ao(),this.Co++,this.To)console.warn("Cannot render: Required resources have been disposed");else if(this.$o?(this.fo.P(),this.Mo(),this.fo.end()):this.fo.update(this.Ds),this.lo.zi()?(this.lo.Ri(this.fo),this.ye.background(0),this.ye.image(this.lo.texture,this.ao.offsetX,this.ao.offsetY,this.lo.texture.width,this.lo.texture.height)):(this.ye.clear(),this.ye.image(this.fo,this.ao.offsetX,this.ao.offsetY,this.fo.width,this.fo.height)),this.Vo){let i=0;this.ye.image(this.lo.Xs,i,0,this.lo.Xs.width,this.lo.Xs.height),i+=this.lo.Xs.width,this.ye.image(this.lo.Ys,i,0,this.lo.Ys.width,this.lo.Ys.height),i+=this.lo.Ys.width,this.ye.image(this.lo.qs,i,0,this.lo.qs.width,this.lo.qs.height),i+=this.lo.qs.width,this.ye.image(this.lo.Zs,i,0,this.lo.Zs.width,this.lo.Zs.height),i+=this.lo.Zs.width,this.ye.image(this.lo.Qs,i,0,this.lo.Qs.width,this.lo.Qs.height)}}resize(){this.ho.resize(),this.fo.resize(this.ho.width,this.ho.height),this.ao.resize(),this.lo.resize(),this.ye.Ot(),this.do!=="manual"&&this.Ri()}Po(){if(this.do!=="auto"||!this.xo)return;this.vo=performance.now();const i=r=>{if(!this.xo)return void(this.po=null);const o=r-this.vo;o>=this._o&&(this.Ri(),this.vo=r-o%this._o),this.xo&&(this.po=requestAnimationFrame(i))};this.po=requestAnimationFrame(i)}Ao(){const i=performance.now();if(this.wo>0){const r=i-this.wo;this.yo.push(r),this.yo.length>this.Fo&&this.yo.shift();const o=this.yo.reduce((n,h)=>n+h,0)/this.yo.length;this.bo=1e3/o}this.wo=i}Uo(){this.po&&(cancelAnimationFrame(this.po),this.po=null)}Ro(i){this.do!==i&&(this.Uo(),this.do=i,i==="auto"&&this.xo&&this.Po())}ko(i){if(i===void 0)return this.bo;this.mo=i,this._o=1e3/i,this.do==="auto"&&this.xo&&(this.Uo(),this.Po())}Go(){this.xo&&(this.xo=!1,this.po&&(cancelAnimationFrame(this.po),this.po=null))}loop(){this.xo||(this.xo=!0,this.do==="auto"&&this.Po())}Eo(i=1){if(y.validate(typeof i=="number"&&i>0&&Number.isInteger(i),"Redraw count must be a positive integer.",{method:"redraw",Ge:i}))for(let r=0;r<i;r++)this.Ri()}Lo(){return this.do==="auto"&&this.xo}draw(i){this.Mo=i}Oo(i){this.Do=i}Bo(i,r){this.ho.resize(i,r),this.fo.resize(this.ho.width,this.ho.height),this.ao.resize(),this.lo.resize(),this.ye.Ot(),this.do!=="manual"&&this.Ri()}destroy(){this.To||(this.Uo(),window.removeEventListener("resize",this.So),this.Vs&&this.Vs.disconnect(),this.lo.dispose(),this.Le.dispose(),this.fo.dispose(),this.ye.dispose(),this.To=!0)}debug(i){this.Vo=i}get grid(){return this.ao}get font(){return this.Le}get mode(){return this.do}get Pr(){return this.lo}get frameCount(){return this.Co}set frameCount(i){this.Co=i}get width(){return this.ho.width}get height(){return this.ho.height}get Ho(){return this.To}}class I{static async create(s,i={}){if(s instanceof HTMLCanvasElement||s instanceof HTMLVideoElement)return j.create(s,i);{const r=s||{};return j.create(null,r)}}static jo(s){y.v(s)}static get version(){return"0.1.6-beta.6"}}const $t=Object.freeze(Object.defineProperty({__proto__:null},Symbol.toStringTag,{value:"Module"})),At=I.create,St=I.jo,Ut=I.version;v.Wo=Q,v.No=tt,v.Xo=L,v.Yo=Z,v.qo=K,v.Qo=j,v.$i=gt,v.create=At,v.Zo=$t,v.jo=St,v.Jo=I,v.version=Ut,Object.defineProperty(v,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).Jo={});
@@ -2,10 +2,6 @@ import { Framebuffer } from './rendering/webgl/Framebuffer';
2
2
  import type { GLRenderer } from './rendering/webgl/Renderer';
3
3
  /**
4
4
  * A 1D color palette stored in a framebuffer that is used to pass colors to shaders.
5
- *
6
- * There is no need to modify instances of this class provided by the library,
7
- * as they are managed internally and can be modified more easily through classes managing them.
8
- * But you technically could - *if you wanted to* - without breaking anything.
9
5
  */
10
6
  export declare class ColorPalette {
11
7
  /** The framebuffer used to store the color palette. */
@@ -21,7 +17,7 @@ export declare class ColorPalette {
21
17
  /**
22
18
  * Update the framebuffer with the currently selected colors.
23
19
  */
24
- private _updateFramebuffer;
20
+ private _update;
25
21
  /**
26
22
  * Sets the colors of the palette and updates the framebuffer.
27
23
  * @param newColors The new RGB colors to set as [r, g, b] arrays.
@@ -1,14 +1,5 @@
1
1
  import type { TextmodeErrorLevel } from './errors';
2
2
  import { Textmodifier, type TextmodeOptions, type CaptureSource } from './textmode/Textmodifier';
3
- export { Textmodifier } from './textmode/Textmodifier';
4
- export { TextmodeFont } from './textmode/font';
5
- export { TextmodeCanvas } from './textmode/Canvas';
6
- export { TextmodeGrid } from './textmode/Grid';
7
- export type { TextmodeOptions, CaptureSource } from './textmode/Textmodifier';
8
- export type { TextmodeCharacter } from './textmode/font';
9
- export { TextmodeErrorLevel } from './errors/ErrorHandler';
10
- /** Contains all converters that can be added to a rendering pipeline to shape the textmode output. */
11
- export * as TextmodeConverters from './textmode/converters';
12
3
  /**
13
4
  * The main entry point for the `textmode.js` library.
14
5
  *
@@ -95,5 +86,4 @@ export declare class Textmode {
95
86
  * ```
96
87
  */
97
88
  static get version(): string;
98
- private constructor();
99
89
  }
@@ -1,4 +1,4 @@
1
- import type { TextmodeCanvas } from '../../Textmode';
1
+ import type { TextmodeCanvas } from '../../index';
2
2
  import { DataExtractor } from '../base/DataExtractor';
3
3
  /**
4
4
  * Data extractor for image export.
@@ -1,5 +1,5 @@
1
1
  import type { ImageExportOptions } from './types';
2
- import type { TextmodeCanvas } from '../../Textmode';
2
+ import type { TextmodeCanvas } from '../../index';
3
3
  /**
4
4
  * Main image exporter for the textmode.js library.
5
5
  * Orchestrates the image export process by coordinating canvas capture,
@@ -11,13 +11,7 @@ export declare class ImageFileHandler extends FileHandler {
11
11
  * @param filename The filename (without extension)
12
12
  * @param format The image format
13
13
  */
14
- saveImage(content: string | Blob, filename: string, format: ImageFormat): void;
15
- /**
16
- * Saves image from data URL
17
- * @param dataURL The data URL containing image data
18
- * @param filename The complete filename with extension
19
- */
20
- private saveImageFromDataURL;
14
+ saveImage(content: Blob, filename: string, format: ImageFormat): void;
21
15
  /**
22
16
  * Saves image from blob
23
17
  * @param blob The blob containing image data
@@ -1,5 +1,5 @@
1
1
  import type { SVGCellData, SVGGenerationOptions } from './types';
2
- import type { TextmodeFont, TextmodeGrid } from '../../Textmode';
2
+ import type { TextmodeFont, TextmodeGrid } from '../../index';
3
3
  /**
4
4
  * Generates SVG content and markup from processed cell data.
5
5
  * This class handles the creation of SVG elements, groups, and styling.
@@ -1,6 +1,6 @@
1
1
  import type { FramebufferData, SVGCellData } from './types';
2
2
  import { DataExtractor } from '../base';
3
- import type { TextmodeGrid } from '../../Textmode';
3
+ import type { TextmodeGrid } from '../../index';
4
4
  /**
5
5
  * Extracts and processes data from framebuffers for SVG generation.
6
6
  * This class handles the conversion of raw pixel data into structured data objects.
@@ -12,11 +12,6 @@ export declare class SVGPathGenerator {
12
12
  * @returns The glyph index, or 0 if not found
13
13
  */
14
14
  private getGlyphIndex;
15
- /**
16
- * Creates an empty path object for characters with no glyph data
17
- * @returns Empty path object
18
- */
19
- private createEmptyPath;
20
15
  /**
21
16
  * Creates a path object for a glyph
22
17
  * @param fontData Font data object
@@ -1,6 +1,6 @@
1
1
  import type { FramebufferData } from './types';
2
2
  import { DataExtractor } from '../base';
3
- import type { TextmodeFont, TextmodeGrid } from '../../Textmode';
3
+ import type { TextmodeFont, TextmodeGrid } from '../../index';
4
4
  /**
5
5
  * Extracts and processes data from framebuffers for TXT generation.
6
6
  * This class handles the conversion of raw pixel data into character arrays.
@@ -12,7 +12,6 @@ export { TextmodeErrorLevel } from './errors/ErrorHandler';
12
12
  /** Contains all converters that can be added to a rendering pipeline to shape the textmode output. */
13
13
  export * as converters from './textmode/converters';
14
14
  export { Textmode as textmode } from './Textmode';
15
- export default Textmode;
16
15
  /**
17
16
  * Exports the create, setErrorLevel, and version functions from the Textmode class for UMD compatibility,
18
17
  * so calls like `textmode.create()` can be used.
@@ -1,8 +1,3 @@
1
1
  export { Shader } from './webgl/Shader';
2
2
  export { GLRenderer } from './webgl/Renderer';
3
3
  export { Framebuffer } from './webgl/Framebuffer';
4
- export { Rectangle } from './webgl/geometries/Rectangle';
5
- export { Line } from './webgl/geometries/Line';
6
- export { Shape } from './webgl/shapes/Shape';
7
- export { RectangleShape } from './webgl/shapes/RectangleShape';
8
- export { LineShape } from './webgl/shapes/LineShape';
@@ -21,16 +21,11 @@ export declare class Framebuffer {
21
21
  private options;
22
22
  private previousState;
23
23
  private _pixels;
24
- private _isFromCanvasSource;
25
24
  constructor(gl: WebGLRenderingContext, width: number, height?: number, options?: FramebufferOptions);
26
- /** Cache for MAX_TEXTURE_IMAGE_UNITS per-context */
27
- private static maxTextureUnitsMap;
28
- private getMaxTextureUnits;
29
25
  /**
30
- * Unbind the provided texture from any texture unit where it is currently bound.
31
- * This prevents framebuffer-texture feedback loops when rendering to this framebuffer.
26
+ * Execute a function with this framebuffer bound, then restore previous binding
32
27
  */
33
- private unbindTextureAcrossUnits;
28
+ private withFramebufferBound;
34
29
  private createTexture;
35
30
  private updateTextureSize;
36
31
  private attachTexture;
@@ -86,6 +81,4 @@ export declare class Framebuffer {
86
81
  get width(): number;
87
82
  get height(): number;
88
83
  get pixels(): Uint8Array | null;
89
- /** True if the last content update came from a canvas/video or raw pixels (top-left origin). False if rendered via FBO (bottom-left). */
90
- get isFromCanvasSource(): boolean;
91
84
  }
@@ -8,7 +8,8 @@ export declare class GLRenderer {
8
8
  private imageShader;
9
9
  private solidColorShader;
10
10
  private currentShader;
11
- private attribCache;
11
+ private rectangleGeometry;
12
+ private lineGeometry;
12
13
  private currentFillColor;
13
14
  private fillMode;
14
15
  private currentStrokeColor;
@@ -121,15 +122,4 @@ export declare class GLRenderer {
121
122
  * Render a framebuffer at a specific position with optional scaling
122
123
  */
123
124
  image(source: Framebuffer, posX: number, posY: number, width?: number, height?: number): void;
124
- private unitBuffer;
125
- private bytesPerVertex;
126
- private scratchFramebuffer;
127
- private ensureUnitBuffer;
128
- private enableAttribs;
129
- private disableAttribs;
130
- private uploadQuadNDC;
131
- private toNDC;
132
- private drawRectImmediate;
133
- private drawRectStrokeImmediate;
134
- private drawLineImmediate;
135
125
  }