font
Draw text in FontLibC fonts on the CE, in assembly programs and Flash Apps. Flash
Apps have the core methods: default(), load(), count(),
height(), width(), char(), text(), and
text_width() with a string. A font comes from a font pack AppVar, the
kind TIpy-asm’s converter writes: a font asset, or
tipy-asm convfont (see Converting fonts).
[[tool.tipy.assets]]
source = "fonts/myfont.fnt"
kind = "font"
name = "MYFONT"
from ti84 import font, graphics
f = font.load("MYFONT")
if f:
x = 10
for code in range(65, 91): # A to Z
x += font.char(f, code, x, 20, graphics.BLUE)
TIpy also has a built-in 8-pixel font, the size of GraphX’s default font.
default() returns it. It is built into the program, so it needs no AppVar:
from ti84 import font, graphics
f = font.default()
font.char(f, 65, 10, 10, graphics.BLACK)
load() returns a font, or 0 if the AppVar is missing or is not a font pack.
A pack can hold several fonts; font.load("MYFONT", 1) is the second, and
count() says how many there are.
char() draws a character’s pixels and leaves the background, and returns
its width, so adding it up places the next character. height() is the
height of a line, including the font’s space above and below.
Drawing text
text() draws a whole string and returns the x after it, and
text_width() measures one. paragraph() word-wraps text to a width,
breaks at newlines, and returns the y below the last line. Each takes a string
literal, or the address of text, such as a pack’s family() name. With a
background color, each character’s box is filled first; the default of -1
leaves what is behind.
from ti84 import font, graphics
f = font.default()
x = font.text(f, "Score: ", 4, 4, graphics.BLACK, graphics.WHITE)
y = font.paragraph(f, "A long line wraps at the last space that fits.", 4, 20, 120)
Text in arrays
Text that a program builds or receives, such as a chat message, lives in an int
array of character codes. text() and text_width() take a
module-level array in place of a string; start= and count= pick a part of it,
and both stay inside the array, so a count past its end stops at the end.
from ti84 import font, graphics
message = [0] * 40
length = 0
# ... fill message with codes, and set length ...
f = font.default()
x = font.text(f, message, 4, 100, graphics.BLACK, count=length)
Font packs on the calculator
find() walks the calculator’s AppVars and returns the n-th font pack,
whatever its name, so a program can offer every font the user has sent:
from ti84 import font, graphics
n = 0
pack = font.find(0)
while pack:
name = font.family(pack)
if name:
font.text(font.load(pack), name, 4, 4 + n * 12, graphics.BLACK)
n += 1
pack = font.find(n)
load(), count(), and by_style() take a pack from
find(), or a pack’s AppVar name as a string literal.
Choosing a font by style
by_style() works like FontLibC’s fontlib_GetFontByStyle: it returns the
first font in a pack whose height, weight, and styles match, or 0.
from ti84 import font
f = font.by_style("MYFONT", 6, 12, font.NORMAL, font.BOLD, 0, font.ITALIC)
Weights run from font.THIN through LIGHT, NORMAL, MEDIUM, and BOLD to
font.HEAVY (FontLibC’s FONTLIB_BLACK, renamed so it cannot be mistaken for
the color). Styles are font.SERIF, ITALIC, OBLIQUE, and MONOSPACED:
styles_set lists styles the font must have, and styles_unset styles it must
not.
FontLibC itself draws on GraphX’s 8-bit screen, while TIpy programs use the
OS’s 16-bit color screen, so this library reads the font packs and draws them
itself. For text in the OS font, use graphics.text() or graphics.char(). On
classic calculators, only the color constants are available.
Constants
font.BLACK, font.WHITE, font.RED, font.GREEN, font.BLUE,
font.YELLOW, font.CYAN, font.MAGENTA, font.ORANGE, font.PURPLE,
and font.GRAY have the same values as the color constants.
Methods
- default() int
TIpy’s built-in 8-pixel font, built into the program: no AppVar or clibs needed.
- Targets:
CE, Flash App
- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.default()
- load(pack, index=0) int
Font index of a font pack: an AppVar name such as one built by a font asset, or a pack from find(); 0 if it is missing.
- Targets:
CE, Flash App
- Flash App form:
load(name, index)- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.load('MYFONT')
- count(pack) int
How many fonts a font pack holds; 0 if it is not one.
- Targets:
CE, Flash App
- Flash App form:
count(name)- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.count('MYFONT')
- find(n=0) int
The n-th font pack AppVar on the calculator, whatever its name; 0 when there are no more.
- Targets:
CE
- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.find(x)
- family(pack) int
The address of a pack’s font family name, for font.text(); 0 if it has none.
- Targets:
CE
- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.family(x)
- by_style(pack, min_height, max_height, min_weight, max_weight, styles_set, styles_unset) int
As fontlib_GetFontByStyle: the first font in a pack whose height, weight (such as font.BOLD), and styles (font.SERIF, ITALIC, OBLIQUE, MONOSPACED) match; 0 if none.
- Targets:
CE
- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.by_style('MYFONT', 6, 12, font.NORMAL, font.BOLD, 0, font.ITALIC)
- height(font) int
A line’s height in pixels, with the font’s space above and below.
- Targets:
CE, Flash App
- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.height(x)
- width(font, code) int
A character’s width in pixels; 0 if the font lacks it.
- Targets:
CE, Flash App
- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.width(x, 65)
- text(font, text, x, y, color=BLACK, background=-1, *, start=0, count=-1) int
Draw text from pixel x, y and return the x after it: a string literal, the address of text, or a module-level int array of character codes (start and count pick part of it). A background of -1 leaves what is behind.
- Targets:
CE, Flash App
- Flash App form:
text(font, text, x, y, color, background)- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.text(x, 'Hi', 2, y, font.BLUE)
- text_width(font, text, *, start=0, count=-1) int
The width of text in pixels, given as for text().
- Targets:
CE, Flash App
- Flash App form:
text_width(font, text)- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.text_width(x, 'Hi')
- paragraph(font, text, x, y, width, color=BLACK, background=-1) int
Draw text word-wrapped to lines of width pixels, breaking at newlines too; returns the y below it.
- Targets:
CE
- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.paragraph(x, 'Hello there', 2, y, 100)
- char(font, code, x, y, color=BLACK) int
Draw a character at pixel x, y, leaving the background; returns its width.
- Targets:
CE, Flash App
- Note:
Font packs draw on the CE; the classic target draws literal text with graphics.text.
z = font.char(x, 65, y, 10, font.RED)