Skip to Content
API ReferenceText Functions

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?)
ParameterTypeRequiredDescription
textstringYesThe text to draw
xnumberYesX position
ynumberYesY position (baseline)
colorstringYesText color
fontstringNoFont 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?)
ParameterTypeRequiredDefaultDescription
textstringYes-The text to draw
xnumberYes-X position
ynumberYes-Y position of first line
maxWidthnumberYes-Maximum width before wrapping
colorstringYes-Text color
lineHeightnumberNofont sizeLine 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?)
ParameterTypeRequiredDefaultDescription
textstringYes-Text with ANSI color codes
xnumberYes-X position
ynumberYes-Y position
fontstringNo”14px monospace”Font specification
defaultColorstringNo”#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?)
ParameterTypeRequiredDefaultDescription
familystringYes-Font family name (e.g., “Arial”, “monospace”)
sizenumberYes-Font size in pixels
weightstringNo”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
ParameterTypeRequiredDefaultDescription
textstringYes-The text to measure
fontstringNo”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)
ParameterTypeRequiredDescription
textstringYesText containing ANSI escape codes

Returns: Array of segment objects with the following properties:

PropertyTypeDescription
textstringThe text content of this segment
colorstringForeground color
backgroundColorstring?Background color (if set)
boldboolean?Bold formatting
italicboolean?Italic formatting
underlineboolean?Underline formatting
charCountnumberCharacter 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 end

stripAnsiCodes

Remove all ANSI escape codes from text, returning plain text.

local plainText = stripAnsiCodes(text)
ParameterTypeRequiredDescription
textstringYesText 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").width

Color Utilities

colorize

Process color codes in text and return in various formats.

local result = colorize(text, format?)
ParameterTypeRequiredDefaultDescription
textstringYes-Text with color codes
formatstringNo”ansi”Output format

Formats:

  • "ansi" - Return text with ANSI codes
  • "segments" - Return array of styled segments
  • "strip" - Return plain text without codes
Last updated on