Skip to content

font

stable

Built-in 5x7 bitmap font for ASCII text rendering. Convert characters and strings to pixel bitmaps, measure text widths, and word-wrap strings to fit a pixel budget.

use plugin font::{char_to_bitmap_5x7, string_to_bitmap, measure_text, …}
10 functions UI
/ filter jk navigate Esc clear
Functions (10)
  1. char_to_bitmap_5x7 Get 5x7 pixel bitmap for a single character
  2. string_to_bitmap Render a string to a pixel bitmap table
  3. measure_text Compute pixel width and height of a string
  4. has_glyph Check if a character has a glyph in the font
  5. glyph_width Fixed glyph width (always 5)
  6. glyph_height Fixed glyph height (always 7)
  7. font_info Metadata about the built-in font
  8. char_width Pixel width of a single character
  9. text_width Total pixel width of a string with spacing
  10. wrap_text Word-wrap a string to fit a pixel width

Overview

font is a tiny, dependency-free text-rendering toolkit built around a single hard-coded 5x7 bitmap typeface covering printable ASCII (codes 32–126, 95 glyphs). There are no font handles or loaded state to manage: every function is a pure static call on the built-in face, taking plain strings and integers and returning numbers or tables of pixels. Use it when you need to draw or measure text on a low-resolution surface — an LED matrix, a pixel canvas, a terminal sprite — without pulling in a real font engine.

The mental model is two layers. The measurement helpers (char_width, text_width, measure_text, wrap_text, glyph_width, glyph_height, has_glyph, font_info) tell you how big text will be and whether a character is supported. The rasterization helpers (char_to_bitmap_5x7, string_to_bitmap) turn characters and strings into flat tables of 0/1 pixels you can blit onto your own framebuffer. Because the face is fixed-width, every glyph is exactly 5 pixels wide and 7 pixels tall.

Common patterns

Measure a string, then rasterize it at the same scale:

use plugin font::{measure_text, string_to_bitmap}

let dims = measure_text("Hi", 5, 1)
print("needs {dims["width"]}x{dims["height"]} pixels")

let bmp = string_to_bitmap("Hi", 5, 7)
print("rendered {bmp["width"]}x{bmp["height"]}")

Word-wrap a paragraph to a pixel budget and report the width of each line:

use plugin font::{wrap_text, text_width}

let lines = wrap_text("the quick brown fox jumps", 60, 1)
for line in lines {
  print("{text_width(line, 1)}px: {line}")
}

Guard against unsupported characters before drawing them:

use plugin font::{has_glyph, char_to_bitmap_5x7}

let ch = "Z"
if has_glyph(ch) {
  let bits = char_to_bitmap_5x7(ch)
  print("drawing {ch} ({bits.length} pixels)")
} else {
  print("no glyph for {ch}, would fall back to space")
}

Get 5x7 pixel bitmap for a single character

Returns a flat table of 35 integers (0 or 1) representing the 5-column by 7-row bitmap of the given ASCII character. Index order is row-major: index row * 5 + col.

use plugin font::{char_to_bitmap_5x7}

let bits = char_to_bitmap_5x7("A")
// bits[0..4] = top row, bits[5..9] = second row, etc.
print(bits)

Render a string to a pixel bitmap table

Renders a string to a bitmap of size {width, height, pixels}. Each character is scaled from the 5x7 grid to char_width x char_height pixels, with 1 pixel spacing between characters.

use plugin font::{string_to_bitmap}

let bmp = string_to_bitmap("Hi", 5, 7)
print("bitmap size: {bmp["width"]}x{bmp["height"]}")
// bmp["pixels"] is a flat array of 0/1 values

Scan the rendered pixels row by row to draw it as ASCII art:

use plugin font::{string_to_bitmap}

let bmp = string_to_bitmap("OK", 5, 7)
let w = bmp["width"]
let h = bmp["height"]
let px = bmp["pixels"]
for y in 0..h {
  let row = ""
  for x in 0..w {
    row = row + (if px[y * w + x] == 1 { "#" } else { " " })
  }
  print(row)
}

Compute pixel width and height of a string

Returns {width, height} in pixels for text rendered at char_width pixels per character with spacing pixels between characters. Height is always 7 (base font height).

use plugin font::{measure_text}

let dims = measure_text("Hello", 5, 1)
print("width: {dims["width"]}, height: {dims["height"]}")
// width = 5*5 + 4*1 = 29, height = 7

Use it to center a label inside a fixed-width display:

use plugin font::{measure_text}

let display_width = 64
let dims = measure_text("MENU", 5, 1)
let left = (display_width - dims["width"]) / 2
print("draw at x={left}")

Check if a character has a glyph in the font

Returns true if the character is in the supported range (ASCII 32–126). Characters outside this range fall back to a space glyph.

use plugin font::{has_glyph}

print(has_glyph("A"))   // true
print(has_glyph(" "))   // true
print(has_glyph("€"))   // false

Fixed glyph width (always 5)

Returns the fixed width of every glyph in the built-in font, which is always 5 pixels.

use plugin font::{glyph_width, glyph_height}

print(glyph_width())   // 5
print(glyph_height())  // 7

Fixed glyph height (always 7)

Returns the fixed height of every glyph in the built-in font, which is always 7 pixels.

use plugin font::{glyph_height}

print(glyph_height())  // 7

Metadata about the built-in font

Returns metadata about the built-in font: {family, style, weight, glyph_width, glyph_height, glyph_count}.

use plugin font::{font_info}

let info = font_info()
print("{info["family"]} — {info["glyph_count"]} glyphs")
// Built-in 5x7 — 95 glyphs

Pixel width of a single character

Returns the pixel width of a single character (always 5 for this fixed-width font, including unknown characters).

use plugin font::{char_width}

print(char_width("W"))  // 5
print(char_width("i"))  // 5

Total pixel width of a string with spacing

Returns the total pixel width of a string rendered with spacing pixels between each character.

use plugin font::{text_width}

print(text_width("ABC", 1))  // 5*3 + 1*2 = 17
print(text_width("ABC", 0))  // 5*3 = 15

Compare candidates to pick the one that fits a budget:

use plugin font::{text_width}

let budget = 40
for label in ["LOGIN", "SIGN IN", "GO"] {
  let w = text_width(label, 1)
  let fits = if w <= budget { "fits" } else { "too wide" }
  print("{label}: {w}px ({fits})")
}

Word-wrap a string to fit a pixel width

Word-wraps text so each line fits within max_width pixels (using the 5-pixel glyph width and spacing between characters). Returns a table of line strings.

use plugin font::{wrap_text}

let lines = wrap_text("Hello world foo bar", 50, 1)
// Each string in lines fits within 50px
for line in lines {
  print(line)
}

Combine with measure_text to compute the total height of a wrapped block (one glyph height per line):

use plugin font::{wrap_text, glyph_height}

let lines = wrap_text("the quick brown fox jumps over", 60, 1)
let line_count = lines.length
let block_height = line_count * glyph_height()
print("{line_count} lines, {block_height}px tall")
enespt-br