2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-07-06 20:29:05 +02:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
|
|
|
module gg
|
2020-06-02 15:35:37 +02:00
|
|
|
|
|
|
|
import os
|
2021-08-12 21:31:04 +02:00
|
|
|
import gx
|
2020-06-02 15:35:37 +02:00
|
|
|
|
2020-09-25 11:52:57 +02:00
|
|
|
enum FontVariant {
|
|
|
|
normal = 0
|
|
|
|
bold
|
|
|
|
mono
|
|
|
|
italic
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:40:24 +02:00
|
|
|
struct FTConfig {
|
2020-12-05 00:03:46 +01:00
|
|
|
font_path string
|
2020-12-01 16:30:22 +01:00
|
|
|
custom_bold_font_path string
|
2020-12-05 00:03:46 +01:00
|
|
|
scale f32 = 1.0
|
|
|
|
font_size int
|
2021-01-21 22:07:47 +01:00
|
|
|
bytes_normal []byte
|
|
|
|
bytes_bold []byte
|
|
|
|
bytes_mono []byte
|
|
|
|
bytes_italic []byte
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-23 10:25:40 +01:00
|
|
|
struct StringToRender {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
text string
|
|
|
|
cfg gx.TextCfg
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:46:18 +02:00
|
|
|
pub fn system_font_path() string {
|
|
|
|
env_font := os.getenv('VUI_FONT')
|
|
|
|
if env_font != '' && os.exists(env_font) {
|
|
|
|
return env_font
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
return 'C:\\Windows\\Fonts\\arial.ttf'
|
|
|
|
}
|
|
|
|
mut fonts := ['Ubuntu-R.ttf', 'Arial.ttf', 'LiberationSans-Regular.ttf', 'NotoSans-Regular.ttf',
|
2020-10-18 08:48:13 +02:00
|
|
|
'FreeSans.ttf', 'DejaVuSans.ttf']
|
2020-08-27 06:46:18 +02:00
|
|
|
$if macos {
|
2021-03-24 11:39:09 +01:00
|
|
|
fonts = ['/System/Library/Fonts/SFNS.ttf', '/System/Library/Fonts/SFNSText.ttf',
|
|
|
|
'/Library/Fonts/Arial.ttf',
|
|
|
|
]
|
2020-09-24 16:09:03 +02:00
|
|
|
for font in fonts {
|
|
|
|
if os.is_file(font) {
|
|
|
|
return font
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 06:46:18 +02:00
|
|
|
}
|
2021-02-06 17:36:43 +01:00
|
|
|
$if android {
|
2021-03-24 11:39:09 +01:00
|
|
|
xml_files := ['/system/etc/system_fonts.xml', '/system/etc/fonts.xml',
|
|
|
|
'/etc/system_fonts.xml', '/etc/fonts.xml', '/data/fonts/fonts.xml',
|
|
|
|
'/etc/fallback_fonts.xml',
|
|
|
|
]
|
2021-02-06 17:36:43 +01:00
|
|
|
font_locations := ['/system/fonts', '/data/fonts']
|
|
|
|
for xml_file in xml_files {
|
|
|
|
if os.is_file(xml_file) && os.is_readable(xml_file) {
|
|
|
|
xml := os.read_file(xml_file) or { continue }
|
|
|
|
lines := xml.split('\n')
|
|
|
|
mut candidate_font := ''
|
|
|
|
for line in lines {
|
|
|
|
if line.contains('<font') {
|
|
|
|
candidate_font = line.all_after('>').all_before('<').trim(' \n\t\r')
|
|
|
|
if candidate_font.contains('.ttf') {
|
|
|
|
for location in font_locations {
|
|
|
|
candidate_path := os.join_path(location, candidate_font)
|
|
|
|
if os.is_file(candidate_path) && os.is_readable(candidate_path) {
|
|
|
|
return candidate_path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 19:52:13 +01:00
|
|
|
s := os.execute('fc-list')
|
|
|
|
if s.exit_code != 0 {
|
|
|
|
panic('failed to fetch system fonts')
|
|
|
|
}
|
2020-08-27 06:46:18 +02:00
|
|
|
system_fonts := s.output.split('\n')
|
|
|
|
for line in system_fonts {
|
|
|
|
for font in fonts {
|
|
|
|
if line.contains(font) && line.contains(':') {
|
|
|
|
res := line.all_before(':')
|
|
|
|
println('Using font $res')
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic('failed to init the font')
|
|
|
|
}
|
2020-09-25 11:52:57 +02:00
|
|
|
|
|
|
|
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
|
2021-06-04 19:00:32 +02:00
|
|
|
mut file := os.file_name(font_path)
|
|
|
|
mut fpath := font_path.replace(file, '')
|
|
|
|
file = file.replace('.ttf', '')
|
|
|
|
|
2020-09-25 11:52:57 +02:00
|
|
|
match variant {
|
|
|
|
.normal {}
|
|
|
|
.bold {
|
|
|
|
if fpath.ends_with('-Regular') {
|
2021-06-04 19:00:32 +02:00
|
|
|
file = file.replace('-Regular', '-Bold')
|
|
|
|
} else if file.starts_with('DejaVuSans') {
|
|
|
|
file += '-Bold'
|
|
|
|
} else if file.to_lower().starts_with('arial') {
|
|
|
|
file += 'bd'
|
2020-09-25 11:52:57 +02:00
|
|
|
} else {
|
2021-06-04 19:00:32 +02:00
|
|
|
file += '-bold'
|
2020-09-25 11:52:57 +02:00
|
|
|
}
|
2020-11-15 15:09:35 +01:00
|
|
|
$if macos {
|
|
|
|
if os.exists('SFNS-bold') {
|
2021-06-04 19:00:32 +02:00
|
|
|
file = 'SFNS-bold'
|
2020-11-15 15:09:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 11:52:57 +02:00
|
|
|
}
|
|
|
|
.italic {
|
2021-06-04 19:00:32 +02:00
|
|
|
if file.ends_with('-Regular') {
|
|
|
|
file = file.replace('-Regular', '-Italic')
|
|
|
|
} else if file.starts_with('DejaVuSans') {
|
|
|
|
file += '-Oblique'
|
|
|
|
} else if file.to_lower().starts_with('arial') {
|
|
|
|
file += 'i'
|
2020-09-25 11:52:57 +02:00
|
|
|
} else {
|
2021-06-04 19:00:32 +02:00
|
|
|
file += 'Italic'
|
2020-09-25 11:52:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.mono {
|
2021-06-04 19:00:32 +02:00
|
|
|
if !file.ends_with('Mono-Regular') && file.ends_with('-Regular') {
|
|
|
|
file = file.replace('-Regular', 'Mono-Regular')
|
|
|
|
} else if file.to_lower().starts_with('arial') {
|
2020-09-25 11:52:57 +02:00
|
|
|
// Arial has no mono variant
|
|
|
|
} else {
|
2021-06-04 19:00:32 +02:00
|
|
|
file += 'Mono'
|
2020-09-25 11:52:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 19:00:32 +02:00
|
|
|
return fpath + file + '.ttf'
|
2020-09-25 11:52:57 +02:00
|
|
|
}
|
2020-09-26 08:36:46 +02:00
|
|
|
|
|
|
|
fn debug_font_println(s string) {
|
2020-10-18 08:48:13 +02:00
|
|
|
$if debug_font ? {
|
2020-09-29 12:20:37 +02:00
|
|
|
println(s)
|
2020-09-26 08:36:46 +02:00
|
|
|
}
|
|
|
|
}
|