mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
gg: allow fonts loaded with $embed_file() to be used (#8263)
This commit is contained in:
34
vlib/gg/gg.v
34
vlib/gg/gg.v
@@ -60,6 +60,11 @@ pub:
|
||||
font_path string
|
||||
custom_bold_font_path string
|
||||
ui_mode bool // refreshes only on events to save CPU usage
|
||||
// font bytes for embedding
|
||||
font_bytes_normal []byte
|
||||
font_bytes_bold []byte
|
||||
font_bytes_mono []byte
|
||||
font_bytes_italic []byte
|
||||
}
|
||||
|
||||
pub struct Context {
|
||||
@@ -131,14 +136,25 @@ fn gg_init_sokol_window(user_data voidptr) {
|
||||
g.font_inited = true
|
||||
} else {
|
||||
if !exists {
|
||||
sfont := system_font_path()
|
||||
eprintln('font file "$g.config.font_path" does not exist, the system font was used instead.')
|
||||
g.ft = new_ft(
|
||||
font_path: sfont
|
||||
custom_bold_font_path: g.config.custom_bold_font_path
|
||||
scale: sapp.dpi_scale()
|
||||
) or { panic(err) }
|
||||
g.font_inited = true
|
||||
if g.config.font_bytes_normal.len > 0 {
|
||||
g.ft = new_ft(
|
||||
bytes_normal: g.config.font_bytes_normal
|
||||
bytes_bold: g.config.font_bytes_bold
|
||||
bytes_mono: g.config.font_bytes_mono
|
||||
bytes_italic: g.config.font_bytes_italic
|
||||
scale: sapp.dpi_scale()
|
||||
) or { panic(err) }
|
||||
g.font_inited = true
|
||||
} else {
|
||||
sfont := system_font_path()
|
||||
eprintln('font file "$g.config.font_path" does not exist, the system font was used instead.')
|
||||
g.ft = new_ft(
|
||||
font_path: sfont
|
||||
custom_bold_font_path: g.config.custom_bold_font_path
|
||||
scale: sapp.dpi_scale()
|
||||
) or { panic(err) }
|
||||
g.font_inited = true
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
@@ -237,7 +253,7 @@ pub fn new_context(cfg Config) &Context {
|
||||
width: cfg.width
|
||||
height: cfg.height
|
||||
config: cfg
|
||||
render_text: cfg.font_path != ''
|
||||
render_text: cfg.font_path != '' || cfg.font_bytes_normal.len > 0
|
||||
ft: 0
|
||||
ui_mode: cfg.ui_mode
|
||||
}
|
||||
|
||||
@@ -29,11 +29,51 @@ struct FTConfig {
|
||||
custom_bold_font_path string
|
||||
scale f32 = 1.0
|
||||
font_size int
|
||||
bytes_normal []byte
|
||||
bytes_bold []byte
|
||||
bytes_mono []byte
|
||||
bytes_italic []byte
|
||||
}
|
||||
|
||||
fn new_ft(c FTConfig) ?&FT {
|
||||
if c.font_path == '' {
|
||||
// Load default font
|
||||
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
|
||||
}
|
||||
}
|
||||
$if !android {
|
||||
if c.font_path == '' || !os.exists(c.font_path) {
|
||||
@@ -144,7 +184,8 @@ pub fn (ctx &Context) text_width(s string) int {
|
||||
mut buf := [4]f32{}
|
||||
C.fonsTextBounds(ctx.ft.fons, 0, 0, s.str, 0, buf)
|
||||
if s.ends_with(' ') {
|
||||
return int((buf[2] - buf[0]) / ctx.scale) + ctx.text_width('i') // TODO fix this in fontstash?
|
||||
return int((buf[2] - buf[0]) /
|
||||
ctx.scale) + ctx.text_width('i') // TODO fix this in fontstash?
|
||||
}
|
||||
return int((buf[2] - buf[0]) / ctx.scale)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user