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
|
|
|
|
|
|
|
|
const (
|
2020-06-04 16:05:12 +02:00
|
|
|
default_font_size = 20
|
2020-06-02 15:35:37 +02:00
|
|
|
)
|
|
|
|
|
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-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-06-02 15:35:37 +02: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 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-06-04 16:05:12 +02:00
|
|
|
scale: c.scale
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:29:05 +02:00
|
|
|
pub fn (ctx &Context) draw_text(x, y int, text string, cfg gx.TextCfg) {
|
|
|
|
if !ctx.font_inited {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ft.fons.set_font(ctx.ft.font_normal)
|
|
|
|
ctx.ft.fons.set_size(2.0 * ctx.ft.scale * f32(cfg.size))
|
2020-06-04 23:51:54 +02:00
|
|
|
if cfg.align == gx.align_right {
|
2020-07-06 20:29:05 +02:00
|
|
|
C.fonsSetAlign(ctx.ft.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_TOP)
|
2020-06-04 23:51:54 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-07-06 20:29:05 +02:00
|
|
|
C.fonsSetAlign(ctx.ft.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
|
2020-06-04 23:51:54 +02:00
|
|
|
}
|
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)
|
|
|
|
C.fonsDrawText(ctx.ft.fons, x*ctx.ft.scale, y*ctx.ft.scale, text.str, 0) // TODO: check offsets/alignment
|
2020-06-02 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 20:29:05 +02:00
|
|
|
pub fn (ctx &Context) draw_text_def(x, y int, text string) {
|
2020-06-02 15:35:37 +02:00
|
|
|
cfg := gx.TextCfg {
|
|
|
|
color: gx.black
|
|
|
|
size: default_font_size
|
|
|
|
align: gx.align_left
|
|
|
|
}
|
2020-07-06 20:29:05 +02:00
|
|
|
ctx.draw_text(x, y, text, cfg)
|
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-06 20:40:54 +02:00
|
|
|
pub fn (ft &Context) text_width(s string) int {
|
2020-07-05 19:28:28 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:40:54 +02:00
|
|
|
pub fn (ft &Context) text_height(s string) int {
|
2020-07-05 19:28:28 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:40:54 +02:00
|
|
|
pub fn (ft &Context) text_size(s string) (int, int) {
|
2020-07-05 19:28:28 +02:00
|
|
|
return 0,0
|
|
|
|
}
|
|
|
|
|