| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* |
| 12 |
* See readme.txt for copyright information. |
| 13 |
*/ |
| 14 |
|
| 15 |
#define ALLEGRO_INTERNAL_UNSTABLE |
| 16 |
|
| 17 |
#include <string.h> |
| 18 |
#include "allegro5/allegro.h" |
| 19 |
#include "allegro5/allegro_font.h" |
| 20 |
#include "allegro5/internal/aintern.h" |
| 21 |
#include "allegro5/internal/aintern_bitmap.h" |
| 22 |
#include "allegro5/internal/aintern_font.h" |
| 23 |
#include "allegro5/internal/aintern_exitfunc.h" |
| 24 |
#include "allegro5/internal/aintern_vector.h" |
| 25 |
|
| 26 |
#include "font.h" |
| 27 |
#include <ctype.h> |
| 28 |
|
| 29 |
ALLEGRO_DEBUG_CHANNEL("font") |
| 30 |
|
| 31 |
|
| 32 |
typedef struct |
| 33 |
{ |
| 34 |
ALLEGRO_USTR *extension; |
| 35 |
ALLEGRO_FONT *(*load_font)(char const *filename, int size, int flags); |
| 36 |
} FONT_HANDLER; |
| 37 |
|
| 38 |
|
| 39 |
/* globals */ |
| 40 |
static bool font_inited = false; |
| 41 |
static _AL_VECTOR font_handlers = _AL_VECTOR_INITIALIZER(FONT_HANDLER); |
| 42 |
|
| 43 |
|
| 44 |
/* al_font_404_character: |
| 45 |
* This is what we render missing glyphs as. |
| 46 |
*/ |
| 47 |
static int al_font_404_character = '^'; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
/* font_height: |
| 52 |
* (mono and color vtable entry) |
| 53 |
* Returns the height, in pixels of the font. |
| 54 |
*/ |
| 55 |
static int font_height(const ALLEGRO_FONT *f) |
| 56 |
{ |
| 57 |
ASSERT(f); |
| 58 |
return f->height; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static int font_ascent(const ALLEGRO_FONT *f) |
| 64 |
{ |
| 65 |
return font_height(f); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static int font_descent(const ALLEGRO_FONT *f) |
| 71 |
{ |
| 72 |
(void)f; |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
/* length: |
| 79 |
* (mono and color vtable entry) |
| 80 |
* Returns the length, in pixels, of a string as rendered in a font. |
| 81 |
*/ |
| 82 |
static int length(const ALLEGRO_FONT* f, const ALLEGRO_USTR *text) |
| 83 |
{ |
| 84 |
int ch = 0, w = 0; |
| 85 |
int pos = 0; |
| 86 |
ASSERT(f); |
| 87 |
|
| 88 |
while ((ch = al_ustr_get_next(text, &pos)) >= 0) { |
| 89 |
w += f->vtable->char_length(f, ch); |
| 90 |
} |
| 91 |
|
| 92 |
return w; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static void color_get_text_dimensions(ALLEGRO_FONT const *f, |
| 98 |
const ALLEGRO_USTR *text, |
| 99 |
int *bbx, int *bby, int *bbw, int *bbh) |
| 100 |
{ |
| 101 |
/* Dummy implementation - for A4-style bitmap fonts the bounding |
| 102 |
* box of text is its width and line-height. |
| 103 |
*/ |
| 104 |
int h = al_get_font_line_height(f); |
| 105 |
if (bbx) *bbx = 0; |
| 106 |
if (bby) *bby = 0; |
| 107 |
if (bbw) *bbw = length(f, text); |
| 108 |
if (bbh) *bbh = h; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static ALLEGRO_FONT_COLOR_DATA *_al_font_find_page( |
| 114 |
ALLEGRO_FONT_COLOR_DATA *cf, int ch) |
| 115 |
{ |
| 116 |
while (cf) { |
| 117 |
if (ch >= cf->begin && ch < cf->end) |
| 118 |
return cf; |
| 119 |
cf = cf->next; |
| 120 |
} |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/* _color_find_glyph: |
| 126 |
* Helper for color vtable entries, below. |
| 127 |
*/ |
| 128 |
static ALLEGRO_BITMAP* _al_font_color_find_glyph(const ALLEGRO_FONT* f, int ch) |
| 129 |
{ |
| 130 |
ALLEGRO_FONT_COLOR_DATA* cf = (ALLEGRO_FONT_COLOR_DATA*)(f->data); |
| 131 |
|
| 132 |
cf = _al_font_find_page(cf, ch); |
| 133 |
if (cf) { |
| 134 |
return cf->bitmaps[ch - cf->begin]; |
| 135 |
} |
| 136 |
|
| 137 |
/* if we don't find the character, then search for the missing |
| 138 |
glyph, but don't get stuck in a loop. */ |
| 139 |
if (ch != al_font_404_character && !f->fallback) |
| 140 |
return _al_font_color_find_glyph(f, al_font_404_character); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
/* color_char_length: |
| 147 |
* (color vtable entry) |
| 148 |
* Returns the length of a character, in pixels, as it would be rendered |
| 149 |
* in this font. |
| 150 |
*/ |
| 151 |
static int color_char_length(const ALLEGRO_FONT* f, int ch) |
| 152 |
{ |
| 153 |
ALLEGRO_BITMAP* g = _al_font_color_find_glyph(f, ch); |
| 154 |
if (g) |
| 155 |
return al_get_bitmap_width(g); |
| 156 |
if (f->fallback) |
| 157 |
return al_get_glyph_width(f->fallback, ch); |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
/* color_render_char: |
| 164 |
* (color vtable entry) |
| 165 |
* Renders a color character onto a bitmap, at the specified location, |
| 166 |
* using the specified color. |
| 167 |
* Returns the character width, in pixels. |
| 168 |
*/ |
| 169 |
static int color_render_char(const ALLEGRO_FONT* f, |
| 170 |
ALLEGRO_COLOR color, int ch, float x, |
| 171 |
float y) |
| 172 |
{ |
| 173 |
int w = 0; |
| 174 |
int h = f->vtable->font_height(f); |
| 175 |
ALLEGRO_BITMAP *g; |
| 176 |
|
| 177 |
g = _al_font_color_find_glyph(f, ch); |
| 178 |
if (g) { |
| 179 |
al_draw_tinted_bitmap(g, color, x, |
| 180 |
y + ((float)h - al_get_bitmap_height(g))/2.0f, 0); |
| 181 |
|
| 182 |
w = al_get_bitmap_width(g); |
| 183 |
} |
| 184 |
else if (f->fallback) { |
| 185 |
al_draw_glyph(f->fallback, color, x, y, ch); |
| 186 |
w = al_get_glyph_width(f->fallback, ch); |
| 187 |
} |
| 188 |
|
| 189 |
return w; |
| 190 |
} |
| 191 |
|
| 192 |
/* color_render: |
| 193 |
* (color vtable entry) |
| 194 |
* Renders a color font onto a bitmap, at the specified location, using |
| 195 |
* the specified color. |
| 196 |
*/ |
| 197 |
static int color_render(const ALLEGRO_FONT* f, ALLEGRO_COLOR color, |
| 198 |
const ALLEGRO_USTR *text, |
| 199 |
float x, float y) |
| 200 |
{ |
| 201 |
int pos = 0; |
| 202 |
int advance = 0; |
| 203 |
int32_t ch; |
| 204 |
bool held = al_is_bitmap_drawing_held(); |
| 205 |
|
| 206 |
al_hold_bitmap_drawing(true); |
| 207 |
while ((ch = al_ustr_get_next(text, &pos)) >= 0) { |
| 208 |
advance += f->vtable->render_char(f, color, ch, x + advance, y); |
| 209 |
} |
| 210 |
al_hold_bitmap_drawing(held); |
| 211 |
return advance; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
static bool color_get_glyph(const ALLEGRO_FONT *f, int prev_codepoint, int codepoint, ALLEGRO_GLYPH *glyph) |
| 216 |
{ |
| 217 |
ALLEGRO_BITMAP *g = _al_font_color_find_glyph(f, codepoint); |
| 218 |
if (g) { |
| 219 |
glyph->bitmap = g; |
| 220 |
glyph->x = 0; |
| 221 |
glyph->y = 0; |
| 222 |
glyph->w = al_get_bitmap_width(g); |
| 223 |
glyph->h = al_get_bitmap_height(g); |
| 224 |
glyph->kerning = 0; |
| 225 |
glyph->offset_x = 0; |
| 226 |
glyph->offset_y = 0; |
| 227 |
glyph->advance = glyph->w; |
| 228 |
return true; |
| 229 |
} |
| 230 |
if (f->fallback) { |
| 231 |
return f->fallback->vtable->get_glyph(f->fallback, prev_codepoint, codepoint, glyph); |
| 232 |
} |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
/* color_destroy: |
| 238 |
* (color vtable entry) |
| 239 |
* Destroys a color font. |
| 240 |
*/ |
| 241 |
static void color_destroy(ALLEGRO_FONT* f) |
| 242 |
{ |
| 243 |
ALLEGRO_FONT_COLOR_DATA* cf; |
| 244 |
ALLEGRO_BITMAP *glyphs = NULL; |
| 245 |
|
| 246 |
if (!f) |
| 247 |
return; |
| 248 |
|
| 249 |
cf = (ALLEGRO_FONT_COLOR_DATA*)(f->data); |
| 250 |
|
| 251 |
if (cf) |
| 252 |
glyphs = cf->glyphs; |
| 253 |
|
| 254 |
while (cf) { |
| 255 |
ALLEGRO_FONT_COLOR_DATA* next = cf->next; |
| 256 |
int i = 0; |
| 257 |
|
| 258 |
for (i = cf->begin; i < cf->end; i++) al_destroy_bitmap(cf->bitmaps[i - cf->begin]); |
| 259 |
/* Each range might point to the same bitmap. */ |
| 260 |
if (cf->glyphs != glyphs) { |
| 261 |
al_destroy_bitmap(cf->glyphs); |
| 262 |
cf->glyphs = NULL; |
| 263 |
} |
| 264 |
|
| 265 |
if (!next && cf->glyphs) |
| 266 |
al_destroy_bitmap(cf->glyphs); |
| 267 |
|
| 268 |
al_free(cf->bitmaps); |
| 269 |
al_free(cf); |
| 270 |
|
| 271 |
cf = next; |
| 272 |
} |
| 273 |
|
| 274 |
al_free(f); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
static int color_get_font_ranges(ALLEGRO_FONT *font, int ranges_count, |
| 279 |
int *ranges) |
| 280 |
{ |
| 281 |
ALLEGRO_FONT_COLOR_DATA *cf = font->data; |
| 282 |
int i = 0; |
| 283 |
while (cf) { |
| 284 |
if (i < ranges_count) { |
| 285 |
ranges[i * 2 + 0] = cf->begin; |
| 286 |
ranges[i * 2 + 1] = cf->end - 1; |
| 287 |
} |
| 288 |
i++; |
| 289 |
cf = cf->next; |
| 290 |
} |
| 291 |
return i; |
| 292 |
} |
| 293 |
|
| 294 |
static bool color_get_glyph_dimensions(ALLEGRO_FONT const *f, |
| 295 |
int codepoint, int *bbx, int *bby, int *bbw, int *bbh) |
| 296 |
{ |
| 297 |
ALLEGRO_BITMAP *glyph = _al_font_color_find_glyph(f, codepoint); |
| 298 |
if(!glyph) { |
| 299 |
if (f->fallback) { |
| 300 |
return al_get_glyph_dimensions(f->fallback, codepoint, |
| 301 |
bbx, bby, bbw, bbh); |
| 302 |
} |
| 303 |
return false; |
| 304 |
} |
| 305 |
if (bbx) *bbx = 0; |
| 306 |
if (bby) *bby = 0; |
| 307 |
if (bbw) *bbw = glyph ? al_get_bitmap_width(glyph) : 0; |
| 308 |
if (bbh) *bbh = al_get_font_line_height(f); |
| 309 |
return true; |
| 310 |
} |
| 311 |
|
| 312 |
static int color_get_glyph_advance(ALLEGRO_FONT const *f, |
| 313 |
int codepoint1, int codepoint2) |
| 314 |
{ |
| 315 |
(void) codepoint2; |
| 316 |
|
| 317 |
/* Handle special case to simplify use in a loop. */ |
| 318 |
if (codepoint1 == ALLEGRO_NO_KERNING) { |
| 319 |
return 0; |
| 320 |
} |
| 321 |
|
| 322 |
/* For other cases, bitmap fonts don't use any kerning, so just use the |
| 323 |
* width of codepoint1. */ |
| 324 |
|
| 325 |
return color_char_length(f, codepoint1); |
| 326 |
} |
| 327 |
|
| 328 |
/******** |
| 329 |
* vtable declarations |
| 330 |
********/ |
| 331 |
|
| 332 |
ALLEGRO_FONT_VTABLE _al_font_vtable_color = { |
| 333 |
font_height, |
| 334 |
font_ascent, |
| 335 |
font_descent, |
| 336 |
color_char_length, |
| 337 |
length, |
| 338 |
color_render_char, |
| 339 |
color_render, |
| 340 |
color_destroy, |
| 341 |
color_get_text_dimensions, |
| 342 |
color_get_font_ranges, |
| 343 |
color_get_glyph_dimensions, |
| 344 |
color_get_glyph_advance, |
| 345 |
color_get_glyph |
| 346 |
}; |
| 347 |
|
| 348 |
|
| 349 |
static void font_shutdown(void) |
| 350 |
{ |
| 351 |
if (!font_inited) |
| 352 |
return; |
| 353 |
|
| 354 |
while (!_al_vector_is_empty(&font_handlers)) { |
| 355 |
FONT_HANDLER *h = _al_vector_ref_back(&font_handlers); |
| 356 |
al_ustr_free(h->extension); |
| 357 |
_al_vector_delete_at(&font_handlers, _al_vector_size(&font_handlers)-1); |
| 358 |
} |
| 359 |
_al_vector_free(&font_handlers); |
| 360 |
|
| 361 |
font_inited = false; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
/* Function: al_init_font_addon |
| 366 |
*/ |
| 367 |
bool al_init_font_addon(void) |
| 368 |
{ |
| 369 |
if (font_inited) { |
| 370 |
ALLEGRO_WARN("Font addon already initialised.\n"); |
| 371 |
return true; |
| 372 |
} |
| 373 |
|
| 374 |
al_register_font_loader(".bmp", _al_load_bitmap_font); |
| 375 |
al_register_font_loader(".jpg", _al_load_bitmap_font); |
| 376 |
al_register_font_loader(".pcx", _al_load_bitmap_font); |
| 377 |
al_register_font_loader(".png", _al_load_bitmap_font); |
| 378 |
al_register_font_loader(".tga", _al_load_bitmap_font); |
| 379 |
|
| 380 |
al_register_font_loader(".xml", _al_load_bmfont_xml); |
| 381 |
al_register_font_loader(".fnt", _al_load_bmfont_xml); |
| 382 |
|
| 383 |
_al_add_exit_func(font_shutdown, "font_shutdown"); |
| 384 |
|
| 385 |
font_inited = true; |
| 386 |
return font_inited; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
/* Function: al_is_font_addon_initialized |
| 392 |
*/ |
| 393 |
bool al_is_font_addon_initialized(void) |
| 394 |
{ |
| 395 |
return font_inited; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
/* Function: al_shutdown_font_addon |
| 401 |
*/ |
| 402 |
void al_shutdown_font_addon(void) |
| 403 |
{ |
| 404 |
font_shutdown(); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
static FONT_HANDLER *find_extension(char const *extension) |
| 409 |
{ |
| 410 |
int i; |
| 411 |
/* Go backwards so a handler registered later for the same extension |
| 412 |
* has precedence. |
| 413 |
*/ |
| 414 |
for (i = _al_vector_size(&font_handlers) - 1; i >= 0 ; i--) { |
| 415 |
FONT_HANDLER *handler = _al_vector_ref(&font_handlers, i); |
| 416 |
if (0 == _al_stricmp(al_cstr(handler->extension), extension)) |
| 417 |
return handler; |
| 418 |
} |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
/* Function: al_register_font_loader |
| 425 |
*/ |
| 426 |
bool al_register_font_loader(char const *extension, |
| 427 |
ALLEGRO_FONT *(*load_font)(char const *filename, int size, int flags)) |
| 428 |
{ |
| 429 |
FONT_HANDLER *handler = find_extension(extension); |
| 430 |
if (!handler) { |
| 431 |
if (!load_font) |
| 432 |
return false; /* Nothing to remove. */ |
| 433 |
handler = _al_vector_alloc_back(&font_handlers); |
| 434 |
handler->extension = al_ustr_new(extension); |
| 435 |
} |
| 436 |
else { |
| 437 |
if (!load_font) { |
| 438 |
al_ustr_free(handler->extension); |
| 439 |
return _al_vector_find_and_delete(&font_handlers, handler); |
| 440 |
} |
| 441 |
} |
| 442 |
handler->load_font = load_font; |
| 443 |
return true; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
/* Function: al_load_font |
| 449 |
*/ |
| 450 |
ALLEGRO_FONT *al_load_font(char const *filename, int size, int flags) |
| 451 |
{ |
| 452 |
int i; |
| 453 |
const char *ext; |
| 454 |
FONT_HANDLER *handler; |
| 455 |
|
| 456 |
ASSERT(filename); |
| 457 |
|
| 458 |
if (!font_inited) { |
| 459 |
ALLEGRO_ERROR("Font addon not initialised.\n"); |
| 460 |
return NULL; |
| 461 |
} |
| 462 |
|
| 463 |
ext = strrchr(filename, '.'); |
| 464 |
if (!ext) { |
| 465 |
ALLEGRO_ERROR("Unable to determine filetype: '%s'\n", filename); |
| 466 |
return NULL; |
| 467 |
} |
| 468 |
handler = find_extension(ext); |
| 469 |
if (handler) |
| 470 |
return handler->load_font(filename, size, flags); |
| 471 |
|
| 472 |
/* No handler for the extension was registered - try to load with |
| 473 |
* all registered font_handlers and see if one works. So if the user |
| 474 |
* does: |
| 475 |
* |
| 476 |
* al_init_font_addon() |
| 477 |
* al_init_ttf_addon() |
| 478 |
* |
| 479 |
* This will first try to load an unknown (let's say Type1) font file |
| 480 |
* with Freetype (and load it successfully in this case), then try |
| 481 |
* to load it as a bitmap font. |
| 482 |
*/ |
| 483 |
for (i = _al_vector_size(&font_handlers) - 1; i >= 0 ; i--) { |
| 484 |
FONT_HANDLER *handler = _al_vector_ref(&font_handlers, i); |
| 485 |
ALLEGRO_FONT *try = handler->load_font(filename, size, flags); |
| 486 |
if (try) |
| 487 |
return try; |
| 488 |
} |
| 489 |
|
| 490 |
return NULL; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
/* Function: al_get_allegro_font_version |
| 496 |
*/ |
| 497 |
uint32_t al_get_allegro_font_version(void) |
| 498 |
{ |
| 499 |
return ALLEGRO_VERSION_INT; |
| 500 |
} |
| 501 |
|
| 502 |
/* vim: set sts=4 sw=4 et: */ |
| 503 |
|