v2 / vlib / gg / text_rendering.v
59 lines · 53 sloc · 1.3 KB · bbb61ab3687afe512a1fa12492c876d011626107
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
3module gg
4
5pub const align_right = HorizontalAlign.right
6pub const align_left = HorizontalAlign.left
7
8struct FTConfig {
9 font_path string
10 custom_bold_font_path string
11 scale f32 = 1.0
12 font_size int
13 bytes_normal []u8
14 bytes_bold []u8
15 bytes_mono []u8
16 bytes_italic []u8
17}
18
19struct StringToRender {
20 x int
21 y int
22 text string
23 cfg TextCfg
24}
25
26@[if debug_font ?]
27fn debug_font_println(s string) {
28 println(s)
29}
30
31@[markused; params]
32pub struct TextCfg {
33pub:
34 color Color = black
35 size int = 16
36 align HorizontalAlign = .left
37 vertical_align VerticalAlign = .top
38 max_width int
39 family string
40 bold bool
41 mono bool
42 italic bool
43}
44
45// to_css_string returns a CSS compatible string of the TextCfg `cfg`.
46// For example: `'mono 14px serif'`.
47pub fn (cfg &TextCfg) to_css_string() string {
48 mut font_style := ''
49 if cfg.bold {
50 font_style += 'bold '
51 }
52 if cfg.mono {
53 font_style += 'mono '
54 }
55 if cfg.italic {
56 font_style += 'italic '
57 }
58 return '${font_style} ${cfg.size}px ${cfg.family}'
59}
60