Commit9a877c5aRecorded22 Oct 2026Repositorysigil-fontconfig

Add dynamic font discovery and listing bindings

Changed
 .gitignore               |   1 +
 LICENSE                  |  26 ++++++++++++++++++++++++
 README.md                |  35 ++++++++++++++++++++++++++++++++
 dev-redirects.sgl        |   4 ++++
 manifest.scm             |   2 ++
 package.sgl              |  27 +++++++++++++++++++++++++
 src/sigil/fontconfig.sgl | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-fontconfig.sgl |  46 ++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 327 insertions(+)
Diff
.gitignoreadded
@@ -0,0 +1 @@
+1
build/
LICENSEadded
@@ -0,0 +1,26 @@
+1
BSD 3-Clause License
+2
+3
Copyright (c) 2026, David Wilson
+4
+5
Redistribution and use in source and binary forms, with or without
+6
modification, are permitted provided that the following conditions are met:
+7
+8
1. Redistributions of source code must retain the above copyright notice,
+9
this list of conditions and the following disclaimer.
+10
2. Redistributions in binary form must reproduce the above copyright notice,
+11
this list of conditions and the following disclaimer in the documentation
+12
and/or other materials provided with the distribution.
+13
3. Neither the name of the copyright holder nor the names of its contributors
+14
may be used to endorse or promote products derived from this software
+15
without specific prior written permission.
+16
+17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+18
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+19
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+20
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+21
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+22
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+23
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+24
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+25
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+26
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.mdadded
@@ -0,0 +1,35 @@
+1
# sigil-fontconfig
+2
+3
Font discovery for Sigil through pure dynamic FFI. The system must supply
+4
`libfontconfig`; 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
```
+14
+15
`fc-pattern` accepts optional `family:`, `style:` and point `size:` fields.
+16
`fc-font-match` applies system substitutions and defaults, returning a font
+17
record or `#f` when no font matches. Fontconfig may substitute an unavailable
+18
family; inspect `fc-font-family` to identify the chosen font.
+19
+20
`fc-font-list` lists installed fonts, optionally filtered by a pattern. Listing
+21
uses 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
+23
data; native patterns and sets are destroyed before returning. File and index
+24
can be passed directly to `ft-new-face` from sigil-freetype.
+25
+26
The package uses the process's default Fontconfig configuration. It initializes
+27
Fontconfig lazily and leaves its shared caches available to other library users.
+28
Missing libraries, invalid arguments and native allocation failures raise errors.
+29
No native pointers escape the API.
+30
+31
Use `guix shell -m manifest.scm` for Fontconfig and the deterministic test font,
+32
then `sigil deps install`, `sigil build` and `sigil test`. Dependencies follow the
+33
same conventions as sigil-freetype, sigil-harfbuzz and sigil-cairo.
+34
+35
BSD-3-Clause.
dev-redirects.sgladded
@@ -0,0 +1,4 @@
+1
(redirects
+2
repos: (list
+3
(for-repo url: "codeberg:sigil/sigil"
+4
use: (from-path dir: "../sigil"))))
manifest.scmadded
@@ -0,0 +1,2 @@
+1
;; Native runtime dependency plus a deterministic test font.
+2
(specifications->manifest (list "fontconfig" "font-jetbrains-mono"))
package.sgladded
@@ -0,0 +1,27 @@
+1
;;; Font discovery through the system Fontconfig library.
+2
(package
+3
name: "sigil-fontconfig"
+4
version: "0.8.1"
+5
sigil: "0.20"
+6
description: "Fontconfig font discovery bindings for Sigil"
+7
url: "https://codeberg.org/sigil/sigil-fontconfig"
+8
license: "BSD-3-Clause"
+9
authors: (list "David Wilson <david@daviwil.com>")
+10
+11
configs: (list
+12
(config name: 'dev output-dir: "build/dev" debug?: #t optimize: 0)
+13
(config name: 'release output-dir: "build/release" debug?: #f optimize: 2))
+14
+15
dependencies: (list
+16
(from-git url: "codeberg:sigil/sigil" package: "sigil-stdlib" version: ">=0.21")
+17
(from-git url: "codeberg:sigil/sigil-ffi" version: "^0.16"))
+18
+19
dev-dependencies: (list
+20
(from-git url: "codeberg:sigil/sigil" package: "sigil-test" version: ">=0.21")
+21
(from-git url: "codeberg:sigil/sigil" package: "sigil-test-runner" version: ">=0.21"))
+22
+23
tasks: (list
+24
(task name: 'build description: "Compile sigil-fontconfig modules"
+25
steps: (list
+26
(compile-sigil-modules sources: "src/**/*.sgl"
+27
output-dir: (config-output-subdir "lib"))))))
src/sigil/fontconfig.sgladded
@@ -0,0 +1,186 @@
+1
;;; Font discovery with dynamic FFI. All public results are copied Sigil data;
+2
;;; native patterns and font sets are released before a call returns.
+3
(define-library (sigil fontconfig)
+4
(import (sigil ffi)
+5
(sigil struct))
+6
(export fc-pattern fc-pattern? fc-pattern-family fc-pattern-style fc-pattern-size
+7
fc-font-match fc-font-list
+8
fc-font? fc-font-file fc-font-index fc-font-family fc-font-style)
+9
(begin
+10
(define-struct fc-pattern
+11
(family default: #f) (style default: #f) (size default: #f))
+12
(define-struct fc-font (file) (index) (family) (style))
+13
+14
(define library (c-library "libfontconfig"))
+15
(define init (c-function library "FcInit" '() ffi/int))
+16
(define pattern-create
+17
(c-function library "FcPatternCreate" '() ffi/pointer))
+18
(define pattern-destroy
+19
(c-function library "FcPatternDestroy" (list ffi/pointer) ffi/void))
+20
(define pattern-add-string
+21
(c-function library "FcPatternAddString"
+22
(list ffi/pointer ffi/string ffi/string) ffi/int))
+23
(define pattern-add-double
+24
(c-function library "FcPatternAddDouble"
+25
(list ffi/pointer ffi/string ffi/double) ffi/int))
+26
(define config-substitute
+27
(c-function library "FcConfigSubstitute"
+28
(list ffi/pointer ffi/pointer ffi/int) ffi/int))
+29
(define default-substitute
+30
(c-function library "FcDefaultSubstitute" (list ffi/pointer) ffi/void))
+31
(define font-match
+32
(c-function library "FcFontMatch"
+33
(list ffi/pointer ffi/pointer ffi/pointer) ffi/pointer))
+34
(define pattern-get-string
+35
(c-function library "FcPatternGetString"
+36
(list ffi/pointer ffi/string ffi/int ffi/pointer) ffi/int))
+37
(define pattern-get-integer
+38
(c-function library "FcPatternGetInteger"
+39
(list ffi/pointer ffi/string ffi/int ffi/pointer) ffi/int))
+40
(define object-set-create
+41
(c-function library "FcObjectSetCreate" '() ffi/pointer))
+42
(define object-set-add
+43
(c-function library "FcObjectSetAdd"
+44
(list ffi/pointer ffi/string) ffi/int))
+45
(define object-set-destroy
+46
(c-function library "FcObjectSetDestroy" (list ffi/pointer) ffi/void))
+47
(define font-list
+48
(c-function library "FcFontList"
+49
(list ffi/pointer ffi/pointer ffi/pointer) ffi/pointer))
+50
(define font-set-destroy
+51
(c-function library "FcFontSetDestroy" (list ffi/pointer) ffi/void))
+52
(define font-set-layout
+53
(c-struct-layout count: ffi/int capacity: ffi/int fonts: ffi/pointer))
+54
+55
;; The default configuration is shared with other users of the library.
+56
;; Do not call FcFini and invalidate their caches.
+57
(define initialized? #f)
+58
+59
(define (check-native result operation)
+60
(unless (and result (not (equal? result 0)))
+61
(error (string-append operation " failed"))))
+62
+63
(define (valid-name? value)
+64
(or (not value)
+65
(and (string? value)
+66
(> (string-length value) 0)
+67
(let loop ((i 0))
+68
(or (= i (string-length value))
+69
(and (not (= (char->integer (string-ref value i)) 0))
+70
(loop (+ i 1))))))))
+71
+72
(define (validate specification)
+73
(unless (fc-pattern? specification)
+74
(error "expected an fc-pattern" specification))
+75
(unless (and (valid-name? (fc-pattern-family specification))
+76
(valid-name? (fc-pattern-style specification)))
+77
(error "font family and style must be nonempty strings without NUL, or #f"))
+78
(let ((size (fc-pattern-size specification)))
+79
(unless (or (not size) (and (real? size) (> size 0) (< size 1000000)))
+80
(error "font point size must be positive and finite, or #f" size))))
+81
+82
(define (with-pattern specification body)
+83
(validate specification)
+84
(unless initialized?
+85
(check-native (init) "FcInit")
+86
(set! initialized? #t))
+87
(let ((pattern (pattern-create)))
+88
(check-native pattern "FcPatternCreate")
+89
(dynamic-wind
+90
(lambda () #t)
+91
(lambda ()
+92
(for-each
+93
(lambda (name value)
+94
(when value
+95
(check-native (pattern-add-string pattern name value)
+96
"FcPatternAddString")))
+97
'("family" "style")
+98
(list (fc-pattern-family specification) (fc-pattern-style specification)))
+99
(when (fc-pattern-size specification)
+100
(check-native
+101
(pattern-add-double pattern "size" (fc-pattern-size specification))
+102
"FcPatternAddDouble"))
+103
(body pattern))
+104
(lambda () (pattern-destroy pattern)))))
+105
+106
(define (get-string pattern name)
+107
(let ((out (c-alloc (c-sizeof ffi/pointer))))
+108
(dynamic-wind
+109
(lambda () #t)
+110
(lambda ()
+111
(and (= (pattern-get-string pattern name 0 out) 0)
+112
(let ((value (pointer-ref out ffi/pointer 0)))
+113
(and value (pointer->string value)))))
+114
(lambda () (c-free out)))))
+115
+116
(define (get-index pattern)
+117
(let ((out (c-alloc (c-sizeof ffi/int))))
+118
(dynamic-wind
+119
(lambda () #t)
+120
(lambda ()
+121
(if (= (pattern-get-integer pattern "index" 0 out) 0)
+122
(pointer-ref out ffi/int 0)
+123
0))
+124
(lambda () (c-free out)))))
+125
+126
(define (copy-font pattern)
+127
(let ((file (get-string pattern "file")))
+128
(unless file (error "font match contains no file"))
+129
(fc-font file: file index: (get-index pattern)
+130
family: (get-string pattern "family")
+131
style: (get-string pattern "style"))))
+132
+133
;; Match with the system's substitutions and defaults. An unavailable
+134
;; family may resolve to a fallback; inspect fc-font-family if needed.
+135
;; Returns #f if Fontconfig reports no matching font.
+136
(define (fc-font-match specification)
+137
(with-pattern specification
+138
(lambda (pattern)
+139
(check-native (config-substitute #f pattern 0) "FcConfigSubstitute")
+140
(default-substitute pattern)
+141
(let ((result (c-alloc (c-sizeof ffi/int)))
+142
(matched #f))
+143
(dynamic-wind
+144
(lambda () #t)
+145
(lambda ()
+146
(set! matched (font-match #f pattern result))
+147
(and matched
+148
(begin
+149
(unless (= (pointer-ref result ffi/int 0) 0)
+150
(error "FcFontMatch returned a failed result"))
+151
(copy-font matched))))
+152
(lambda ()
+153
(when matched (pattern-destroy matched))
+154
(c-free result)))))))
+155
+156
;; List exact matches without fallback substitutions. No arguments lists
+157
;; all installed fonts; a pattern limits family/style/size properties.
+158
(define (fc-font-list . options)
+159
(when (> (length options) 1) (error "fc-font-list takes at most one pattern"))
+160
(with-pattern (if (pair? options) (car options) (fc-pattern))
+161
(lambda (pattern)
+162
(let ((objects (object-set-create))
+163
(fonts #f))
+164
(check-native objects "FcObjectSetCreate")
+165
(dynamic-wind
+166
(lambda () #t)
+167
(lambda ()
+168
(for-each
+169
(lambda (name)
+170
(check-native (object-set-add objects name) "FcObjectSetAdd"))
+171
'("file" "index" "family" "style"))
+172
(set! fonts (font-list #f pattern objects))
+173
(check-native fonts "FcFontList")
+174
(let ((count (c-struct-ref fonts font-set-layout count:))
+175
(items (c-struct-ref fonts font-set-layout fonts:)))
+176
(let loop ((i 0) (copied '()))
+177
(if (= i count)
+178
(reverse copied)
+179
(loop (+ i 1)
+180
(cons (copy-font
+181
(pointer-ref items ffi/pointer
+182
(* i (c-sizeof ffi/pointer))))
+183
copied))))))
+184
(lambda ()
+185
(when fonts (font-set-destroy fonts))
+186
(object-set-destroy objects)))))))))
test/test-fontconfig.sgladded
@@ -0,0 +1,46 @@
+1
(import (sigil test)
+2
(sigil fs)
+3
(sigil fontconfig))
+4
+5
(test-group "fontconfig"
+6
(test "family style and size select a usable file and face index"
+7
(let ((font (fc-font-match
+8
(fc-pattern family: "JetBrains Mono" style: "Regular" size: 7))))
+9
(assert-true (fc-font? font))
+10
(assert-equal "JetBrains Mono" (fc-font-family font))
+11
(assert-true (file-exists? (fc-font-file font)))
+12
(assert-true (exact-integer? (fc-font-index font)))
+13
(assert-true (>= (fc-font-index font) 0))))
+14
+15
(test "bold style is honored"
+16
(let ((font (fc-font-match
+17
(fc-pattern family: "JetBrains Mono" style: "Bold" size: 7))))
+18
(assert-equal "Bold" (fc-font-style font))))
+19
+20
(test "listing returns copied records surviving native set destruction"
+21
(let* ((fonts (fc-font-list (fc-pattern family: "JetBrains Mono")))
+22
(saved (and (pair? fonts) (fc-font-file (car fonts)))))
+23
(assert-true (pair? fonts))
+24
(assert-true (pair? (fc-font-list)))
+25
(assert-true (file-exists? saved))
+26
(for-each
+27
(lambda (font)
+28
(assert-true (file-exists? (fc-font-file font)))
+29
(assert-equal "JetBrains Mono" (fc-font-family font)))
+30
fonts)))
+31
+32
(test "listing an unavailable family is empty while matching falls back"
+33
(let ((pattern (fc-pattern family: "Sigil nonexistent font 8e9c60")))
+34
(assert-equal '() (fc-font-list pattern))
+35
(assert-true (fc-font? (fc-font-match pattern)))))
+36
+37
(test "invalid patterns are rejected before native calls"
+38
(assert-error (fc-font-match "sans"))
+39
(assert-error (fc-font-match (fc-pattern family: "")))
+40
(assert-error (fc-font-match (fc-pattern family: 3)))
+41
(assert-error (fc-font-match (fc-pattern style: 3)))
+42
(assert-error (fc-font-match (fc-pattern size: 0)))
+43
(assert-error (fc-font-match (fc-pattern size: -1)))
+44
(assert-error (fc-font-match (fc-pattern size: "7")))
+45
(assert-error
+46
(fc-font-match (fc-pattern family: (string #\A (integer->char 0) #\B))))))