From c26b7666c7610aed595e6e45f868286626a8bacd Mon Sep 17 00:00:00 2001 From: R cqls Date: Tue, 26 Apr 2022 19:59:36 +0200 Subject: [PATCH] gg: some stuff required to have svg and png screenshots working on v ui (#14180) --- vlib/gg/gg_ui.c.v | 24 ++++++++++++++++++++++++ vlib/x/ttf/render_bmp.v | 4 ++-- vlib/x/ttf/text_block.v | 2 +- vlib/x/ttf/ttf.v | 6 +++--- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 vlib/gg/gg_ui.c.v diff --git a/vlib/gg/gg_ui.c.v b/vlib/gg/gg_ui.c.v new file mode 100644 index 0000000000..955e18e281 --- /dev/null +++ b/vlib/gg/gg_ui.c.v @@ -0,0 +1,24 @@ +module gg + +import gx +import sokol.sgl + +// Stuff for ui from now for screenshot (that would be interesting for gg if screenshot is implemented also for gg) + +// required for ui.DrawDevice interface (with &gg.Context as an instance) +pub fn (ctx &Context) scissor_rect(x int, y int, w int, h int) { + sgl.scissor_rect(int(dpi_scale() * x), int(dpi_scale() * y), int(dpi_scale() * w), + int(dpi_scale() * h), true) +} + +pub fn (ctx &Context) has_text_style() bool { + return false +} + +pub fn (ctx &Context) set_text_style(font_name string, font_path string, size int, color gx.Color, align int, vertical_align int) {} + +// default draw_text (draw_text_def but without set_cfg) +pub fn (ctx &Context) draw_text_default(x int, y int, text string) { + scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale } + ctx.ft.fons.draw_text(x * scale, y * scale, text) // TODO: check offsets/alignment +} diff --git a/vlib/x/ttf/render_bmp.v b/vlib/x/ttf/render_bmp.v index fa1cf422b3..4ed137bc48 100644 --- a/vlib/x/ttf/render_bmp.v +++ b/vlib/x/ttf/render_bmp.v @@ -59,13 +59,13 @@ pub fn (mut bmp BitMap) clear() { } // transform matrix applied to the text -fn (bmp &BitMap) trf_txt(p &Point) (int, int) { +pub fn (bmp &BitMap) trf_txt(p &Point) (int, int) { return int(p.x * bmp.tr_matrix[0] + p.y * bmp.tr_matrix[3] + bmp.tr_matrix[6]), int( p.x * bmp.tr_matrix[1] + p.y * bmp.tr_matrix[4] + bmp.tr_matrix[7]) } // transform matrix applied to the char -fn (bmp &BitMap) trf_ch(p &Point) (int, int) { +pub fn (bmp &BitMap) trf_ch(p &Point) (int, int) { return int(p.x * bmp.ch_matrix[0] + p.y * bmp.ch_matrix[3] + bmp.ch_matrix[6]), int( p.x * bmp.ch_matrix[1] + p.y * bmp.ch_matrix[4] + bmp.ch_matrix[7]) } diff --git a/vlib/x/ttf/text_block.v b/vlib/x/ttf/text_block.v index 58e909d351..51164a1c33 100644 --- a/vlib/x/ttf/text_block.v +++ b/vlib/x/ttf/text_block.v @@ -20,7 +20,7 @@ pub struct Text_block { cut_lines bool = true // force to cut the line if the length is over the text block width } -fn (mut dev BitMap) get_justify_space_cw(txt string, w int, block_w int, space_cw int) f32 { +pub fn (mut dev BitMap) get_justify_space_cw(txt string, w int, block_w int, space_cw int) f32 { num_spaces := txt.count(' ') if num_spaces < 1 { return 0 diff --git a/vlib/x/ttf/ttf.v b/vlib/x/ttf/ttf.v index 2c90f88783..91a065423f 100644 --- a/vlib/x/ttf/ttf.v +++ b/vlib/x/ttf/ttf.v @@ -771,7 +771,7 @@ fn (mut tf TTF_File) read_cmap(offset u32) { * CMAPS 0/4 * ******************************************************************************/ -fn (mut tf TTF_File) map_code(char_code int) u16 { +pub fn (mut tf TTF_File) map_code(char_code int) u16 { mut index := 0 for i in 0 .. tf.cmaps.len { mut cmap := tf.cmaps[i] @@ -1006,13 +1006,13 @@ fn (mut tf TTF_File) read_kern_table() { } } -fn (mut tf TTF_File) reset_kern() { +pub fn (mut tf TTF_File) reset_kern() { for i in 0 .. tf.kern.len { tf.kern[i].reset() } } -fn (mut tf TTF_File) next_kern(glyph_index int) (int, int) { +pub fn (mut tf TTF_File) next_kern(glyph_index int) (int, int) { mut x := 0 mut y := 0 for i in 0 .. tf.kern.len {