examples: make v -prod build-examples pass without warnings/errors

This commit is contained in:
Delyan Angelov 2020-10-18 09:48:13 +03:00
parent 67ecc04580
commit 8b2e704741
9 changed files with 594 additions and 512 deletions

View file

@ -16,22 +16,21 @@ enum FontVariant {
struct FT {
pub:
fons &C.FONScontext
fons &C.FONScontext
font_normal int
font_bold int
font_mono int
font_bold int
font_mono int
font_italic int
scale f32 = 1.0
scale f32 = 1.0
}
struct FTConfig {
font_path string
scale f32 = 1.0
scale f32 = 1.0
font_size int
}
fn new_ft(c FTConfig) ?&FT{
fn new_ft(c FTConfig) ?&FT {
if c.font_path == '' {
// Load default font
}
@ -41,7 +40,6 @@ fn new_ft(c FTConfig) ?&FT{
return none
}
}
mut bytes := []byte{}
$if android {
bytes = os.read_apk_asset(c.font_path) or {
@ -60,40 +58,38 @@ fn new_ft(c FTConfig) ?&FT{
bytes
}
mono_path := get_font_path_variant(c.font_path, .mono)
bytes_mono:= os.read_bytes(mono_path) or {
bytes_mono := os.read_bytes(mono_path) or {
debug_font_println('failed to load font "$mono_path"')
bytes
}
italic_path := get_font_path_variant(c.font_path, .italic)
bytes_italic:= os.read_bytes(italic_path) or {
bytes_italic := os.read_bytes(italic_path) or {
debug_font_println('failed to load font "$italic_path"')
bytes
}
fons := sfons.create(512, 512, 1)
return &FT{
fons : fons
fons: fons
font_normal: C.fonsAddFontMem(fons, 'sans', bytes.data, bytes.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)
font_italic: C.fonsAddFontMem(fons, 'sans', bytes_italic.data, bytes_italic.len,
false)
scale: c.scale
}
}
fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
if !ctx.font_inited {
return
}
if cfg.bold {
ctx.ft.fons.set_font(ctx.ft.font_bold)
}
else if cfg.mono {
} else if cfg.mono {
ctx.ft.fons.set_font(ctx.ft.font_mono)
}
else if cfg.italic {
} else if cfg.italic {
ctx.ft.fons.set_font(ctx.ft.font_italic)
}
else {
} else {
ctx.ft.fons.set_font(ctx.ft.font_normal)
}
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
@ -111,12 +107,12 @@ fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
ctx.ft.fons.vert_metrics(&ascender, &descender, &lh)
}
pub fn (ctx &Context) draw_text(x, y int, text_ string, cfg gx.TextCfg) {
pub fn (ctx &Context) draw_text(x int, y int, text_ string, cfg gx.TextCfg) {
if !ctx.font_inited {
eprintln('gg: draw_text(): font not initialized')
return
}
//text := text_.trim_space() // TODO remove/optimize
// text := text_.trim_space() // TODO remove/optimize
mut text := text_
if text.contains('\t') {
text = text.replace('\t', ' ')
@ -126,7 +122,7 @@ pub fn (ctx &Context) draw_text(x, y int, text_ string, cfg gx.TextCfg) {
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) {
pub fn (ctx &Context) draw_text_def(x int, y int, text string) {
ctx.draw_text(x, y, text, {})
}
@ -134,8 +130,7 @@ pub fn (ctx &Context) draw_text_def(x, y int, text string) {
pub fn (mut gg FT) init_font() {
}
*/
pub fn (ft &FT) flush(){
pub fn (ft &FT) flush() {
sfons.flush(ft.fons)
}
@ -165,14 +160,13 @@ pub fn (ctx &Context) text_height(s string) int {
pub fn (ctx &Context) text_size(s string) (int, int) {
// ctx.set_cfg(cfg) TODO
if !ctx.font_inited {
return 0,0
return 0, 0
}
mut buf := [4]f32{}
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)
}
pub fn system_font_path() string {
env_font := os.getenv('VUI_FONT')
if env_font != '' && os.exists(env_font) {
@ -182,7 +176,7 @@ pub fn system_font_path() string {
return 'C:\\Windows\\Fonts\\arial.ttf'
}
mut fonts := ['Ubuntu-R.ttf', 'Arial.ttf', 'LiberationSans-Regular.ttf', 'NotoSans-Regular.ttf',
'FreeSans.ttf', 'DejaVuSans.ttf']
'FreeSans.ttf', 'DejaVuSans.ttf']
$if macos {
fonts = ['/System/Library/Fonts/SFNS.ttf', '/System/Library/Fonts/SFNSText.ttf', '/Library/Fonts/Arial.ttf']
for font in fonts {
@ -191,7 +185,9 @@ pub fn system_font_path() string {
}
}
}
s := os.exec('fc-list') or { panic('failed to fetch system fonts') }
s := os.exec('fc-list') or {
panic('failed to fetch system fonts')
}
system_fonts := s.output.split('\n')
for line in system_fonts {
for font in fonts {
@ -247,7 +243,7 @@ fn get_font_path_variant(font_path string, variant FontVariant) string {
}
fn debug_font_println(s string) {
$if debug_font? {
$if debug_font ? {
println(s)
}
}