gg: fix loading variants of many fonts in new_ft() (#6468)
parent
bf8592fe93
commit
2ea94d621f
|
@ -7,6 +7,13 @@ import sokol.sgl
|
|||
import gx
|
||||
import os
|
||||
|
||||
enum FontVariant {
|
||||
normal = 0
|
||||
bold
|
||||
mono
|
||||
italic
|
||||
}
|
||||
|
||||
struct FT {
|
||||
pub:
|
||||
fons &C.FONScontext
|
||||
|
@ -47,18 +54,17 @@ fn new_ft(c FTConfig) ?&FT{
|
|||
return none
|
||||
}
|
||||
}
|
||||
|
||||
bold_path := 'SFNS-bold.ttf'// c.font_path.replace('.ttf', '-bold.ttf')
|
||||
bold_path := get_font_path_variant(c.font_path, .bold)
|
||||
bytes_bold := os.read_bytes(bold_path) or {
|
||||
println('failed to load font "$bold_path"')
|
||||
bytes
|
||||
}
|
||||
mono_path := '/System/Library/Fonts/SFNSMono.ttf'// c.font_path.replace('.ttf', '-bold.ttf')
|
||||
mono_path := get_font_path_variant(c.font_path, .mono)
|
||||
bytes_mono:= os.read_bytes(mono_path) or {
|
||||
println('failed to load font "$mono_path"')
|
||||
bytes
|
||||
}
|
||||
italic_path := '/System/Library/Fonts/SFNSItalic.ttf'
|
||||
italic_path := get_font_path_variant(c.font_path, .italic)
|
||||
bytes_italic:= os.read_bytes(italic_path) or {
|
||||
println('failed to load font "$italic_path"')
|
||||
bytes
|
||||
|
@ -199,3 +205,44 @@ pub fn system_font_path() string {
|
|||
}
|
||||
panic('failed to init the font')
|
||||
}
|
||||
|
||||
fn get_font_path_variant(font_path string, variant FontVariant) string {
|
||||
// TODO: find some way to make this shorter and more eye-pleasant
|
||||
// NotoSans, LiberationSans, DejaVuSans, Arial and SFNS should work
|
||||
mut fpath := font_path.replace('.ttf', '')
|
||||
match variant {
|
||||
.normal {}
|
||||
.bold {
|
||||
if fpath.ends_with('-Regular') {
|
||||
fpath = fpath.replace('-Regular', '-Bold')
|
||||
} else if fpath.starts_with('DejaVuSans') {
|
||||
fpath += '-Bold'
|
||||
} else if fpath.to_lower().starts_with('arial') {
|
||||
fpath += 'bd'
|
||||
} else {
|
||||
fpath += '-bold'
|
||||
}
|
||||
}
|
||||
.italic {
|
||||
if fpath.ends_with('-Regular') {
|
||||
fpath = fpath.replace('-Regular', '-Italic')
|
||||
} else if fpath.starts_with('DejaVuSans') {
|
||||
fpath += '-Oblique'
|
||||
} else if fpath.to_lower().starts_with('arial') {
|
||||
fpath += 'i'
|
||||
} else {
|
||||
fpath += 'Italic'
|
||||
}
|
||||
}
|
||||
.mono {
|
||||
if fpath.ends_with('-Regular') {
|
||||
fpath = fpath.replace('-Regular', 'Mono-Regular')
|
||||
} else if fpath.to_lower().starts_with('arial') {
|
||||
// Arial has no mono variant
|
||||
} else {
|
||||
fpath += 'Mono'
|
||||
}
|
||||
}
|
||||
}
|
||||
return fpath + '.ttf'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue