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
|
/**
* Copyright (C) 2026 andromeda <andromeda@galaxious.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
module rfont;
const int MAX_GLYPHS = 256;
const int INIT_VERTS = 20 * MAX_GLYPHS;
typedef Texture = usz;
alias Surface = void*;
struct RenderData
{
float* verts;
float* tcoords;
ushort* elements;
Texture atlas;
usz nverts;
usz nelements;
}
alias RenderProcSize = fn usz();
alias RenderProcInitPtr = fn void(void*);
alias RenderProcCreateAtlas = fn Texture(void*, uint, uint);
alias RenderProcFreeAtlas = fn void(void*, Texture);
alias RenderProcBitmapToAtlas = fn void(void*, Texture, uint, uint, uint, char*, float, float, float*, float*);
alias RenderProcRender = fn void(void*, RenderData*);
alias RenderProcSetFramebuffer = fn void(void*, uint, uint);
alias RenderProcSetColor = fn void(void*, float, float, float, float);
alias RenderProcSetSurface = fn void(void*, Surface);
alias RenderProcFreePtr = fn void(void*);
struct RendererProc
{
RenderProcSize size;
RenderProcInitPtr init_ptr;
RenderProcCreateAtlas create_atlas;
RenderProcFreeAtlas free_atlas;
RenderProcBitmapToAtlas bitmap_to_atlas;
RenderProcRender render;
RenderProcSetFramebuffer set_framebuffer;
RenderProcSetColor set_color;
RenderProcSetSurface set_surface;
RenderProcFreePtr free_ptr;
}
struct Renderer
{
void* ctx;
RendererProc proc;
}
extern fn usz Renderer.size(&self) @cname("RFont_renderer_size");
extern fn Renderer* renderer_init(RendererProc) @cname("RFont_renderer_init");
extern fn void renderer_initPtr(RendererProc, void*, Renderer*) @cname("RFont_renderer_initPtr");
extern fn void Renderer.set_framebuffer(&self, uint, uint) @cname("RFont_renderer_set_framebuffer");
extern fn void Renderer.set_surface(&self, Surface) @cname("RFont_renderer_set_surface");
extern fn void Renderer.set_color(&self, float, float, float, float) @cname("RFont_renderer_set_color");
extern fn void Renderer.free(&self) @cname("RFont_renderer_free");
extern fn void Renderer.free_ptr(&self) @cname("RFont_renderer_freePtr");
struct Glyph
{
uint codepoint;
usz size;
int x;
int x2;
int y;
int y2;
Font* font;
int src;
float w;
float h;
float x1;
float y1;
float advance;
}
typedef Src = void;
struct Font
{
Src* src;
float fheight;
float descent;
float num_of_long_hor_metrics;
float space_adv;
uint max_height;
Glyph[MAX_GLYPHS] glyphs;
usz glyph_len;
Texture atlas;
usz atlas_width;
usz atlas_height;
float atlas_x;
float atlas_y;
float[INIT_VERTS * 3] verts;
float[INIT_VERTS * 2] tcoords;
ushort[INIT_VERTS * 6] elements;
}
extern fn CChar* codepoint_to_utf8(uint) @cname("RFont_codepoint_to_utf8");
extern fn Font* Renderer.font_init(&self, ZString, uint, usz, usz) @cname("RFont_font_init");
extern fn Font* Renderer.font_init_ptr(&self, ZString, uint, usz, usz, Font*) @cname("RFont_font_init_ptr");
extern fn Font* Renderer.font_init_data(&self, char*, uint, usz, usz) @cname("RFont_font_init_data");
extern fn Font* Renderer.font_init_data_ptr(&self, char*, uint, usz, usz, Font*) @cname("RFont_font_init_data_ptr");
extern fn void Renderer.font_free(&self, Font*) @cname("RFont_font_free");
extern fn void Renderer.font_free_ptr(&self, Font*) @cname("RFont_font_free_ptr");
alias GlyphFallbackCallback = fn Glyph(Renderer*, Font*, uint, usz);
extern fn GlyphFallbackCallback set_glyph_fallback_callback(GlyphFallbackCallback) @cname("RFont_set_glyph_fallback_callback");
extern fn Glyph Renderer.add_char(&self, Font*, CChar, usz) @cname("RFont_font_add_char");
extern fn Glyph Renderer.add_codepoint(&self, Font*, uint, usz) @cname("RFont_font_add_codepoint");
extern fn Glyph Renderer.add_codepoint_ex(&self, Font*, uint, usz, char) @cname("RFont_font_add_codepoint_ex");
extern fn void Renderer.add_string(&self, Font*, ZString, usz*, usz) @cname("RFont_font_add_string");
extern fn void Renderer.add_string_len(&self, Font*, ZString, usz, usz*, usz) @cname("RFont_font_add_string_len");
extern fn void Renderer.area(&self, Font*, ZString, uint, uint*, uint*) @cname("RFont_text_area");
extern fn void Renderer.area_spacing(&self, Font*, ZString, float, uint, uint*, uint*) @cname("RFont_text_area_spacing");
extern fn void Renderer.area_len(&self, Font*, ZString, usz, uint, usz, float, uint*, uint*) @cname("RFont_text_area_len");
extern fn void Renderer.draw_text(&self, Font*, ZString, float, float, uint) @cname("RFont_draw_text");
extern fn void Renderer.draw_text_spacing(&self, Font*, ZString, float, float, uint, float) @cname("RFont_draw_text_spacing");
extern fn void Renderer.draw_text_len(&self, Font*, ZString, usz, float, float, uint, float) @cname("RFont_draw_text_len");
|