From b1e1d288795ed0d654cf71f656dfa8ed00fcf665 Mon Sep 17 00:00:00 2001 From: David Salvisberg Date: Wed, 31 Dec 2014 13:49:18 +0100 Subject: [PATCH] Added rudimentary support for font outlines. --- src/bitmap.cpp | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/bitmap.cpp b/src/bitmap.cpp index fa499ad..37b3984 100644 --- a/src/bitmap.cpp +++ b/src/bitmap.cpp @@ -51,6 +51,8 @@ "Operation not supported for mega surfaces"); \ } +#define OUTLINE_SIZE 1 + /* Normalize (= ensure width and * height are positive) */ static IntRect normalizedRect(const IntRect &rect) @@ -942,6 +944,7 @@ void Bitmap::drawText(const IntRect &rect, const char *str, int align) TTF_Font *font = p->font->getSdlFont(); const Color &fontColor = p->font->getColor(); + const Color &outColor = p->font->getOutColor(); SDL_Color c = fontColor.toSDLColor(); c.a = 255; @@ -957,11 +960,33 @@ void Bitmap::drawText(const IntRect &rect, const char *str, int align) p->ensureFormat(txtSurf, SDL_PIXELFORMAT_ABGR8888); - // While real outlining is not yet here, use shadow - // as a replacement to at least make text legible - if (p->font->getShadow() || p->font->getOutline()) + if (p->font->getShadow()) applyShadow(txtSurf, *p->format, c); + /* outline using TTF_Outline and blending it together with SDL_BlitSurface + * FIXME: outline is forced to have the same opacity as the font color */ + if (p->font->getOutline()) { + SDL_Color co = outColor.toSDLColor(); + co.a = 255; + SDL_Surface *outline; + /* set the next font render to render the outline */ + TTF_SetFontOutline(font, OUTLINE_SIZE); + if (shState->rtData().config.solidFonts) + outline = TTF_RenderUTF8_Solid(font, str, co); + else + outline = TTF_RenderUTF8_Blended(font, str, co); + + p->ensureFormat(outline, SDL_PIXELFORMAT_ABGR8888); + SDL_Rect outRect = {OUTLINE_SIZE, OUTLINE_SIZE, txtSurf->w, txtSurf->h}; + + SDL_SetSurfaceBlendMode(txtSurf, SDL_BLENDMODE_BLEND); + SDL_BlitSurface(txtSurf, NULL, outline, &outRect); + SDL_FreeSurface(txtSurf); + txtSurf = outline; + /* reset outline to 0 */ + TTF_SetFontOutline(font, 0); + } + int alignX = rect.x; switch (align) -- 2.43.0