
Company News
/Security News
Socket Selected for OpenAI's Cybersecurity Grant Program
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.
@mlightcad/mtext-parser
Advanced tools
TypeScript version of AutoCAD MText parser. It is based on ezdxf dxf mtext parser and ported by Cursor. Moveover, unit tests are added based on use case in the specification in next section.
The text formatting is done by inline codes. You can get more information from this page.
the terminator symbol “;” is mandatory to end the command, all chars beyond the “\p” until the next “;” or the end of the string are part of the command
the command arguments are separated by commas “,”
all values are factors for the initial char height of the MTEXT entity, example: char height = 2.5, “\pl1;” set the left paragraph indentation to 1 x 2.5 = 2.5 drawing units.
all values are floating point values, see height command
arguments are “i”, “l”, “r”, “q”, “t”
a “” as argument value, resets the argument to the initial value: “i0”, “l0”, “r0”, the “q” argument most likely depends on the text direction; I haven’t seen “t”. The sequence used by BricsCAD to reset all values is "\pi*,l*,r*,q*,t;"
“i” indentation of the first line relative to the “l” argument as floating point value, “\pi1.5”
“l” left paragraph indentation as floating point value, “\pl1.5”
“r” right paragraph indentation as floating point value, “\pr1.5”
“x” is required if a “q” or a “t” argument is present, the placement of the “x” has no obvious rules
“q” paragraph alignment
“t” tabulator stops as comma separated list, the default tabulator stops are located at 4, 8, 12, …, by defining at least one tabulator stop, the default tabulator stops will be ignored. There 3 kind of tabulator stops: left, right and center adjusted stops, e.g. “pxt1,r5,c8”:
complex example to create a numbered list with two items: "pxi-3,l4t4;1.^Ifirst item\P2.^Isecond item"
a parser should be very flexible, I have seen several different orders of the arguments and placing the sometimes required “x” has no obvious rules.
exporting seems to be safe to follow these three rules:
npm install @mlightcad/mtext-parser
Here's how to use the MText parser in your TypeScript/JavaScript project:
import { MTextParser, TokenType } from '@mlightcad/mtext-parser';
// Basic usage
const parser = new MTextParser('Hello World');
const tokens = Array.from(parser.parse());
// tokens will contain:
// - WORD token with "Hello"
// - SPACE token
// - WORD token with "World"
// Parsing formatted text
const formattedParser = new MTextParser('\\H2.5;Large\\H.5x;Small');
const formattedTokens = Array.from(formattedParser.parse());
// formattedTokens will contain:
// - WORD token with "Large" and capHeight = 2.5
// - WORD token with "Small" and capHeight = 0.5
// Parsing special characters
const specialParser = new MTextParser('Diameter: %%c, Angle: %%d, Tolerance: %%p');
const specialTokens = Array.from(specialParser.parse());
// specialTokens will contain:
// - WORD token with "Diameter: Ø, Angle: °, Tolerance: ±"
// Parsing with context
const ctx = new MTextContext();
ctx.capHeight = 2.0;
ctx.widthFactor = 1.5;
const contextParser = new MTextParser('Text with context', ctx);
const contextTokens = Array.from(contextParser.parse());
// contextTokens will contain tokens with the specified context
// Parsing with property commands
const propertyParser = new MTextParser('\\C1;Red Text', undefined, { yieldPropertyCommands: true });
const propertyTokens = Array.from(propertyParser.parse());
// propertyTokens will contain:
// - PROPERTIES_CHANGED token with the color command
// - WORD token with "Red Text" and aci = 1
// Parsing with paragraph reset after new paragraph
const resetParser = new MTextParser('Line1\\PLine2', undefined, { yieldPropertyCommands: true, resetParagraphParameters: true });
const resetTokens = Array.from(resetParser.parse());
// resetTokens will contain:
// - WORD token with "Line1"
// - NEW_PARAGRAPH token
// - PROPERTIES_CHANGED token with paragraph reset
// - WORD token with "Line2"
The parser produces tokens of the following types:
WORD: Text content with associated formatting contextSPACE: Space characterNBSP: Non-breaking spaceTABULATOR: Tab characterNEW_PARAGRAPH: Paragraph breakNEW_COLUMN: Column breakWRAP_AT_DIMLINE: Wrap at dimension lineSTACK: Stacked fraction with [numerator, denominator, type] dataPROPERTIES_CHANGED: Property change command (only when yieldPropertyCommands is true)Each token includes a context object (MTextContext) that contains the current formatting state:
underline: Whether text is underlinedoverline: Whether text has overlinestrikeThrough: Whether text has strike-throughaci: ACI color value (0-256)rgb: RGB color value [r, g, b]align: Line alignment (BOTTOM, MIDDLE, TOP)fontFace: Font properties (family, style, weight)capHeight: Capital letter heightwidthFactor: Character width factorcharTrackingFactor: Character tracking factoroblique: Oblique angleparagraph: Paragraph properties (indent, margins, alignment, tab stops)The parser handles invalid commands gracefully:
This makes the parser robust for handling real-world MText content that may contain errors or unexpected formatting.
FAQs
AutoCAD MText parser written in TypeScript
The npm package @mlightcad/mtext-parser receives a total of 178 weekly downloads. As such, @mlightcad/mtext-parser popularity was classified as not popular.
We found that @mlightcad/mtext-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.