Skip to content

Commit f7918f0

Browse files
check-switch-26SiegeLord
authored andcommitted
TTF bugfixes - al_get_glyph_width(), al_get_glyph_al_get_glyph()
- calling al_get_glyph_width() on a non-existent codepoint would crash with a stack overflow if a fallback font is defined - value returned by al_get_glyph_width() was off by two pixels (this is a regression bug caused by a previous commit) - al_get_glyph_dimensions() would return negative width or height for zero-width or zero-height glyphs - made data returned by al_get_glyph() consistent with al_get_glyph_dimensions() for zero-width glyphs
1 parent 7d57723 commit f7918f0

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

addons/ttf/ttf.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ static void cache_glyph(ALLEGRO_TTF_FONT_DATA *font_data, FT_Face face,
468468
/* Mark this glyph so we won't try to cache it next time. */
469469
glyph->region.x = -1;
470470
glyph->region.y = -1;
471+
glyph->page_bitmap = NULL;
472+
/* Even though this glyph has no "region", include the 2-pixel border in the size */
473+
glyph->region.w = w + 4;
474+
glyph->region.h = h + 4;
471475
ALLEGRO_DEBUG("Glyph %d has zero size.\n", ft_index);
472476
return;
473477
}
@@ -549,30 +553,24 @@ static bool ttf_get_glyph_worker(ALLEGRO_FONT const *f, int prev_ft_index, int f
549553

550554
advance += get_kerning(data, face, prev_ft_index, ft_index);
551555

552-
if (glyph->page_bitmap) {
553-
info->bitmap = glyph->page_bitmap;
554-
/* the adjustments below remove the 2-pixel border from the glyph */
556+
info->bitmap = glyph->page_bitmap;
557+
/* the adjustments below remove the 2-pixel border from the glyph */
558+
if (info->bitmap) {
555559
info->x = glyph->region.x + 2;
556560
info->y = glyph->region.y + 2;
557-
info->w = glyph->region.w - 4;
558-
info->h = glyph->region.h - 4;
559-
info->kerning = advance;
560-
info->offset_x = glyph->offset_x;
561-
info->offset_y = glyph->offset_y;
562-
}
563-
else if (glyph->region.x > 0) {
564-
ALLEGRO_ERROR("Glyph %d not on any page.\n", ft_index);
565-
return false;
566561
}
567562
else {
568-
info->bitmap = 0;
563+
info->x = -1;
564+
info->y = -1;
569565
}
566+
info->w = glyph->region.w - 4;
567+
info->h = glyph->region.h - 4;
568+
info->kerning = advance;
569+
info->offset_x = glyph->offset_x;
570+
info->offset_y = glyph->offset_y;
571+
info->advance = glyph->advance + advance;
570572

571-
advance += glyph->advance;
572-
573-
info->advance = advance;
574-
575-
return true;
573+
return ft_index != 0;
576574
}
577575

578576

@@ -671,15 +669,15 @@ static int ttf_char_length(ALLEGRO_FONT const *f, int ch)
671669
int ft_index = FT_Get_Char_Index(face, ch);
672670
if (!get_glyph(data, ft_index, &glyph)) {
673671
if (f->fallback) {
674-
return al_get_glyph_width(f, ch);
672+
return al_get_glyph_width(f->fallback, ch);
675673
}
676674
else {
677675
get_glyph(data, 0, &glyph);
678676
ft_index = 0;
679677
}
680678
}
681679
cache_glyph(data, face, ft_index, glyph, false);
682-
result = glyph->region.w - 2;
680+
result = glyph->region.w - 4; /* Remove 2-pixel border from width */
683681

684682
return result;
685683
}

0 commit comments

Comments
 (0)