aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/text.c
blob: 92c578c5bc9c18d86675fafc03bb920dd63d4977 (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
#include "../../include/mgl/graphics/text.h"
#include "../../include/mgl/graphics/font.h"
#include "../../include/mgl/system/utf8.h"
#include "../../include/mgl/mgl.h"
#include <stdio.h>

/* TODO: Cache mgl_font_get_glyph */

#define TAB_WIDTH 4

/* Return false to stop the loop. |glyph| is not valid if codepoint is '\n' */
typedef bool (*codepoint_loop_callback)(size_t codepoint_index, uint32_t codepoint, const mgl_font_glyph *glyph, void *userdata);
static void mgl_text_for_each_codepoint(const mgl_text *self, codepoint_loop_callback callback, void *userdata) {
    mgl_font_glyph glyph;
    size_t codepoint_index = 0;
    for(size_t i = 0; i < self->text_size;) {
        unsigned char *cp = (unsigned char*)&self->text[i];
        uint32_t codepoint;
        size_t clen;
        if(!mgl_utf8_decode(cp, self->text_size - i, &codepoint, &clen)) {
            codepoint = *cp;
            clen = 1;
        }

        if(codepoint == '\t') {
            if(mgl_font_get_glyph(self->font, ' ', &glyph) == 0) {
                if(!callback(codepoint_index, codepoint, &glyph, userdata))
                    return;
            }
        } else if(codepoint == '\n') {
            if(!callback(codepoint_index, codepoint, &glyph, userdata))
                return;
        } else {
            if(mgl_font_get_glyph(self->font, codepoint, &glyph) == 0) {
                if(!callback(codepoint_index, codepoint, &glyph, userdata))
                    return;
            }
        }

        i += clen;
        ++codepoint_index;
    }
}

static float max_float(float a, float b) {
    return a >= b ? a : b;
}

typedef struct {
    mgl_vec2f bounds;
    float line_width;
    const mgl_font *font;
    uint32_t prev_codepoint;
} CalculateBoundsUserdata;

static bool calculate_bounds_callback(size_t codepoint_index, uint32_t codepoint, const mgl_font_glyph *glyph, void *userdata) {
    (void)codepoint_index;

    CalculateBoundsUserdata *calculate_bounds_userdata = userdata;
    if(codepoint == '\t') {
        calculate_bounds_userdata->line_width += (glyph->advance * TAB_WIDTH) + mgl_font_get_kerning(calculate_bounds_userdata->font, calculate_bounds_userdata->prev_codepoint, codepoint);
        calculate_bounds_userdata->bounds.x = max_float(calculate_bounds_userdata->bounds.x, calculate_bounds_userdata->line_width);
    } else if(codepoint == '\n') {
        calculate_bounds_userdata->line_width = 0.0f;
        calculate_bounds_userdata->bounds.y += calculate_bounds_userdata->font->character_size;
        calculate_bounds_userdata->prev_codepoint = 0;
    } else {
        calculate_bounds_userdata->line_width += glyph->advance + mgl_font_get_kerning(calculate_bounds_userdata->font, calculate_bounds_userdata->prev_codepoint, codepoint);
        calculate_bounds_userdata->bounds.x = max_float(calculate_bounds_userdata->bounds.x, calculate_bounds_userdata->line_width);
    }

    calculate_bounds_userdata->prev_codepoint = codepoint;
    return true;
}

static mgl_vec2f mgl_text_calculate_bounds(mgl_text *self) {
    CalculateBoundsUserdata calculate_bounds_userdata;
    calculate_bounds_userdata.bounds.x = 0.0f;
    calculate_bounds_userdata.bounds.y = self->font->character_size;
    calculate_bounds_userdata.line_width = 0.0f;
    calculate_bounds_userdata.font = self->font;
    calculate_bounds_userdata.prev_codepoint = 0;
    mgl_text_for_each_codepoint(self, calculate_bounds_callback, &calculate_bounds_userdata);
    return calculate_bounds_userdata.bounds;
}

void mgl_text_init(mgl_text *self, mgl_font *font, const char *str, size_t str_size) {
    self->font = font;
    self->color = (mgl_color){ 255, 255, 255, 255 };
    self->position = (mgl_vec2f){ 0.0f, 0.0f };
    self->text = NULL;
    self->text_size = 0;
    self->bounds_dirty = true;
    mgl_text_set_string(self, str, str_size);
}

void mgl_text_deinit(mgl_text *self) {
    self->font = NULL;
    self->position = (mgl_vec2f){ 0.0f, 0.0f };
    self->text = NULL;
    self->text_size = 0;
    self->bounds = (mgl_vec2f){ 0.0f, 0.0f };
}

void mgl_text_set_string(mgl_text *self, const char *str, size_t str_size) {
    self->text = str;
    self->text_size = str_size;
    if(self->text && self->text_size > 0 && self->font)
        self->bounds_dirty = true;
}

void mgl_text_set_font(mgl_text *self, mgl_font *font) {
    self->font = font;
    if(self->text && self->text_size > 0 && self->font)
        self->bounds_dirty = true;
}

void mgl_text_set_position(mgl_text *self, mgl_vec2f position) {
    self->position = position;
}

void mgl_text_set_color(mgl_text *self, mgl_color color) {
    self->color = color;
}

mgl_vec2f mgl_text_get_bounds(mgl_text *self) {
    if(self->bounds_dirty) {
        self->bounds_dirty = false;
        if(self->text && self->text_size > 0 && self->font)
            self->bounds = mgl_text_calculate_bounds(self);
        else
            self->bounds = (mgl_vec2f){ 0.0f, 0.0f };
    }
    return self->bounds;
}

typedef struct {
    mgl_vec2f position;
    const mgl_text *text;
    size_t index;
    uint32_t prev_codepoint;
} FindCharacterPosUserdata;

static bool find_character_pos_callback(size_t codepoint_index, uint32_t codepoint, const mgl_font_glyph *glyph, void *userdata) {
    FindCharacterPosUserdata *find_character_pos_userdata = userdata;
    if(codepoint_index >= find_character_pos_userdata->index)
        return false;

    if(codepoint == '\t') {
        find_character_pos_userdata->position.x += (glyph->advance * TAB_WIDTH) + mgl_font_get_kerning(find_character_pos_userdata->text->font, find_character_pos_userdata->prev_codepoint, codepoint);
    } else if(codepoint == '\n') {
        find_character_pos_userdata->position.x = find_character_pos_userdata->text->position.x;
        find_character_pos_userdata->position.y += find_character_pos_userdata->text->font->character_size;
        find_character_pos_userdata->prev_codepoint = 0;
    } else {
        find_character_pos_userdata->position.x += glyph->advance + mgl_font_get_kerning(find_character_pos_userdata->text->font, find_character_pos_userdata->prev_codepoint, codepoint);
    }
    return true;
}

mgl_vec2f mgl_text_find_character_pos(mgl_text *self, size_t index) {
    FindCharacterPosUserdata find_character_pos_userdata;
    find_character_pos_userdata.position = self->position;
    find_character_pos_userdata.text = self;
    find_character_pos_userdata.index = index;
    find_character_pos_userdata.prev_codepoint = 0;
    mgl_text_for_each_codepoint(self, find_character_pos_callback, &find_character_pos_userdata);
    return (mgl_vec2f){ find_character_pos_userdata.position.x, find_character_pos_userdata.position.y };
}

static void mgl_text_draw_glyph(mgl_context *context, const mgl_font_glyph *glyph, mgl_vec2i position) {
    context->gl.glTexCoord2f(glyph->texture_position.x, glyph->texture_position.y);
    context->gl.glVertex3f(position.x + glyph->position.x, position.y + glyph->position.y, 0.0f);

    context->gl.glTexCoord2f(glyph->texture_position.x + glyph->texture_size.x, glyph->texture_position.y);
    context->gl.glVertex3f(position.x + glyph->position.x + glyph->size.x, position.y + glyph->position.y, 0.0f);

    context->gl.glTexCoord2f(glyph->texture_position.x + glyph->texture_size.x, glyph->texture_position.y + glyph->texture_size.y);
    context->gl.glVertex3f(position.x + glyph->position.x + glyph->size.x, position.y + glyph->position.y + glyph->size.y, 0.0f);

    context->gl.glTexCoord2f(glyph->texture_position.x, glyph->texture_position.y + glyph->texture_size.y);
    context->gl.glVertex3f(position.x + glyph->position.x, position.y + glyph->position.y + glyph->size.y, 0.0f);
}

typedef struct {
    mgl_vec2f position;
    const mgl_text *text;
    mgl_context *context;
    uint32_t prev_codepoint;
} TextDrawUserdata;

static bool text_draw_callback(size_t codepoint_index, uint32_t codepoint, const mgl_font_glyph *glyph, void *userdata) {
    (void)codepoint_index;
    TextDrawUserdata *text_draw_userdata = userdata;
    if(codepoint == '\t') {
        text_draw_userdata->position.x += (glyph->advance * TAB_WIDTH) + mgl_font_get_kerning(text_draw_userdata->text->font, text_draw_userdata->prev_codepoint, codepoint);
    } else if(codepoint == '\n') {
        text_draw_userdata->position.x = text_draw_userdata->text->position.x;
        text_draw_userdata->position.y += text_draw_userdata->text->font->character_size;
        text_draw_userdata->prev_codepoint = 0;
    } else {
        text_draw_userdata->position.x += mgl_font_get_kerning(text_draw_userdata->text->font, text_draw_userdata->prev_codepoint, codepoint);
        mgl_text_draw_glyph(text_draw_userdata->context, glyph, (mgl_vec2i){ text_draw_userdata->position.x, text_draw_userdata->position.y });
        text_draw_userdata->position.x += glyph->advance;
    }
    return true;
}

/* TODO: Use opengl buffer object instead */
/* TODO: Cache texture bind to not bind texture if its already bound and do not bind texture 0 */
void mgl_text_draw(mgl_context *context, mgl_text *text) {
    if(!text->text || text->text_size == 0 || !text->font)
        return;

    /* Calculate bounds beforehand if needed, which also generates glyphs because opengl outputs an error if that is done inside glBegin/glEnd */
    /* TODO: Only run this on the new glyphs */
    mgl_text_get_bounds(text);

    TextDrawUserdata text_draw_userdata;
    text_draw_userdata.position = (mgl_vec2f){ text->position.x, text->position.y };
    text_draw_userdata.text = text;
    text_draw_userdata.context = context;
    text_draw_userdata.prev_codepoint = 0;

    context->gl.glColor4ub(text->color.r, text->color.g, text->color.b, text->color.a);
    mgl_texture_use(&text->font->texture);
    context->gl.glBegin(GL_QUADS);
    mgl_text_for_each_codepoint(text, text_draw_callback, &text_draw_userdata);
    context->gl.glEnd();
    mgl_texture_use(NULL);
}