Skip to main content

coloX Text API

coloX provides two text output functions and complete font management, supporting Chinese text and custom TrueType fonts.


Contents​

  1. print — device-independent text output
  2. drawTxt_h — host text output
  3. Cursor control
  4. Escape sequences
  5. Font system
  6. Encoding conversion
  7. Complete API index

1. print — Device-Independent Text Output​

print(str [, col])​

print(str, x, y [, col [, opt [, size [, font]]]]) → new_x​

Outputs text on screen. This device-independent printing interface behaves consistently across platforms.

Parameters:

ParameterTypeDefaultDescription
strstringRequiredText to print (UTF-8)
x, ynumberCursor positionPrint position
colnumberCurrent colorText color
optnumber0Print options (bit definitions below)
sizenumber0Font size (see below; values other than 8/16 render as 4×6)
fontstringDefault fontFont name

Returns: Cursor x-coordinate after printing (can be used as text width).

Supported fonts and sizes (print):

FontAvailable sizesDescription
English (default)8 (8×8), 16 (8×16); all other values (including default 0) use 4×6 miniBitmap font
Chinese16 (16×16); some firmware provides 24 (24×24)Chinese bitmap font

print supports the full set of escape sequences; see Escape sequences.

OPT bit definitions:

BitDescription
bit0Align coordinates to the bottom
bit1-2Alignment: 0=left, 1=right, 2=center, 3=do not draw (return width only). Note: unsupported by print; applies to drawTxt_h
bit7Do not start a new line after printing
bit8-15Character spacing (signed 8-bit integer; may be negative)
cls()
print("Hello!", 10, 10, white) -- Simple output
print("Score: 100", 80, 10, yellow, 0, 8) -- Specify the size

2. drawTxt_h — Host Text Output​

drawTxt_h(str, x, y, col, opt, size, font) → width​

Host-dependent printing interface; results vary by platform.

Returns: Text width (with opt bit1-2 = 3, measures width without drawing).

Supported fonts and sizes (drawTxt_h):

Supports all TTF fonts at arbitrary sizes:

Font nameDescription
asciiASCII English
masciiASCII English (medium)
wasciiASCII English (wide)
DIN_NATURE_BoldDIN-style bold
gpGP font
digNumeric font
vampireDecorative font
defaultDefault font (supports Chinese, Japanese, and Korean); unavailable on ColoPLAY. Pass the font name explicitly rather than omitting it (otherwise the system may choose automatically based on size)
Custom font nameFont registered with font_add
cls()
drawTxt_h("Hello", 10, 10, white, 0, 32, "DIN_NATURE_Bold")
drawTxt_h("World", 10, 50, yellow, 0, 16, "mascii")

Note: drawTxt_h does not support print escape sequences.


3. Cursor Control​

cursor(x, y [, col]) → old_x, old_y, old_col​

Sets the text cursor position. Specifying col also sets the current color.

Returns: Three values: previous cursor (x, y) and previous color.

cursor(10, 10)
print("Line 1")
print("Line 2") -- Automatic newline after Line 1

4. Escape Sequences​

print supports the following escape sequences (drawTxt_h does not):

Basic Controls​

EscapeDescription
\nNewline
\rReturn cursor to the beginning of the current line
\bMove cursor back one character
\tMove cursor to the next tab stop

Character Repetition​

EscapeDescription
*{p0}CRepeat character C p0 times. p0 is one character 0-9/a-z (0-9 → 0-9, a-z → 10-35)
print("*4A")    -- Prints "AAAA"

Color and Position​

EscapeDescription
#{p0}Set background color to palette index p0
-{p0}Move cursor horizontally by (16 - p0) pixels
\|{p0}Move cursor vertically by (16 - p0) pixels
+{p0p1}Move cursor horizontally and vertically by (16-p0) and (16-p1) pixels
f{p0}Set font color to palette index p0

Special Instructions (^ Prefix)​

EscapeDescription
^c{p0}Clear screen with palette index p0
^gReturn cursor to its origin
^hSet current position as the origin
^j{p0p1}Set cursor to (p0×character width, p1×line height), absolute coordinates from the canvas origin
^r{p0}Set wrapping width to p0×character width pixels
^s{p0}Set tab width to p0
^x{p0}Set character width to p0
^y{p0}Set line height to p0
^wDouble font width
^tDouble font height
^=Striped display mode
^pDouble font size
^iInvert display (swap background/foreground)
^bFill character spacing (padding) with background color
^#Fill the whole line background with background color
^1 .. ^9Pause for the specified number of frames after printing (typewriter effect)
^d{p0}Pause p0 frames between characters
^-{w/t/=/p/i/b}Cancel the corresponding display setting

Custom Characters​

EscapeDescription
^:AABBCCDDEEFFGGHHDisplay an 8×8 custom character from 8 bytes of hex data
^.ABCDEFGHDisplay an 8×8 custom character from 8 bytes of binary data

5. Font System​

font_add(name, size, ttfdata, cache, rbname) → font_object​

Loads a custom TrueType font.

Parameters:

ParameterTypeDescription
namestringFont name (for subsequent print/drawTxt_h calls)
sizenumberFont size; 0 means a vector font (size specified when used)
ttfdatastringTTF font file data
cachenumberNumber of cached characters; nil uses the default
rbnamestringFallback font name for missing glyphs; nil means no fallback

Returns: Font object. Keep a reference to prevent GC collection (a global variable is recommended).

local dig =
#blob ./my.ttf

font_tt = font_add("my", 0, dig, nil, "default")

function _draw()
cls()
drawTxt_h("Hello 世界", 10, 10, white, 0, 32, "my") -- Intentional Chinese text for font rendering
end

Font subsetting: TTF files are usually large; subset them to contain only the characters actually used.

  1. Put the required characters in a text file, symb.txt
  2. Subset using: java -jar sfnttool.jar -c ./symb.txt ./SourceHanSansCN-Medium.ttf my.ttf

font_close(font)​

Removes a custom font.

font_close(font_tt)
font_tt = nil

font_shrink([global])​

Clears font caches to release memory.

ParameterDescription
global = trueClear custom and global font caches
global = falseClear only custom font caches

Recommendation: Call font_shrink after rendering large fonts to release cached data and avoid excessive memory use.


6. Encoding Conversion​

gb2312_to_utf8(str_gb) → str_utf8​

Converts a GB2312 string to UTF-8. print and drawTxt_h support only UTF-8.

local utf8_str = gb2312_to_utf8(gb2312_data)
print(utf8_str, 10, 10, white)

7. Complete API Index​

FunctionDescription
print(str [, col])Simple text output → new cursor x
print(str, x, y [, col [, opt [, size [, font]]]])Full text output → new cursor x
drawTxt_h(str, x, y, col, opt, size, font)Host text output → text width
cursor(x, y [, col])Set cursor position and color → previous x, y, col
font_add(name, size, ttfdata, cache, rbname)Load a TTF font → font object
font_close(font)Remove a font
font_shrink([global])Clear font caches
gb2312_to_utf8(str)GB2312 → UTF-8 conversion