Skip to main content

Finding fonts

So far the font was a file you named. A program often has to find one: the user picked "Arial Bold" in a menu, a document asks for a family, or an application ships a folder of fonts. libstb can list what a folder holds, tell the faces apart by what they call themselves, and open the one you choose. It never touches fontconfig, the Windows registry or any system service: it walks folders and reads files.

import os

import libstb as stb

# Every face of every font file under the folders you give (subfolders too), sorted by path.
# Without `dirs` it looks in the system font folders: stb.Font.system_font_dirs().
faces = stb.Font.system_fonts(dirs=["."])
for face in faces:
kind = "bold" if face.bold else "regular"
print(f"{os.path.basename(face.path)}: {face.family_name}, {kind}, weight {face.weight} (can be {face.weight_min} to {face.weight_max})")

# Choose by what the faces say about themselves, and open the one you want: a path and the number of the face in its file.
bold = next(face for face in faces if face.family_name == "Vazirmatn" and face.weight == 700)
font = stb.Font.open(bold.path, index=bold.index)
print("opened:", font.family_name, "weight", font.weight, "bold:", "yes" if font.is_bold else "no")

# One file, read for its name and style only: no glyph, outline or metric is parsed.
info = stb.Font.scan("Vazirmatn-Variable.ttf")[0]
print("scanned:", info.family_name, "from weight", info.weight_min, "to", info.weight_max)
Output
Vazirmatn-Bold.ttf: Vazirmatn, bold, weight 700 (can be 700 to 700)
Vazirmatn-Regular.ttf: Vazirmatn, regular, weight 400 (can be 400 to 400)
Vazirmatn-Variable.ttf: Vazirmatn, regular, weight 400 (can be 100 to 900)
opened: Vazirmatn weight 700 bold: yes
scanned: Vazirmatn from weight 100 to 900

Listing​

system_fonts() returns every face of every font file under the operating system's font folders, .ttf, .otf, .ttc, .otc and .woff in any case, sorted by path and by face number inside a file. Give it dirs and it looks in those folders instead, with their subfolders, which is what an application with its own font folder wants. A file that is not a font, or cannot be read, is skipped, so one damaged file does not hide the others. The example looks in the current folder so that its output is the same on every machine.

system_font_dirs() lists the folders the system keeps its fonts in, the ones that exist: /usr/share/fonts and the XDG folders on Linux, /System/Library/Fonts, /Library/Fonts and ~/Library/Fonts on macOS, and %WINDIR%\Fonts on Windows.

What a face says about itself​

Each entry says where the face is (path and index, because a .ttc file holds several) and:

  • family_name: the family a person would say, "Inter" or "DejaVu Sans". It is the typographic family of the font when it has one, else the plain family.
  • legacy_family_name: the family as a program that knows only four styles lists it, such as "DejaVu Sans Light". A CSS font-family that names a weight or a width means this one.
  • bold and italic, from the font's own flags.
  • weight, from 100 (thin) through 400 (regular) and 700 (bold) to 900 (black), and width, from 1 (ultra-condensed) through 5 (normal) to 9 (ultra-expanded). These tell "DejaVu Sans", "DejaVu Sans ExtraLight" and "DejaVu Sans Condensed Bold" apart when they share a family name.
  • weight_min and weight_max, the weights a variable font can be set to. They both equal weight for a font that is not variable.

Without opening​

scan(path_or_bytes) gives the same entries for one file or one block of bytes. It reads the name, OS/2 and head tables only: no character map, outline or metric is parsed, and a file on disk is mapped and not read. Scanning thousands of files is therefore cheap, and opening is left for the one you pick. A file that is none of those formats, or is WOFF2, is a decode error.

Opening what you found​

Font.open(path, index) takes the path and the number of the face. The opened font reports the same things (family_name, is_bold, is_italic, weight, width), so the answer is the same whichever way you ask.

API at a glance​

The system font foldersstb.Font.system_font_dirs()
List facesstb.Font.system_fonts(dirs=None)
Read one filestb.Font.scan(path_or_bytes)
An entryface.path, index, family_name, weight, bold, italic
Open itstb.Font.open(face.path, index=face.index)
Ask an open fontfont.family_name, font.is_bold, font.weight