1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

x.ttf: vfmt the examples in the README.md

This commit is contained in:
Delyan Angelov 2021-06-24 11:13:57 +03:00
parent f18265e6a8
commit 96adfc9d5b
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -22,10 +22,10 @@ At this point the font "arial" is loaded and parsed and if it is a valid TTF fon
ready for the rendering.
We can get some quick info on the font as string using the `get_info_string` function:
```v ignore
```v oksyntax
println(ttf_font.get_info_string())
```
that give an outpul like this:
produces an output like this:
```
----- Font Info -----
font_family : Arial
@ -56,10 +56,11 @@ Let's start with a simple snippet of code:
```v oksyntax
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.buf = os.read_bytes('arial.ttf') or { panic(err) }
ttf_font.init()
// print font info
println(ttf_font.get_info_string())
@ -77,7 +78,7 @@ 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.buf = os.read_bytes('arial.ttf') or { panic(err) }
ttf_font.init()
// print font info
println(ttf_font.get_info_string())
@ -110,8 +111,8 @@ fn main(){
bmp.init_filler()
bmp.clear()
bmp.set_pos(10, y_base)
bmp.draw_text("Test Text!")
bmp.save_as_ppm("test.ppm")
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
@ -141,7 +142,7 @@ 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.buf = os.read_bytes('arial.ttf') or { panic(err) }
ttf_font.init()
// print font info
println(ttf_font.get_info_string())
@ -180,8 +181,8 @@ But Vwill prevail for sure, V is the way!!
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")
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.
@ -196,11 +197,12 @@ struct Text_block {
}
```
and use the following bitmap fields:
```v oksyntax
```v ignore
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
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.
@ -216,7 +218,6 @@ import gg
import gx
import sokol.sapp
import sokol.sgl
import x.ttf
import os
@ -225,7 +226,7 @@ const (
win_height = 700
bg_color = gx.white
font_paths = [
"arial.ttf"
'arial.ttf',
]
)
@ -244,8 +245,8 @@ 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"
fn draw_frame(mut app App_data) {
cframe_txt := 'Current Frame: $app.frame_c'
app.gg.begin()
@ -272,7 +273,7 @@ fn main(){
gg: 0
}
app.gg = gg.new_context({
app.gg = gg.new_context(
width: win_width
height: win_height
create_window: true
@ -281,13 +282,13 @@ fn main(){
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}")
println('TrueTypeFont file [$font_path] len: $tf.buf.len')
tf.init()
println(tf.get_info_string())
app.tf << tf
@ -297,7 +298,7 @@ fn main(){
app.ttf_render << &ttf.TTF_render_Sokol{
bmp: &ttf.BitMap{
tf: &(app.tf[0])
buf: malloc(32000000)
buf: unsafe { malloc(32000000) }
buf_size: (32000000)
color: 0xFF0000FF
// style: .raw