@mlightcad/mtext-parser 1.3.2 → 1.4.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.
@@ -1,3 +0,0 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var _=(s=>(s[s.NONE=0]="NONE",s[s.WORD=1]="WORD",s[s.STACK=2]="STACK",s[s.SPACE=3]="SPACE",s[s.NBSP=4]="NBSP",s[s.TABULATOR=5]="TABULATOR",s[s.NEW_PARAGRAPH=6]="NEW_PARAGRAPH",s[s.NEW_COLUMN=7]="NEW_COLUMN",s[s.WRAP_AT_DIMLINE=8]="WRAP_AT_DIMLINE",s[s.PROPERTIES_CHANGED=9]="PROPERTIES_CHANGED",s))(_||{}),S=(s=>(s[s.BOTTOM=0]="BOTTOM",s[s.MIDDLE=1]="MIDDLE",s[s.TOP=2]="TOP",s))(S||{}),F=(s=>(s[s.DEFAULT=0]="DEFAULT",s[s.LEFT=1]="LEFT",s[s.RIGHT=2]="RIGHT",s[s.CENTER=3]="CENTER",s[s.JUSTIFIED=4]="JUSTIFIED",s[s.DISTRIBUTED=5]="DISTRIBUTED",s))(F||{}),E=(s=>(s[s.NONE=0]="NONE",s[s.UNDERLINE=1]="UNDERLINE",s[s.OVERLINE=2]="OVERLINE",s[s.STRIKE_THROUGH=4]="STRIKE_THROUGH",s))(E||{});const R={c:"Ø",d:"°",p:"±","%":"%"},y={l:1,r:2,c:3,j:4,d:5};function v(s){const[t,e,r]=s;return r<<16|e<<8|t}function I(s){const t=s&255,e=s>>8&255,r=s>>16&255;return[t,e,r]}function O(s){return s.replace(/\r\n|\r|\n/g,"\\P")}function N(s){return s.replace(/\\P/g,"").replace(/\\~/g,"").includes("\\")}function x(s,t=!1){const e=new Set,r=/\\[fF](.*?)[;|]/g;return[...s.matchAll(r)].forEach(a=>{let h=a[1].toLowerCase();t&&(h=h.replace(/\.(ttf|otf|woff|shx)$/,"")),e.add(h)}),e}class T{constructor(t){this.stack=[],this.stack.push(t)}push(t){this.stack.push(t)}pop(){if(this.stack.length<=1)return;const t=this.stack.pop(),e=this.stack[this.stack.length-1];return JSON.stringify(e.paragraph)!==JSON.stringify(t.paragraph)&&(e.paragraph={...t.paragraph}),t}get current(){return this.stack[this.stack.length-1]}get depth(){return this.stack.length-1}get root(){return this.stack[0]}setCurrent(t){this.stack[this.stack.length-1]=t}}class w{constructor(t,e,r={}){this.continueStroke=!1,this.inStackContext=!1,this.scanner=new f(t);const a=e??new b;this.ctxStack=new T(a),this.yieldPropertyCommands=r.yieldPropertyCommands??!1,this.resetParagraphParameters=r.resetParagraphParameters??!1,this.mifDecoder=r.mifDecoder??this.decodeMultiByteChar.bind(this),this.mifCodeLength=r.mifCodeLength??"auto"}decodeMultiByteChar(t){try{if(t.length===5){const e=t[0];let r="gbk";e==="1"?r="shift-jis":e==="2"&&(r="big5");const a=new Uint8Array([parseInt(t.substr(1,2),16),parseInt(t.substr(3,2),16)]);return new TextDecoder(r).decode(a)}else if(t.length===4){const e=new Uint8Array([parseInt(t.substr(0,2),16),parseInt(t.substr(2,2),16)]),a=new TextDecoder("gbk").decode(e);if(a!=="▯")return a;const i=new TextDecoder("big5").decode(e);if(i!=="▯")return i}return"▯"}catch{return"▯"}}extractMifCode(t){var e,r,a;if(t==="auto"){const h=(e=this.scanner.tail.match(/^[0-9A-Fa-f]{5}/))==null?void 0:e[0];if(h)return h;const i=(r=this.scanner.tail.match(/^[0-9A-Fa-f]{4}/))==null?void 0:r[0];return i||null}else return((a=this.scanner.tail.match(new RegExp(`^[0-9A-Fa-f]{${t}}`)))==null?void 0:a[0])||null}pushCtx(){this.ctxStack.push(this.ctxStack.current)}popCtx(){this.ctxStack.pop()}parseStacking(){const t=new f(this.extractExpression(!0));let e="",r="",a="";const h=()=>{let n=t.peek(),l=!1;return n.charCodeAt(0)<32&&(n=" "),n==="\\"&&(l=!0,t.consume(1),n=t.peek()),t.consume(1),[n,l]},i=()=>{let n="";for(;t.hasData;){const[l,o]=h();if(!o&&(l==="/"||l==="#"||l==="^"))return[n,l];n+=l}return[n,""]},u=n=>{let l="",o=n;for(;t.hasData;){const[c,g]=h();if(!(o&&c===" ")){if(o=!1,!g&&c===";")break;l+=c}}return l};return[e,a]=i(),a&&(r=u(a==="^")),e===""&&r.includes("I/")?[2,[" "," ","/"]]:a==="^"?[2,[e,r,"^"]]:[2,[e,r,a]]}parseProperties(t){const e=this.ctxStack.current.copy(),r=this.ctxStack.current.copy();switch(t){case"L":r.underline=!0,this.continueStroke=!0;break;case"l":r.underline=!1,r.hasAnyStroke||(this.continueStroke=!1);break;case"O":r.overline=!0,this.continueStroke=!0;break;case"o":r.overline=!1,r.hasAnyStroke||(this.continueStroke=!1);break;case"K":r.strikeThrough=!0,this.continueStroke=!0;break;case"k":r.strikeThrough=!1,r.hasAnyStroke||(this.continueStroke=!1);break;case"A":this.parseAlign(r);break;case"C":this.parseAciColor(r);break;case"c":this.parseRgbColor(r);break;case"H":this.parseHeight(r);break;case"W":this.parseWidth(r);break;case"Q":this.parseOblique(r);break;case"T":this.parseCharTracking(r);break;case"p":this.parseParagraphProperties(r);break;case"f":case"F":this.parseFontProperties(r);break;default:throw new Error(`Unknown command: ${t}`)}if(this.continueStroke=r.hasAnyStroke,r.continueStroke=this.continueStroke,this.ctxStack.setCurrent(r),this.yieldPropertyCommands){const a=this.getPropertyChanges(e,r);if(Object.keys(a).length>0)return{command:t,changes:a,depth:this.ctxStack.depth}}}getPropertyChanges(t,e){const r={};if(t.underline!==e.underline&&(r.underline=e.underline),t.overline!==e.overline&&(r.overline=e.overline),t.strikeThrough!==e.strikeThrough&&(r.strikeThrough=e.strikeThrough),t.color.aci!==e.color.aci&&(r.aci=e.color.aci),t.color.rgbValue!==e.color.rgbValue&&(r.rgb=e.color.rgb),t.align!==e.align&&(r.align=e.align),JSON.stringify(t.fontFace)!==JSON.stringify(e.fontFace)&&(r.fontFace=e.fontFace),(t.capHeight.value!==e.capHeight.value||t.capHeight.isRelative!==e.capHeight.isRelative)&&(r.capHeight=e.capHeight),(t.widthFactor.value!==e.widthFactor.value||t.widthFactor.isRelative!==e.widthFactor.isRelative)&&(r.widthFactor=e.widthFactor),(t.charTrackingFactor.value!==e.charTrackingFactor.value||t.charTrackingFactor.isRelative!==e.charTrackingFactor.isRelative)&&(r.charTrackingFactor=e.charTrackingFactor),t.oblique!==e.oblique&&(r.oblique=e.oblique),JSON.stringify(t.paragraph)!==JSON.stringify(e.paragraph)){const a={};t.paragraph.indent!==e.paragraph.indent&&(a.indent=e.paragraph.indent),t.paragraph.align!==e.paragraph.align&&(a.align=e.paragraph.align),t.paragraph.left!==e.paragraph.left&&(a.left=e.paragraph.left),t.paragraph.right!==e.paragraph.right&&(a.right=e.paragraph.right),JSON.stringify(t.paragraph.tabs)!==JSON.stringify(e.paragraph.tabs)&&(a.tabs=e.paragraph.tabs),Object.keys(a).length>0&&(r.paragraph=a)}return r}parseAlign(t){const e=this.scanner.get();"012".includes(e)?t.align=parseInt(e):t.align=0,this.consumeOptionalTerminator()}parseHeight(t){const e=this.extractFloatExpression(!0);if(e)try{e.endsWith("x")?t.capHeight={value:parseFloat(e.slice(0,-1)),isRelative:!0}:t.capHeight={value:parseFloat(e),isRelative:!1}}catch{this.scanner.consume(-e.length);return}this.consumeOptionalTerminator()}parseWidth(t){const e=this.extractFloatExpression(!0);if(e)try{e.endsWith("x")?t.widthFactor={value:parseFloat(e.slice(0,-1)),isRelative:!0}:t.widthFactor={value:parseFloat(e),isRelative:!1}}catch{this.scanner.consume(-e.length);return}this.consumeOptionalTerminator()}parseCharTracking(t){const e=this.extractFloatExpression(!0);if(e)try{e.endsWith("x")?t.charTrackingFactor={value:Math.abs(parseFloat(e.slice(0,-1))),isRelative:!0}:t.charTrackingFactor={value:Math.abs(parseFloat(e)),isRelative:!1}}catch{this.scanner.consume(-e.length);return}this.consumeOptionalTerminator()}parseFloatValueOrFactor(t){const e=this.extractFloatExpression(!0);if(e)if(e.endsWith("x")){const r=parseFloat(e.slice(0,-1));t*=r}else t=parseFloat(e);return t}parseOblique(t){const e=this.extractFloatExpression(!1);e&&(t.oblique=parseFloat(e)),this.consumeOptionalTerminator()}parseAciColor(t){const e=this.extractIntExpression();if(e){const r=parseInt(e);r<257&&(t.color.aci=r)}this.consumeOptionalTerminator()}parseRgbColor(t){const e=this.extractIntExpression();if(e){const r=parseInt(e)&16777215;t.color.rgbValue=r}this.consumeOptionalTerminator()}extractFloatExpression(t=!1){const e=t?/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?x?/:/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/,r=this.scanner.tail.match(e);if(r){const a=r[0];return this.scanner.consume(a.length),a}return""}extractIntExpression(){const t=this.scanner.tail.match(/^\d+/);if(t){const e=t[0];return this.scanner.consume(e.length),e}return""}extractExpression(t=!1){const e=this.scanner.find(";",t);if(e<0){const i=this.scanner.tail;return this.scanner.consume(i.length),i}const a=this.scanner.peek(e-this.scanner.currentIndex-1)==="\\",h=this.scanner.tail.slice(0,e-this.scanner.currentIndex+(a?1:0));return this.scanner.consume(h.length+1),h}parseFontProperties(t){const e=this.extractExpression().split("|");if(e.length>0&&e[0]){const r=e[0];let a="Regular",h=400;for(const i of e.slice(1))i.startsWith("b1")?h=700:i==="i"||i.startsWith("i1")?a="Italic":(i==="i0"||i.startsWith("i0"))&&(a="Regular");t.fontFace={family:r,style:a,weight:h}}}parseParagraphProperties(t){const e=new f(this.extractExpression());let r=t.paragraph.indent,a=t.paragraph.left,h=t.paragraph.right,i=t.paragraph.align,u=[];const n=()=>{const l=e.tail.match(/^[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/);if(l){const o=parseFloat(l[0]);for(e.consume(l[0].length);e.peek()===",";)e.consume(1);return o}return 0};for(;e.hasData;)switch(e.get()){case"i":r=n();break;case"l":a=n();break;case"r":h=n();break;case"x":break;case"q":{const o=e.get();for(i=y[o]||0;e.peek()===",";)e.consume(1);break}case"t":for(u=[];e.hasData;){const o=e.peek();if(o==="r"||o==="c"){e.consume(1);const c=n();u.push(o+c.toString())}else{const c=n();isNaN(c)?e.consume(1):u.push(c)}}break}t.paragraph={indent:r,left:a,right:h,align:i,tabs:u}}consumeOptionalTerminator(){this.scanner.peek()===";"&&this.scanner.consume(1)}*parse(){let r=null;function a(i){const u={...i.paragraph};i.paragraph={indent:0,left:0,right:0,align:0,tabs:[]};const n={};return u.indent!==0&&(n.indent=0),u.left!==0&&(n.left=0),u.right!==0&&(n.right=0),u.align!==0&&(n.align=0),JSON.stringify(u.tabs)!==JSON.stringify([])&&(n.tabs=[]),n}const h=()=>{let i="";for(;this.scanner.hasData;){let u=!1,n=this.scanner.peek();const l=this.scanner.currentIndex;if(n.charCodeAt(0)<32){if(this.scanner.consume(1),n===" ")return[5,null];if(n===`
2
- `)return[6,null];n=" "}if(n==="\\")if("\\{}".includes(this.scanner.peek(1)))u=!0,this.scanner.consume(1),n=this.scanner.peek();else{if(i)return[1,i];this.scanner.consume(1);const o=this.scanner.get();switch(o){case"~":return[4,null];case"P":return[6,null];case"N":return[7,null];case"X":return[8,null];case"S":{this.inStackContext=!0;const c=this.parseStacking();return this.inStackContext=!1,c}case"m":case"M":if(this.scanner.peek()==="+"){this.scanner.consume(1);const c=this.extractMifCode(this.mifCodeLength);if(c){this.scanner.consume(c.length);const g=this.mifDecoder(c);return i?[1,i]:[1,g]}this.scanner.consume(-1)}i+="\\M";continue;case"U":if(this.scanner.peek()==="+"){this.scanner.consume(1);const c=this.scanner.tail.match(/^[0-9A-Fa-f]{4,8}/);if(c){const g=c[0];this.scanner.consume(g.length);const p=parseInt(g,16);let m="";try{m=String.fromCodePoint(p)}catch{m="▯"}return i?[1,i]:[1,m]}this.scanner.consume(-1)}i+="\\U";continue;default:if(o)try{const c=this.parseProperties(o);if(this.yieldPropertyCommands&&c)return[9,c];continue}catch{const c=this.scanner.tail.slice(l,this.scanner.currentIndex);i+=c}}continue}if(n==="%"&&this.scanner.peek(1)==="%"){const o=this.scanner.peek(2).toLowerCase(),c=R[o];if(c){this.scanner.consume(3),i+=c;continue}else{const g=[o,this.scanner.peek(3),this.scanner.peek(4)];if(g.every(p=>p>="0"&&p<="9")){const p=Number.parseInt(g.join(""),10);this.scanner.consume(5),i+=String.fromCharCode(p)}else this.scanner.consume(3);continue}}if(n===" ")return i?(this.scanner.consume(1),r=3,[1,i]):(this.scanner.consume(1),[3,null]);if(!u){if(n==="{"){if(i)return[1,i];this.scanner.consume(1),this.pushCtx();continue}else if(n==="}"){if(i)return[1,i];if(this.scanner.consume(1),this.yieldPropertyCommands){const o=this.ctxStack.current;this.popCtx();const c=this.getPropertyChanges(o,this.ctxStack.current);if(Object.keys(c).length>0)return[9,{command:void 0,changes:c,depth:this.ctxStack.depth}]}else this.popCtx();continue}}if(!this.inStackContext&&n==="^"){const o=this.scanner.peek(1);if(o){const c=o.charCodeAt(0);if(this.scanner.consume(2),c===32)i+="^";else{if(c===73)return i?[1,i]:[5,null];if(c===74)return i?[1,i]:[6,null];if(c===77)continue;i+="▯"}continue}}this.scanner.consume(1),n.charCodeAt(0)>=32&&(i+=n)}return i?[1,i]:[0,null]};for(;;){const[i,u]=h.call(this);if(i){if(yield new d(i,this.ctxStack.current.copy(),u),i===6&&this.resetParagraphParameters){const n=this.ctxStack.current,l=a(n);this.yieldPropertyCommands&&Object.keys(l).length>0&&(yield new d(9,n.copy(),{command:void 0,changes:{paragraph:l},depth:this.ctxStack.depth}))}r&&(yield new d(r,this.ctxStack.current.copy(),null),r=null)}else break}}}class f{constructor(t){this.text=t,this.textLen=t.length,this._index=0}get currentIndex(){return this._index}get isEmpty(){return this._index>=this.textLen}get hasData(){return this._index<this.textLen}get(){if(this.isEmpty)return"";const t=this.text[this._index];return this._index++,t}consume(t=1){this._index=Math.max(0,Math.min(this._index+t,this.textLen))}peek(t=0){const e=this._index+t;return e>=this.textLen||e<0?"":this.text[e]}find(t,e=!1){let r=this._index;for(;r<this.textLen;){if(e&&this.text[r]==="\\"){if(r+1<this.textLen){if(this.text[r+1]===t)return r+1;r+=2;continue}r++;continue}if(this.text[r]===t)return r;r++}return-1}get tail(){return this.text.slice(this._index)}isNextSpace(){return this.peek()===" "}consumeSpaces(){let t=0;for(;this.isNextSpace();)this.consume(),t++;return t}}class k{constructor(t){this._aci=256,this._rgbValue=null,Array.isArray(t)?this.rgb=t:typeof t=="number"?this.aci=t:this.aci=256}get aci(){return this._aci}set aci(t){if(t===null)this._aci=null;else if(t>=0&&t<=256)this._aci=t,this._rgbValue=null;else throw new Error("ACI not in range [0, 256]")}get rgb(){if(this._rgbValue===null)return null;const t=this._rgbValue>>16&255,e=this._rgbValue>>8&255,r=this._rgbValue&255;return[t,e,r]}set rgb(t){if(t){const[e,r,a]=t;this._rgbValue=(e&255)<<16|(r&255)<<8|a&255,this._aci=null}else this._rgbValue=null}get isRgb(){return this._rgbValue!==null}get isAci(){return this._rgbValue===null&&this._aci!==null}get rgbValue(){return this._rgbValue}set rgbValue(t){t===null?this._rgbValue=null:(this._rgbValue=t&16777215,this._aci=null)}copy(){const t=new k;return t._aci=this._aci,t._rgbValue=this._rgbValue,t}toObject(){return{aci:this._aci,rgb:this.rgb,rgbValue:this._rgbValue}}equals(t){return this._aci===t._aci&&this._rgbValue===t._rgbValue}}class b{constructor(){this._stroke=0,this.continueStroke=!1,this.color=new k,this.align=0,this.fontFace={family:"",style:"Regular",weight:400},this._capHeight={value:1,isRelative:!1},this._widthFactor={value:1,isRelative:!1},this._charTrackingFactor={value:1,isRelative:!1},this.oblique=0,this.paragraph={indent:0,left:0,right:0,align:0,tabs:[]}}get capHeight(){return this._capHeight}set capHeight(t){this._capHeight={value:Math.abs(t.value),isRelative:t.isRelative}}get widthFactor(){return this._widthFactor}set widthFactor(t){this._widthFactor={value:Math.abs(t.value),isRelative:t.isRelative}}get charTrackingFactor(){return this._charTrackingFactor}set charTrackingFactor(t){this._charTrackingFactor={value:Math.abs(t.value),isRelative:t.isRelative}}get aci(){return this.color.aci}set aci(t){this.color.aci=t}get rgb(){return this.color.rgb}set rgb(t){this.color.rgb=t}get italic(){return this.fontFace.style==="Italic"}set italic(t){this.fontFace.style=t?"Italic":"Regular"}get bold(){return(this.fontFace.weight||400)>=700}set bold(t){this.fontFace.weight=t?700:400}get underline(){return!!(this._stroke&1)}set underline(t){this._setStrokeState(1,t)}get strikeThrough(){return!!(this._stroke&4)}set strikeThrough(t){this._setStrokeState(4,t)}get overline(){return!!(this._stroke&2)}set overline(t){this._setStrokeState(2,t)}get hasAnyStroke(){return!!this._stroke}_setStrokeState(t,e=!0){e?this._stroke|=t:this._stroke&=~t}copy(){const t=new b;return t._stroke=this._stroke,t.continueStroke=this.continueStroke,t.color=this.color.copy(),t.align=this.align,t.fontFace={...this.fontFace},t._capHeight={...this._capHeight},t._widthFactor={...this._widthFactor},t._charTrackingFactor={...this._charTrackingFactor},t.oblique=this.oblique,t.paragraph={...this.paragraph},t}}class d{constructor(t,e,r){this.type=t,this.ctx=e,this.data=r}}exports.MTextColor=k;exports.MTextContext=b;exports.MTextLineAlignment=S;exports.MTextParagraphAlignment=F;exports.MTextParser=w;exports.MTextStroke=E;exports.MTextToken=d;exports.TextScanner=f;exports.TokenType=_;exports.escapeDxfLineEndings=O;exports.getFonts=x;exports.hasInlineFormattingCodes=N;exports.int2rgb=I;exports.rgb2int=v;
3
- //# sourceMappingURL=parser.cjs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parser.cjs.js","sources":["../src/parser.ts"],"sourcesContent":["/**\r\n * Token types used in MText parsing\r\n */\r\nexport enum TokenType {\r\n /** No token */\r\n NONE = 0,\r\n /** Word token with string data */\r\n WORD = 1,\r\n /** Stack token with [numerator, denominator, type] data */\r\n STACK = 2,\r\n /** Space token with no data */\r\n SPACE = 3,\r\n /** Non-breaking space token with no data */\r\n NBSP = 4,\r\n /** Tab token with no data */\r\n TABULATOR = 5,\r\n /** New paragraph token with no data */\r\n NEW_PARAGRAPH = 6,\r\n /** New column token with no data */\r\n NEW_COLUMN = 7,\r\n /** Wrap at dimension line token with no data */\r\n WRAP_AT_DIMLINE = 8,\r\n /** Properties changed token with string data (full command) */\r\n PROPERTIES_CHANGED = 9,\r\n}\r\n\r\n/**\r\n * Represents a factor value that can be either absolute or relative.\r\n * Used for properties like height, width, and character tracking in MText formatting.\r\n */\r\nexport interface FactorValue {\r\n /** The numeric value of the factor */\r\n value: number;\r\n /** Whether the value is relative (true) or absolute (false) */\r\n isRelative: boolean;\r\n}\r\n\r\n/**\r\n * Format properties of MText word tokens.\r\n * This interface defines all the formatting properties that can be applied to MText content,\r\n * including text styling, colors, alignment, font properties, and paragraph formatting.\r\n */\r\nexport interface Properties {\r\n /** Whether text is underlined */\r\n underline?: boolean;\r\n /** Whether text has an overline */\r\n overline?: boolean;\r\n /** Whether text has strike-through */\r\n strikeThrough?: boolean;\r\n /** AutoCAD Color Index (ACI) color value (0-256), or null if not set */\r\n aci?: number | null;\r\n /** RGB color tuple [r, g, b], or null if not set */\r\n rgb?: RGB | null;\r\n /** Line alignment for the text */\r\n align?: MTextLineAlignment;\r\n /** Font face properties including family, style, and weight */\r\n fontFace?: FontFace;\r\n /** Capital letter height factor (can be relative or absolute) */\r\n capHeight?: FactorValue;\r\n /** Character width factor (can be relative or absolute) */\r\n widthFactor?: FactorValue;\r\n /** Character tracking factor for spacing between characters (can be relative or absolute) */\r\n charTrackingFactor?: FactorValue;\r\n /** Oblique angle in degrees for text slant */\r\n oblique?: number;\r\n /** Paragraph formatting properties (partial to allow selective updates) */\r\n paragraph?: Partial<ParagraphProperties>;\r\n}\r\n\r\n/**\r\n * Represents a change in MText properties, including the command, the changed properties, and the context depth.\r\n */\r\nexport interface ChangedProperties {\r\n /**\r\n * The property command that triggered the change (e.g., 'L', 'C', 'f').\r\n * The command will be undefined if it is to restore context.\r\n */\r\n command: string | undefined;\r\n /**\r\n * The set of properties that have changed as a result of the command.\r\n */\r\n changes: Properties;\r\n /**\r\n * The current context stack depth when the property change occurs.\r\n * - 0: The change is global (applies outside of any `{}` block).\r\n * - >0: The change is local (applies within one or more nested `{}` blocks).\r\n */\r\n depth: number; // 0 = global, >0 = local\r\n}\r\n\r\n/**\r\n * Type for token data based on token type\r\n */\r\nexport type TokenData = {\r\n [TokenType.NONE]: null;\r\n [TokenType.WORD]: string;\r\n [TokenType.STACK]: [string, string, string];\r\n [TokenType.SPACE]: null;\r\n [TokenType.NBSP]: null;\r\n [TokenType.TABULATOR]: null;\r\n [TokenType.NEW_PARAGRAPH]: null;\r\n [TokenType.NEW_COLUMN]: null;\r\n [TokenType.WRAP_AT_DIMLINE]: null;\r\n [TokenType.PROPERTIES_CHANGED]: ChangedProperties;\r\n};\r\n\r\n/**\r\n * Line alignment options for MText\r\n */\r\nexport enum MTextLineAlignment {\r\n /** Align text to bottom */\r\n BOTTOM = 0,\r\n /** Align text to middle */\r\n MIDDLE = 1,\r\n /** Align text to top */\r\n TOP = 2,\r\n}\r\n\r\n/**\r\n * Paragraph alignment options for MText\r\n */\r\nexport enum MTextParagraphAlignment {\r\n /** Default alignment */\r\n DEFAULT = 0,\r\n /** Left alignment */\r\n LEFT = 1,\r\n /** Right alignment */\r\n RIGHT = 2,\r\n /** Center alignment */\r\n CENTER = 3,\r\n /** Justified alignment */\r\n JUSTIFIED = 4,\r\n /** Distributed alignment */\r\n DISTRIBUTED = 5,\r\n}\r\n\r\n/**\r\n * Text stroke options for MText\r\n */\r\nexport enum MTextStroke {\r\n /** No stroke */\r\n NONE = 0,\r\n /** Underline stroke */\r\n UNDERLINE = 1,\r\n /** Overline stroke */\r\n OVERLINE = 2,\r\n /** Strike-through stroke */\r\n STRIKE_THROUGH = 4,\r\n}\r\n\r\n/**\r\n * RGB color tuple\r\n */\r\nexport type RGB = [number, number, number];\r\n\r\n/**\r\n * Font style type\r\n */\r\nexport type FontStyle = 'Regular' | 'Italic';\r\n\r\n/**\r\n * Font face properties\r\n */\r\nexport interface FontFace {\r\n /** Font family name */\r\n family: string;\r\n /** Font style (e.g., 'Regular', 'Italic') */\r\n style: FontStyle;\r\n /** Font weight (e.g., 400 for normal, 700 for bold) */\r\n weight: number;\r\n}\r\n\r\n/**\r\n * Paragraph properties\r\n */\r\nexport interface ParagraphProperties {\r\n /** Indentation value */\r\n indent: number;\r\n /** Left margin value */\r\n left: number;\r\n /** Right margin value */\r\n right: number;\r\n /** Paragraph alignment */\r\n align: MTextParagraphAlignment;\r\n /** Tab stop positions and types */\r\n tabs: (number | string)[];\r\n}\r\n\r\n/**\r\n * Special character encoding mapping\r\n */\r\nconst SPECIAL_CHAR_ENCODING: Record<string, string> = {\r\n c: 'Ø',\r\n d: '°',\r\n p: '±',\r\n '%': '%',\r\n};\r\n\r\n/**\r\n * Character to paragraph alignment mapping\r\n */\r\nconst CHAR_TO_ALIGN: Record<string, MTextParagraphAlignment> = {\r\n l: MTextParagraphAlignment.LEFT,\r\n r: MTextParagraphAlignment.RIGHT,\r\n c: MTextParagraphAlignment.CENTER,\r\n j: MTextParagraphAlignment.JUSTIFIED,\r\n d: MTextParagraphAlignment.DISTRIBUTED,\r\n};\r\n\r\n/**\r\n * Convert RGB tuple to integer color value\r\n * @param rgb - RGB color tuple\r\n * @returns Integer color value\r\n */\r\nexport function rgb2int(rgb: RGB): number {\r\n const [r, g, b] = rgb;\r\n return (b << 16) | (g << 8) | r;\r\n}\r\n\r\n/**\r\n * Convert integer color value to RGB tuple\r\n * @param value - Integer color value\r\n * @returns RGB color tuple\r\n */\r\nexport function int2rgb(value: number): RGB {\r\n const r = value & 0xff;\r\n const g = (value >> 8) & 0xff;\r\n const b = (value >> 16) & 0xff;\r\n return [r, g, b];\r\n}\r\n\r\n/**\r\n * Escape DXF line endings\r\n * @param text - Text to escape\r\n * @returns Escaped text\r\n */\r\nexport function escapeDxfLineEndings(text: string): string {\r\n return text.replace(/\\r\\n|\\r|\\n/g, '\\\\P');\r\n}\r\n\r\n/**\r\n * Check if text contains inline formatting codes\r\n * @param text - Text to check\r\n * @returns True if text contains formatting codes\r\n */\r\nexport function hasInlineFormattingCodes(text: string): boolean {\r\n return text.replace(/\\\\P/g, '').replace(/\\\\~/g, '').includes('\\\\');\r\n}\r\n\r\n/**\r\n * Extracts all unique font names used in an MText string.\r\n * This function searches for font commands in the format \\f{fontname}| or \\f{fontname}; and returns a set of unique font names.\r\n * Font names are converted to lowercase to ensure case-insensitive uniqueness.\r\n *\r\n * @param mtext - The MText string to analyze for font names\r\n * @param removeExtension - Whether to remove font file extensions (e.g., .ttf, .shx) from font names. Defaults to false.\r\n * @returns A Set containing all unique font names found in the MText string, converted to lowercase\r\n * @example\r\n * ```ts\r\n * const mtext = \"\\\\fArial.ttf|Hello\\\\fTimes New Roman.otf|World\";\r\n * const fonts = getFonts(mtext, true);\r\n * // Returns: Set(2) { \"arial\", \"times new roman\" }\r\n * ```\r\n */\r\nexport function getFonts(mtext: string, removeExtension: boolean = false) {\r\n const fonts: Set<string> = new Set();\r\n const regex = /\\\\[fF](.*?)[;|]/g;\r\n\r\n [...mtext.matchAll(regex)].forEach(match => {\r\n let fontName = match[1].toLowerCase();\r\n if (removeExtension) {\r\n fontName = fontName.replace(/\\.(ttf|otf|woff|shx)$/, '');\r\n }\r\n fonts.add(fontName);\r\n });\r\n\r\n return fonts;\r\n}\r\n\r\n/**\r\n * ContextStack manages a stack of MTextContext objects for character-level formatting.\r\n *\r\n * - Character-level formatting (underline, color, font, etc.) is scoped to `{}` blocks and managed by the stack.\r\n * - Paragraph-level formatting (\\p) is not scoped, but when a block ends, any paragraph property changes are merged into the parent context.\r\n * - On pop, paragraph properties from the popped context are always merged into the new top context.\r\n */\r\nclass ContextStack {\r\n private stack: MTextContext[] = [];\r\n\r\n /**\r\n * Creates a new ContextStack with an initial context.\r\n * @param initial The initial MTextContext to use as the base of the stack.\r\n */\r\n constructor(initial: MTextContext) {\r\n this.stack.push(initial);\r\n }\r\n\r\n /**\r\n * Pushes a copy of the given context onto the stack.\r\n * @param ctx The MTextContext to push (copied).\r\n */\r\n push(ctx: MTextContext) {\r\n this.stack.push(ctx);\r\n }\r\n\r\n /**\r\n * Pops the top context from the stack and merges its paragraph properties into the new top context.\r\n * If only one context remains, nothing is popped.\r\n * @returns The popped MTextContext, or undefined if the stack has only one context.\r\n */\r\n pop(): MTextContext | undefined {\r\n if (this.stack.length <= 1) return undefined;\r\n const popped = this.stack.pop()!;\r\n // Merge paragraph properties into the new top context\r\n const top = this.stack[this.stack.length - 1];\r\n if (JSON.stringify(top.paragraph) !== JSON.stringify(popped.paragraph)) {\r\n top.paragraph = { ...popped.paragraph };\r\n }\r\n return popped;\r\n }\r\n\r\n /**\r\n * Returns the current (top) context on the stack.\r\n */\r\n get current(): MTextContext {\r\n return this.stack[this.stack.length - 1];\r\n }\r\n\r\n /**\r\n * Returns the current stack depth (number of nested blocks), not counting the root context.\r\n */\r\n get depth(): number {\r\n return this.stack.length - 1;\r\n }\r\n\r\n /**\r\n * Returns the root (bottom) context, which represents the global formatting state.\r\n * Used for paragraph property application.\r\n */\r\n get root(): MTextContext {\r\n return this.stack[0];\r\n }\r\n\r\n /**\r\n * Replaces the current (top) context with the given context.\r\n * @param ctx The new context to set as the current context.\r\n */\r\n setCurrent(ctx: MTextContext) {\r\n this.stack[this.stack.length - 1] = ctx;\r\n }\r\n}\r\n\r\n/**\r\n * Configuration options for the MText parser.\r\n * These options control how the parser behaves during tokenization and property handling.\r\n */\r\nexport interface MTextParserOptions {\r\n /**\r\n * Whether to yield PROPERTIES_CHANGED tokens when formatting properties change.\r\n * When true, the parser will emit tokens whenever properties like color, font, or alignment change.\r\n * When false, property changes are applied silently to the context without generating tokens.\r\n * @default false\r\n */\r\n yieldPropertyCommands?: boolean;\r\n /**\r\n * Whether to reset paragraph parameters when encountering a new paragraph token.\r\n * When true, paragraph properties (indent, margins, alignment, tab stops) are reset to defaults\r\n * at the start of each new paragraph.\r\n * @default false\r\n */\r\n resetParagraphParameters?: boolean;\r\n /**\r\n * Custom decoder function for MIF (Multibyte Interchange Format) codes.\r\n * If provided, this function will be used instead of the default decodeMultiByteChar.\r\n * The function receives the hex code string and should return the decoded character.\r\n * @param hex - Hex code string (e.g., \"C4E3\" or \"1A2B3\")\r\n * @returns Decoded character or empty square (▯) if invalid\r\n * @default undefined (uses default decoder)\r\n */\r\n mifDecoder?: (hex: string) => string;\r\n /**\r\n * The length of MIF hex codes to parse. MIF codes in AutoCAD can vary in length\r\n * depending on the specific SHX big font used (typically 4 or 5 digits).\r\n * If not specified, the parser will try to auto-detect the length by attempting\r\n * to match 4 digits first, then 5 digits if needed.\r\n * @default undefined (auto-detect)\r\n */\r\n mifCodeLength?: 4 | 5 | 'auto';\r\n}\r\n\r\n/**\r\n * Main parser class for MText content\r\n */\r\nexport class MTextParser {\r\n private scanner: TextScanner;\r\n private ctxStack: ContextStack;\r\n private continueStroke: boolean = false;\r\n private yieldPropertyCommands: boolean;\r\n private resetParagraphParameters: boolean;\r\n private inStackContext: boolean = false;\r\n private mifDecoder: (hex: string) => string;\r\n private mifCodeLength: 4 | 5 | 'auto';\r\n\r\n /**\r\n * Creates a new MTextParser instance\r\n * @param content - The MText content to parse\r\n * @param ctx - Optional initial MText context\r\n * @param options - Parser options\r\n */\r\n constructor(content: string, ctx?: MTextContext, options: MTextParserOptions = {}) {\r\n this.scanner = new TextScanner(content);\r\n const initialCtx = ctx ?? new MTextContext();\r\n this.ctxStack = new ContextStack(initialCtx);\r\n this.yieldPropertyCommands = options.yieldPropertyCommands ?? false;\r\n this.resetParagraphParameters = options.resetParagraphParameters ?? false;\r\n this.mifDecoder = options.mifDecoder ?? this.decodeMultiByteChar.bind(this);\r\n this.mifCodeLength = options.mifCodeLength ?? 'auto';\r\n }\r\n\r\n /**\r\n * Decode multi-byte character from hex code\r\n * @param hex - Hex code string (e.g. \"C4E3\" or \"1A2B3\")\r\n * @returns Decoded character or empty square if invalid\r\n */\r\n private decodeMultiByteChar(hex: string): string {\r\n try {\r\n // For 5-digit codes, return placeholder directly\r\n if (hex.length === 5) {\r\n const prefix = hex[0];\r\n\r\n // Notes:\r\n // I know AutoCAD uses prefix 1 for Shift-JIS, 2 for big5, and 5 for gbk.\r\n // But I don't know whether there are other prefixes and their meanings.\r\n let encoding = 'gbk';\r\n if (prefix === '1') {\r\n encoding = 'shift-jis';\r\n } else if (prefix === '2') {\r\n encoding = 'big5';\r\n }\r\n const bytes = new Uint8Array([\r\n parseInt(hex.substr(1, 2), 16),\r\n parseInt(hex.substr(3, 2), 16),\r\n ]);\r\n const decoder = new TextDecoder(encoding);\r\n const result = decoder.decode(bytes);\r\n return result;\r\n } else if (hex.length === 4) {\r\n // For 4-digit hex codes, decode as 2-byte character\r\n const bytes = new Uint8Array([\r\n parseInt(hex.substr(0, 2), 16),\r\n parseInt(hex.substr(2, 2), 16),\r\n ]);\r\n\r\n // Try GBK first\r\n const gbkDecoder = new TextDecoder('gbk');\r\n const gbkResult = gbkDecoder.decode(bytes);\r\n if (gbkResult !== '▯') {\r\n return gbkResult;\r\n }\r\n\r\n // Try BIG5 if GBK fails\r\n const big5Decoder = new TextDecoder('big5');\r\n const big5Result = big5Decoder.decode(bytes);\r\n if (big5Result !== '▯') {\r\n return big5Result;\r\n }\r\n }\r\n\r\n return '▯';\r\n } catch {\r\n return '▯';\r\n }\r\n }\r\n\r\n /**\r\n * Extract MIF hex code from scanner\r\n * @param length - The length of the hex code to extract (4 or 5), or 'auto' to detect\r\n * @returns The extracted hex code, or null if not found\r\n */\r\n private extractMifCode(length: 4 | 5 | 'auto'): string | null {\r\n if (length === 'auto') {\r\n // Try 5 digits first if available, then fall back to 4 digits\r\n const code5 = this.scanner.tail.match(/^[0-9A-Fa-f]{5}/)?.[0];\r\n if (code5) {\r\n return code5;\r\n }\r\n const code4 = this.scanner.tail.match(/^[0-9A-Fa-f]{4}/)?.[0];\r\n if (code4) {\r\n return code4;\r\n }\r\n return null;\r\n } else {\r\n const code = this.scanner.tail.match(new RegExp(`^[0-9A-Fa-f]{${length}}`))?.[0];\r\n return code || null;\r\n }\r\n }\r\n\r\n /**\r\n * Push current context onto the stack\r\n */\r\n private pushCtx(): void {\r\n this.ctxStack.push(this.ctxStack.current);\r\n }\r\n\r\n /**\r\n * Pop context from the stack\r\n */\r\n private popCtx(): void {\r\n this.ctxStack.pop();\r\n }\r\n\r\n /**\r\n * Parse stacking expression (numerator/denominator)\r\n * @returns Tuple of [TokenType.STACK, [numerator, denominator, type]]\r\n */\r\n private parseStacking(): [TokenType, [string, string, string]] {\r\n const scanner = new TextScanner(this.extractExpression(true));\r\n let numerator = '';\r\n let denominator = '';\r\n let stackingType = '';\r\n\r\n const getNextChar = (): [string, boolean] => {\r\n let c = scanner.peek();\r\n let escape = false;\r\n if (c.charCodeAt(0) < 32) {\r\n c = ' ';\r\n }\r\n if (c === '\\\\') {\r\n escape = true;\r\n scanner.consume(1);\r\n c = scanner.peek();\r\n }\r\n scanner.consume(1);\r\n return [c, escape];\r\n };\r\n\r\n const parseNumerator = (): [string, string] => {\r\n let word = '';\r\n while (scanner.hasData) {\r\n const [c, escape] = getNextChar();\r\n // Check for stacking operators first\r\n if (!escape && (c === '/' || c === '#' || c === '^')) {\r\n return [word, c];\r\n }\r\n word += c;\r\n }\r\n return [word, ''];\r\n };\r\n\r\n const parseDenominator = (skipLeadingSpace: boolean): string => {\r\n let word = '';\r\n let skipping = skipLeadingSpace;\r\n while (scanner.hasData) {\r\n const [c, escape] = getNextChar();\r\n if (skipping && c === ' ') {\r\n continue;\r\n }\r\n skipping = false;\r\n // Stop at terminator unless escaped\r\n if (!escape && c === ';') {\r\n break;\r\n }\r\n word += c;\r\n }\r\n return word;\r\n };\r\n\r\n [numerator, stackingType] = parseNumerator();\r\n if (stackingType) {\r\n // Only skip leading space for caret divider\r\n denominator = parseDenominator(stackingType === '^');\r\n }\r\n\r\n // Special case for \\S^!/^?;\r\n if (numerator === '' && denominator.includes('I/')) {\r\n return [TokenType.STACK, [' ', ' ', '/']];\r\n }\r\n\r\n // Handle caret as a stacking operator\r\n if (stackingType === '^') {\r\n return [TokenType.STACK, [numerator, denominator, '^']];\r\n }\r\n\r\n return [TokenType.STACK, [numerator, denominator, stackingType]];\r\n }\r\n\r\n /**\r\n * Parse MText properties\r\n * @param cmd - The property command to parse\r\n * @returns Property changes if yieldPropertyCommands is true and changes occurred\r\n */\r\n private parseProperties(cmd: string): TokenData[TokenType.PROPERTIES_CHANGED] | void {\r\n const prevCtx = this.ctxStack.current.copy();\r\n const newCtx = this.ctxStack.current.copy();\r\n switch (cmd) {\r\n case 'L':\r\n newCtx.underline = true;\r\n this.continueStroke = true;\r\n break;\r\n case 'l':\r\n newCtx.underline = false;\r\n if (!newCtx.hasAnyStroke) {\r\n this.continueStroke = false;\r\n }\r\n break;\r\n case 'O':\r\n newCtx.overline = true;\r\n this.continueStroke = true;\r\n break;\r\n case 'o':\r\n newCtx.overline = false;\r\n if (!newCtx.hasAnyStroke) {\r\n this.continueStroke = false;\r\n }\r\n break;\r\n case 'K':\r\n newCtx.strikeThrough = true;\r\n this.continueStroke = true;\r\n break;\r\n case 'k':\r\n newCtx.strikeThrough = false;\r\n if (!newCtx.hasAnyStroke) {\r\n this.continueStroke = false;\r\n }\r\n break;\r\n case 'A':\r\n this.parseAlign(newCtx);\r\n break;\r\n case 'C':\r\n this.parseAciColor(newCtx);\r\n break;\r\n case 'c':\r\n this.parseRgbColor(newCtx);\r\n break;\r\n case 'H':\r\n this.parseHeight(newCtx);\r\n break;\r\n case 'W':\r\n this.parseWidth(newCtx);\r\n break;\r\n case 'Q':\r\n this.parseOblique(newCtx);\r\n break;\r\n case 'T':\r\n this.parseCharTracking(newCtx);\r\n break;\r\n case 'p':\r\n this.parseParagraphProperties(newCtx);\r\n break;\r\n case 'f':\r\n case 'F':\r\n this.parseFontProperties(newCtx);\r\n break;\r\n default:\r\n throw new Error(`Unknown command: ${cmd}`);\r\n }\r\n\r\n // Update continueStroke based on current stroke state\r\n this.continueStroke = newCtx.hasAnyStroke;\r\n newCtx.continueStroke = this.continueStroke;\r\n // Use setCurrent to replace the current context\r\n this.ctxStack.setCurrent(newCtx);\r\n\r\n if (this.yieldPropertyCommands) {\r\n const changes = this.getPropertyChanges(prevCtx, newCtx);\r\n if (Object.keys(changes).length > 0) {\r\n return {\r\n command: cmd,\r\n changes,\r\n depth: this.ctxStack.depth,\r\n };\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Get property changes between two contexts\r\n * @param oldCtx - The old context\r\n * @param newCtx - The new context\r\n * @returns Object containing changed properties\r\n */\r\n private getPropertyChanges(\r\n oldCtx: MTextContext,\r\n newCtx: MTextContext\r\n ): TokenData[TokenType.PROPERTIES_CHANGED]['changes'] {\r\n const changes: TokenData[TokenType.PROPERTIES_CHANGED]['changes'] = {};\r\n\r\n if (oldCtx.underline !== newCtx.underline) {\r\n changes.underline = newCtx.underline;\r\n }\r\n if (oldCtx.overline !== newCtx.overline) {\r\n changes.overline = newCtx.overline;\r\n }\r\n if (oldCtx.strikeThrough !== newCtx.strikeThrough) {\r\n changes.strikeThrough = newCtx.strikeThrough;\r\n }\r\n if (oldCtx.color.aci !== newCtx.color.aci) {\r\n changes.aci = newCtx.color.aci;\r\n }\r\n if (oldCtx.color.rgbValue !== newCtx.color.rgbValue) {\r\n changes.rgb = newCtx.color.rgb;\r\n }\r\n if (oldCtx.align !== newCtx.align) {\r\n changes.align = newCtx.align;\r\n }\r\n if (JSON.stringify(oldCtx.fontFace) !== JSON.stringify(newCtx.fontFace)) {\r\n changes.fontFace = newCtx.fontFace;\r\n }\r\n if (\r\n oldCtx.capHeight.value !== newCtx.capHeight.value ||\r\n oldCtx.capHeight.isRelative !== newCtx.capHeight.isRelative\r\n ) {\r\n changes.capHeight = newCtx.capHeight;\r\n }\r\n if (\r\n oldCtx.widthFactor.value !== newCtx.widthFactor.value ||\r\n oldCtx.widthFactor.isRelative !== newCtx.widthFactor.isRelative\r\n ) {\r\n changes.widthFactor = newCtx.widthFactor;\r\n }\r\n if (\r\n oldCtx.charTrackingFactor.value !== newCtx.charTrackingFactor.value ||\r\n oldCtx.charTrackingFactor.isRelative !== newCtx.charTrackingFactor.isRelative\r\n ) {\r\n changes.charTrackingFactor = newCtx.charTrackingFactor;\r\n }\r\n if (oldCtx.oblique !== newCtx.oblique) {\r\n changes.oblique = newCtx.oblique;\r\n }\r\n if (JSON.stringify(oldCtx.paragraph) !== JSON.stringify(newCtx.paragraph)) {\r\n // Only include changed paragraph properties\r\n const changedProps: Partial<ParagraphProperties> = {};\r\n if (oldCtx.paragraph.indent !== newCtx.paragraph.indent) {\r\n changedProps.indent = newCtx.paragraph.indent;\r\n }\r\n if (oldCtx.paragraph.align !== newCtx.paragraph.align) {\r\n changedProps.align = newCtx.paragraph.align;\r\n }\r\n if (oldCtx.paragraph.left !== newCtx.paragraph.left) {\r\n changedProps.left = newCtx.paragraph.left;\r\n }\r\n if (oldCtx.paragraph.right !== newCtx.paragraph.right) {\r\n changedProps.right = newCtx.paragraph.right;\r\n }\r\n if (JSON.stringify(oldCtx.paragraph.tabs) !== JSON.stringify(newCtx.paragraph.tabs)) {\r\n changedProps.tabs = newCtx.paragraph.tabs;\r\n }\r\n if (Object.keys(changedProps).length > 0) {\r\n changes.paragraph = changedProps;\r\n }\r\n }\r\n\r\n return changes;\r\n }\r\n\r\n /**\r\n * Parse alignment property\r\n * @param ctx - The context to update\r\n */\r\n private parseAlign(ctx: MTextContext): void {\r\n const char = this.scanner.get();\r\n if ('012'.includes(char)) {\r\n ctx.align = parseInt(char) as MTextLineAlignment;\r\n } else {\r\n ctx.align = MTextLineAlignment.BOTTOM;\r\n }\r\n this.consumeOptionalTerminator();\r\n }\r\n\r\n /**\r\n * Parse height property\r\n * @param ctx - The context to update\r\n */\r\n private parseHeight(ctx: MTextContext): void {\r\n const expr = this.extractFloatExpression(true);\r\n if (expr) {\r\n try {\r\n if (expr.endsWith('x')) {\r\n // For height command, treat x suffix as relative value\r\n ctx.capHeight = {\r\n value: parseFloat(expr.slice(0, -1)),\r\n isRelative: true,\r\n };\r\n } else {\r\n ctx.capHeight = {\r\n value: parseFloat(expr),\r\n isRelative: false,\r\n };\r\n }\r\n } catch {\r\n // If parsing fails, treat the entire command as literal text\r\n this.scanner.consume(-expr.length); // Rewind to before the expression\r\n return;\r\n }\r\n }\r\n this.consumeOptionalTerminator();\r\n }\r\n\r\n /**\r\n * Parse width property\r\n * @param ctx - The context to update\r\n */\r\n private parseWidth(ctx: MTextContext): void {\r\n const expr = this.extractFloatExpression(true);\r\n if (expr) {\r\n try {\r\n if (expr.endsWith('x')) {\r\n // For width command, treat x suffix as relative value\r\n ctx.widthFactor = {\r\n value: parseFloat(expr.slice(0, -1)),\r\n isRelative: true,\r\n };\r\n } else {\r\n ctx.widthFactor = {\r\n value: parseFloat(expr),\r\n isRelative: false,\r\n };\r\n }\r\n } catch {\r\n // If parsing fails, treat the entire command as literal text\r\n this.scanner.consume(-expr.length); // Rewind to before the expression\r\n return;\r\n }\r\n }\r\n this.consumeOptionalTerminator();\r\n }\r\n\r\n /**\r\n * Parse character tracking property\r\n * @param ctx - The context to update\r\n */\r\n private parseCharTracking(ctx: MTextContext): void {\r\n const expr = this.extractFloatExpression(true);\r\n if (expr) {\r\n try {\r\n if (expr.endsWith('x')) {\r\n // For tracking command, treat x suffix as relative value\r\n ctx.charTrackingFactor = {\r\n value: Math.abs(parseFloat(expr.slice(0, -1))),\r\n isRelative: true,\r\n };\r\n } else {\r\n ctx.charTrackingFactor = {\r\n value: Math.abs(parseFloat(expr)),\r\n isRelative: false,\r\n };\r\n }\r\n } catch {\r\n // If parsing fails, treat the entire command as literal text\r\n this.scanner.consume(-expr.length); // Rewind to before the expression\r\n return;\r\n }\r\n }\r\n this.consumeOptionalTerminator();\r\n }\r\n\r\n /**\r\n * Parse float value or factor\r\n * @param value - Current value to apply factor to\r\n * @returns New value\r\n */\r\n private parseFloatValueOrFactor(value: number): number {\r\n const expr = this.extractFloatExpression(true);\r\n if (expr) {\r\n if (expr.endsWith('x')) {\r\n const factor = parseFloat(expr.slice(0, -1));\r\n value *= factor; // Allow negative factors\r\n } else {\r\n value = parseFloat(expr); // Allow negative values\r\n }\r\n }\r\n return value;\r\n }\r\n\r\n /**\r\n * Parse oblique angle property\r\n * @param ctx - The context to update\r\n */\r\n private parseOblique(ctx: MTextContext): void {\r\n const obliqueExpr = this.extractFloatExpression(false);\r\n if (obliqueExpr) {\r\n ctx.oblique = parseFloat(obliqueExpr);\r\n }\r\n this.consumeOptionalTerminator();\r\n }\r\n\r\n /**\r\n * Parse ACI color property\r\n * @param ctx - The context to update\r\n */\r\n private parseAciColor(ctx: MTextContext): void {\r\n const aciExpr = this.extractIntExpression();\r\n if (aciExpr) {\r\n const aci = parseInt(aciExpr);\r\n if (aci < 257) {\r\n ctx.color.aci = aci;\r\n }\r\n }\r\n this.consumeOptionalTerminator();\r\n }\r\n\r\n /**\r\n * Parse RGB color property\r\n * @param ctx - The context to update\r\n */\r\n private parseRgbColor(ctx: MTextContext): void {\r\n const rgbExpr = this.extractIntExpression();\r\n if (rgbExpr) {\r\n const value = parseInt(rgbExpr) & 0xffffff;\r\n ctx.color.rgbValue = value;\r\n }\r\n this.consumeOptionalTerminator();\r\n }\r\n\r\n /**\r\n * Extract float expression from scanner\r\n * @param relative - Whether to allow relative values (ending in 'x')\r\n * @returns Extracted expression\r\n */\r\n private extractFloatExpression(relative: boolean = false): string {\r\n const pattern = relative\r\n ? /^[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?x?/\r\n : /^[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?/;\r\n const match = this.scanner.tail.match(pattern);\r\n if (match) {\r\n const result = match[0];\r\n this.scanner.consume(result.length);\r\n return result;\r\n }\r\n return '';\r\n }\r\n\r\n /**\r\n * Extract integer expression from scanner\r\n * @returns Extracted expression\r\n */\r\n private extractIntExpression(): string {\r\n const match = this.scanner.tail.match(/^\\d+/);\r\n if (match) {\r\n const result = match[0];\r\n this.scanner.consume(result.length);\r\n return result;\r\n }\r\n return '';\r\n }\r\n\r\n /**\r\n * Extract expression until semicolon or end\r\n * @param escape - Whether to handle escaped semicolons\r\n * @returns Extracted expression\r\n */\r\n private extractExpression(escape: boolean = false): string {\r\n const stop = this.scanner.find(';', escape);\r\n if (stop < 0) {\r\n const expr = this.scanner.tail;\r\n this.scanner.consume(expr.length);\r\n return expr;\r\n }\r\n // Check if the semicolon is escaped by looking at the previous character\r\n const prevChar = this.scanner.peek(stop - this.scanner.currentIndex - 1);\r\n const isEscaped = prevChar === '\\\\';\r\n const expr = this.scanner.tail.slice(0, stop - this.scanner.currentIndex + (isEscaped ? 1 : 0));\r\n this.scanner.consume(expr.length + 1);\r\n return expr;\r\n }\r\n\r\n /**\r\n * Parse font properties\r\n * @param ctx - The context to update\r\n */\r\n private parseFontProperties(ctx: MTextContext): void {\r\n const parts = this.extractExpression().split('|');\r\n if (parts.length > 0 && parts[0]) {\r\n const name = parts[0];\r\n let style: FontStyle = 'Regular';\r\n let weight = 400;\r\n\r\n for (const part of parts.slice(1)) {\r\n if (part.startsWith('b1')) {\r\n weight = 700;\r\n } else if (part === 'i' || part.startsWith('i1')) {\r\n style = 'Italic';\r\n } else if (part === 'i0' || part.startsWith('i0')) {\r\n style = 'Regular';\r\n }\r\n }\r\n\r\n ctx.fontFace = {\r\n family: name,\r\n style,\r\n weight,\r\n };\r\n }\r\n }\r\n\r\n /**\r\n * Parse paragraph properties from the MText content\r\n * Handles properties like indentation, alignment, and tab stops\r\n * @param ctx - The context to update\r\n */\r\n private parseParagraphProperties(ctx: MTextContext): void {\r\n const scanner = new TextScanner(this.extractExpression());\r\n /** Current indentation value */\r\n let indent = ctx.paragraph.indent;\r\n /** Left margin value */\r\n let left = ctx.paragraph.left;\r\n /** Right margin value */\r\n let right = ctx.paragraph.right;\r\n /** Current paragraph alignment */\r\n let align = ctx.paragraph.align;\r\n /** Array of tab stop positions and types */\r\n let tabStops: (number | string)[] = [];\r\n\r\n /**\r\n * Parse a floating point number from the scanner's current position\r\n * Handles optional sign, decimal point, and scientific notation\r\n * @returns The parsed float value, or 0 if no valid number is found\r\n */\r\n const parseFloatValue = (): number => {\r\n const match = scanner.tail.match(/^[+-]?\\d+(?:\\.\\d*)?(?:[eE][+-]?\\d+)?/);\r\n if (match) {\r\n const value = parseFloat(match[0]);\r\n scanner.consume(match[0].length);\r\n while (scanner.peek() === ',') {\r\n scanner.consume(1);\r\n }\r\n return value;\r\n }\r\n return 0;\r\n };\r\n\r\n while (scanner.hasData) {\r\n const cmd = scanner.get();\r\n switch (cmd) {\r\n case 'i': // Indentation\r\n indent = parseFloatValue();\r\n break;\r\n case 'l': // Left margin\r\n left = parseFloatValue();\r\n break;\r\n case 'r': // Right margin\r\n right = parseFloatValue();\r\n break;\r\n case 'x': // Skip\r\n break;\r\n case 'q': {\r\n // Alignment\r\n const adjustment = scanner.get();\r\n align = CHAR_TO_ALIGN[adjustment] || MTextParagraphAlignment.DEFAULT;\r\n while (scanner.peek() === ',') {\r\n scanner.consume(1);\r\n }\r\n break;\r\n }\r\n case 't': // Tab stops\r\n tabStops = [];\r\n while (scanner.hasData) {\r\n const type = scanner.peek();\r\n if (type === 'r' || type === 'c') {\r\n scanner.consume(1);\r\n const value = parseFloatValue();\r\n tabStops.push(type + value.toString());\r\n } else {\r\n const value = parseFloatValue();\r\n if (!isNaN(value)) {\r\n tabStops.push(value);\r\n } else {\r\n scanner.consume(1);\r\n }\r\n }\r\n }\r\n break;\r\n }\r\n }\r\n\r\n ctx.paragraph = {\r\n indent,\r\n left,\r\n right,\r\n align,\r\n tabs: tabStops,\r\n };\r\n }\r\n\r\n /**\r\n * Consume optional terminator (semicolon)\r\n */\r\n private consumeOptionalTerminator(): void {\r\n if (this.scanner.peek() === ';') {\r\n this.scanner.consume(1);\r\n }\r\n }\r\n\r\n /**\r\n * Parse MText content into tokens\r\n * @yields MTextToken objects\r\n */\r\n *parse(): Generator<MTextToken> {\r\n const wordToken = TokenType.WORD;\r\n const spaceToken = TokenType.SPACE;\r\n let followupToken: TokenType | null = null;\r\n\r\n function resetParagraph(ctx: MTextContext): Partial<ParagraphProperties> {\r\n const prev = { ...ctx.paragraph };\r\n ctx.paragraph = {\r\n indent: 0,\r\n left: 0,\r\n right: 0,\r\n align: MTextParagraphAlignment.DEFAULT,\r\n tabs: [],\r\n };\r\n const changed: Partial<ParagraphProperties> = {};\r\n if (prev.indent !== 0) changed.indent = 0;\r\n if (prev.left !== 0) changed.left = 0;\r\n if (prev.right !== 0) changed.right = 0;\r\n if (prev.align !== MTextParagraphAlignment.DEFAULT)\r\n changed.align = MTextParagraphAlignment.DEFAULT;\r\n if (JSON.stringify(prev.tabs) !== JSON.stringify([])) changed.tabs = [];\r\n return changed;\r\n }\r\n\r\n const nextToken = (): [TokenType, TokenData[TokenType]] => {\r\n let word = '';\r\n while (this.scanner.hasData) {\r\n let escape = false;\r\n let letter = this.scanner.peek();\r\n const cmdStartIndex = this.scanner.currentIndex;\r\n\r\n // Handle control characters first\r\n if (letter.charCodeAt(0) < 32) {\r\n this.scanner.consume(1); // Always consume the control character\r\n if (letter === '\\t') {\r\n return [TokenType.TABULATOR, null];\r\n }\r\n if (letter === '\\n') {\r\n return [TokenType.NEW_PARAGRAPH, null];\r\n }\r\n letter = ' ';\r\n }\r\n\r\n if (letter === '\\\\') {\r\n if ('\\\\{}'.includes(this.scanner.peek(1))) {\r\n escape = true;\r\n this.scanner.consume(1);\r\n letter = this.scanner.peek();\r\n } else {\r\n if (word) {\r\n return [wordToken, word];\r\n }\r\n this.scanner.consume(1);\r\n const cmd = this.scanner.get();\r\n switch (cmd) {\r\n case '~':\r\n return [TokenType.NBSP, null];\r\n case 'P':\r\n return [TokenType.NEW_PARAGRAPH, null];\r\n case 'N':\r\n return [TokenType.NEW_COLUMN, null];\r\n case 'X':\r\n return [TokenType.WRAP_AT_DIMLINE, null];\r\n case 'S': {\r\n this.inStackContext = true;\r\n const result = this.parseStacking();\r\n this.inStackContext = false;\r\n return result;\r\n }\r\n case 'm':\r\n case 'M':\r\n // Handle multi-byte character encoding (MIF)\r\n if (this.scanner.peek() === '+') {\r\n this.scanner.consume(1); // Consume the '+'\r\n const hexCode = this.extractMifCode(this.mifCodeLength);\r\n if (hexCode) {\r\n this.scanner.consume(hexCode.length);\r\n const decodedChar = this.mifDecoder(hexCode);\r\n if (word) {\r\n return [wordToken, word];\r\n }\r\n return [wordToken, decodedChar];\r\n }\r\n // If no valid hex code found, rewind the '+' character\r\n this.scanner.consume(-1);\r\n }\r\n // If not a valid multi-byte code, treat as literal text\r\n word += '\\\\M';\r\n continue;\r\n case 'U':\r\n // Handle Unicode escape: \\U+XXXX or \\U+XXXXXXXX\r\n if (this.scanner.peek() === '+') {\r\n this.scanner.consume(1); // Consume the '+'\r\n const hexMatch = this.scanner.tail.match(/^[0-9A-Fa-f]{4,8}/);\r\n if (hexMatch) {\r\n const hexCode = hexMatch[0];\r\n this.scanner.consume(hexCode.length);\r\n const codePoint = parseInt(hexCode, 16);\r\n let decodedChar = '';\r\n try {\r\n decodedChar = String.fromCodePoint(codePoint);\r\n } catch {\r\n decodedChar = '▯';\r\n }\r\n if (word) {\r\n return [wordToken, word];\r\n }\r\n return [wordToken, decodedChar];\r\n }\r\n // If no valid hex code found, rewind the '+' character\r\n this.scanner.consume(-1);\r\n }\r\n // If not a valid Unicode code, treat as literal text\r\n word += '\\\\U';\r\n continue;\r\n default:\r\n if (cmd) {\r\n try {\r\n const propertyChanges = this.parseProperties(cmd);\r\n if (this.yieldPropertyCommands && propertyChanges) {\r\n return [TokenType.PROPERTIES_CHANGED, propertyChanges];\r\n }\r\n // After processing a property command, continue with normal parsing\r\n continue;\r\n } catch {\r\n const commandText = this.scanner.tail.slice(\r\n cmdStartIndex,\r\n this.scanner.currentIndex\r\n );\r\n word += commandText;\r\n }\r\n }\r\n }\r\n continue;\r\n }\r\n }\r\n\r\n if (letter === '%' && this.scanner.peek(1) === '%') {\r\n const code = this.scanner.peek(2).toLowerCase();\r\n const specialChar = SPECIAL_CHAR_ENCODING[code];\r\n if (specialChar) {\r\n this.scanner.consume(3);\r\n word += specialChar;\r\n continue;\r\n } else {\r\n /**\r\n * Supports Control Codes: `%%ddd`, where ddd is a three-digit decimal number representing the ASCII code value of the character.\r\n * \r\n * Reference: https://help.autodesk.com/view/ACD/2026/ENU/?guid=GUID-968CBC1D-BA99-4519-ABDD-88419EB2BF92\r\n */\r\n const digits = [code, this.scanner.peek(3), this.scanner.peek(4)];\r\n\r\n if (digits.every((d) => d >= '0' && d <= '9')) {\r\n const charCode = Number.parseInt(digits.join(''), 10);\r\n this.scanner.consume(5);\r\n word += String.fromCharCode(charCode);\r\n } else {\r\n // Skip invalid special character codes\r\n this.scanner.consume(3);\r\n }\r\n\r\n continue;\r\n }\r\n }\r\n\r\n if (letter === ' ') {\r\n if (word) {\r\n this.scanner.consume(1);\r\n followupToken = spaceToken;\r\n return [wordToken, word];\r\n }\r\n this.scanner.consume(1);\r\n return [spaceToken, null];\r\n }\r\n\r\n if (!escape) {\r\n if (letter === '{') {\r\n if (word) {\r\n return [wordToken, word];\r\n }\r\n this.scanner.consume(1);\r\n this.pushCtx();\r\n continue;\r\n } else if (letter === '}') {\r\n if (word) {\r\n return [wordToken, word];\r\n }\r\n this.scanner.consume(1);\r\n // Context restoration with yieldPropertyCommands\r\n if (this.yieldPropertyCommands) {\r\n const prevCtx = this.ctxStack.current;\r\n this.popCtx();\r\n const changes = this.getPropertyChanges(prevCtx, this.ctxStack.current);\r\n if (Object.keys(changes).length > 0) {\r\n return [\r\n TokenType.PROPERTIES_CHANGED,\r\n { command: undefined, changes, depth: this.ctxStack.depth },\r\n ];\r\n }\r\n } else {\r\n this.popCtx();\r\n }\r\n continue;\r\n }\r\n }\r\n\r\n // Handle caret-encoded characters only when not in stack context\r\n if (!this.inStackContext && letter === '^') {\r\n const nextChar = this.scanner.peek(1);\r\n if (nextChar) {\r\n const code = nextChar.charCodeAt(0);\r\n this.scanner.consume(2); // Consume both ^ and the next character\r\n if (code === 32) {\r\n // Space\r\n word += '^';\r\n } else if (code === 73) {\r\n // Tab\r\n if (word) {\r\n return [wordToken, word];\r\n }\r\n return [TokenType.TABULATOR, null];\r\n } else if (code === 74) {\r\n // Line feed\r\n if (word) {\r\n return [wordToken, word];\r\n }\r\n return [TokenType.NEW_PARAGRAPH, null];\r\n } else if (code === 77) {\r\n // Carriage return\r\n // Ignore carriage return\r\n continue;\r\n } else {\r\n word += '▯';\r\n }\r\n continue;\r\n }\r\n }\r\n\r\n this.scanner.consume(1);\r\n if (letter.charCodeAt(0) >= 32) {\r\n word += letter;\r\n }\r\n }\r\n\r\n if (word) {\r\n return [wordToken, word];\r\n }\r\n return [TokenType.NONE, null];\r\n };\r\n\r\n while (true) {\r\n const [type, data] = nextToken.call(this);\r\n if (type) {\r\n yield new MTextToken(type, this.ctxStack.current.copy(), data);\r\n if (type === TokenType.NEW_PARAGRAPH && this.resetParagraphParameters) {\r\n // Reset paragraph properties and emit PROPERTIES_CHANGED if needed\r\n const ctx = this.ctxStack.current;\r\n const changed = resetParagraph(ctx);\r\n if (this.yieldPropertyCommands && Object.keys(changed).length > 0) {\r\n yield new MTextToken(TokenType.PROPERTIES_CHANGED, ctx.copy(), {\r\n command: undefined,\r\n changes: { paragraph: changed },\r\n depth: this.ctxStack.depth,\r\n });\r\n }\r\n }\r\n if (followupToken) {\r\n yield new MTextToken(followupToken, this.ctxStack.current.copy(), null);\r\n followupToken = null;\r\n }\r\n } else {\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Text scanner for parsing MText content\r\n */\r\nexport class TextScanner {\r\n private text: string;\r\n private textLen: number;\r\n private _index: number;\r\n\r\n /**\r\n * Create a new text scanner\r\n * @param text - The text to scan\r\n */\r\n constructor(text: string) {\r\n this.text = text;\r\n this.textLen = text.length;\r\n this._index = 0;\r\n }\r\n\r\n /**\r\n * Get the current index in the text\r\n */\r\n get currentIndex(): number {\r\n return this._index;\r\n }\r\n\r\n /**\r\n * Check if the scanner has reached the end of the text\r\n */\r\n get isEmpty(): boolean {\r\n return this._index >= this.textLen;\r\n }\r\n\r\n /**\r\n * Check if there is more text to scan\r\n */\r\n get hasData(): boolean {\r\n return this._index < this.textLen;\r\n }\r\n\r\n /**\r\n * Get the next character and advance the index\r\n * @returns The next character, or empty string if at end\r\n */\r\n get(): string {\r\n if (this.isEmpty) {\r\n return '';\r\n }\r\n const char = this.text[this._index];\r\n this._index++;\r\n return char;\r\n }\r\n\r\n /**\r\n * Advance the index by the specified count\r\n * @param count - Number of characters to advance\r\n */\r\n consume(count: number = 1): void {\r\n this._index = Math.max(0, Math.min(this._index + count, this.textLen));\r\n }\r\n\r\n /**\r\n * Look at a character without advancing the index\r\n * @param offset - Offset from current position\r\n * @returns The character at the offset position, or empty string if out of bounds\r\n */\r\n peek(offset: number = 0): string {\r\n const index = this._index + offset;\r\n if (index >= this.textLen || index < 0) {\r\n return '';\r\n }\r\n return this.text[index];\r\n }\r\n\r\n /**\r\n * Find the next occurrence of a character\r\n * @param char - The character to find\r\n * @param escape - Whether to handle escaped characters\r\n * @returns Index of the character, or -1 if not found\r\n */\r\n find(char: string, escape: boolean = false): number {\r\n let index = this._index;\r\n while (index < this.textLen) {\r\n if (escape && this.text[index] === '\\\\') {\r\n if (index + 1 < this.textLen) {\r\n if (this.text[index + 1] === char) {\r\n return index + 1;\r\n }\r\n index += 2;\r\n continue;\r\n }\r\n index++;\r\n continue;\r\n }\r\n if (this.text[index] === char) {\r\n return index;\r\n }\r\n index++;\r\n }\r\n return -1;\r\n }\r\n\r\n /**\r\n * Get the remaining text from the current position\r\n */\r\n get tail(): string {\r\n return this.text.slice(this._index);\r\n }\r\n\r\n /**\r\n * Check if the next character is a space\r\n */\r\n isNextSpace(): boolean {\r\n return this.peek() === ' ';\r\n }\r\n\r\n /**\r\n * Consume spaces until a non-space character is found\r\n * @returns Number of spaces consumed\r\n */\r\n consumeSpaces(): number {\r\n let count = 0;\r\n while (this.isNextSpace()) {\r\n this.consume();\r\n count++;\r\n }\r\n return count;\r\n }\r\n}\r\n\r\n/**\r\n * Class to handle ACI and RGB color logic for MText.\r\n *\r\n * This class encapsulates color state for MText, supporting both AutoCAD Color Index (ACI) and RGB color.\r\n * Only one color mode is active at a time: setting an RGB color disables ACI, and vice versa.\r\n * RGB is stored as a single 24-bit integer (0xRRGGBB) for efficient comparison and serialization.\r\n *\r\n * Example usage:\r\n * ```ts\r\n * const color1 = new MTextColor(1); // ACI color\r\n * const color2 = new MTextColor([255, 0, 0]); // RGB color\r\n * const color3 = new MTextColor(); // Default (ACI=256, \"by layer\")\r\n * ```\r\n */\r\nexport class MTextColor {\r\n /**\r\n * The AutoCAD Color Index (ACI) value. Only used if no RGB color is set.\r\n * @default 256 (\"by layer\")\r\n */\r\n private _aci: number | null = 256;\r\n /**\r\n * The RGB color value as a single 24-bit integer (0xRRGGBB), or null if not set.\r\n * @default null\r\n */\r\n private _rgbValue: number | null = null; // Store as 0xRRGGBB or null\r\n\r\n /**\r\n * Create a new MTextColor instance.\r\n * @param color The initial color: number for ACI, [r,g,b] for RGB, or null/undefined for default (ACI=256).\r\n */\r\n constructor(color?: number | RGB | null) {\r\n if (Array.isArray(color)) {\r\n this.rgb = color;\r\n } else if (typeof color === 'number') {\r\n this.aci = color;\r\n } else {\r\n this.aci = 256;\r\n }\r\n }\r\n\r\n /**\r\n * Get the current ACI color value.\r\n * @returns The ACI color (0-256), or null if using RGB.\r\n */\r\n get aci(): number | null {\r\n return this._aci;\r\n }\r\n\r\n /**\r\n * Set the ACI color value. Setting this disables any RGB color.\r\n * @param value The ACI color (0-256), or null to unset.\r\n * @throws Error if value is out of range.\r\n */\r\n set aci(value: number | null) {\r\n if (value === null) {\r\n this._aci = null;\r\n } else if (value >= 0 && value <= 256) {\r\n this._aci = value;\r\n this._rgbValue = null;\r\n } else {\r\n throw new Error('ACI not in range [0, 256]');\r\n }\r\n }\r\n\r\n /**\r\n * Get the current RGB color as a tuple [r, g, b], or null if not set.\r\n * @returns The RGB color tuple, or null if using ACI.\r\n */\r\n get rgb(): RGB | null {\r\n if (this._rgbValue === null) return null;\r\n // Extract R, G, B from 0xRRGGBB\r\n const r = (this._rgbValue >> 16) & 0xff;\r\n const g = (this._rgbValue >> 8) & 0xff;\r\n const b = this._rgbValue & 0xff;\r\n return [r, g, b];\r\n }\r\n\r\n /**\r\n * Set the RGB color. Setting this disables ACI color.\r\n * @param value The RGB color tuple [r, g, b], or null to use ACI.\r\n */\r\n set rgb(value: RGB | null) {\r\n if (value) {\r\n const [r, g, b] = value;\r\n this._rgbValue = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);\r\n this._aci = null;\r\n } else {\r\n this._rgbValue = null;\r\n }\r\n }\r\n\r\n /**\r\n * Returns true if the color is set by RGB, false if by ACI.\r\n */\r\n get isRgb(): boolean {\r\n return this._rgbValue !== null;\r\n }\r\n\r\n /**\r\n * Returns true if the color is set by ACI, false if by RGB.\r\n */\r\n get isAci(): boolean {\r\n return this._rgbValue === null && this._aci !== null;\r\n }\r\n\r\n /**\r\n * Get or set the internal RGB value as a number (0xRRGGBB), or null if not set.\r\n * Setting this will switch to RGB mode and set ACI to null.\r\n */\r\n get rgbValue(): number | null {\r\n return this._rgbValue;\r\n }\r\n\r\n set rgbValue(val: number | null) {\r\n if (val === null) {\r\n this._rgbValue = null;\r\n } else {\r\n this._rgbValue = val & 0xffffff;\r\n this._aci = null;\r\n }\r\n }\r\n\r\n /**\r\n * Returns a deep copy of this color.\r\n * @returns A new MTextColor instance with the same color state.\r\n */\r\n copy(): MTextColor {\r\n const c = new MTextColor();\r\n c._aci = this._aci;\r\n c._rgbValue = this._rgbValue;\r\n return c;\r\n }\r\n\r\n /**\r\n * Returns a plain object for serialization.\r\n * @returns An object with aci, rgb (tuple), and rgbValue (number or null).\r\n */\r\n toObject(): { aci: number | null; rgb: RGB | null; rgbValue: number | null } {\r\n return { aci: this._aci, rgb: this.rgb, rgbValue: this._rgbValue };\r\n }\r\n\r\n /**\r\n * Equality check for color.\r\n * @param other The other MTextColor to compare.\r\n * @returns True if both ACI and RGB values are equal.\r\n */\r\n equals(other: MTextColor): boolean {\r\n return this._aci === other._aci && this._rgbValue === other._rgbValue;\r\n }\r\n}\r\n\r\n/**\r\n * MText context class for managing text formatting state\r\n */\r\nexport class MTextContext {\r\n private _stroke: number = 0;\r\n /** Whether to continue stroke formatting */\r\n continueStroke: boolean = false;\r\n /** Color (ACI or RGB) */\r\n color: MTextColor = new MTextColor();\r\n /** Line alignment */\r\n align: MTextLineAlignment = MTextLineAlignment.BOTTOM;\r\n /** Font face properties */\r\n fontFace: FontFace = { family: '', style: 'Regular', weight: 400 };\r\n /** Capital letter height */\r\n private _capHeight: FactorValue = { value: 1.0, isRelative: false };\r\n /** Character width factor */\r\n private _widthFactor: FactorValue = { value: 1.0, isRelative: false };\r\n /**\r\n * Character tracking factor a multiplier applied to the default spacing between characters in the MText object.\r\n * - Value = 1.0 → Normal spacing.\r\n * - Value < 1.0 → Characters are closer together.\r\n * - Value > 1.0 → Characters are spaced farther apart.\r\n */\r\n private _charTrackingFactor: FactorValue = { value: 1.0, isRelative: false };\r\n /** Oblique angle */\r\n oblique: number = 0.0;\r\n /** Paragraph properties */\r\n paragraph: ParagraphProperties = {\r\n indent: 0,\r\n left: 0,\r\n right: 0,\r\n align: MTextParagraphAlignment.DEFAULT,\r\n tabs: [],\r\n };\r\n\r\n /**\r\n * Get the capital letter height\r\n */\r\n get capHeight(): FactorValue {\r\n return this._capHeight;\r\n }\r\n\r\n /**\r\n * Set the capital letter height\r\n * @param value - Height value\r\n */\r\n set capHeight(value: FactorValue) {\r\n this._capHeight = {\r\n value: Math.abs(value.value),\r\n isRelative: value.isRelative,\r\n };\r\n }\r\n\r\n /**\r\n * Get the character width factor\r\n */\r\n get widthFactor(): FactorValue {\r\n return this._widthFactor;\r\n }\r\n\r\n /**\r\n * Set the character width factor\r\n * @param value - Width factor value\r\n */\r\n set widthFactor(value: FactorValue) {\r\n this._widthFactor = {\r\n value: Math.abs(value.value),\r\n isRelative: value.isRelative,\r\n };\r\n }\r\n\r\n /**\r\n * Get the character tracking factor\r\n */\r\n get charTrackingFactor(): FactorValue {\r\n return this._charTrackingFactor;\r\n }\r\n\r\n /**\r\n * Set the character tracking factor\r\n * @param value - Tracking factor value\r\n */\r\n set charTrackingFactor(value: FactorValue) {\r\n this._charTrackingFactor = {\r\n value: Math.abs(value.value),\r\n isRelative: value.isRelative,\r\n };\r\n }\r\n\r\n /**\r\n * Get the ACI color value\r\n */\r\n get aci(): number | null {\r\n return this.color.aci;\r\n }\r\n\r\n /**\r\n * Set the ACI color value\r\n * @param value - ACI color value (0-256)\r\n * @throws Error if value is out of range\r\n */\r\n set aci(value: number) {\r\n this.color.aci = value;\r\n }\r\n\r\n /**\r\n * Get the RGB color value\r\n */\r\n get rgb(): RGB | null {\r\n return this.color.rgb;\r\n }\r\n\r\n /**\r\n * Set the RGB color value\r\n */\r\n set rgb(value: RGB | null) {\r\n this.color.rgb = value;\r\n }\r\n\r\n /**\r\n * Gets whether the current text should be rendered in italic style.\r\n * @returns {boolean} True if the font style is 'Italic', otherwise false.\r\n */\r\n get italic(): boolean {\r\n return this.fontFace.style === 'Italic';\r\n }\r\n /**\r\n * Sets whether the current text should be rendered in italic style.\r\n * @param value - If true, sets the font style to 'Italic'; if false, sets it to 'Regular'.\r\n */\r\n set italic(value: boolean) {\r\n this.fontFace.style = value ? 'Italic' : 'Regular';\r\n }\r\n\r\n /**\r\n * Gets whether the current text should be rendered in bold style.\r\n * This is primarily used for mesh fonts and affects font selection.\r\n * @returns {boolean} True if the font weight is 700 or higher, otherwise false.\r\n */\r\n get bold(): boolean {\r\n return (this.fontFace.weight || 400) >= 700;\r\n }\r\n /**\r\n * Sets whether the current text should be rendered in bold style.\r\n * This is primarily used for mesh fonts and affects font selection.\r\n * @param value - If true, sets the font weight to 700; if false, sets it to 400.\r\n */\r\n set bold(value: boolean) {\r\n this.fontFace.weight = value ? 700 : 400;\r\n }\r\n\r\n /**\r\n * Get whether text is underlined\r\n */\r\n get underline(): boolean {\r\n return Boolean(this._stroke & MTextStroke.UNDERLINE);\r\n }\r\n\r\n /**\r\n * Set whether text is underlined\r\n * @param value - Whether to underline\r\n */\r\n set underline(value: boolean) {\r\n this._setStrokeState(MTextStroke.UNDERLINE, value);\r\n }\r\n\r\n /**\r\n * Get whether text has strike-through\r\n */\r\n get strikeThrough(): boolean {\r\n return Boolean(this._stroke & MTextStroke.STRIKE_THROUGH);\r\n }\r\n\r\n /**\r\n * Set whether text has strike-through\r\n * @param value - Whether to strike through\r\n */\r\n set strikeThrough(value: boolean) {\r\n this._setStrokeState(MTextStroke.STRIKE_THROUGH, value);\r\n }\r\n\r\n /**\r\n * Get whether text has overline\r\n */\r\n get overline(): boolean {\r\n return Boolean(this._stroke & MTextStroke.OVERLINE);\r\n }\r\n\r\n /**\r\n * Set whether text has overline\r\n * @param value - Whether to overline\r\n */\r\n set overline(value: boolean) {\r\n this._setStrokeState(MTextStroke.OVERLINE, value);\r\n }\r\n\r\n /**\r\n * Check if any stroke formatting is active\r\n */\r\n get hasAnyStroke(): boolean {\r\n return Boolean(this._stroke);\r\n }\r\n\r\n /**\r\n * Set the state of a stroke type\r\n * @param stroke - The stroke type to set\r\n * @param state - Whether to enable or disable the stroke\r\n */\r\n private _setStrokeState(stroke: MTextStroke, state: boolean = true): void {\r\n if (state) {\r\n this._stroke |= stroke;\r\n } else {\r\n this._stroke &= ~stroke;\r\n }\r\n }\r\n\r\n /**\r\n * Create a copy of this context\r\n * @returns A new context with the same properties\r\n */\r\n copy(): MTextContext {\r\n const ctx = new MTextContext();\r\n ctx._stroke = this._stroke;\r\n ctx.continueStroke = this.continueStroke;\r\n ctx.color = this.color.copy();\r\n ctx.align = this.align;\r\n ctx.fontFace = { ...this.fontFace };\r\n ctx._capHeight = { ...this._capHeight };\r\n ctx._widthFactor = { ...this._widthFactor };\r\n ctx._charTrackingFactor = { ...this._charTrackingFactor };\r\n ctx.oblique = this.oblique;\r\n ctx.paragraph = { ...this.paragraph };\r\n return ctx;\r\n }\r\n}\r\n\r\n/**\r\n * Token class for MText parsing\r\n */\r\nexport class MTextToken {\r\n /**\r\n * Create a new MText token\r\n * @param type - The token type\r\n * @param ctx - The text context at this token\r\n * @param data - Optional token data\r\n */\r\n constructor(\r\n public type: TokenType,\r\n public ctx: MTextContext,\r\n public data: TokenData[TokenType]\r\n ) {}\r\n}\r\n"],"names":["TokenType","MTextLineAlignment","MTextParagraphAlignment","MTextStroke","SPECIAL_CHAR_ENCODING","CHAR_TO_ALIGN","rgb2int","rgb","r","g","b","int2rgb","value","escapeDxfLineEndings","text","hasInlineFormattingCodes","getFonts","mtext","removeExtension","fonts","regex","match","fontName","ContextStack","initial","ctx","popped","top","MTextParser","content","options","TextScanner","initialCtx","MTextContext","hex","prefix","encoding","bytes","gbkResult","big5Result","length","code5","_a","code4","_b","_c","scanner","numerator","denominator","stackingType","getNextChar","c","escape","parseNumerator","word","parseDenominator","skipLeadingSpace","skipping","cmd","prevCtx","newCtx","changes","oldCtx","changedProps","char","expr","factor","obliqueExpr","aciExpr","aci","rgbExpr","relative","pattern","result","stop","isEscaped","parts","name","style","weight","part","indent","left","right","align","tabStops","parseFloatValue","adjustment","type","followupToken","resetParagraph","prev","changed","nextToken","letter","cmdStartIndex","hexCode","decodedChar","hexMatch","codePoint","propertyChanges","commandText","code","specialChar","digits","d","charCode","nextChar","data","MTextToken","count","offset","index","MTextColor","color","val","other","stroke","state"],"mappings":"gFAGY,IAAAA,GAAAA,IAEVA,EAAAA,EAAA,KAAO,CAAP,EAAA,OAEAA,EAAAA,EAAA,KAAO,CAAP,EAAA,OAEAA,EAAAA,EAAA,MAAQ,CAAR,EAAA,QAEAA,EAAAA,EAAA,MAAQ,CAAR,EAAA,QAEAA,EAAAA,EAAA,KAAO,CAAP,EAAA,OAEAA,EAAAA,EAAA,UAAY,CAAZ,EAAA,YAEAA,EAAAA,EAAA,cAAgB,CAAhB,EAAA,gBAEAA,EAAAA,EAAA,WAAa,CAAb,EAAA,aAEAA,EAAAA,EAA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