1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use std::sync::atomic::AtomicU64;

use crate::*;

pub struct DrawText {
    pub text: TextData,
    pub position: Vec2,
    pub font: egui::FontId,
    pub color: Color,
    pub align: TextAlign,
    // Temporarily to allow both egui and comfy rasterization
    pub pro_params: Option<ProTextParams>,
}

#[derive(Clone, Debug)]
pub struct TextParams {
    pub font: egui::FontId,
    pub rotation: f32,
    pub color: Color,
}

impl Default for TextParams {
    fn default() -> TextParams {
        TextParams {
            font: egui::FontId::new(20.0, egui::FontFamily::Monospace),
            color: WHITE,
            rotation: 0.0,
        }
    }
}

#[doc(hidden)]
pub enum TextData {
    Raw(String),
    Rich(RichText),
}

pub fn draw_text_ex(
    text: &str,
    position: Vec2,
    align: TextAlign,
    params: TextParams,
) {
    let _span = span!("draw_text_ex");

    draw_text_internal(
        TextData::Raw(text.to_string()),
        position,
        align,
        None,
        params,
    );
}

pub fn draw_text(text: &str, position: Vec2, color: Color, align: TextAlign) {
    draw_text_internal(
        TextData::Raw(text.to_string()),
        position,
        align,
        None,
        TextParams { color, ..Default::default() },
    )
}

/// This is a first iteration of Comfy's rich text rendering.
///
/// The API works and is fully usable, but it's going to change in backwards
/// incompatible ways in the future, which is the main reason for the `_experimental`
/// suffix.
///
/// This is not intended as a high performance API for rendering webpages
/// or fullscreen books as fast as possible. The goal is maximizing flexibility
/// and ergonomics of highly stylized text for games.
pub fn draw_text_pro_experimental(
    text: RichText,
    position: Vec2,
    color: Color,
    align: TextAlign,
    font_size: f32,
    font: FontHandle,
) {
    draw_text_internal(
        TextData::Rich(text),
        position,
        align,
        Some(ProTextParams { font_size, font }),
        TextParams { color, ..Default::default() },
    );
}

static FONT_HANDLE_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Internal use only font handle generation.
#[doc(hidden)]
pub fn gen_font_handle() -> FontHandle {
    FontHandle(
        FONT_HANDLE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
    )
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FontHandle(u64);

fn draw_text_internal(
    text: TextData,
    position: Vec2,
    align: TextAlign,
    pro_params: Option<ProTextParams>,
    params: TextParams,
) {
    GLOBAL_STATE.borrow_mut().text_queue.push(DrawText {
        text,
        position,
        color: params.color,
        font: params.font,
        align,
        pro_params,
    });
}

#[doc(hidden)]
/// Temporary while the API stabilizes.
pub struct ProTextParams {
    pub font: FontHandle,
    pub font_size: f32,
}

#[derive(Copy, Clone, Debug)]
pub enum TextAlign {
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
    Center,
}

#[derive(Copy, Clone, Debug)]
pub struct StyledGlyph {
    #[allow(dead_code)]
    /// The related character.
    ///
    /// We don't really need this, but it's easier for debugging the parser.
    pub char: char,
    /// If true the character will wiggle.
    pub wiggle: bool,
    /// Color override of the character
    pub color: Option<Color>,
}

pub struct RichText {
    pub clean_text: String,
    pub styled_glyphs: Vec<StyledGlyph>,
}

/// Parses a simple subset of markdown-like syntax.
///
/// Users should feel encouraged to build their own syntax for rich text
/// based on their needs. This should only serve as a baseline.
pub fn simple_styled_text(text: &str) -> RichText {
    let mut i = 0;

    let mut clean_text = String::new();
    let mut styled_glyphs = vec![];

    let chars = text.chars().collect_vec();

    while i < chars.len() {
        let mut c = chars[i];

        if c == '*' {
            i += 1;
            if i == chars.len() {
                break;
            }

            c = chars[i];
            styled_glyphs.push(StyledGlyph {
                char: c,
                wiggle: true,
                color: Some(PINK.boost(4.0)),
            });
        } else {
            styled_glyphs.push(StyledGlyph {
                char: c,
                wiggle: false,
                color: None,
            });
        }

        clean_text.push(c);

        i += 1;
    }

    RichText { clean_text, styled_glyphs }
}