gg: merge ft module

pull/5710/head
Alexander Medvednikov 2020-07-06 20:29:05 +02:00
parent 43c8726c37
commit 0ed8199da2
2 changed files with 23 additions and 41 deletions

View File

@ -1,6 +1,5 @@
// 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.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gg
import gx
@ -9,7 +8,6 @@ import sokol
import sokol.sapp
import sokol.sgl
import sokol.gfx
import gg.ft
pub type FNCb fn(x voidptr)
pub type FNEvent fn(e voidptr, x voidptr)
@ -55,7 +53,7 @@ pub mut:
clear_pass C.sg_pass_action
window C.sapp_desc
config Config
ft &ft.FT
ft &FT
font_inited bool
}
@ -87,7 +85,7 @@ fn gg_init_sokol_window(user_data voidptr) {
//println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
//if g.config.init_text {
if g.config.font_path != '' {
g.ft = ft.new({ font_path: g.config.font_path, scale: sapp.dpi_scale() }) or {panic(err)}
g.ft = new_ft({ font_path: g.config.font_path, scale: sapp.dpi_scale() }) or {panic(err)}
g.font_inited = true
}
if g.config.init_fn != voidptr(0) {
@ -272,18 +270,6 @@ pub fn (ctx &Context) draw_rounded_rect(x, y, width, height, radius f32, color g
pub fn (ctx &Context) draw_empty_rounded_rect(x, y, width, height, radius f32, border_color gx.Color) {
}
pub fn (ctx &Context) draw_text(x, y int, text string, cfg gx.TextCfg) {
if !ctx.font_inited {
return
}
ctx.ft.draw_text(x, y, text, cfg)
}
pub fn (ctx &Context) draw_text_def(x, y int, text string) {
ctx.ft.draw_text_def(x, y, text)
}
fn C.WaitMessage()
pub fn wait_events() {

View File

@ -1,4 +1,6 @@
module ft
// 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
import sokol.sfons
import gx
@ -7,13 +9,6 @@ import os
const (
default_font_size = 20
)
// TODO remove globals
/*
__global g_fons &C.FONScontext
__global g_font_normal int
__global g_font_path string
*/
pub struct FT {
pub:
@ -23,13 +18,13 @@ pub struct FT {
scale f32 = 1.0
}
pub struct Config {
pub struct FTConfig {
font_path string
scale f32 = 1.0
font_size int
}
pub fn new(c Config) ?&FT{
fn new_ft(c Config) ?&FT{
if c.font_path == '' {
// Load default font
}
@ -50,38 +45,40 @@ pub fn new(c Config) ?&FT{
}
pub fn (ft &FT) draw_text(x, y int, text string, cfg gx.TextCfg) {
ft.fons.set_font(ft.font_normal)
ft.fons.set_size(2.0 * ft.scale * f32(cfg.size))
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))
if cfg.align == gx.align_right {
C.fonsSetAlign(ft.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_TOP)
C.fonsSetAlign(ctx.ft.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_TOP)
}
else {
C.fonsSetAlign(ft.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
C.fonsSetAlign(ctx.ft.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
}
color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255)
C.fonsSetColor(ft.fons, color)
C.fonsSetColor(ctx.ft.fons, color)
ascender := f32(0.0)
descender := f32(0.0)
lh := f32(0.0)
ft.fons.vert_metrics(&ascender, &descender, &lh)
C.fonsDrawText(ft.fons, x*ft.scale, y*ft.scale, text.str, 0) // TODO: check offsets/alignment
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
}
pub fn (ft &FT) draw_text_def(x, y int, text string) {
pub fn (ctx &Context) draw_text_def(x, y int, text string) {
cfg := gx.TextCfg {
color: gx.black
size: default_font_size
align: gx.align_left
}
ft.draw_text(x, y, text, cfg)
ctx.draw_text(x, y, text, cfg)
}
/*
pub fn (mut gg FT) init_font() {
// TODO
////gg.fons =g_fons
//gg.font_normal=g_font_normal
}
*/
pub fn (ft &FT) flush(){
sfons.flush(ft.fons)
@ -93,7 +90,6 @@ pub fn (ft &FT) text_width(s string) int {
pub fn (ft &FT) text_height(s string) int {
return 0
}
pub fn (ft &FT) text_size(s string) (int, int) {