@utsp/render 0.14.0-nightly.20251219144833.1c27cfe → 0.14.0

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.
package/dist/2d/index.cjs CHANGED
@@ -1,11 +1,11 @@
1
- "use strict";var W=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var N=(d,t,e)=>t in d?W(d,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):d[t]=e;var f=(d,t)=>W(d,"name",{value:t,configurable:!0});var _=(d,t)=>{for(var e in t)W(d,e,{get:t[e],enumerable:!0})},j=(d,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of Y(t))!P.call(d,s)&&s!==e&&W(d,s,{get:()=>t[s],enumerable:!(i=X(t,s))||i.enumerable});return d};var q=d=>j(W({},"__esModule",{value:!0}),d);var o=(d,t,e)=>(N(d,typeof t!="symbol"?t+"":t,e),e);var Z={};_(Z,{BitmapFontAtlas:()=>H,DEFAULT_PALETTE:()=>I,GridOverlay:()=>w,ImageFontAtlas:()=>M,ScalingMode:()=>O.ScalingMode,Terminal2D:()=>A,colorToPaletteIndex:()=>L,getAtlasColumns:()=>D,getCharGridPosition:()=>R,getMaxCharCode:()=>E,paletteIndexToColor:()=>z});module.exports=q(Z);var T=class T{constructor(t,e,i,s,a){o(this,"atlases");o(this,"charMap");o(this,"baseCharWidth");o(this,"baseCharHeight");o(this,"baseCellWidth");o(this,"baseCellHeight");o(this,"atlasColumns",16);o(this,"font");o(this,"SCALES",[1,2,4,8]);o(this,"colorCache",new Map);o(this,"MAX_CACHE_SIZE",512);this.font=t,this.baseCharWidth=e,this.baseCharHeight=i,this.baseCellWidth=s??e,this.baseCellHeight=a??i,this.charMap=new Map,this.atlases=new Map,this.generateAtlases()}generateAtlases(){this.atlasColumns=16;let t=0;for(let[e,i]of this.font){let s=t%this.atlasColumns,a=Math.floor(t/this.atlasColumns);this.charMap.set(e,{x:s*this.baseCharWidth,y:a*this.baseCharHeight});for(let r of this.SCALES){let n=this.getOrCreateAtlas(r),h=s*n.charWidth,l=a*n.charHeight;this.renderBitmapToAtlas(n,i,h,l)}t++}}getOrCreateAtlas(t){let e=this.atlases.get(t);if(!e){let i=this.baseCharWidth*t,s=this.baseCharHeight*t,r=Math.ceil(256/this.atlasColumns),n=document.createElement("canvas");n.width=this.atlasColumns*i,n.height=r*s;let h=n.getContext("2d",{alpha:!0,willReadFrequently:!1});if(!h)throw new Error(`Impossible de cr\xE9er le contexte 2D pour l'atlas ${t}x`);h.imageSmoothingEnabled=!1,e={canvas:n,ctx:h,scale:t,charWidth:i,charHeight:s},this.atlases.set(t,e)}return e}renderBitmapToAtlas(t,e,i,s){let a=t.scale,r=t.ctx;r.fillStyle="#ffffff";for(let n=0;n<Math.min(e.length,this.baseCharHeight);n++){let h=e[n];for(let l=0;l<this.baseCharWidth;l++){let c=1<<7-l;h&c&&r.fillRect(i+l*a,s+n*a,a,a)}}}drawChar(t,e,i,s,a,r,n){let h=this.charMap.get(e);if(!h)return;let l=`${e}:${n}`,c=this.colorCache.get(l);if(c)c.lastUsed=performance.now();else{let v=this.createColoredGlyph(e,n,h);if(!v)return;c={canvas:v,lastUsed:performance.now()},this.colorCache.set(l,c),this.colorCache.size>this.MAX_CACHE_SIZE&&this.evictLRU()}let g=a/this.baseCellWidth,m=r/this.baseCellHeight,u=(this.baseCellWidth-this.baseCharWidth)/2*g,C=(this.baseCellHeight-this.baseCharHeight)/2*m,b=this.baseCharWidth*g,p=this.baseCharHeight*m;t.drawImage(c.canvas,i+u,s+C,b,p)}createColoredGlyph(t,e,i){let s=this.atlases.get(1);if(!s)return null;let a=s.ctx.getImageData(i.x,i.y,this.baseCharWidth,this.baseCharHeight),r=a.data,n=this.hexToRgb(e);for(let c=0;c<r.length;c+=4)r[c+3]>0&&(r[c]=n.r,r[c+1]=n.g,r[c+2]=n.b);let h=document.createElement("canvas");h.width=this.baseCharWidth,h.height=this.baseCharHeight;let l=h.getContext("2d",{alpha:!0});return l?(l.imageSmoothingEnabled=!1,l.putImageData(a,0,0),h):null}hexToRgb(t){if(t.startsWith("#")){let a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(a,(n,h,l,c)=>h+h+l+l+c+c);let r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return r?{r:parseInt(r[1],16),g:parseInt(r[2],16),b:parseInt(r[3],16)}:{r:255,g:255,b:255}}let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)/.exec(t);if(e)return{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)};let s={white:[255,255,255],black:[0,0,0],red:[255,0,0],green:[0,255,0],blue:[0,0,255],yellow:[255,255,0],cyan:[0,255,255],magenta:[255,0,255]}[t.toLowerCase()];return s?{r:s[0],g:s[1],b:s[2]}:{r:255,g:255,b:255}}evictLRU(){let t=null,e=1/0;for(let[i,s]of this.colorCache)s.lastUsed<e&&(e=s.lastUsed,t=i);if(t){let i=this.colorCache.get(t);i&&(i.canvas.width=0,i.canvas.height=0),this.colorCache.delete(t)}}getAtlasCanvas(t=1){return this.atlases.get(t)?.canvas}getAllAtlases(){return this.atlases}getCharDimensions(){return{width:this.baseCharWidth,height:this.baseCharHeight}}getCharCount(){return this.charMap.size}hasChar(t){return this.charMap.has(t)}getAtlasDimensions(t=1){let e=this.atlases.get(t);if(e)return{width:e.canvas.width,height:e.canvas.height}}toDataURL(t=1,e="image/png"){return this.atlases.get(t)?.canvas.toDataURL(e)}getCacheStats(){return{size:this.colorCache.size,maxSize:this.MAX_CACHE_SIZE}}clearCache(){for(let t of this.colorCache.values())t.canvas.width=0,t.canvas.height=0;this.colorCache.clear()}destroy(){this.charMap.clear();for(let t of this.atlases.values())t.canvas.width=0,t.canvas.height=0;this.atlases.clear(),this.clearCache()}};f(T,"BitmapFontAtlas");var H=T;function D(d){return Math.sqrt(d)*16}f(D,"getAtlasColumns");function E(d){return d*256-1}f(E,"getMaxCharCode");function R(d,t){let e=Math.floor(d/256),i=d%256,s=Math.sqrt(t),a=e%s,r=Math.floor(e/s),n=i%16,h=Math.floor(i/16);return{col:a*16+n,row:r*16+h}}f(R,"getCharGridPosition");var B=class B{constructor(t){o(this,"atlasCanvas",null);o(this,"atlasCtx",null);o(this,"atlasImage",null);o(this,"glyphWidth");o(this,"glyphHeight");o(this,"cellWidth");o(this,"cellHeight");o(this,"atlasBlocks");o(this,"atlasColumns");o(this,"maxCharCode");o(this,"isLoaded",!1);o(this,"colorCache",new Map);o(this,"MAX_CACHE_SIZE",1024);this.glyphWidth=t.glyphWidth,this.glyphHeight=t.glyphHeight,this.cellWidth=t.cellWidth??t.glyphWidth,this.cellHeight=t.cellHeight??t.glyphHeight,this.atlasBlocks=t.atlasBlocks,this.atlasColumns=D(t.atlasBlocks),this.maxCharCode=E(t.atlasBlocks)}async loadFromPNG(t){return new Promise((e,i)=>{let s=new Uint8Array(t),a=new Blob([s],{type:"image/png"}),r=URL.createObjectURL(a),n=new Image;n.onload=()=>{URL.revokeObjectURL(r);let h=this.atlasColumns*this.glyphWidth,l=this.atlasColumns*this.glyphHeight;if((n.width!==h||n.height!==l)&&console.warn(`ImageFontAtlas: Image size ${n.width}\xD7${n.height} doesn't match expected ${h}\xD7${l} for ${this.atlasBlocks} block(s) with ${this.glyphWidth}\xD7${this.glyphHeight} glyphs`),this.atlasCanvas=document.createElement("canvas"),this.atlasCanvas.width=n.width,this.atlasCanvas.height=n.height,this.atlasCtx=this.atlasCanvas.getContext("2d",{alpha:!0,willReadFrequently:!0}),!this.atlasCtx){i(new Error("Failed to create 2D context for atlas"));return}this.atlasCtx.imageSmoothingEnabled=!1,this.atlasCtx.drawImage(n,0,0),this.atlasImage=n,this.isLoaded=!0,e()},n.onerror=()=>{URL.revokeObjectURL(r),i(new Error("Failed to load PNG image for atlas"))},n.src=r})}isReady(){return this.isLoaded}getGlyphPosition(t){if(t<0||t>this.maxCharCode)return null;let{col:e,row:i}=R(t,this.atlasBlocks);return{x:e*this.glyphWidth,y:i*this.glyphHeight}}getCharUV(t){if(t<0||t>this.maxCharCode)return null;let{col:e,row:i}=R(t,this.atlasBlocks),s=e/this.atlasColumns,a=i/this.atlasColumns,r=(e+1)/this.atlasColumns,n=(i+1)/this.atlasColumns;return{u1:s,v1:a,u2:r,v2:n}}drawChar(t,e,i,s,a,r,n){if(!this.isLoaded||!this.atlasCanvas)return;let h=this.getGlyphPosition(e);if(!h)return;let l=`${e}:${n}`,c=this.colorCache.get(l);if(c)c.lastUsed=performance.now();else{let m=this.createColoredGlyph(e,n,h);if(!m)return;c={canvas:m,lastUsed:performance.now()},this.colorCache.set(l,c),this.colorCache.size>this.MAX_CACHE_SIZE&&this.evictLRU()}if(t.imageSmoothingEnabled=!1,a===this.cellWidth&&r===this.cellHeight){let m=Math.floor((this.cellWidth-this.glyphWidth)/2),u=Math.floor((this.cellHeight-this.glyphHeight)/2);t.drawImage(c.canvas,Math.floor(i)+m,Math.floor(s)+u)}else{let m=a/this.cellWidth,u=r/this.cellHeight,C=Math.floor((this.cellWidth-this.glyphWidth)/2*m),b=Math.floor((this.cellHeight-this.glyphHeight)/2*u),p=Math.ceil(this.glyphWidth*m),v=Math.ceil(this.glyphHeight*u);t.drawImage(c.canvas,Math.floor(i)+C,Math.floor(s)+b,p,v)}}createColoredGlyph(t,e,i){if(!this.atlasCtx)return null;let s=this.atlasCtx.getImageData(i.x,i.y,this.glyphWidth,this.glyphHeight),a=s.data,r=this.parseColor(e);for(let l=0;l<a.length;l+=4)a[l+3]>0&&(a[l]=r.r,a[l+1]=r.g,a[l+2]=r.b);let n=document.createElement("canvas");n.width=this.glyphWidth,n.height=this.glyphHeight;let h=n.getContext("2d",{alpha:!0});return h?(h.putImageData(s,0,0),n):null}parseColor(t){if(t.startsWith("#")){let i=t.slice(1);return i.length===3&&(i=i[0]+i[0]+i[1]+i[1]+i[2]+i[2]),{r:parseInt(i.slice(0,2),16),g:parseInt(i.slice(2,4),16),b:parseInt(i.slice(4,6),16)}}let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(t);return e?{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)}:{r:255,g:255,b:255}}evictLRU(){let t=null,e=1/0;for(let[i,s]of this.colorCache)s.lastUsed<e&&(e=s.lastUsed,t=i);if(t){let i=this.colorCache.get(t);i&&(i.canvas.width=0,i.canvas.height=0),this.colorCache.delete(t)}}getAtlasCanvas(){return this.atlasCanvas}getAtlasImage(){return this.atlasImage}getConfig(){return{glyphWidth:this.glyphWidth,glyphHeight:this.glyphHeight,cellWidth:this.cellWidth,cellHeight:this.cellHeight,atlasBlocks:this.atlasBlocks}}getGlyphDimensions(){return{width:this.glyphWidth,height:this.glyphHeight}}getCellDimensions(){return{width:this.cellWidth,height:this.cellHeight}}getAtlasDimensions(){return{width:this.atlasColumns*this.glyphWidth,height:this.atlasColumns*this.glyphHeight}}getAtlasColumns(){return this.atlasColumns}getMaxCharCode(){return this.maxCharCode}isValidCharCode(t){return t>=0&&t<=this.maxCharCode}clearCache(){for(let t of this.colorCache.values())t.canvas.width=0,t.canvas.height=0;this.colorCache.clear()}getCacheStats(){return{size:this.colorCache.size,maxSize:this.MAX_CACHE_SIZE}}destroy(){this.clearCache(),this.atlasCanvas&&(this.atlasCanvas.width=0,this.atlasCanvas.height=0,this.atlasCanvas=null),this.atlasCtx=null,this.atlasImage=null,this.isLoaded=!1}};f(B,"ImageFontAtlas");var M=B;var S=class S{constructor(t,e){o(this,"canvas");o(this,"ctx");o(this,"container");o(this,"cols",0);o(this,"rows",0);o(this,"cellWidth",0);o(this,"cellHeight",0);o(this,"offsetX",0);o(this,"offsetY",0);o(this,"strokeColor","rgba(80, 80, 80, 0.4)");o(this,"lineWidth",1);this.container=t,this.canvas=document.createElement("canvas"),this.canvas.className="grid-overlay-canvas";let i=e?.zIndex??10;this.canvas.style.cssText=`
1
+ "use strict";var W=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var N=(m,t,e)=>t in m?W(m,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):m[t]=e;var f=(m,t)=>W(m,"name",{value:t,configurable:!0});var _=(m,t)=>{for(var e in t)W(m,e,{get:t[e],enumerable:!0})},j=(m,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Y(t))!P.call(m,i)&&i!==e&&W(m,i,{get:()=>t[i],enumerable:!(s=X(t,i))||s.enumerable});return m};var q=m=>j(W({},"__esModule",{value:!0}),m);var h=(m,t,e)=>(N(m,typeof t!="symbol"?t+"":t,e),e);var Z={};_(Z,{BitmapFontAtlas:()=>M,DEFAULT_PALETTE:()=>S,GridOverlay:()=>x,ImageFontAtlas:()=>H,ScalingMode:()=>O.ScalingMode,Terminal2D:()=>A,colorToPaletteIndex:()=>L,getAtlasColumns:()=>E,getCharGridPosition:()=>R,getMaxCharCode:()=>D,paletteIndexToColor:()=>z});module.exports=q(Z);var I=class I{constructor(t,e,s,i,a){h(this,"atlases");h(this,"charMap");h(this,"baseCharWidth");h(this,"baseCharHeight");h(this,"baseCellWidth");h(this,"baseCellHeight");h(this,"atlasColumns",16);h(this,"font");h(this,"SCALES",[1,2,4,8]);h(this,"colorCache",new Map);h(this,"MAX_CACHE_SIZE",512);this.font=t,this.baseCharWidth=e,this.baseCharHeight=s,this.baseCellWidth=i??e,this.baseCellHeight=a??s,this.charMap=new Map,this.atlases=new Map,this.generateAtlases()}generateAtlases(){this.atlasColumns=16;let t=0;for(let[e,s]of this.font){let i=t%this.atlasColumns,a=Math.floor(t/this.atlasColumns);this.charMap.set(e,{x:i*this.baseCharWidth,y:a*this.baseCharHeight});for(let n of this.SCALES){let r=this.getOrCreateAtlas(n),l=i*r.charWidth,o=a*r.charHeight;this.renderBitmapToAtlas(r,s,l,o)}t++}}getOrCreateAtlas(t){let e=this.atlases.get(t);if(!e){let s=this.baseCharWidth*t,i=this.baseCharHeight*t,n=Math.ceil(256/this.atlasColumns),r=document.createElement("canvas");r.width=this.atlasColumns*s,r.height=n*i;let l=r.getContext("2d",{alpha:!0,willReadFrequently:!1});if(!l)throw new Error(`Impossible de cr\xE9er le contexte 2D pour l'atlas ${t}x`);l.imageSmoothingEnabled=!1,e={canvas:r,ctx:l,scale:t,charWidth:s,charHeight:i},this.atlases.set(t,e)}return e}renderBitmapToAtlas(t,e,s,i){let a=t.scale,n=t.ctx;n.fillStyle="#ffffff";for(let r=0;r<Math.min(e.length,this.baseCharHeight);r++){let l=e[r];for(let o=0;o<this.baseCharWidth;o++){let c=1<<7-o;l&c&&n.fillRect(s+o*a,i+r*a,a,a)}}}drawChar(t,e,s,i,a,n,r){let l=this.charMap.get(e);if(!l)return;let o=`${e}:${r}`,c=this.colorCache.get(o);if(c)c.lastUsed=performance.now();else{let w=this.createColoredGlyph(e,r,l);if(!w)return;c={canvas:w,lastUsed:performance.now()},this.colorCache.set(o,c),this.colorCache.size>this.MAX_CACHE_SIZE&&this.evictLRU()}let d=a/this.baseCellWidth,g=n/this.baseCellHeight,u=(this.baseCellWidth-this.baseCharWidth)/2*d,v=(this.baseCellHeight-this.baseCharHeight)/2*g,b=this.baseCharWidth*d,p=this.baseCharHeight*g;t.drawImage(c.canvas,s+u,i+v,b,p)}createColoredGlyph(t,e,s){let i=this.atlases.get(1);if(!i)return null;let a=i.ctx.getImageData(s.x,s.y,this.baseCharWidth,this.baseCharHeight),n=a.data,r=this.hexToRgb(e);for(let c=0;c<n.length;c+=4)n[c+3]>0&&(n[c]=r.r,n[c+1]=r.g,n[c+2]=r.b);let l=document.createElement("canvas");l.width=this.baseCharWidth,l.height=this.baseCharHeight;let o=l.getContext("2d",{alpha:!0});return o?(o.imageSmoothingEnabled=!1,o.putImageData(a,0,0),l):null}hexToRgb(t){if(t.startsWith("#")){let a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(a,(r,l,o,c)=>l+l+o+o+c+c);let n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return n?{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16)}:{r:255,g:255,b:255}}let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)/.exec(t);if(e)return{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)};let i={white:[255,255,255],black:[0,0,0],red:[255,0,0],green:[0,255,0],blue:[0,0,255],yellow:[255,255,0],cyan:[0,255,255],magenta:[255,0,255]}[t.toLowerCase()];return i?{r:i[0],g:i[1],b:i[2]}:{r:255,g:255,b:255}}evictLRU(){let t=null,e=1/0;for(let[s,i]of this.colorCache)i.lastUsed<e&&(e=i.lastUsed,t=s);if(t){let s=this.colorCache.get(t);s&&(s.canvas.width=0,s.canvas.height=0),this.colorCache.delete(t)}}getAtlasCanvas(t=1){return this.atlases.get(t)?.canvas}getAllAtlases(){return this.atlases}getCharDimensions(){return{width:this.baseCharWidth,height:this.baseCharHeight}}getCharCount(){return this.charMap.size}hasChar(t){return this.charMap.has(t)}getAtlasDimensions(t=1){let e=this.atlases.get(t);if(e)return{width:e.canvas.width,height:e.canvas.height}}toDataURL(t=1,e="image/png"){return this.atlases.get(t)?.canvas.toDataURL(e)}getCacheStats(){return{size:this.colorCache.size,maxSize:this.MAX_CACHE_SIZE}}clearCache(){for(let t of this.colorCache.values())t.canvas.width=0,t.canvas.height=0;this.colorCache.clear()}destroy(){this.charMap.clear();for(let t of this.atlases.values())t.canvas.width=0,t.canvas.height=0;this.atlases.clear(),this.clearCache()}};f(I,"BitmapFontAtlas");var M=I;function E(m){return Math.sqrt(m)*16}f(E,"getAtlasColumns");function D(m){return m*256-1}f(D,"getMaxCharCode");function R(m,t){let e=Math.floor(m/256),s=m%256,i=Math.sqrt(t),a=e%i,n=Math.floor(e/i),r=s%16,l=Math.floor(s/16);return{col:a*16+r,row:n*16+l}}f(R,"getCharGridPosition");var T=class T{constructor(t){h(this,"atlasCanvas",null);h(this,"atlasCtx",null);h(this,"atlasImage",null);h(this,"glyphWidth");h(this,"glyphHeight");h(this,"cellWidth");h(this,"cellHeight");h(this,"atlasBlocks");h(this,"atlasColumns");h(this,"maxCharCode");h(this,"isLoaded",!1);h(this,"colorCache",new Map);h(this,"MAX_CACHE_SIZE",1024);this.glyphWidth=t.glyphWidth,this.glyphHeight=t.glyphHeight,this.cellWidth=t.cellWidth??t.glyphWidth,this.cellHeight=t.cellHeight??t.glyphHeight,this.atlasBlocks=t.atlasBlocks,this.atlasColumns=E(t.atlasBlocks),this.maxCharCode=D(t.atlasBlocks)}async loadFromPNG(t){return new Promise((e,s)=>{let i=new Uint8Array(t),a=new Blob([i],{type:"image/png"}),n=URL.createObjectURL(a),r=new Image;r.onload=()=>{URL.revokeObjectURL(n);let l=this.atlasColumns*this.glyphWidth,o=this.atlasColumns*this.glyphHeight;if((r.width!==l||r.height!==o)&&console.warn(`ImageFontAtlas: Image size ${r.width}\xD7${r.height} doesn't match expected ${l}\xD7${o} for ${this.atlasBlocks} block(s) with ${this.glyphWidth}\xD7${this.glyphHeight} glyphs`),this.atlasCanvas=document.createElement("canvas"),this.atlasCanvas.width=r.width,this.atlasCanvas.height=r.height,this.atlasCtx=this.atlasCanvas.getContext("2d",{alpha:!0,willReadFrequently:!0}),!this.atlasCtx){s(new Error("Failed to create 2D context for atlas"));return}this.atlasCtx.imageSmoothingEnabled=!1,this.atlasCtx.drawImage(r,0,0),this.atlasImage=r,this.isLoaded=!0,e()},r.onerror=()=>{URL.revokeObjectURL(n),s(new Error("Failed to load PNG image for atlas"))},r.src=n})}isReady(){return this.isLoaded}getGlyphPosition(t){if(t<0||t>this.maxCharCode)return null;let{col:e,row:s}=R(t,this.atlasBlocks);return{x:e*this.glyphWidth,y:s*this.glyphHeight}}getCharUV(t){if(t<0||t>this.maxCharCode)return null;let{col:e,row:s}=R(t,this.atlasBlocks),i=e/this.atlasColumns,a=s/this.atlasColumns,n=(e+1)/this.atlasColumns,r=(s+1)/this.atlasColumns;return{u1:i,v1:a,u2:n,v2:r}}drawChar(t,e,s,i,a,n,r){if(!this.isLoaded||!this.atlasCanvas)return;let l=this.getGlyphPosition(e);if(!l)return;let o=`${e}:${r}`,c=this.colorCache.get(o);if(c)c.lastUsed=performance.now();else{let g=this.createColoredGlyph(e,r,l);if(!g)return;c={canvas:g,lastUsed:performance.now()},this.colorCache.set(o,c),this.colorCache.size>this.MAX_CACHE_SIZE&&this.evictLRU()}if(t.imageSmoothingEnabled=!1,a===this.cellWidth&&n===this.cellHeight){let g=Math.floor((this.cellWidth-this.glyphWidth)/2),u=Math.floor((this.cellHeight-this.glyphHeight)/2);t.drawImage(c.canvas,Math.floor(s)+g,Math.floor(i)+u)}else{let g=a/this.cellWidth,u=n/this.cellHeight,v=Math.floor((this.cellWidth-this.glyphWidth)/2*g),b=Math.floor((this.cellHeight-this.glyphHeight)/2*u),p=Math.ceil(this.glyphWidth*g),w=Math.ceil(this.glyphHeight*u);t.drawImage(c.canvas,Math.floor(s)+v,Math.floor(i)+b,p,w)}}createColoredGlyph(t,e,s){if(!this.atlasCtx)return null;let i=this.atlasCtx.getImageData(s.x,s.y,this.glyphWidth,this.glyphHeight),a=i.data,n=this.parseColor(e);for(let o=0;o<a.length;o+=4)a[o+3]>0&&(a[o]=n.r,a[o+1]=n.g,a[o+2]=n.b);let r=document.createElement("canvas");r.width=this.glyphWidth,r.height=this.glyphHeight;let l=r.getContext("2d",{alpha:!0});return l?(l.putImageData(i,0,0),r):null}parseColor(t){if(t.startsWith("#")){let s=t.slice(1);return s.length===3&&(s=s[0]+s[0]+s[1]+s[1]+s[2]+s[2]),{r:parseInt(s.slice(0,2),16),g:parseInt(s.slice(2,4),16),b:parseInt(s.slice(4,6),16)}}let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(t);return e?{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)}:{r:255,g:255,b:255}}evictLRU(){let t=null,e=1/0;for(let[s,i]of this.colorCache)i.lastUsed<e&&(e=i.lastUsed,t=s);if(t){let s=this.colorCache.get(t);s&&(s.canvas.width=0,s.canvas.height=0),this.colorCache.delete(t)}}getAtlasCanvas(){return this.atlasCanvas}getAtlasImage(){return this.atlasImage}getConfig(){return{glyphWidth:this.glyphWidth,glyphHeight:this.glyphHeight,cellWidth:this.cellWidth,cellHeight:this.cellHeight,atlasBlocks:this.atlasBlocks}}getGlyphDimensions(){return{width:this.glyphWidth,height:this.glyphHeight}}getCellDimensions(){return{width:this.cellWidth,height:this.cellHeight}}getAtlasDimensions(){return{width:this.atlasColumns*this.glyphWidth,height:this.atlasColumns*this.glyphHeight}}getAtlasColumns(){return this.atlasColumns}getMaxCharCode(){return this.maxCharCode}isValidCharCode(t){return t>=0&&t<=this.maxCharCode}clearCache(){for(let t of this.colorCache.values())t.canvas.width=0,t.canvas.height=0;this.colorCache.clear()}getCacheStats(){return{size:this.colorCache.size,maxSize:this.MAX_CACHE_SIZE}}destroy(){this.clearCache(),this.atlasCanvas&&(this.atlasCanvas.width=0,this.atlasCanvas.height=0,this.atlasCanvas=null),this.atlasCtx=null,this.atlasImage=null,this.isLoaded=!1}};f(T,"ImageFontAtlas");var H=T;var B=class B{constructor(t,e){h(this,"canvas");h(this,"ctx");h(this,"container");h(this,"cols",0);h(this,"rows",0);h(this,"cellWidth",0);h(this,"cellHeight",0);h(this,"offsetX",0);h(this,"offsetY",0);h(this,"strokeColor","rgba(80, 80, 80, 0.4)");h(this,"lineWidth",1);this.container=t,this.canvas=document.createElement("canvas"),this.canvas.className="grid-overlay-canvas";let s=e?.zIndex??10;this.canvas.style.cssText=`
2
2
  display: block !important;
3
3
  position: absolute !important;
4
4
  pointer-events: none !important;
5
5
  image-rendering: pixelated !important;
6
6
  image-rendering: crisp-edges !important;
7
- z-index: ${i} !important;
8
- `,this.container.appendChild(this.canvas);let s=this.canvas.getContext("2d");if(!s)throw new Error("[GridOverlay] Impossible de cr\xE9er le contexte 2D");this.ctx=s,e&&(e.strokeColor&&(this.strokeColor=e.strokeColor),e.lineWidth!==void 0&&(this.lineWidth=e.lineWidth))}setDimensions(t,e,i,s,a=0,r=0){this.cols=t,this.rows=e,this.cellWidth=i,this.cellHeight=s,this.offsetX=a,this.offsetY=r}setCanvasSize(t,e){this.canvas.width=t,this.canvas.height=e,this.ctx.setTransform(1,0,0,1,0,0)}setStyle(t){t.strokeColor&&(this.strokeColor=t.strokeColor),t.lineWidth!==void 0&&(this.lineWidth=t.lineWidth)}setTransform(t,e,i,s,a){let r=s.left-a.left,n=s.top-a.top,h=s.width,l=s.height;(this.canvas.width!==h||this.canvas.height!==l)&&(this.canvas.width=h,this.canvas.height=l);let c=this.canvas.style;c.width=`${h}px`,c.height=`${l}px`,c.left=`${r}px`,c.top=`${n}px`}render(){if(!this.ctx)return;let t=this.cols*this.cellWidth,e=this.rows*this.cellHeight,i=t>0?this.canvas.width/t:1,s=e>0?this.canvas.height/e:1,a=Math.min(i,s),r=this.cellWidth*a,n=this.cellHeight*a,h=this.cols*r,l=this.rows*n,c=this.offsetX*a,g=this.offsetY*a;if(!(h===0||l===0)){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.strokeStyle=this.strokeColor,this.ctx.lineWidth=Math.max(1,this.lineWidth*a),this.ctx.beginPath();for(let m=0;m<=this.cols;m++){let u=c+m*r+.5;this.ctx.moveTo(u,g),this.ctx.lineTo(u,g+l)}for(let m=0;m<=this.rows;m++){let u=g+m*n+.5;this.ctx.moveTo(c,u),this.ctx.lineTo(c+h,u)}this.ctx.stroke()}}update(t,e,i,s,a,r,n=0,h=0){this.setDimensions(t,e,i,s,n,h),this.setCanvasSize(a,r),this.render()}setVisible(t){this.canvas.style.display=t?"block":"none"}destroy(){this.canvas.parentElement&&this.canvas.parentElement.removeChild(this.canvas)}getCanvas(){return this.canvas}};f(S,"GridOverlay");var w=S;var x=require("@utsp/types");var G=class G{constructor(t,e={}){o(this,"containerDiv");o(this,"canvas");o(this,"ctx");o(this,"parentElement");o(this,"cells");o(this,"cols",0);o(this,"rows",0);o(this,"fontSize");o(this,"fontFamily");o(this,"defaultFgColor");o(this,"defaultBgColor");o(this,"canvasBgColor");o(this,"cellWidth");o(this,"cellHeight");o(this,"offsetX",0);o(this,"offsetY",0);o(this,"fontType","web");o(this,"bitmapFont");o(this,"bitmapAtlas");o(this,"imageAtlas");o(this,"bitmapCharWidth",8);o(this,"bitmapCharHeight",8);o(this,"showDebugGrid");o(this,"debugGridColor");o(this,"gridOverlay");o(this,"fixedGridMode");o(this,"fixedCols");o(this,"fixedRows");o(this,"cellAspectRatio");o(this,"resizeObserver");o(this,"imageDataBuffer");o(this,"useImageDataRendering",!1);o(this,"paletteCache");o(this,"scalingMode",x.ScalingMode.None);o(this,"currentScale",1);if(!t)throw new Error("Render: L'\xE9l\xE9ment parent est requis");this.parentElement=t,this.fixedGridMode=!!(e.fixedCols&&e.fixedRows),this.fixedCols=e.fixedCols,this.fixedRows=e.fixedRows,this.cellAspectRatio=e.cellAspectRatio??10/14,this.cellWidth=e.cellWidth??10,this.cellHeight=e.cellHeight??14,this.fontSize=e.fontSize??12,this.fontFamily=e.fontFamily??"monospace",this.defaultFgColor=e.defaultFgColor??"#ffffff",this.defaultBgColor=e.defaultBgColor??"#000000",this.canvasBgColor=e.canvasBgColor!==void 0?e.canvasBgColor:null,this.showDebugGrid=e.showDebugGrid??!1,this.debugGridColor=e.debugGridColor??"rgba(144, 24, 24, 1)",this.scalingMode=e.scalingMode??x.ScalingMode.None,window.getComputedStyle(this.parentElement).position==="static"&&(this.parentElement.style.position="relative"),this.containerDiv=document.createElement("div"),this.containerDiv.className="terminal2d-container",this.containerDiv.style.cssText=`
7
+ z-index: ${s} !important;
8
+ `,this.container.appendChild(this.canvas);let i=this.canvas.getContext("2d");if(!i)throw new Error("[GridOverlay] Impossible de cr\xE9er le contexte 2D");this.ctx=i,e&&(e.strokeColor&&(this.strokeColor=e.strokeColor),e.lineWidth!==void 0&&(this.lineWidth=e.lineWidth))}setDimensions(t,e,s,i,a=0,n=0){this.cols=t,this.rows=e,this.cellWidth=s,this.cellHeight=i,this.offsetX=a,this.offsetY=n}setCanvasSize(t,e){this.canvas.width=t,this.canvas.height=e,this.ctx.setTransform(1,0,0,1,0,0)}setStyle(t){t.strokeColor&&(this.strokeColor=t.strokeColor),t.lineWidth!==void 0&&(this.lineWidth=t.lineWidth)}setTransform(t,e,s,i,a){let n=i.left-a.left,r=i.top-a.top,l=i.width,o=i.height;(this.canvas.width!==l||this.canvas.height!==o)&&(this.canvas.width=l,this.canvas.height=o);let c=this.canvas.style;c.width=`${l}px`,c.height=`${o}px`,c.left=`${n}px`,c.top=`${r}px`}render(){if(!this.ctx)return;let t=this.cols*this.cellWidth,e=this.rows*this.cellHeight,s=t>0?this.canvas.width/t:1,i=e>0?this.canvas.height/e:1,a=Math.min(s,i),n=this.cellWidth*a,r=this.cellHeight*a,l=this.cols*n,o=this.rows*r,c=this.offsetX*a,d=this.offsetY*a;if(!(l===0||o===0)){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.strokeStyle=this.strokeColor,this.ctx.lineWidth=Math.max(1,this.lineWidth*a),this.ctx.beginPath();for(let g=0;g<=this.cols;g++){let u=c+g*n+.5;this.ctx.moveTo(u,d),this.ctx.lineTo(u,d+o)}for(let g=0;g<=this.rows;g++){let u=d+g*r+.5;this.ctx.moveTo(c,u),this.ctx.lineTo(c+l,u)}this.ctx.stroke()}}update(t,e,s,i,a,n,r=0,l=0){this.setDimensions(t,e,s,i,r,l),this.setCanvasSize(a,n),this.render()}setVisible(t){this.canvas.style.display=t?"block":"none"}destroy(){this.canvas.parentElement&&this.canvas.parentElement.removeChild(this.canvas)}getCanvas(){return this.canvas}};f(B,"GridOverlay");var x=B;var C=require("@utsp/types");var G=class G{constructor(t,e={}){h(this,"containerDiv");h(this,"canvas");h(this,"ctx");h(this,"parentElement");h(this,"cells");h(this,"cols",0);h(this,"rows",0);h(this,"fontSize");h(this,"fontFamily");h(this,"defaultFgColor");h(this,"defaultBgColor");h(this,"canvasBgColor");h(this,"cellWidth");h(this,"cellHeight");h(this,"offsetX",0);h(this,"offsetY",0);h(this,"fontType","web");h(this,"bitmapFont");h(this,"bitmapAtlas");h(this,"imageAtlas");h(this,"bitmapCharWidth",8);h(this,"bitmapCharHeight",8);h(this,"showDebugGrid");h(this,"debugGridColor");h(this,"gridOverlay");h(this,"fixedGridMode");h(this,"fixedCols");h(this,"fixedRows");h(this,"cellAspectRatio");h(this,"resizeObserver");h(this,"imageDataBuffer");h(this,"useImageDataRendering",!1);h(this,"paletteCache");h(this,"scalingMode",C.ScalingMode.None);h(this,"currentScale",1);h(this,"customCellSize",!1);if(!t)throw new Error("Render: L'\xE9l\xE9ment parent est requis");this.parentElement=t,this.fixedGridMode=!!(e.fixedCols&&e.fixedRows),this.fixedCols=e.fixedCols,this.fixedRows=e.fixedRows,this.cellAspectRatio=e.cellAspectRatio??1,this.cellWidth=e.cellWidth??8,this.cellHeight=e.cellHeight??8,this.fontSize=e.fontSize??12,this.fontFamily=e.fontFamily??"monospace",this.defaultFgColor=e.defaultFgColor??"#ffffff",this.defaultBgColor=e.defaultBgColor??"#000000",this.canvasBgColor=e.canvasBgColor!==void 0?e.canvasBgColor:null,this.showDebugGrid=e.showDebugGrid??!1,this.debugGridColor=e.debugGridColor??"rgba(144, 24, 24, 1)",this.scalingMode=e.scalingMode??C.ScalingMode.None,window.getComputedStyle(this.parentElement).position==="static"&&(this.parentElement.style.position="relative"),this.containerDiv=document.createElement("div"),this.containerDiv.className="terminal2d-container",this.containerDiv.style.cssText=`
9
9
  position: absolute !important;
10
10
  top: 0 !important;
11
11
  left: 0 !important;
@@ -19,16 +19,16 @@
19
19
  justify-content: center !important;
20
20
  align-items: center !important;
21
21
  isolation: isolate !important;
22
- `,this.canvas=document.createElement("canvas"),this.canvas.className="terminal2d-canvas",e.className&&(this.canvas.className+=" "+e.className),e.style&&Object.assign(this.canvas.style,e.style),this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges",this.containerDiv.appendChild(this.canvas),this.parentElement.appendChild(this.containerDiv);let s=this.canvas.getContext("2d",{alpha:!0,desynchronized:!1});if(!s)throw new Error("UTSPRender: Impossible d'obtenir le contexte 2D du canvas");this.ctx=s,this.ctx.imageSmoothingEnabled=!1,this.calculateGridSize(),this.cells=this.createEmptyGrid(),this.showDebugGrid&&(this.gridOverlay=new w(this.parentElement,{strokeColor:this.debugGridColor,lineWidth:1,zIndex:10})),this.enableAutoResize(),this.render()}calculateGridSize(){let t=this.parentElement.clientWidth||800,e=this.parentElement.clientHeight||600,i,s;if(this.fontType==="image"&&this.imageAtlas){let g=this.imageAtlas.getCellDimensions();i=g.width,s=g.height}else this.fontType==="bitmap"?(i=this.bitmapCharWidth,s=this.bitmapCharHeight):(i=Math.round(this.cellWidth)||10,s=Math.round(this.cellHeight)||14);this.cellWidth=Math.round(i),this.cellHeight=Math.round(s),this.fixedGridMode&&this.fixedCols&&this.fixedRows?(this.cols=this.fixedCols,this.rows=this.fixedRows):(this.cols=Math.floor(t/this.cellWidth)||1,this.rows=Math.floor(e/this.cellHeight)||1);let a=this.cols*this.cellWidth,r=this.rows*this.cellHeight,n=t/a,h=e/r,l=Math.min(n,h),c;switch(this.scalingMode){case x.ScalingMode.Integer:c=Math.max(1,Math.floor(l));break;case x.ScalingMode.Half:c=Math.max(.5,Math.floor(l*2)/2);break;case x.ScalingMode.Quarter:c=Math.max(.25,Math.floor(l*4)/4);break;case x.ScalingMode.Eighth:c=Math.max(.125,Math.floor(l*8)/8);break;case x.ScalingMode.None:default:c=Math.max(.1,l);break}this.currentScale=c,this.canvas.width=a,this.canvas.height=r,this.ctx.imageSmoothingEnabled=!1,this.canvas.style.cssText=`
22
+ `,this.canvas=document.createElement("canvas"),this.canvas.className="terminal2d-canvas",e.className&&(this.canvas.className+=" "+e.className),e.style&&Object.assign(this.canvas.style,e.style),this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges",this.containerDiv.appendChild(this.canvas),this.parentElement.appendChild(this.containerDiv);let i=this.canvas.getContext("2d",{alpha:!0,desynchronized:!1});if(!i)throw new Error("UTSPRender: Impossible d'obtenir le contexte 2D du canvas");this.ctx=i,this.ctx.imageSmoothingEnabled=!1,this.calculateGridSize(),this.cells=this.createEmptyGrid(),this.showDebugGrid&&(this.gridOverlay=new x(this.parentElement,{strokeColor:this.debugGridColor,lineWidth:1,zIndex:10})),this.enableAutoResize(),this.render()}calculateGridSize(){let t=this.parentElement.clientWidth||800,e=this.parentElement.clientHeight||600;if(!this.customCellSize){let o,c;if(this.fontType==="image"&&this.imageAtlas){let d=this.imageAtlas.getCellDimensions();o=d.width,c=d.height}else this.fontType==="bitmap"?(o=this.bitmapCharWidth,c=this.bitmapCharHeight):(o=Math.round(this.cellWidth)||10,c=Math.round(this.cellHeight)||14);this.cellWidth=Math.round(o),this.cellHeight=Math.round(c)}this.scalingMode===C.ScalingMode.Responsive?(this.cols=Math.floor(t/this.cellWidth)||1,this.rows=Math.floor(e/this.cellHeight)||1):this.fixedGridMode&&this.fixedCols&&this.fixedRows?(this.cols=this.fixedCols,this.rows=this.fixedRows):(this.cols=Math.floor(t/this.cellWidth)||1,this.rows=Math.floor(e/this.cellHeight)||1);let s=this.cols*this.cellWidth,i=this.rows*this.cellHeight,a=t/s,n=e/i,r=Math.min(a,n),l;switch(this.scalingMode){case C.ScalingMode.Integer:l=Math.max(1,Math.floor(r));break;case C.ScalingMode.Half:l=Math.max(.5,Math.floor(r*2)/2);break;case C.ScalingMode.Quarter:l=Math.max(.25,Math.floor(r*4)/4);break;case C.ScalingMode.Eighth:l=Math.max(.125,Math.floor(r*8)/8);break;case C.ScalingMode.Responsive:l=1;break;case C.ScalingMode.None:default:l=Math.max(.1,r);break}this.currentScale=l,this.canvas.width=s,this.canvas.height=i,this.ctx.imageSmoothingEnabled=!1,this.canvas.style.cssText=`
23
23
  flex-shrink: 0 !important;
24
24
  flex-grow: 0 !important;
25
- width: ${a}px !important;
26
- height: ${r}px !important;
25
+ width: ${s}px !important;
26
+ height: ${i}px !important;
27
27
  image-rendering: pixelated !important;
28
28
  image-rendering: crisp-edges !important;
29
29
  -ms-interpolation-mode: nearest-neighbor !important;
30
30
  transform-origin: center center !important;
31
- transform: scale(${c}) !important;
31
+ transform: scale(${l}) !important;
32
32
  will-change: transform !important;
33
33
  backface-visibility: hidden !important;
34
- `,this.fontType==="web"&&(this.fontSize=Math.round(this.cellHeight/14*12)),this.offsetX=0,this.offsetY=0}createEmptyGrid(){let t=[];for(let e=0;e<this.rows;e++){let i=[];for(let s=0;s<this.cols;s++)i.push({char:" ",fgColor:this.defaultFgColor,bgColor:this.defaultBgColor});t.push(i)}return t}enableAutoResize(){this.resizeObserver=new ResizeObserver(()=>{let t=this.cols,e=this.rows;if(this.calculateGridSize(),this.cols!==t||this.rows!==e){let i=this.cells;this.cells=this.createEmptyGrid();let s=Math.min(t,this.cols),a=Math.min(e,this.rows);for(let r=0;r<a;r++)for(let n=0;n<s;n++)this.cells[r][n]=i[r][n]}this.render()}),this.resizeObserver.observe(this.parentElement)}setCell(t,e,i,s,a){if(e<0||e>=this.rows||t<0||t>=this.cols)return;let r=i&&typeof i=="string"?i.charAt(0):" ";this.cells[e][t]={char:r,fgColor:s??this.defaultFgColor,bgColor:a??this.defaultBgColor}}getCell(t,e){return e<0||e>=this.rows||t<0||t>=this.cols?null:this.cells[e][t]}write(t,e,i,s,a){for(let r=0;r<i.length;r++)this.setCell(t+r,e,i[r],s,a)}fillRect(t,e,i,s,a=" ",r,n){for(let h=e;h<e+s;h++)for(let l=t;l<t+i;l++)this.setCell(l,h,a,r,n)}clear(){this.cells=this.createEmptyGrid()}setFromArray(t){let e=t.width*t.height;if(t.cells.length!==e)throw new Error(`Invalid array length: expected ${e} (${t.width}\xD7${t.height}), got ${t.cells.length}`);let i=0;for(let s=0;s<t.height;s++)for(let a=0;a<t.width;a++){let r=t.cells[i];s<this.rows&&a<this.cols&&this.setCell(a,s,r.char,r.fgColor,r.bgColor),i++}}render(){if(this.fontType==="bitmap"&&this.bitmapFont&&this.useImageDataRendering){this.renderWithImageData();return}this.renderClassic()}renderWithImageData(){if(!this.bitmapFont)return;let t=this.cols*this.bitmapCharWidth,e=this.rows*this.bitmapCharHeight;(!this.imageDataBuffer||this.imageDataBuffer.width!==t||this.imageDataBuffer.height!==e)&&(this.imageDataBuffer=this.ctx.createImageData(t,e));let i=this.imageDataBuffer.data;for(let h=0;h<this.rows;h++)for(let l=0;l<this.cols;l++){let c=this.cells[h][l],g=this.parseColorToRGB(c.bgColor),m=this.parseColorToRGB(c.fgColor),u=c.char.charCodeAt(0),C=this.bitmapFont.get(u);for(let b=0;b<this.bitmapCharHeight;b++)for(let p=0;p<this.bitmapCharWidth;p++){let v=l*this.bitmapCharWidth+p,y=((h*this.bitmapCharHeight+b)*t+v)*4,F=!1;if(C&&b<C.length){let $=C[b],U=1<<7-p;F=($&U)!==0}F?(i[y]=m.r,i[y+1]=m.g,i[y+2]=m.b,i[y+3]=m.a):(i[y]=g.r,i[y+1]=g.g,i[y+2]=g.b,i[y+3]=g.a)}}this.canvasBgColor!==null?(this.ctx.fillStyle=this.canvasBgColor,this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)):this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.imageSmoothingEnabled=!1,this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges";let s=document.createElement("canvas");s.width=t,s.height=e,s.getContext("2d").putImageData(this.imageDataBuffer,0,0);let r=this.cols*this.cellWidth,n=this.rows*this.cellHeight;this.ctx.drawImage(s,0,0,t,e,this.offsetX,this.offsetY,r,n),this.showDebugGrid&&this.drawDebugGrid()}parseColorToRGB(t){let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/.exec(t);if(e)return{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10),a:e[4]?Math.round(parseFloat(e[4])*255):255};if(t.startsWith("#")){let a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(a,(n,h,l,c)=>h+h+l+l+c+c);let r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);if(r)return{r:parseInt(r[1],16),g:parseInt(r[2],16),b:parseInt(r[3],16),a:255}}let s={white:[255,255,255],black:[0,0,0],red:[255,0,0],green:[0,255,0],blue:[0,0,255],yellow:[255,255,0],cyan:[0,255,255],magenta:[255,0,255],transparent:[0,0,0]}[t.toLowerCase()];return s?{r:s[0],g:s[1],b:s[2],a:t==="transparent"?0:255}:{r:255,g:255,b:255,a:255}}renderClassic(){this.canvasBgColor!==null?(this.ctx.fillStyle=this.canvasBgColor,this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)):this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.fontType==="bitmap"||this.fontType==="image"?(this.ctx.imageSmoothingEnabled=!1,this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges"):(this.ctx.imageSmoothingEnabled=!0,this.ctx.imageSmoothingQuality="high",this.canvas.style.imageRendering="auto",this.ctx.font=`${this.fontSize}px ${this.fontFamily}`,this.ctx.textBaseline="top",this.ctx.textRendering="optimizeLegibility");for(let t=0;t<this.rows;t++)for(let e=0;e<this.cols;e++){let i=this.cells[t][e],s=Math.floor(this.offsetX+e*this.cellWidth),a=Math.floor(this.offsetY+t*this.cellHeight),r=Math.floor(this.offsetX+(e+1)*this.cellWidth),n=Math.floor(this.offsetY+(t+1)*this.cellHeight),h=r-s,l=n-a;if((this.canvasBgColor!==null||i.bgColor!==this.defaultBgColor)&&(this.ctx.fillStyle=i.bgColor,this.ctx.fillRect(s,a,h,l)),i.char!==" ")if(this.fontType==="image"&&this.imageAtlas){let g=i.char.charCodeAt(0);this.imageAtlas.drawChar(this.ctx,g,s,a,h,l,i.fgColor)}else if(this.fontType==="bitmap"&&this.bitmapFont)this.drawBitmapChar(i.char,s,a,h,l,i.fgColor);else{this.ctx.fillStyle=i.fgColor;let g=s+(h-this.ctx.measureText(i.char).width)/2,m=a+(l-this.fontSize)/2;this.ctx.fillText(i.char,g,m)}}this.showDebugGrid&&this.drawDebugGrid()}drawBitmapChar(t,e,i,s,a,r){let n=t.charCodeAt(0);if(this.bitmapAtlas){this.bitmapAtlas.drawChar(this.ctx,n,e,i,s,a,r);return}if(!this.bitmapFont)return;let h=this.bitmapFont.get(n);if(!h)return;let l=s/this.bitmapCharWidth,c=a/this.bitmapCharHeight;this.ctx.fillStyle=r;for(let g=0;g<Math.min(h.length,this.bitmapCharHeight);g++){let m=h[g],u=i+g*c,C=i+(g+1)*c;for(let b=0;b<this.bitmapCharWidth;b++){let p=1<<7-b;if(m&p){let v=e+b*l,k=e+(b+1)*l;this.ctx.fillRect(v,u,k-v,C-u)}}}}drawDebugGrid(){if(!this.gridOverlay)return;let t=this.parentElement.clientWidth||800,e=this.parentElement.clientHeight||600;this.gridOverlay.update(this.cols,this.rows,this.cellWidth,this.cellHeight,t,e,this.offsetX,this.offsetY)}getCanvas(){return this.canvas}getContext(){return this.ctx}getDimensions(){return{cols:this.cols,rows:this.rows}}getCellDimensions(){return{cellWidth:this.cellWidth,cellHeight:this.cellHeight}}getCellWidth(){return this.cellWidth}getCellHeight(){return this.cellHeight}getCurrentScale(){return this.currentScale}getScalingMode(){return this.scalingMode}getOffsets(){return{offsetX:this.offsetX,offsetY:this.offsetY}}setDebugGrid(t){this.showDebugGrid=t,t&&!this.gridOverlay?(this.gridOverlay=new w(this.parentElement,{strokeColor:this.debugGridColor,lineWidth:1,zIndex:10}),this.drawDebugGrid()):!t&&this.gridOverlay?(this.gridOverlay.destroy(),this.gridOverlay=void 0):t&&this.gridOverlay&&this.gridOverlay.setVisible(!0)}setCanvasBackgroundColor(t){this.canvasBgColor=t}getCanvasBackgroundColor(){return this.canvasBgColor}setFixedGrid(t,e,i){this.fixedGridMode=!0,this.fixedCols=t,this.fixedRows=e,i!==void 0&&(this.cellAspectRatio=i);let s=this.cols,a=this.rows,r=this.cells;if(this.calculateGridSize(),this.cols!==s||this.rows!==a){this.cells=this.createEmptyGrid();let n=Math.min(s,this.cols),h=Math.min(a,this.rows);for(let l=0;l<h;l++)for(let c=0;c<n;c++)this.cells[l][c]=r[l][c]}this.render()}setAdaptiveGrid(t,e){this.fixedGridMode=!1,this.fixedCols=void 0,this.fixedRows=void 0,t!==void 0&&(this.cellWidth=t),e!==void 0&&(this.cellHeight=e);let i=this.cols,s=this.rows,a=this.cells;if(this.calculateGridSize(),this.cols!==i||this.rows!==s){this.cells=this.createEmptyGrid();let r=Math.min(i,this.cols),n=Math.min(s,this.rows);for(let h=0;h<n;h++)for(let l=0;l<r;l++)this.cells[h][l]=a[h][l]}this.render()}isFixedGridMode(){return this.fixedGridMode}setDebugGridColor(t){this.debugGridColor=t}isDebugGridEnabled(){return this.showDebugGrid}setImageDataRendering(t){this.useImageDataRendering=t,t&&this.fontType==="bitmap"?console.warn("[Render] ImageData rendering enabled (optimized for benchmarks)"):t&&(console.warn("[Render] ImageData rendering requires bitmap font, keeping classic mode"),this.useImageDataRendering=!1)}isImageDataRenderingEnabled(){return this.useImageDataRendering}setWebFont(t,e){this.fontType="web",this.fontFamily=t,e!==void 0&&(this.fontSize=e),this.bitmapFont=void 0,this.bitmapAtlas=void 0,this.useImageDataRendering=!1,this.calculateGridSize(),this.cells=this.createEmptyGrid()}setBitmapFont(t,e,i,s,a){this.fontType="bitmap",this.bitmapFont=t,this.bitmapCharWidth=e,this.bitmapCharHeight=i,this.bitmapAtlas=new H(t,e,i,s,a),this.cellWidth=s,this.cellHeight=a,this.calculateGridSize(),this.cells=this.createEmptyGrid()}async setImageFont(t,e,i,s,a,r){this.fontType="image",this.imageAtlas=new M({glyphWidth:e,glyphHeight:i,cellWidth:s,cellHeight:a,atlasBlocks:r}),await this.imageAtlas.loadFromPNG(t),this.cellWidth=s,this.cellHeight=a,this.bitmapCharWidth=e,this.bitmapCharHeight=i,this.calculateGridSize(),this.cells=this.createEmptyGrid()}getFontType(){return this.fontType}getBitmapFont(){return this.bitmapFont}getBitmapCharDimensions(){return this.fontType!=="bitmap"?null:{width:this.bitmapCharWidth,height:this.bitmapCharHeight}}setPalette(t){this.paletteCache=t}renderDisplayData(t){if(!t||!t.cells||t.cells.length===0){console.warn("[Terminal2D] Empty display data");return}if(t.width===0||t.height===0){console.warn("[Terminal2D] Invalid display dimensions:",t.width,t.height);return}(t.width!==this.cols||t.height!==this.rows)&&this.setFixedGrid(t.width,t.height,this.cellAspectRatio);let e=this.paletteCache??t.palette;if(!e||e.length===0){console.error("[Terminal2D] No palette available (neither cached nor in display)");return}let i=f(s=>{if(s==null||isNaN(s))return console.warn("[Terminal2D] Invalid palette index (undefined/null/NaN):",s),this.defaultFgColor;if(s===255)return"rgba(0, 0, 0, 0)";if(s<0||s>=e.length)return console.warn(`[Terminal2D] Invalid palette index: ${s}`),this.defaultFgColor;let a=e[s];return!a||typeof a.r!="number"?(console.warn(`[Terminal2D] Corrupted palette entry at index ${s}:`,a),this.defaultFgColor):`rgba(${a.r}, ${a.g}, ${a.b}, ${a.a/255})`},"convertColor");for(let s=0;s<t.height&&s<this.rows;s++)for(let a=0;a<t.width&&a<this.cols;a++){let r=s*t.width+a;if(r>=t.cells.length)break;let n=t.cells[r];if(!n)continue;let h=i(n.fgColorIndex),l=i(n.bgColorIndex);this.setCell(a,s,n.char??" ",h,l)}this.render()}isReady(){return!0}getCols(){return this.cols}getRows(){return this.rows}resize(t,e){this.setFixedGrid(t,e)}destroy(){this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=void 0),this.gridOverlay&&(this.gridOverlay.destroy(),this.gridOverlay=void 0),this.containerDiv.parentElement&&this.containerDiv.parentElement.removeChild(this.containerDiv)}};f(G,"Terminal2D");var A=G;var I=["#000000","#800000","#008000","#808000","#000080","#800080","#008080","#c0c0c0","#808080","#ff0000","#00ff00","#ffff00","#0000ff","#ff00ff","#00ffff","#ffffff","#080808","#121212","#1c1c1c","#262626","#303030","#3a3a3a","#444444","#4e4e4e","#585858","#626262","#6c6c6c","#767676","#808080","#8a8a8a","#949494","#9e9e9e","#a80000","#00a800","#a8a800","#0000a8","#a800a8","#00a8a8","#a8a8a8","#545454","#fc5454","#54fc54","#fcfc54","#5454fc","#fc54fc","#54fcfc","#fcfcfc","#000000",...Array(192).fill("#808080")];function z(d,t=I){return d<0||d>=t.length?"#ff00ff":t[d]}f(z,"paletteIndexToColor");function L(d,t=I){let e=t.indexOf(d.toLowerCase());return e>=0?e:0}f(L,"colorToPaletteIndex");var O=require("@utsp/types");
34
+ `,this.fontType==="web"&&(this.fontSize=Math.round(this.cellHeight/14*12)),this.offsetX=0,this.offsetY=0}createEmptyGrid(){let t=[];for(let e=0;e<this.rows;e++){let s=[];for(let i=0;i<this.cols;i++)s.push({char:" ",fgColor:this.defaultFgColor,bgColor:this.defaultBgColor});t.push(s)}return t}enableAutoResize(){this.resizeObserver=new ResizeObserver(()=>{let t=this.cols,e=this.rows;if(this.calculateGridSize(),this.cols!==t||this.rows!==e){let s=this.cells;this.cells=this.createEmptyGrid();let i=Math.min(t,this.cols),a=Math.min(e,this.rows);for(let n=0;n<a;n++)for(let r=0;r<i;r++)this.cells[n][r]=s[n][r]}this.render()}),this.resizeObserver.observe(this.parentElement)}setCell(t,e,s,i,a){if(e<0||e>=this.rows||t<0||t>=this.cols)return;let n=s&&typeof s=="string"?s.charAt(0):" ";this.cells[e][t]={char:n,fgColor:i??this.defaultFgColor,bgColor:a??this.defaultBgColor}}getCell(t,e){return e<0||e>=this.rows||t<0||t>=this.cols?null:this.cells[e][t]}write(t,e,s,i,a){for(let n=0;n<s.length;n++)this.setCell(t+n,e,s[n],i,a)}fillRect(t,e,s,i,a=" ",n,r){for(let l=e;l<e+i;l++)for(let o=t;o<t+s;o++)this.setCell(o,l,a,n,r)}clear(){this.cells=this.createEmptyGrid()}setFromArray(t){let e=t.width*t.height;if(t.cells.length!==e)throw new Error(`Invalid array length: expected ${e} (${t.width}\xD7${t.height}), got ${t.cells.length}`);let s=0;for(let i=0;i<t.height;i++)for(let a=0;a<t.width;a++){let n=t.cells[s];i<this.rows&&a<this.cols&&this.setCell(a,i,n.char,n.fgColor,n.bgColor),s++}}render(){if(this.fontType==="bitmap"&&this.bitmapFont&&this.useImageDataRendering){this.renderWithImageData();return}this.renderClassic()}renderWithImageData(){if(!this.bitmapFont)return;let t=this.cols*this.bitmapCharWidth,e=this.rows*this.bitmapCharHeight;(!this.imageDataBuffer||this.imageDataBuffer.width!==t||this.imageDataBuffer.height!==e)&&(this.imageDataBuffer=this.ctx.createImageData(t,e));let s=this.imageDataBuffer.data;for(let l=0;l<this.rows;l++)for(let o=0;o<this.cols;o++){let c=this.cells[l][o],d=this.parseColorToRGB(c.bgColor),g=this.parseColorToRGB(c.fgColor),u=c.char.charCodeAt(0),v=this.bitmapFont.get(u);for(let b=0;b<this.bitmapCharHeight;b++)for(let p=0;p<this.bitmapCharWidth;p++){let w=o*this.bitmapCharWidth+p,y=((l*this.bitmapCharHeight+b)*t+w)*4,F=!1;if(v&&b<v.length){let $=v[b],U=1<<7-p;F=($&U)!==0}F?(s[y]=g.r,s[y+1]=g.g,s[y+2]=g.b,s[y+3]=g.a):(s[y]=d.r,s[y+1]=d.g,s[y+2]=d.b,s[y+3]=d.a)}}this.canvasBgColor!==null?(this.ctx.fillStyle=this.canvasBgColor,this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)):this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.imageSmoothingEnabled=!1,this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges";let i=document.createElement("canvas");i.width=t,i.height=e,i.getContext("2d").putImageData(this.imageDataBuffer,0,0);let n=this.cols*this.cellWidth,r=this.rows*this.cellHeight;this.ctx.drawImage(i,0,0,t,e,this.offsetX,this.offsetY,n,r),this.showDebugGrid&&this.drawDebugGrid()}parseColorToRGB(t){let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/.exec(t);if(e)return{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10),a:e[4]?Math.round(parseFloat(e[4])*255):255};if(t.startsWith("#")){let a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(a,(r,l,o,c)=>l+l+o+o+c+c);let n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);if(n)return{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16),a:255}}let i={white:[255,255,255],black:[0,0,0],red:[255,0,0],green:[0,255,0],blue:[0,0,255],yellow:[255,255,0],cyan:[0,255,255],magenta:[255,0,255],transparent:[0,0,0]}[t.toLowerCase()];return i?{r:i[0],g:i[1],b:i[2],a:t==="transparent"?0:255}:{r:255,g:255,b:255,a:255}}renderClassic(){this.canvasBgColor!==null?(this.ctx.fillStyle=this.canvasBgColor,this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)):this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.fontType==="bitmap"||this.fontType==="image"?(this.ctx.imageSmoothingEnabled=!1,this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges"):(this.ctx.imageSmoothingEnabled=!0,this.ctx.imageSmoothingQuality="high",this.canvas.style.imageRendering="auto",this.ctx.font=`${this.fontSize}px ${this.fontFamily}`,this.ctx.textBaseline="top",this.ctx.textRendering="optimizeLegibility");for(let t=0;t<this.rows;t++)for(let e=0;e<this.cols;e++){let s=this.cells[t][e],i=Math.floor(this.offsetX+e*this.cellWidth),a=Math.floor(this.offsetY+t*this.cellHeight),n=Math.floor(this.offsetX+(e+1)*this.cellWidth),r=Math.floor(this.offsetY+(t+1)*this.cellHeight),l=n-i,o=r-a;if((this.canvasBgColor!==null||s.bgColor!==this.defaultBgColor)&&(this.ctx.fillStyle=s.bgColor,this.ctx.fillRect(i,a,l,o)),s.char!==" ")if(this.fontType==="image"&&this.imageAtlas){let d=s.char.charCodeAt(0);this.imageAtlas.drawChar(this.ctx,d,i,a,l,o,s.fgColor)}else if(this.fontType==="bitmap"&&this.bitmapFont)this.drawBitmapChar(s.char,i,a,l,o,s.fgColor);else{this.ctx.fillStyle=s.fgColor;let d=i+(l-this.ctx.measureText(s.char).width)/2,g=a+(o-this.fontSize)/2;this.ctx.fillText(s.char,d,g)}}this.showDebugGrid&&this.drawDebugGrid()}drawBitmapChar(t,e,s,i,a,n){let r=t.charCodeAt(0);if(this.bitmapAtlas){this.bitmapAtlas.drawChar(this.ctx,r,e,s,i,a,n);return}if(!this.bitmapFont)return;let l=this.bitmapFont.get(r);if(!l)return;let o=i/this.bitmapCharWidth,c=a/this.bitmapCharHeight;this.ctx.fillStyle=n;for(let d=0;d<Math.min(l.length,this.bitmapCharHeight);d++){let g=l[d],u=s+d*c,v=s+(d+1)*c;for(let b=0;b<this.bitmapCharWidth;b++){let p=1<<7-b;if(g&p){let w=e+b*o,k=e+(b+1)*o;this.ctx.fillRect(w,u,k-w,v-u)}}}}drawDebugGrid(){if(!this.gridOverlay)return;let t=this.parentElement.clientWidth||800,e=this.parentElement.clientHeight||600;this.gridOverlay.update(this.cols,this.rows,this.cellWidth,this.cellHeight,t,e,this.offsetX,this.offsetY)}getCanvas(){return this.canvas}getContext(){return this.ctx}getDimensions(){return{cols:this.cols,rows:this.rows}}getCellDimensions(){return{cellWidth:this.cellWidth,cellHeight:this.cellHeight}}getCellWidth(){return this.cellWidth}getCellHeight(){return this.cellHeight}getCurrentScale(){return this.currentScale}getScalingMode(){return this.scalingMode}getOffsets(){return{offsetX:this.offsetX,offsetY:this.offsetY}}setDebugGrid(t){this.showDebugGrid=t,t&&!this.gridOverlay?(this.gridOverlay=new x(this.parentElement,{strokeColor:this.debugGridColor,lineWidth:1,zIndex:10}),this.drawDebugGrid()):!t&&this.gridOverlay?(this.gridOverlay.destroy(),this.gridOverlay=void 0):t&&this.gridOverlay&&this.gridOverlay.setVisible(!0)}setCanvasBackgroundColor(t){this.canvasBgColor=t}getCanvasBackgroundColor(){return this.canvasBgColor}setFixedGrid(t,e,s){this.fixedGridMode=!0,this.fixedCols=t,this.fixedRows=e,s!==void 0&&(this.cellAspectRatio=s);let i=this.cols,a=this.rows,n=this.cells;if(this.calculateGridSize(),this.cols!==i||this.rows!==a){this.cells=this.createEmptyGrid();let r=Math.min(i,this.cols),l=Math.min(a,this.rows);for(let o=0;o<l;o++)for(let c=0;c<r;c++)this.cells[o][c]=n[o][c]}this.render()}setAdaptiveGrid(t,e){this.fixedGridMode=!1,this.fixedCols=void 0,this.fixedRows=void 0,t!==void 0&&(this.cellWidth=t),e!==void 0&&(this.cellHeight=e);let s=this.cols,i=this.rows,a=this.cells;if(this.calculateGridSize(),this.cols!==s||this.rows!==i){this.cells=this.createEmptyGrid();let n=Math.min(s,this.cols),r=Math.min(i,this.rows);for(let l=0;l<r;l++)for(let o=0;o<n;o++)this.cells[l][o]=a[l][o]}this.render()}isFixedGridMode(){return this.fixedGridMode}setDebugGridColor(t){this.debugGridColor=t}isDebugGridEnabled(){return this.showDebugGrid}setImageDataRendering(t){this.useImageDataRendering=t,t&&this.fontType==="bitmap"?console.warn("[Render] ImageData rendering enabled (optimized for benchmarks)"):t&&(console.warn("[Render] ImageData rendering requires bitmap font, keeping classic mode"),this.useImageDataRendering=!1)}isImageDataRenderingEnabled(){return this.useImageDataRendering}setWebFont(t,e){this.fontType="web",this.fontFamily=t,e!==void 0&&(this.fontSize=e),this.bitmapFont=void 0,this.bitmapAtlas=void 0,this.useImageDataRendering=!1,this.calculateGridSize(),this.cells=this.createEmptyGrid()}setBitmapFont(t,e,s,i,a){this.fontType="bitmap",this.bitmapFont=t,this.bitmapCharWidth=e,this.bitmapCharHeight=s,this.bitmapAtlas=new M(t,e,s,i,a),this.cellWidth=i,this.cellHeight=a,this.calculateGridSize(),this.cells=this.createEmptyGrid()}async setImageFont(t,e,s,i,a,n){this.fontType="image",this.imageAtlas=new H({glyphWidth:e,glyphHeight:s,cellWidth:i,cellHeight:a,atlasBlocks:n}),await this.imageAtlas.loadFromPNG(t),this.cellWidth=i,this.cellHeight=a,this.bitmapCharWidth=e,this.bitmapCharHeight=s,this.calculateGridSize(),this.cells=this.createEmptyGrid()}getFontType(){return this.fontType}getBitmapFont(){return this.bitmapFont}getBitmapCharDimensions(){return this.fontType!=="bitmap"?null:{width:this.bitmapCharWidth,height:this.bitmapCharHeight}}setPalette(t){this.paletteCache=t}renderDisplayData(t){if(!t||!t.cells||t.cells.length===0){console.warn("[Terminal2D] Empty display data");return}if(t.width===0||t.height===0){console.warn("[Terminal2D] Invalid display dimensions:",t.width,t.height);return}(t.width!==this.cols||t.height!==this.rows)&&this.setFixedGrid(t.width,t.height,this.cellAspectRatio);let e=this.paletteCache??t.palette;if(!e||e.length===0){console.error("[Terminal2D] No palette available (neither cached nor in display)");return}let s=f(i=>{if(i==null||isNaN(i))return console.warn("[Terminal2D] Invalid palette index (undefined/null/NaN):",i),this.defaultFgColor;if(i===255)return"rgba(0, 0, 0, 0)";if(i<0||i>=e.length)return console.warn(`[Terminal2D] Invalid palette index: ${i}`),this.defaultFgColor;let a=e[i];return!a||typeof a.r!="number"?(console.warn(`[Terminal2D] Corrupted palette entry at index ${i}:`,a),this.defaultFgColor):`rgba(${a.r}, ${a.g}, ${a.b}, ${a.a/255})`},"convertColor");for(let i=0;i<t.height&&i<this.rows;i++)for(let a=0;a<t.width&&a<this.cols;a++){let n=i*t.width+a;if(n>=t.cells.length)break;let r=t.cells[n];if(!r)continue;let l=s(r.fgColorIndex),o=s(r.bgColorIndex);this.setCell(a,i,r.char??" ",l,o)}this.render()}isReady(){return!0}getCols(){return this.cols}getRows(){return this.rows}resize(t,e){this.setFixedGrid(t,e)}setScalingMode(t){if(this.scalingMode===t)return;let e=this.cols,s=this.rows;if(this.scalingMode=t,this.calculateGridSize(),this.cols!==e||this.rows!==s){let i=this.cells;if(this.cells=this.createEmptyGrid(),i&&i.length>0){let a=Math.min(e,this.cols),n=Math.min(s,this.rows);for(let r=0;r<n;r++)for(let l=0;l<a;l++)i[r]&&i[r][l]&&(this.cells[r][l]=i[r][l])}}this.render()}setCellSize(t,e){let s=Math.max(1,Math.min(255,Math.round(t))),i=Math.max(1,Math.min(255,Math.round(e)));if(this.cellWidth===s&&this.cellHeight===i)return;let a=this.cols,n=this.rows;if(this.cellWidth=s,this.cellHeight=i,this.customCellSize=!0,this.calculateGridSize(),this.cols!==a||this.rows!==n){let r=this.cells;if(this.cells=this.createEmptyGrid(),r&&r.length>0){let l=Math.min(a,this.cols),o=Math.min(n,this.rows);for(let c=0;c<o;c++)for(let d=0;d<l;d++)r[c]&&r[c][d]&&(this.cells[c][d]=r[c][d])}}this.render()}getCellSize(){return{cellWidth:this.cellWidth,cellHeight:this.cellHeight}}getAvailableSize(){return{width:this.parentElement.clientWidth,height:this.parentElement.clientHeight}}getMaxCells(){return 65536}destroy(){this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=void 0),this.gridOverlay&&(this.gridOverlay.destroy(),this.gridOverlay=void 0),this.containerDiv.parentElement&&this.containerDiv.parentElement.removeChild(this.containerDiv)}};f(G,"Terminal2D");var A=G;var S=["#000000","#800000","#008000","#808000","#000080","#800080","#008080","#c0c0c0","#808080","#ff0000","#00ff00","#ffff00","#0000ff","#ff00ff","#00ffff","#ffffff","#080808","#121212","#1c1c1c","#262626","#303030","#3a3a3a","#444444","#4e4e4e","#585858","#626262","#6c6c6c","#767676","#808080","#8a8a8a","#949494","#9e9e9e","#a80000","#00a800","#a8a800","#0000a8","#a800a8","#00a8a8","#a8a8a8","#545454","#fc5454","#54fc54","#fcfc54","#5454fc","#fc54fc","#54fcfc","#fcfcfc","#000000",...Array(192).fill("#808080")];function z(m,t=S){return m<0||m>=t.length?"#ff00ff":t[m]}f(z,"paletteIndexToColor");function L(m,t=S){let e=t.indexOf(m.toLowerCase());return e>=0?e:0}f(L,"colorToPaletteIndex");var O=require("@utsp/types");
@@ -103,9 +103,9 @@ type FontType = {
103
103
  * Options pour la configuration du terminal
104
104
  */
105
105
  interface RenderOptions {
106
- /** Largeur d'une cellule en pixels (défaut: 10) - ignoré en mode fixedGrid */
106
+ /** Largeur d'une cellule en pixels (défaut: 8) - ignoré en mode fixedGrid */
107
107
  cellWidth?: number;
108
- /** Hauteur d'une cellule en pixels (défaut: 14) - ignoré en mode fixedGrid */
108
+ /** Hauteur d'une cellule en pixels (défaut: 8) - ignoré en mode fixedGrid */
109
109
  cellHeight?: number;
110
110
  /** Taille de la police en pixels (défaut: 12) */
111
111
  fontSize?: number;
@@ -129,7 +129,7 @@ interface RenderOptions {
129
129
  fixedCols?: number;
130
130
  /** Nombre de lignes fixe (active le mode fixedGrid) */
131
131
  fixedRows?: number;
132
- /** Ratio largeur/hauteur de cellule pour mode fixedGrid (défaut: 10/14 0.714) */
132
+ /** Ratio largeur/hauteur de cellule pour mode fixedGrid (défaut: 1 = carré) */
133
133
  cellAspectRatio?: number;
134
134
  /**
135
135
  * Scaling mode for pixel-perfect rendering (default: ScalingMode.None)
@@ -187,6 +187,7 @@ declare class Terminal2D implements IRenderer {
187
187
  private paletteCache?;
188
188
  private scalingMode;
189
189
  private currentScale;
190
+ private customCellSize;
190
191
  /**
191
192
  * Crée une instance de UTSPRender
192
193
  * @param parentDiv - L'élément HTML parent dans lequel créer le canvas
@@ -477,6 +478,55 @@ declare class Terminal2D implements IRenderer {
477
478
  * @param rows - New height in rows
478
479
  */
479
480
  resize(cols: number, rows: number): void;
481
+ /**
482
+ * Set scaling mode for pixel-perfect rendering
483
+ *
484
+ * @param mode - ScalingMode enum value:
485
+ * - ScalingMode.None: Fill space, may have sub-pixel artifacts
486
+ * - ScalingMode.Eighth: Snap to 0.125 increments (1.0, 1.125, 1.25...)
487
+ * - ScalingMode.Quarter: Snap to 0.25 increments (1.0, 1.25, 1.5...)
488
+ * - ScalingMode.Half: Snap to 0.5 increments (1.0, 1.5, 2.0...)
489
+ * - ScalingMode.Integer: Crisp pixels, may waste space (1x, 2x, 3x...)
490
+ * - ScalingMode.Responsive: No CSS scaling, cols/rows adapt to space
491
+ */
492
+ setScalingMode(mode: ScalingMode): void;
493
+ /**
494
+ * Set cell dimensions in pixels (for Responsive mode)
495
+ *
496
+ * Changes the native pixel size of each character cell.
497
+ * This will trigger a canvas resize and re-render.
498
+ *
499
+ * @param cellWidth - Cell width in pixels (1-255)
500
+ * @param cellHeight - Cell height in pixels (1-255)
501
+ */
502
+ setCellSize(cellWidth: number, cellHeight: number): void;
503
+ /**
504
+ * Get current cell size
505
+ */
506
+ getCellSize(): {
507
+ cellWidth: number;
508
+ cellHeight: number;
509
+ };
510
+ /**
511
+ * Get the available size from the parent container.
512
+ * This is the actual pixel space available for rendering.
513
+ * Useful in Responsive mode to calculate how many cells can fit.
514
+ *
515
+ * @returns Object with width and height in pixels
516
+ */
517
+ getAvailableSize(): {
518
+ width: number;
519
+ height: number;
520
+ };
521
+ /**
522
+ * Get the maximum number of cells this renderer can handle
523
+ *
524
+ * Canvas 2D has very generous limits compared to WebGL.
525
+ * The main constraint is memory for the ImageData buffer.
526
+ *
527
+ * @returns Maximum cells supported (256×256 = 65536 for compatibility)
528
+ */
529
+ getMaxCells(): number;
480
530
  /**
481
531
  * Détruit le terminal et nettoie les ressources
482
532
  */
package/dist/2d/index.mjs CHANGED
@@ -1,11 +1,11 @@
1
- var F=Object.defineProperty;var O=(g,t,e)=>t in g?F(g,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):g[t]=e;var f=(g,t)=>F(g,"name",{value:t,configurable:!0});var o=(g,t,e)=>(O(g,typeof t!="symbol"?t+"":t,e),e);var R=class R{constructor(t,e,i,s,a){o(this,"atlases");o(this,"charMap");o(this,"baseCharWidth");o(this,"baseCharHeight");o(this,"baseCellWidth");o(this,"baseCellHeight");o(this,"atlasColumns",16);o(this,"font");o(this,"SCALES",[1,2,4,8]);o(this,"colorCache",new Map);o(this,"MAX_CACHE_SIZE",512);this.font=t,this.baseCharWidth=e,this.baseCharHeight=i,this.baseCellWidth=s??e,this.baseCellHeight=a??i,this.charMap=new Map,this.atlases=new Map,this.generateAtlases()}generateAtlases(){this.atlasColumns=16;let t=0;for(let[e,i]of this.font){let s=t%this.atlasColumns,a=Math.floor(t/this.atlasColumns);this.charMap.set(e,{x:s*this.baseCharWidth,y:a*this.baseCharHeight});for(let r of this.SCALES){let n=this.getOrCreateAtlas(r),h=s*n.charWidth,l=a*n.charHeight;this.renderBitmapToAtlas(n,i,h,l)}t++}}getOrCreateAtlas(t){let e=this.atlases.get(t);if(!e){let i=this.baseCharWidth*t,s=this.baseCharHeight*t,r=Math.ceil(256/this.atlasColumns),n=document.createElement("canvas");n.width=this.atlasColumns*i,n.height=r*s;let h=n.getContext("2d",{alpha:!0,willReadFrequently:!1});if(!h)throw new Error(`Impossible de cr\xE9er le contexte 2D pour l'atlas ${t}x`);h.imageSmoothingEnabled=!1,e={canvas:n,ctx:h,scale:t,charWidth:i,charHeight:s},this.atlases.set(t,e)}return e}renderBitmapToAtlas(t,e,i,s){let a=t.scale,r=t.ctx;r.fillStyle="#ffffff";for(let n=0;n<Math.min(e.length,this.baseCharHeight);n++){let h=e[n];for(let l=0;l<this.baseCharWidth;l++){let c=1<<7-l;h&c&&r.fillRect(i+l*a,s+n*a,a,a)}}}drawChar(t,e,i,s,a,r,n){let h=this.charMap.get(e);if(!h)return;let l=`${e}:${n}`,c=this.colorCache.get(l);if(c)c.lastUsed=performance.now();else{let v=this.createColoredGlyph(e,n,h);if(!v)return;c={canvas:v,lastUsed:performance.now()},this.colorCache.set(l,c),this.colorCache.size>this.MAX_CACHE_SIZE&&this.evictLRU()}let m=a/this.baseCellWidth,d=r/this.baseCellHeight,u=(this.baseCellWidth-this.baseCharWidth)/2*m,C=(this.baseCellHeight-this.baseCharHeight)/2*d,b=this.baseCharWidth*m,p=this.baseCharHeight*d;t.drawImage(c.canvas,i+u,s+C,b,p)}createColoredGlyph(t,e,i){let s=this.atlases.get(1);if(!s)return null;let a=s.ctx.getImageData(i.x,i.y,this.baseCharWidth,this.baseCharHeight),r=a.data,n=this.hexToRgb(e);for(let c=0;c<r.length;c+=4)r[c+3]>0&&(r[c]=n.r,r[c+1]=n.g,r[c+2]=n.b);let h=document.createElement("canvas");h.width=this.baseCharWidth,h.height=this.baseCharHeight;let l=h.getContext("2d",{alpha:!0});return l?(l.imageSmoothingEnabled=!1,l.putImageData(a,0,0),h):null}hexToRgb(t){if(t.startsWith("#")){let a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(a,(n,h,l,c)=>h+h+l+l+c+c);let r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return r?{r:parseInt(r[1],16),g:parseInt(r[2],16),b:parseInt(r[3],16)}:{r:255,g:255,b:255}}let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)/.exec(t);if(e)return{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)};let s={white:[255,255,255],black:[0,0,0],red:[255,0,0],green:[0,255,0],blue:[0,0,255],yellow:[255,255,0],cyan:[0,255,255],magenta:[255,0,255]}[t.toLowerCase()];return s?{r:s[0],g:s[1],b:s[2]}:{r:255,g:255,b:255}}evictLRU(){let t=null,e=1/0;for(let[i,s]of this.colorCache)s.lastUsed<e&&(e=s.lastUsed,t=i);if(t){let i=this.colorCache.get(t);i&&(i.canvas.width=0,i.canvas.height=0),this.colorCache.delete(t)}}getAtlasCanvas(t=1){return this.atlases.get(t)?.canvas}getAllAtlases(){return this.atlases}getCharDimensions(){return{width:this.baseCharWidth,height:this.baseCharHeight}}getCharCount(){return this.charMap.size}hasChar(t){return this.charMap.has(t)}getAtlasDimensions(t=1){let e=this.atlases.get(t);if(e)return{width:e.canvas.width,height:e.canvas.height}}toDataURL(t=1,e="image/png"){return this.atlases.get(t)?.canvas.toDataURL(e)}getCacheStats(){return{size:this.colorCache.size,maxSize:this.MAX_CACHE_SIZE}}clearCache(){for(let t of this.colorCache.values())t.canvas.width=0,t.canvas.height=0;this.colorCache.clear()}destroy(){this.charMap.clear();for(let t of this.atlases.values())t.canvas.width=0,t.canvas.height=0;this.atlases.clear(),this.clearCache()}};f(R,"BitmapFontAtlas");var H=R;function D(g){return Math.sqrt(g)*16}f(D,"getAtlasColumns");function E(g){return g*256-1}f(E,"getMaxCharCode");function W(g,t){let e=Math.floor(g/256),i=g%256,s=Math.sqrt(t),a=e%s,r=Math.floor(e/s),n=i%16,h=Math.floor(i/16);return{col:a*16+n,row:r*16+h}}f(W,"getCharGridPosition");var A=class A{constructor(t){o(this,"atlasCanvas",null);o(this,"atlasCtx",null);o(this,"atlasImage",null);o(this,"glyphWidth");o(this,"glyphHeight");o(this,"cellWidth");o(this,"cellHeight");o(this,"atlasBlocks");o(this,"atlasColumns");o(this,"maxCharCode");o(this,"isLoaded",!1);o(this,"colorCache",new Map);o(this,"MAX_CACHE_SIZE",1024);this.glyphWidth=t.glyphWidth,this.glyphHeight=t.glyphHeight,this.cellWidth=t.cellWidth??t.glyphWidth,this.cellHeight=t.cellHeight??t.glyphHeight,this.atlasBlocks=t.atlasBlocks,this.atlasColumns=D(t.atlasBlocks),this.maxCharCode=E(t.atlasBlocks)}async loadFromPNG(t){return new Promise((e,i)=>{let s=new Uint8Array(t),a=new Blob([s],{type:"image/png"}),r=URL.createObjectURL(a),n=new Image;n.onload=()=>{URL.revokeObjectURL(r);let h=this.atlasColumns*this.glyphWidth,l=this.atlasColumns*this.glyphHeight;if((n.width!==h||n.height!==l)&&console.warn(`ImageFontAtlas: Image size ${n.width}\xD7${n.height} doesn't match expected ${h}\xD7${l} for ${this.atlasBlocks} block(s) with ${this.glyphWidth}\xD7${this.glyphHeight} glyphs`),this.atlasCanvas=document.createElement("canvas"),this.atlasCanvas.width=n.width,this.atlasCanvas.height=n.height,this.atlasCtx=this.atlasCanvas.getContext("2d",{alpha:!0,willReadFrequently:!0}),!this.atlasCtx){i(new Error("Failed to create 2D context for atlas"));return}this.atlasCtx.imageSmoothingEnabled=!1,this.atlasCtx.drawImage(n,0,0),this.atlasImage=n,this.isLoaded=!0,e()},n.onerror=()=>{URL.revokeObjectURL(r),i(new Error("Failed to load PNG image for atlas"))},n.src=r})}isReady(){return this.isLoaded}getGlyphPosition(t){if(t<0||t>this.maxCharCode)return null;let{col:e,row:i}=W(t,this.atlasBlocks);return{x:e*this.glyphWidth,y:i*this.glyphHeight}}getCharUV(t){if(t<0||t>this.maxCharCode)return null;let{col:e,row:i}=W(t,this.atlasBlocks),s=e/this.atlasColumns,a=i/this.atlasColumns,r=(e+1)/this.atlasColumns,n=(i+1)/this.atlasColumns;return{u1:s,v1:a,u2:r,v2:n}}drawChar(t,e,i,s,a,r,n){if(!this.isLoaded||!this.atlasCanvas)return;let h=this.getGlyphPosition(e);if(!h)return;let l=`${e}:${n}`,c=this.colorCache.get(l);if(c)c.lastUsed=performance.now();else{let d=this.createColoredGlyph(e,n,h);if(!d)return;c={canvas:d,lastUsed:performance.now()},this.colorCache.set(l,c),this.colorCache.size>this.MAX_CACHE_SIZE&&this.evictLRU()}if(t.imageSmoothingEnabled=!1,a===this.cellWidth&&r===this.cellHeight){let d=Math.floor((this.cellWidth-this.glyphWidth)/2),u=Math.floor((this.cellHeight-this.glyphHeight)/2);t.drawImage(c.canvas,Math.floor(i)+d,Math.floor(s)+u)}else{let d=a/this.cellWidth,u=r/this.cellHeight,C=Math.floor((this.cellWidth-this.glyphWidth)/2*d),b=Math.floor((this.cellHeight-this.glyphHeight)/2*u),p=Math.ceil(this.glyphWidth*d),v=Math.ceil(this.glyphHeight*u);t.drawImage(c.canvas,Math.floor(i)+C,Math.floor(s)+b,p,v)}}createColoredGlyph(t,e,i){if(!this.atlasCtx)return null;let s=this.atlasCtx.getImageData(i.x,i.y,this.glyphWidth,this.glyphHeight),a=s.data,r=this.parseColor(e);for(let l=0;l<a.length;l+=4)a[l+3]>0&&(a[l]=r.r,a[l+1]=r.g,a[l+2]=r.b);let n=document.createElement("canvas");n.width=this.glyphWidth,n.height=this.glyphHeight;let h=n.getContext("2d",{alpha:!0});return h?(h.putImageData(s,0,0),n):null}parseColor(t){if(t.startsWith("#")){let i=t.slice(1);return i.length===3&&(i=i[0]+i[0]+i[1]+i[1]+i[2]+i[2]),{r:parseInt(i.slice(0,2),16),g:parseInt(i.slice(2,4),16),b:parseInt(i.slice(4,6),16)}}let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(t);return e?{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)}:{r:255,g:255,b:255}}evictLRU(){let t=null,e=1/0;for(let[i,s]of this.colorCache)s.lastUsed<e&&(e=s.lastUsed,t=i);if(t){let i=this.colorCache.get(t);i&&(i.canvas.width=0,i.canvas.height=0),this.colorCache.delete(t)}}getAtlasCanvas(){return this.atlasCanvas}getAtlasImage(){return this.atlasImage}getConfig(){return{glyphWidth:this.glyphWidth,glyphHeight:this.glyphHeight,cellWidth:this.cellWidth,cellHeight:this.cellHeight,atlasBlocks:this.atlasBlocks}}getGlyphDimensions(){return{width:this.glyphWidth,height:this.glyphHeight}}getCellDimensions(){return{width:this.cellWidth,height:this.cellHeight}}getAtlasDimensions(){return{width:this.atlasColumns*this.glyphWidth,height:this.atlasColumns*this.glyphHeight}}getAtlasColumns(){return this.atlasColumns}getMaxCharCode(){return this.maxCharCode}isValidCharCode(t){return t>=0&&t<=this.maxCharCode}clearCache(){for(let t of this.colorCache.values())t.canvas.width=0,t.canvas.height=0;this.colorCache.clear()}getCacheStats(){return{size:this.colorCache.size,maxSize:this.MAX_CACHE_SIZE}}destroy(){this.clearCache(),this.atlasCanvas&&(this.atlasCanvas.width=0,this.atlasCanvas.height=0,this.atlasCanvas=null),this.atlasCtx=null,this.atlasImage=null,this.isLoaded=!1}};f(A,"ImageFontAtlas");var M=A;var I=class I{constructor(t,e){o(this,"canvas");o(this,"ctx");o(this,"container");o(this,"cols",0);o(this,"rows",0);o(this,"cellWidth",0);o(this,"cellHeight",0);o(this,"offsetX",0);o(this,"offsetY",0);o(this,"strokeColor","rgba(80, 80, 80, 0.4)");o(this,"lineWidth",1);this.container=t,this.canvas=document.createElement("canvas"),this.canvas.className="grid-overlay-canvas";let i=e?.zIndex??10;this.canvas.style.cssText=`
1
+ var F=Object.defineProperty;var O=(g,t,e)=>t in g?F(g,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):g[t]=e;var f=(g,t)=>F(g,"name",{value:t,configurable:!0});var h=(g,t,e)=>(O(g,typeof t!="symbol"?t+"":t,e),e);var R=class R{constructor(t,e,i,s,a){h(this,"atlases");h(this,"charMap");h(this,"baseCharWidth");h(this,"baseCharHeight");h(this,"baseCellWidth");h(this,"baseCellHeight");h(this,"atlasColumns",16);h(this,"font");h(this,"SCALES",[1,2,4,8]);h(this,"colorCache",new Map);h(this,"MAX_CACHE_SIZE",512);this.font=t,this.baseCharWidth=e,this.baseCharHeight=i,this.baseCellWidth=s??e,this.baseCellHeight=a??i,this.charMap=new Map,this.atlases=new Map,this.generateAtlases()}generateAtlases(){this.atlasColumns=16;let t=0;for(let[e,i]of this.font){let s=t%this.atlasColumns,a=Math.floor(t/this.atlasColumns);this.charMap.set(e,{x:s*this.baseCharWidth,y:a*this.baseCharHeight});for(let n of this.SCALES){let r=this.getOrCreateAtlas(n),l=s*r.charWidth,o=a*r.charHeight;this.renderBitmapToAtlas(r,i,l,o)}t++}}getOrCreateAtlas(t){let e=this.atlases.get(t);if(!e){let i=this.baseCharWidth*t,s=this.baseCharHeight*t,n=Math.ceil(256/this.atlasColumns),r=document.createElement("canvas");r.width=this.atlasColumns*i,r.height=n*s;let l=r.getContext("2d",{alpha:!0,willReadFrequently:!1});if(!l)throw new Error(`Impossible de cr\xE9er le contexte 2D pour l'atlas ${t}x`);l.imageSmoothingEnabled=!1,e={canvas:r,ctx:l,scale:t,charWidth:i,charHeight:s},this.atlases.set(t,e)}return e}renderBitmapToAtlas(t,e,i,s){let a=t.scale,n=t.ctx;n.fillStyle="#ffffff";for(let r=0;r<Math.min(e.length,this.baseCharHeight);r++){let l=e[r];for(let o=0;o<this.baseCharWidth;o++){let c=1<<7-o;l&c&&n.fillRect(i+o*a,s+r*a,a,a)}}}drawChar(t,e,i,s,a,n,r){let l=this.charMap.get(e);if(!l)return;let o=`${e}:${r}`,c=this.colorCache.get(o);if(c)c.lastUsed=performance.now();else{let v=this.createColoredGlyph(e,r,l);if(!v)return;c={canvas:v,lastUsed:performance.now()},this.colorCache.set(o,c),this.colorCache.size>this.MAX_CACHE_SIZE&&this.evictLRU()}let d=a/this.baseCellWidth,m=n/this.baseCellHeight,u=(this.baseCellWidth-this.baseCharWidth)/2*d,C=(this.baseCellHeight-this.baseCharHeight)/2*m,b=this.baseCharWidth*d,p=this.baseCharHeight*m;t.drawImage(c.canvas,i+u,s+C,b,p)}createColoredGlyph(t,e,i){let s=this.atlases.get(1);if(!s)return null;let a=s.ctx.getImageData(i.x,i.y,this.baseCharWidth,this.baseCharHeight),n=a.data,r=this.hexToRgb(e);for(let c=0;c<n.length;c+=4)n[c+3]>0&&(n[c]=r.r,n[c+1]=r.g,n[c+2]=r.b);let l=document.createElement("canvas");l.width=this.baseCharWidth,l.height=this.baseCharHeight;let o=l.getContext("2d",{alpha:!0});return o?(o.imageSmoothingEnabled=!1,o.putImageData(a,0,0),l):null}hexToRgb(t){if(t.startsWith("#")){let a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(a,(r,l,o,c)=>l+l+o+o+c+c);let n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return n?{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16)}:{r:255,g:255,b:255}}let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)/.exec(t);if(e)return{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)};let s={white:[255,255,255],black:[0,0,0],red:[255,0,0],green:[0,255,0],blue:[0,0,255],yellow:[255,255,0],cyan:[0,255,255],magenta:[255,0,255]}[t.toLowerCase()];return s?{r:s[0],g:s[1],b:s[2]}:{r:255,g:255,b:255}}evictLRU(){let t=null,e=1/0;for(let[i,s]of this.colorCache)s.lastUsed<e&&(e=s.lastUsed,t=i);if(t){let i=this.colorCache.get(t);i&&(i.canvas.width=0,i.canvas.height=0),this.colorCache.delete(t)}}getAtlasCanvas(t=1){return this.atlases.get(t)?.canvas}getAllAtlases(){return this.atlases}getCharDimensions(){return{width:this.baseCharWidth,height:this.baseCharHeight}}getCharCount(){return this.charMap.size}hasChar(t){return this.charMap.has(t)}getAtlasDimensions(t=1){let e=this.atlases.get(t);if(e)return{width:e.canvas.width,height:e.canvas.height}}toDataURL(t=1,e="image/png"){return this.atlases.get(t)?.canvas.toDataURL(e)}getCacheStats(){return{size:this.colorCache.size,maxSize:this.MAX_CACHE_SIZE}}clearCache(){for(let t of this.colorCache.values())t.canvas.width=0,t.canvas.height=0;this.colorCache.clear()}destroy(){this.charMap.clear();for(let t of this.atlases.values())t.canvas.width=0,t.canvas.height=0;this.atlases.clear(),this.clearCache()}};f(R,"BitmapFontAtlas");var M=R;function E(g){return Math.sqrt(g)*16}f(E,"getAtlasColumns");function D(g){return g*256-1}f(D,"getMaxCharCode");function W(g,t){let e=Math.floor(g/256),i=g%256,s=Math.sqrt(t),a=e%s,n=Math.floor(e/s),r=i%16,l=Math.floor(i/16);return{col:a*16+r,row:n*16+l}}f(W,"getCharGridPosition");var A=class A{constructor(t){h(this,"atlasCanvas",null);h(this,"atlasCtx",null);h(this,"atlasImage",null);h(this,"glyphWidth");h(this,"glyphHeight");h(this,"cellWidth");h(this,"cellHeight");h(this,"atlasBlocks");h(this,"atlasColumns");h(this,"maxCharCode");h(this,"isLoaded",!1);h(this,"colorCache",new Map);h(this,"MAX_CACHE_SIZE",1024);this.glyphWidth=t.glyphWidth,this.glyphHeight=t.glyphHeight,this.cellWidth=t.cellWidth??t.glyphWidth,this.cellHeight=t.cellHeight??t.glyphHeight,this.atlasBlocks=t.atlasBlocks,this.atlasColumns=E(t.atlasBlocks),this.maxCharCode=D(t.atlasBlocks)}async loadFromPNG(t){return new Promise((e,i)=>{let s=new Uint8Array(t),a=new Blob([s],{type:"image/png"}),n=URL.createObjectURL(a),r=new Image;r.onload=()=>{URL.revokeObjectURL(n);let l=this.atlasColumns*this.glyphWidth,o=this.atlasColumns*this.glyphHeight;if((r.width!==l||r.height!==o)&&console.warn(`ImageFontAtlas: Image size ${r.width}\xD7${r.height} doesn't match expected ${l}\xD7${o} for ${this.atlasBlocks} block(s) with ${this.glyphWidth}\xD7${this.glyphHeight} glyphs`),this.atlasCanvas=document.createElement("canvas"),this.atlasCanvas.width=r.width,this.atlasCanvas.height=r.height,this.atlasCtx=this.atlasCanvas.getContext("2d",{alpha:!0,willReadFrequently:!0}),!this.atlasCtx){i(new Error("Failed to create 2D context for atlas"));return}this.atlasCtx.imageSmoothingEnabled=!1,this.atlasCtx.drawImage(r,0,0),this.atlasImage=r,this.isLoaded=!0,e()},r.onerror=()=>{URL.revokeObjectURL(n),i(new Error("Failed to load PNG image for atlas"))},r.src=n})}isReady(){return this.isLoaded}getGlyphPosition(t){if(t<0||t>this.maxCharCode)return null;let{col:e,row:i}=W(t,this.atlasBlocks);return{x:e*this.glyphWidth,y:i*this.glyphHeight}}getCharUV(t){if(t<0||t>this.maxCharCode)return null;let{col:e,row:i}=W(t,this.atlasBlocks),s=e/this.atlasColumns,a=i/this.atlasColumns,n=(e+1)/this.atlasColumns,r=(i+1)/this.atlasColumns;return{u1:s,v1:a,u2:n,v2:r}}drawChar(t,e,i,s,a,n,r){if(!this.isLoaded||!this.atlasCanvas)return;let l=this.getGlyphPosition(e);if(!l)return;let o=`${e}:${r}`,c=this.colorCache.get(o);if(c)c.lastUsed=performance.now();else{let m=this.createColoredGlyph(e,r,l);if(!m)return;c={canvas:m,lastUsed:performance.now()},this.colorCache.set(o,c),this.colorCache.size>this.MAX_CACHE_SIZE&&this.evictLRU()}if(t.imageSmoothingEnabled=!1,a===this.cellWidth&&n===this.cellHeight){let m=Math.floor((this.cellWidth-this.glyphWidth)/2),u=Math.floor((this.cellHeight-this.glyphHeight)/2);t.drawImage(c.canvas,Math.floor(i)+m,Math.floor(s)+u)}else{let m=a/this.cellWidth,u=n/this.cellHeight,C=Math.floor((this.cellWidth-this.glyphWidth)/2*m),b=Math.floor((this.cellHeight-this.glyphHeight)/2*u),p=Math.ceil(this.glyphWidth*m),v=Math.ceil(this.glyphHeight*u);t.drawImage(c.canvas,Math.floor(i)+C,Math.floor(s)+b,p,v)}}createColoredGlyph(t,e,i){if(!this.atlasCtx)return null;let s=this.atlasCtx.getImageData(i.x,i.y,this.glyphWidth,this.glyphHeight),a=s.data,n=this.parseColor(e);for(let o=0;o<a.length;o+=4)a[o+3]>0&&(a[o]=n.r,a[o+1]=n.g,a[o+2]=n.b);let r=document.createElement("canvas");r.width=this.glyphWidth,r.height=this.glyphHeight;let l=r.getContext("2d",{alpha:!0});return l?(l.putImageData(s,0,0),r):null}parseColor(t){if(t.startsWith("#")){let i=t.slice(1);return i.length===3&&(i=i[0]+i[0]+i[1]+i[1]+i[2]+i[2]),{r:parseInt(i.slice(0,2),16),g:parseInt(i.slice(2,4),16),b:parseInt(i.slice(4,6),16)}}let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(t);return e?{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)}:{r:255,g:255,b:255}}evictLRU(){let t=null,e=1/0;for(let[i,s]of this.colorCache)s.lastUsed<e&&(e=s.lastUsed,t=i);if(t){let i=this.colorCache.get(t);i&&(i.canvas.width=0,i.canvas.height=0),this.colorCache.delete(t)}}getAtlasCanvas(){return this.atlasCanvas}getAtlasImage(){return this.atlasImage}getConfig(){return{glyphWidth:this.glyphWidth,glyphHeight:this.glyphHeight,cellWidth:this.cellWidth,cellHeight:this.cellHeight,atlasBlocks:this.atlasBlocks}}getGlyphDimensions(){return{width:this.glyphWidth,height:this.glyphHeight}}getCellDimensions(){return{width:this.cellWidth,height:this.cellHeight}}getAtlasDimensions(){return{width:this.atlasColumns*this.glyphWidth,height:this.atlasColumns*this.glyphHeight}}getAtlasColumns(){return this.atlasColumns}getMaxCharCode(){return this.maxCharCode}isValidCharCode(t){return t>=0&&t<=this.maxCharCode}clearCache(){for(let t of this.colorCache.values())t.canvas.width=0,t.canvas.height=0;this.colorCache.clear()}getCacheStats(){return{size:this.colorCache.size,maxSize:this.MAX_CACHE_SIZE}}destroy(){this.clearCache(),this.atlasCanvas&&(this.atlasCanvas.width=0,this.atlasCanvas.height=0,this.atlasCanvas=null),this.atlasCtx=null,this.atlasImage=null,this.isLoaded=!1}};f(A,"ImageFontAtlas");var H=A;var S=class S{constructor(t,e){h(this,"canvas");h(this,"ctx");h(this,"container");h(this,"cols",0);h(this,"rows",0);h(this,"cellWidth",0);h(this,"cellHeight",0);h(this,"offsetX",0);h(this,"offsetY",0);h(this,"strokeColor","rgba(80, 80, 80, 0.4)");h(this,"lineWidth",1);this.container=t,this.canvas=document.createElement("canvas"),this.canvas.className="grid-overlay-canvas";let i=e?.zIndex??10;this.canvas.style.cssText=`
2
2
  display: block !important;
3
3
  position: absolute !important;
4
4
  pointer-events: none !important;
5
5
  image-rendering: pixelated !important;
6
6
  image-rendering: crisp-edges !important;
7
7
  z-index: ${i} !important;
8
- `,this.container.appendChild(this.canvas);let s=this.canvas.getContext("2d");if(!s)throw new Error("[GridOverlay] Impossible de cr\xE9er le contexte 2D");this.ctx=s,e&&(e.strokeColor&&(this.strokeColor=e.strokeColor),e.lineWidth!==void 0&&(this.lineWidth=e.lineWidth))}setDimensions(t,e,i,s,a=0,r=0){this.cols=t,this.rows=e,this.cellWidth=i,this.cellHeight=s,this.offsetX=a,this.offsetY=r}setCanvasSize(t,e){this.canvas.width=t,this.canvas.height=e,this.ctx.setTransform(1,0,0,1,0,0)}setStyle(t){t.strokeColor&&(this.strokeColor=t.strokeColor),t.lineWidth!==void 0&&(this.lineWidth=t.lineWidth)}setTransform(t,e,i,s,a){let r=s.left-a.left,n=s.top-a.top,h=s.width,l=s.height;(this.canvas.width!==h||this.canvas.height!==l)&&(this.canvas.width=h,this.canvas.height=l);let c=this.canvas.style;c.width=`${h}px`,c.height=`${l}px`,c.left=`${r}px`,c.top=`${n}px`}render(){if(!this.ctx)return;let t=this.cols*this.cellWidth,e=this.rows*this.cellHeight,i=t>0?this.canvas.width/t:1,s=e>0?this.canvas.height/e:1,a=Math.min(i,s),r=this.cellWidth*a,n=this.cellHeight*a,h=this.cols*r,l=this.rows*n,c=this.offsetX*a,m=this.offsetY*a;if(!(h===0||l===0)){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.strokeStyle=this.strokeColor,this.ctx.lineWidth=Math.max(1,this.lineWidth*a),this.ctx.beginPath();for(let d=0;d<=this.cols;d++){let u=c+d*r+.5;this.ctx.moveTo(u,m),this.ctx.lineTo(u,m+l)}for(let d=0;d<=this.rows;d++){let u=m+d*n+.5;this.ctx.moveTo(c,u),this.ctx.lineTo(c+h,u)}this.ctx.stroke()}}update(t,e,i,s,a,r,n=0,h=0){this.setDimensions(t,e,i,s,n,h),this.setCanvasSize(a,r),this.render()}setVisible(t){this.canvas.style.display=t?"block":"none"}destroy(){this.canvas.parentElement&&this.canvas.parentElement.removeChild(this.canvas)}getCanvas(){return this.canvas}};f(I,"GridOverlay");var w=I;import{ScalingMode as x}from"@utsp/types";var B=class B{constructor(t,e={}){o(this,"containerDiv");o(this,"canvas");o(this,"ctx");o(this,"parentElement");o(this,"cells");o(this,"cols",0);o(this,"rows",0);o(this,"fontSize");o(this,"fontFamily");o(this,"defaultFgColor");o(this,"defaultBgColor");o(this,"canvasBgColor");o(this,"cellWidth");o(this,"cellHeight");o(this,"offsetX",0);o(this,"offsetY",0);o(this,"fontType","web");o(this,"bitmapFont");o(this,"bitmapAtlas");o(this,"imageAtlas");o(this,"bitmapCharWidth",8);o(this,"bitmapCharHeight",8);o(this,"showDebugGrid");o(this,"debugGridColor");o(this,"gridOverlay");o(this,"fixedGridMode");o(this,"fixedCols");o(this,"fixedRows");o(this,"cellAspectRatio");o(this,"resizeObserver");o(this,"imageDataBuffer");o(this,"useImageDataRendering",!1);o(this,"paletteCache");o(this,"scalingMode",x.None);o(this,"currentScale",1);if(!t)throw new Error("Render: L'\xE9l\xE9ment parent est requis");this.parentElement=t,this.fixedGridMode=!!(e.fixedCols&&e.fixedRows),this.fixedCols=e.fixedCols,this.fixedRows=e.fixedRows,this.cellAspectRatio=e.cellAspectRatio??10/14,this.cellWidth=e.cellWidth??10,this.cellHeight=e.cellHeight??14,this.fontSize=e.fontSize??12,this.fontFamily=e.fontFamily??"monospace",this.defaultFgColor=e.defaultFgColor??"#ffffff",this.defaultBgColor=e.defaultBgColor??"#000000",this.canvasBgColor=e.canvasBgColor!==void 0?e.canvasBgColor:null,this.showDebugGrid=e.showDebugGrid??!1,this.debugGridColor=e.debugGridColor??"rgba(144, 24, 24, 1)",this.scalingMode=e.scalingMode??x.None,window.getComputedStyle(this.parentElement).position==="static"&&(this.parentElement.style.position="relative"),this.containerDiv=document.createElement("div"),this.containerDiv.className="terminal2d-container",this.containerDiv.style.cssText=`
8
+ `,this.container.appendChild(this.canvas);let s=this.canvas.getContext("2d");if(!s)throw new Error("[GridOverlay] Impossible de cr\xE9er le contexte 2D");this.ctx=s,e&&(e.strokeColor&&(this.strokeColor=e.strokeColor),e.lineWidth!==void 0&&(this.lineWidth=e.lineWidth))}setDimensions(t,e,i,s,a=0,n=0){this.cols=t,this.rows=e,this.cellWidth=i,this.cellHeight=s,this.offsetX=a,this.offsetY=n}setCanvasSize(t,e){this.canvas.width=t,this.canvas.height=e,this.ctx.setTransform(1,0,0,1,0,0)}setStyle(t){t.strokeColor&&(this.strokeColor=t.strokeColor),t.lineWidth!==void 0&&(this.lineWidth=t.lineWidth)}setTransform(t,e,i,s,a){let n=s.left-a.left,r=s.top-a.top,l=s.width,o=s.height;(this.canvas.width!==l||this.canvas.height!==o)&&(this.canvas.width=l,this.canvas.height=o);let c=this.canvas.style;c.width=`${l}px`,c.height=`${o}px`,c.left=`${n}px`,c.top=`${r}px`}render(){if(!this.ctx)return;let t=this.cols*this.cellWidth,e=this.rows*this.cellHeight,i=t>0?this.canvas.width/t:1,s=e>0?this.canvas.height/e:1,a=Math.min(i,s),n=this.cellWidth*a,r=this.cellHeight*a,l=this.cols*n,o=this.rows*r,c=this.offsetX*a,d=this.offsetY*a;if(!(l===0||o===0)){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.strokeStyle=this.strokeColor,this.ctx.lineWidth=Math.max(1,this.lineWidth*a),this.ctx.beginPath();for(let m=0;m<=this.cols;m++){let u=c+m*n+.5;this.ctx.moveTo(u,d),this.ctx.lineTo(u,d+o)}for(let m=0;m<=this.rows;m++){let u=d+m*r+.5;this.ctx.moveTo(c,u),this.ctx.lineTo(c+l,u)}this.ctx.stroke()}}update(t,e,i,s,a,n,r=0,l=0){this.setDimensions(t,e,i,s,r,l),this.setCanvasSize(a,n),this.render()}setVisible(t){this.canvas.style.display=t?"block":"none"}destroy(){this.canvas.parentElement&&this.canvas.parentElement.removeChild(this.canvas)}getCanvas(){return this.canvas}};f(S,"GridOverlay");var x=S;import{ScalingMode as w}from"@utsp/types";var T=class T{constructor(t,e={}){h(this,"containerDiv");h(this,"canvas");h(this,"ctx");h(this,"parentElement");h(this,"cells");h(this,"cols",0);h(this,"rows",0);h(this,"fontSize");h(this,"fontFamily");h(this,"defaultFgColor");h(this,"defaultBgColor");h(this,"canvasBgColor");h(this,"cellWidth");h(this,"cellHeight");h(this,"offsetX",0);h(this,"offsetY",0);h(this,"fontType","web");h(this,"bitmapFont");h(this,"bitmapAtlas");h(this,"imageAtlas");h(this,"bitmapCharWidth",8);h(this,"bitmapCharHeight",8);h(this,"showDebugGrid");h(this,"debugGridColor");h(this,"gridOverlay");h(this,"fixedGridMode");h(this,"fixedCols");h(this,"fixedRows");h(this,"cellAspectRatio");h(this,"resizeObserver");h(this,"imageDataBuffer");h(this,"useImageDataRendering",!1);h(this,"paletteCache");h(this,"scalingMode",w.None);h(this,"currentScale",1);h(this,"customCellSize",!1);if(!t)throw new Error("Render: L'\xE9l\xE9ment parent est requis");this.parentElement=t,this.fixedGridMode=!!(e.fixedCols&&e.fixedRows),this.fixedCols=e.fixedCols,this.fixedRows=e.fixedRows,this.cellAspectRatio=e.cellAspectRatio??1,this.cellWidth=e.cellWidth??8,this.cellHeight=e.cellHeight??8,this.fontSize=e.fontSize??12,this.fontFamily=e.fontFamily??"monospace",this.defaultFgColor=e.defaultFgColor??"#ffffff",this.defaultBgColor=e.defaultBgColor??"#000000",this.canvasBgColor=e.canvasBgColor!==void 0?e.canvasBgColor:null,this.showDebugGrid=e.showDebugGrid??!1,this.debugGridColor=e.debugGridColor??"rgba(144, 24, 24, 1)",this.scalingMode=e.scalingMode??w.None,window.getComputedStyle(this.parentElement).position==="static"&&(this.parentElement.style.position="relative"),this.containerDiv=document.createElement("div"),this.containerDiv.className="terminal2d-container",this.containerDiv.style.cssText=`
9
9
  position: absolute !important;
10
10
  top: 0 !important;
11
11
  left: 0 !important;
@@ -19,16 +19,16 @@ var F=Object.defineProperty;var O=(g,t,e)=>t in g?F(g,t,{enumerable:!0,configura
19
19
  justify-content: center !important;
20
20
  align-items: center !important;
21
21
  isolation: isolate !important;
22
- `,this.canvas=document.createElement("canvas"),this.canvas.className="terminal2d-canvas",e.className&&(this.canvas.className+=" "+e.className),e.style&&Object.assign(this.canvas.style,e.style),this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges",this.containerDiv.appendChild(this.canvas),this.parentElement.appendChild(this.containerDiv);let s=this.canvas.getContext("2d",{alpha:!0,desynchronized:!1});if(!s)throw new Error("UTSPRender: Impossible d'obtenir le contexte 2D du canvas");this.ctx=s,this.ctx.imageSmoothingEnabled=!1,this.calculateGridSize(),this.cells=this.createEmptyGrid(),this.showDebugGrid&&(this.gridOverlay=new w(this.parentElement,{strokeColor:this.debugGridColor,lineWidth:1,zIndex:10})),this.enableAutoResize(),this.render()}calculateGridSize(){let t=this.parentElement.clientWidth||800,e=this.parentElement.clientHeight||600,i,s;if(this.fontType==="image"&&this.imageAtlas){let m=this.imageAtlas.getCellDimensions();i=m.width,s=m.height}else this.fontType==="bitmap"?(i=this.bitmapCharWidth,s=this.bitmapCharHeight):(i=Math.round(this.cellWidth)||10,s=Math.round(this.cellHeight)||14);this.cellWidth=Math.round(i),this.cellHeight=Math.round(s),this.fixedGridMode&&this.fixedCols&&this.fixedRows?(this.cols=this.fixedCols,this.rows=this.fixedRows):(this.cols=Math.floor(t/this.cellWidth)||1,this.rows=Math.floor(e/this.cellHeight)||1);let a=this.cols*this.cellWidth,r=this.rows*this.cellHeight,n=t/a,h=e/r,l=Math.min(n,h),c;switch(this.scalingMode){case x.Integer:c=Math.max(1,Math.floor(l));break;case x.Half:c=Math.max(.5,Math.floor(l*2)/2);break;case x.Quarter:c=Math.max(.25,Math.floor(l*4)/4);break;case x.Eighth:c=Math.max(.125,Math.floor(l*8)/8);break;case x.None:default:c=Math.max(.1,l);break}this.currentScale=c,this.canvas.width=a,this.canvas.height=r,this.ctx.imageSmoothingEnabled=!1,this.canvas.style.cssText=`
22
+ `,this.canvas=document.createElement("canvas"),this.canvas.className="terminal2d-canvas",e.className&&(this.canvas.className+=" "+e.className),e.style&&Object.assign(this.canvas.style,e.style),this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges",this.containerDiv.appendChild(this.canvas),this.parentElement.appendChild(this.containerDiv);let s=this.canvas.getContext("2d",{alpha:!0,desynchronized:!1});if(!s)throw new Error("UTSPRender: Impossible d'obtenir le contexte 2D du canvas");this.ctx=s,this.ctx.imageSmoothingEnabled=!1,this.calculateGridSize(),this.cells=this.createEmptyGrid(),this.showDebugGrid&&(this.gridOverlay=new x(this.parentElement,{strokeColor:this.debugGridColor,lineWidth:1,zIndex:10})),this.enableAutoResize(),this.render()}calculateGridSize(){let t=this.parentElement.clientWidth||800,e=this.parentElement.clientHeight||600;if(!this.customCellSize){let o,c;if(this.fontType==="image"&&this.imageAtlas){let d=this.imageAtlas.getCellDimensions();o=d.width,c=d.height}else this.fontType==="bitmap"?(o=this.bitmapCharWidth,c=this.bitmapCharHeight):(o=Math.round(this.cellWidth)||10,c=Math.round(this.cellHeight)||14);this.cellWidth=Math.round(o),this.cellHeight=Math.round(c)}this.scalingMode===w.Responsive?(this.cols=Math.floor(t/this.cellWidth)||1,this.rows=Math.floor(e/this.cellHeight)||1):this.fixedGridMode&&this.fixedCols&&this.fixedRows?(this.cols=this.fixedCols,this.rows=this.fixedRows):(this.cols=Math.floor(t/this.cellWidth)||1,this.rows=Math.floor(e/this.cellHeight)||1);let i=this.cols*this.cellWidth,s=this.rows*this.cellHeight,a=t/i,n=e/s,r=Math.min(a,n),l;switch(this.scalingMode){case w.Integer:l=Math.max(1,Math.floor(r));break;case w.Half:l=Math.max(.5,Math.floor(r*2)/2);break;case w.Quarter:l=Math.max(.25,Math.floor(r*4)/4);break;case w.Eighth:l=Math.max(.125,Math.floor(r*8)/8);break;case w.Responsive:l=1;break;case w.None:default:l=Math.max(.1,r);break}this.currentScale=l,this.canvas.width=i,this.canvas.height=s,this.ctx.imageSmoothingEnabled=!1,this.canvas.style.cssText=`
23
23
  flex-shrink: 0 !important;
24
24
  flex-grow: 0 !important;
25
- width: ${a}px !important;
26
- height: ${r}px !important;
25
+ width: ${i}px !important;
26
+ height: ${s}px !important;
27
27
  image-rendering: pixelated !important;
28
28
  image-rendering: crisp-edges !important;
29
29
  -ms-interpolation-mode: nearest-neighbor !important;
30
30
  transform-origin: center center !important;
31
- transform: scale(${c}) !important;
31
+ transform: scale(${l}) !important;
32
32
  will-change: transform !important;
33
33
  backface-visibility: hidden !important;
34
- `,this.fontType==="web"&&(this.fontSize=Math.round(this.cellHeight/14*12)),this.offsetX=0,this.offsetY=0}createEmptyGrid(){let t=[];for(let e=0;e<this.rows;e++){let i=[];for(let s=0;s<this.cols;s++)i.push({char:" ",fgColor:this.defaultFgColor,bgColor:this.defaultBgColor});t.push(i)}return t}enableAutoResize(){this.resizeObserver=new ResizeObserver(()=>{let t=this.cols,e=this.rows;if(this.calculateGridSize(),this.cols!==t||this.rows!==e){let i=this.cells;this.cells=this.createEmptyGrid();let s=Math.min(t,this.cols),a=Math.min(e,this.rows);for(let r=0;r<a;r++)for(let n=0;n<s;n++)this.cells[r][n]=i[r][n]}this.render()}),this.resizeObserver.observe(this.parentElement)}setCell(t,e,i,s,a){if(e<0||e>=this.rows||t<0||t>=this.cols)return;let r=i&&typeof i=="string"?i.charAt(0):" ";this.cells[e][t]={char:r,fgColor:s??this.defaultFgColor,bgColor:a??this.defaultBgColor}}getCell(t,e){return e<0||e>=this.rows||t<0||t>=this.cols?null:this.cells[e][t]}write(t,e,i,s,a){for(let r=0;r<i.length;r++)this.setCell(t+r,e,i[r],s,a)}fillRect(t,e,i,s,a=" ",r,n){for(let h=e;h<e+s;h++)for(let l=t;l<t+i;l++)this.setCell(l,h,a,r,n)}clear(){this.cells=this.createEmptyGrid()}setFromArray(t){let e=t.width*t.height;if(t.cells.length!==e)throw new Error(`Invalid array length: expected ${e} (${t.width}\xD7${t.height}), got ${t.cells.length}`);let i=0;for(let s=0;s<t.height;s++)for(let a=0;a<t.width;a++){let r=t.cells[i];s<this.rows&&a<this.cols&&this.setCell(a,s,r.char,r.fgColor,r.bgColor),i++}}render(){if(this.fontType==="bitmap"&&this.bitmapFont&&this.useImageDataRendering){this.renderWithImageData();return}this.renderClassic()}renderWithImageData(){if(!this.bitmapFont)return;let t=this.cols*this.bitmapCharWidth,e=this.rows*this.bitmapCharHeight;(!this.imageDataBuffer||this.imageDataBuffer.width!==t||this.imageDataBuffer.height!==e)&&(this.imageDataBuffer=this.ctx.createImageData(t,e));let i=this.imageDataBuffer.data;for(let h=0;h<this.rows;h++)for(let l=0;l<this.cols;l++){let c=this.cells[h][l],m=this.parseColorToRGB(c.bgColor),d=this.parseColorToRGB(c.fgColor),u=c.char.charCodeAt(0),C=this.bitmapFont.get(u);for(let b=0;b<this.bitmapCharHeight;b++)for(let p=0;p<this.bitmapCharWidth;p++){let v=l*this.bitmapCharWidth+p,y=((h*this.bitmapCharHeight+b)*t+v)*4,k=!1;if(C&&b<C.length){let z=C[b],L=1<<7-p;k=(z&L)!==0}k?(i[y]=d.r,i[y+1]=d.g,i[y+2]=d.b,i[y+3]=d.a):(i[y]=m.r,i[y+1]=m.g,i[y+2]=m.b,i[y+3]=m.a)}}this.canvasBgColor!==null?(this.ctx.fillStyle=this.canvasBgColor,this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)):this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.imageSmoothingEnabled=!1,this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges";let s=document.createElement("canvas");s.width=t,s.height=e,s.getContext("2d").putImageData(this.imageDataBuffer,0,0);let r=this.cols*this.cellWidth,n=this.rows*this.cellHeight;this.ctx.drawImage(s,0,0,t,e,this.offsetX,this.offsetY,r,n),this.showDebugGrid&&this.drawDebugGrid()}parseColorToRGB(t){let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/.exec(t);if(e)return{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10),a:e[4]?Math.round(parseFloat(e[4])*255):255};if(t.startsWith("#")){let a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(a,(n,h,l,c)=>h+h+l+l+c+c);let r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);if(r)return{r:parseInt(r[1],16),g:parseInt(r[2],16),b:parseInt(r[3],16),a:255}}let s={white:[255,255,255],black:[0,0,0],red:[255,0,0],green:[0,255,0],blue:[0,0,255],yellow:[255,255,0],cyan:[0,255,255],magenta:[255,0,255],transparent:[0,0,0]}[t.toLowerCase()];return s?{r:s[0],g:s[1],b:s[2],a:t==="transparent"?0:255}:{r:255,g:255,b:255,a:255}}renderClassic(){this.canvasBgColor!==null?(this.ctx.fillStyle=this.canvasBgColor,this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)):this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.fontType==="bitmap"||this.fontType==="image"?(this.ctx.imageSmoothingEnabled=!1,this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges"):(this.ctx.imageSmoothingEnabled=!0,this.ctx.imageSmoothingQuality="high",this.canvas.style.imageRendering="auto",this.ctx.font=`${this.fontSize}px ${this.fontFamily}`,this.ctx.textBaseline="top",this.ctx.textRendering="optimizeLegibility");for(let t=0;t<this.rows;t++)for(let e=0;e<this.cols;e++){let i=this.cells[t][e],s=Math.floor(this.offsetX+e*this.cellWidth),a=Math.floor(this.offsetY+t*this.cellHeight),r=Math.floor(this.offsetX+(e+1)*this.cellWidth),n=Math.floor(this.offsetY+(t+1)*this.cellHeight),h=r-s,l=n-a;if((this.canvasBgColor!==null||i.bgColor!==this.defaultBgColor)&&(this.ctx.fillStyle=i.bgColor,this.ctx.fillRect(s,a,h,l)),i.char!==" ")if(this.fontType==="image"&&this.imageAtlas){let m=i.char.charCodeAt(0);this.imageAtlas.drawChar(this.ctx,m,s,a,h,l,i.fgColor)}else if(this.fontType==="bitmap"&&this.bitmapFont)this.drawBitmapChar(i.char,s,a,h,l,i.fgColor);else{this.ctx.fillStyle=i.fgColor;let m=s+(h-this.ctx.measureText(i.char).width)/2,d=a+(l-this.fontSize)/2;this.ctx.fillText(i.char,m,d)}}this.showDebugGrid&&this.drawDebugGrid()}drawBitmapChar(t,e,i,s,a,r){let n=t.charCodeAt(0);if(this.bitmapAtlas){this.bitmapAtlas.drawChar(this.ctx,n,e,i,s,a,r);return}if(!this.bitmapFont)return;let h=this.bitmapFont.get(n);if(!h)return;let l=s/this.bitmapCharWidth,c=a/this.bitmapCharHeight;this.ctx.fillStyle=r;for(let m=0;m<Math.min(h.length,this.bitmapCharHeight);m++){let d=h[m],u=i+m*c,C=i+(m+1)*c;for(let b=0;b<this.bitmapCharWidth;b++){let p=1<<7-b;if(d&p){let v=e+b*l,G=e+(b+1)*l;this.ctx.fillRect(v,u,G-v,C-u)}}}}drawDebugGrid(){if(!this.gridOverlay)return;let t=this.parentElement.clientWidth||800,e=this.parentElement.clientHeight||600;this.gridOverlay.update(this.cols,this.rows,this.cellWidth,this.cellHeight,t,e,this.offsetX,this.offsetY)}getCanvas(){return this.canvas}getContext(){return this.ctx}getDimensions(){return{cols:this.cols,rows:this.rows}}getCellDimensions(){return{cellWidth:this.cellWidth,cellHeight:this.cellHeight}}getCellWidth(){return this.cellWidth}getCellHeight(){return this.cellHeight}getCurrentScale(){return this.currentScale}getScalingMode(){return this.scalingMode}getOffsets(){return{offsetX:this.offsetX,offsetY:this.offsetY}}setDebugGrid(t){this.showDebugGrid=t,t&&!this.gridOverlay?(this.gridOverlay=new w(this.parentElement,{strokeColor:this.debugGridColor,lineWidth:1,zIndex:10}),this.drawDebugGrid()):!t&&this.gridOverlay?(this.gridOverlay.destroy(),this.gridOverlay=void 0):t&&this.gridOverlay&&this.gridOverlay.setVisible(!0)}setCanvasBackgroundColor(t){this.canvasBgColor=t}getCanvasBackgroundColor(){return this.canvasBgColor}setFixedGrid(t,e,i){this.fixedGridMode=!0,this.fixedCols=t,this.fixedRows=e,i!==void 0&&(this.cellAspectRatio=i);let s=this.cols,a=this.rows,r=this.cells;if(this.calculateGridSize(),this.cols!==s||this.rows!==a){this.cells=this.createEmptyGrid();let n=Math.min(s,this.cols),h=Math.min(a,this.rows);for(let l=0;l<h;l++)for(let c=0;c<n;c++)this.cells[l][c]=r[l][c]}this.render()}setAdaptiveGrid(t,e){this.fixedGridMode=!1,this.fixedCols=void 0,this.fixedRows=void 0,t!==void 0&&(this.cellWidth=t),e!==void 0&&(this.cellHeight=e);let i=this.cols,s=this.rows,a=this.cells;if(this.calculateGridSize(),this.cols!==i||this.rows!==s){this.cells=this.createEmptyGrid();let r=Math.min(i,this.cols),n=Math.min(s,this.rows);for(let h=0;h<n;h++)for(let l=0;l<r;l++)this.cells[h][l]=a[h][l]}this.render()}isFixedGridMode(){return this.fixedGridMode}setDebugGridColor(t){this.debugGridColor=t}isDebugGridEnabled(){return this.showDebugGrid}setImageDataRendering(t){this.useImageDataRendering=t,t&&this.fontType==="bitmap"?console.warn("[Render] ImageData rendering enabled (optimized for benchmarks)"):t&&(console.warn("[Render] ImageData rendering requires bitmap font, keeping classic mode"),this.useImageDataRendering=!1)}isImageDataRenderingEnabled(){return this.useImageDataRendering}setWebFont(t,e){this.fontType="web",this.fontFamily=t,e!==void 0&&(this.fontSize=e),this.bitmapFont=void 0,this.bitmapAtlas=void 0,this.useImageDataRendering=!1,this.calculateGridSize(),this.cells=this.createEmptyGrid()}setBitmapFont(t,e,i,s,a){this.fontType="bitmap",this.bitmapFont=t,this.bitmapCharWidth=e,this.bitmapCharHeight=i,this.bitmapAtlas=new H(t,e,i,s,a),this.cellWidth=s,this.cellHeight=a,this.calculateGridSize(),this.cells=this.createEmptyGrid()}async setImageFont(t,e,i,s,a,r){this.fontType="image",this.imageAtlas=new M({glyphWidth:e,glyphHeight:i,cellWidth:s,cellHeight:a,atlasBlocks:r}),await this.imageAtlas.loadFromPNG(t),this.cellWidth=s,this.cellHeight=a,this.bitmapCharWidth=e,this.bitmapCharHeight=i,this.calculateGridSize(),this.cells=this.createEmptyGrid()}getFontType(){return this.fontType}getBitmapFont(){return this.bitmapFont}getBitmapCharDimensions(){return this.fontType!=="bitmap"?null:{width:this.bitmapCharWidth,height:this.bitmapCharHeight}}setPalette(t){this.paletteCache=t}renderDisplayData(t){if(!t||!t.cells||t.cells.length===0){console.warn("[Terminal2D] Empty display data");return}if(t.width===0||t.height===0){console.warn("[Terminal2D] Invalid display dimensions:",t.width,t.height);return}(t.width!==this.cols||t.height!==this.rows)&&this.setFixedGrid(t.width,t.height,this.cellAspectRatio);let e=this.paletteCache??t.palette;if(!e||e.length===0){console.error("[Terminal2D] No palette available (neither cached nor in display)");return}let i=f(s=>{if(s==null||isNaN(s))return console.warn("[Terminal2D] Invalid palette index (undefined/null/NaN):",s),this.defaultFgColor;if(s===255)return"rgba(0, 0, 0, 0)";if(s<0||s>=e.length)return console.warn(`[Terminal2D] Invalid palette index: ${s}`),this.defaultFgColor;let a=e[s];return!a||typeof a.r!="number"?(console.warn(`[Terminal2D] Corrupted palette entry at index ${s}:`,a),this.defaultFgColor):`rgba(${a.r}, ${a.g}, ${a.b}, ${a.a/255})`},"convertColor");for(let s=0;s<t.height&&s<this.rows;s++)for(let a=0;a<t.width&&a<this.cols;a++){let r=s*t.width+a;if(r>=t.cells.length)break;let n=t.cells[r];if(!n)continue;let h=i(n.fgColorIndex),l=i(n.bgColorIndex);this.setCell(a,s,n.char??" ",h,l)}this.render()}isReady(){return!0}getCols(){return this.cols}getRows(){return this.rows}resize(t,e){this.setFixedGrid(t,e)}destroy(){this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=void 0),this.gridOverlay&&(this.gridOverlay.destroy(),this.gridOverlay=void 0),this.containerDiv.parentElement&&this.containerDiv.parentElement.removeChild(this.containerDiv)}};f(B,"Terminal2D");var T=B;var S=["#000000","#800000","#008000","#808000","#000080","#800080","#008080","#c0c0c0","#808080","#ff0000","#00ff00","#ffff00","#0000ff","#ff00ff","#00ffff","#ffffff","#080808","#121212","#1c1c1c","#262626","#303030","#3a3a3a","#444444","#4e4e4e","#585858","#626262","#6c6c6c","#767676","#808080","#8a8a8a","#949494","#9e9e9e","#a80000","#00a800","#a8a800","#0000a8","#a800a8","#00a8a8","#a8a8a8","#545454","#fc5454","#54fc54","#fcfc54","#5454fc","#fc54fc","#54fcfc","#fcfcfc","#000000",...Array(192).fill("#808080")];function $(g,t=S){return g<0||g>=t.length?"#ff00ff":t[g]}f($,"paletteIndexToColor");function U(g,t=S){let e=t.indexOf(g.toLowerCase());return e>=0?e:0}f(U,"colorToPaletteIndex");import{ScalingMode as ft}from"@utsp/types";export{H as BitmapFontAtlas,S as DEFAULT_PALETTE,w as GridOverlay,M as ImageFontAtlas,ft as ScalingMode,T as Terminal2D,U as colorToPaletteIndex,D as getAtlasColumns,W as getCharGridPosition,E as getMaxCharCode,$ as paletteIndexToColor};
34
+ `,this.fontType==="web"&&(this.fontSize=Math.round(this.cellHeight/14*12)),this.offsetX=0,this.offsetY=0}createEmptyGrid(){let t=[];for(let e=0;e<this.rows;e++){let i=[];for(let s=0;s<this.cols;s++)i.push({char:" ",fgColor:this.defaultFgColor,bgColor:this.defaultBgColor});t.push(i)}return t}enableAutoResize(){this.resizeObserver=new ResizeObserver(()=>{let t=this.cols,e=this.rows;if(this.calculateGridSize(),this.cols!==t||this.rows!==e){let i=this.cells;this.cells=this.createEmptyGrid();let s=Math.min(t,this.cols),a=Math.min(e,this.rows);for(let n=0;n<a;n++)for(let r=0;r<s;r++)this.cells[n][r]=i[n][r]}this.render()}),this.resizeObserver.observe(this.parentElement)}setCell(t,e,i,s,a){if(e<0||e>=this.rows||t<0||t>=this.cols)return;let n=i&&typeof i=="string"?i.charAt(0):" ";this.cells[e][t]={char:n,fgColor:s??this.defaultFgColor,bgColor:a??this.defaultBgColor}}getCell(t,e){return e<0||e>=this.rows||t<0||t>=this.cols?null:this.cells[e][t]}write(t,e,i,s,a){for(let n=0;n<i.length;n++)this.setCell(t+n,e,i[n],s,a)}fillRect(t,e,i,s,a=" ",n,r){for(let l=e;l<e+s;l++)for(let o=t;o<t+i;o++)this.setCell(o,l,a,n,r)}clear(){this.cells=this.createEmptyGrid()}setFromArray(t){let e=t.width*t.height;if(t.cells.length!==e)throw new Error(`Invalid array length: expected ${e} (${t.width}\xD7${t.height}), got ${t.cells.length}`);let i=0;for(let s=0;s<t.height;s++)for(let a=0;a<t.width;a++){let n=t.cells[i];s<this.rows&&a<this.cols&&this.setCell(a,s,n.char,n.fgColor,n.bgColor),i++}}render(){if(this.fontType==="bitmap"&&this.bitmapFont&&this.useImageDataRendering){this.renderWithImageData();return}this.renderClassic()}renderWithImageData(){if(!this.bitmapFont)return;let t=this.cols*this.bitmapCharWidth,e=this.rows*this.bitmapCharHeight;(!this.imageDataBuffer||this.imageDataBuffer.width!==t||this.imageDataBuffer.height!==e)&&(this.imageDataBuffer=this.ctx.createImageData(t,e));let i=this.imageDataBuffer.data;for(let l=0;l<this.rows;l++)for(let o=0;o<this.cols;o++){let c=this.cells[l][o],d=this.parseColorToRGB(c.bgColor),m=this.parseColorToRGB(c.fgColor),u=c.char.charCodeAt(0),C=this.bitmapFont.get(u);for(let b=0;b<this.bitmapCharHeight;b++)for(let p=0;p<this.bitmapCharWidth;p++){let v=o*this.bitmapCharWidth+p,y=((l*this.bitmapCharHeight+b)*t+v)*4,k=!1;if(C&&b<C.length){let z=C[b],L=1<<7-p;k=(z&L)!==0}k?(i[y]=m.r,i[y+1]=m.g,i[y+2]=m.b,i[y+3]=m.a):(i[y]=d.r,i[y+1]=d.g,i[y+2]=d.b,i[y+3]=d.a)}}this.canvasBgColor!==null?(this.ctx.fillStyle=this.canvasBgColor,this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)):this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.imageSmoothingEnabled=!1,this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges";let s=document.createElement("canvas");s.width=t,s.height=e,s.getContext("2d").putImageData(this.imageDataBuffer,0,0);let n=this.cols*this.cellWidth,r=this.rows*this.cellHeight;this.ctx.drawImage(s,0,0,t,e,this.offsetX,this.offsetY,n,r),this.showDebugGrid&&this.drawDebugGrid()}parseColorToRGB(t){let e=/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/.exec(t);if(e)return{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10),a:e[4]?Math.round(parseFloat(e[4])*255):255};if(t.startsWith("#")){let a=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;t=t.replace(a,(r,l,o,c)=>l+l+o+o+c+c);let n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);if(n)return{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16),a:255}}let s={white:[255,255,255],black:[0,0,0],red:[255,0,0],green:[0,255,0],blue:[0,0,255],yellow:[255,255,0],cyan:[0,255,255],magenta:[255,0,255],transparent:[0,0,0]}[t.toLowerCase()];return s?{r:s[0],g:s[1],b:s[2],a:t==="transparent"?0:255}:{r:255,g:255,b:255,a:255}}renderClassic(){this.canvasBgColor!==null?(this.ctx.fillStyle=this.canvasBgColor,this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)):this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.fontType==="bitmap"||this.fontType==="image"?(this.ctx.imageSmoothingEnabled=!1,this.canvas.style.imageRendering="pixelated",this.canvas.style.imageRendering="crisp-edges"):(this.ctx.imageSmoothingEnabled=!0,this.ctx.imageSmoothingQuality="high",this.canvas.style.imageRendering="auto",this.ctx.font=`${this.fontSize}px ${this.fontFamily}`,this.ctx.textBaseline="top",this.ctx.textRendering="optimizeLegibility");for(let t=0;t<this.rows;t++)for(let e=0;e<this.cols;e++){let i=this.cells[t][e],s=Math.floor(this.offsetX+e*this.cellWidth),a=Math.floor(this.offsetY+t*this.cellHeight),n=Math.floor(this.offsetX+(e+1)*this.cellWidth),r=Math.floor(this.offsetY+(t+1)*this.cellHeight),l=n-s,o=r-a;if((this.canvasBgColor!==null||i.bgColor!==this.defaultBgColor)&&(this.ctx.fillStyle=i.bgColor,this.ctx.fillRect(s,a,l,o)),i.char!==" ")if(this.fontType==="image"&&this.imageAtlas){let d=i.char.charCodeAt(0);this.imageAtlas.drawChar(this.ctx,d,s,a,l,o,i.fgColor)}else if(this.fontType==="bitmap"&&this.bitmapFont)this.drawBitmapChar(i.char,s,a,l,o,i.fgColor);else{this.ctx.fillStyle=i.fgColor;let d=s+(l-this.ctx.measureText(i.char).width)/2,m=a+(o-this.fontSize)/2;this.ctx.fillText(i.char,d,m)}}this.showDebugGrid&&this.drawDebugGrid()}drawBitmapChar(t,e,i,s,a,n){let r=t.charCodeAt(0);if(this.bitmapAtlas){this.bitmapAtlas.drawChar(this.ctx,r,e,i,s,a,n);return}if(!this.bitmapFont)return;let l=this.bitmapFont.get(r);if(!l)return;let o=s/this.bitmapCharWidth,c=a/this.bitmapCharHeight;this.ctx.fillStyle=n;for(let d=0;d<Math.min(l.length,this.bitmapCharHeight);d++){let m=l[d],u=i+d*c,C=i+(d+1)*c;for(let b=0;b<this.bitmapCharWidth;b++){let p=1<<7-b;if(m&p){let v=e+b*o,G=e+(b+1)*o;this.ctx.fillRect(v,u,G-v,C-u)}}}}drawDebugGrid(){if(!this.gridOverlay)return;let t=this.parentElement.clientWidth||800,e=this.parentElement.clientHeight||600;this.gridOverlay.update(this.cols,this.rows,this.cellWidth,this.cellHeight,t,e,this.offsetX,this.offsetY)}getCanvas(){return this.canvas}getContext(){return this.ctx}getDimensions(){return{cols:this.cols,rows:this.rows}}getCellDimensions(){return{cellWidth:this.cellWidth,cellHeight:this.cellHeight}}getCellWidth(){return this.cellWidth}getCellHeight(){return this.cellHeight}getCurrentScale(){return this.currentScale}getScalingMode(){return this.scalingMode}getOffsets(){return{offsetX:this.offsetX,offsetY:this.offsetY}}setDebugGrid(t){this.showDebugGrid=t,t&&!this.gridOverlay?(this.gridOverlay=new x(this.parentElement,{strokeColor:this.debugGridColor,lineWidth:1,zIndex:10}),this.drawDebugGrid()):!t&&this.gridOverlay?(this.gridOverlay.destroy(),this.gridOverlay=void 0):t&&this.gridOverlay&&this.gridOverlay.setVisible(!0)}setCanvasBackgroundColor(t){this.canvasBgColor=t}getCanvasBackgroundColor(){return this.canvasBgColor}setFixedGrid(t,e,i){this.fixedGridMode=!0,this.fixedCols=t,this.fixedRows=e,i!==void 0&&(this.cellAspectRatio=i);let s=this.cols,a=this.rows,n=this.cells;if(this.calculateGridSize(),this.cols!==s||this.rows!==a){this.cells=this.createEmptyGrid();let r=Math.min(s,this.cols),l=Math.min(a,this.rows);for(let o=0;o<l;o++)for(let c=0;c<r;c++)this.cells[o][c]=n[o][c]}this.render()}setAdaptiveGrid(t,e){this.fixedGridMode=!1,this.fixedCols=void 0,this.fixedRows=void 0,t!==void 0&&(this.cellWidth=t),e!==void 0&&(this.cellHeight=e);let i=this.cols,s=this.rows,a=this.cells;if(this.calculateGridSize(),this.cols!==i||this.rows!==s){this.cells=this.createEmptyGrid();let n=Math.min(i,this.cols),r=Math.min(s,this.rows);for(let l=0;l<r;l++)for(let o=0;o<n;o++)this.cells[l][o]=a[l][o]}this.render()}isFixedGridMode(){return this.fixedGridMode}setDebugGridColor(t){this.debugGridColor=t}isDebugGridEnabled(){return this.showDebugGrid}setImageDataRendering(t){this.useImageDataRendering=t,t&&this.fontType==="bitmap"?console.warn("[Render] ImageData rendering enabled (optimized for benchmarks)"):t&&(console.warn("[Render] ImageData rendering requires bitmap font, keeping classic mode"),this.useImageDataRendering=!1)}isImageDataRenderingEnabled(){return this.useImageDataRendering}setWebFont(t,e){this.fontType="web",this.fontFamily=t,e!==void 0&&(this.fontSize=e),this.bitmapFont=void 0,this.bitmapAtlas=void 0,this.useImageDataRendering=!1,this.calculateGridSize(),this.cells=this.createEmptyGrid()}setBitmapFont(t,e,i,s,a){this.fontType="bitmap",this.bitmapFont=t,this.bitmapCharWidth=e,this.bitmapCharHeight=i,this.bitmapAtlas=new M(t,e,i,s,a),this.cellWidth=s,this.cellHeight=a,this.calculateGridSize(),this.cells=this.createEmptyGrid()}async setImageFont(t,e,i,s,a,n){this.fontType="image",this.imageAtlas=new H({glyphWidth:e,glyphHeight:i,cellWidth:s,cellHeight:a,atlasBlocks:n}),await this.imageAtlas.loadFromPNG(t),this.cellWidth=s,this.cellHeight=a,this.bitmapCharWidth=e,this.bitmapCharHeight=i,this.calculateGridSize(),this.cells=this.createEmptyGrid()}getFontType(){return this.fontType}getBitmapFont(){return this.bitmapFont}getBitmapCharDimensions(){return this.fontType!=="bitmap"?null:{width:this.bitmapCharWidth,height:this.bitmapCharHeight}}setPalette(t){this.paletteCache=t}renderDisplayData(t){if(!t||!t.cells||t.cells.length===0){console.warn("[Terminal2D] Empty display data");return}if(t.width===0||t.height===0){console.warn("[Terminal2D] Invalid display dimensions:",t.width,t.height);return}(t.width!==this.cols||t.height!==this.rows)&&this.setFixedGrid(t.width,t.height,this.cellAspectRatio);let e=this.paletteCache??t.palette;if(!e||e.length===0){console.error("[Terminal2D] No palette available (neither cached nor in display)");return}let i=f(s=>{if(s==null||isNaN(s))return console.warn("[Terminal2D] Invalid palette index (undefined/null/NaN):",s),this.defaultFgColor;if(s===255)return"rgba(0, 0, 0, 0)";if(s<0||s>=e.length)return console.warn(`[Terminal2D] Invalid palette index: ${s}`),this.defaultFgColor;let a=e[s];return!a||typeof a.r!="number"?(console.warn(`[Terminal2D] Corrupted palette entry at index ${s}:`,a),this.defaultFgColor):`rgba(${a.r}, ${a.g}, ${a.b}, ${a.a/255})`},"convertColor");for(let s=0;s<t.height&&s<this.rows;s++)for(let a=0;a<t.width&&a<this.cols;a++){let n=s*t.width+a;if(n>=t.cells.length)break;let r=t.cells[n];if(!r)continue;let l=i(r.fgColorIndex),o=i(r.bgColorIndex);this.setCell(a,s,r.char??" ",l,o)}this.render()}isReady(){return!0}getCols(){return this.cols}getRows(){return this.rows}resize(t,e){this.setFixedGrid(t,e)}setScalingMode(t){if(this.scalingMode===t)return;let e=this.cols,i=this.rows;if(this.scalingMode=t,this.calculateGridSize(),this.cols!==e||this.rows!==i){let s=this.cells;if(this.cells=this.createEmptyGrid(),s&&s.length>0){let a=Math.min(e,this.cols),n=Math.min(i,this.rows);for(let r=0;r<n;r++)for(let l=0;l<a;l++)s[r]&&s[r][l]&&(this.cells[r][l]=s[r][l])}}this.render()}setCellSize(t,e){let i=Math.max(1,Math.min(255,Math.round(t))),s=Math.max(1,Math.min(255,Math.round(e)));if(this.cellWidth===i&&this.cellHeight===s)return;let a=this.cols,n=this.rows;if(this.cellWidth=i,this.cellHeight=s,this.customCellSize=!0,this.calculateGridSize(),this.cols!==a||this.rows!==n){let r=this.cells;if(this.cells=this.createEmptyGrid(),r&&r.length>0){let l=Math.min(a,this.cols),o=Math.min(n,this.rows);for(let c=0;c<o;c++)for(let d=0;d<l;d++)r[c]&&r[c][d]&&(this.cells[c][d]=r[c][d])}}this.render()}getCellSize(){return{cellWidth:this.cellWidth,cellHeight:this.cellHeight}}getAvailableSize(){return{width:this.parentElement.clientWidth,height:this.parentElement.clientHeight}}getMaxCells(){return 65536}destroy(){this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=void 0),this.gridOverlay&&(this.gridOverlay.destroy(),this.gridOverlay=void 0),this.containerDiv.parentElement&&this.containerDiv.parentElement.removeChild(this.containerDiv)}};f(T,"Terminal2D");var I=T;var B=["#000000","#800000","#008000","#808000","#000080","#800080","#008080","#c0c0c0","#808080","#ff0000","#00ff00","#ffff00","#0000ff","#ff00ff","#00ffff","#ffffff","#080808","#121212","#1c1c1c","#262626","#303030","#3a3a3a","#444444","#4e4e4e","#585858","#626262","#6c6c6c","#767676","#808080","#8a8a8a","#949494","#9e9e9e","#a80000","#00a800","#a8a800","#0000a8","#a800a8","#00a8a8","#a8a8a8","#545454","#fc5454","#54fc54","#fcfc54","#5454fc","#fc54fc","#54fcfc","#fcfcfc","#000000",...Array(192).fill("#808080")];function $(g,t=B){return g<0||g>=t.length?"#ff00ff":t[g]}f($,"paletteIndexToColor");function U(g,t=B){let e=t.indexOf(g.toLowerCase());return e>=0?e:0}f(U,"colorToPaletteIndex");import{ScalingMode as ft}from"@utsp/types";export{M as BitmapFontAtlas,B as DEFAULT_PALETTE,x as GridOverlay,H as ImageFontAtlas,ft as ScalingMode,I as Terminal2D,U as colorToPaletteIndex,E as getAtlasColumns,W as getCharGridPosition,D as getMaxCharCode,$ as paletteIndexToColor};