Skip to content

Commit 31f9afc

Browse files
committed
Fonts: search for fonts in system/user paths
Local copies of fonts will still be prioritized, but this fallback allows us to eventually remove a few TrueType fonts from the repo and rely on system packages for fonts instead.
1 parent ad73e1b commit 31f9afc

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/displayapp/fonts/generate.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,43 @@
99
import argparse
1010
import subprocess
1111

12+
# Common system font locations to try when a font file is not found locally
13+
COMMON_FONT_DIRS = [
14+
'/usr/share/fonts',
15+
'/usr/local/share/fonts',
16+
os.path.expanduser('~/.local/share/fonts'),
17+
os.path.expanduser('~/.fonts'),
18+
]
19+
1220
class Source(object):
1321
def __init__(self, d):
1422
self.file = d['file']
1523
if not os.path.exists(self.file):
16-
self.file = os.path.join(os.path.dirname(sys.argv[0]), self.file)
24+
# First try relative to the script directory (previous behavior)
25+
candidate = os.path.join(os.path.dirname(sys.argv[0]), self.file)
26+
if os.path.exists(candidate):
27+
self.file = candidate
28+
else:
29+
# Fallback: search common system font directories for a matching basename
30+
name = os.path.basename(self.file)
31+
found = None
32+
for font_dir in COMMON_FONT_DIRS:
33+
if not font_dir:
34+
continue
35+
font_dir = os.path.expanduser(font_dir)
36+
if not os.path.isdir(font_dir):
37+
continue
38+
for root, _, files in os.walk(font_dir):
39+
for f in files:
40+
if f.lower() == name.lower():
41+
found = os.path.join(root, f)
42+
break
43+
if found:
44+
break
45+
if found:
46+
break
47+
if found:
48+
self.file = found
1749
self.range = d.get('range')
1850
self.symbols = d.get('symbols')
1951

src/resources/generate-fonts.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,43 @@
99
import argparse
1010
import subprocess
1111

12+
# Common system font locations to try when a font file is not found locally
13+
COMMON_FONT_DIRS = [
14+
'/usr/share/fonts',
15+
'/usr/local/share/fonts',
16+
os.path.expanduser('~/.local/share/fonts'),
17+
os.path.expanduser('~/.fonts'),
18+
]
19+
1220
class Source(object):
1321
def __init__(self, d):
1422
self.file = d['file']
1523
if not os.path.exists(self.file):
16-
self.file = os.path.join(os.path.dirname(sys.argv[0]), self.file)
24+
# First try relative to the script directory (previous behavior)
25+
candidate = os.path.join(os.path.dirname(sys.argv[0]), self.file)
26+
if os.path.exists(candidate):
27+
self.file = candidate
28+
else:
29+
# Fallback: search common system font directories for a matching basename
30+
name = os.path.basename(self.file)
31+
found = None
32+
for font_dir in COMMON_FONT_DIRS:
33+
if not font_dir:
34+
continue
35+
font_dir = os.path.expanduser(font_dir)
36+
if not os.path.isdir(font_dir):
37+
continue
38+
for root, _, files in os.walk(font_dir):
39+
for f in files:
40+
if f.lower() == name.lower():
41+
found = os.path.join(root, f)
42+
break
43+
if found:
44+
break
45+
if found:
46+
break
47+
if found:
48+
self.file = found
1749
self.range = d.get('range')
1850
self.symbols = d.get('symbols')
1951

0 commit comments

Comments
 (0)