Skip to main content

Render text

libstb reads TrueType and OpenType (CFF) fonts itself, so text needs no system library and no font installed on the machine: you give it the font file. The example uses Vazirmatn, which has Latin, Persian and Arabic letters (SIL Open Font License 1.1, in data/fonts/).

import os

import libstb as stb

os.makedirs("out", exist_ok=True)

font = stb.Font.open("Vazirmatn-Regular.ttf")
size = 32 # the pixel height of the text

# Numbers in pixels for that height.
metrics = font.metrics(size)
print("ascent", round(metrics.ascent), "descent", round(metrics.descent))
print("advance of A:", round(font.advance("A", size)))
print("kerning of A and V:", round(font.kerning("A", "V", size)))
box = font.measure("Hello\nworld", size) # '\n' starts a new line
print("two lines:", round(box.width), "x", round(box.height), "px,", box.lines, "lines")

# draw_text puts a text on a copy of the image; (x, y) is the top-left of the text.
photo = stb.Image.open("astronaut.png")
labeled = photo.draw_text("Hello\nworld", font, size, 20, 16, color="white")

# With another alignment, x is the middle or the right edge of every line.
labeled = labeled.draw_text("centered", font, 24, 256, 110, color="#ffcc00", align="center")
labeled = labeled.draw_text("right", font, 24, 492, 150, color="#ffcc00", align="right")

# Another face is just another Font.
bold = stb.Font.open("Vazirmatn-Bold.ttf")
labeled = labeled.draw_text("Bold", bold, 44, 20, 400, color="white")
labeled.write("out/labeled.png")

# render() gives a whole text as a 1-channel coverage bitmap, for your own colors or masks.
text = font.render("Hello", size)
print("coverage bitmap:", text.bitmap.width, "x", text.bitmap.height)
text.bitmap.write("out/coverage.png")

# One glyph, and a packed sheet of glyphs with the table a renderer needs.
glyph = font.render_glyph("A", size)
print("glyph A:", glyph.bitmap.width, "x", glyph.bitmap.height)
atlas = font.make_atlas("ABCabc123", size, 256, 256)
atlas.image.write("out/atlas.png")
a = atlas["A"]
print("A in the atlas:", a.x0, a.y0, a.x1, a.y1)
Output
ascent 21 descent -11
advance of A: 13
kerning of A and V: -1
two lines: 51 x 64 px, 2 lines
coverage bitmap: 48 x 32
glyph A: 14 x 15
A in the atlas: 37 1 51 16
draw_text, four times
draw_text, four times
render(): coverage bitmap
render(): coverage bitmap
make_atlas()
make_atlas()

Size​

The size you pass is the pixel height of the text, from the bottom of the lowest letter's descent to the top of the highest ascent, not a point size. metrics(size) gives the ascent, the descent and the line gap in pixels for it, advance the width one character takes, kerning the adjustment between two characters (usually negative or zero), and measure the box of a whole text. Lines are split on \n.

Drawing​

draw_text puts a text on a copy of an image and returns the copy. The position (x, y) is the top-left of the text box. With the alignment center or right, x is instead the middle or the right edge of every line. The color is a name ("white"), a hex string, or a number for each channel, and its alpha is honoured, so translucent text works. The text may hang over the edges and is clipped.

Bitmaps​

render returns the text as a one-channel coverage image (255 where the letter is solid, 0 where it is not, and in between at the edges). Use it as a mask with your own colors, a gradient or a texture. render_glyph does the same for one character, and make_atlas packs many characters into one sheet and gives you the table of where each one is, which is what a game or a GPU text renderer needs.

A font you do not trust​

The font reader checks its bounds, but it has not been fuzzed. Load fonts you trust, the way you would for any native font library.

API at a glance​

Open a fontstb.Font.open(path_or_bytes, index=0)
Metrics for a sizefont.metrics(px)
Width of one characterfont.advance("A", px)
Kerningfont.kerning("A", "V", px)
Measure a textfont.measure(text, px)
Draw on an imageimg.draw_text(text, font, px, x, y, color=, align=)
Coverage bitmapfont.render(text, px).bitmap
One glyphfont.render_glyph("A", px)
Glyph sheetfont.make_atlas("ABC", px, w, h), atlas["A"]