v2 / vlib / os / font / font.v
262 lines · 241 sloc · 6.69 KB · 48c155382fd9091aa7392ba287db9509e36396c9
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
3
4module font
5
6import os
7
8// Variant enumerates the different variants a font can have.
9pub enum Variant {
10 normal = 0
11 bold
12 mono
13 italic
14}
15
16@[if debug_font ?]
17fn debug_font_println(s string) {
18 println(s)
19}
20
21fn is_readable_font_file(font_path string) bool {
22 if font_path == '' {
23 return false
24 }
25 return !font_path.to_lower().ends_with('.ttc') && os.is_file(font_path)
26 && os.is_readable(font_path)
27}
28
29fn use_font_if_readable(font_path string) string {
30 if is_readable_font_file(font_path) {
31 debug_font_println('Using font "${font_path}"')
32 return font_path
33 }
34 return ''
35}
36
37fn find_first_font_in_paths(font_paths []string) string {
38 for font_path in font_paths {
39 if is_readable_font_file(font_path) {
40 return font_path.clone()
41 }
42 }
43 return ''
44}
45
46fn find_first_font_in_dirs(font_dirs []string) string {
47 for font_dir in font_dirs {
48 if !os.is_dir(font_dir) {
49 continue
50 }
51 mut font_paths := os.walk_ext(font_dir, '.ttf')
52 font_paths.sort()
53 font_path := find_first_font_in_paths(font_paths)
54 if font_path != '' {
55 return font_path
56 }
57 }
58 return ''
59}
60
61fn default_user_font_dirs() []string {
62 mut font_dirs := []string{}
63 if config_dir := os.config_dir() {
64 font_dirs << os.join_path(config_dir, 'v', 'fonts')
65 }
66 return font_dirs
67}
68
69fn bundled_font_dirs() []string {
70 return [os.join_path(@VEXEROOT, 'examples', 'assets', 'fonts')]
71}
72
73fn find_fc_match_font(output string) string {
74 return find_first_font_in_paths(output.split('\n'))
75}
76
77// default returns an absolute path to a readable default font.
78// Search order:
79// 1. The env variable `VUI_FONT`.
80// 2. A user-provided fallback font under `${os.config_dir()}/v/fonts`.
81// 3. Platform defaults and `fc-match`.
82// 4. Bundled fonts under `@VEXEROOT/examples/assets/fonts`.
83// NOTE that, in some cases, the function calls out to external OS programs
84// so running this in a hot loop is not advised.
85@[manualfree]
86pub fn default() string {
87 env_font := os.getenv('VUI_FONT')
88 if env_font != '' && os.is_file(env_font) && os.is_readable(env_font) {
89 debug_font_println('Using font "${env_font}"')
90 return env_font
91 }
92 unsafe { env_font.free() }
93 user_font_path := find_first_font_in_dirs(default_user_font_dirs())
94 if user_font_path != '' {
95 debug_font_println('Using font "${user_font_path}"')
96 return user_font_path
97 }
98 $if windows {
99 fonts := ['C:\\Windows\\Fonts\\segoeui.ttf', 'C:\\Windows\\Fonts\\arial.ttf']
100 for system_font in fonts {
101 existing_font := use_font_if_readable(system_font)
102 if existing_font != '' {
103 return existing_font
104 }
105 }
106 }
107 $if macos {
108 fonts := ['/System/Library/Fonts/SFNS.ttf', '/System/Library/Fonts/SFNSText.ttf',
109 '/Library/Fonts/Arial.ttf']
110 for system_font in fonts {
111 existing_font := use_font_if_readable(system_font)
112 if existing_font != '' {
113 return existing_font
114 }
115 }
116 unsafe { fonts.free() }
117 }
118 $if android {
119 xml_files := ['/system/etc/system_fonts.xml', '/system/etc/fonts.xml',
120 '/etc/system_fonts.xml', '/etc/fonts.xml', '/data/fonts/fonts.xml',
121 '/etc/fallback_fonts.xml']
122 font_locations := ['/system/fonts', '/data/fonts']
123 for xml_file in xml_files {
124 if os.is_file(xml_file) && os.is_readable(xml_file) {
125 xml := os.read_file(xml_file) or { continue }
126 lines := xml.split('\n')
127 mut candidate_font := ''
128 for line in lines {
129 if line.contains('<font') {
130 tmp1 := line.all_after('>')
131 tmp2 := tmp1.all_before('<')
132 tmp3 := tmp2.trim(' \n\t\r')
133 mut_assign(candidate_font, tmp3)
134 if candidate_font.contains('.ttf') {
135 for location in font_locations {
136 candidate_path := os.join_path_single(location, candidate_font)
137 existing_font := use_font_if_readable(candidate_path)
138 if existing_font != '' {
139 return existing_font
140 }
141 unsafe { candidate_path.free() }
142 }
143 }
144 unsafe { tmp3.free() }
145 unsafe { tmp2.free() }
146 unsafe { tmp1.free() }
147 }
148 }
149 unsafe { candidate_font.free() }
150 unsafe { lines.free() }
151 unsafe { xml.free() }
152 }
153 }
154 unsafe { font_locations.free() }
155 unsafe { xml_files.free() }
156 }
157 mut fm := os.execute("fc-match --format='%{file}\n' -s")
158 if fm.exit_code == 0 {
159 fc_match_font_path := find_fc_match_font(fm.output)
160 if fc_match_font_path != '' {
161 unsafe { fm.free() }
162 debug_font_println('Using font "${fc_match_font_path}"')
163 return fc_match_font_path
164 }
165 } else {
166 debug_font_println('fc-match failed to fetch system font')
167 }
168 unsafe { fm.free() }
169 bundled_font_path := find_first_font_in_dirs(bundled_font_dirs())
170 if bundled_font_path != '' {
171 debug_font_println('Using font "${bundled_font_path}"')
172 return bundled_font_path
173 }
174 panic('failed to init the font')
175}
176
177// get_path_variant returns the `font_path` file name replaced with the
178// file name of the font's `variant` version if it exists.
179@[manualfree]
180pub fn get_path_variant(font_path string, variant Variant) string {
181 // TODO: find some way to make this shorter and more eye-pleasant
182 // NotoSans, LiberationSans, DejaVuSans, Arial and SFNS should work
183 mut file := os.file_name(font_path)
184 defer { unsafe { file.free() } }
185
186 mut fpath := font_path.replace(file, '')
187 defer { unsafe { fpath.free() } }
188
189 mut_replace(file, '.ttf', '')
190
191 flower := file.to_lower()
192 defer { unsafe { flower.free() } }
193
194 match variant {
195 .normal {}
196 .bold {
197 if fpath.ends_with('-Regular') {
198 mut_replace(file, '-Regular', '-Bold')
199 } else if file.starts_with('DejaVuSans') {
200 mut_plus(file, '-Bold')
201 } else if flower.starts_with('arial') {
202 mut_plus(file, 'bd')
203 } else {
204 mut_plus(file, '-bold')
205 }
206 $if macos {
207 if os.exists('SFNS-bold') {
208 mut_assign(file, 'SFNS-bold')
209 }
210 }
211 }
212 .italic {
213 if file.ends_with('-Regular') {
214 mut_replace(file, '-Regular', '-Italic')
215 } else if file.starts_with('DejaVuSans') {
216 mut_plus(file, '-Oblique')
217 } else if flower.starts_with('arial') {
218 mut_plus(file, 'i')
219 } else {
220 mut_plus(file, 'Italic')
221 }
222 }
223 .mono {
224 if !file.ends_with('Mono-Regular') && file.ends_with('-Regular') {
225 mut_replace(file, '-Regular', 'Mono-Regular')
226 } else if flower.starts_with('arial') {
227 // Arial has no mono variant
228 } else {
229 mut_plus(file, 'Mono')
230 }
231 }
232 }
233
234 res := '${fpath}${file}.ttf'
235 return res
236}
237
238@[manualfree]
239fn mut_replace(s &string, find string, replacement string) {
240 new := (*s).replace(find, replacement)
241 unsafe { s.free() }
242 unsafe {
243 *s = new
244 }
245}
246
247@[manualfree]
248fn mut_plus(s &string, tail string) {
249 new := (*s) + tail
250 unsafe { s.free() }
251 unsafe {
252 *s = new
253 }
254}
255
256@[manualfree]
257fn mut_assign(s &string, value string) {
258 unsafe { s.free() }
259 unsafe {
260 *s = value.clone()
261 }
262}
263