Debian Codegraph @

addons/font/bmfont.c

raw
1 #include "allegro5/allegro.h"
2 #include "allegro5/allegro_font.h"
3 #include <stdio.h>
4
5 #include "allegro5/internal/aintern_font.h"
6
7 #include "font.h"
8 #include "xml.h"
9
10 ALLEGRO_DEBUG_CHANNEL("font")
11
12 typedef struct {
13 int first;
14 int second;
15 int amount;
16 } BMFONT_KERNING;
17
18 typedef struct {
19 int page;
20 int x, y;
21 int width, height;
22 int xoffset, yoffset;
23 int xadvance;
24 int chnl;
25 int kerning_pairs;
26 BMFONT_KERNING *kerning;
27 } BMFONT_CHAR;
28
29 typedef struct BMFONT_RANGE BMFONT_RANGE;
30
31 struct BMFONT_RANGE {
32 int first;
33 int count;
34 BMFONT_CHAR **characters;
35 BMFONT_RANGE *next;
36 };
37
38 typedef struct {
39 int pages_count;
40 ALLEGRO_BITMAP **pages;
41 BMFONT_RANGE *range_first;
42 int base;
43 int line_height;
44 int flags;
45
46 int kerning_pairs;
47 BMFONT_KERNING *kerning;
48 } BMFONT_DATA;
49
50 typedef struct {
51 ALLEGRO_FONT *font;
52 ALLEGRO_USTR *tag;
53 ALLEGRO_USTR *attribute;
54 BMFONT_CHAR *c;
55 ALLEGRO_PATH *path;
56 } BMFONT_PARSER;
57
58 static void reallocate(BMFONT_RANGE *range) {
59 range->characters = al_realloc(range->characters,
60 range->count * sizeof *range->characters);
61 }
62
63 static void prepend_char(BMFONT_PARSER *parser, BMFONT_RANGE *range) {
64 range->first--;
65 range->count++;
66 reallocate(range);
67 memmove(range->characters + 1, range->characters,
68 (range->count - 1) * sizeof *range->characters);
69 range->characters[0] = parser->c;
70 }
71
72 static void append_char(BMFONT_PARSER *parser, BMFONT_RANGE *range) {
73 range->count++;
74 reallocate(range);
75 range->characters[range->count - 1] = parser->c;
76 }
77
78 static void combine_ranges(BMFONT_DATA *data,
79 BMFONT_RANGE *range, BMFONT_RANGE *range2) {
80 (void)data;
81 range->count += range2->count;
82 reallocate(range);
83 memmove(range->characters + range->count - range2->count,
84 range2->characters,
85 range2->count * sizeof *range->characters);
86 range->next = range2->next;
87 al_free(range2->characters);
88 al_free(range2);
89 }
90
91 static void insert_new_range(BMFONT_PARSER *parser, BMFONT_RANGE *prev,
92 int codepoint) {
93 BMFONT_RANGE *range = al_calloc(1, sizeof *range);
94 range->first = codepoint;
95 range->count = 1;
96 reallocate(range);
97 range->characters[0] = parser->c;
98 if (prev) {
99 range->next = prev->next;
100 prev->next = range;
101 }
102 else {
103 ALLEGRO_FONT *font = parser->font;
104 BMFONT_DATA *data = font->data;
105 range->next = data->range_first;
106 data->range_first = range;
107 }
108 }
109
110 /* Adding a new codepoint creates either a new range consisting of that
111 * single codepoint, appends the codepoint to the end of a range,
112 * prepends it to the beginning of a range, or merges two ranges.
113 */
114 static void add_codepoint(BMFONT_PARSER *parser, int codepoint) {
115 ALLEGRO_FONT *font = parser->font;
116 BMFONT_DATA *data = font->data;
117 BMFONT_RANGE *prev = NULL;
118 BMFONT_RANGE *range = data->range_first;
119 while (range) {
120 if (codepoint == range->first - 1) {
121 prepend_char(parser, range);
122 return;
123 }
124 if (codepoint < range->first) {
125 insert_new_range(parser, prev, codepoint);
126 return;
127 }
128 if (codepoint == range->first + range->count) {
129 append_char(parser, range);
130 BMFONT_RANGE *range2 = range->next;
131 if (range2 != NULL && codepoint == range2->first - 1) {
132 combine_ranges(data, range, range2);
133 }
134 return;
135 }
136 prev = range;
137 range = range->next;
138 }
139 insert_new_range(parser, prev, codepoint);
140 }
141
142 static BMFONT_CHAR *find_codepoint(BMFONT_DATA *data, int codepoint) {
143 BMFONT_RANGE *range = data->range_first;
144 while (range) {
145 if (codepoint >= range->first &&
146 codepoint < range->first + range->count) {
147 return range->characters[codepoint - range->first];
148 }
149 range = range->next;
150 }
151 return NULL;
152 }
153
154 static void add_page(BMFONT_PARSER *parser, char const *filename) {
155 ALLEGRO_FONT *font = parser->font;
156 BMFONT_DATA *data = font->data;
157 data->pages_count++;
158 data->pages = al_realloc(data->pages, data->pages_count *
159 sizeof *data->pages);
160 al_set_path_filename(parser->path, filename);
161 ALLEGRO_BITMAP *page = al_load_bitmap_flags(
162 al_path_cstr(parser->path, '/'), data->flags);
163 data->pages[data->pages_count - 1] = page;
164 }
165
166 static bool tag_is(BMFONT_PARSER *parser, char const *str) {
167 return strcmp(al_cstr(parser->tag), str) == 0;
168 }
169
170 static bool attribute_is(BMFONT_PARSER *parser, char const *str) {
171 return strcmp(al_cstr(parser->attribute), str) == 0;
172 }
173
174 static int get_int(char const *value) {
175 return strtol(value, NULL, 10);
176 }
177
178 static int xml_callback(XmlState state, char const *value, void *u)
179 {
180 BMFONT_PARSER *parser = u;
181 ALLEGRO_FONT *font = parser->font;
182 BMFONT_DATA *data = font->data;
183 if (state == ElementName) {
184 al_ustr_assign_cstr(parser->tag, value);
185 if (tag_is(parser, "char")) {
186 parser->c = al_calloc(1, sizeof *parser->c);
187 }
188 else if (tag_is(parser, "kerning")) {
189 data->kerning_pairs++;
190 data->kerning = al_realloc(data->kerning, data->kerning_pairs *
191 sizeof *data->kerning);
192 }
193 }
194 if (state == AttributeName) {
195 al_ustr_assign_cstr(parser->attribute, value);
196 }
197 if (state == AttributeValue) {
198 if (tag_is(parser, "char")) {
199 if (attribute_is(parser, "x")) parser->c->x = get_int(value);
200 else if (attribute_is(parser, "y")) parser->c->y = get_int(value);
201 else if (attribute_is(parser, "xoffset")) parser->c->xoffset = get_int(value);
202 else if (attribute_is(parser, "yoffset")) parser->c->yoffset = get_int(value);
203 else if (attribute_is(parser, "width")) parser->c->width = get_int(value);
204 else if (attribute_is(parser, "height")) parser->c->height = get_int(value);
205 else if (attribute_is(parser, "page")) parser->c->page = get_int(value);
206 else if (attribute_is(parser, "xadvance")) parser->c->xadvance = get_int(value);
207 else if (attribute_is(parser, "chnl")) parser->c->chnl = get_int(value);
208 else if (attribute_is(parser, "id")) {
209 add_codepoint(parser, get_int(value));
210 }
211 }
212 else if (tag_is(parser, "page")) {
213 if (attribute_is(parser, "file")) {
214 add_page(parser, value);
215 }
216 }
217 else if (tag_is(parser, "common")) {
218 if (attribute_is(parser, "lineHeight")) data->line_height = get_int(value);
219 else if (attribute_is(parser, "base")) data->base = get_int(value);
220 }
221 else if (tag_is(parser, "kerning")) {
222 BMFONT_KERNING *k = data->kerning + data->kerning_pairs - 1;
223 if (attribute_is(parser, "first")) k->first = get_int(value);
224 else if (attribute_is(parser, "second")) k->second = get_int(value);
225 else if (attribute_is(parser, "amount")) k->amount = get_int(value);
226 }
227 }
228 return 0;
229 }
230
231 static int font_height(const ALLEGRO_FONT *f) {
232 BMFONT_DATA *data = f->data;
233 return data->line_height;
234 }
235
236 static int font_ascent(const ALLEGRO_FONT *f) {
237 BMFONT_DATA *data = f->data;
238 return data->base;
239 }
240
241 static int font_descent(const ALLEGRO_FONT *f) {
242 BMFONT_DATA *data = f->data;
243 return data->line_height - data->base;
244 }
245
246 static int get_kerning(BMFONT_CHAR *prev, int c) {
247 if (!prev) return 0;
248 int i;
249 for (i = 0; i < prev->kerning_pairs; i++) {
250 if (prev->kerning[i].second == c) {
251 int a = prev->kerning[i].amount;
252 return a;
253 }
254 }
255 return 0;
256 }
257
258 static int each_character(const ALLEGRO_FONT *f, ALLEGRO_COLOR color,
259 const ALLEGRO_USTR *text, float x, float y, ALLEGRO_GLYPH *glyph,
260 int (*cb)(const ALLEGRO_FONT *f, ALLEGRO_COLOR color, int ch,
261 float x, float y, ALLEGRO_GLYPH *glyph)) {
262 BMFONT_DATA *data = f->data;
263 int pos = 0;
264 int advance = 0;
265 int prev = 0;
266 while (true) {
267 int c = al_ustr_get_next(text, &pos);
268 if (c < 0) break;
269 if (prev) {
270 BMFONT_CHAR *pc = find_codepoint(data, prev);
271 advance += get_kerning(pc, c);
272 }
273 advance += cb(f, color, c, x + advance, y, glyph);
274 prev = c;
275 }
276 return advance;
277 }
278
279 static int measure_char(const ALLEGRO_FONT *f, ALLEGRO_COLOR color,
280 int ch, float x, float y, ALLEGRO_GLYPH *glyph) {
281 (void)color;
282 (void)y;
283 BMFONT_DATA *data = f->data;
284 BMFONT_CHAR *c = find_codepoint(data, ch);
285 int advance = 0;
286 int xo = 0, yo = 0, w = 0, h = 0;
287 if (c) {
288 advance = c->xadvance;
289 xo = c->xoffset;
290 yo = c->yoffset;
291 w = c->width;
292 h = c->height;
293 }
294 else {
295 if (!f->fallback) return 0;
296 advance = al_get_glyph_width(f->fallback, ch);
297 al_get_glyph_dimensions(f->fallback, ch, &xo, &yo, &w, &h);
298 }
299 if (glyph) {
300 if (glyph->x == INT_MAX) glyph->x = xo;
301 if (yo < glyph->y) glyph->y = yo;
302 if (yo + h > glyph->h) glyph->h = yo + h;
303 if (x + xo + w > glyph->w) glyph->w = x + xo + w;
304 }
305 return advance;
306 }
307
308 static bool get_glyph_dimensions(const ALLEGRO_FONT *f,
309 int codepoint, int *bbx, int *bby, int *bbw, int *bbh) {
310 BMFONT_DATA *data = f->data;
311 BMFONT_CHAR *c = find_codepoint(data, codepoint);
312 if (!c) {
313 if (!f->fallback) return false;
314 return al_get_glyph_dimensions(f->fallback, codepoint,
315 bbx, bby, bbw, bbh);
316 }
317 *bbx = c->xoffset;
318 *bby = c->yoffset;
319 *bbw = c->width;
320 *bbh = c->height;
321 return true;
322 }
323
324 static int get_glyph_advance(const ALLEGRO_FONT *f,
325 int codepoint1, int codepoint2) {
326 BMFONT_DATA *data = f->data;
327 BMFONT_CHAR *c = find_codepoint(data, codepoint1);
328 int kerning = 0;
329 if (codepoint1 == ALLEGRO_NO_KERNING) {
330 return 0;
331 }
332
333 if (!c) {
334 if (!f->fallback) return 0;
335 return al_get_glyph_advance(f->fallback, codepoint1, codepoint2);
336 }
337
338 if (codepoint2 != ALLEGRO_NO_KERNING)
339 kerning = get_kerning(c, codepoint2);
340
341 return c->xadvance + kerning;
342 }
343
344 static bool get_glyph(const ALLEGRO_FONT *f, int prev_codepoint,
345 int codepoint, ALLEGRO_GLYPH *glyph) {
346 BMFONT_DATA *data = f->data;
347 BMFONT_CHAR *prev = find_codepoint(data, prev_codepoint);
348 BMFONT_CHAR *c = find_codepoint(data, codepoint);
349 if (c) {
350 glyph->bitmap = data->pages[c->page];
351 glyph->x = c->x;
352 glyph->y = c->y;
353 glyph->w = c->width;
354 glyph->h = c->height;
355 glyph->kerning = get_kerning(prev, codepoint);
356 glyph->offset_x = c->xoffset;
357 glyph->offset_y = c->yoffset;
358 glyph->advance = c->xadvance + glyph->kerning;
359 return true;
360 }
361 if (f->fallback) {
362 return al_get_glyph(f->fallback, prev_codepoint, codepoint,
363 glyph);
364 }
365 return false;
366 }
367
368 static int char_length(const ALLEGRO_FONT *f, int ch) {
369 ALLEGRO_COLOR color = {0, 0, 0, 0};
370 return measure_char(f, color, ch, 0, 0, NULL);
371 }
372
373 static int text_length(const ALLEGRO_FONT *f, const ALLEGRO_USTR *text) {
374 ALLEGRO_COLOR color = {0, 0, 0, 0};
375 return each_character(f, color, text, 0, 0, NULL, measure_char);
376 }
377
378 static int render_char(const ALLEGRO_FONT *f, ALLEGRO_COLOR color, int ch, float x, float y) {
379 BMFONT_DATA *data = f->data;
380 BMFONT_CHAR *c = find_codepoint(data, ch);
381 if (!c) {
382 if (f->fallback) return f->fallback->vtable->render_char(
383 f->fallback, color, ch, x, y);
384 return 0;
385 }
386 ALLEGRO_BITMAP *page = data->pages[c->page];
387 al_draw_tinted_bitmap_region(page, color, c->x, c->y, c->width, c->height,
388 x + c->xoffset, y + c->yoffset, 0);
389 return c->xadvance;
390 }
391
392 static int render_char_cb(const ALLEGRO_FONT *f, ALLEGRO_COLOR color,
393 int ch, float x, float y, ALLEGRO_GLYPH *glyph) {
394 (void)glyph;
395 return render_char(f, color, ch, x, y);
396 }
397
398 static int render(const ALLEGRO_FONT *f, ALLEGRO_COLOR color,
399 const ALLEGRO_USTR *text, float x, float y) {
400 return each_character(f, color, text, x, y, NULL, render_char_cb);
401 }
402
403 static void destroy_range(BMFONT_RANGE *range) {
404 int i;
405 for (i = 0; i < range->count; i++) {
406 BMFONT_CHAR *c = range->characters[i];
407 al_free(c->kerning);
408 al_free(c);
409 }
410 al_free(range);
411 }
412
413 static void destroy(ALLEGRO_FONT *f) {
414 BMFONT_DATA *data = f->data;
415 BMFONT_RANGE *range = data->range_first;
416 while (range) {
417 BMFONT_RANGE *next = range->next;
418 destroy_range(range);
419 range = next;
420 }
421
422 int i;
423 for (i = 0; i < data->pages_count; i++) {
424 al_destroy_bitmap(data->pages[i]);
425 }
426 al_free(data->pages);
427
428 al_free(data->kerning);
429 al_free(f);
430 }
431
432 static void get_text_dimensions(const ALLEGRO_FONT *f,
433 const ALLEGRO_USTR *text, int *bbx, int *bby, int *bbw, int *bbh) {
434 ALLEGRO_COLOR color = {0, 0, 0, 0};
435 ALLEGRO_GLYPH glyph;
436 glyph.x = INT_MAX;
437 glyph.y = INT_MAX;
438 glyph.w = INT_MIN;
439 glyph.h = INT_MIN;
440 each_character(f, color, text, 0, 0, &glyph, measure_char);
441 *bbx = glyph.x;
442 *bby = glyph.y;
443 *bbw = glyph.w - glyph.x;
444 *bbh = glyph.h - glyph.y;
445 }
446
447 static int get_font_ranges(ALLEGRO_FONT *f,
448 int ranges_count, int *ranges) {
449 BMFONT_DATA *data = f->data;
450 BMFONT_RANGE *range = data->range_first;
451 int i = 0;
452 while (range) {
453 if (i < ranges_count) {
454 ranges[i * 2 + 0] = range->first;
455 ranges[i * 2 + 1] = range->first + range->count - 1;
456 }
457 range = range->next;
458 i++;
459 }
460 return i;
461 }
462
463 static ALLEGRO_FONT_VTABLE _al_font_vtable_xml = {
464 font_height,
465 font_ascent,
466 font_descent,
467 char_length,
468 text_length,
469 render_char,
470 render,
471 destroy,
472 get_text_dimensions,
473 get_font_ranges,
474 get_glyph_dimensions,
475 get_glyph_advance,
476 get_glyph
477 };
478
479 ALLEGRO_FONT *_al_load_bmfont_xml(const char *filename, int size,
480 int font_flags)
481 {
482 (void)size;
483 ALLEGRO_FILE *f = al_fopen(filename, "r");
484 if (!f) {
485 ALLEGRO_DEBUG("Could not open %s.\n", filename);
486 return NULL;
487 }
488
489 BMFONT_DATA *data = al_calloc(1, sizeof *data);
490 BMFONT_PARSER _parser;
491 BMFONT_PARSER *parser = &_parser;
492 parser->tag = al_ustr_new("");
493 parser->attribute = al_ustr_new("");
494 parser->path = al_create_path(filename);
495 data->flags = font_flags;
496
497 ALLEGRO_FONT *font = al_calloc(1, sizeof *font);
498 font->vtable = &_al_font_vtable_xml;
499 font->data = data;
500 parser->font = font;
501
502 _al_xml_parse(f, xml_callback, parser);
503
504 int i;
505 for (i = 0; i < data->kerning_pairs; i++) {
506 BMFONT_KERNING *k = data->kerning + i;
507 BMFONT_CHAR *c = find_codepoint(data, k->first);
508 c->kerning_pairs++;
509 c->kerning = al_realloc(c->kerning, c->kerning_pairs *
510 sizeof *c->kerning);
511 c->kerning[c->kerning_pairs - 1] = *k;
512 }
513
514 al_ustr_free(parser->tag);
515 al_ustr_free(parser->attribute);
516 al_destroy_path(parser->path);
517
518 return font;
519 }