aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/text.c
blob: 31ae794c6b03856e3580cab757292067a35905d7 (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
#include "../../include/mgl/graphics/text.h"
#include "../../include/mgl/graphics/font.h"
#include "../../include/mgl/mgl.h"

/*
    TODO: Do syntax highlight in constructor/mgl_text_set_string for optimization.
    Syntax highlighting should the first text that is visible in the text editor and find the start and end,
    and start syntax highlight from there until the last visible text.
    In that case there should also be a function to rerun the syntax highlighting, to update it
    when an asynchronous process finishes that parses code, such as a lsp client parsing C++ code which
    takes a while.
*/

#define TAB_WIDTH 4

static bool default_syntax_highlight(void *userdata, const char *str, size_t size, mgl_color *color) {
    (void)userdata;
    (void)str;
    (void)size;
    (void)color;
    return false;
}

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

static mgl_vec2f mgl_text_calculate_bounds(mgl_text *self) {
    mgl_vec2f bounds;
    bounds.x = 0.0f;
    bounds.y = self->font->character_size;

    /* TODO: Combine this with the loop in mgl_text_draw */
    mgl_font_glyph glyph;
    float width = 0.0f;
    for(size_t i = 0; i < self->text_size; ++i) {
        unsigned char c = *(unsigned char*)&self->text[i];
        if((c >= 32 && c < 128)) {
            if(mgl_font_get_glyph(self->font, c, &glyph) == 0) {
                width += glyph.advance;
                bounds.x = fmax(bounds.x, width);
            }
        } else if(c == '\t') {
            if(mgl_font_get_glyph(self->font, ' ', &glyph) == 0) {
                width += (glyph.advance * TAB_WIDTH);
                bounds.x = fmax(bounds.x, width);
            }
        } else if(c == '\n') {
            width = 0.0f;
            bounds.y += self->font->character_size;
        }
    }

    return bounds;
}

int mgl_text_init(mgl_text *self, mgl_font *font, const char *str, size_t str_size, mgl_text_options *options) {
    self->font = font;
    mgl_text_set_string(self, str, str_size);
    self->color = (mgl_color){ 255, 255, 255, 255 };
    self->position = (mgl_vec2f){ 0.0f, 0.0f };

    if(options) {
        self->options = *options;
    } else {
        self->options.userdata = NULL;
        self->options.before_syntax_highlight = NULL;
    }

    if(!options || !options->syntax_highlight)
        self->options.syntax_highlight = default_syntax_highlight;

    return 0;
}

void mgl_text_deinit(mgl_text *self) {
    (void)self;
}

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 = mgl_text_calculate_bounds(self);
    else
        self->bounds = (mgl_vec2f){ 0.0f, 0.0f };
}

void mgl_text_set_font(mgl_text *self, mgl_font *font) {
    self->font = font;
    if(self->font) {
        if(self->bounds.x < 0.001f && self->bounds.y < 0.001f)
            self->bounds = mgl_text_calculate_bounds(self);
    } else {
        self->bounds = (mgl_vec2f){ 0.0f, 0.0f };
    }
}

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(const mgl_text *self) {
    return self->bounds;
}

static void mgl_text_draw_glyph(mgl_context *context, mgl_font_glyph *glyph, mgl_vec2f 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);
}

static void mgl_text_run_syntax_highlight(mgl_context *context, mgl_text *text, const char *str, size_t size, mgl_color *color, bool *color_changed) {
    if(text->options.syntax_highlight(text->options.userdata, str, size, color)) {
        context->gl.glColor4ub(color->r, color->g, color->b, color->a);
        *color_changed = true;
    } else {
        if(*color_changed) {
            *color = text->color;
            context->gl.glColor4ub(color->r, color->g, color->b, color->a);
            *color_changed = false;
        }
    }
}

/* 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;

    mgl_font_glyph glyph;
    mgl_vec2f position = text->position;
    position.y += text->font->character_size;

    mgl_color color = text->color;
    bool color_changed = false;

    if(text->options.before_syntax_highlight)
        text->options.before_syntax_highlight(text->options.userdata);

    context->gl.glColor4ub(text->color.r, text->color.g, text->color.b, text->color.a);
    context->gl.glBindTexture(GL_TEXTURE_2D, text->font->texture.id);
    context->gl.glBegin(GL_QUADS);
    for(size_t i = 0; i < text->text_size; ++i) {
        unsigned char c = *(unsigned char*)&text->text[i];
        if((c >= 32 && c < 128)) {
            if(mgl_font_get_glyph(text->font, c, &glyph) == 0) {
                mgl_text_run_syntax_highlight(context, text, (const char*)&c, 1, &color, &color_changed);
                mgl_text_draw_glyph(context, &glyph, position);
                position.x += glyph.advance;
            }
        } else if(c == '\t') {
            if(mgl_font_get_glyph(text->font, ' ', &glyph) == 0) {
                for(int i = 0; i < TAB_WIDTH; ++i) {
                    mgl_text_draw_glyph(context, &glyph, position);
                    position.x += glyph.advance;
                }
            }
        } else if(c == '\n') {
            position.x = text->position.x;
            position.y += text->font->character_size;
        }
    }
    context->gl.glEnd();
    context->gl.glBindTexture(GL_TEXTURE_2D, 0);
}