Fix text alignment when using stb_unicode

This commit is contained in:
jdgleaver 2019-04-08 17:36:02 +01:00
parent 8aaef0e95c
commit 38d2febba5
1 changed files with 12 additions and 5 deletions

View File

@ -66,6 +66,13 @@ typedef struct
unsigned usage_counter;
} stb_unicode_font_renderer_t;
/* Ugly little thing... */
static int INLINE round_away_from_zero(float f)
{
double round = (f < 0.0) ? floor((double)f) : ceil((double)f);
return (int)round;
}
static struct font_atlas *font_renderer_stb_unicode_get_atlas(void *data)
{
stb_unicode_font_renderer_t *self = (stb_unicode_font_renderer_t*)data;
@ -153,10 +160,10 @@ static const struct font_glyph *font_renderer_stb_unicode_get_glyph(
atlas_slot->glyph.width = self->max_glyph_width;
atlas_slot->glyph.height = self->max_glyph_height;
atlas_slot->glyph.advance_x = advance_width * self->scale_factor;
/* atlas_slot->glyph.advance_y = 0 ; */
atlas_slot->glyph.draw_offset_x = x0 * self->scale_factor;
atlas_slot->glyph.draw_offset_y = -y1 * self->scale_factor;
atlas_slot->glyph.advance_x = round_away_from_zero((float)advance_width * self->scale_factor);
atlas_slot->glyph.advance_y = 0;
atlas_slot->glyph.draw_offset_x = round_away_from_zero((float)x0 * self->scale_factor);
atlas_slot->glyph.draw_offset_y = round_away_from_zero((float)(-y1) * self->scale_factor);
self->atlas.dirty = true;
atlas_slot->last_used = self->usage_counter++;
@ -177,7 +184,7 @@ static bool font_renderer_stb_unicode_create_atlas(
self->atlas.height = self->max_glyph_height * STB_UNICODE_ATLAS_ROWS;
self->atlas.buffer = (uint8_t*)
calloc(self->atlas.width * self->atlas.height, 1);
calloc(self->atlas.width * self->atlas.height, sizeof(uint8_t));
if (!self->atlas.buffer)
return false;