From 8bab7926c95c0a785e05ee9197401332a70af92b Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 03 Aug 2021 16:50:26 -0400 Subject: ttf: Center text within line skip height And free text surface. --- (limited to 'src') diff --git a/src/ttf.c b/src/ttf.c index 9cfaab0..2bc24cf 100644 --- a/src/ttf.c +++ b/src/ttf.c @@ -28,6 +28,7 @@ mf_ttf_render(TTF_Font *font, const char *text, SDL_Color *color, { SDL_Surface *surface; SDL_Texture *texture; + int line_skip; int ascent; int max_y; const char *c; @@ -46,10 +47,12 @@ mf_ttf_render(TTF_Font *font, const char *text, SDL_Color *color, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError()); + SDL_FreeSurface(surface); return NULL; } - ascent = TTF_FontAscent(font); + line_skip = TTF_FontLineSkip(font); + ascent = TTF_FontAscent (font); max_y = 0; for (c = text; *c != '\0'; ++c) { if (TTF_GlyphMetrics(font, *c, NULL, NULL, NULL, &glyph_max_y, @@ -64,7 +67,7 @@ mf_ttf_render(TTF_Font *font, const char *text, SDL_Color *color, } } rect->x = 0; - rect->y = ascent - max_y; + rect->y = (line_skip - ascent) / 2 + (ascent - max_y); rect->w = surface->w; rect->h = surface->h; -- cgit v0.9.1