Atv0.9.1Repositorysigil-fontconfig
Document

sigil-fontconfig

Font discovery for Sigil through pure dynamic FFI. The system must supply libfontconfig 2.12.5 or later; no C extension or external font lookup process is used.

(import (sigil fontconfig))
(define font
  (fc-font-match (fc-pattern family: "JetBrains Mono" style: "Regular" size: 7)))
(fc-font-file font)
(fc-font-index font)
(fc-font-list (fc-pattern family: "JetBrains Mono"))

fc-pattern accepts optional family:, style: and point size: fields. fc-font-match applies system substitutions and defaults, returning a font record or #f when no font matches. Fontconfig may substitute an unavailable family; inspect fc-font-family to identify the chosen font.

fc-font-list lists installed fonts, optionally filtered by a pattern. Listing uses exact property matching without fallback. Results expose fc-font-file, fc-font-index, fc-font-family and fc-font-style. They contain copied Sigil data; native patterns and sets are destroyed before returning. File and index can be passed directly to ft-new-face from sigil-freetype.

These two legacy functions use the process default and initialize it lazily. They leave its shared caches available to other library users. Legacy listing rejects a native set larger than 65536 records before copying its entries.

For UI discovery independent of a page renderer, use an owned inventory:

(define context
  (make-fc-context
    files: '("fonts/JetBrainsMono-Regular.ttf" "fonts/JetBrainsMono-Bold.ttf")
    directories: '("/usr/share/fonts/truetype/jetbrains-mono")
    cache-directory: "/absolute/private/ui-font-cache"
    max-fonts: 4096))
(dynamic-wind
  (lambda () #f)
  (lambda ()
    (fc-context-match context (fc-pattern family: "JetBrains Mono" style: "Regular"))
    (fc-context-sort context (fc-pattern family: "JetBrains Mono"))
    (fc-context-list context))
  (lambda () (fc-context-close! context)))

The context starts empty and scans only explicit files/directories. It loads no system or user configuration, inherits no cache directories or sysroot, and disables its own automatic rescans. It never replaces the process default or changes environment variables. Directory scanning is synchronous; select specific font roots, not broad filesystem roots. max-fonts: bounds the completed inventory and returned copies (default 4096, positive exact integer below 1000000); it is not a native scan-memory or time limit. The bound counts font patterns, including named instances in variable-font files. Directory inventories require an explicit absolute cache-directory:; create a private writable directory before calling. Only that cache path is loaded into the context. File-only inventories need no disk cache.

Owned matching/sorting applies normal weight, roman slant, normal width and the standard numeric/boolean matching defaults directly. Unspecified point size uses 12 points at 75 DPI. Name languages come from the first selected locale, normalized without a default configuration, with a weak English fallback. The legacy system API still calls Fontconfig's default-substitution function. The owned path never calls it, including on Fontconfig 2.17 and later where that function initializes the process default. The uint64 string-union representation assumes little-endian LP64 targets; the driver checks its layout and weak binding against the native header.

fc-context-match returns one copied record or #f. fc-context-list optionally accepts one pattern. fc-context-sort returns copied candidates in Fontconfig preference order; its default trim?: #t removes candidates adding no character coverage. Use trim?: #f to retain all candidates. Consumers should check actual glyph coverage in their face loader. No native patterns or charsets escape. Results survive later calls and closing their context.

Close is idempotent; subsequent discovery calls raise an error. fc-context-closed? exposes this state. Pointer finalization also covers abandoned contexts, while explicit close clears that finalizer before release. Missing libraries, invalid arguments and native allocation failures raise errors. Static compilation tools may import the module without a dynamic loader; an attempted native operation then reports the saved load failure.

The implementation follows the upstream Fontconfig developer reference for explicit configurations, application font inventories and sorted sets.

Use guix shell -m manifest.scm for Fontconfig, its native test compiler, JetBrains Mono and Noto Sans/Serif, then sigil deps install, sigil build and sigil scripts/ci-test.sgl. Or run the ordered gate directly:

guix shell -m manifest.scm -- sigil scripts/ci-test.sgl

The shipped driver copies just the four test faces into a temporary inventory and supplies private process-default configuration and cache directories. To supply fixtures without the manifest, set SIGIL_FONTCONFIG_TEST_DIRECTORY to an absolute owned directory containing JetBrainsMono-Regular.ttf, JetBrainsMono-Bold.ttf, NotoSans.ttf and NotoSerif.ttf. SIGIL_FONTCONFIG_TEST_CACHE optionally selects an absolute private writable cache directory for the owned-directory tests; otherwise the driver creates one temporarily. These variables are required when running individual test files without the driver. No desktop font configuration is needed.

The driver runs test/test-00-isolation.sgl alone in a fresh process before the other two suites. Its sentinel cache must remain empty after owned matching, sorting and listing, then become nonempty when FcConfigGetCurrent explicitly initializes that default. This positive control makes prior default initialization fail the test. Plain sigil test is not the ordered gate: file discovery order is unspecified. The separate default pointer-identity control checks preservation after initialization.

The driver sets SIGIL_FONTCONFIG_ISOLATION_FIRST=1 only for the fresh isolation process. Without that marker, plain sigil test skips isolation with a clear message; marked runs still reject prior default initialization. It compiles test/value-abi.c, exports SIGIL_FONTCONFIG_VALUE_ABI, and runs the separate by-value layout/weak-binding test. That test reports a skip when no receipt is supplied by the driver.

BSD-3-Clause.