aboutsummaryrefslogtreecommitdiff
path: root/include/mgl/graphics/text.h
blob: 9169ab0cd90af7447d43b933c943fd55cda2f329 (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
#ifndef MGL_TEXT_H
#define MGL_TEXT_H

/*
    TODO: Do not render text outside the window and start from the visible part.
    Support 64-bit text length on 32-bit systems, and use mmap to optimize loading very large files,
    and munmap the text parts that are not visible on the screen.
*/

#include "../system/vec.h"
#include "color.h"
#include <stddef.h>
#include <stdbool.h>

typedef struct mgl_font mgl_font;
typedef struct mgl_context mgl_context;

typedef struct {
    /* Optional */
    void (*before_syntax_highlight)(void *userdata);
    /* Return true if the text format should be changed. Optional */
    bool (*syntax_highlight)(void *userdata, const char *str, size_t size, mgl_color *color);
    void *userdata;
} mgl_text_options;

typedef struct {
    mgl_font *font; /* nullable */
    const char *text; /* nullable */
    size_t text_size;
    mgl_color color;
    mgl_vec2f position;
    mgl_text_options options;
} mgl_text;

/*
    Note: keeps a reference to |text|. |text| needs to be valid as long as |self| is used.
    |font| and |text| may be NULL.
    |load_options| can be NULL, in which case the default options are used.
*/
int mgl_text_init(mgl_text *self, mgl_font *font, const char *str, size_t str_size, mgl_text_options *options);
void mgl_text_deinit(mgl_text *self);

/* Note: keeps a reference to |text|. |text| needs to be valid as long as |self| is used. |text| may be NULL. */
void mgl_text_set_string(mgl_text *self, const char *str, size_t str_size);
/* |font| may be NULL */
void mgl_text_set_font(mgl_text *self, mgl_font *font);
void mgl_text_set_position(mgl_text *self, mgl_vec2f position);
void mgl_text_set_color(mgl_text *self, mgl_color color);
void mgl_text_draw(mgl_context *context, mgl_text *text);

#endif /* MGL_TEXT_H */