This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbakefont3.c
391 lines (287 loc) · 12.4 KB
/
bakefont3.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
bakefont3 - low-level C loader
Copyright © 2015 - 2017 Ben Golightly <golightly.ben@googlemail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "bakefont3.h"
#include <string.h> // memcpy
// NOTE - this implementation works for LITTLE ENDIAN HOSTS only
// (its quite trivial to fix but I don't have anything to test on)
// Decode a fixed point 2.6 number, rounded towards +/-ve infinity, but rounded
// towards zero if the fractional portion is below a given tolerance.
// Used to "round up" to the next integer, unless its "almost exact".
int BF3_DECODE_FP26_CEIL_impl(bf3_fp26 x, bf3_fp26 tolerance)
{
// TODO (PERF) this could be clever enough to operate on the integers
float fx = BF3_DECODE_FP26(x);
float ftolerance = BF3_DECODE_FP26(tolerance);
ftolerance = copysignf(ftolerance, fx);
float sgn_ceil = copysignf(0.5f, fx); // round towards +/-ve infinity
float result = (fx - ftolerance + sgn_ceil);
return (int) lround(result);
}
// As above, but rounds towards zero
int BF3_DECODE_FP26_FLOOR_impl(bf3_fp26 x, bf3_fp26 tolerance)
{
// TODO (PERF) this could be clever enough to operate on the integers
float fx = BF3_DECODE_FP26(x);
float ftolerance = BF3_DECODE_FP26(tolerance);
ftolerance = copysignf(ftolerance, fx);
float sgn_floor = copysignf(0.5f, fx); // round towards +/-ve infinity
float result = (fx + ftolerance - sgn_floor);
return (int) lround(result);
}
size_t bf3_header_peek(bf3_filelike *filelike)
{
// HEADER - 24 byte block
// b"BAKEFONTv3r0" # 0 | 12 | magic bytes, version 3 revision 0
// uint16(width) # 12 | 2 | texture atlas width
// uint16(height) # 14 | 2 | texture atlas height
// uint16(depth) # 16 | 2 | texture atlas depth (1, 3, 4)
// uint16(bytesize) # 18 | 2 | ...
// b'\0\0\0\0' # 20 | 4 | padding (realign to 8 bytes)
char buf[20];
uint16_t size;
size_t was_read = filelike->read(buf, filelike, 0, 20);
if (was_read < 20) { return 0; }
if (0 != memcmp(buf, "BAKEFONTv3r1", 12)) { return 0; }
memcpy(&size, buf + 18, 2);
return (size_t) size;
}
bool bf3_header_load(bf3_info *info, char *hdr, bf3_filelike *filelike, size_t header_size)
{
// HEADER - 24 byte block
// b"BAKEFONTv3r0" # 0 | 12 | magic bytes, version 3 revision 0
// uint16(width) # 12 | 2 | texture atlas width
// uint16(height) # 14 | 2 | texture atlas height
// uint16(depth) # 16 | 2 | texture atlas depth (1, 3, 4)
// uint16(bytesize) # 18 | 2 | ...
// b'\0\0\0\0' # 20 | 4 | padding (realign to 8 bytes)
int w, h, d, num_fonts, num_modes, num_tables;
size_t was_read = filelike->read(hdr, filelike, 0, header_size);
if (was_read < header_size) { goto fail; }
uint16_t v[3];
memcpy(v, hdr + 12, 6);
w = v[0]; h = v[1]; d = v[2];
// FONT TABLE HEADER - 6 bytes
// b"FONT" # 24 | 4 | debugging marker
// uint16(len(result.fonts)) # 28 | 2 | number of fonts
// b"\0\0" # 30 | 2 | padding (realign to 8 bytes)
// FONT RECORDS
// [48 bytes] * number_of_fonts
if (0 != memcmp(hdr + 24, "FONT", 4)) { goto fail; }
memcpy(v, hdr + 28, 2);
num_fonts = v[0];
// offset is relative to r = 32 + (48 * number of fonts)
// FONT MODE TABLE HEADER - 8 bytes
// "MODE" # r+0 | 4 | debugging marker
// uint16(len(result.modes)) # r+4 | 2 | number of modes
// b"\0\0" # r+6 | 2 | padding (realign to 8 bytes)
size_t offset = 32 + (48 * num_fonts);
if (0 != memcmp(hdr + offset, "MODE", 4)) { goto fail; }
memcpy(v, hdr + 4 + offset, 2);
num_modes = v[0];
//offset is relative to r = 24 + 8 + (48 * number of fonts)
// + 8 + (32 * number of modes)
// GLYPH TABLE HEADER - 8 bytes
// b"GTBL" # r+0 | 4 | debugging marker
// uint16(len(result.modeTable)) # r+4 | 2 | number of (modeID, charsetname) pairs
offset = 32 + (48 * num_fonts) + 8 + (32 * num_modes);
if (0 != memcmp(hdr + offset, "GTBL", 4)) { goto fail; }
memcpy(v, hdr + 4 + offset, 2);
num_tables = v[0];
bf3_info _info = {w, h, d, num_fonts, num_modes, num_tables};
memcpy(info, &_info, sizeof(bf3_info));
return true;
fail:
return false;
}
void bf3_font_get(bf3_font *font, char *buf, int index)
{
// 32+48n | 4 | attributes
// 36+48n | 44 | name for font with FontID=n (null terminated string)
size_t i = (size_t) index;
char horizontal = *(buf + 32 + 0 + (48 * i)) == 'H';
char vertical = *(buf + 32 + 1 + (48 * i)) == 'V';
const char *name = buf + 32 + 4 + (48 * i);
bf3_font _font = {index, horizontal, vertical, name};
memcpy(font, &_font, sizeof(bf3_font));
}
void bf3_mode_get(bf3_mode *mode, char *buf, int index)
{
uint16_t num_fonts;
memcpy(&num_fonts, buf + 28, 2);
size_t offset = 24 + 8 + (48 * num_fonts); // past font table
offset += 8; // past mode header
offset += (32 * index);
// o +0 | 2 | font ID
// o +2 | 1 | flag: 'A' if the font is antialiased, otherwise 'a'
// o +3 | 1 | RESERVED
// o +4 | 4 | font size - fixed point 26.6
// (divide the signed int32 by 64 to get original float)
// o +8 | 4 | lineheight aka linespacing - (fixed point 26.6)
// o+12 | 4 | underline position relative to baseline (fixed point 26.6)
// o+16 | 4 | underline vertical thickness, centered on position (fp26.6)
// o+20 | 12 | RESERVED
uint16_t font_id;
memcpy(&font_id, buf + offset + 0, 2);
char antialias = *(buf + offset + 2) == 'A';
uint32_t pts[4];
memcpy(pts, buf + offset + 4, 16);
bf3_fp26 size = { pts[0] };
bf3_fp26 lineheight = { pts[1] };
bf3_fp26 underline_position = { pts[2] };
bf3_fp26 underline_thickness = { pts[3] };
bf3_mode _mode = {index, font_id, antialias,
size, lineheight, underline_position, underline_thickness};
memcpy(mode, &_mode, sizeof(bf3_mode));
}
void bf3_table_get(bf3_table *table, char *buf, int index)
{
// 40 byte records
// 40n +0 | 2 | mode ID
// 40n +2 | 2 | RESERVED
// 40n +4 | 4 | absolute byte offset of glyph metrics data
// 40n +8 | 4 | byte size of glyph metrics data
// (subtract 4, divide by 36 to get number of entries)
// 40n+12 | 4 | absolute byte offset of glyph kerning data
// 40n+16 | 4 | byte size of glyph kerning data
// (subtract 4, divide by 16 to get number of entries)
// 40n+20 | 20 | charset name (string, null terminated)
uint16_t num_fonts, num_modes;
memcpy(&num_fonts, buf + 28, 2);
memcpy(&num_modes, buf + 32 + (48 * num_fonts) + 4, 2);
size_t offset = 32 + (48 * num_fonts) + 8 + (32 * num_modes) + 8;
offset += (40 * index);
uint16_t mode_id;
uint32_t metrics_offset, metrics_size, kerning_offset, kerning_size;
memcpy(&mode_id, buf + offset + 0, 2);
memcpy(&metrics_offset, buf + offset + 4, 4);
memcpy(&metrics_size, buf + offset + 8, 4);
memcpy(&kerning_offset, buf + offset + 12, 4);
memcpy(&kerning_size, buf + offset + 16, 4);
const char *name = buf + offset + 20;
bf3_table _table = {index, mode_id, metrics_offset, metrics_size,
kerning_offset, kerning_size, name};
memcpy(table, &_table, sizeof(bf3_table));
}
bool bf3_metrics_load(char *metrics, bf3_filelike *filelike, bf3_table *table)
{
if (table->metrics_size < 4) { goto fail; }
size_t was_read = filelike->read(metrics, filelike, table->metrics_offset, table->metrics_size);
if (was_read < table->metrics_size) { goto fail; }
if (0 != memcmp(metrics, "GSET", 4)) { goto fail; }
// patch over the SGET header with nmemb
uint32_t num = (table->metrics_size - 4) / 40;
memcpy(metrics, &num, 4);
return true;
fail:
return false;
}
bool bf3_kerning_load(char *kerning, bf3_filelike *filelike, bf3_table *table)
{
if (table->kerning_size < 4) { goto fail; }
size_t was_read = filelike->read(kerning, filelike,table->kerning_offset, table->kerning_size);
if (was_read < table->kerning_size) { goto fail; }
if (0 != memcmp(kerning, "KERN", 4)) { goto fail; }
// patch over the KERN header with nmemb
uint32_t num = (table->kerning_size - 4) / 16;
memcpy(kerning, &num, 4);
return true;
fail:
return false;
}
static void bf3_metric_decode(bf3_metric *metric, const char *buf)
{
// the metric structure is tightly packed so this works
memcpy(metric, buf, 40);
}
bool bf3_metric_get(bf3_metric *metric, const char *metrics, uint32_t codepoint)
{
// read nmemb we stashed earlier
uint32_t nmemb;
memcpy(&nmemb, metrics, 4);
// for a record, n, where is the offset to its codepoint relative to the
// start of the metrics buffer?
# define RECORD(n) (4 + (40*(n)))
// binary search for a matching codepoint
size_t start = 0;
size_t pos = nmemb / 2;
size_t end = nmemb;
while ((pos >= start) && (pos < end))
{
size_t offset = RECORD(pos);
uint32_t current;
memcpy(¤t, metrics + offset, 4);
int cmp = (codepoint == current) ? 0 : (codepoint > current) ? 1 : -1;
if (cmp == 0)
{
bf3_metric_decode(metric, metrics + offset);
return true;
}
else if (cmp < 0) { end = pos; }
else if (cmp > 0) { start = pos + 1; }
pos = start + ((end - start) / 2);
}
return false;
# undef RECORD
}
static void bf3_kpair_decode(bf3_kpair *kpair, const char *buf)
{
bf3_fp26 x, xf;
memcpy(&x, buf + 8, 4);
memcpy(&xf, buf + 12, 4);
kpair->x = BF3_DECODE_FP26_NEAREST(x);
kpair->xf = xf;
}
bool bf3_kpair_get(bf3_kpair *kpair, const char *kerning,
uint32_t codepoint_left, uint32_t codepoint_right)
{
// read nmemb we stashed earlier
uint32_t nmemb;
memcpy(&nmemb, kerning, 4);
// for a record, n, where is the offset to its codepoint relative to the
// start of the kerning buffer?
# define RECORD(n) (4 + (16*(n)))
// binary search for a matching (left, right) pair
size_t start = 0;
size_t pos = nmemb / 2;
size_t end = nmemb;
while ((pos >= start) && (pos < end))
{
size_t offset = RECORD(pos);
uint32_t current_left, current_right;
memcpy(¤t_left, kerning + offset, 4);
memcpy(¤t_right, kerning + offset + 4, 4);
int cmp0 = (codepoint_left == current_left) ? 0 :
(codepoint_left > current_left) ? 1 : -1;
int cmp1 = (codepoint_right == current_right) ? 0 :
(codepoint_right > current_right) ? 1 : -1;
if ((cmp0 == 0) && (cmp1 == 0))
{
bf3_kpair_decode(kpair, kerning + offset);
return true;
}
else if ((cmp0 == 0) && (cmp1 < 0)) { end = pos; }
else if ((cmp0 == 0) && (cmp1 > 0)) { start = pos + 1; }
else if (cmp0 < 0) { end = pos; }
else if (cmp0 > 0) { start = pos + 1; }
pos = start + ((end - start) / 2);
}
return false;
# undef RECORD
}