11package main
22
3- import "runtime"
3+ import (
4+ "os"
5+ "runtime"
6+ )
7+
8+ // firstExisting returns the first path that exists on disk, or the last path as fallback.
9+ func firstExisting (paths ... string ) string {
10+ for _ , p := range paths {
11+ if _ , err := os .Stat (p ); err == nil {
12+ return p
13+ }
14+ }
15+ return paths [len (paths )- 1 ]
16+ }
417
518// fontPath returns the OS-specific file path for a given font name.
619// Supported names: "Impact", "Arial", "Arial Bold".
@@ -9,29 +22,62 @@ func fontPath(name string) string {
922 case "windows" :
1023 switch name {
1124 case "Impact" :
12- return `C:\Windows\Fonts\impact.ttf`
25+ return firstExisting (
26+ `C:\Windows\Fonts\impact.ttf` ,
27+ `C:\Windows\Fonts\DejaVuSans-Bold.ttf` ,
28+ )
1329 case "Arial" :
14- return `C:\Windows\Fonts\arial.ttf`
30+ return firstExisting (
31+ `C:\Windows\Fonts\arial.ttf` ,
32+ `C:\Windows\Fonts\LiberationSans-Regular.ttf` ,
33+ )
1534 case "Arial Bold" :
16- return `C:\Windows\Fonts\arialbd.ttf`
35+ return firstExisting (
36+ `C:\Windows\Fonts\arialbd.ttf` ,
37+ `C:\Windows\Fonts\LiberationSans-Bold.ttf` ,
38+ )
1739 }
1840 case "darwin" :
1941 switch name {
2042 case "Impact" :
21- return "/System/Library/Fonts/Supplemental/Impact.ttf"
43+ return firstExisting (
44+ "/System/Library/Fonts/Supplemental/Impact.ttf" ,
45+ "/System/Library/Fonts/Impact.ttf" ,
46+ "/Library/Fonts/Impact.ttf" ,
47+ )
2248 case "Arial" :
23- return "/System/Library/Fonts/Supplemental/Arial.ttf"
49+ return firstExisting (
50+ "/System/Library/Fonts/Supplemental/Arial.ttf" ,
51+ "/System/Library/Fonts/Arial.ttf" ,
52+ "/Library/Fonts/Arial.ttf" ,
53+ )
2454 case "Arial Bold" :
25- return "/System/Library/Fonts/Supplemental/Arial Bold.ttf"
55+ return firstExisting (
56+ "/System/Library/Fonts/Supplemental/Arial Bold.ttf" ,
57+ "/System/Library/Fonts/Arial Bold.ttf" ,
58+ "/Library/Fonts/Arial Bold.ttf" ,
59+ )
2660 }
2761 default : // linux, freebsd, etc.
2862 switch name {
2963 case "Impact" :
30- return "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf"
64+ return firstExisting (
65+ "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf" ,
66+ "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" ,
67+ "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf" ,
68+ )
3169 case "Arial" :
32- return "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf"
70+ return firstExisting (
71+ "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf" ,
72+ "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf" ,
73+ "/usr/share/fonts/TTF/LiberationSans-Regular.ttf" ,
74+ )
3375 case "Arial Bold" :
34- return "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf"
76+ return firstExisting (
77+ "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf" ,
78+ "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf" ,
79+ "/usr/share/fonts/TTF/LiberationSans-Bold.ttf" ,
80+ )
3581 }
3682 }
3783 return name
0 commit comments