2020-07-06 20:29:05 +02:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// 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 sokol.sfons
|
|
|
|
import gx
|
|
|
|
import os
|
|
|
|
|
2020-07-06 21:40:24 +02:00
|
|
|
struct FT {
|
2020-07-06 20:40:54 +02:00
|
|
|
pub:
|
2020-06-02 15:35:37 +02:00
|
|
|
fons &C.FONScontext
|
2020-06-04 16:05:12 +02:00
|
|
|
|
2020-06-02 15:35:37 +02:00
|
|
|
font_normal int
|
2020-07-27 21:19:43 +02:00
|
|
|
font_bold int
|
|
|
|
font_mono int
|
|
|
|
font_italic int
|
2020-06-04 16:05:12 +02:00
|
|
|
scale f32 = 1.0
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:40:24 +02:00
|
|
|
struct FTConfig {
|
2020-06-02 15:35:37 +02:00
|
|
|
font_path string
|
2020-06-04 16:05:12 +02:00
|
|
|
scale f32 = 1.0
|
|
|
|
font_size int
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:40:24 +02:00
|
|
|
fn new_ft(c FTConfig) ?&FT{
|
2020-06-04 16:05:12 +02:00
|
|
|
if c.font_path == '' {
|
|
|
|
// Load default font
|
|
|
|
}
|
2020-08-19 07:10:42 +02:00
|
|
|
$if !android {
|
|
|
|
if c.font_path == '' || !os.exists(c.font_path) {
|
|
|
|
println('failed to load font "$c.font_path"')
|
|
|
|
return none
|
|
|
|
}
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
2020-08-19 07:10:42 +02:00
|
|
|
|
|
|
|
mut bytes := []byte{}
|
|
|
|
$if android {
|
|
|
|
bytes = os.read_apk_asset(c.font_path) or {
|
|
|
|
println('failed to load font "$c.font_path"')
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
bytes = os.read_bytes(c.font_path) or {
|
|
|
|
println('failed to load font "$c.font_path"')
|
|
|
|
return none
|
|
|
|
}
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
2020-08-19 07:10:42 +02:00
|
|
|
|
2020-07-27 21:19:43 +02:00
|
|
|
bold_path := 'SFNS-bold.ttf'// c.font_path.replace('.ttf', '-bold.ttf')
|
|
|
|
bytes_bold := os.read_bytes(bold_path) or {
|
|
|
|
println('failed to load font "$bold_path"')
|
2020-07-28 11:12:29 +02:00
|
|
|
bytes
|
2020-07-27 21:19:43 +02:00
|
|
|
}
|
|
|
|
mono_path := '/System/Library/Fonts/SFNSMono.ttf'// c.font_path.replace('.ttf', '-bold.ttf')
|
|
|
|
bytes_mono:= os.read_bytes(mono_path) or {
|
|
|
|
println('failed to load font "$mono_path"')
|
2020-07-28 11:12:29 +02:00
|
|
|
bytes
|
2020-07-27 21:19:43 +02:00
|
|
|
}
|
|
|
|
italic_path := '/System/Library/Fonts/SFNSItalic.ttf'
|
|
|
|
bytes_italic:= os.read_bytes(italic_path) or {
|
|
|
|
println('failed to load font "$italic_path"')
|
2020-07-28 11:12:29 +02:00
|
|
|
bytes
|
2020-07-27 21:19:43 +02:00
|
|
|
}
|
2020-06-03 22:08:55 +02:00
|
|
|
fons := sfons.create(512, 512, 1)
|
2020-06-02 15:35:37 +02:00
|
|
|
return &FT{
|
|
|
|
fons : fons
|
|
|
|
font_normal: C.fonsAddFontMem(fons, 'sans', bytes.data, bytes.len, false)
|
2020-07-27 21:19:43 +02: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)
|
|
|
|
font_italic: C.fonsAddFontMem(fons, 'sans', bytes_italic.data, bytes_italic.len, false)
|
2020-06-04 16:05:12 +02:00
|
|
|
scale: c.scale
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-19 07:10:42 +02:00
|
|
|
fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
|
2020-07-06 20:29:05 +02:00
|
|
|
if !ctx.font_inited {
|
|
|
|
return
|
|
|
|
}
|
2020-07-27 21:19:43 +02:00
|
|
|
if cfg.bold {
|
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_bold)
|
|
|
|
}
|
|
|
|
else if cfg.mono {
|
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_mono)
|
|
|
|
}
|
|
|
|
else if cfg.italic {
|
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_italic)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_normal)
|
|
|
|
}
|
2020-07-07 17:09:35 +02:00
|
|
|
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
|
2020-08-19 07:10:42 +02:00
|
|
|
size := if cfg.mono { cfg.size - 2 } else { cfg.size }
|
2020-07-13 01:02:47 +02:00
|
|
|
ctx.ft.fons.set_size(scale * f32(size))
|
2020-08-19 07:10:42 +02:00
|
|
|
C.fonsSetAlign(ctx.ft.fons, int(cfg.align) | int(cfg.vertical_align))
|
2020-06-03 22:08:55 +02:00
|
|
|
color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255)
|
2020-07-06 20:29:05 +02:00
|
|
|
C.fonsSetColor(ctx.ft.fons, color)
|
2020-06-02 15:35:37 +02:00
|
|
|
ascender := f32(0.0)
|
|
|
|
descender := f32(0.0)
|
|
|
|
lh := f32(0.0)
|
2020-07-06 20:29:05 +02:00
|
|
|
ctx.ft.fons.vert_metrics(&ascender, &descender, &lh)
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 07:10:42 +02:00
|
|
|
pub fn (ctx &Context) draw_text(x, y int, text_ string, cfg gx.TextCfg) {
|
2020-08-20 08:30:52 +02:00
|
|
|
if !ctx.font_inited {
|
|
|
|
eprintln('gg: draw_text(): font not initialized')
|
|
|
|
return
|
|
|
|
}
|
2020-08-19 07:10:42 +02:00
|
|
|
//text := text_.trim_space() // TODO remove/optimize
|
|
|
|
mut text := text_
|
|
|
|
if text.contains('\t') {
|
|
|
|
text = text.replace('\t', ' ')
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
2020-08-19 07:10:42 +02:00
|
|
|
ctx.set_cfg(cfg)
|
|
|
|
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
|
|
|
|
C.fonsDrawText(ctx.ft.fons, x * scale, y * scale, text.str, 0) // TODO: check offsets/alignment
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (ctx &Context) draw_text_def(x, y int, text string) {
|
|
|
|
ctx.draw_text(x, y, text, {})
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 20:29:05 +02:00
|
|
|
/*
|
2020-06-02 15:35:37 +02:00
|
|
|
pub fn (mut gg FT) init_font() {
|
|
|
|
}
|
2020-07-06 20:29:05 +02:00
|
|
|
*/
|
2020-06-02 15:35:37 +02:00
|
|
|
|
2020-06-03 22:08:55 +02:00
|
|
|
pub fn (ft &FT) flush(){
|
|
|
|
sfons.flush(ft.fons)
|
|
|
|
}
|
2020-07-05 19:28:28 +02:00
|
|
|
|
2020-07-12 01:46:21 +02:00
|
|
|
pub fn (ctx &Context) text_width(s string) int {
|
2020-08-19 07:10:42 +02:00
|
|
|
// ctx.set_cfg(cfg) TODO
|
2020-07-12 01:46:21 +02:00
|
|
|
if !ctx.font_inited {
|
|
|
|
return 0
|
|
|
|
}
|
2020-08-16 04:54:05 +02:00
|
|
|
mut buf := [4]f32{}
|
2020-07-12 01:46:21 +02:00
|
|
|
C.fonsTextBounds(ctx.ft.fons, 0, 0, s.str, 0, buf)
|
2020-08-20 08:30:52 +02:00
|
|
|
if s.ends_with(' ') {
|
|
|
|
return int((buf[2] - buf[0]) / ctx.scale) + ctx.text_width('i') // TODO fix this in fontstash?
|
|
|
|
}
|
2020-07-12 01:46:21 +02:00
|
|
|
return int((buf[2] - buf[0]) / ctx.scale)
|
2020-07-05 19:28:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 12:48:39 +02:00
|
|
|
pub fn (ctx &Context) text_height(s string) int {
|
2020-08-19 07:10:42 +02:00
|
|
|
// ctx.set_cfg(cfg) TODO
|
2020-07-12 12:48:39 +02:00
|
|
|
if !ctx.font_inited {
|
|
|
|
return 0
|
|
|
|
}
|
2020-08-16 04:54:05 +02:00
|
|
|
mut buf := [4]f32{}
|
2020-07-12 12:48:39 +02:00
|
|
|
C.fonsTextBounds(ctx.ft.fons, 0, 0, s.str, 0, buf)
|
|
|
|
return int((buf[3] - buf[1]) / ctx.scale)
|
2020-07-05 19:28:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 12:48:39 +02:00
|
|
|
pub fn (ctx &Context) text_size(s string) (int, int) {
|
2020-08-19 07:10:42 +02:00
|
|
|
// ctx.set_cfg(cfg) TODO
|
2020-07-12 12:48:39 +02:00
|
|
|
if !ctx.font_inited {
|
|
|
|
return 0,0
|
|
|
|
}
|
2020-08-16 04:54:05 +02:00
|
|
|
mut buf := [4]f32{}
|
2020-07-12 12:48:39 +02:00
|
|
|
C.fonsTextBounds(ctx.ft.fons, 0, 0, s.str, 0, buf)
|
|
|
|
return int((buf[2] - buf[0]) / ctx.scale), int((buf[3] - buf[1]) / ctx.scale)
|
2020-07-05 19:28:28 +02:00
|
|
|
}
|
|
|
|
|