AtlatestRepositorysigil-fontconfig
1# sigil-fontconfig
2
3Font discovery for Sigil through pure dynamic FFI. The system must supply
4`libfontconfig` 2.12.5 or later; no C extension or external font lookup process is used.
5
6```scheme
7(import (sigil fontconfig))
8(define font
9 (fc-font-match (fc-pattern family: "JetBrains Mono" style: "Regular" size: 7)))
10(fc-font-file font)
11(fc-font-index font)
12(fc-font-list (fc-pattern family: "JetBrains Mono"))
13```
15`fc-pattern` accepts optional `family:`, `style:` and point `size:` fields.
16`fc-font-match` applies system substitutions and defaults, returning a font
17record or `#f` when no font matches. Fontconfig may substitute an unavailable
18family; inspect `fc-font-family` to identify the chosen font.
20`fc-font-list` lists installed fonts, optionally filtered by a pattern. Listing
21uses exact property matching without fallback. Results expose `fc-font-file`,
22`fc-font-index`, `fc-font-family` and `fc-font-style`. They contain copied Sigil
23data; native patterns and sets are destroyed before returning. File and index
24can be passed directly to `ft-new-face` from sigil-freetype.
26These two legacy functions use the process default and initialize it lazily.
27They leave its shared caches available to other library users. Legacy listing
28rejects a native set larger than 65536 records before copying its entries.
30For UI discovery independent of a page renderer, use an owned inventory:
32```scheme
33(define context
34 (make-fc-context
35 files: '("fonts/JetBrainsMono-Regular.ttf" "fonts/JetBrainsMono-Bold.ttf")
36 directories: '("/usr/share/fonts/truetype/jetbrains-mono")
37 cache-directory: "/absolute/private/ui-font-cache"
38 max-fonts: 4096))
39(dynamic-wind
40 (lambda () #f)
41 (lambda ()
42 (fc-context-match context (fc-pattern family: "JetBrains Mono" style: "Regular"))
43 (fc-context-sort context (fc-pattern family: "JetBrains Mono"))
44 (fc-context-list context))
45 (lambda () (fc-context-close! context)))
46```
48The context starts empty and scans only explicit files/directories. It loads no
49system or user configuration, inherits no cache directories or sysroot, and
50disables its own automatic rescans. It never replaces the process default or
51changes environment variables. Directory scanning is synchronous; select
52specific font roots, not broad filesystem roots. `max-fonts:` bounds the
53completed inventory and returned copies (default 4096, positive exact integer
54below 1000000); it is not a native scan-memory or time limit. The bound counts
55font patterns, including named instances in variable-font files.
56Directory inventories require an explicit absolute `cache-directory:`;
57create a private writable directory before calling. Only that cache path is
58loaded into the context. File-only inventories need no disk cache.
60Owned matching/sorting applies normal weight, roman slant, normal width and
61the standard numeric/boolean matching defaults directly. Unspecified point
62size uses 12 points at 75 DPI. Name languages come from the first selected
63locale, normalized without a default configuration, with a weak English fallback.
64The legacy system API still calls Fontconfig's default-substitution function.
65The owned path never calls it, including on Fontconfig 2.17 and later where
66that function initializes the process default.
67The uint64 string-union representation assumes little-endian LP64 targets;
68the driver checks its layout and weak binding against the native header.
70`fc-context-match` returns one copied record or `#f`.
71`fc-context-list` optionally accepts one pattern.
72`fc-context-sort` returns copied candidates in Fontconfig preference order;
73its default `trim?: #t` removes candidates adding no character coverage.
74Use `trim?: #f` to retain all candidates. Consumers should check actual glyph
75coverage in their face loader. No native patterns or charsets escape. Results
76survive later calls and closing their context.
78Close is idempotent; subsequent discovery calls raise an error.
79`fc-context-closed?` exposes this state. Pointer finalization also covers
80abandoned contexts, while explicit close clears that finalizer before release.
81Missing libraries, invalid arguments and native allocation failures raise errors.
82Static compilation tools may import the module without a dynamic loader;
83an attempted native operation then reports the saved load failure.
85The implementation follows the upstream
86[Fontconfig developer reference](https://fontconfig.pages.freedesktop.org/fontconfig/fontconfig-devel/)
87for explicit configurations, application font inventories and sorted sets.
89Use `guix shell -m manifest.scm` for Fontconfig, its native test compiler,
90JetBrains Mono and Noto Sans/Serif,
91then `sigil deps install`, `sigil build` and `sigil scripts/ci-test.sgl`.
92Or run the ordered gate directly:
94```text
95guix shell -m manifest.scm -- sigil scripts/ci-test.sgl
96```
98The shipped driver copies just the four test faces into a temporary inventory
99and supplies private process-default configuration and cache directories.
100To supply fixtures without the manifest, set `SIGIL_FONTCONFIG_TEST_DIRECTORY`
101to an absolute owned directory containing `JetBrainsMono-Regular.ttf`,
102`JetBrainsMono-Bold.ttf`, `NotoSans.ttf` and `NotoSerif.ttf`.
103`SIGIL_FONTCONFIG_TEST_CACHE` optionally selects an absolute private writable
104cache directory for the owned-directory tests; otherwise the driver creates
105one temporarily. These variables are required when running individual test
106files without the driver. No desktop font configuration is needed.
108The driver runs `test/test-00-isolation.sgl` alone in a fresh process before
109the other two suites. Its sentinel cache must remain empty after owned
110matching, sorting and listing, then become nonempty when `FcConfigGetCurrent`
111explicitly initializes that default. This positive control makes prior default
112initialization fail the test. Plain `sigil test` is not the ordered gate: file
113discovery order is unspecified. The separate default pointer-identity control
114checks preservation after initialization.
116The driver sets `SIGIL_FONTCONFIG_ISOLATION_FIRST=1` only for the fresh
117isolation process. Without that marker, plain `sigil test` skips isolation
118with a clear message; marked runs still reject prior default initialization.
119It compiles `test/value-abi.c`, exports `SIGIL_FONTCONFIG_VALUE_ABI`, and runs
120the separate by-value layout/weak-binding test. That test reports a skip when
121no receipt is supplied by the driver.
123BSD-3-Clause.