2020-06-02 16:35:37 +03:00
|
|
|
module ft
|
|
|
|
|
|
|
|
import sokol.sfons
|
|
|
|
import gx
|
|
|
|
import os
|
|
|
|
|
|
|
|
const (
|
2020-06-04 17:05:12 +03:00
|
|
|
default_font_size = 20
|
2020-06-02 16:35:37 +03:00
|
|
|
)
|
|
|
|
// TODO remove globals
|
|
|
|
/*
|
|
|
|
__global g_fons &C.FONScontext
|
|
|
|
__global g_font_normal int
|
|
|
|
__global g_font_path string
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
pub struct FT {
|
|
|
|
pub:
|
|
|
|
fons &C.FONScontext
|
2020-06-04 17:05:12 +03:00
|
|
|
|
2020-06-02 16:35:37 +03:00
|
|
|
font_normal int
|
2020-06-04 17:05:12 +03:00
|
|
|
scale f32 = 1.0
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Config {
|
|
|
|
font_path string
|
2020-06-04 17:05:12 +03:00
|
|
|
scale f32 = 1.0
|
|
|
|
font_size int
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(c Config) ?&FT{
|
2020-06-04 17:05:12 +03:00
|
|
|
if c.font_path == '' {
|
|
|
|
// Load default font
|
|
|
|
}
|
2020-06-02 16:35:37 +03:00
|
|
|
if c.font_path == '' || !os.exists(c.font_path) {
|
|
|
|
println('failed to load font "$c.font_path"')
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
bytes := os.read_bytes(c.font_path) or {
|
|
|
|
println('failed to load font "$c.font_path"')
|
|
|
|
return none
|
|
|
|
}
|
2020-06-03 23:08:55 +03:00
|
|
|
fons := sfons.create(512, 512, 1)
|
2020-06-02 16:35:37 +03:00
|
|
|
return &FT{
|
|
|
|
fons : fons
|
|
|
|
font_normal: C.fonsAddFontMem(fons, 'sans', bytes.data, bytes.len, false)
|
2020-06-04 17:05:12 +03:00
|
|
|
scale: c.scale
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-03 23:08:55 +03:00
|
|
|
pub fn (ft &FT) draw_text(x, y int, text string, cfg gx.TextCfg) {
|
|
|
|
ft.fons.set_font(ft.font_normal)
|
2020-06-04 17:05:12 +03:00
|
|
|
ft.fons.set_size(2.0 * ft.scale * f32(cfg.size))
|
2020-06-05 00:51:54 +03:00
|
|
|
if cfg.align == gx.align_right {
|
|
|
|
C.fonsSetAlign(ft.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_TOP)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
C.fonsSetAlign(ft.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
|
|
|
|
}
|
2020-06-03 23:08:55 +03:00
|
|
|
color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255)
|
|
|
|
C.fonsSetColor(ft.fons, color)
|
2020-06-02 16:35:37 +03:00
|
|
|
ascender := f32(0.0)
|
|
|
|
descender := f32(0.0)
|
|
|
|
lh := f32(0.0)
|
2020-06-03 23:08:55 +03:00
|
|
|
ft.fons.vert_metrics(&ascender, &descender, &lh)
|
2020-06-05 00:51:54 +03:00
|
|
|
C.fonsDrawText(ft.fons, x*ft.scale, y*ft.scale, text.str, 0) // TODO: check offsets/alignment
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
2020-06-03 23:08:55 +03:00
|
|
|
pub fn (ft &FT) draw_text_def(x, y int, text string) {
|
2020-06-02 16:35:37 +03:00
|
|
|
cfg := gx.TextCfg {
|
|
|
|
color: gx.black
|
|
|
|
size: default_font_size
|
|
|
|
align: gx.align_left
|
|
|
|
}
|
2020-06-03 23:08:55 +03:00
|
|
|
ft.draw_text(x, y, text, cfg)
|
2020-06-02 16:35:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut gg FT) init_font() {
|
|
|
|
// TODO
|
|
|
|
////gg.fons =g_fons
|
|
|
|
//gg.font_normal=g_font_normal
|
|
|
|
}
|
|
|
|
|
2020-06-03 23:08:55 +03:00
|
|
|
pub fn (ft &FT) flush(){
|
|
|
|
sfons.flush(ft.fons)
|
|
|
|
}
|
2020-07-05 20:28:28 +03:00
|
|
|
|
|
|
|
pub fn (ft &FT) text_width(s string) int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (ft &FT) text_height(s string) int {
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (ft &FT) text_size(s string) (int, int) {
|
|
|
|
return 0,0
|
|
|
|
}
|
|
|
|
|