aboutsummaryrefslogtreecommitdiff
path: root/src/mgui/richtext.c
blob: 77ee3c108506c427d19bc72f5c0436b36bbbd5df (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "../../include/mgui/richtext.h"
#include "../../include/resource_loader.h"
#include "../../include/common.h"
#include "../../include/alloc.h"
#include <mgl/mgl.h>
#include <mgl/window/event.h>
#include <mgl/graphics/vertex.h>
#include <mgl/graphics/font.h>
#include <mgl/graphics/texture.h>
#include <mgl/system/utf8.h>
#include <string.h>
#include <assert.h>

#define TAB_WIDTH 4
#define LATIN_INDEX 0
#define CJK_INDEX 1
#define NUM_VERTEX_DATA 2

/* TODO: Scale richtext by scale setting */
/* TODO: Split multiple lines into multiple vertex draw calls and do not render the vertices outside the scissor */
/* TODO: Cleanup vertex when deleting mgui_richtext widget */

static int round_float(float value) {
    return value + 0.5f;
}

static int max_int(int a, int b) {
    return a >= b ? a : b;
}

static int min_int(int a, int b) {
    return a <= b ? a : b;
}

/* TODO: Is there a more efficient way to do this? maybe japanese characters have a specific bit-pattern? */
static bool is_japanese_codepoint(uint32_t codepoint) {
    return (codepoint >= 0x2E80 && codepoint <= 0x2FD5)     /* Kanji radicals */
        || (codepoint >= 0x3000 && codepoint <= 0x303F)     /* Punctuation */
        || (codepoint >= 0x3041 && codepoint <= 0x3096)     /* Hiragana */
        || (codepoint >= 0x30A0 && codepoint <= 0x30FF)     /* Katakana */
        || (codepoint >= 0x31F0 && codepoint <= 0x31FF)     /* Miscellaneous symbols and characters 1 */
        || (codepoint >= 0x3220 && codepoint <= 0x3243)     /* Miscellaneous symbols and characters 2 */
        || (codepoint >= 0x3280 && codepoint <= 0x337F)     /* Miscellaneous symbols and characters 3 */
        || (codepoint >= 0x3400 && codepoint <= 0x4DB5)     /* Kanji 1 */
        || (codepoint >= 0x4E00 && codepoint <= 0x9FCB)     /* Kanji 2 */
        || (codepoint >= 0xF900 && codepoint <= 0xFA6A)     /* Kanji 3 */
        || (codepoint >= 0xFF01 && codepoint <= 0xFF5E)     /* Alphanumeric and punctuation (full width) */
        || (codepoint >= 0xFF5F && codepoint <= 0xFF9F);    /* Katakana and punctuation (half width) */
}    

static bool is_korean_codepoint(uint32_t codepoint) {
    return codepoint >= 0xAC00 && codepoint <= 0xD7A3;
}    

/* TODO: Is there a more efficient way to do this? maybe chinese characters have a specific bit-pattern? */
static bool is_chinese_codepoint(uint32_t codepoint) {
    return (codepoint >= 0x4E00 && codepoint <= 0x9FFF)     /* CJK Unified Ideographs */
        || (codepoint >= 0x3400 && codepoint <= 0x4DBF)     /* CJK Unified Ideographs Extension A */
        || (codepoint >= 0x20000 && codepoint <= 0x2A6DF)   /* CJK Unified Ideographs Extension B */
        || (codepoint >= 0x2A700 && codepoint <= 0x2B73F)   /* CJK Unified Ideographs Extension C */
        || (codepoint >= 0x2B740 && codepoint <= 0x2B81F)   /* CJK Unified Ideographs Extension D */
        || (codepoint >= 0x2B820 && codepoint <= 0x2CEAF)   /* CJK Unified Ideographs Extension E */
        || (codepoint >= 0xF900 && codepoint <= 0xFAFF)     /* CJK Compatibility Ideographs */
        || (codepoint >= 0x2F800 && codepoint <= 0x2FA1F);  /* CJK Compatibility Ideographs Supplement */
}    

/* TODO: Merge chinese, japanese and korean codepoints into one function since they share ranges */
static bool is_cjk_codepoint(uint32_t codepoint) {
    return is_chinese_codepoint(codepoint) || is_japanese_codepoint(codepoint) || is_korean_codepoint(codepoint);
}

static void mgui_richtext_vertices_ensure_vertex(mgui_richtext *self, size_t vertex_index, size_t new_capacity) {
    mgui_richtext_vertex_data *vertex_data = &self->vertex_data[vertex_index];
    if(vertex_data->vertices_capacity >= new_capacity)
        return;

    size_t capacity = vertex_data->vertices_capacity;
    if(capacity == 0)
        capacity = 8;

    while(capacity < new_capacity) {
        capacity = capacity + (capacity >> 1); /* capacity *= 1.5 */
    }

    vertex_data->vertices = mgui_realloc(vertex_data->vertices, capacity);
    vertex_data->vertices_capacity = capacity;
}

static void mgui_richtext_vertices_append(mgui_richtext *self, size_t vertex_index, const mgl_vertex *vertex) {
    mgui_richtext_vertex_data *vertex_data = &self->vertex_data[vertex_index];
    mgui_richtext_vertices_ensure_vertex(self, vertex_index, (vertex_data->vertex_count + 1) * sizeof(mgl_vertex));
    vertex_data->vertices[vertex_data->vertex_count] = *vertex;
    ++vertex_data->vertex_count;
}

static void mgui_richtext_vertices_clear(mgui_richtext *self, size_t vertex_index) {
    self->vertex_data[vertex_index].vertex_count = 0;
}

static void mgui_richtext_vertices_free(mgui_richtext *self, size_t vertex_index) {
    mgui_free(self->vertex_data[vertex_index].vertices);
    self->vertex_data[vertex_index].vertices = NULL;
    self->vertex_data[vertex_index].vertices_capacity = 0;
    self->vertex_data[vertex_index].vertex_count = 0;
}

mgui_richtext* mgui_richtext_create(const char *str, size_t size, unsigned char character_size) {
    /* TODO: Make this work for character size >= 100 */
    if(character_size >= 100)
        character_size = 99;

    mgui_richtext *richtext = mgui_alloc(sizeof(mgui_richtext));
    mgui_widget_init(&richtext->widget, MGUI_WIDGET_RICHTEXT);
    richtext->str = mgui_alloc(size);
    richtext->str_size = size;
    richtext->character_size = character_size;
    memcpy(richtext->str, str, size);
    richtext->position = (mgl_vec2i){ 0, 0 };
    richtext->render_size = (mgl_vec2i){ 0, 0 };
    richtext->width = 0;
    for(size_t i = 0; i < NUM_VERTEX_DATA; ++i) {
        richtext->vertex_data[i].vertices = NULL;
        richtext->vertex_data[i].vertices_capacity = 0;
        richtext->vertex_data[i].vertex_count = 0;
    }
    richtext->dirty = true;
    richtext->vertices_dirty = true;
    return richtext;
}

void mgui_richtext_destroy(mgui_richtext *richtext) {
    for(size_t i = 0; i < NUM_VERTEX_DATA; ++i) {
        mgui_free(richtext->vertex_data[i].vertices);
    }
    mgui_free(richtext->str);
    mgui_free(richtext);
}

mgui_widget* mgui_richtext_to_widget(mgui_richtext *list) {
    return &list->widget;
}

mgui_richtext* mgui_widget_to_richtext(mgui_widget *widget) {
    assert(widget->type == MGUI_WIDGET_RICHTEXT);
    return (mgui_richtext*)widget;
}

void mgui_richtext_set_position(mgui_richtext *self, mgl_vec2i position) {
    self->position = position;
}

static void mgui_richtext_append_glyph(mgui_richtext *self, size_t vertex_index, mgl_vec2f position, mgl_color color, mgl_font_glyph *glyph) {
    const mgl_vertex top_left_vertex = {
        .position = (mgl_vec2f){ round_float(position.x + glyph->position.x), round_float(position.y + glyph->position.y) },
        .texcoords = (mgl_vec2f){ round_float(glyph->texture_position.x), round_float(glyph->texture_position.y) },
        .color = color
    };

    const mgl_vertex top_right_vertex = {
        .position = (mgl_vec2f){ round_float(position.x + glyph->position.x + glyph->size.x), round_float(position.y + glyph->position.y) },
        .texcoords = (mgl_vec2f){ round_float(glyph->texture_position.x) + round_float(glyph->texture_size.x), round_float(glyph->texture_position.y) },
        .color = color
    };

    const mgl_vertex bottom_left_vertex = {
        .position = (mgl_vec2f){ round_float(position.x + glyph->position.x), round_float(position.y + glyph->position.y + glyph->size.y) },
        .texcoords = (mgl_vec2f){ round_float(glyph->texture_position.x), round_float(glyph->texture_position.y + glyph->texture_size.y) },
        .color = color
    };

    const mgl_vertex bottom_right_vertex = {
        .position = (mgl_vec2f){ round_float(position.x + glyph->position.x + glyph->size.x), round_float(position.y + glyph->position.y + glyph->size.y) },
        .texcoords = (mgl_vec2f){ round_float(glyph->texture_position.x + glyph->texture_size.x), round_float(glyph->texture_position.y + glyph->texture_size.y) },
        .color = color
    };

    mgui_richtext_vertices_append(self, vertex_index, &top_right_vertex);
    mgui_richtext_vertices_append(self, vertex_index, &top_left_vertex);
    mgui_richtext_vertices_append(self, vertex_index, &bottom_left_vertex);
    mgui_richtext_vertices_append(self, vertex_index, &bottom_left_vertex);
    mgui_richtext_vertices_append(self, vertex_index, &bottom_right_vertex);
    mgui_richtext_vertices_append(self, vertex_index, &top_right_vertex);
}

static void mgui_richtext_update(mgui_richtext *self, bool build_vertices) {
    for(size_t i = 0; i < NUM_VERTEX_DATA; ++i) {
        mgui_richtext_vertices_clear(self, i);
    }

    mgl_font *font = NULL;
    mgl_vec2f position = (mgl_vec2f){ 0.0f, 0.0f };
    mgl_font_glyph glyph;
    uint32_t prev_codepoint = 0;
    size_t codepoint_index = 0;
    mgl_color color = (mgl_color){ 255, 255, 255, 255 };
    int vertex_index = -1;
    self->render_size = (mgl_vec2i){ 0, self->character_size };

    const mgui_font_type font_types[NUM_VERTEX_DATA] = {
        MGUI_FONT_LATIN,
        MGUI_FONT_CJK
    };
    
    for(size_t i = 0; i < self->str_size;) {
        unsigned char *cp = (unsigned char*)&self->str[i];
        uint32_t codepoint;
        size_t clen;
        if(!mgl_utf8_decode(cp, self->str_size - i, &codepoint, &clen)) {
            codepoint = *cp;
            clen = 1;
        }

        int new_vertex_index;
        if(is_cjk_codepoint(codepoint)) {
            new_vertex_index = CJK_INDEX;
        } else {
            new_vertex_index = LATIN_INDEX;
        }

        if(new_vertex_index != vertex_index) {
            vertex_index = new_vertex_index;
            font = mgui_get_font(font_types[vertex_index], self->character_size);
        }

        if(codepoint == '\t') {
            if(mgl_font_get_glyph(font, ' ', &glyph) == 0) {
                position.x += (glyph.advance * TAB_WIDTH);
                self->render_size.x = max_int(self->render_size.x, position.x);
            }
        } else if(codepoint == '\n') {
            position.x = 0;
            position.y += self->character_size;
            self->render_size.y += self->character_size;
        } else {
            if(mgl_font_get_glyph(font, codepoint, &glyph) == 0) {
                if(position.x + glyph.size.x > self->width) {
                    position.x = 0;
                    position.y += self->character_size;
                    self->render_size.y += self->character_size;
                }

                if(build_vertices)
                    mgui_richtext_append_glyph(self, vertex_index, position, color, &glyph);
                position.x += glyph.advance + mgl_font_get_kerning(font, prev_codepoint, codepoint);
                self->render_size.x = max_int(self->render_size.x, position.x);
            }
        }

        i += clen;
        ++codepoint_index;
        prev_codepoint = codepoint;
    }
}

void mgui_richtext_calculate_size(mgui_richtext *self, mgl_vec2i max_size) {
    /* TODO: Do not update if not visible on screen? */
    if(max_size.x != self->width) {
        self->width = max_size.x;
        const bool is_multiple_lines = self->render_size.y > (int)self->character_size;
        if(is_multiple_lines || self->width < self->render_size.x)
            self->dirty = true;
    }

    /* TODO: Instead of updating richtext vertices, calculcate the richtext bounds only and update the vertices in the draw function if dirty */
    if(self->dirty) {
        self->dirty = false;
        mgui_richtext_update(self, false);
        self->vertices_dirty = true;
        self->widget.size.x = self->render_size.x;
        self->widget.size.y = min_int(self->render_size.y, max_size.y);
    }
}

void mgui_richtext_on_event(mgui_richtext *self, mgl_window *window, mgl_event *event) {
    /* TODO: Implement */
    (void)self;
    (void)window;
    (void)event;
}

void mgui_richtext_draw(mgui_richtext *self, mgl_window *window) {
    if(mgui_rectangle_intersects_with_scissor(self->position, self->render_size, window)) {
        /* This can happen when the item is first not visible in its scissor and then becomes visible */
        if(self->vertices_dirty) {
            self->vertices_dirty = false;
            mgui_richtext_update(self, true);
        }

        const mgui_font_type font_types[NUM_VERTEX_DATA] = {
            MGUI_FONT_LATIN,
            MGUI_FONT_CJK
        };

        for(size_t i = 0; i < NUM_VERTEX_DATA; ++i) {
            if(self->vertex_data[i].vertex_count == 0)
                continue;

            const mgl_font *font = mgui_get_font(font_types[i], self->character_size);
            mgl_texture_use(&font->texture);
            mgl_vertices_draw(mgl_get_context(), self->vertex_data[i].vertices, self->vertex_data[i].vertex_count, MGL_PRIMITIVE_TRIANGLES, (mgl_vec2f){ self->position.x, self->position.y });
        }

        mgl_texture_use(NULL);
    } else {
        for(size_t i = 0; i < NUM_VERTEX_DATA; ++i) {
            mgui_richtext_vertices_free(self, i);
        }
        self->vertices_dirty = true;
    }
}