Text Functions
Functions for rendering text, managing fonts, and processing ANSI colors.
Text Drawing
drawText
Draw text at a specified position.
drawText(text, x, y, color, font?)| Parameter | Type | Required | Description |
|---|---|---|---|
| text | string | Yes | The text to draw |
| x | number | Yes | X position |
| y | number | Yes | Y position (baseline) |
| color | string | Yes | Text color |
| font | string | No | Font specification (e.g., “14px Arial”) |
Example:
drawText("Hello World!", 10, 30, "#ffffff", "16px monospace")drawTextWrapped
Draw text with automatic word wrapping at a maximum width.
drawTextWrapped(text, x, y, maxWidth, color, lineHeight?)| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| text | string | Yes | - | The text to draw |
| x | number | Yes | - | X position |
| y | number | Yes | - | Y position of first line |
| maxWidth | number | Yes | - | Maximum width before wrapping |
| color | string | Yes | - | Text color |
| lineHeight | number | No | font size | Line height in pixels |
Example:
local longText = "This is a long paragraph that will automatically wrap to fit within the specified width."
drawTextWrapped(longText, 10, 20, 180, "#ffffff", 18)drawColoredText
Draw text containing ANSI escape codes with proper color rendering.
drawColoredText(text, x, y, font?, defaultColor?)| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| text | string | Yes | - | Text with ANSI color codes |
| x | number | Yes | - | X position |
| y | number | Yes | - | Y position |
| font | string | No | ”14px monospace” | Font specification |
| defaultColor | string | No | ”#ffffff” | Default color for uncolored text |
Example:
-- Text with ANSI codes (e.g., from MUD output)
local coloredText = "\027[31mRed\027[0m and \027[32mGreen\027[0m"
drawColoredText(coloredText, 10, 20, "14px monospace", "#cccccc")Font Management
setFont
Set the current font for subsequent text drawing operations.
setFont(family, size, weight?)| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| family | string | Yes | - | Font family name (e.g., “Arial”, “monospace”) |
| size | number | Yes | - | Font size in pixels |
| weight | string | No | ”normal” | Font weight (“normal”, “bold”, etc.) |
Example:
setFont("Arial", 16, "bold")
drawText("Bold Arial Text", 10, 30, "#ffffff")
setFont("monospace", 14)
drawText("Monospace Text", 10, 60, "#00ff00")measureText
Measure the dimensions of text without drawing it.
local dimensions = measureText(text, font?)
-- dimensions.width, dimensions.height| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| text | string | Yes | - | The text to measure |
| font | string | No | ”14px monospace” | Font specification |
Returns: {width: number, height: number} - Text dimensions in pixels
Example:
local size = measureText("Hello World!", "16px Arial")
print("Width: " .. size.width .. ", Height: " .. size.height)
-- Use for centering text
local text = "Centered"
local size = measureText(text, "14px monospace")
local canvasWidth = getCanvasWidth()
local x = (canvasWidth - size.width) / 2
drawText(text, x, 50, "#ffffff")ANSI Processing
parseAnsiText
Parse text containing ANSI escape codes into an array of styled segments.
local segments = parseAnsiText(text)| Parameter | Type | Required | Description |
|---|---|---|---|
| text | string | Yes | Text containing ANSI escape codes |
Returns: Array of segment objects with the following properties:
| Property | Type | Description |
|---|---|---|
| text | string | The text content of this segment |
| color | string | Foreground color |
| backgroundColor | string? | Background color (if set) |
| bold | boolean? | Bold formatting |
| italic | boolean? | Italic formatting |
| underline | boolean? | Underline formatting |
| charCount | number | Character count (JavaScript string length) |
Example:
local text = "\027[1;31mBold Red\027[0m Normal \027[32mGreen\027[0m"
local segments = parseAnsiText(text)
for i, seg in ipairs(segments) do
print("Text: " .. seg.text .. ", Color: " .. seg.color)
if seg.bold then
print(" (bold)")
end
endstripAnsiCodes
Remove all ANSI escape codes from text, returning plain text.
local plainText = stripAnsiCodes(text)| Parameter | Type | Required | Description |
|---|---|---|---|
| text | string | Yes | Text containing ANSI escape codes |
Returns: string - Plain text with all ANSI codes removed
Example:
local coloredText = "\027[31mRed\027[0m and \027[32mGreen\027[0m"
local plain = stripAnsiCodes(coloredText)
print(plain) -- Output: "Red and Green"
-- Useful for measuring text width without color codes
local width = measureText(stripAnsiCodes(coloredText), "14px monospace").widthColor Utilities
colorize
Process color codes in text and return in various formats.
local result = colorize(text, format?)| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| text | string | Yes | - | Text with color codes |
| format | string | No | ”ansi” | Output format |
Formats:
"ansi"- Return text with ANSI codes"segments"- Return array of styled segments"strip"- Return plain text without codes