From ad9d9125f89df12b4ea18d7eb6a7cc47f00aae1d Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 31 Jan 2026 21:13:02 +0200 Subject: [PATCH] breaking,x.ttf: extract vlib/x/ttf/render_sokol_cpu.v in a separate module x.ttf.render_sokol, to enable headless programs, that do not use gg, to not depend on OpenGL, DX12, Metal etc, through sokol (#26477) --- examples/ttf_font/draw_static_text.v | 7 ++++--- examples/ttf_font/example_ttf.v | 9 +++++---- vlib/x/ttf/README.md | 8 ++++---- vlib/x/ttf/common.v | 11 +++++------ .../ttf/{render_sokol_cpu.v => render_sokol/cpu.v} | 13 +++++++------ vlib/x/ttf/text_block.v | 1 + 6 files changed, 26 insertions(+), 23 deletions(-) rename vlib/x/ttf/{render_sokol_cpu.v => render_sokol/cpu.v} (96%) diff --git a/examples/ttf_font/draw_static_text.v b/examples/ttf_font/draw_static_text.v index e67bbe6da..d70ac22f2 100644 --- a/examples/ttf_font/draw_static_text.v +++ b/examples/ttf_font/draw_static_text.v @@ -2,6 +2,7 @@ import gg import sokol.sapp import sokol.sgl import x.ttf +import x.ttf.render_sokol import os const custom_font_path = os.args[1] or { @@ -27,7 +28,7 @@ pub mut: gg &gg.Context = unsafe { nil } init_flag bool tf []ttf.TTF_File - ttf_render []ttf.TTF_render_Sokol + ttf_render []render_sokol.TTF_render_Sokol text_ready_flag bool } @@ -89,7 +90,7 @@ fn main() { } // TTF hello render - app.ttf_render << &ttf.TTF_render_Sokol{ + app.ttf_render << &render_sokol.TTF_render_Sokol{ bmp: &ttf.BitMap{ tf: &app.tf[0] buf: unsafe { malloc_noscan(32000000) } @@ -99,7 +100,7 @@ fn main() { } // TTF custom text render - app.ttf_render << &ttf.TTF_render_Sokol{ + app.ttf_render << &render_sokol.TTF_render_Sokol{ bmp: &ttf.BitMap{ tf: &app.tf[1] buf: unsafe { malloc_noscan(32000000) } diff --git a/examples/ttf_font/example_ttf.v b/examples/ttf_font/example_ttf.v index 84d835d00..22a695d5b 100644 --- a/examples/ttf_font/example_ttf.v +++ b/examples/ttf_font/example_ttf.v @@ -3,6 +3,7 @@ import sokol.sapp import sokol.sgl import sokol.gfx import x.ttf +import x.ttf.render_sokol import os // import math @@ -22,7 +23,7 @@ pub mut: init_flag bool frame_c int tf []ttf.TTF_File - ttf_render []ttf.TTF_render_Sokol + ttf_render []render_sokol.TTF_render_Sokol text_ready_flag bool mouse_x int = -1 mouse_y int = -1 @@ -136,7 +137,7 @@ fn main() { app.tf << tf } // TTF render 0 Frame counter - app.ttf_render << &ttf.TTF_render_Sokol{ + app.ttf_render << &render_sokol.TTF_render_Sokol{ bmp: &ttf.BitMap{ tf: &app.tf[0] buf: unsafe { malloc_noscan(32000000) } @@ -147,7 +148,7 @@ fn main() { } } // TTF render 1 Text Block - app.ttf_render << &ttf.TTF_render_Sokol{ + app.ttf_render << &render_sokol.TTF_render_Sokol{ bmp: &ttf.BitMap{ tf: &app.tf[1] // color : 0xFF0000_10 @@ -156,7 +157,7 @@ fn main() { } } // TTF mouse position render - app.ttf_render << &ttf.TTF_render_Sokol{ + app.ttf_render << &render_sokol.TTF_render_Sokol{ bmp: &ttf.BitMap{ tf: &app.tf[0] } diff --git a/vlib/x/ttf/README.md b/vlib/x/ttf/README.md index 2f45c54cb..841c4722b 100644 --- a/vlib/x/ttf/README.md +++ b/vlib/x/ttf/README.md @@ -65,7 +65,7 @@ At the present time all the rendering are made on the CPU, sokol is used only to rendered text to the screen. Let's start with a simple snippet of code: -```v oksyntax +```v import os import x.ttf @@ -85,7 +85,7 @@ This simple code load a TTF font and display its basic information. The draw text function draw simple strings without indentation or other imagination tasks. At this point we can render a simple text: -```v oksyntax +```v import os import x.ttf @@ -113,7 +113,7 @@ fn main() { // declare the bitmap struct mut bmp := ttf.BitMap{ tf: &ttf_font - buf: malloc(bmp_size) + buf: unsafe { malloc(bmp_size) } buf_size: bmp_size width: bmp_width height: bmp_height @@ -155,7 +155,7 @@ Use this level only if you want achieve particular result on text rendering. Draw text block draw a justified and indented block of multiline text in the bitmap. -```v oksyntax +```v import os import x.ttf diff --git a/vlib/x/ttf/common.v b/vlib/x/ttf/common.v index 0e23fad33..6002d13d9 100644 --- a/vlib/x/ttf/common.v +++ b/vlib/x/ttf/common.v @@ -36,12 +36,11 @@ pub enum Style { * DEBUG Utility * ******************************************************************************/ -const debug_flag = false -fn dprintln(txt string) { - if debug_flag { - println(txt) - } +// dprintln prints the `txt` message, *only* when `-d debug_flag` is passed while compiling a program +@[if debug_flag ?] +pub fn dprintln(txt string) { + println(txt) } /****************************************************************************** @@ -50,7 +49,7 @@ fn dprintln(txt string) { * ******************************************************************************/ // format_texture transforms the bitmap from one layer to color layers -fn (mut bmp BitMap) format_texture() { +pub fn (mut bmp BitMap) format_texture() { r := u8(bmp.color >> 24) g := u8((bmp.color >> 16) & 0xFF) b := u8((bmp.color >> 8) & 0xFF) diff --git a/vlib/x/ttf/render_sokol_cpu.v b/vlib/x/ttf/render_sokol/cpu.v similarity index 96% rename from vlib/x/ttf/render_sokol_cpu.v rename to vlib/x/ttf/render_sokol/cpu.v index dad08b32b..66e591fb2 100644 --- a/vlib/x/ttf/render_sokol_cpu.v +++ b/vlib/x/ttf/render_sokol/cpu.v @@ -1,4 +1,4 @@ -module ttf +module render_sokol /********************************************************************** * @@ -16,11 +16,12 @@ import math import gg import sokol.sgl import sokol.gfx +import x.ttf // TTF_render_Sokol is a structure containing data for rendering a TTF font as a sokol texture. pub struct TTF_render_Sokol { pub mut: - bmp &BitMap = unsafe { nil } // Base bitmap render + bmp &ttf.BitMap = unsafe { nil } // Base bitmap render // rendering fields sg_img gfx.Image // sokol image sg_smp gfx.Sampler // sokol sampler @@ -47,7 +48,7 @@ pub fn (mut tf_skl TTF_render_Sokol) create_text(in_txt string, in_font_size f32 // Formula: (font_size * device dpi) / (72dpi * em_unit) // scale := ((1.0 * device_dpi )/ f32(72 * tf_skl.bmp.tf.units_per_em))* font_size scale := f32(font_size * device_dpi) / f32(72 * int(tf_skl.bmp.tf.units_per_em)) - // dprintln("Scale: $scale") + // ttf.dprintln("Scale: $scale") tf_skl.bmp.scale = scale * scale_reduct w, h := tf_skl.bmp.get_bbox(in_txt) @@ -60,7 +61,7 @@ pub fn (mut tf_skl TTF_render_Sokol) create_text(in_txt string, in_font_size f32 if sz > 0 { unsafe { free(tf_skl.bmp.buf) } } - dprintln('create_text Alloc: ${sz} bytes') + ttf.dprintln('create_text Alloc: ${sz} bytes') tf_skl.bmp.buf = unsafe { malloc_noscan(sz) } tf_skl.bmp.buf_size = sz } @@ -83,7 +84,7 @@ pub fn (mut tf_skl TTF_render_Sokol) create_text_block(in_txt string, in_w int, // Formula: (font_size * device dpi) / (72dpi * em_unit) // scale := ((1.0 * device_dpi )/ f32(72 * tf_skl.bmp.tf.units_per_em))* font_size scale := f32(font_size * device_dpi) / f32(72 * int(tf_skl.bmp.tf.units_per_em)) - // dprintln("Scale: $scale") + // ttf.dprintln("Scale: $scale") tf_skl.bmp.scale = scale * scale_reduct w := in_w @@ -99,7 +100,7 @@ pub fn (mut tf_skl TTF_render_Sokol) create_text_block(in_txt string, in_w int, if sz > 0 { unsafe { free(tf_skl.bmp.buf) } } - dprintln('Alloc: ${sz} bytes') + ttf.dprintln('Alloc: ${sz} bytes') tf_skl.bmp.buf = unsafe { malloc_noscan(sz) } tf_skl.bmp.buf_size = sz } diff --git a/vlib/x/ttf/text_block.v b/vlib/x/ttf/text_block.v index 537a28f91..b8543d8e9 100644 --- a/vlib/x/ttf/text_block.v +++ b/vlib/x/ttf/text_block.v @@ -14,6 +14,7 @@ module ttf **********************************************************************/ // Text_block represents a visual block of TTF text. pub struct Text_block { +pub: x int // x position of the left high corner y int // y position of the left high corner w int // width of the text block -- 2.39.5