375. The font cache
This page explains how the image ships a font cache that every program accepts. Read it if text is slow to appear the first time a program starts, or if the image phase stops in this step.
A fontconfig client that finds no valid cache for a font directory scans every face in it before it can draw text. Without a valid system cache, the first fontconfig program of every user scans every font on the system; on the live medium, which starts from the same root at every boot, that happens at every boot.
script/phases/70_image/085_fontcache.sh writes the system cache over the finished tree into /usr/lib/fontconfig/cache, the directory the fontconfig port is configured with, and checks that fontconfig accepts what it wrote.
Clamping the directory times
FONTS=/usr/share/fonts
CACHE=/usr/lib/fontconfig/cache
if ! command -v fc-cache >/dev/null 2>&1; then
echo "[fonts] fc-cache is not installed — skipping" >&2
exit 0
fi
if [ ! -d "$FONTS" ]; then
echo "FATAL: $FONTS does not exist; nothing installed a font." >&2
exit 1
fi
: "${SOURCE_DATE_EPOCH:?SOURCE_DATE_EPOCH is not set}"
find "$FONTS" -type d -exec touch -h -d "@$SOURCE_DATE_EPOCH" {} +
rm -f "$CACHE"/*.cache-*
script/phases/70_image/085_fontcache.sh, lines 32–46
Without fc-cache the step says so and exits 0. A tree with no /usr/share/fonts has had no font installed, and stops the phase; so does an unset SOURCE_DATE_EPOCH.
fontconfig judges a cache by the modification time of the directory it describes. Under SOURCE_DATE_EPOCH, which every chroot phase sets, fc-cache records the epoch as each directory’s time instead of the real one, so a directory whose real time differs makes its own cache stale: fc-cache deletes that cache as an invalid file, and a client scans the directory again. The touch -h gives every directory under /usr/share/fonts the epoch as its time, so the recorded and real times agree. mksquashfs reads the same variable and clamps every time in system.sfs to the epoch, so the medium keeps the directories at that time.
The old cache files are removed first, so the files counted afterwards are this run’s.
Writing the cache
fc-cache -sf
n=$(find "$CACHE" -name '*.cache-*' | wc -l)
if [ "$n" -eq 0 ]; then
echo "FATAL: fc-cache wrote no cache file under $CACHE." >&2
exit 1
fi
script/phases/70_image/085_fontcache.sh, lines 48–54
-s caches the system directories only, never a user’s, and -f rescans every directory even when its cache looks valid. A run that writes no cache file stops the phase.
Checking it
if fc-cache -sv 2>&1 | grep -Eq 'invalid cache file|caching, new cache contents'; then
echo "FATAL: fontconfig rejects the cache it just wrote under $CACHE." >&2
exit 1
fi
echo "[fonts] $n cache files, $(fc-list | wc -l) faces"
script/phases/70_image/085_fontcache.sh, lines 59–63
The second run, fc-cache -sv, does what a client does at start: for each directory it either skips it, reporting the existing cache as valid, or writes it again (caching, new cache contents), reporting the old file as an invalid cache file. Either of those two lines means a client would rescan, and stops the phase. The log ends with the number of cache files and faces.
Why it runs here
After 040_orphans.sh, so the faces of a removed package are not cached, and before 087_gates.sh, 090_initramfs.sh and 110_iso.sh.
What it leaves
The cache files in /usr/lib/fontconfig/cache on the image, and every directory under /usr/share/fonts with the epoch as its time. On a running system, fc-cache -sv should report every directory as skipping, existing cache is valid.
See also
- Databases stamped into the image: the font cache among the three indexes
- Fonts: the fonts KDOS ships and how to choose one
- Reproducible packages:
SOURCE_DATE_EPOCH