-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsokol_bitmap_font.h
More file actions
250 lines (207 loc) · 6.49 KB
/
sokol_bitmap_font.h
File metadata and controls
250 lines (207 loc) · 6.49 KB
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
#ifndef SOKOL_BITMAP_FONT_H
#define SOKOL_BITMAP_FONT_H
#include "sokol_gfx.h"
#include "sokol_gp.h"
#include <stdint.h>
#include <stdlib.h>
typedef struct sbm_allocator {
void *(*alloc)(size_t, void *);
void *(*realloc)(void *, size_t, void *);
void (*free)(void *, void *);
void *ctx;
} sbm_allocator;
typedef struct sbm_string_slice {
const char *items;
size_t len;
} sbm_string_slice;
typedef struct sbm_desc {
sg_image img;
int img_channel;
uint32_t img_width_pixels;
uint32_t img_height_pixels;
// the padding surrounding the chars
// not the padding between the chars
uint32_t img_outer_padding_x;
uint32_t img_outer_padding_y;
uint32_t char_width_pixels;
uint32_t char_height_pixels;
uint32_t char_padding_x_pixels;
uint32_t char_padding_y_pixels;
const char *chars;
size_t num_chars;
} sbm_desc;
typedef struct sbm_font {
sbm_allocator allocator;
sbm_desc opts;
uint32_t img_width_chars;
uint32_t img_height_chars;
// the largest ascii val in the jump table
size_t max_ascii_val;
size_t *char_jump_tbl;
} sbm_font;
extern bool build_char_jump_tbl(size_t *tbl, const char *chars,
size_t num_chars);
extern bool sbm_font_init(sbm_allocator allocator, sbm_font *self,
sbm_desc opts);
extern sbm_allocator sbm_default_allocator();
extern sgp_rect sbm_measure_line(sbm_font *self, sbm_string_slice slice,
float spacing, sgp_rect char_bounds);
extern size_t find_max(const char *chars, size_t num_chars);
extern void *sbm_alloc(size_t n, void *ctx);
extern void *sbm_realloc(void *mem, size_t n, void *ctx);
extern void sbm_draw_char(sbm_font *self, char c, sgp_rect r);
extern void sbm_draw_line(sbm_font *self, sbm_string_slice slice, float gap,
sgp_rect r);
extern void sbm_draw_lines(sbm_font *self, sbm_string_slice slice, float gap_x,
float gap_y, sgp_rect r);
extern void sbm_font_free(sbm_font *self);
extern void sbm_free(void *mem, void *ctx);
extern void sbm_prepare(sbm_font *self);
#if defined(SOKOL_BITMAP_IMPL) && !defined(SOKOL_BITMAP_IMPL_DONE)
#define SOKOL_BITMAP_IMPL_DONE
void *sbm_alloc(size_t n, void *ctx) {
(void)ctx;
return malloc(n);
}
void *sbm_realloc(void *mem, size_t n, void *ctx) {
(void)ctx;
return realloc(mem, n);
}
void sbm_free(void *mem, void *ctx) {
(void)ctx;
free(mem);
}
sbm_allocator sbm_default_allocator() {
return (sbm_allocator){
.alloc = sbm_alloc,
.realloc = sbm_realloc,
.free = sbm_free,
.ctx = NULL,
};
}
void sbm_prepare(sbm_font *self) {
sgp_set_image(self->opts.img_channel, self->opts.img);
}
sgp_rect sbm_measure_line(sbm_font *self, sbm_string_slice slice, float spacing,
sgp_rect char_bounds) {
return (sgp_rect){
.x = char_bounds.x,
.y = char_bounds.y,
.w = slice.len * char_bounds.w + (slice.len - 1) * spacing,
.h = char_bounds.h,
};
}
size_t find_max(const char *chars, size_t num_chars) {
if (num_chars == 0) {
return 0;
}
size_t max_val = chars[0];
for (size_t i = 0; i < num_chars; i++) {
if (chars[i] > max_val) {
max_val = chars[i];
}
}
return max_val;
}
bool build_char_jump_tbl(size_t *tbl, const char *chars, size_t num_chars) {
if (!tbl || !chars) {
return false;
}
for (size_t i = 0; i < num_chars; i++) {
tbl[(size_t)chars[i]] = i;
}
return true;
}
bool sbm_font_init(sbm_allocator allocator, sbm_font *self, sbm_desc opts) {
if (!self) {
return false;
}
self->allocator = allocator;
self->opts = opts;
self->img_width_chars = (opts.img_width_pixels + opts.char_padding_x_pixels -
(opts.img_outer_padding_x * 2)) /
(opts.char_width_pixels + opts.char_padding_x_pixels);
self->img_height_chars =
(opts.img_height_pixels + opts.char_padding_y_pixels -
(opts.img_outer_padding_y * 2)) /
(opts.char_height_pixels + opts.char_padding_y_pixels);
size_t max_val = find_max(opts.chars, opts.num_chars);
self->max_ascii_val = max_val;
size_t *tbl = allocator.alloc((max_val + 1) * sizeof(size_t), allocator.ctx);
if (!tbl) {
return false;
}
if (!build_char_jump_tbl(tbl, opts.chars, opts.num_chars)) {
free(tbl);
return false;
}
self->char_jump_tbl = tbl;
return true;
}
void sbm_font_free(sbm_font *self) {
if (self && self->char_jump_tbl) {
self->allocator.free(self->char_jump_tbl, self->allocator.ctx);
self->char_jump_tbl = NULL;
}
}
void sbm_draw_char(sbm_font *self, char c, sgp_rect r) {
if (!self || (size_t)c > self->max_ascii_val) {
return;
}
size_t index = self->char_jump_tbl[(size_t)c];
uint32_t x_tiles = index % self->img_width_chars;
uint32_t y_tiles = index / self->img_width_chars;
uint32_t x_pixels = x_tiles * (self->opts.char_width_pixels +
self->opts.char_padding_x_pixels) +
self->opts.img_outer_padding_x;
uint32_t y_pixels = y_tiles * (self->opts.char_width_pixels +
self->opts.char_padding_x_pixels) +
self->opts.img_outer_padding_y;
sgp_draw_textured_rect(self->opts.img_channel, r,
(sgp_rect){
.x = x_pixels,
.y = y_pixels,
.w = self->opts.char_width_pixels,
.h = self->opts.char_height_pixels,
});
}
void sbm_draw_line(sbm_font *self, sbm_string_slice slice, float gap,
sgp_rect r) {
for (size_t i = 0; i < slice.len; i++) {
sbm_draw_char(self, slice.items[i],
(sgp_rect){
.x = r.x + (i * (r.w + gap)),
.y = r.y,
.w = r.w,
.h = r.h,
});
}
}
void sbm_draw_lines(sbm_font *self, sbm_string_slice slice, float gap_x,
float gap_y, sgp_rect r) {
float x = r.x;
float y = r.y;
for (size_t i = 0; i < slice.len; i++) {
switch (slice.items[i]) {
case '\n':
x = r.x;
y += r.h + gap_y;
continue;
case '\t':
x += (r.w + gap_x) * 2;
continue;
default:
break;
}
sbm_draw_char(self, slice.items[i],
(sgp_rect){
.x = x,
.y = y,
.w = r.w,
.h = r.h,
});
x += r.w + gap_x;
}
}
#endif
#endif