This module is designed to perform two main task
The render system can be single or multiple, for example it is possible to have a bitmap render and a HW accelerated render.
This part of the module do a simple task, load a TTF file and preprocess all the loaded data in order to simplify the rendering phase.
Let's start with a simple snippet of code that load a font from the disk:
mut ttf_font := ttf.TTF_File{}
ttf_font.buf = os.read_bytes("arial.ttf") or { panic(err) }
ttf_font.init()
Note: the font must be passed to the TTF_file
as RAM buffer.
At this point the font "arial" is loaded and parsed and if it is a valid TTF font it is
ready for the rendering.
We can get some quick info on the font as string using the get_info_string
function:
println(ttf_font.get_info_string())
that give an outpul like this:
----- Font Info -----
font_family : Arial
font_sub_family : Normal
full_name : Arial
postscript_name : ArialMT
version : 1
font_revision : 5.06
magic_number : 5f0f3cf5
flags : 81b
created unixTS : 649950890
modified unixTS : 1282151447
units_per_em : 2048
box : [x_min:-1361, y_min:-665, x_Max:4096, y_Max:2060]
mac_style : 0
-----------------------
Once loaded a font the TTF_File
struct is filled with the font data and texts can be rendered.
At high level no more action are required to use the loaded font.
Multiple fonts can be loaded without problems at the same time.
In this modue it is possible to have different renders running at the same time. At the present time all the rendering are made on the CPU, sokol is used only to draw the rendered text to the screen. Let's start with a simple snippet of code:
import os
import x.ttf
[console]
fn main(){
mut ttf_font := ttf.TTF_File{}
ttf_font.buf = os.read_bytes("arial.ttf") or { panic(err) }
ttf_font.init()
// print font info
println(ttf_font.get_info_string())
}
This simple code load a TTF font and display its basic informations.
The draw text function draw simple strings without indentation or other imagination tasks. At this point we can render a simple text:
import os
import x.ttf
[console]
fn main(){
mut ttf_font := ttf.TTF_File{}
ttf_font.buf = os.read_bytes("arial.ttf") or { panic(err) }
ttf_font.init()
// print font info
println(ttf_font.get_info_string())
bmp_width := 200
bmp_heigth := 64
bmp_layers := 4 // number of planes for an RGBA buffer
// memory size of the buffer
bmp_size := bmp_width * bmp_heigth * bmp_layers
font_size := 32 // font size in points
device_dpi := 72 // default screen DPI
// Formula for scale calculation
// scaler := (font_size * device dpi) / (72dpi * em_unit)
scale := f32(font_size * device_dpi) / f32(72 * ttf_font.units_per_em)
// height of the font to use in the buffer to separate the lines
y_base := int((ttf_font.y_max - ttf_font.y_min) * scale)
// declare the bitmap struct
mut bmp:= ttf.BitMap{
tf : &ttf_font
buf : malloc(bmp_size)
buf_size : bmp_size
width : bmp_width
height : bmp_heigth
bp : bmp_layers
color : 0x000000_FF // RGBA black
scale : scale
}
bmp.init_filler()
bmp.clear()
bmp.set_pos(10,y_base)
bmp.draw_text("Test Text!")
bmp.save_as_ppm("test.ppm")
}
This is the low level render that draw ther text on a bitmap and save the bitmap on a disk as
.ppm
file.
Note: The render in this case is a raw rendering without any postfiltering or other processing.
Using the low level rendering you need to manage all the amenities like allocate and release memory and other tasks like calc the character dimensions.
You can specify the style for the text rendering in the BitMap
struct::
enum Style {
outline
outline_aliased
filled // default syle
raw
}
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.
import os
import x.ttf
[console]
fn main(){
mut ttf_font := ttf.TTF_File{}
ttf_font.buf = os.read_bytes("arial.ttf") or { panic(err) }
ttf_font.init()
// print font info
println(ttf_font.get_info_string())
bmp_width := 200
bmp_heigth := 200
bmp_layers := 4 // number of planes for an RGBA buffer
// memory size of the buffer
bmp_size := bmp_width * bmp_heigth * bmp_layers
font_size := 32 // font size in points
device_dpi := 72 // default screen DPI
// Formula for scale calculation
// scaler := (font_size * device dpi) / (72dpi * em_unit)
scale := f32(font_size * device_dpi) / f32(72 * ttf_font.units_per_em)
// height of the font to use in the buffer to separate the lines
y_base := int((ttf_font.y_max - ttf_font.y_min) * scale)
text := "Today it is a good day!
Tomorrow I'm not so sure :(
But Vwill prevail for sure, V is the way!!
òàèì@ò!£$%&
"
// declare the bitmap struct
mut bmp:= ttf.BitMap{
tf : &ttf_font
buf : malloc(bmp_size)
buf_size : bmp_size
width : bmp_width
height : bmp_heigth
bp : bmp_layers
color : 0x000000_FF // RGBA black
scale : scale
}
bmp.init_filler()
bmp.clear()
bmp.justify = true
bmp.align = .left
bmp.draw_text_block(text, {x: 0, y:0, w: bmp_width-20, h: bmp_heigth})
bmp.save_as_ppm("test.ppm")
}
This is the low level render that draw text block on the bitmap.
A text block is defined from a Text_block
struct:
struct Text_block {
x int // x postion of the left high corner
y int // y postion of the left high corner
w int // width of the text block
h int // heigth of the text block
cut_lines bool = true // force to cut the line if the length is over the text block width
}
and use the following bitmap fields:
style Style = .filled // default syle
align Text_align = .left // default text align
justify bool // justify text flag, default deactivated
justify_fill_ratio f32 = 0.5 // justify fill ratio, if the ratio of the filled row is >= of this then justify the text
It is possible to modify these parameters to obtain the desired effect on the text rendering.
The sokol render use the bitmap render to create the text and the gg
functions to render
the text to the screen.
It is mor esimpel to use in a gg app
that the raw bitmap render.
Each single text rendered need its own reder to be declared, after you can modify it.
Here a simple example of the usage:
import gg
import gx
import sokol.sapp
import sokol.sgl
import x.ttf
import os
const (
win_width = 600
win_height = 700
bg_color = gx.white
font_paths = [
"arial.ttf"
]
)
struct App_data {
pub mut:
gg &gg.Context
sg_img C.sg_image
init_flag bool
frame_c int
tf []ttf.TTF_File
ttf_render []ttf.TTF_render_Sokol
}
fn my_init(mut app App_data) {
app.init_flag = true
}
fn draw_frame(mut app &App_data) {
cframe_txt := "Current Frame: $app.frame_c"
app.gg.begin()
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
// draw text only if the app is already initialized
if app.init_flag == true {
// update the text
mut txt1 := &app.ttf_render[0]
txt1.destroy_texture()
txt1.create_text(cframe_txt ,43)
txt1.create_texture()
txt1.draw_text_bmp(app.gg, 30, 60)
}
app.frame_c++
app.gg.end()
}
[console]
fn main(){
mut app := &App_data{
gg: 0
}
app.gg = gg.new_context({
width: win_width
height: win_height
use_ortho: true // This is needed for 2D drawing
create_window: true
window_title: 'Test TTF module'
user_data: app
bg_color: bg_color
frame_fn: draw_frame
init_fn: my_init
})
// load TTF fonts
for font_path in font_paths {
mut tf := ttf.TTF_File{}
tf.buf = os.read_bytes(font_path) or { panic(err) }
println("TrueTypeFont file [$font_path] len: ${tf.buf.len}")
tf.init()
println(tf.get_info_string())
app.tf << tf
}
// TTF render 0 Frame counter
app.ttf_render << &ttf.TTF_render_Sokol {
bmp: &ttf.BitMap{
tf: &(app.tf[0])
buf: malloc(32000000)
buf_size: (32000000)
color : 0xFF0000FF
//style: .raw
}
}
app.gg.run()
}
fn color_multiply(c u32, level f32) u32
fn color_multiply_alpha(c u32, level f32) u32
enum Style {
outline
outline_aliased
filled
raw
}
draw style
enum Text_align {
left
center
right
justify
}
text align
struct BitMap {
pub mut:
tf &TTF_File
buf &byte
buf_size int
width int = 1
height int = 1
bp int = 4
bg_color u32 = 00xFFFFFF00
color u32 = 00x000000FF
scale f32 = 1.0
scale_x f32 = 1.0
scale_y f32 = 1.0
angle f32 = 0.0
space_cw f32 = 1.0
space_mult f32 = f32(0.0)
tr_matrix []f32 = [f32(1), 0, 0, 0, 1, 0, 0, 0, 0]
ch_matrix []f32 = [f32(1), 0, 0, 0, 1, 0, 0, 0, 0]
style Style = .filled
align Text_align = .left
justify bool
justify_fill_ratio f32 = 0.5
filler [][]int
use_font_metrics bool
}
fn (mut bmp BitMap) aline(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32)
aline draw an aliased line on the bitmap
fn (mut bmp BitMap) box(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32)
fn (mut bmp BitMap) clear()
clear clear the bitmap with 0 bytes
fn (mut bmp BitMap) clear_filler()
fn (mut bmp BitMap) draw_glyph(index u16) (int, int)
fn (mut bmp BitMap) draw_text(in_string string) (int, int)
fn (mut bmp BitMap) draw_text_block(text string, block Text_block)
write out a text
fn (mut bmp BitMap) exec_filler()
fn (mut bmp BitMap) fline(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32)
fn (mut bmp BitMap) get_bbox(in_string string) (int, int)
fn (mut bmp BitMap) get_chars_bbox(in_string string) []int
fn (mut bmp BitMap) get_raw_bytes() []byte
fn (mut bmp BitMap) init_filler()
fn (mut bmp BitMap) line(in_x0 int, in_y0 int, in_x1 int, in_y1 int, c u32)
fn (mut bmp BitMap) plot(x int, y int, c u32) bool
fn (mut bmp BitMap) quadratic(in_x0 int, in_y0 int, in_x1 int, in_y1 int, in_cx int, in_cy int, c u32)
fn (mut bmp BitMap) save_as_ppm(file_name string)
write out a .ppm file
fn (mut bmp BitMap) save_raw_data(file_name string)
fn (mut bmp BitMap) set_pos(x f32, y f32)
set draw postion in the buffer
fn (mut bmp BitMap) set_rotation(a f32)
set the rotation angle in radiants
struct Glyph {
pub mut:
g_type u16 = ttf.g_type_simple
contour_ends []u16
number_of_contours i16
points []Point
x_min i16
x_max i16
y_min i16
y_max i16
valid_glyph bool
components []Component
}
struct Point {
pub mut:
x int
y int
on_curve bool
}
struct Text_block {
x int
y int
w int
h int
cut_lines bool = true
}
struct TTF_File {
pub mut:
buf []byte
pos u32
length u16
scalar_type u32
search_range u16
entry_selector u16
range_shift u16
tables map[string]Offset_Table
version f32
font_revision f32
checksum_adjustment u32
magic_number u32
flags u16
units_per_em u16
created u64
modified u64
x_min f32
y_min f32
x_max f32
y_max f32
mac_style u16
lowest_rec_ppem u16
font_direction_hint i16
index_to_loc_format i16
glyph_data_format i16
font_family string
font_sub_family string
full_name string
postscript_name string
cmaps []TrueTypeCmap
ascent i16
descent i16
line_gap i16
advance_width_max u16
min_left_side_bearing i16
min_right_side_bearing i16
x_max_extent i16
caret_slope_rise i16
caret_slope_run i16
caret_offset i16
metric_data_format i16
num_of_long_hor_metrics u16
kern []Kern0Table
glyph_cache map[int]Glyph
}
fn (mut tf TTF_File) init()
fn (mut tf TTF_File) get_horizontal_metrics(glyph_index u16) (int, int)
fn (mut tf TTF_File) read_glyph_dim(index u16) (int, int, int, int)
fn (mut tf TTF_File) read_glyph(index u16) Glyph
fn (tf TTF_File) get_info_string() string
struct TTF_render_Sokol {
pub mut:
bmp &BitMap
sg_img C.sg_image
scale_reduct f32 = 2.0
device_dpi int = 72
}
fn (mut tf_skl TTF_render_Sokol) create_text(in_txt string, in_font_size f32)
fn (mut tf_skl TTF_render_Sokol) create_text_block(in_txt string, in_w int, in_h int, in_font_size f32)
fn (mut tf_skl TTF_render_Sokol) create_texture()
fn (tf_skl TTF_render_Sokol) destroy_texture()
fn (mut tf_skl TTF_render_Sokol) update_text_texture()
Use only if usage: .dynamic
fn (tf_skl TTF_render_Sokol) draw_text_bmp(ctx &gg.Context, x f32, y f32)