AtlatestRepositorysigil-fontconfig

sigil-fontconfig / tree / testtest-context.sgl

1(import (sigil test) (sigil fs) (sigil process) (sigil list) (sigil path) (sigil ffi)
2 (sigil fontconfig))
3
4(define inventory
5 (or (getenv "SIGIL_FONTCONFIG_TEST_DIRECTORY")
6 (error "Select the owned font inventory")))
7(define regular (path-join inventory "JetBrainsMono-Regular.ttf"))
8(define bold (path-join inventory "JetBrainsMono-Bold.ttf"))
9(define sans (path-join inventory "NotoSans.ttf"))
10(define serif (path-join inventory "NotoSerif.ttf"))
11(define context-cache
12 (or (getenv "SIGIL_FONTCONFIG_TEST_CACHE") (error "Select an owned context cache")))
13(define regular-pattern (fc-pattern family: "JetBrains Mono" style: "Regular"))
14(define test-library (c-library "libfontconfig"))
15(define current-config
16 (c-function test-library "FcConfigGetCurrent" '() ffi/pointer))
18(define (with-context files body)
19 (let ((context (make-fc-context files: files)))
20 (dynamic-wind (lambda () #f)
21 (lambda () (body context))
22 (lambda () (fc-context-close! context)))))
24(test-group "isolated fontconfig"
25 (test "an empty context never borrows the process font inventory"
26 (with-context '()
27 (lambda (context)
28 (assert-equal '() (fc-context-list context))
29 (assert-false (fc-context-match context regular-pattern))
30 (assert-equal '() (fc-context-sort context regular-pattern)))))
32 (test "explicit files select Regular and Bold within their own inventory"
33 (with-context (list regular bold)
34 (lambda (context)
35 (assert-equal 2 (length (fc-context-list context)))
36 (assert-equal regular (fc-font-file (fc-context-match context regular-pattern)))
37 (assert-equal bold
38 (fc-font-file (fc-context-match context
39 (fc-pattern family: "JetBrains Mono" style: "Bold")))))))
41 (test "an explicit directory supplies copied ordered fallback candidates"
42 (let ((context (make-fc-context directories: (list inventory) cache-directory: context-cache)))
43 (dynamic-wind (lambda () #f)
44 (lambda ()
45 (let ((fonts (fc-context-sort context regular-pattern trim?: #f)))
46 ;; Variable fonts can expose several named instances per file.
47 (assert-true (>= (length fonts) 4))
48 (for-each
49 (lambda (file) (assert-true (member file (map fc-font-file fonts))))
50 (list regular bold sans serif))
51 (assert-equal regular (fc-font-file (car fonts)))
52 (assert-true (member sans (map fc-font-file fonts)))
53 (assert-true (member serif (map fc-font-file fonts)))
54 (for-each
55 (lambda (font)
56 (assert-true (file-exists? (fc-font-file font)))
57 (assert-true (member (fc-font-file font) (list regular bold sans serif)))
58 (assert-true (exact-integer? (fc-font-index font))))
59 fonts)))
60 (lambda () (fc-context-close! context)))))
62 (test "coverage trimming retains the primary and adds only useful candidates"
63 (let ((context (make-fc-context directories: (list inventory) cache-directory: context-cache)))
64 (dynamic-wind (lambda () #f)
65 (lambda ()
66 (let ((all (fc-context-sort context regular-pattern trim?: #f))
67 (trimmed (fc-context-sort context regular-pattern)))
68 (assert-equal regular (fc-font-file (car trimmed)))
69 (assert-true (<= 1 (length trimmed) (length all)))
70 (for-each
71 (lambda (font) (assert-true (member (fc-font-file font) (map fc-font-file all))))
72 trimmed)))
73 (lambda () (fc-context-close! context)))))
75 (test "independent contexts do not replace the initialized process default"
76 (let* ((before (fc-font-file (fc-font-match regular-pattern)))
77 (names '("FONTCONFIG_FILE" "FONTCONFIG_PATH" "FONTCONFIG_SYSROOT" "XDG_CACHE_HOME"))
78 (environment (map getenv names)))
79 (with-context (list sans)
80 (lambda (context)
81 (assert-equal sans (fc-font-file (fc-context-match context regular-pattern)))
82 (with-context (list regular)
83 (lambda (other)
84 (assert-equal regular (fc-font-file (fc-context-match other regular-pattern)))))
85 (assert-equal sans (fc-font-file (fc-context-match context regular-pattern)))))
86 (assert-equal before (fc-font-file (fc-font-match regular-pattern)))
87 (assert-equal environment (map getenv names))))
89 (test "a process sysroot does not redirect explicit context inputs"
90 (let ((previous (getenv "FONTCONFIG_SYSROOT")))
91 (dynamic-wind
92 (lambda () (setenv! "FONTCONFIG_SYSROOT" inventory))
93 (lambda ()
94 (with-context (list regular)
95 (lambda (context)
96 (assert-equal regular (fc-font-file (fc-context-match context regular-pattern)))
97 (assert-equal inventory (getenv "FONTCONFIG_SYSROOT")))))
98 (lambda () (setenv! "FONTCONFIG_SYSROOT" previous)))))
100 (test "copied sort and list results survive later calls and context close"
101 (let* ((context (make-fc-context files: (list regular bold sans)))
102 (sorted (fc-context-sort context regular-pattern trim?: #f))
103 (listed (fc-context-list context)))
104 (fc-context-match context (fc-pattern family: "Noto Sans"))
105 (fc-context-close! context)
106 (assert-equal regular (fc-font-file (car sorted)))
107 (for-each
108 (lambda (font) (assert-true (file-exists? (fc-font-file font))))
109 (append sorted listed))))
111 (test "closing is idempotent and all subsequent context operations fail"
112 (let ((context (make-fc-context files: (list regular))))
113 (assert-false (fc-context-closed? context))
114 (fc-context-close! context)
115 (fc-context-close! context)
116 (assert-true (fc-context-closed? context))
117 (assert-error (fc-context-list context))
118 (assert-error (fc-context-match context regular-pattern))
119 (assert-error (fc-context-sort context regular-pattern))))
121 (test "inventory bounds and invalid inputs fail with owned cleanup"
122 (assert-error (make-fc-context files: (list regular bold) max-fonts: 1))
123 (assert-error (make-fc-context max-fonts: 0))
124 (assert-error (make-fc-context max-fonts: 1.5))
125 (assert-error (make-fc-context files: regular))
126 (assert-error (make-fc-context directories: '(3)))
127 (assert-error (make-fc-context directories: (list inventory)))
128 (assert-error (make-fc-context cache-directory: "relative-cache"))
129 (assert-error (make-fc-context files: (list "")))
130 (assert-error (make-fc-context files: (list (string #\A (integer->char 0)))))
131 (assert-error (make-fc-context files: (list (path-join inventory "missing.ttf"))))
132 (with-context (list regular)
133 (lambda (context)
134 (assert-error (fc-context-sort context "sans"))
135 (assert-error (fc-context-sort context regular-pattern trim?: 1))
136 (assert-error (fc-context-list context (fc-pattern) (fc-pattern))))))
138 (test "context creation and destruction preserve the actual default config pointer"
139 (let ((before (pointer-address (current-config))))
140 (with-context (list sans serif)
141 (lambda (context)
142 (fc-context-sort context (fc-pattern family: "Noto Sans"))
143 (assert-equal before (pointer-address (current-config)))))
144 (assert-equal before (pointer-address (current-config))))))