| 1 |
#define ALLEGRO_INTERNAL_UNSTABLE |
| 2 |
|
| 3 |
#include "allegro5/allegro.h" |
| 4 |
#ifdef ALLEGRO_CFG_OPENGL |
| 5 |
#include "allegro5/allegro_opengl.h" |
| 6 |
#endif |
| 7 |
#include "allegro5/internal/aintern.h" |
| 8 |
#include "allegro5/internal/aintern_vector.h" |
| 9 |
|
| 10 |
#include "allegro5/allegro_ttf.h" |
| 11 |
#include "allegro5/internal/aintern_font.h" |
| 12 |
#include "allegro5/internal/aintern_ttf_cfg.h" |
| 13 |
#include "allegro5/internal/aintern_dtor.h" |
| 14 |
#include "allegro5/internal/aintern_system.h" |
| 15 |
#include "allegro5/internal/aintern_exitfunc.h" |
| 16 |
|
| 17 |
#include <ft2build.h> |
| 18 |
#include FT_FREETYPE_H |
| 19 |
#include FT_TRUETYPE_TABLES_H |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
ALLEGRO_DEBUG_CHANNEL("font") |
| 24 |
|
| 25 |
|
| 26 |
/* Some low-end drivers enable automatic S3TC compression, which |
| 27 |
* requires glTexSubImage2D to only work on multiples of aligned |
| 28 |
* 4x4 pixel blocks with some buggy OpenGL drivers. |
| 29 |
* There's not much we can do about that in general - if the user |
| 30 |
* locks a portion of a bitmap not conformin to this it will fail |
| 31 |
* with such a driver. |
| 32 |
* |
| 33 |
* However in many programs this is no problem at all safe for rendering |
| 34 |
* glyphs and simply aligning to 4 pixels here fixes it. |
| 35 |
*/ |
| 36 |
#define ALIGN_TO_4_PIXEL |
| 37 |
|
| 38 |
|
| 39 |
#define RANGE_SIZE 128 |
| 40 |
|
| 41 |
|
| 42 |
typedef struct REGION |
| 43 |
{ |
| 44 |
short x; |
| 45 |
short y; |
| 46 |
short w; |
| 47 |
short h; |
| 48 |
} REGION; |
| 49 |
|
| 50 |
|
| 51 |
typedef struct ALLEGRO_TTF_GLYPH_DATA |
| 52 |
{ |
| 53 |
ALLEGRO_BITMAP *page_bitmap; |
| 54 |
REGION region; |
| 55 |
short offset_x; |
| 56 |
short offset_y; |
| 57 |
short advance; |
| 58 |
} ALLEGRO_TTF_GLYPH_DATA; |
| 59 |
|
| 60 |
|
| 61 |
typedef struct ALLEGRO_TTF_GLYPH_RANGE |
| 62 |
{ |
| 63 |
int32_t range_start; |
| 64 |
ALLEGRO_TTF_GLYPH_DATA *glyphs; /* [RANGE_SIZE] */ |
| 65 |
} ALLEGRO_TTF_GLYPH_RANGE; |
| 66 |
|
| 67 |
|
| 68 |
typedef struct ALLEGRO_TTF_FONT_DATA |
| 69 |
{ |
| 70 |
FT_Face face; |
| 71 |
int flags; |
| 72 |
_AL_VECTOR glyph_ranges; /* sorted array of of ALLEGRO_TTF_GLYPH_RANGE */ |
| 73 |
|
| 74 |
_AL_VECTOR page_bitmaps; /* of ALLEGRO_BITMAP pointers */ |
| 75 |
int page_pos_x; |
| 76 |
int page_pos_y; |
| 77 |
int page_line_height; |
| 78 |
ALLEGRO_LOCKED_REGION *page_lr; |
| 79 |
|
| 80 |
FT_StreamRec stream; |
| 81 |
ALLEGRO_FILE *file; |
| 82 |
unsigned long base_offset; |
| 83 |
unsigned long offset; |
| 84 |
|
| 85 |
int bitmap_format; |
| 86 |
int bitmap_flags; |
| 87 |
|
| 88 |
int min_page_size; |
| 89 |
int max_page_size; |
| 90 |
|
| 91 |
bool skip_cache_misses; |
| 92 |
} ALLEGRO_TTF_FONT_DATA; |
| 93 |
|
| 94 |
|
| 95 |
/* globals */ |
| 96 |
static bool ttf_inited; |
| 97 |
static FT_Library ft; |
| 98 |
static ALLEGRO_FONT_VTABLE vt; |
| 99 |
|
| 100 |
|
| 101 |
static INLINE int align4(int x) |
| 102 |
{ |
| 103 |
#ifdef ALIGN_TO_4_PIXEL |
| 104 |
return (x + 3) & ~3; |
| 105 |
#else |
| 106 |
return x; |
| 107 |
#endif |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/* Returns false if the glyph is invalid. |
| 112 |
*/ |
| 113 |
static bool get_glyph(ALLEGRO_TTF_FONT_DATA *data, |
| 114 |
int ft_index, ALLEGRO_TTF_GLYPH_DATA **glyph) |
| 115 |
{ |
| 116 |
ALLEGRO_TTF_GLYPH_RANGE *range; |
| 117 |
int32_t range_start; |
| 118 |
int lo, hi, mid; |
| 119 |
ASSERT(glyph); |
| 120 |
|
| 121 |
range_start = ft_index - (ft_index % RANGE_SIZE); |
| 122 |
|
| 123 |
/* Binary search for the range. */ |
| 124 |
lo = 0; |
| 125 |
hi = _al_vector_size(&data->glyph_ranges); |
| 126 |
mid = (hi + lo)/2; |
| 127 |
range = NULL; |
| 128 |
|
| 129 |
while (lo < hi) { |
| 130 |
ALLEGRO_TTF_GLYPH_RANGE *r = _al_vector_ref(&data->glyph_ranges, mid); |
| 131 |
if (r->range_start == range_start) { |
| 132 |
range = r; |
| 133 |
break; |
| 134 |
} |
| 135 |
else if (r->range_start < range_start) { |
| 136 |
lo = mid + 1; |
| 137 |
} |
| 138 |
else { |
| 139 |
hi = mid; |
| 140 |
} |
| 141 |
mid = (hi + lo)/2; |
| 142 |
} |
| 143 |
|
| 144 |
if (!range) { |
| 145 |
range = _al_vector_alloc_mid(&data->glyph_ranges, mid); |
| 146 |
range->range_start = range_start; |
| 147 |
range->glyphs = al_calloc(RANGE_SIZE, sizeof(ALLEGRO_TTF_GLYPH_DATA)); |
| 148 |
} |
| 149 |
|
| 150 |
*glyph = &range->glyphs[ft_index - range_start]; |
| 151 |
|
| 152 |
/* If we're skipping cache misses and it isn't already cached, return it as invalid. */ |
| 153 |
if (data->skip_cache_misses && !(*glyph)->page_bitmap && (*glyph)->region.x >= 0) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
return ft_index != 0; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
static void unlock_current_page(ALLEGRO_TTF_FONT_DATA *data) |
| 162 |
{ |
| 163 |
if (data->page_lr) { |
| 164 |
ALLEGRO_BITMAP **back = _al_vector_ref_back(&data->page_bitmaps); |
| 165 |
ASSERT(al_is_bitmap_locked(*back)); |
| 166 |
al_unlock_bitmap(*back); |
| 167 |
data->page_lr = NULL; |
| 168 |
ALLEGRO_DEBUG("Unlocking page: %p\n", *back); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
static ALLEGRO_BITMAP *push_new_page(ALLEGRO_TTF_FONT_DATA *data, int glyph_size) |
| 174 |
{ |
| 175 |
ALLEGRO_BITMAP **back; |
| 176 |
ALLEGRO_BITMAP *page; |
| 177 |
ALLEGRO_STATE state; |
| 178 |
int page_size = 1; |
| 179 |
/* 16 seems to work well. A particular problem are fixed width fonts which |
| 180 |
* take an inordinate amount of space. */ |
| 181 |
while (page_size < 16 * glyph_size) { |
| 182 |
page_size *= 2; |
| 183 |
} |
| 184 |
if (page_size < data->min_page_size) { |
| 185 |
page_size = data->min_page_size; |
| 186 |
} |
| 187 |
if (page_size > data->max_page_size) { |
| 188 |
page_size = data->max_page_size; |
| 189 |
} |
| 190 |
if (glyph_size > page_size) { |
| 191 |
ALLEGRO_ERROR("Unable create new page, glyph too large: %d > %d\n", |
| 192 |
glyph_size, page_size); |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
|
| 196 |
unlock_current_page(data); |
| 197 |
|
| 198 |
/* The bitmap will be destroyed when the parent font is destroyed so |
| 199 |
* it is not safe to register a destructor for it. |
| 200 |
*/ |
| 201 |
_al_push_destructor_owner(); |
| 202 |
al_store_state(&state, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS); |
| 203 |
al_set_new_bitmap_format(data->bitmap_format); |
| 204 |
al_set_new_bitmap_flags(data->bitmap_flags); |
| 205 |
page = al_create_bitmap(page_size, page_size); |
| 206 |
al_restore_state(&state); |
| 207 |
_al_pop_destructor_owner(); |
| 208 |
|
| 209 |
if (page) { |
| 210 |
back = _al_vector_alloc_back(&data->page_bitmaps); |
| 211 |
*back = page; |
| 212 |
|
| 213 |
data->page_pos_x = 0; |
| 214 |
data->page_pos_y = 0; |
| 215 |
data->page_line_height = 0; |
| 216 |
} |
| 217 |
|
| 218 |
return page; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
static unsigned char *alloc_glyph_region(ALLEGRO_TTF_FONT_DATA *data, |
| 223 |
int ft_index, int w, int h, bool new, ALLEGRO_TTF_GLYPH_DATA *glyph, |
| 224 |
bool lock_whole_page) |
| 225 |
{ |
| 226 |
ALLEGRO_BITMAP *page; |
| 227 |
int w4 = align4(w); |
| 228 |
int h4 = align4(h); |
| 229 |
int glyph_size = w4 > h4 ? w4 : h4; |
| 230 |
bool lock = false; |
| 231 |
|
| 232 |
if (_al_vector_is_empty(&data->page_bitmaps) || new) { |
| 233 |
page = push_new_page(data, glyph_size); |
| 234 |
if (!page) { |
| 235 |
ALLEGRO_ERROR("Failed to create a new page for glyph %d.\n", ft_index); |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
} |
| 239 |
else { |
| 240 |
ALLEGRO_BITMAP **back = _al_vector_ref_back(&data->page_bitmaps); |
| 241 |
page = *back; |
| 242 |
} |
| 243 |
|
| 244 |
ALLEGRO_DEBUG("Glyph %d: %dx%d (%dx%d)%s\n", |
| 245 |
ft_index, w, h, w4, h4, new ? " new" : ""); |
| 246 |
|
| 247 |
if (data->page_pos_x + w4 > al_get_bitmap_width(page)) { |
| 248 |
data->page_pos_y += data->page_line_height; |
| 249 |
data->page_pos_y = align4(data->page_pos_y); |
| 250 |
data->page_pos_x = 0; |
| 251 |
data->page_line_height = 0; |
| 252 |
} |
| 253 |
|
| 254 |
if (data->page_pos_y + h4 > al_get_bitmap_height(page)) { |
| 255 |
return alloc_glyph_region(data, ft_index, w, h, true, glyph, lock_whole_page); |
| 256 |
} |
| 257 |
|
| 258 |
glyph->page_bitmap = page; |
| 259 |
glyph->region.x = data->page_pos_x; |
| 260 |
glyph->region.y = data->page_pos_y; |
| 261 |
glyph->region.w = w; |
| 262 |
glyph->region.h = h; |
| 263 |
|
| 264 |
data->page_pos_x = align4(data->page_pos_x + w4); |
| 265 |
if (h > data->page_line_height) { |
| 266 |
data->page_line_height = h4; |
| 267 |
} |
| 268 |
|
| 269 |
REGION lock_rect; |
| 270 |
if (lock_whole_page) { |
| 271 |
lock_rect.x = 0; |
| 272 |
lock_rect.y = 0; |
| 273 |
lock_rect.w = al_get_bitmap_width(page); |
| 274 |
lock_rect.h = al_get_bitmap_height(page); |
| 275 |
if (!data->page_lr) { |
| 276 |
lock = true; |
| 277 |
ALLEGRO_DEBUG("Locking whole page: %p\n", page); |
| 278 |
} |
| 279 |
} |
| 280 |
else { |
| 281 |
/* Unlock just in case... */ |
| 282 |
unlock_current_page(data); |
| 283 |
lock_rect.x = glyph->region.x; |
| 284 |
lock_rect.y = glyph->region.y; |
| 285 |
lock_rect.w = w4; |
| 286 |
lock_rect.h = h4; |
| 287 |
lock = true; |
| 288 |
ALLEGRO_DEBUG("Locking glyph region: %p %d %d %d %d\n", page, |
| 289 |
lock_rect.x, lock_rect.y, lock_rect.w, lock_rect.h); |
| 290 |
} |
| 291 |
|
| 292 |
if (lock) { |
| 293 |
unsigned char *ptr; |
| 294 |
int i; |
| 295 |
|
| 296 |
data->page_lr = al_lock_bitmap_region(page, |
| 297 |
lock_rect.x, lock_rect.y, lock_rect.w, lock_rect.h, |
| 298 |
ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY); |
| 299 |
|
| 300 |
if (!data->page_lr) { |
| 301 |
ALLEGRO_ERROR("Failed to lock page.\n"); |
| 302 |
return NULL; |
| 303 |
} |
| 304 |
|
| 305 |
if (data->flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) { |
| 306 |
/* Fill in border with "transparent white" |
| 307 |
* to make anti-aliasing work well when rotation |
| 308 |
* and/or scaling transforms are in effect |
| 309 |
* FIXME We could clear just the border |
| 310 |
*/ |
| 311 |
for (i = 0; i < lock_rect.h; i++) { |
| 312 |
ptr = (unsigned char *)(data->page_lr->data) + (i * data->page_lr->pitch); |
| 313 |
int j; |
| 314 |
for (j = 0; j < lock_rect.w; ++j) { |
| 315 |
*ptr++ = 255; |
| 316 |
*ptr++ = 255; |
| 317 |
*ptr++ = 255; |
| 318 |
*ptr++ = 0; |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
else { |
| 323 |
/* Clear the data so we don't get garbage when using filtering |
| 324 |
* FIXME We could clear just the border but I'm not convinced that |
| 325 |
* would be faster (yet) |
| 326 |
*/ |
| 327 |
for (i = 0; i < lock_rect.h; i++) { |
| 328 |
ptr = (unsigned char *)(data->page_lr->data) + (i * data->page_lr->pitch); |
| 329 |
memset(ptr, 0, lock_rect.w * 4); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
ASSERT(data->page_lr); |
| 335 |
|
| 336 |
/* Copy a displaced pointer for the glyph. */ |
| 337 |
return (unsigned char *)data->page_lr->data |
| 338 |
+ ((glyph->region.y + 2) - lock_rect.y) * data->page_lr->pitch |
| 339 |
+ ((glyph->region.x + 2) - lock_rect.x) * sizeof(int32_t); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
static void copy_glyph_mono(ALLEGRO_TTF_FONT_DATA *font_data, FT_Face face, |
| 344 |
unsigned char *glyph_data) |
| 345 |
{ |
| 346 |
int pitch = font_data->page_lr->pitch; |
| 347 |
int x, y; |
| 348 |
|
| 349 |
for (y = 0; y < (int)face->glyph->bitmap.rows; y++) { |
| 350 |
unsigned char const *ptr = face->glyph->bitmap.buffer + face->glyph->bitmap.pitch * y; |
| 351 |
unsigned char *dptr = glyph_data + pitch * y; |
| 352 |
int bit = 0; |
| 353 |
|
| 354 |
if (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) { |
| 355 |
for (x = 0; x < (int)face->glyph->bitmap.width; x++) { |
| 356 |
unsigned char set = ((ptr[3] >> (7-bit)) & 1) ? 255 : 0; |
| 357 |
*dptr++ = set; |
| 358 |
*dptr++ = set; |
| 359 |
*dptr++ = set; |
| 360 |
*dptr++ = set; |
| 361 |
ptr += 4; |
| 362 |
} |
| 363 |
} |
| 364 |
else if (font_data->flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) { |
| 365 |
/* FIXME We could just set the alpha byte since the |
| 366 |
* region was cleared above when allocated |
| 367 |
*/ |
| 368 |
for (x = 0; x < (int)face->glyph->bitmap.width; x++) { |
| 369 |
unsigned char set = ((*ptr >> (7-bit)) & 1) ? 255 : 0; |
| 370 |
*dptr++ = 255; |
| 371 |
*dptr++ = 255; |
| 372 |
*dptr++ = 255; |
| 373 |
*dptr++ = set; |
| 374 |
bit = (bit + 1) & 7; |
| 375 |
if (bit == 0) { |
| 376 |
ptr++; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
else { |
| 381 |
for (x = 0; x < (int)face->glyph->bitmap.width; x++) { |
| 382 |
unsigned char set = ((*ptr >> (7-bit)) & 1) ? 255 : 0; |
| 383 |
*dptr++ = set; |
| 384 |
*dptr++ = set; |
| 385 |
*dptr++ = set; |
| 386 |
*dptr++ = set; |
| 387 |
bit = (bit + 1) & 7; |
| 388 |
if (bit == 0) { |
| 389 |
ptr++; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
static void copy_glyph_color(ALLEGRO_TTF_FONT_DATA *font_data, FT_Face face, |
| 398 |
unsigned char *glyph_data) |
| 399 |
{ |
| 400 |
int pitch = font_data->page_lr->pitch; |
| 401 |
int x, y; |
| 402 |
|
| 403 |
for (y = 0; y < (int)face->glyph->bitmap.rows; y++) { |
| 404 |
unsigned char const *ptr = face->glyph->bitmap.buffer + face->glyph->bitmap.pitch * y; |
| 405 |
unsigned char *dptr = glyph_data + pitch * y; |
| 406 |
|
| 407 |
if (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) { |
| 408 |
for (x = 0; x < (int)face->glyph->bitmap.width; x++) { |
| 409 |
*dptr++ = ptr[2]; |
| 410 |
*dptr++ = ptr[1]; |
| 411 |
*dptr++ = ptr[0]; |
| 412 |
*dptr++ = ptr[3]; |
| 413 |
ptr += 4; |
| 414 |
} |
| 415 |
} |
| 416 |
else if (font_data->flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) { |
| 417 |
/* FIXME We could just set the alpha byte since the |
| 418 |
* region was cleared above when allocated |
| 419 |
*/ |
| 420 |
for (x = 0; x < (int)face->glyph->bitmap.width; x++) { |
| 421 |
unsigned char c = *ptr; |
| 422 |
*dptr++ = 255; |
| 423 |
*dptr++ = 255; |
| 424 |
*dptr++ = 255; |
| 425 |
*dptr++ = c; |
| 426 |
ptr++; |
| 427 |
} |
| 428 |
} |
| 429 |
else { |
| 430 |
for (x = 0; x < (int)face->glyph->bitmap.width; x++) { |
| 431 |
unsigned char c = *ptr; |
| 432 |
*dptr++ = c; |
| 433 |
*dptr++ = c; |
| 434 |
*dptr++ = c; |
| 435 |
*dptr++ = c; |
| 436 |
ptr++; |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
/* NOTE: this function may disable the bitmap hold drawing state |
| 444 |
* and leave the current page bitmap locked. |
| 445 |
* |
| 446 |
* NOTE: We have previously tried to be more clever about caching multiple |
| 447 |
* glyphs during incidental cache misses, but found that approach to be slower. |
| 448 |
*/ |
| 449 |
static void cache_glyph(ALLEGRO_TTF_FONT_DATA *font_data, FT_Face face, |
| 450 |
int ft_index, ALLEGRO_TTF_GLYPH_DATA *glyph, bool lock_whole_page) |
| 451 |
{ |
| 452 |
FT_Int32 ft_load_flags; |
| 453 |
FT_Error e; |
| 454 |
int w, h; |
| 455 |
unsigned char *glyph_data; |
| 456 |
|
| 457 |
if (glyph->page_bitmap || glyph->region.x < 0) |
| 458 |
return; |
| 459 |
|
| 460 |
/* We shouldn't ever get here, as cache misses |
| 461 |
* should have been set to ft_index = 0. */ |
| 462 |
ASSERT(!(font_data->skip_cache_misses && !lock_whole_page)); |
| 463 |
|
| 464 |
// FIXME: make this a config setting? FT_LOAD_FORCE_AUTOHINT |
| 465 |
|
| 466 |
ft_load_flags = FT_LOAD_RENDER; |
| 467 |
if (face->num_fixed_sizes) { |
| 468 |
// This is a bitmap font with fixed sizes, let's load them in |
| 469 |
// color if available. |
| 470 |
ft_load_flags |= FT_LOAD_COLOR; |
| 471 |
} |
| 472 |
else { |
| 473 |
// FIXME: Investigate why some fonts don't work without the |
| 474 |
// NO_BITMAP flags. Supposedly using that flag makes small sizes |
| 475 |
// look bad so ideally we would not used it. |
| 476 |
ft_load_flags |= FT_LOAD_NO_BITMAP; |
| 477 |
} |
| 478 |
|
| 479 |
if (font_data->flags & ALLEGRO_TTF_MONOCHROME) |
| 480 |
ft_load_flags |= FT_LOAD_TARGET_MONO; |
| 481 |
if (font_data->flags & ALLEGRO_TTF_NO_AUTOHINT) |
| 482 |
ft_load_flags |= FT_LOAD_NO_AUTOHINT; |
| 483 |
|
| 484 |
e = FT_Load_Glyph(face, ft_index, ft_load_flags); |
| 485 |
if (e) { |
| 486 |
ALLEGRO_WARN("Failed loading glyph %d from.\n", ft_index); |
| 487 |
} |
| 488 |
|
| 489 |
glyph->offset_x = face->glyph->bitmap_left; |
| 490 |
glyph->offset_y = (face->size->metrics.ascender >> 6) - face->glyph->bitmap_top; |
| 491 |
glyph->advance = face->glyph->advance.x >> 6; |
| 492 |
|
| 493 |
w = face->glyph->bitmap.width; |
| 494 |
h = face->glyph->bitmap.rows; |
| 495 |
|
| 496 |
if (w == 0 || h == 0) { |
| 497 |
/* Mark this glyph so we won't try to cache it next time. */ |
| 498 |
glyph->region.x = -1; |
| 499 |
glyph->region.y = -1; |
| 500 |
glyph->page_bitmap = NULL; |
| 501 |
/* Even though this glyph has no "region", include the 2-pixel border in the size */ |
| 502 |
glyph->region.w = w + 4; |
| 503 |
glyph->region.h = h + 4; |
| 504 |
ALLEGRO_DEBUG("Glyph %d has zero size. (pixel mode %d)\n", ft_index, face->glyph->bitmap.pixel_mode); |
| 505 |
return; |
| 506 |
} |
| 507 |
|
| 508 |
/* Each glyph has a 2-pixel border all around. Note: The border is kept |
| 509 |
* even against the outer bitmap edge, to ensure consistent rendering. |
| 510 |
*/ |
| 511 |
glyph_data = alloc_glyph_region(font_data, ft_index, |
| 512 |
w + 4, h + 4, false, glyph, lock_whole_page); |
| 513 |
|
| 514 |
if (glyph_data == NULL) { |
| 515 |
return; |
| 516 |
} |
| 517 |
|
| 518 |
if (font_data->flags & ALLEGRO_TTF_MONOCHROME) |
| 519 |
copy_glyph_mono(font_data, face, glyph_data); |
| 520 |
else |
| 521 |
copy_glyph_color(font_data, face, glyph_data); |
| 522 |
|
| 523 |
if (!lock_whole_page) { |
| 524 |
unlock_current_page(font_data); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
/* WARNING: It is only valid to call this function when the current page is empty |
| 529 |
* (or already locked), otherwise it will gibberify the current glyphs on that page. |
| 530 |
* |
| 531 |
* This leaves the current page unlocked. |
| 532 |
*/ |
| 533 |
static void cache_glyphs(ALLEGRO_TTF_FONT_DATA *data, const char *text, size_t text_size) |
| 534 |
{ |
| 535 |
ALLEGRO_USTR_INFO info; |
| 536 |
const ALLEGRO_USTR* ustr = al_ref_buffer(&info, text, text_size); |
| 537 |
FT_Face face = data->face; |
| 538 |
int pos = 0; |
| 539 |
int32_t ch; |
| 540 |
|
| 541 |
while ((ch = al_ustr_get_next(ustr, &pos)) >= 0) { |
| 542 |
ALLEGRO_TTF_GLYPH_DATA *glyph; |
| 543 |
int ft_index = FT_Get_Char_Index(face, ch); |
| 544 |
get_glyph(data, ft_index, &glyph); |
| 545 |
cache_glyph(data, face, ft_index, glyph, true); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
static int get_kerning(ALLEGRO_TTF_FONT_DATA const *data, FT_Face face, |
| 551 |
int prev_ft_index, int ft_index) |
| 552 |
{ |
| 553 |
/* Do kerning? */ |
| 554 |
if (!(data->flags & ALLEGRO_TTF_NO_KERNING) && prev_ft_index != -1) { |
| 555 |
FT_Vector delta; |
| 556 |
FT_Get_Kerning(face, prev_ft_index, ft_index, |
| 557 |
FT_KERNING_DEFAULT, &delta); |
| 558 |
return delta.x >> 6; |
| 559 |
} |
| 560 |
|
| 561 |
return 0; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
static bool ttf_get_glyph_worker(ALLEGRO_FONT const *f, int prev_ft_index, int ft_index, int prev_codepoint, int codepoint, ALLEGRO_GLYPH *info) |
| 566 |
{ |
| 567 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 568 |
FT_Face face = data->face; |
| 569 |
ALLEGRO_TTF_GLYPH_DATA *glyph; |
| 570 |
int advance = 0; |
| 571 |
|
| 572 |
if (!get_glyph(data, ft_index, &glyph)) { |
| 573 |
if (f->fallback) |
| 574 |
return f->fallback->vtable->get_glyph(f->fallback, prev_codepoint, codepoint, info); |
| 575 |
else { |
| 576 |
get_glyph(data, 0, &glyph); |
| 577 |
ft_index = 0; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
cache_glyph(data, face, ft_index, glyph, false); |
| 582 |
|
| 583 |
advance += get_kerning(data, face, prev_ft_index, ft_index); |
| 584 |
|
| 585 |
info->bitmap = glyph->page_bitmap; |
| 586 |
/* the adjustments below remove the 2-pixel border from the glyph */ |
| 587 |
if (info->bitmap) { |
| 588 |
info->x = glyph->region.x + 2; |
| 589 |
info->y = glyph->region.y + 2; |
| 590 |
} |
| 591 |
else { |
| 592 |
info->x = -1; |
| 593 |
info->y = -1; |
| 594 |
} |
| 595 |
info->w = glyph->region.w - 4; |
| 596 |
info->h = glyph->region.h - 4; |
| 597 |
info->kerning = advance; |
| 598 |
info->offset_x = glyph->offset_x; |
| 599 |
info->offset_y = glyph->offset_y; |
| 600 |
info->advance = glyph->advance + advance; |
| 601 |
|
| 602 |
return ft_index != 0; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
static bool ttf_get_glyph(ALLEGRO_FONT const *f, int prev_codepoint, int codepoint, ALLEGRO_GLYPH *glyph) |
| 607 |
{ |
| 608 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 609 |
FT_Face face = data->face; |
| 610 |
int prev_ft_index = (prev_codepoint == -1) ? -1 : (int)FT_Get_Char_Index(face, prev_codepoint); |
| 611 |
int ft_index = FT_Get_Char_Index(face, codepoint); |
| 612 |
return ttf_get_glyph_worker(f, prev_ft_index, ft_index, prev_codepoint, codepoint, glyph); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
static int render_glyph(ALLEGRO_FONT const *f, ALLEGRO_COLOR color, |
| 617 |
int prev_ft_index, int ft_index, int32_t prev_ch, int32_t ch, float xpos, float ypos) |
| 618 |
{ |
| 619 |
ALLEGRO_GLYPH glyph; |
| 620 |
|
| 621 |
if (ttf_get_glyph_worker(f, prev_ft_index, ft_index, prev_ch, ch, &glyph) == false) |
| 622 |
return 0; |
| 623 |
|
| 624 |
if (glyph.bitmap != NULL) { |
| 625 |
/* |
| 626 |
* Include 1 pixel of the 2-pixel border when drawing glyph. |
| 627 |
* This improves render results when rotating and scaling. |
| 628 |
*/ |
| 629 |
al_draw_tinted_bitmap_region( |
| 630 |
glyph.bitmap, color, |
| 631 |
glyph.x - 1, glyph.y - 1, glyph.w + 2, glyph.h + 2, |
| 632 |
xpos + glyph.offset_x + glyph.kerning - 1, |
| 633 |
ypos + glyph.offset_y - 1, |
| 634 |
0 |
| 635 |
); |
| 636 |
} |
| 637 |
|
| 638 |
return glyph.advance; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
static int ttf_font_height(ALLEGRO_FONT const *f) |
| 643 |
{ |
| 644 |
ASSERT(f); |
| 645 |
return f->height; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
static int ttf_font_ascent(ALLEGRO_FONT const *f) |
| 650 |
{ |
| 651 |
ALLEGRO_TTF_FONT_DATA *data; |
| 652 |
FT_Face face; |
| 653 |
|
| 654 |
ASSERT(f); |
| 655 |
|
| 656 |
data = f->data; |
| 657 |
face = data->face; |
| 658 |
|
| 659 |
return face->size->metrics.ascender >> 6; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
static int ttf_font_descent(ALLEGRO_FONT const *f) |
| 664 |
{ |
| 665 |
ALLEGRO_TTF_FONT_DATA *data; |
| 666 |
FT_Face face; |
| 667 |
|
| 668 |
ASSERT(f); |
| 669 |
|
| 670 |
data = f->data; |
| 671 |
face = data->face; |
| 672 |
|
| 673 |
return (-face->size->metrics.descender) >> 6; |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
static int ttf_render_char(ALLEGRO_FONT const *f, ALLEGRO_COLOR color, |
| 678 |
int ch, float xpos, float ypos) |
| 679 |
{ |
| 680 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 681 |
FT_Face face = data->face; |
| 682 |
int advance = 0; |
| 683 |
int32_t ch32 = (int32_t) ch; |
| 684 |
|
| 685 |
int ft_index = FT_Get_Char_Index(face, ch32); |
| 686 |
advance = render_glyph(f, color, -1, ft_index, -1, ch, xpos, ypos); |
| 687 |
|
| 688 |
return advance; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
static int ttf_char_length(ALLEGRO_FONT const *f, int ch) |
| 693 |
{ |
| 694 |
int result; |
| 695 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 696 |
ALLEGRO_TTF_GLYPH_DATA *glyph; |
| 697 |
FT_Face face = data->face; |
| 698 |
int ft_index = FT_Get_Char_Index(face, ch); |
| 699 |
if (!get_glyph(data, ft_index, &glyph)) { |
| 700 |
if (f->fallback) { |
| 701 |
return al_get_glyph_width(f->fallback, ch); |
| 702 |
} |
| 703 |
else { |
| 704 |
get_glyph(data, 0, &glyph); |
| 705 |
ft_index = 0; |
| 706 |
} |
| 707 |
} |
| 708 |
cache_glyph(data, face, ft_index, glyph, false); |
| 709 |
result = glyph->region.w - 4; /* Remove 2-pixel border from width */ |
| 710 |
|
| 711 |
return result; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
static int ttf_render(ALLEGRO_FONT const *f, ALLEGRO_COLOR color, |
| 716 |
const ALLEGRO_USTR *text, float x, float y) |
| 717 |
{ |
| 718 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 719 |
FT_Face face = data->face; |
| 720 |
int pos = 0; |
| 721 |
int advance = 0; |
| 722 |
int prev_ft_index = -1; |
| 723 |
int32_t prev_ch = -1; |
| 724 |
int32_t ch; |
| 725 |
bool hold; |
| 726 |
|
| 727 |
hold = al_is_bitmap_drawing_held(); |
| 728 |
al_hold_bitmap_drawing(true); |
| 729 |
|
| 730 |
while ((ch = al_ustr_get_next(text, &pos)) >= 0) { |
| 731 |
int ft_index = FT_Get_Char_Index(face, ch); |
| 732 |
advance += render_glyph(f, color, prev_ft_index, ft_index, prev_ch, ch, |
| 733 |
x + advance, y); |
| 734 |
prev_ft_index = ft_index; |
| 735 |
prev_ch = ch; |
| 736 |
} |
| 737 |
|
| 738 |
al_hold_bitmap_drawing(hold); |
| 739 |
|
| 740 |
return advance; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
static int ttf_text_length(ALLEGRO_FONT const *f, const ALLEGRO_USTR *text) |
| 745 |
{ |
| 746 |
int pos = 0; |
| 747 |
int x = 0; |
| 748 |
int32_t ch, nch; |
| 749 |
|
| 750 |
nch = al_ustr_get_next(text, &pos); |
| 751 |
while (nch >= 0) { |
| 752 |
ch = nch; |
| 753 |
nch = al_ustr_get_next(text, &pos); |
| 754 |
|
| 755 |
x += al_get_glyph_advance(f, ch, nch < 0 ? |
| 756 |
ALLEGRO_NO_KERNING : nch); |
| 757 |
} |
| 758 |
|
| 759 |
return x; |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
static void ttf_get_text_dimensions(ALLEGRO_FONT const *f, |
| 764 |
ALLEGRO_USTR const *text, |
| 765 |
int *bbx, int *bby, int *bbw, int *bbh) |
| 766 |
{ |
| 767 |
int pos = 0; |
| 768 |
bool first = true; |
| 769 |
int x = 0; |
| 770 |
int32_t ch, nch; |
| 771 |
int ymin = f->height; |
| 772 |
int ymax = 0; |
| 773 |
*bbx = 0; |
| 774 |
|
| 775 |
nch = al_ustr_get_next(text, &pos); |
| 776 |
while (nch >= 0) { |
| 777 |
int gx, gy, gw, gh; |
| 778 |
ch = nch; |
| 779 |
nch = al_ustr_get_next(text, &pos); |
| 780 |
if (!al_get_glyph_dimensions(f, ch, &gx, &gy, &gw, &gh)) { |
| 781 |
continue; |
| 782 |
} |
| 783 |
|
| 784 |
if (nch < 0) { |
| 785 |
x += gx + gw; |
| 786 |
} |
| 787 |
else { |
| 788 |
x += al_get_glyph_advance(f, ch, nch); |
| 789 |
} |
| 790 |
|
| 791 |
if (gy < ymin) { |
| 792 |
ymin = gy; |
| 793 |
} |
| 794 |
|
| 795 |
if (gh+gy > ymax) { |
| 796 |
ymax = gh + gy; |
| 797 |
} |
| 798 |
|
| 799 |
if (first) { |
| 800 |
*bbx = gx; |
| 801 |
first = false; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
*bby = ymin; |
| 806 |
*bbw = x - *bbx; |
| 807 |
*bbh = ymax - ymin; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
#ifdef DEBUG_CACHE |
| 812 |
#include "allegro5/allegro_image.h" |
| 813 |
static void debug_cache(ALLEGRO_FONT *f) |
| 814 |
{ |
| 815 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 816 |
_AL_VECTOR *v = &data->page_bitmaps; |
| 817 |
static int j = 0; |
| 818 |
int i; |
| 819 |
|
| 820 |
al_init_image_addon(); |
| 821 |
|
| 822 |
for (i = 0; i < (int)_al_vector_size(v); i++) { |
| 823 |
ALLEGRO_BITMAP **bmp = _al_vector_ref(v, i); |
| 824 |
ALLEGRO_USTR *u = al_ustr_newf("font%d_%d.png", j, i); |
| 825 |
al_save_bitmap(al_cstr(u), *bmp); |
| 826 |
al_ustr_free(u); |
| 827 |
} |
| 828 |
j++; |
| 829 |
} |
| 830 |
#endif |
| 831 |
|
| 832 |
|
| 833 |
static void ttf_destroy(ALLEGRO_FONT *f) |
| 834 |
{ |
| 835 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 836 |
int i; |
| 837 |
|
| 838 |
unlock_current_page(data); |
| 839 |
|
| 840 |
#ifdef DEBUG_CACHE |
| 841 |
debug_cache(f); |
| 842 |
#endif |
| 843 |
|
| 844 |
FT_Done_Face(data->face); |
| 845 |
for (i = _al_vector_size(&data->glyph_ranges) - 1; i >= 0; i--) { |
| 846 |
ALLEGRO_TTF_GLYPH_RANGE *range = _al_vector_ref(&data->glyph_ranges, i); |
| 847 |
al_free(range->glyphs); |
| 848 |
} |
| 849 |
_al_vector_free(&data->glyph_ranges); |
| 850 |
for (i = _al_vector_size(&data->page_bitmaps) - 1; i >= 0; i--) { |
| 851 |
ALLEGRO_BITMAP **bmp = _al_vector_ref(&data->page_bitmaps, i); |
| 852 |
al_destroy_bitmap(*bmp); |
| 853 |
} |
| 854 |
_al_vector_free(&data->page_bitmaps); |
| 855 |
al_free(data); |
| 856 |
al_free(f); |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
static unsigned long ftread(FT_Stream stream, unsigned long offset, |
| 861 |
unsigned char *buffer, unsigned long count) |
| 862 |
{ |
| 863 |
ALLEGRO_TTF_FONT_DATA *data = stream->pathname.pointer; |
| 864 |
unsigned long bytes; |
| 865 |
|
| 866 |
if (count == 0) |
| 867 |
return 0; |
| 868 |
|
| 869 |
if (offset != data->offset) |
| 870 |
al_fseek(data->file, data->base_offset + offset, ALLEGRO_SEEK_SET); |
| 871 |
bytes = al_fread(data->file, buffer, count); |
| 872 |
data->offset = offset + bytes; |
| 873 |
return bytes; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
static void ftclose(FT_Stream stream) |
| 878 |
{ |
| 879 |
ALLEGRO_TTF_FONT_DATA *data = stream->pathname.pointer; |
| 880 |
al_fclose(data->file); |
| 881 |
data->file = NULL; |
| 882 |
} |
| 883 |
|
| 884 |
/* Function: al_load_ttf_font_f |
| 885 |
*/ |
| 886 |
ALLEGRO_FONT *al_load_ttf_font_f(ALLEGRO_FILE *file, |
| 887 |
char const *filename, int size, int flags) |
| 888 |
{ |
| 889 |
return al_load_ttf_font_stretch_f(file, filename, 0, size, flags); |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
/* Function: al_load_ttf_font_stretch_f |
| 894 |
*/ |
| 895 |
ALLEGRO_FONT *al_load_ttf_font_stretch_f(ALLEGRO_FILE *file, |
| 896 |
char const *filename, int w, int h, int flags) |
| 897 |
{ |
| 898 |
FT_Face face; |
| 899 |
ALLEGRO_TTF_FONT_DATA *data; |
| 900 |
ALLEGRO_FONT *f; |
| 901 |
ALLEGRO_PATH *path; |
| 902 |
FT_Open_Args args; |
| 903 |
int result; |
| 904 |
ALLEGRO_CONFIG* system_cfg = al_get_system_config(); |
| 905 |
const char* min_page_size_str = |
| 906 |
al_get_config_value(system_cfg, "ttf", "min_page_size"); |
| 907 |
const char* max_page_size_str = |
| 908 |
al_get_config_value(system_cfg, "ttf", "max_page_size"); |
| 909 |
const char* cache_str = |
| 910 |
al_get_config_value(system_cfg, "ttf", "cache_text"); |
| 911 |
const char* skip_cache_misses_str = |
| 912 |
al_get_config_value(system_cfg, "ttf", "skip_cache_misses"); |
| 913 |
|
| 914 |
if ((h > 0 && w < 0) || (h < 0 && w > 0)) { |
| 915 |
ALLEGRO_ERROR("Height/width have opposite signs (w = %d, h = %d).\n", w, h); |
| 916 |
return NULL; |
| 917 |
} |
| 918 |
|
| 919 |
data = al_calloc(1, sizeof *data); |
| 920 |
data->stream.read = ftread; |
| 921 |
data->stream.close = ftclose; |
| 922 |
data->stream.pathname.pointer = data; |
| 923 |
data->base_offset = al_ftell(file); |
| 924 |
data->stream.size = al_fsize(file); |
| 925 |
data->file = file; |
| 926 |
data->bitmap_format = al_get_new_bitmap_format(); |
| 927 |
data->bitmap_flags = al_get_new_bitmap_flags(); |
| 928 |
data->min_page_size = 256; |
| 929 |
data->max_page_size = 8192; |
| 930 |
|
| 931 |
if (min_page_size_str) { |
| 932 |
int min_page_size = atoi(min_page_size_str); |
| 933 |
if (min_page_size > 0) { |
| 934 |
data->min_page_size = min_page_size; |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
if (max_page_size_str) { |
| 939 |
int max_page_size = atoi(max_page_size_str); |
| 940 |
if (max_page_size > 0 && max_page_size >= data->min_page_size) { |
| 941 |
data->max_page_size = max_page_size; |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
if (skip_cache_misses_str && !strcmp(skip_cache_misses_str, "true")) { |
| 946 |
data->skip_cache_misses = true; |
| 947 |
} |
| 948 |
|
| 949 |
memset(&args, 0, sizeof args); |
| 950 |
args.flags = FT_OPEN_STREAM; |
| 951 |
args.stream = &data->stream; |
| 952 |
|
| 953 |
if ((result = FT_Open_Face(ft, &args, 0, &face)) != 0) { |
| 954 |
ALLEGRO_ERROR("Reading %s failed. Freetype error code %d\n", filename, |
| 955 |
result); |
| 956 |
// Note: Freetype already closed the file for us. |
| 957 |
al_free(data); |
| 958 |
return NULL; |
| 959 |
} |
| 960 |
|
| 961 |
// FIXME: The below doesn't use Allegro's streaming. |
| 962 |
/* Small hack for Type1 fonts which store kerning information in |
| 963 |
* a separate file - and we try to guess the name of that file. |
| 964 |
*/ |
| 965 |
path = al_create_path(filename); |
| 966 |
if (!strcmp(al_get_path_extension(path), ".pfa")) { |
| 967 |
const char *helper; |
| 968 |
ALLEGRO_DEBUG("Type1 font assumed for %s.\n", filename); |
| 969 |
|
| 970 |
al_set_path_extension(path, ".afm"); |
| 971 |
helper = al_path_cstr(path, '/'); |
| 972 |
FT_Attach_File(face, helper); |
| 973 |
ALLEGRO_DEBUG("Guessed afm file %s.\n", helper); |
| 974 |
|
| 975 |
al_set_path_extension(path, ".tfm"); |
| 976 |
helper = al_path_cstr(path, '/'); |
| 977 |
FT_Attach_File(face, helper); |
| 978 |
ALLEGRO_DEBUG("Guessed tfm file %s.\n", helper); |
| 979 |
} |
| 980 |
al_destroy_path(path); |
| 981 |
|
| 982 |
if (face->num_fixed_sizes) { |
| 983 |
// TODO: we always pick the first size |
| 984 |
FT_Select_Size(face, 0); |
| 985 |
} else if (h > 0) { |
| 986 |
FT_Set_Pixel_Sizes(face, w, h); |
| 987 |
} |
| 988 |
else { |
| 989 |
/* Set the "real dimension" of the font to be the passed size, |
| 990 |
* in pixels. |
| 991 |
*/ |
| 992 |
FT_Size_RequestRec req; |
| 993 |
ASSERT(w <= 0); |
| 994 |
ASSERT(h <= 0); |
| 995 |
req.type = FT_SIZE_REQUEST_TYPE_REAL_DIM; |
| 996 |
req.width = (-w) << 6; |
| 997 |
req.height = (-h) << 6; |
| 998 |
req.horiResolution = 0; |
| 999 |
req.vertResolution = 0; |
| 1000 |
FT_Request_Size(face, &req); |
| 1001 |
} |
| 1002 |
|
| 1003 |
ALLEGRO_DEBUG("Font %s loaded with pixel size %d x %d.\n", filename, |
| 1004 |
w, h); |
| 1005 |
ALLEGRO_DEBUG(" ascent=%.1f, descent=%.1f, height=%.1f\n", |
| 1006 |
face->size->metrics.ascender / 64.0, |
| 1007 |
face->size->metrics.descender / 64.0, |
| 1008 |
face->size->metrics.height / 64.0); |
| 1009 |
|
| 1010 |
data->face = face; |
| 1011 |
data->flags = flags; |
| 1012 |
|
| 1013 |
_al_vector_init(&data->glyph_ranges, sizeof(ALLEGRO_TTF_GLYPH_RANGE)); |
| 1014 |
_al_vector_init(&data->page_bitmaps, sizeof(ALLEGRO_BITMAP*)); |
| 1015 |
|
| 1016 |
if (data->skip_cache_misses) { |
| 1017 |
cache_glyphs(data, "\0", 1); |
| 1018 |
} |
| 1019 |
if (cache_str) { |
| 1020 |
cache_glyphs(data, cache_str, strlen(cache_str)); |
| 1021 |
} |
| 1022 |
unlock_current_page(data); |
| 1023 |
|
| 1024 |
f = al_calloc(sizeof *f, 1); |
| 1025 |
f->height = face->size->metrics.height >> 6; |
| 1026 |
f->vtable = &vt; |
| 1027 |
f->data = data; |
| 1028 |
|
| 1029 |
f->dtor_item = _al_register_destructor(_al_dtor_list, "ttf_font", f, |
| 1030 |
(void (*)(void *))al_destroy_font); |
| 1031 |
|
| 1032 |
return f; |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
/* Function: al_load_ttf_font |
| 1037 |
*/ |
| 1038 |
ALLEGRO_FONT *al_load_ttf_font(char const *filename, int size, int flags) |
| 1039 |
{ |
| 1040 |
return al_load_ttf_font_stretch(filename, 0, size, flags); |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
/* Function: al_load_ttf_font_stretch |
| 1045 |
*/ |
| 1046 |
ALLEGRO_FONT *al_load_ttf_font_stretch(char const *filename, int w, int h, |
| 1047 |
int flags) |
| 1048 |
{ |
| 1049 |
ALLEGRO_FILE *f; |
| 1050 |
ALLEGRO_FONT *font; |
| 1051 |
ASSERT(filename); |
| 1052 |
|
| 1053 |
f = al_fopen(filename, "rb"); |
| 1054 |
if (!f) { |
| 1055 |
ALLEGRO_ERROR("Unable to open file for reading: %s\n", filename); |
| 1056 |
return NULL; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* The file handle is owned by the function and the file is usually only |
| 1060 |
* closed when the font is destroyed, in case Freetype has to load data |
| 1061 |
* at a later time. |
| 1062 |
*/ |
| 1063 |
font = al_load_ttf_font_stretch_f(f, filename, w, h, flags); |
| 1064 |
|
| 1065 |
return font; |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
static int ttf_get_font_ranges(ALLEGRO_FONT *font, int ranges_count, |
| 1070 |
int *ranges) |
| 1071 |
{ |
| 1072 |
ALLEGRO_TTF_FONT_DATA *data = font->data; |
| 1073 |
FT_UInt g; |
| 1074 |
FT_ULong unicode = FT_Get_First_Char(data->face, &g); |
| 1075 |
int i = 0; |
| 1076 |
if (i < ranges_count) { |
| 1077 |
ranges[i * 2 + 0] = unicode; |
| 1078 |
ranges[i * 2 + 1] = unicode; |
| 1079 |
} |
| 1080 |
while (g) { |
| 1081 |
FT_ULong unicode2 = FT_Get_Next_Char(data->face, unicode, &g); |
| 1082 |
if (unicode + 1 != unicode2) { |
| 1083 |
if (i < ranges_count) { |
| 1084 |
ranges[i * 2 + 1] = unicode; |
| 1085 |
if (i + 1 < ranges_count) { |
| 1086 |
ranges[(i + 1) * 2 + 0] = unicode2; |
| 1087 |
} |
| 1088 |
} |
| 1089 |
i++; |
| 1090 |
} |
| 1091 |
if (i < ranges_count) { |
| 1092 |
ranges[i * 2 + 1] = unicode2; |
| 1093 |
} |
| 1094 |
unicode = unicode2; |
| 1095 |
} |
| 1096 |
return i; |
| 1097 |
} |
| 1098 |
|
| 1099 |
static bool ttf_get_glyph_dimensions(ALLEGRO_FONT const *f, |
| 1100 |
int codepoint, |
| 1101 |
int *bbx, int *bby, int *bbw, int *bbh) |
| 1102 |
{ |
| 1103 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 1104 |
ALLEGRO_TTF_GLYPH_DATA *glyph; |
| 1105 |
FT_Face face = data->face; |
| 1106 |
int ft_index = FT_Get_Char_Index(face, codepoint); |
| 1107 |
if (!get_glyph(data, ft_index, &glyph)) { |
| 1108 |
if (f->fallback) { |
| 1109 |
return al_get_glyph_dimensions(f->fallback, codepoint, |
| 1110 |
bbx, bby, bbw, bbh); |
| 1111 |
} |
| 1112 |
else { |
| 1113 |
get_glyph(data, 0, &glyph); |
| 1114 |
ft_index = 0; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
cache_glyph(data, face, ft_index, glyph, false); |
| 1118 |
*bbx = glyph->offset_x; |
| 1119 |
*bbw = glyph->region.w - 4; |
| 1120 |
*bbh = glyph->region.h - 4; |
| 1121 |
*bby = glyph->offset_y; |
| 1122 |
|
| 1123 |
return true; |
| 1124 |
} |
| 1125 |
|
| 1126 |
static int ttf_get_glyph_advance(ALLEGRO_FONT const *f, int codepoint1, |
| 1127 |
int codepoint2) |
| 1128 |
{ |
| 1129 |
ALLEGRO_TTF_FONT_DATA *data = f->data; |
| 1130 |
FT_Face face = data->face; |
| 1131 |
int ft_index = FT_Get_Char_Index(face, codepoint1); |
| 1132 |
ALLEGRO_TTF_GLYPH_DATA *glyph; |
| 1133 |
int kerning = 0; |
| 1134 |
int advance = 0; |
| 1135 |
|
| 1136 |
if (codepoint1 == ALLEGRO_NO_KERNING) { |
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
if (!get_glyph(data, ft_index, &glyph)) { |
| 1141 |
if (f->fallback) { |
| 1142 |
return al_get_glyph_advance(f->fallback, codepoint1, codepoint2); |
| 1143 |
} |
| 1144 |
else { |
| 1145 |
get_glyph(data, 0, &glyph); |
| 1146 |
ft_index = 0; |
| 1147 |
} |
| 1148 |
} |
| 1149 |
cache_glyph(data, face, ft_index, glyph, false); |
| 1150 |
|
| 1151 |
if (codepoint2 != ALLEGRO_NO_KERNING) { |
| 1152 |
int ft_index1 = FT_Get_Char_Index(face, codepoint1); |
| 1153 |
int ft_index2 = FT_Get_Char_Index(face, codepoint2); |
| 1154 |
kerning = get_kerning(data, face, ft_index1, ft_index2); |
| 1155 |
} |
| 1156 |
|
| 1157 |
advance = glyph->advance; |
| 1158 |
return advance + kerning; |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
/* Function: al_init_ttf_addon |
| 1164 |
*/ |
| 1165 |
bool al_init_ttf_addon(void) |
| 1166 |
{ |
| 1167 |
if (ttf_inited) { |
| 1168 |
ALLEGRO_WARN("TTF addon already initialised.\n"); |
| 1169 |
return true; |
| 1170 |
} |
| 1171 |
|
| 1172 |
FT_Init_FreeType(&ft); |
| 1173 |
vt.font_height = ttf_font_height; |
| 1174 |
vt.font_ascent = ttf_font_ascent; |
| 1175 |
vt.font_descent = ttf_font_descent; |
| 1176 |
vt.char_length = ttf_char_length; |
| 1177 |
vt.text_length = ttf_text_length; |
| 1178 |
vt.render_char = ttf_render_char; |
| 1179 |
vt.render = ttf_render; |
| 1180 |
vt.destroy = ttf_destroy; |
| 1181 |
vt.get_text_dimensions = ttf_get_text_dimensions; |
| 1182 |
vt.get_font_ranges = ttf_get_font_ranges; |
| 1183 |
vt.get_glyph_dimensions = ttf_get_glyph_dimensions; |
| 1184 |
vt.get_glyph_advance = ttf_get_glyph_advance; |
| 1185 |
vt.get_glyph = ttf_get_glyph; |
| 1186 |
|
| 1187 |
al_register_font_loader(".ttf", al_load_ttf_font); |
| 1188 |
|
| 1189 |
_al_add_exit_func(al_shutdown_ttf_addon, "al_shutdown_ttf_addon"); |
| 1190 |
|
| 1191 |
/* Can't fail right now - in the future we might dynamically load |
| 1192 |
* the FreeType DLL here and/or initialize FreeType (which both |
| 1193 |
* could fail and would cause a false return). |
| 1194 |
*/ |
| 1195 |
ttf_inited = true; |
| 1196 |
return ttf_inited; |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
/* Function: al_is_ttf_addon_initialized |
| 1201 |
*/ |
| 1202 |
bool al_is_ttf_addon_initialized(void) |
| 1203 |
{ |
| 1204 |
return ttf_inited; |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
/* Function: al_shutdown_ttf_addon |
| 1209 |
*/ |
| 1210 |
void al_shutdown_ttf_addon(void) |
| 1211 |
{ |
| 1212 |
if (!ttf_inited) { |
| 1213 |
ALLEGRO_ERROR("TTF addon not initialised.\n"); |
| 1214 |
return; |
| 1215 |
} |
| 1216 |
|
| 1217 |
al_register_font_loader(".ttf", NULL); |
| 1218 |
|
| 1219 |
FT_Done_FreeType(ft); |
| 1220 |
|
| 1221 |
ttf_inited = false; |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
/* Function: al_get_allegro_ttf_version |
| 1226 |
*/ |
| 1227 |
uint32_t al_get_allegro_ttf_version(void) |
| 1228 |
{ |
| 1229 |
return ALLEGRO_VERSION_INT; |
| 1230 |
} |
| 1231 |
|
| 1232 |
/* vim: set sts=3 sw=3 et: */ |