textmode.figlet.js 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
import{color as t}from"textmode.js";var e=class t extends Error{constructor(e,n){super(t.formatMessage(e,n)),this.name="FigletError"}static formatMessage(e,n){return n&&0!==Object.keys(n).length?`${e}\n\nContext:${Object.entries(n).map(([e,n])=>`\n - ${e}: ${t.formatValue(n)}`).join("")}`:e}static formatValue(e){return null===e?"null":void 0===e?"undefined":"string"==typeof e?`"${e}"`:"number"==typeof e||"boolean"==typeof e?String(e):Array.isArray(e)?`[${e.map(e=>t.formatValue(e)).join(", ")}]`:"object"==typeof e?JSON.stringify(e):String(e)}},n=[...Array.from({length:95},(t,e)=>e+32),196,214,220,228,246,252,223],r=class{static t(t){if(0===t.length)throw new e("Cannot parse an empty FIGfont file.");const n=t.replace(/\r\n?/g,"\n").split("\n"),r=n[0];if(!r)throw new e("Missing FIGfont header line.");const i=this.i(r);let s=1+i.commentLines;if(s>n.length)throw new e("FIGfont comment line count exceeds file length.",{commentLines:i.commentLines,totalLines:n.length});const o=/* @__PURE__ */new Map;return s=this.o(n,s,i,o),this.l(n,s,i,o),{header:i,characters:o}}static i(t){const n=t.trim().split(/\s+/),r=n[0]??"";if(r.length<6)throw new e("Invalid FIGfont header token.",{headerToken:r});const i=r.slice(0,5),s=r.slice(5);if("flf2a"!==i)throw new e("Unsupported FIGfont signature.",{signature:i});if(1!==s.length)throw new e("FIGfont hardblank must be a single character.",{hardblank:s});if(n.length<6)throw new e("Incomplete FIGfont header.",{headerLine:t});return{signature:i,hardblank:s,height:this.h(n[1],"height"),baseline:this.h(n[2],"baseline"),maxLength:this.h(n[3],"maxLength"),oldLayout:this.h(n[4],"oldLayout"),commentLines:this.h(n[5],"commentLines"),printDirection:this.h(n[6]??"0","printDirection"),fullLayout:this.h(n[7]??"0","fullLayout"),codetagCount:this.h(n[8]??"0","codetagCount")}}static o(t,e,r,i){let s=e;for(const o of n){const e=this.u(t,s,r.height,o);i.set(o,e),s+=r.height}return s}static l(t,e,n,r){let i=e;for(;i<t.length;){const e=t[i]?.trim()??"";if(0===e.length){i+=1;continue}const s=this._(e);i+=1;const o=this.u(t,i,n.height,s);r.set(s,o),i+=n.height}}static u(t,n,r,i){if(n+r>t.length)throw new e("Unexpected end of FIGfont character data.",{codePoint:i,startIndex:n,height:r,totalLines:t.length});const s=[];let o=0;for(let a=0;a<r;a+=1){const r=t[n+a]??"";if(0===r.length)throw new e("Encountered an empty FIGcharacter row.",{codePoint:i,row:a,startIndex:n});const c=r.at(-1);if(!c)throw new e("Failed to determine FIGcharacter endmark.",{codePoint:i,row:a});const l=r.replace(new RegExp(`${this.escapeForRegExp(c)}+$`),"");s.push(l),o=Math.max(o,l.length)}return{code:i,lines:s,width:o}}static _(t){const n=t.split(/\s+/,1)[0];if(/^[-+]?0x[0-9a-f]+$/i.test(n))return Number.parseInt(n,16);if(/^[-+]?0[0-7]+$/.test(n))return Number.parseInt(n,8);if(/^[-+]?\d+$/.test(n))return Number.parseInt(n,10);throw new e("Invalid FIGfont code tag.",{codetagLine:t})}static h(t,n){if(!/^-?\d+$/.test(t))throw new e("Invalid FIGfont header value.",{field:n,token:t});return Number.parseInt(t,10)}static escapeForRegExp(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}},i=class{static RULE_EQUAL_CHARACTER=1;static RULE_UNDERSCORE=2;static RULE_HIERARCHY=4;static RULE_OPPOSITE_PAIR=8;static RULE_BIG_X=16;static RULE_HARDBLANK=32;static VERTICAL_RULE_EQUAL_CHARACTER=256;static VERTICAL_RULE_UNDERSCORE=512;static VERTICAL_RULE_HIERARCHY=1024;static VERTICAL_RULE_HORIZONTAL_LINE=2048;static VERTICAL_RULE_VERTICAL_LINE_SUPERSMUSH=4096;static p(t){return 0!==t.fullLayout?63&t.fullLayout:t.oldLayout>0?63&t.oldLayout:0}static m(t){return 7936&t.fullLayout}static I(t,e,n,r){return" "===t?e:" "===e?t:0===r?this.L(t,e,n):(0!==(r&this.RULE_EQUAL_CHARACTER)?this.C(t,e,n):null)??(0!==(r&this.RULE_UNDERSCORE)?this.R(t,e):null)??(0!==(r&this.RULE_HIERARCHY)?this.v(t,e):null)??(0!==(r&this.RULE_OPPOSITE_PAIR)?this.A(t,e):null)??(0!==(r&this.RULE_BIG_X)?this.F(t,e):null)??(0!==(r&this.RULE_HARDBLANK)?this.T(t,e,n):null)}static L(t,e,n){return" "===t?e:" "===e?t:e}static U(t,e,n){return" "===t?e:" "===e?t:0===n?this.L(t,e," "):(0!==(n&this.VERTICAL_RULE_EQUAL_CHARACTER)?this.C(t,e," "):null)??(0!==(n&this.VERTICAL_RULE_UNDERSCORE)?this.R(t,e):null)??(0!==(n&this.VERTICAL_RULE_HIERARCHY)?this.v(t,e):null)??(0!==(n&this.VERTICAL_RULE_HORIZONTAL_LINE)?this.$(t,e):null)??(0!==(n&this.VERTICAL_RULE_VERTICAL_LINE_SUPERSMUSH)?this.k(t,e):null)}static C(t,e,n){return t===e&&t!==n?t:null}static R(t,e){const n="|/\\[]{}()<>";return"_"===t&&n.includes(e)?e:"_"===e&&n.includes(t)?t:null}static v(t,e){const n=["|","/\\","[]","{}","()","<>"],r=n.findIndex(e=>e.includes(t)),i=n.findIndex(t=>t.includes(e));return-1===r||-1===i||r===i?null:r>i?t:e}static A(t,e){return["[]","][","{}","}{","()",")("].includes(`${t}${e}`)?"|":null}static F(t,e){const n=`${t}${e}`;return"/\\"===n?"|":"\\/"===n?"Y":"><"===n?"X":null}static T(t,e,n){return t===n&&e===n?n:null}static $(t,e){const n=`${t}${e}`;return"-_"===n||"_-"===n?"=":null}static k(t,e){return"|"===t&&"|"===e?"|":null}},s=class{M;S;H;constructor(t){this.M=t,this.S=i.p(t),this.H=i.m(t)}G(t,e){const n=t.map((t,e)=>({inputIndex:e,inputChar:String.fromCodePoint(t.code)})),r=this.N(t,n,e,0),i=Array.from({length:r.rows},()=>Array.from({length:r.cols},()=>" "));for(const s of r.cells)i[s.row][s.col]=s.char;return i}N(t,e,n,r){if(0===t.length)return{lineIndex:r,cells:[],cols:0,rows:this.M.height};let s=this.V(t[0],e[0],r),o=t[0].width;for(let a=1;a<t.length;a+=1){const c=t[a],l=e[a]??{inputIndex:a,inputChar:String.fromCodePoint(c.code)},h=o-this.O(s,o,c,n),u=Math.max(o,h+c.width),f=s.map(t=>Array.from({length:u},(e,n)=>t[n]));for(let t=0;t<this.M.height;t+=1){const e=c.lines[t]??"",n=f[t];for(let s=0;s<c.width;s+=1){const o=e[s]??" ";if(" "===o)continue;const a=h+s,u=n[a],f=this.P(a,t,s,r,c,l,o);if(!u){n[a]=f;continue}const d=i.I(u.char,o,this.M.hardblank,this.S);n[a]=this.D(u,f,d??o)}}s=f,o=u}return this.B(s,o,r)}j(t,e){if(0===t.length)return{cells:[],lines:[],cols:0,rows:0};let n=this.W(t[0]),r=t[0].cols,s=t[0].rows;const o=[this.q(t[0],0)];for(let a=1;a<t.length;a+=1){const c=t[a],l=s-this.X(n,s,r,c,e),h=Math.max(r,c.cols),u=Math.max(s,l+c.rows),f=Array.from({length:u},(t,e)=>Array.from({length:h},(t,r)=>n[e]?.[r]));for(const t of c.cells){const e=l+t.row,n=t.col,r=f[e]?.[n];if(!r){f[e][n]={...t,row:e};continue}const s=i.U(r.char,t.char,this.H);f[e][n]=this.D(r,{...t,row:e},s??t.char)}n=f,r=h,s=u,o.push(this.q(c,l))}return this.Y(n,r,s,o)}O(t,e,n,r){if("full"===r)return 0;for(let i=e;i>=0;i-=1){let s=!0;for(let o=0;o<this.M.height&&s;o+=1){const a=t[o]??[],c=n.lines[o]??"";for(let t=0;t<i;t+=1){const n=a[e-i+t]?.char??" ",o=c[t]??" ";if(!this.J(n,o,r)){s=!1;break}}}if(s)return i}return 0}J(t,e,n){return" "===t||" "===e||"fitted"!==n&&null!==i.I(t,e,this.M.hardblank,this.S)}V(t,e={inputIndex:0,inputChar:String.fromCodePoint(t.code)},n=0){return Array.from({length:this.M.height},(r,i)=>{const s=t.lines[i]??"";return Array.from({length:t.width},(r,o)=>{const a=s[o]??" ";return" "===a?void 0:this.P(o,i,o,n,t,e,a)})})}P(t,e,n,r,i,s,o){return{char:o,col:t,row:e,inputIndex:s.inputIndex,inputChar:s.inputChar,figCharCode:i.code,subRow:e,subCol:n,lineIndex:r}}D(t,e,n){return n===t.char?t:{...e,char:n}}B(t,e,n){const r=[];for(let i=0;i<this.M.height;i+=1){const s=t[i]??[];for(let t=0;t<e;t+=1){const e=s[t];e&&e.char!==this.M.hardblank&&r.push({...e,col:t,row:i,lineIndex:n})}}return{lineIndex:n,cells:r,cols:e,rows:this.M.height}}X(t,e,n,r,i){if("full"===i)return 0;const s=this.W(r),o=Math.min(e,r.rows),a=Math.max(n,r.cols);for(let c=o;c>=0;c-=1){let n=!0;for(let r=0;r<c&&n;r+=1){const o=t[e-c+r]??[],l=s[r]??[];for(let t=0;t<a;t+=1){const e=o[t]?.char??" ",r=l[t]?.char??" ";if(!this.K(e,r,i)){n=!1;break}}}if(n)return c}return 0}K(t,e,n){return" "===t||" "===e||"fitted"!==n&&null!==i.U(t,e,this.H)}W(t){const e=Array.from({length:t.rows},()=>Array.from({length:t.cols},()=>{}));for(const n of t.cells)e[n.row][n.col]=n;return e}q(t,e){return{...t,cells:t.cells.map(t=>({...t,row:t.row+e}))}}Y(t,e,n,r){const i=[];for(let s=0;s<n;s+=1)for(let n=0;n<e;n+=1){const e=t[s]?.[n];e&&i.push({...e,row:s,col:n})}return{cells:i,lines:r,cols:e,rows:n}}},o=class{Z=/* @__PURE__ */new Set;tt(t){this.Z.add(t)}dispose(){for(const t of this.Z)t();this.Z.clear()}},a=class t extends o{et;M;nt;rt;constructor(t,e,n){super(),this.et=t,this.M=e,this.nt=n,this.rt=new s(e)}static async it(t){const n=await fetch(t);if(!n.ok)throw new e("Failed to load FIGfont file.",{source:String(t),status:n.status,statusText:n.statusText});const r=await n.text();return this.st(this.ot(t),r)}static st(e,n){const i=r.t(n);return new t(e,i.header,i.characters)}getCharacter(t){const e="number"==typeof t?t:Array.from(t)[0]?.codePointAt(0);if(void 0!==e)return c(this.nt.get(e))}planText(t,e={}){const n=e.horizontalLayout??this.defaultLayout,r=e.verticalLayout??this.defaultVerticalLayout,i=this.ct(e.direction),s=this.lt(t).map(t=>this.ht(t,i)).flatMap(t=>this.ut(t,n,e)).map((t,e)=>{const r=this.ft(t);return this.rt.N(r.map(t=>t.figChar),r.map(({inputChar:t,inputIndex:e})=>({inputChar:t,inputIndex:e})),n,e)}),o=this.rt.j(s,r);return{cells:o.cells,lines:o.lines,cols:o.cols,rows:o.rows,direction:i}}renderText(t,e={}){const n=this.planText(t,e),r=Array.from({length:n.rows},()=>Array.from({length:n.cols},()=>" "));for(const i of n.cells)r[i.row][i.col]=i.char;return{grid:r,cols:n.cols,rows:n.rows}}measureText(t,e={}){const n=this.planText(t,e);return{cols:n.cols,rows:n.rows}}get name(){return this.et}get header(){return{...this.M}}get characters(){return new Map(Array.from(this.nt,([t,e])=>[t,c(e)]))}get hardblank(){return this.M.hardblank}get height(){return this.M.height}get baseline(){return this.M.baseline}get defaultLayout(){return 128&this.M.fullLayout?"smushed":64&this.M.fullLayout?"fitted":-1===this.M.oldLayout?"full":0===this.M.oldLayout?"fitted":this.M.oldLayout>0?"smushed":"full"}get defaultPrintDirection(){return 1===this.M.printDirection?"rtl":"ltr"}get defaultVerticalLayout(){return 16384&this.M.fullLayout?"smushed":8192&this.M.fullLayout?"fitted":"full"}static ot(t){const e=t instanceof URL?t.pathname:t,n=(e.split(/[?#]/,1)[0]??e).split("/").filter(Boolean).pop()??"figfont";return n.toLowerCase().endsWith(".flf")?n.slice(0,-4)||"figfont":n}lt(t){const e=[[]];for(const[n,r]of Array.from(t).entries())"\n"!==r?e[e.length-1].push({character:r,inputIndex:n}):e.push([]);return e}ht(t,e){return"rtl"===e?[...t].reverse():t}ft(t){const n=t.map(({character:t,inputIndex:e})=>({figChar:this.getCharacter(t)??this.getCharacter(32),inputChar:t,inputIndex:e})),r=n.find(t=>void 0===t.figChar);if(r)throw new e("FIGfont is missing a required character for layout.",{character:r.inputChar,index:r.inputIndex});return n}ut(t,e,n){const r="number"==typeof n.maxCols&&Number.isFinite(n.maxCols)?Math.floor(n.maxCols):void 0,i=n.wrap??"none";return 0===t.length||!r||r<1||"none"===i?[t]:"char"===i?this.dt(t,e,r):this.gt(t,e,r)}gt(t,e,n){const r=this._t(t);if(!r.some(t=>!t.isWhitespace))return[t];const i=[];let s=[],o=[];for(const a of r){if(a.isWhitespace){s.length>0&&(o=a.characters);continue}const t=0===s.length?a.characters:s.concat(o,a.characters);s.length>0&&this.wt(t,e)>n?(i.push(s),s=a.characters.slice()):s=t,o=[]}return 0===s.length?[[]]:(i.push(s),i)}dt(t,e,n){const r=[];let i=[];for(const s of t){const t=i.concat(s);i.length>0&&this.wt(t,e)>n?(r.push(i),i=[s]):i=t}return 0===i.length?[[]]:(r.push(i),r)}wt(t,e){const n=this.ft(t);return this.rt.N(n.map(t=>t.figChar),n.map(({inputChar:t,inputIndex:e})=>({inputChar:t,inputIndex:e})),e,0).cols}_t(t){const e=[];for(const n of t){const t=/\s/u.test(n.character),r=e[e.length-1];r&&r.isWhitespace===t?r.characters.push(n):e.push({isWhitespace:t,characters:[n]})}return e}ct(t){return"ltr"===t||"rtl"===t?t:this.defaultPrintDirection}};function c(t){if(t)return{...t,lines:[...t.lines]}}var l=/* @__PURE__ */new WeakMap;function h(t){let e=l.get(t);return e||(e={activeFont:void 0,align:"left",baseline:"baseline"},l.set(t,e)),e}function u(t,e){if(void 0!==t)return"function"==typeof t?t(e):t}function f(e,n,r){return r instanceof t.TextmodeColor||"string"==typeof r||"number"==typeof r?"charColor"===n?void e.charColor(r):void e.cellColor(r):4===r.length?"charColor"===n?void e.charColor(r[0],r[1],r[2],r[3]):void e.cellColor(r[0],r[1],r[2],r[3]):void("charColor"!==n?e.cellColor(r[0],r[1],r[2]):e.charColor(r[0],r[1],r[2]))}function d(t){const n=h(t).activeFont;if(!n)throw new e("No FIGlet font is active. Call figFont() first.");return n}function g(t,e){t.It?.(e)}var _={name:"textmode.figlet.js",install:(t,e)=>(function(t){t.defineExtension("textmodifier","loadFigFont",{value:async function(t){const e=await a.it(t);return g(this,e),e}}),t.defineExtension("textmodifier","parseFigFont",{value:function(t,e){const n=a.st(t,e);return g(this,n),n}}),t.defineExtension("textmodifier","figFont",{value:function(t){const e=h(this);if(void 0===t)return e.activeFont;e.activeFont=t}}),t.defineExtension("textmodifier","figText",{value:function(t,e,n,r={}){const i=d(this),s=i.planText(t,r),o=h(this),a=e+(c=s.cols,"center"===(l=o.align)?-Math.floor(c/2):"right"===l?-(c-1):0);var c,l;const g=n+(_=s.rows,p=i.baseline,"top"===(w=o.baseline)?0:"center"===w?-Math.floor(_/2):"bottom"===w?-(_-1):-(p-1));var _,p,w;this.push(),this.translate(a,g,0);for(const h of s.cells){this.push(),this.translate(h.col,h.row,0);const t=u(r.charColor,h),e=u(r.cellColor,h);this.char(h.char),void 0!==t&&f(this,"charColor",t),void 0!==e&&f(this,"cellColor",e),this.point(),this.pop()}this.pop()}}),t.defineExtension("textmodifier","figTextWidth",{value:function(t,e={}){return d(this).measureText(t,e).cols}}),t.defineExtension("textmodifier","figTextHeight",{value:function(t,e={}){return d(this).measureText(t,e).rows}}),t.defineExtension("textmodifier","figTextBounds",{value:function(t,e={}){return d(this).measureText(t,e)}}),t.defineExtension("textmodifier","figTextAlign",{value:function(t){const e=h(this);if(void 0===t)return e.align;e.align=t}}),t.defineExtension("textmodifier","figTextBaseline",{value:function(t){const e=h(this);if(void 0===t)return e.baseline;e.baseline=t}})}(e),()=>function(t){l.delete(t)}(t))};"undefined"!=typeof window&&(window.FigletPlugin=_,window.TextmodeFigFont=a,window.FigFontParser=r,window.FigLayoutEngine=s,window.FigSmushRules=i,window.FIGFONT_REQUIRED_CODEPOINTS=n);export{n as FIGFONT_REQUIRED_CODEPOINTS,r as FigFontParser,s as FigLayoutEngine,i as FigSmushRules,_ as FigletPlugin,a as TextmodeFigFont};
|
|
1
|
+
import{color as t}from"textmode.js";var e=class t extends Error{constructor(e,n){super(t.formatMessage(e,n)),this.name="FigletError"}static formatMessage(e,n){return n&&0!==Object.keys(n).length?`${e}\n\nContext:${Object.entries(n).map(([e,n])=>`\n - ${e}: ${t.formatValue(n)}`).join("")}`:e}static formatValue(e){return null===e?"null":void 0===e?"undefined":"string"==typeof e?`"${e}"`:"number"==typeof e||"boolean"==typeof e?String(e):Array.isArray(e)?`[${e.map(e=>t.formatValue(e)).join(", ")}]`:"object"==typeof e?JSON.stringify(e):String(e)}},n=[...Array.from({length:95},(t,e)=>e+32),196,214,220,228,246,252,223],r=class{static t(t){if(0===t.length)throw new e("Cannot parse an empty FIGfont file.");const n=t.replace(/\r\n?/g,"\n").split("\n"),r=n[0];if(!r)throw new e("Missing FIGfont header line.");const i=this.i(r);let s=1+i.commentLines;if(s>n.length)throw new e("FIGfont comment line count exceeds file length.",{commentLines:i.commentLines,totalLines:n.length});const o=/* @__PURE__ */new Map;return s=this.o(n,s,i,o),this.l(n,s,i,o),{header:i,characters:o}}static i(t){const n=t.trim().split(/\s+/),r=n[0]??"";if(r.length<6)throw new e("Invalid FIGfont header token.",{headerToken:r});const i=r.slice(0,5),s=r.slice(5);if("flf2a"!==i)throw new e("Unsupported FIGfont signature.",{signature:i});if(1!==s.length)throw new e("FIGfont hardblank must be a single character.",{hardblank:s});if(n.length<6)throw new e("Incomplete FIGfont header.",{headerLine:t});return{signature:i,hardblank:s,height:this.u(n[1],"height"),baseline:this.u(n[2],"baseline"),maxLength:this.u(n[3],"maxLength"),oldLayout:this.u(n[4],"oldLayout"),commentLines:this.u(n[5],"commentLines"),printDirection:this.u(n[6]??"0","printDirection"),fullLayout:this.u(n[7]??"0","fullLayout"),codetagCount:this.u(n[8]??"0","codetagCount")}}static o(t,e,r,i){let s=e;for(const o of n){const e=this.h(t,s,r.height,o);i.set(o,e),s+=r.height}return s}static l(t,e,n,r){let i=e;for(;i<t.length;){const e=t[i]?.trim()??"";if(0===e.length){i+=1;continue}const s=this.p(e);i+=1;const o=this.h(t,i,n.height,s);r.set(s,o),i+=n.height}}static h(t,n,r,i){if(n+r>t.length)throw new e("Unexpected end of FIGfont character data.",{codePoint:i,startIndex:n,height:r,totalLines:t.length});const s=[];let o=0;for(let a=0;a<r;a+=1){const r=t[n+a]??"";if(0===r.length)throw new e("Encountered an empty FIGcharacter row.",{codePoint:i,row:a,startIndex:n});const c=r.at(-1);if(!c)throw new e("Failed to determine FIGcharacter endmark.",{codePoint:i,row:a});const l=r.replace(new RegExp(`${this.escapeForRegExp(c)}+$`),"");s.push(l),o=Math.max(o,l.length)}return{code:i,lines:s,width:o}}static p(t){const n=t.split(/\s+/,1)[0];if(/^[-+]?0x[0-9a-f]+$/i.test(n))return Number.parseInt(n,16);if(/^[-+]?0[0-7]+$/.test(n))return Number.parseInt(n,8);if(/^[-+]?\d+$/.test(n))return Number.parseInt(n,10);throw new e("Invalid FIGfont code tag.",{codetagLine:t})}static u(t,n){if(!/^-?\d+$/.test(t))throw new e("Invalid FIGfont header value.",{field:n,token:t});return Number.parseInt(t,10)}static escapeForRegExp(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}},i=class{static RULE_EQUAL_CHARACTER=1;static RULE_UNDERSCORE=2;static RULE_HIERARCHY=4;static RULE_OPPOSITE_PAIR=8;static RULE_BIG_X=16;static RULE_HARDBLANK=32;static VERTICAL_RULE_EQUAL_CHARACTER=256;static VERTICAL_RULE_UNDERSCORE=512;static VERTICAL_RULE_HIERARCHY=1024;static VERTICAL_RULE_HORIZONTAL_LINE=2048;static VERTICAL_RULE_VERTICAL_LINE_SUPERSMUSH=4096;static _(t){return 0!==t.fullLayout?63&t.fullLayout:t.oldLayout>0?63&t.oldLayout:0}static m(t){return 7936&t.fullLayout}static I(t,e,n,r){return" "===t?e:" "===e?t:0===r?this.L(t,e,n):(0!==(r&this.RULE_EQUAL_CHARACTER)?this.C(t,e,n):null)??(0!==(r&this.RULE_UNDERSCORE)?this.R(t,e):null)??(0!==(r&this.RULE_HIERARCHY)?this.v(t,e):null)??(0!==(r&this.RULE_OPPOSITE_PAIR)?this.A(t,e):null)??(0!==(r&this.RULE_BIG_X)?this.F(t,e):null)??(0!==(r&this.RULE_HARDBLANK)?this.T(t,e,n):null)}static L(t,e,n){return" "===t?e:" "===e?t:e}static U(t,e,n){return" "===t?e:" "===e?t:0===n?this.L(t,e," "):(0!==(n&this.VERTICAL_RULE_EQUAL_CHARACTER)?this.C(t,e," "):null)??(0!==(n&this.VERTICAL_RULE_UNDERSCORE)?this.R(t,e):null)??(0!==(n&this.VERTICAL_RULE_HIERARCHY)?this.v(t,e):null)??(0!==(n&this.VERTICAL_RULE_HORIZONTAL_LINE)?this.S(t,e):null)??(0!==(n&this.VERTICAL_RULE_VERTICAL_LINE_SUPERSMUSH)?this.$(t,e):null)}static C(t,e,n){return t===e&&t!==n?t:null}static R(t,e){const n="|/\\[]{}()<>";return"_"===t&&n.includes(e)?e:"_"===e&&n.includes(t)?t:null}static v(t,e){const n=["|","/\\","[]","{}","()","<>"],r=n.findIndex(e=>e.includes(t)),i=n.findIndex(t=>t.includes(e));return-1===r||-1===i||r===i?null:r>i?t:e}static A(t,e){return["[]","][","{}","}{","()",")("].includes(`${t}${e}`)?"|":null}static F(t,e){const n=`${t}${e}`;return"/\\"===n?"|":"\\/"===n?"Y":"><"===n?"X":null}static T(t,e,n){return t===n&&e===n?n:null}static S(t,e){const n=`${t}${e}`;return"-_"===n||"_-"===n?"=":null}static $(t,e){return"|"===t&&"|"===e?"|":null}},s=class{k;H;M;constructor(t){this.k=t,this.H=i._(t),this.M=i.m(t)}G(t,e){const n=t.map((t,e)=>({inputIndex:e,inputChar:String.fromCodePoint(t.code)})),r=this.N(t,n,e,0),i=Array.from({length:r.rows},()=>Array.from({length:r.cols},()=>" "));for(const s of r.cells)i[s.row][s.col]=s.char;return i}N(t,e,n,r){if(0===t.length)return{lineIndex:r,cells:[],cols:0,rows:this.k.height};let s=this.V(t[0],e[0],r),o=t[0].width;for(let a=1;a<t.length;a+=1){const c=t[a],l=e[a]??{inputIndex:a,inputChar:String.fromCodePoint(c.code)},u=o-this.O(s,o,c,n),h=Math.max(o,u+c.width),f=s.map(t=>Array.from({length:h},(e,n)=>t[n]));for(let t=0;t<this.k.height;t+=1){const e=c.lines[t]??"",n=f[t];for(let s=0;s<c.width;s+=1){const o=e[s]??" ";if(" "===o)continue;const a=u+s,h=n[a],f=this.P(a,t,s,r,c,l,o);if(!h){n[a]=f;continue}const d=i.I(h.char,o,this.k.hardblank,this.H);n[a]=this.D(h,f,d??o)}}s=f,o=h}return this.B(s,o,r)}j(t,e){if(0===t.length)return{cells:[],lines:[],cols:0,rows:0};let n=this.W(t[0]),r=t[0].cols,s=t[0].rows;const o=[this.q(t[0],0)];for(let a=1;a<t.length;a+=1){const c=t[a],l=s-this.X(n,s,r,c,e),u=Math.max(r,c.cols),h=Math.max(s,l+c.rows),f=Array.from({length:h},(t,e)=>Array.from({length:u},(t,r)=>n[e]?.[r]));for(const t of c.cells){const e=l+t.row,n=t.col,r=f[e]?.[n];if(!r){f[e][n]={...t,row:e};continue}const s=i.U(r.char,t.char,this.M);f[e][n]=this.D(r,{...t,row:e},s??t.char)}n=f,r=u,s=h,o.push(this.q(c,l))}return this.Y(n,r,s,o)}O(t,e,n,r){if("full"===r)return 0;for(let i=e;i>=0;i-=1){let s=!0;for(let o=0;o<this.k.height&&s;o+=1){const a=t[o]??[],c=n.lines[o]??"";for(let t=0;t<i;t+=1){const n=a[e-i+t]?.char??" ",o=c[t]??" ";if(!this.J(n,o,r)){s=!1;break}}}if(s)return i}return 0}J(t,e,n){return" "===t||" "===e||"fitted"!==n&&null!==i.I(t,e,this.k.hardblank,this.H)}V(t,e={inputIndex:0,inputChar:String.fromCodePoint(t.code)},n=0){return Array.from({length:this.k.height},(r,i)=>{const s=t.lines[i]??"";return Array.from({length:t.width},(r,o)=>{const a=s[o]??" ";return" "===a?void 0:this.P(o,i,o,n,t,e,a)})})}P(t,e,n,r,i,s,o){return{char:o,col:t,row:e,inputIndex:s.inputIndex,inputChar:s.inputChar,figCharCode:i.code,subRow:e,subCol:n,lineIndex:r}}D(t,e,n){return n===t.char?t:{...e,char:n}}B(t,e,n){const r=[];for(let i=0;i<this.k.height;i+=1){const s=t[i]??[];for(let t=0;t<e;t+=1){const e=s[t];e&&e.char!==this.k.hardblank&&r.push({...e,col:t,row:i,lineIndex:n})}}return{lineIndex:n,cells:r,cols:e,rows:this.k.height}}X(t,e,n,r,i){if("full"===i)return 0;const s=this.W(r),o=Math.min(e,r.rows),a=Math.max(n,r.cols);for(let c=o;c>=0;c-=1){let n=!0;for(let r=0;r<c&&n;r+=1){const o=t[e-c+r]??[],l=s[r]??[];for(let t=0;t<a;t+=1){const e=o[t]?.char??" ",r=l[t]?.char??" ";if(!this.K(e,r,i)){n=!1;break}}}if(n)return c}return 0}K(t,e,n){return" "===t||" "===e||"fitted"!==n&&null!==i.U(t,e,this.M)}W(t){const e=Array.from({length:t.rows},()=>Array.from({length:t.cols},()=>{}));for(const n of t.cells)e[n.row][n.col]=n;return e}q(t,e){return{...t,cells:t.cells.map(t=>({...t,row:t.row+e}))}}Y(t,e,n,r){const i=[];for(let s=0;s<n;s+=1)for(let n=0;n<e;n+=1){const e=t[s]?.[n];e&&i.push({...e,row:s,col:n})}return{cells:i,lines:r,cols:e,rows:n}}},o=class{Z=/* @__PURE__ */new Set;tt(t){this.Z.add(t)}dispose(){for(const t of this.Z)t();this.Z.clear()}},a=class t extends o{et;k;nt;rt;constructor(t,e,n){super(),this.et=t,this.k=e,this.nt=n,this.rt=new s(e)}static async it(t){const n=await fetch(t);if(!n.ok)throw new e("Failed to load FIGfont file.",{source:String(t),status:n.status,statusText:n.statusText});const r=await n.text();return this.st(this.ot(t),r)}static st(e,n){const i=r.t(n);return new t(e,i.header,i.characters)}getCharacter(t){const e="number"==typeof t?t:Array.from(t)[0]?.codePointAt(0);if(void 0!==e)return c(this.nt.get(e))}planText(t,e={}){const n=e.horizontalLayout??this.defaultLayout,r=e.verticalLayout??this.defaultVerticalLayout,i=this.ct(e.direction),s=this.lt(t).map(t=>this.ut(t,i)).flatMap(t=>this.ht(t,n,e)).map((t,e)=>{const r=this.ft(t);return this.rt.N(r.map(t=>t.figChar),r.map(({inputChar:t,inputIndex:e})=>({inputChar:t,inputIndex:e})),n,e)}),o=this.rt.j(s,r);return{cells:o.cells,lines:o.lines,cols:o.cols,rows:o.rows,direction:i}}renderText(t,e={}){const n=this.planText(t,e),r=Array.from({length:n.rows},()=>Array.from({length:n.cols},()=>" "));for(const i of n.cells)r[i.row][i.col]=i.char;return{grid:r,cols:n.cols,rows:n.rows}}measureText(t,e={}){const n=this.planText(t,e);return{cols:n.cols,rows:n.rows}}get name(){return this.et}get header(){return{...this.k}}get characters(){return new Map(Array.from(this.nt,([t,e])=>[t,c(e)]))}get hardblank(){return this.k.hardblank}get height(){return this.k.height}get baseline(){return this.k.baseline}get defaultLayout(){return 128&this.k.fullLayout?"smushed":64&this.k.fullLayout?"fitted":-1===this.k.oldLayout?"full":0===this.k.oldLayout?"fitted":this.k.oldLayout>0?"smushed":"full"}get defaultPrintDirection(){return 1===this.k.printDirection?"rtl":"ltr"}get defaultVerticalLayout(){return 16384&this.k.fullLayout?"smushed":8192&this.k.fullLayout?"fitted":"full"}static ot(t){const e=t instanceof URL?t.pathname:t,n=(e.split(/[?#]/,1)[0]??e).split("/").filter(Boolean).pop()??"figfont";return n.toLowerCase().endsWith(".flf")?n.slice(0,-4)||"figfont":n}lt(t){const e=[[]];for(const[n,r]of Array.from(t).entries())"\n"!==r?e[e.length-1].push({character:r,inputIndex:n}):e.push([]);return e}ut(t,e){return"rtl"===e?[...t].reverse():t}ft(t){const n=t.map(({character:t,inputIndex:e})=>({figChar:this.getCharacter(t)??this.getCharacter(32),inputChar:t,inputIndex:e})),r=n.find(t=>void 0===t.figChar);if(r)throw new e("FIGfont is missing a required character for layout.",{character:r.inputChar,index:r.inputIndex});return n}ht(t,e,n){const r="number"==typeof n.maxCols&&Number.isFinite(n.maxCols)?Math.floor(n.maxCols):void 0,i=n.wrap??"none";return 0===t.length||!r||r<1||"none"===i?[t]:"char"===i?this.dt(t,e,r):this.wt(t,e,r)}wt(t,e,n){const r=this.gt(t);if(!r.some(t=>!t.isWhitespace))return[t];const i=[];let s=[],o=[];for(const a of r){if(a.isWhitespace){s.length>0&&(o=a.characters);continue}const t=0===s.length?a.characters:s.concat(o,a.characters);s.length>0&&this._t(t,e)>n?(i.push(s),s=a.characters.slice()):s=t,o=[]}return 0===s.length?[[]]:(i.push(s),i)}dt(t,e,n){const r=[];let i=[];for(const s of t){const t=i.concat(s);i.length>0&&this._t(t,e)>n?(r.push(i),i=[s]):i=t}return 0===i.length?[[]]:(r.push(i),r)}_t(t,e){const n=this.ft(t);return this.rt.N(n.map(t=>t.figChar),n.map(({inputChar:t,inputIndex:e})=>({inputChar:t,inputIndex:e})),e,0).cols}gt(t){const e=[];for(const n of t){const t=/\s/u.test(n.character),r=e[e.length-1];r&&r.isWhitespace===t?r.characters.push(n):e.push({isWhitespace:t,characters:[n]})}return e}ct(t){return"ltr"===t||"rtl"===t?t:this.defaultPrintDirection}};function c(t){if(t)return{...t,lines:[...t.lines]}}function l(t,e){return u(t),t.ownedFonts.add(e),e}function u(t){if(t.disposed)throw new e("FIGlet plugin has been disposed.")}function h(t){if(!t.disposed){t.disposed=!0,t.activeFont=void 0;for(const e of t.ownedFonts)e.dispose();t.ownedFonts.clear()}}function f(t,e){if(void 0!==t)return"function"==typeof t?t(e):t}function d(e,n,r){return r instanceof t.TextmodeColor||"string"==typeof r||"number"==typeof r?"charColor"===n?void e.charColor(r):void e.cellColor(r):4===r.length?"charColor"===n?void e.charColor(r[0],r[1],r[2],r[3]):void e.cellColor(r[0],r[1],r[2],r[3]):void("charColor"!==n?e.cellColor(r[0],r[1],r[2]):e.charColor(r[0],r[1],r[2]))}function w(t){const n=t.activeFont;if(!n)throw new e("No FIGlet font is active. Call figFont() first.");return n}var g={name:"textmode.figlet.js",install(t,e){const n={activeFont:void 0,align:"left",baseline:"baseline",ownedFonts:/* @__PURE__ */new Set,disposed:!1};try{!function(t,e){t.defineExtension("textmodifier","loadFigFont",{value:async function(t){u(e);const n=await a.it(t);try{return l(e,n)}catch(r){throw n.dispose(),r}}}),t.defineExtension("textmodifier","parseFigFont",{value:function(t,n){const r=a.st(t,n);try{return l(e,r)}catch(i){throw r.dispose(),i}}}),t.defineExtension("textmodifier","figFont",{value:function(t){if(void 0===t)return e.activeFont;e.activeFont=t}}),t.defineExtension("textmodifier","figText",{value:function(t,n,r,i={}){const s=w(e),o=s.planText(t,i),a=n+(c=o.cols,"center"===(l=e.align)?-Math.floor(c/2):"right"===l?-(c-1):0);var c,l;const u=r+(h=o.rows,g=s.baseline,"top"===(p=e.baseline)?0:"center"===p?-Math.floor(h/2):"bottom"===p?-(h-1):-(g-1));var h,g,p;this.push();try{this.translate(a,u,0);for(const t of o.cells){this.push();try{this.translate(t.col,t.row,0);const e=f(i.charColor,t),n=f(i.cellColor,t);this.char(t.char),void 0!==e&&d(this,"charColor",e),void 0!==n&&d(this,"cellColor",n),this.point()}finally{this.pop()}}}finally{this.pop()}}}),t.defineExtension("textmodifier","figTextWidth",{value:function(t,n={}){return w(e).measureText(t,n).cols}}),t.defineExtension("textmodifier","figTextHeight",{value:function(t,n={}){return w(e).measureText(t,n).rows}}),t.defineExtension("textmodifier","figTextBounds",{value:function(t,n={}){return w(e).measureText(t,n)}}),t.defineExtension("textmodifier","figTextAlign",{value:function(t){if(void 0===t)return e.align;e.align=t}}),t.defineExtension("textmodifier","figTextBaseline",{value:function(t){if(void 0===t)return e.baseline;e.baseline=t}})}(e,n)}catch(r){throw h(n),r}return()=>h(n)}};"undefined"!=typeof window&&(window.FigletPlugin=g,window.TextmodeFigFont=a,window.FigFontParser=r,window.FigLayoutEngine=s,window.FigSmushRules=i,window.FIGFONT_REQUIRED_CODEPOINTS=n);export{n as FIGFONT_REQUIRED_CODEPOINTS,r as FigFontParser,s as FigLayoutEngine,i as FigSmushRules,g as FigletPlugin,a as TextmodeFigFont};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("textmode.js")):"function"==typeof define&&define.amd?define(["exports","textmode.js"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TextmodeFiglet={},t.textmode)}(this,function(t,e){Object.defineProperty(t,Symbol.toStringTag,{value:"Module"});var n=class t extends Error{constructor(e,n){super(t.formatMessage(e,n)),this.name="FigletError"}static formatMessage(e,n){return n&&0!==Object.keys(n).length?`${e}\n\nContext:${Object.entries(n).map(([e,n])=>`\n - ${e}: ${t.formatValue(n)}`).join("")}`:e}static formatValue(e){return null===e?"null":void 0===e?"undefined":"string"==typeof e?`"${e}"`:"number"==typeof e||"boolean"==typeof e?String(e):Array.isArray(e)?`[${e.map(e=>t.formatValue(e)).join(", ")}]`:"object"==typeof e?JSON.stringify(e):String(e)}},r=[...Array.from({length:95},(t,e)=>e+32),196,214,220,228,246,252,223],i=class{static t(t){if(0===t.length)throw new n("Cannot parse an empty FIGfont file.");const e=t.replace(/\r\n?/g,"\n").split("\n"),r=e[0];if(!r)throw new n("Missing FIGfont header line.");const i=this.i(r);let s=1+i.commentLines;if(s>e.length)throw new n("FIGfont comment line count exceeds file length.",{commentLines:i.commentLines,totalLines:e.length});const o=new Map;return s=this.o(e,s,i,o),this.l(e,s,i,o),{header:i,characters:o}}static i(t){const e=t.trim().split(/\s+/),r=e[0]??"";if(r.length<6)throw new n("Invalid FIGfont header token.",{headerToken:r});const i=r.slice(0,5),s=r.slice(5);if("flf2a"!==i)throw new n("Unsupported FIGfont signature.",{signature:i});if(1!==s.length)throw new n("FIGfont hardblank must be a single character.",{hardblank:s});if(e.length<6)throw new n("Incomplete FIGfont header.",{headerLine:t});return{signature:i,hardblank:s,height:this.u(e[1],"height"),baseline:this.u(e[2],"baseline"),maxLength:this.u(e[3],"maxLength"),oldLayout:this.u(e[4],"oldLayout"),commentLines:this.u(e[5],"commentLines"),printDirection:this.u(e[6]??"0","printDirection"),fullLayout:this.u(e[7]??"0","fullLayout"),codetagCount:this.u(e[8]??"0","codetagCount")}}static o(t,e,n,i){let s=e;for(const o of r){const e=this.h(t,s,n.height,o);i.set(o,e),s+=n.height}return s}static l(t,e,n,r){let i=e;for(;i<t.length;){const e=t[i]?.trim()??"";if(0===e.length){i+=1;continue}const s=this.p(e);i+=1;const o=this.h(t,i,n.height,s);r.set(s,o),i+=n.height}}static h(t,e,r,i){if(e+r>t.length)throw new n("Unexpected end of FIGfont character data.",{codePoint:i,startIndex:e,height:r,totalLines:t.length});const s=[];let o=0;for(let a=0;a<r;a+=1){const r=t[e+a]??"";if(0===r.length)throw new n("Encountered an empty FIGcharacter row.",{codePoint:i,row:a,startIndex:e});const l=r.at(-1);if(!l)throw new n("Failed to determine FIGcharacter endmark.",{codePoint:i,row:a});const c=r.replace(new RegExp(`${this.escapeForRegExp(l)}+$`),"");s.push(c),o=Math.max(o,c.length)}return{code:i,lines:s,width:o}}static p(t){const e=t.split(/\s+/,1)[0];if(/^[-+]?0x[0-9a-f]+$/i.test(e))return Number.parseInt(e,16);if(/^[-+]?0[0-7]+$/.test(e))return Number.parseInt(e,8);if(/^[-+]?\d+$/.test(e))return Number.parseInt(e,10);throw new n("Invalid FIGfont code tag.",{codetagLine:t})}static u(t,e){if(!/^-?\d+$/.test(t))throw new n("Invalid FIGfont header value.",{field:e,token:t});return Number.parseInt(t,10)}static escapeForRegExp(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}},s=class{static RULE_EQUAL_CHARACTER=1;static RULE_UNDERSCORE=2;static RULE_HIERARCHY=4;static RULE_OPPOSITE_PAIR=8;static RULE_BIG_X=16;static RULE_HARDBLANK=32;static VERTICAL_RULE_EQUAL_CHARACTER=256;static VERTICAL_RULE_UNDERSCORE=512;static VERTICAL_RULE_HIERARCHY=1024;static VERTICAL_RULE_HORIZONTAL_LINE=2048;static VERTICAL_RULE_VERTICAL_LINE_SUPERSMUSH=4096;static _(t){return 0!==t.fullLayout?63&t.fullLayout:t.oldLayout>0?63&t.oldLayout:0}static m(t){return 7936&t.fullLayout}static I(t,e,n,r){return" "===t?e:" "===e?t:0===r?this.L(t,e,n):(0!==(r&this.RULE_EQUAL_CHARACTER)?this.C(t,e,n):null)??(0!==(r&this.RULE_UNDERSCORE)?this.v(t,e):null)??(0!==(r&this.RULE_HIERARCHY)?this.R(t,e):null)??(0!==(r&this.RULE_OPPOSITE_PAIR)?this.A(t,e):null)??(0!==(r&this.RULE_BIG_X)?this.F(t,e):null)??(0!==(r&this.RULE_HARDBLANK)?this.T(t,e,n):null)}static L(t,e,n){return" "===t?e:" "===e?t:e}static U(t,e,n){return" "===t?e:" "===e?t:0===n?this.L(t,e," "):(0!==(n&this.VERTICAL_RULE_EQUAL_CHARACTER)?this.C(t,e," "):null)??(0!==(n&this.VERTICAL_RULE_UNDERSCORE)?this.v(t,e):null)??(0!==(n&this.VERTICAL_RULE_HIERARCHY)?this.R(t,e):null)??(0!==(n&this.VERTICAL_RULE_HORIZONTAL_LINE)?this.M(t,e):null)??(0!==(n&this.VERTICAL_RULE_VERTICAL_LINE_SUPERSMUSH)?this.S(t,e):null)}static C(t,e,n){return t===e&&t!==n?t:null}static v(t,e){const n="|/\\[]{}()<>";return"_"===t&&n.includes(e)?e:"_"===e&&n.includes(t)?t:null}static R(t,e){const n=["|","/\\","[]","{}","()","<>"],r=n.findIndex(e=>e.includes(t)),i=n.findIndex(t=>t.includes(e));return-1===r||-1===i||r===i?null:r>i?t:e}static A(t,e){return["[]","][","{}","}{","()",")("].includes(`${t}${e}`)?"|":null}static F(t,e){const n=`${t}${e}`;return"/\\"===n?"|":"\\/"===n?"Y":"><"===n?"X":null}static T(t,e,n){return t===n&&e===n?n:null}static M(t,e){const n=`${t}${e}`;return"-_"===n||"_-"===n?"=":null}static S(t,e){return"|"===t&&"|"===e?"|":null}},o=class{$;k;H;constructor(t){this.$=t,this.k=s._(t),this.H=s.m(t)}G(t,e){const n=t.map((t,e)=>({inputIndex:e,inputChar:String.fromCodePoint(t.code)})),r=this.N(t,n,e,0),i=Array.from({length:r.rows},()=>Array.from({length:r.cols},()=>" "));for(const s of r.cells)i[s.row][s.col]=s.char;return i}N(t,e,n,r){if(0===t.length)return{lineIndex:r,cells:[],cols:0,rows:this.$.height};let i=this.O(t[0],e[0],r),o=t[0].width;for(let a=1;a<t.length;a+=1){const l=t[a],c=e[a]??{inputIndex:a,inputChar:String.fromCodePoint(l.code)},u=o-this.V(i,o,l,n),h=Math.max(o,u+l.width),f=i.map(t=>Array.from({length:h},(e,n)=>t[n]));for(let t=0;t<this.$.height;t+=1){const e=l.lines[t]??"",n=f[t];for(let i=0;i<l.width;i+=1){const o=e[i]??" ";if(" "===o)continue;const a=u+i,h=n[a],f=this.P(a,t,i,r,l,c,o);if(!h){n[a]=f;continue}const d=s.I(h.char,o,this.$.hardblank,this.k);n[a]=this.D(h,f,d??o)}}i=f,o=h}return this.B(i,o,r)}j(t,e){if(0===t.length)return{cells:[],lines:[],cols:0,rows:0};let n=this.W(t[0]),r=t[0].cols,i=t[0].rows;const o=[this.q(t[0],0)];for(let a=1;a<t.length;a+=1){const l=t[a],c=i-this.X(n,i,r,l,e),u=Math.max(r,l.cols),h=Math.max(i,c+l.rows),f=Array.from({length:h},(t,e)=>Array.from({length:u},(t,r)=>n[e]?.[r]));for(const t of l.cells){const e=c+t.row,n=t.col,r=f[e]?.[n];if(!r){f[e][n]={...t,row:e};continue}const i=s.U(r.char,t.char,this.H);f[e][n]=this.D(r,{...t,row:e},i??t.char)}n=f,r=u,i=h,o.push(this.q(l,c))}return this.Y(n,r,i,o)}V(t,e,n,r){if("full"===r)return 0;for(let i=e;i>=0;i-=1){let s=!0;for(let o=0;o<this.$.height&&s;o+=1){const a=t[o]??[],l=n.lines[o]??"";for(let t=0;t<i;t+=1){const n=a[e-i+t]?.char??" ",o=l[t]??" ";if(!this.J(n,o,r)){s=!1;break}}}if(s)return i}return 0}J(t,e,n){return" "===t||" "===e||"fitted"!==n&&null!==s.I(t,e,this.$.hardblank,this.k)}O(t,e={inputIndex:0,inputChar:String.fromCodePoint(t.code)},n=0){return Array.from({length:this.$.height},(r,i)=>{const s=t.lines[i]??"";return Array.from({length:t.width},(r,o)=>{const a=s[o]??" ";return" "===a?void 0:this.P(o,i,o,n,t,e,a)})})}P(t,e,n,r,i,s,o){return{char:o,col:t,row:e,inputIndex:s.inputIndex,inputChar:s.inputChar,figCharCode:i.code,subRow:e,subCol:n,lineIndex:r}}D(t,e,n){return n===t.char?t:{...e,char:n}}B(t,e,n){const r=[];for(let i=0;i<this.$.height;i+=1){const s=t[i]??[];for(let t=0;t<e;t+=1){const e=s[t];e&&e.char!==this.$.hardblank&&r.push({...e,col:t,row:i,lineIndex:n})}}return{lineIndex:n,cells:r,cols:e,rows:this.$.height}}X(t,e,n,r,i){if("full"===i)return 0;const s=this.W(r),o=Math.min(e,r.rows),a=Math.max(n,r.cols);for(let l=o;l>=0;l-=1){let n=!0;for(let r=0;r<l&&n;r+=1){const o=t[e-l+r]??[],c=s[r]??[];for(let t=0;t<a;t+=1){const e=o[t]?.char??" ",r=c[t]?.char??" ";if(!this.K(e,r,i)){n=!1;break}}}if(n)return l}return 0}K(t,e,n){return" "===t||" "===e||"fitted"!==n&&null!==s.U(t,e,this.H)}W(t){const e=Array.from({length:t.rows},()=>Array.from({length:t.cols},()=>{}));for(const n of t.cells)e[n.row][n.col]=n;return e}q(t,e){return{...t,cells:t.cells.map(t=>({...t,row:t.row+e}))}}Y(t,e,n,r){const i=[];for(let s=0;s<n;s+=1)for(let n=0;n<e;n+=1){const e=t[s]?.[n];e&&i.push({...e,row:s,col:n})}return{cells:i,lines:r,cols:e,rows:n}}},a=class{Z=new Set;tt(t){this.Z.add(t)}dispose(){for(const t of this.Z)t();this.Z.clear()}},l=class t extends a{et;$;nt;rt;constructor(t,e,n){super(),this.et=t,this.$=e,this.nt=n,this.rt=new o(e)}static async it(t){const e=await fetch(t);if(!e.ok)throw new n("Failed to load FIGfont file.",{source:String(t),status:e.status,statusText:e.statusText});const r=await e.text();return this.st(this.ot(t),r)}static st(e,n){const r=i.t(n);return new t(e,r.header,r.characters)}getCharacter(t){const e="number"==typeof t?t:Array.from(t)[0]?.codePointAt(0);if(void 0!==e)return c(this.nt.get(e))}planText(t,e={}){const n=e.horizontalLayout??this.defaultLayout,r=e.verticalLayout??this.defaultVerticalLayout,i=this.lt(e.direction),s=this.ct(t).map(t=>this.ut(t,i)).flatMap(t=>this.ht(t,n,e)).map((t,e)=>{const r=this.ft(t);return this.rt.N(r.map(t=>t.figChar),r.map(({inputChar:t,inputIndex:e})=>({inputChar:t,inputIndex:e})),n,e)}),o=this.rt.j(s,r);return{cells:o.cells,lines:o.lines,cols:o.cols,rows:o.rows,direction:i}}renderText(t,e={}){const n=this.planText(t,e),r=Array.from({length:n.rows},()=>Array.from({length:n.cols},()=>" "));for(const i of n.cells)r[i.row][i.col]=i.char;return{grid:r,cols:n.cols,rows:n.rows}}measureText(t,e={}){const n=this.planText(t,e);return{cols:n.cols,rows:n.rows}}get name(){return this.et}get header(){return{...this.$}}get characters(){return new Map(Array.from(this.nt,([t,e])=>[t,c(e)]))}get hardblank(){return this.$.hardblank}get height(){return this.$.height}get baseline(){return this.$.baseline}get defaultLayout(){return 128&this.$.fullLayout?"smushed":64&this.$.fullLayout?"fitted":-1===this.$.oldLayout?"full":0===this.$.oldLayout?"fitted":this.$.oldLayout>0?"smushed":"full"}get defaultPrintDirection(){return 1===this.$.printDirection?"rtl":"ltr"}get defaultVerticalLayout(){return 16384&this.$.fullLayout?"smushed":8192&this.$.fullLayout?"fitted":"full"}static ot(t){const e=t instanceof URL?t.pathname:t,n=(e.split(/[?#]/,1)[0]??e).split("/").filter(Boolean).pop()??"figfont";return n.toLowerCase().endsWith(".flf")?n.slice(0,-4)||"figfont":n}ct(t){const e=[[]];for(const[n,r]of Array.from(t).entries())"\n"!==r?e[e.length-1].push({character:r,inputIndex:n}):e.push([]);return e}ut(t,e){return"rtl"===e?[...t].reverse():t}ft(t){const e=t.map(({character:t,inputIndex:e})=>({figChar:this.getCharacter(t)??this.getCharacter(32),inputChar:t,inputIndex:e})),r=e.find(t=>void 0===t.figChar);if(r)throw new n("FIGfont is missing a required character for layout.",{character:r.inputChar,index:r.inputIndex});return e}ht(t,e,n){const r="number"==typeof n.maxCols&&Number.isFinite(n.maxCols)?Math.floor(n.maxCols):void 0,i=n.wrap??"none";return 0===t.length||!r||r<1||"none"===i?[t]:"char"===i?this.dt(t,e,r):this.gt(t,e,r)}gt(t,e,n){const r=this._t(t);if(!r.some(t=>!t.isWhitespace))return[t];const i=[];let s=[],o=[];for(const a of r){if(a.isWhitespace){s.length>0&&(o=a.characters);continue}const t=0===s.length?a.characters:s.concat(o,a.characters);s.length>0&&this.wt(t,e)>n?(i.push(s),s=a.characters.slice()):s=t,o=[]}return 0===s.length?[[]]:(i.push(s),i)}dt(t,e,n){const r=[];let i=[];for(const s of t){const t=i.concat(s);i.length>0&&this.wt(t,e)>n?(r.push(i),i=[s]):i=t}return 0===i.length?[[]]:(r.push(i),r)}wt(t,e){const n=this.ft(t);return this.rt.N(n.map(t=>t.figChar),n.map(({inputChar:t,inputIndex:e})=>({inputChar:t,inputIndex:e})),e,0).cols}_t(t){const e=[];for(const n of t){const t=/\s/u.test(n.character),r=e[e.length-1];r&&r.isWhitespace===t?r.characters.push(n):e.push({isWhitespace:t,characters:[n]})}return e}lt(t){return"ltr"===t||"rtl"===t?t:this.defaultPrintDirection}};function c(t){if(t)return{...t,lines:[...t.lines]}}var u=new WeakMap;function h(t){let e=u.get(t);return e||(e={activeFont:void 0,align:"left",baseline:"baseline"},u.set(t,e)),e}function f(t,e){if(void 0!==t)return"function"==typeof t?t(e):t}function d(t,n,r){return r instanceof e.color.TextmodeColor||"string"==typeof r||"number"==typeof r?"charColor"===n?void t.charColor(r):void t.cellColor(r):4===r.length?"charColor"===n?void t.charColor(r[0],r[1],r[2],r[3]):void t.cellColor(r[0],r[1],r[2],r[3]):void("charColor"!==n?t.cellColor(r[0],r[1],r[2]):t.charColor(r[0],r[1],r[2]))}function p(t){const e=h(t).activeFont;if(!e)throw new n("No FIGlet font is active. Call figFont() first.");return e}function g(t,e){t.It?.(e)}var _={name:"textmode.figlet.js",install:(t,e)=>(function(t){t.defineExtension("textmodifier","loadFigFont",{value:async function(t){const e=await l.it(t);return g(this,e),e}}),t.defineExtension("textmodifier","parseFigFont",{value:function(t,e){const n=l.st(t,e);return g(this,n),n}}),t.defineExtension("textmodifier","figFont",{value:function(t){const e=h(this);if(void 0===t)return e.activeFont;e.activeFont=t}}),t.defineExtension("textmodifier","figText",{value:function(t,e,n,r={}){const i=p(this),s=i.planText(t,r),o=h(this),a=e+(l=s.cols,"center"===(c=o.align)?-Math.floor(l/2):"right"===c?-(l-1):0);var l,c;const u=n+(g=s.rows,_=i.baseline,"top"===(w=o.baseline)?0:"center"===w?-Math.floor(g/2):"bottom"===w?-(g-1):-(_-1));var g,_,w;this.push(),this.translate(a,u,0);for(const h of s.cells){this.push(),this.translate(h.col,h.row,0);const t=f(r.charColor,h),e=f(r.cellColor,h);this.char(h.char),void 0!==t&&d(this,"charColor",t),void 0!==e&&d(this,"cellColor",e),this.point(),this.pop()}this.pop()}}),t.defineExtension("textmodifier","figTextWidth",{value:function(t,e={}){return p(this).measureText(t,e).cols}}),t.defineExtension("textmodifier","figTextHeight",{value:function(t,e={}){return p(this).measureText(t,e).rows}}),t.defineExtension("textmodifier","figTextBounds",{value:function(t,e={}){return p(this).measureText(t,e)}}),t.defineExtension("textmodifier","figTextAlign",{value:function(t){const e=h(this);if(void 0===t)return e.align;e.align=t}}),t.defineExtension("textmodifier","figTextBaseline",{value:function(t){const e=h(this);if(void 0===t)return e.baseline;e.baseline=t}})}(e),()=>function(t){u.delete(t)}(t))};"undefined"!=typeof window&&(window.FigletPlugin=_,window.TextmodeFigFont=l,window.FigFontParser=i,window.FigLayoutEngine=o,window.FigSmushRules=s,window.FIGFONT_REQUIRED_CODEPOINTS=r),t.FIGFONT_REQUIRED_CODEPOINTS=r,t.FigFontParser=i,t.FigLayoutEngine=o,t.FigSmushRules=s,t.FigletPlugin=_,t.TextmodeFigFont=l});
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("textmode.js")):"function"==typeof define&&define.amd?define(["exports","textmode.js"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TextmodeFiglet={},t.textmode)}(this,function(t,e){Object.defineProperty(t,Symbol.toStringTag,{value:"Module"});var n=class t extends Error{constructor(e,n){super(t.formatMessage(e,n)),this.name="FigletError"}static formatMessage(e,n){return n&&0!==Object.keys(n).length?`${e}\n\nContext:${Object.entries(n).map(([e,n])=>`\n - ${e}: ${t.formatValue(n)}`).join("")}`:e}static formatValue(e){return null===e?"null":void 0===e?"undefined":"string"==typeof e?`"${e}"`:"number"==typeof e||"boolean"==typeof e?String(e):Array.isArray(e)?`[${e.map(e=>t.formatValue(e)).join(", ")}]`:"object"==typeof e?JSON.stringify(e):String(e)}},r=[...Array.from({length:95},(t,e)=>e+32),196,214,220,228,246,252,223],i=class{static t(t){if(0===t.length)throw new n("Cannot parse an empty FIGfont file.");const e=t.replace(/\r\n?/g,"\n").split("\n"),r=e[0];if(!r)throw new n("Missing FIGfont header line.");const i=this.i(r);let s=1+i.commentLines;if(s>e.length)throw new n("FIGfont comment line count exceeds file length.",{commentLines:i.commentLines,totalLines:e.length});const o=new Map;return s=this.o(e,s,i,o),this.l(e,s,i,o),{header:i,characters:o}}static i(t){const e=t.trim().split(/\s+/),r=e[0]??"";if(r.length<6)throw new n("Invalid FIGfont header token.",{headerToken:r});const i=r.slice(0,5),s=r.slice(5);if("flf2a"!==i)throw new n("Unsupported FIGfont signature.",{signature:i});if(1!==s.length)throw new n("FIGfont hardblank must be a single character.",{hardblank:s});if(e.length<6)throw new n("Incomplete FIGfont header.",{headerLine:t});return{signature:i,hardblank:s,height:this.u(e[1],"height"),baseline:this.u(e[2],"baseline"),maxLength:this.u(e[3],"maxLength"),oldLayout:this.u(e[4],"oldLayout"),commentLines:this.u(e[5],"commentLines"),printDirection:this.u(e[6]??"0","printDirection"),fullLayout:this.u(e[7]??"0","fullLayout"),codetagCount:this.u(e[8]??"0","codetagCount")}}static o(t,e,n,i){let s=e;for(const o of r){const e=this.h(t,s,n.height,o);i.set(o,e),s+=n.height}return s}static l(t,e,n,r){let i=e;for(;i<t.length;){const e=t[i]?.trim()??"";if(0===e.length){i+=1;continue}const s=this.p(e);i+=1;const o=this.h(t,i,n.height,s);r.set(s,o),i+=n.height}}static h(t,e,r,i){if(e+r>t.length)throw new n("Unexpected end of FIGfont character data.",{codePoint:i,startIndex:e,height:r,totalLines:t.length});const s=[];let o=0;for(let a=0;a<r;a+=1){const r=t[e+a]??"";if(0===r.length)throw new n("Encountered an empty FIGcharacter row.",{codePoint:i,row:a,startIndex:e});const l=r.at(-1);if(!l)throw new n("Failed to determine FIGcharacter endmark.",{codePoint:i,row:a});const c=r.replace(new RegExp(`${this.escapeForRegExp(l)}+$`),"");s.push(c),o=Math.max(o,c.length)}return{code:i,lines:s,width:o}}static p(t){const e=t.split(/\s+/,1)[0];if(/^[-+]?0x[0-9a-f]+$/i.test(e))return Number.parseInt(e,16);if(/^[-+]?0[0-7]+$/.test(e))return Number.parseInt(e,8);if(/^[-+]?\d+$/.test(e))return Number.parseInt(e,10);throw new n("Invalid FIGfont code tag.",{codetagLine:t})}static u(t,e){if(!/^-?\d+$/.test(t))throw new n("Invalid FIGfont header value.",{field:e,token:t});return Number.parseInt(t,10)}static escapeForRegExp(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}},s=class{static RULE_EQUAL_CHARACTER=1;static RULE_UNDERSCORE=2;static RULE_HIERARCHY=4;static RULE_OPPOSITE_PAIR=8;static RULE_BIG_X=16;static RULE_HARDBLANK=32;static VERTICAL_RULE_EQUAL_CHARACTER=256;static VERTICAL_RULE_UNDERSCORE=512;static VERTICAL_RULE_HIERARCHY=1024;static VERTICAL_RULE_HORIZONTAL_LINE=2048;static VERTICAL_RULE_VERTICAL_LINE_SUPERSMUSH=4096;static _(t){return 0!==t.fullLayout?63&t.fullLayout:t.oldLayout>0?63&t.oldLayout:0}static m(t){return 7936&t.fullLayout}static I(t,e,n,r){return" "===t?e:" "===e?t:0===r?this.L(t,e,n):(0!==(r&this.RULE_EQUAL_CHARACTER)?this.C(t,e,n):null)??(0!==(r&this.RULE_UNDERSCORE)?this.v(t,e):null)??(0!==(r&this.RULE_HIERARCHY)?this.R(t,e):null)??(0!==(r&this.RULE_OPPOSITE_PAIR)?this.A(t,e):null)??(0!==(r&this.RULE_BIG_X)?this.F(t,e):null)??(0!==(r&this.RULE_HARDBLANK)?this.T(t,e,n):null)}static L(t,e,n){return" "===t?e:" "===e?t:e}static U(t,e,n){return" "===t?e:" "===e?t:0===n?this.L(t,e," "):(0!==(n&this.VERTICAL_RULE_EQUAL_CHARACTER)?this.C(t,e," "):null)??(0!==(n&this.VERTICAL_RULE_UNDERSCORE)?this.v(t,e):null)??(0!==(n&this.VERTICAL_RULE_HIERARCHY)?this.R(t,e):null)??(0!==(n&this.VERTICAL_RULE_HORIZONTAL_LINE)?this.S(t,e):null)??(0!==(n&this.VERTICAL_RULE_VERTICAL_LINE_SUPERSMUSH)?this.$(t,e):null)}static C(t,e,n){return t===e&&t!==n?t:null}static v(t,e){const n="|/\\[]{}()<>";return"_"===t&&n.includes(e)?e:"_"===e&&n.includes(t)?t:null}static R(t,e){const n=["|","/\\","[]","{}","()","<>"],r=n.findIndex(e=>e.includes(t)),i=n.findIndex(t=>t.includes(e));return-1===r||-1===i||r===i?null:r>i?t:e}static A(t,e){return["[]","][","{}","}{","()",")("].includes(`${t}${e}`)?"|":null}static F(t,e){const n=`${t}${e}`;return"/\\"===n?"|":"\\/"===n?"Y":"><"===n?"X":null}static T(t,e,n){return t===n&&e===n?n:null}static S(t,e){const n=`${t}${e}`;return"-_"===n||"_-"===n?"=":null}static $(t,e){return"|"===t&&"|"===e?"|":null}},o=class{M;k;H;constructor(t){this.M=t,this.k=s._(t),this.H=s.m(t)}G(t,e){const n=t.map((t,e)=>({inputIndex:e,inputChar:String.fromCodePoint(t.code)})),r=this.N(t,n,e,0),i=Array.from({length:r.rows},()=>Array.from({length:r.cols},()=>" "));for(const s of r.cells)i[s.row][s.col]=s.char;return i}N(t,e,n,r){if(0===t.length)return{lineIndex:r,cells:[],cols:0,rows:this.M.height};let i=this.O(t[0],e[0],r),o=t[0].width;for(let a=1;a<t.length;a+=1){const l=t[a],c=e[a]??{inputIndex:a,inputChar:String.fromCodePoint(l.code)},u=o-this.V(i,o,l,n),h=Math.max(o,u+l.width),f=i.map(t=>Array.from({length:h},(e,n)=>t[n]));for(let t=0;t<this.M.height;t+=1){const e=l.lines[t]??"",n=f[t];for(let i=0;i<l.width;i+=1){const o=e[i]??" ";if(" "===o)continue;const a=u+i,h=n[a],f=this.P(a,t,i,r,l,c,o);if(!h){n[a]=f;continue}const d=s.I(h.char,o,this.M.hardblank,this.k);n[a]=this.D(h,f,d??o)}}i=f,o=h}return this.B(i,o,r)}j(t,e){if(0===t.length)return{cells:[],lines:[],cols:0,rows:0};let n=this.q(t[0]),r=t[0].cols,i=t[0].rows;const o=[this.W(t[0],0)];for(let a=1;a<t.length;a+=1){const l=t[a],c=i-this.X(n,i,r,l,e),u=Math.max(r,l.cols),h=Math.max(i,c+l.rows),f=Array.from({length:h},(t,e)=>Array.from({length:u},(t,r)=>n[e]?.[r]));for(const t of l.cells){const e=c+t.row,n=t.col,r=f[e]?.[n];if(!r){f[e][n]={...t,row:e};continue}const i=s.U(r.char,t.char,this.H);f[e][n]=this.D(r,{...t,row:e},i??t.char)}n=f,r=u,i=h,o.push(this.W(l,c))}return this.Y(n,r,i,o)}V(t,e,n,r){if("full"===r)return 0;for(let i=e;i>=0;i-=1){let s=!0;for(let o=0;o<this.M.height&&s;o+=1){const a=t[o]??[],l=n.lines[o]??"";for(let t=0;t<i;t+=1){const n=a[e-i+t]?.char??" ",o=l[t]??" ";if(!this.J(n,o,r)){s=!1;break}}}if(s)return i}return 0}J(t,e,n){return" "===t||" "===e||"fitted"!==n&&null!==s.I(t,e,this.M.hardblank,this.k)}O(t,e={inputIndex:0,inputChar:String.fromCodePoint(t.code)},n=0){return Array.from({length:this.M.height},(r,i)=>{const s=t.lines[i]??"";return Array.from({length:t.width},(r,o)=>{const a=s[o]??" ";return" "===a?void 0:this.P(o,i,o,n,t,e,a)})})}P(t,e,n,r,i,s,o){return{char:o,col:t,row:e,inputIndex:s.inputIndex,inputChar:s.inputChar,figCharCode:i.code,subRow:e,subCol:n,lineIndex:r}}D(t,e,n){return n===t.char?t:{...e,char:n}}B(t,e,n){const r=[];for(let i=0;i<this.M.height;i+=1){const s=t[i]??[];for(let t=0;t<e;t+=1){const e=s[t];e&&e.char!==this.M.hardblank&&r.push({...e,col:t,row:i,lineIndex:n})}}return{lineIndex:n,cells:r,cols:e,rows:this.M.height}}X(t,e,n,r,i){if("full"===i)return 0;const s=this.q(r),o=Math.min(e,r.rows),a=Math.max(n,r.cols);for(let l=o;l>=0;l-=1){let n=!0;for(let r=0;r<l&&n;r+=1){const o=t[e-l+r]??[],c=s[r]??[];for(let t=0;t<a;t+=1){const e=o[t]?.char??" ",r=c[t]?.char??" ";if(!this.K(e,r,i)){n=!1;break}}}if(n)return l}return 0}K(t,e,n){return" "===t||" "===e||"fitted"!==n&&null!==s.U(t,e,this.H)}q(t){const e=Array.from({length:t.rows},()=>Array.from({length:t.cols},()=>{}));for(const n of t.cells)e[n.row][n.col]=n;return e}W(t,e){return{...t,cells:t.cells.map(t=>({...t,row:t.row+e}))}}Y(t,e,n,r){const i=[];for(let s=0;s<n;s+=1)for(let n=0;n<e;n+=1){const e=t[s]?.[n];e&&i.push({...e,row:s,col:n})}return{cells:i,lines:r,cols:e,rows:n}}},a=class{Z=new Set;tt(t){this.Z.add(t)}dispose(){for(const t of this.Z)t();this.Z.clear()}},l=class t extends a{et;M;nt;rt;constructor(t,e,n){super(),this.et=t,this.M=e,this.nt=n,this.rt=new o(e)}static async it(t){const e=await fetch(t);if(!e.ok)throw new n("Failed to load FIGfont file.",{source:String(t),status:e.status,statusText:e.statusText});const r=await e.text();return this.st(this.ot(t),r)}static st(e,n){const r=i.t(n);return new t(e,r.header,r.characters)}getCharacter(t){const e="number"==typeof t?t:Array.from(t)[0]?.codePointAt(0);if(void 0!==e)return c(this.nt.get(e))}planText(t,e={}){const n=e.horizontalLayout??this.defaultLayout,r=e.verticalLayout??this.defaultVerticalLayout,i=this.lt(e.direction),s=this.ct(t).map(t=>this.ut(t,i)).flatMap(t=>this.ht(t,n,e)).map((t,e)=>{const r=this.ft(t);return this.rt.N(r.map(t=>t.figChar),r.map(({inputChar:t,inputIndex:e})=>({inputChar:t,inputIndex:e})),n,e)}),o=this.rt.j(s,r);return{cells:o.cells,lines:o.lines,cols:o.cols,rows:o.rows,direction:i}}renderText(t,e={}){const n=this.planText(t,e),r=Array.from({length:n.rows},()=>Array.from({length:n.cols},()=>" "));for(const i of n.cells)r[i.row][i.col]=i.char;return{grid:r,cols:n.cols,rows:n.rows}}measureText(t,e={}){const n=this.planText(t,e);return{cols:n.cols,rows:n.rows}}get name(){return this.et}get header(){return{...this.M}}get characters(){return new Map(Array.from(this.nt,([t,e])=>[t,c(e)]))}get hardblank(){return this.M.hardblank}get height(){return this.M.height}get baseline(){return this.M.baseline}get defaultLayout(){return 128&this.M.fullLayout?"smushed":64&this.M.fullLayout?"fitted":-1===this.M.oldLayout?"full":0===this.M.oldLayout?"fitted":this.M.oldLayout>0?"smushed":"full"}get defaultPrintDirection(){return 1===this.M.printDirection?"rtl":"ltr"}get defaultVerticalLayout(){return 16384&this.M.fullLayout?"smushed":8192&this.M.fullLayout?"fitted":"full"}static ot(t){const e=t instanceof URL?t.pathname:t,n=(e.split(/[?#]/,1)[0]??e).split("/").filter(Boolean).pop()??"figfont";return n.toLowerCase().endsWith(".flf")?n.slice(0,-4)||"figfont":n}ct(t){const e=[[]];for(const[n,r]of Array.from(t).entries())"\n"!==r?e[e.length-1].push({character:r,inputIndex:n}):e.push([]);return e}ut(t,e){return"rtl"===e?[...t].reverse():t}ft(t){const e=t.map(({character:t,inputIndex:e})=>({figChar:this.getCharacter(t)??this.getCharacter(32),inputChar:t,inputIndex:e})),r=e.find(t=>void 0===t.figChar);if(r)throw new n("FIGfont is missing a required character for layout.",{character:r.inputChar,index:r.inputIndex});return e}ht(t,e,n){const r="number"==typeof n.maxCols&&Number.isFinite(n.maxCols)?Math.floor(n.maxCols):void 0,i=n.wrap??"none";return 0===t.length||!r||r<1||"none"===i?[t]:"char"===i?this.dt(t,e,r):this.gt(t,e,r)}gt(t,e,n){const r=this.wt(t);if(!r.some(t=>!t.isWhitespace))return[t];const i=[];let s=[],o=[];for(const a of r){if(a.isWhitespace){s.length>0&&(o=a.characters);continue}const t=0===s.length?a.characters:s.concat(o,a.characters);s.length>0&&this._t(t,e)>n?(i.push(s),s=a.characters.slice()):s=t,o=[]}return 0===s.length?[[]]:(i.push(s),i)}dt(t,e,n){const r=[];let i=[];for(const s of t){const t=i.concat(s);i.length>0&&this._t(t,e)>n?(r.push(i),i=[s]):i=t}return 0===i.length?[[]]:(r.push(i),r)}_t(t,e){const n=this.ft(t);return this.rt.N(n.map(t=>t.figChar),n.map(({inputChar:t,inputIndex:e})=>({inputChar:t,inputIndex:e})),e,0).cols}wt(t){const e=[];for(const n of t){const t=/\s/u.test(n.character),r=e[e.length-1];r&&r.isWhitespace===t?r.characters.push(n):e.push({isWhitespace:t,characters:[n]})}return e}lt(t){return"ltr"===t||"rtl"===t?t:this.defaultPrintDirection}};function c(t){if(t)return{...t,lines:[...t.lines]}}function u(t,e){return h(t),t.ownedFonts.add(e),e}function h(t){if(t.disposed)throw new n("FIGlet plugin has been disposed.")}function f(t){if(!t.disposed){t.disposed=!0,t.activeFont=void 0;for(const e of t.ownedFonts)e.dispose();t.ownedFonts.clear()}}function d(t,e){if(void 0!==t)return"function"==typeof t?t(e):t}function p(t,n,r){return r instanceof e.color.TextmodeColor||"string"==typeof r||"number"==typeof r?"charColor"===n?void t.charColor(r):void t.cellColor(r):4===r.length?"charColor"===n?void t.charColor(r[0],r[1],r[2],r[3]):void t.cellColor(r[0],r[1],r[2],r[3]):void("charColor"!==n?t.cellColor(r[0],r[1],r[2]):t.charColor(r[0],r[1],r[2]))}function g(t){const e=t.activeFont;if(!e)throw new n("No FIGlet font is active. Call figFont() first.");return e}var w={name:"textmode.figlet.js",install(t,e){const n={activeFont:void 0,align:"left",baseline:"baseline",ownedFonts:new Set,disposed:!1};try{!function(t,e){t.defineExtension("textmodifier","loadFigFont",{value:async function(t){h(e);const n=await l.it(t);try{return u(e,n)}catch(r){throw n.dispose(),r}}}),t.defineExtension("textmodifier","parseFigFont",{value:function(t,n){const r=l.st(t,n);try{return u(e,r)}catch(i){throw r.dispose(),i}}}),t.defineExtension("textmodifier","figFont",{value:function(t){if(void 0===t)return e.activeFont;e.activeFont=t}}),t.defineExtension("textmodifier","figText",{value:function(t,n,r,i={}){const s=g(e),o=s.planText(t,i),a=n+(l=o.cols,"center"===(c=e.align)?-Math.floor(l/2):"right"===c?-(l-1):0);var l,c;const u=r+(h=o.rows,f=s.baseline,"top"===(w=e.baseline)?0:"center"===w?-Math.floor(h/2):"bottom"===w?-(h-1):-(f-1));var h,f,w;this.push();try{this.translate(a,u,0);for(const t of o.cells){this.push();try{this.translate(t.col,t.row,0);const e=d(i.charColor,t),n=d(i.cellColor,t);this.char(t.char),void 0!==e&&p(this,"charColor",e),void 0!==n&&p(this,"cellColor",n),this.point()}finally{this.pop()}}}finally{this.pop()}}}),t.defineExtension("textmodifier","figTextWidth",{value:function(t,n={}){return g(e).measureText(t,n).cols}}),t.defineExtension("textmodifier","figTextHeight",{value:function(t,n={}){return g(e).measureText(t,n).rows}}),t.defineExtension("textmodifier","figTextBounds",{value:function(t,n={}){return g(e).measureText(t,n)}}),t.defineExtension("textmodifier","figTextAlign",{value:function(t){if(void 0===t)return e.align;e.align=t}}),t.defineExtension("textmodifier","figTextBaseline",{value:function(t){if(void 0===t)return e.baseline;e.baseline=t}})}(e,n)}catch(r){throw f(n),r}return()=>f(n)}};"undefined"!=typeof window&&(window.FigletPlugin=w,window.TextmodeFigFont=l,window.FigFontParser=i,window.FigLayoutEngine=o,window.FigSmushRules=s,window.FIGFONT_REQUIRED_CODEPOINTS=r),t.FIGFONT_REQUIRED_CODEPOINTS=r,t.FigFontParser=i,t.FigLayoutEngine=o,t.FigSmushRules=s,t.FigletPlugin=w,t.TextmodeFigFont=l});
|
|
@@ -151,7 +151,7 @@ export interface TextmodifierFigletExtensions {
|
|
|
151
151
|
*/
|
|
152
152
|
figTextBaseline(baseline: FigTextBaseline): void;
|
|
153
153
|
}
|
|
154
|
-
declare module 'textmode.js' {
|
|
155
|
-
interface
|
|
154
|
+
declare module 'textmode.js/addon' {
|
|
155
|
+
interface TextmodifierExtensions extends TextmodifierFigletExtensions {
|
|
156
156
|
}
|
|
157
157
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { TextmodePluginContext } from 'textmode.js';
|
|
2
|
+
import { type FigletPluginState } from '../state/figletState';
|
|
2
3
|
/**
|
|
3
4
|
* Install FIGlet Textmodifier extensions on a specific `Textmodifier` instance.
|
|
4
5
|
*
|
|
@@ -9,4 +10,4 @@ import type { TextmodePluginContext } from 'textmode.js';
|
|
|
9
10
|
*
|
|
10
11
|
* @param api The textmode.js plugin context.
|
|
11
12
|
*/
|
|
12
|
-
export declare function installTextmodifierFigletExtensions(api: TextmodePluginContext): void;
|
|
13
|
+
export declare function installTextmodifierFigletExtensions(api: TextmodePluginContext, state: FigletPluginState): void;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Textmodifier } from 'textmode.js';
|
|
2
1
|
import type { FigTextAlign, FigTextBaseline } from '../figfont';
|
|
3
2
|
import type { TextmodeFigFont } from '../figfont';
|
|
4
3
|
/**
|
|
@@ -8,17 +7,20 @@ export interface FigletPluginState {
|
|
|
8
7
|
activeFont?: TextmodeFigFont;
|
|
9
8
|
align: FigTextAlign;
|
|
10
9
|
baseline: FigTextBaseline;
|
|
10
|
+
readonly ownedFonts: Set<TextmodeFigFont>;
|
|
11
|
+
disposed: boolean;
|
|
11
12
|
}
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
+
* Create state owned by one FIGlet plugin installation.
|
|
14
15
|
*
|
|
15
|
-
* @
|
|
16
|
-
* @returns Mutable FIGlet plugin state for that instance.
|
|
16
|
+
* @returns Mutable state captured by that installation's extensions.
|
|
17
17
|
*/
|
|
18
|
-
export declare function
|
|
18
|
+
export declare function createFigletState(): FigletPluginState;
|
|
19
|
+
/** Register a font created by this plugin installation for teardown. */
|
|
20
|
+
export declare function trackFont(state: FigletPluginState, font: TextmodeFigFont): TextmodeFigFont;
|
|
21
|
+
/** Throw when a retained extension or asynchronous callback outlives its installation. */
|
|
22
|
+
export declare function assertFigletStateLive(state: FigletPluginState): void;
|
|
19
23
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @param textmodifier Target instance.
|
|
24
|
+
* Dispose resources owned by one FIGlet plugin installation.
|
|
23
25
|
*/
|
|
24
|
-
export declare function
|
|
26
|
+
export declare function disposeFigletState(state: FigletPluginState): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "textmode.figlet.js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "FIGlet font support for textmode.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"devEngines": {
|
|
@@ -33,6 +33,8 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"scripts": {
|
|
36
|
+
"prebuild": "npm run clean",
|
|
37
|
+
"clean": "rm -rf dist",
|
|
36
38
|
"dev": "textmode-build dev --port 5175 --host",
|
|
37
39
|
"build:bundle": "textmode-build build",
|
|
38
40
|
"build:types": "textmode-types build",
|
|
@@ -60,16 +62,16 @@
|
|
|
60
62
|
"release": "textmode-release release"
|
|
61
63
|
},
|
|
62
64
|
"peerDependencies": {
|
|
63
|
-
"textmode.js": "^0.
|
|
65
|
+
"textmode.js": "^0.18.0"
|
|
64
66
|
},
|
|
65
67
|
"devDependencies": {
|
|
66
68
|
"@textmode/build": "^0.1.0",
|
|
67
69
|
"@textmode/docs": "^0.3.0",
|
|
68
70
|
"@textmode/lint": "^0.1.0",
|
|
69
71
|
"@textmode/release": "^0.2.0",
|
|
70
|
-
"@textmode/tsconfig": "^0.2.
|
|
72
|
+
"@textmode/tsconfig": "^0.2.1",
|
|
71
73
|
"@types/node": "^24.0.0",
|
|
72
|
-
"textmode.js": "^0.
|
|
74
|
+
"textmode.js": "^0.18.0",
|
|
73
75
|
"typescript": "^6.0.3",
|
|
74
76
|
"vitest": "^4.1.10"
|
|
75
77
|
},
|