coloX Text API
coloX provides two text output functions and complete font management, supporting Chinese text and custom TrueType fonts.
Contents
- print — device-independent text output
- drawTxt_h — host text output
- Cursor control
- Escape sequences
- Font system
- Encoding conversion
- 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:
| Parameter | Type | Default | Description |
|---|---|---|---|
str | string | Required | Text to print (UTF-8) |
x, y | number | Cursor position | Print position |
col | number | Current color | Text color |
opt | number | 0 | Print options (bit definitions below) |
size | number | 0 | Font size (see below; values other than 8/16 render as 4×6) |
font | string | Default font | Font name |
Returns: Cursor x-coordinate after printing (can be used as text width).
Supported fonts and sizes (print):
| Font | Available sizes | Description |
|---|---|---|
| English (default) | 8 (8×8), 16 (8×16); all other values (including default 0) use 4×6 mini | Bitmap font |
| Chinese | 16 (16×16); some firmware provides 24 (24×24) | Chinese bitmap font |
OPT bit definitions:
| Bit | Description |
|---|---|
| bit0 | Align coordinates to the bottom |
| bit1-2 | Alignment: 0=left, 1=right, 2=center, 3=do not draw (return width only). Note: unsupported by print; applies to drawTxt_h |
| bit7 | Do not start a new line after printing |
| bit8-15 | Character 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 name | Description |
|---|---|
ascii | ASCII English |
mascii | ASCII English (medium) |
wascii | ASCII English (wide) |
DIN_NATURE_Bold | DIN-style bold |
gp | GP font |
dig | Numeric font |
vampire | Decorative font |
default | Default 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 name | Font 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_hdoes not support
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
| Escape | Description |
|---|---|
\n | Newline |
\r | Return cursor to the beginning of the current line |
\b | Move cursor back one character |
\t | Move cursor to the next tab stop |
Character Repetition
| Escape | Description |
|---|---|
*{p0}C | Repeat 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
| Escape | Description |
|---|---|
#{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)
| Escape | Description |
|---|---|
^c{p0} | Clear screen with palette index p0 |
^g | Return cursor to its origin |
^h | Set 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 |
^w | Double font width |
^t | Double font height |
^= | Striped display mode |
^p | Double font size |
^i | Invert display (swap background/foreground) |
^b | Fill character spacing (padding) with background color |
^# | Fill the whole line background with background color |
^1 .. ^9 | Pause 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
| Escape | Description |
|---|---|
^:AABBCCDDEEFFGGHH | Display an 8×8 custom character from 8 bytes of hex data |
^.ABCDEFGH | Display 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:
| Parameter | Type | Description |
|---|---|---|
name | string | Font name (for subsequent print/drawTxt_h calls) |
size | number | Font size; 0 means a vector font (size specified when used) |
ttfdata | string | TTF font file data |
cache | number | Number of cached characters; nil uses the default |
rbname | string | Fallback 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.
- Put the required characters in a text file,
symb.txt- 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.
| Parameter | Description |
|---|---|
global = true | Clear custom and global font caches |
global = false | Clear only custom font caches |
Recommendation: Call
font_shrinkafter 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
| Function | Description |
|---|---|
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 |