2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-07-06 21:29:05 +03: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 16:35:37 +03:00
|
|
|
|
|
|
|
import sokol.sfons
|
2020-09-20 12:05:30 +03:00
|
|
|
import sokol.sgl
|
2020-06-02 16:35:37 +03:00
|
|
|
import gx
|
|
|
|
import os
|
|
|
|
|
2020-09-25 12:52:57 +03:00
|
|
|
enum FontVariant {
|
|
|
|
normal = 0
|
|
|
|
bold
|
|
|
|
mono
|
|
|
|
italic
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:40:24 +03:00
|
|
|
struct FT {
|
2020-07-06 21:40:54 +03:00
|
|
|
pub:
|
2020-10-18 09:48:13 +03:00
|
|
|
fons &C.FONScontext
|
2020-06-02 16:35:37 +03:00
|
|
|
font_normal int
|
2020-10-18 09:48:13 +03:00
|
|
|
font_bold int
|
|
|
|
font_mono int
|
2020-07-27 22:19:43 +03:00
|
|
|
font_italic int
|
2020-10-18 09:48:13 +03:00
|
|
|
scale f32 = 1.0
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
2020-07-06 22:40:24 +03:00
|
|
|
struct FTConfig {
|
2020-12-05 02:03:46 +03:00
|
|
|
font_path string
|
2020-12-01 18:30:22 +03:00
|
|
|
custom_bold_font_path string
|
2020-12-05 02:03:46 +03:00
|
|
|
scale f32 = 1.0
|
|
|
|
font_size int
|
2021-01-22 00:07:47 +03:00
|
|
|
bytes_normal []byte
|
|
|
|
bytes_bold []byte
|
|
|
|
bytes_mono []byte
|
|
|
|
bytes_italic []byte
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-23 12:25:40 +03:00
|
|
|
struct StringToRender {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
text string
|
|
|
|
cfg gx.TextCfg
|
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
fn new_ft(c FTConfig) ?&FT {
|
2020-06-04 17:05:12 +03:00
|
|
|
if c.font_path == '' {
|
2021-01-22 00:07:47 +03:00
|
|
|
if c.bytes_normal.len > 0 {
|
|
|
|
fons := sfons.create(512, 512, 1)
|
|
|
|
bytes_normal := c.bytes_normal
|
|
|
|
bytes_bold := if c.bytes_bold.len > 0 {
|
|
|
|
c.bytes_bold
|
|
|
|
} else {
|
|
|
|
debug_font_println('setting bold variant to normal')
|
|
|
|
bytes_normal
|
|
|
|
}
|
|
|
|
bytes_mono := if c.bytes_mono.len > 0 {
|
|
|
|
c.bytes_mono
|
|
|
|
} else {
|
|
|
|
debug_font_println('setting mono variant to normal')
|
|
|
|
bytes_normal
|
|
|
|
}
|
|
|
|
bytes_italic := if c.bytes_italic.len > 0 {
|
|
|
|
c.bytes_italic
|
|
|
|
} else {
|
|
|
|
debug_font_println('setting italic variant to normal')
|
|
|
|
bytes_normal
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FT{
|
|
|
|
fons: fons
|
|
|
|
font_normal: C.fonsAddFontMem(fons, 'sans', bytes_normal.data, bytes_normal.len,
|
|
|
|
false)
|
|
|
|
font_bold: C.fonsAddFontMem(fons, 'sans', bytes_bold.data, bytes_bold.len,
|
|
|
|
false)
|
|
|
|
font_mono: C.fonsAddFontMem(fons, 'sans', bytes_mono.data, bytes_mono.len,
|
|
|
|
false)
|
|
|
|
font_italic: C.fonsAddFontMem(fons, 'sans', bytes_italic.data, bytes_italic.len,
|
|
|
|
false)
|
|
|
|
scale: c.scale
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Load default font
|
|
|
|
}
|
2020-06-04 17:05:12 +03:00
|
|
|
}
|
2021-02-06 19:36:43 +03:00
|
|
|
|
|
|
|
if c.font_path == '' || !os.exists(c.font_path) {
|
|
|
|
$if !android {
|
2020-08-19 08:10:42 +03:00
|
|
|
println('failed to load font "$c.font_path"')
|
|
|
|
return none
|
|
|
|
}
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
2021-02-06 19:36:43 +03:00
|
|
|
|
2020-08-19 08:10:42 +03:00
|
|
|
mut bytes := []byte{}
|
|
|
|
$if android {
|
2021-02-06 19:36:43 +03:00
|
|
|
// First try any filesystem paths
|
|
|
|
bytes = os.read_bytes(c.font_path) or { []byte{} }
|
|
|
|
if bytes.len == 0 {
|
|
|
|
// ... then try the APK asset path
|
|
|
|
bytes = os.read_apk_asset(c.font_path) or {
|
|
|
|
println('failed to load font "$c.font_path"')
|
|
|
|
return none
|
|
|
|
}
|
2020-08-19 08:10:42 +03:00
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
bytes = os.read_bytes(c.font_path) or {
|
|
|
|
println('failed to load font "$c.font_path"')
|
|
|
|
return none
|
|
|
|
}
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
2021-03-08 21:52:13 +03:00
|
|
|
bold_path := if c.custom_bold_font_path != '' {
|
|
|
|
c.custom_bold_font_path
|
|
|
|
} else {
|
|
|
|
get_font_path_variant(c.font_path, .bold)
|
|
|
|
}
|
2020-07-27 22:19:43 +03:00
|
|
|
bytes_bold := os.read_bytes(bold_path) or {
|
2020-09-26 09:36:46 +03:00
|
|
|
debug_font_println('failed to load font "$bold_path"')
|
2020-07-28 12:12:29 +03:00
|
|
|
bytes
|
2020-07-27 22:19:43 +03:00
|
|
|
}
|
2020-09-25 12:52:57 +03:00
|
|
|
mono_path := get_font_path_variant(c.font_path, .mono)
|
2020-10-18 09:48:13 +03:00
|
|
|
bytes_mono := os.read_bytes(mono_path) or {
|
2020-09-26 09:36:46 +03:00
|
|
|
debug_font_println('failed to load font "$mono_path"')
|
2020-07-28 12:12:29 +03:00
|
|
|
bytes
|
2020-07-27 22:19:43 +03:00
|
|
|
}
|
2020-09-25 12:52:57 +03:00
|
|
|
italic_path := get_font_path_variant(c.font_path, .italic)
|
2020-10-18 09:48:13 +03:00
|
|
|
bytes_italic := os.read_bytes(italic_path) or {
|
2020-09-26 09:36:46 +03:00
|
|
|
debug_font_println('failed to load font "$italic_path"')
|
2020-07-28 12:12:29 +03:00
|
|
|
bytes
|
2020-07-27 22:19:43 +03:00
|
|
|
}
|
2020-06-03 23:08:55 +03:00
|
|
|
fons := sfons.create(512, 512, 1)
|
2020-06-02 16:35:37 +03:00
|
|
|
return &FT{
|
2020-10-18 09:48:13 +03:00
|
|
|
fons: fons
|
2020-06-02 16:35:37 +03:00
|
|
|
font_normal: C.fonsAddFontMem(fons, 'sans', bytes.data, bytes.len, false)
|
2020-07-27 22:19:43 +03:00
|
|
|
font_bold: C.fonsAddFontMem(fons, 'sans', bytes_bold.data, bytes_bold.len, false)
|
|
|
|
font_mono: C.fonsAddFontMem(fons, 'sans', bytes_mono.data, bytes_mono.len, false)
|
2020-10-18 09:48:13 +03:00
|
|
|
font_italic: C.fonsAddFontMem(fons, 'sans', bytes_italic.data, bytes_italic.len,
|
|
|
|
false)
|
2020-06-04 17:05:12 +03:00
|
|
|
scale: c.scale
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-18 09:48:13 +03:00
|
|
|
|
2021-02-27 10:15:26 +03:00
|
|
|
pub fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
|
2020-07-06 21:29:05 +03:00
|
|
|
if !ctx.font_inited {
|
|
|
|
return
|
|
|
|
}
|
2020-07-27 22:19:43 +03:00
|
|
|
if cfg.bold {
|
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_bold)
|
2020-10-18 09:48:13 +03:00
|
|
|
} else if cfg.mono {
|
2020-07-27 22:19:43 +03:00
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_mono)
|
2020-10-18 09:48:13 +03:00
|
|
|
} else if cfg.italic {
|
2020-07-27 22:19:43 +03:00
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_italic)
|
2020-10-18 09:48:13 +03:00
|
|
|
} else {
|
2020-07-27 22:19:43 +03:00
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_normal)
|
|
|
|
}
|
2020-07-07 18:09:35 +03:00
|
|
|
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
|
2020-08-19 08:10:42 +03:00
|
|
|
size := if cfg.mono { cfg.size - 2 } else { cfg.size }
|
2020-07-13 02:02:47 +03:00
|
|
|
ctx.ft.fons.set_size(scale * f32(size))
|
2020-08-19 08:10:42 +03:00
|
|
|
C.fonsSetAlign(ctx.ft.fons, int(cfg.align) | int(cfg.vertical_align))
|
2020-09-20 12:05:30 +03:00
|
|
|
color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.a)
|
|
|
|
if cfg.color.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
2020-07-06 21:29:05 +03:00
|
|
|
C.fonsSetColor(ctx.ft.fons, color)
|
2020-06-02 16:35:37 +03:00
|
|
|
ascender := f32(0.0)
|
|
|
|
descender := f32(0.0)
|
|
|
|
lh := f32(0.0)
|
2020-07-06 21:29:05 +03:00
|
|
|
ctx.ft.fons.vert_metrics(&ascender, &descender, &lh)
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_text(x int, y int, text_ string, cfg gx.TextCfg) {
|
2021-01-23 12:25:40 +03:00
|
|
|
$if macos {
|
|
|
|
if ctx.native_rendering {
|
|
|
|
if cfg.align == gx.align_right {
|
|
|
|
width := ctx.text_width(text_)
|
|
|
|
C.darwin_draw_string(x - width, ctx.height - y, text_, cfg)
|
|
|
|
} else {
|
|
|
|
C.darwin_draw_string(x, ctx.height - y, text_, cfg)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 09:30:52 +03:00
|
|
|
if !ctx.font_inited {
|
|
|
|
eprintln('gg: draw_text(): font not initialized')
|
|
|
|
return
|
|
|
|
}
|
2020-10-18 09:48:13 +03:00
|
|
|
// text := text_.trim_space() // TODO remove/optimize
|
2020-12-05 02:03:46 +03:00
|
|
|
// mut text := text_
|
|
|
|
// if text.contains('\t') {
|
|
|
|
// text = text.replace('\t', ' ')
|
|
|
|
// }
|
2020-08-19 08:10:42 +03:00
|
|
|
ctx.set_cfg(cfg)
|
|
|
|
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
|
2020-12-05 02:03:46 +03:00
|
|
|
C.fonsDrawText(ctx.ft.fons, x * scale, y * scale, text_.str, 0) // TODO: check offsets/alignment
|
2020-08-19 08:10:42 +03:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_text_def(x int, y int, text string) {
|
2020-08-19 08:10:42 +03:00
|
|
|
ctx.draw_text(x, y, text, {})
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:29:05 +03:00
|
|
|
/*
|
2020-06-02 16:35:37 +03:00
|
|
|
pub fn (mut gg FT) init_font() {
|
|
|
|
}
|
2020-07-06 21:29:05 +03:00
|
|
|
*/
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ft &FT) flush() {
|
2020-06-03 23:08:55 +03:00
|
|
|
sfons.flush(ft.fons)
|
|
|
|
}
|
2020-07-05 20:28:28 +03:00
|
|
|
|
2020-07-12 02:46:21 +03:00
|
|
|
pub fn (ctx &Context) text_width(s string) int {
|
2021-01-23 12:25:40 +03:00
|
|
|
$if macos {
|
|
|
|
if ctx.native_rendering {
|
|
|
|
return C.darwin_text_width(s)
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 08:10:42 +03:00
|
|
|
// ctx.set_cfg(cfg) TODO
|
2020-07-12 02:46:21 +03:00
|
|
|
if !ctx.font_inited {
|
|
|
|
return 0
|
|
|
|
}
|
2020-08-16 05:54:05 +03:00
|
|
|
mut buf := [4]f32{}
|
2021-03-15 16:55:07 +03:00
|
|
|
C.fonsTextBounds(ctx.ft.fons, 0, 0, s.str, 0, &buf[0])
|
2020-08-20 09:30:52 +03:00
|
|
|
if s.ends_with(' ') {
|
2021-01-23 11:33:22 +03:00
|
|
|
return int((buf[2] - buf[0]) / ctx.scale) +
|
|
|
|
ctx.text_width('i') // TODO fix this in fontstash?
|
2020-08-20 09:30:52 +03:00
|
|
|
}
|
2021-01-23 12:25:40 +03:00
|
|
|
res := int((buf[2] - buf[0]) / ctx.scale)
|
|
|
|
// println('TW "$s" = $res')
|
|
|
|
$if macos {
|
|
|
|
if ctx.native_rendering {
|
|
|
|
return res * 2
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 02:46:21 +03:00
|
|
|
return int((buf[2] - buf[0]) / ctx.scale)
|
2020-07-05 20:28:28 +03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:48:39 +03:00
|
|
|
pub fn (ctx &Context) text_height(s string) int {
|
2020-08-19 08:10:42 +03:00
|
|
|
// ctx.set_cfg(cfg) TODO
|
2020-07-12 13:48:39 +03:00
|
|
|
if !ctx.font_inited {
|
|
|
|
return 0
|
|
|
|
}
|
2020-08-16 05:54:05 +03:00
|
|
|
mut buf := [4]f32{}
|
2021-03-15 16:55:07 +03:00
|
|
|
C.fonsTextBounds(ctx.ft.fons, 0, 0, s.str, 0, &buf[0])
|
2020-07-12 13:48:39 +03:00
|
|
|
return int((buf[3] - buf[1]) / ctx.scale)
|
2020-07-05 20:28:28 +03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:48:39 +03:00
|
|
|
pub fn (ctx &Context) text_size(s string) (int, int) {
|
2020-08-19 08:10:42 +03:00
|
|
|
// ctx.set_cfg(cfg) TODO
|
2020-07-12 13:48:39 +03:00
|
|
|
if !ctx.font_inited {
|
2020-10-18 09:48:13 +03:00
|
|
|
return 0, 0
|
2020-07-12 13:48:39 +03:00
|
|
|
}
|
2020-08-16 05:54:05 +03:00
|
|
|
mut buf := [4]f32{}
|
2021-03-15 16:55:07 +03:00
|
|
|
C.fonsTextBounds(ctx.ft.fons, 0, 0, s.str, 0, &buf[0])
|
2020-07-12 13:48:39 +03:00
|
|
|
return int((buf[2] - buf[0]) / ctx.scale), int((buf[3] - buf[1]) / ctx.scale)
|
2020-07-05 20:28:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 07:46:18 +03: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 09:48:13 +03:00
|
|
|
'FreeSans.ttf', 'DejaVuSans.ttf']
|
2020-08-27 07:46:18 +03:00
|
|
|
$if macos {
|
2020-09-24 17:09:03 +03:00
|
|
|
fonts = ['/System/Library/Fonts/SFNS.ttf', '/System/Library/Fonts/SFNSText.ttf', '/Library/Fonts/Arial.ttf']
|
|
|
|
for font in fonts {
|
|
|
|
if os.is_file(font) {
|
|
|
|
return font
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 07:46:18 +03:00
|
|
|
}
|
2021-02-06 19:36:43 +03:00
|
|
|
$if android {
|
|
|
|
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']
|
|
|
|
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 21:52:13 +03:00
|
|
|
s := os.execute('fc-list')
|
|
|
|
if s.exit_code != 0 {
|
|
|
|
panic('failed to fetch system fonts')
|
|
|
|
}
|
2020-08-27 07:46:18 +03: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 12:52:57 +03: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
|
|
|
|
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'
|
|
|
|
}
|
2020-11-15 17:09:35 +03:00
|
|
|
$if macos {
|
|
|
|
if os.exists('SFNS-bold') {
|
|
|
|
fpath = 'SFNS-bold'
|
|
|
|
}
|
|
|
|
}
|
2020-09-25 12:52:57 +03:00
|
|
|
}
|
|
|
|
.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 {
|
2020-09-29 13:20:37 +03:00
|
|
|
if !fpath.ends_with('Mono-Regular') && fpath.ends_with('-Regular') {
|
2020-09-25 12:52:57 +03:00
|
|
|
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'
|
|
|
|
}
|
2020-09-26 09:36:46 +03:00
|
|
|
|
|
|
|
fn debug_font_println(s string) {
|
2020-10-18 09:48:13 +03:00
|
|
|
$if debug_font ? {
|
2020-09-29 13:20:37 +03:00
|
|
|
println(s)
|
2020-09-26 09:36:46 +03:00
|
|
|
}
|
|
|
|
}
|