| 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. |
| 3 | module gg |
| 4 | |
| 5 | pub const align_right = HorizontalAlign.right |
| 6 | pub const align_left = HorizontalAlign.left |
| 7 | |
| 8 | struct 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 | |
| 19 | struct StringToRender { |
| 20 | x int |
| 21 | y int |
| 22 | text string |
| 23 | cfg TextCfg |
| 24 | } |
| 25 | |
| 26 | @[if debug_font ?] |
| 27 | fn debug_font_println(s string) { |
| 28 | println(s) |
| 29 | } |
| 30 | |
| 31 | @[markused; params] |
| 32 | pub struct TextCfg { |
| 33 | pub: |
| 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'`. |
| 47 | pub 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 | |