gg: fix loading variants of many fonts in new_ft() (#6468)

pull/6478/head
Lukas Neubert 2020-09-25 11:52:57 +02:00 committed by GitHub
parent bf8592fe93
commit 2ea94d621f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 4 deletions

View File

@ -7,6 +7,13 @@ import sokol.sgl
import gx import gx
import os import os
enum FontVariant {
normal = 0
bold
mono
italic
}
struct FT { struct FT {
pub: pub:
fons &C.FONScontext fons &C.FONScontext
@ -47,18 +54,17 @@ fn new_ft(c FTConfig) ?&FT{
return none return none
} }
} }
bold_path := get_font_path_variant(c.font_path, .bold)
bold_path := 'SFNS-bold.ttf'// c.font_path.replace('.ttf', '-bold.ttf')
bytes_bold := os.read_bytes(bold_path) or { bytes_bold := os.read_bytes(bold_path) or {
println('failed to load font "$bold_path"') println('failed to load font "$bold_path"')
bytes 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 { bytes_mono:= os.read_bytes(mono_path) or {
println('failed to load font "$mono_path"') println('failed to load font "$mono_path"')
bytes 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 { bytes_italic:= os.read_bytes(italic_path) or {
println('failed to load font "$italic_path"') println('failed to load font "$italic_path"')
bytes bytes
@ -199,3 +205,44 @@ pub fn system_font_path() string {
} }
panic('failed to init the font') 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'
}