SDL_ttf outline font rendering patch [RFC]

Hello,

I am working on surveillance video player project. Since I needed font
outlining I had to extend SDL_ttf functionality with ability of using
FreeType’s FT_Glyph_Stroke to create outlined rendering for overlay
text (white text with black outline).

Attaching patch for SDL_ttf SVN. (below)

Here’s some screenshot of rendering:
http://www.nanoant.com/screenshots/sdl/SDL_ttf-outline-patch.png

… with following function:

TTF_SetFontOutline(font_outline, 2);

(…)
SDL_Color white = {0xFF, 0xFF, 0xFF};
SDL_Color black = {0x00, 0x00, 0x00};
SDL_Surface *black_text_surface = TTF_RenderText_Blended
(font_outline, text, black);
SDL_Surface *white_text_surface = TTF_RenderText_Blended(font, text,
white);
SDL_TextureID black_text = SDL_CreateTextureFromSurface(0,
black_text_surface);
SDL_TextureID white_text = SDL_CreateTextureFromSurface(0,
white_text_surface);
SDL_Rect black_rect = {x, y, black_text_surface->w,
black_text_surface->h};
SDL_Rect white_rect = {x + 2, y + 2, white_text_surface->w,
white_text_surface->h};
SDL_RenderCopy(black_text, NULL, &black_rect);
SDL_RenderCopy(white_text, NULL, &white_rect);
SDL_FreeSurface(black_text_surface); SDL_FreeSurface
(white_text_surface);
SDL_DestroyTexture(black_text); SDL_DestroyTexture(white_text);

… where font & font_outline are same TTF and size, but the second
has TTF_SetFontOutline set.

Hope it can find a way into SVN,

Waiting for comments,–
Adam Strzelecki | nanoant.com

-------------- next part --------------
A non-text attachment was scrubbed…
Name: SDL_ttf-font-outline.patch
Type: application/octet-stream
Size: 7182 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20091002/7c1ce27a/attachment.obj

This looks pretty good, but the width and height calculation aren’t
quite correct.

For example, try the attached showfont.c with -outline FletcherGothicFLF.ttfOn Thu, Oct 1, 2009 at 3:07 PM, Adam Strzelecki wrote:

Hello,

I am working on surveillance video player project. Since I needed font
outlining I had to extend SDL_ttf functionality with ability of using
FreeType’s FT_Glyph_Stroke to create outlined rendering for overlay text
(white text with black outline).

Attaching patch for SDL_ttf SVN. (below)

Here’s some screenshot of rendering:
http://www.nanoant.com/screenshots/sdl/SDL_ttf-outline-patch.png

… with following function:

? ? ? ?TTF_SetFontOutline(font_outline, 2);
(…)
? ? ? ?SDL_Color white = {0xFF, 0xFF, 0xFF};
? ? ? ?SDL_Color black = {0x00, 0x00, 0x00};
? ? ? ?SDL_Surface *black_text_surface =
TTF_RenderText_Blended(font_outline, text, black);
? ? ? ?SDL_Surface *white_text_surface = TTF_RenderText_Blended(font, text,
white);
? ? ? ?SDL_TextureID black_text = SDL_CreateTextureFromSurface(0,
black_text_surface);
? ? ? ?SDL_TextureID white_text = SDL_CreateTextureFromSurface(0,
white_text_surface);
? ? ? ?SDL_Rect black_rect = {x, y, black_text_surface->w,
black_text_surface->h};
? ? ? ?SDL_Rect white_rect = {x + 2, y + 2, white_text_surface->w,
white_text_surface->h};
? ? ? ?SDL_RenderCopy(black_text, NULL, &black_rect);
? ? ? ?SDL_RenderCopy(white_text, NULL, &white_rect);
? ? ? ?SDL_FreeSurface(black_text_surface);
SDL_FreeSurface(white_text_surface);
? ? ? ?SDL_DestroyTexture(black_text); SDL_DestroyTexture(white_text);

… where font & font_outline are same TTF and size, but the second has
TTF_SetFontOutline set.

Hope it can find a way into SVN,

Waiting for comments,

Adam Strzelecki | nanoant.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC
-------------- next part --------------
A non-text attachment was scrubbed…
Name: showfont.c
Type: application/octet-stream
Size: 10028 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20091004/4b158b36/attachment.obj

This looks pretty good, but the width and height calculation aren’t
quite correct.

Argh, you’re right, I’ve added proper calculation just into Blended
version, forgot Shaded and Mono.

Attaching updated patch.

(1) Adds TTF_SetFontOutline, note outline parameter is a outline size
(0 for no outline)

(2) Modifies showfont.c to accept -outline with integer (outline size)
parameter

(3) Fixes header inclusion for Mac OS X (APPLE).

Since SDL_ttf and SDL lay in separate frameworks in OSX, and their
headers lay in separate directories in OSX we should include SDL using:
#include <SDL/SDL.h> conforming #include <FrameworkName/
FrameworkHeader.h> that guarantees we don’t need to add any -I becuase
gcc will seek headers in appropriate framework folders.
Thats why patch has:
+#ifdef APPLE
+#include <SDL/SDL.h>
+#include <SDL/begin_code.h>
+#else
#include “SDL.h”
#include “begin_code.h”
+#endif

I think the same pt.(3) should/could be added to SDL_image & SDL_rtf
too.

Regards,–
Adam Strzelecki | nanoant.com

-------------- next part --------------
A non-text attachment was scrubbed…
Name: SDL_ttf-font-outline-2.patch
Type: application/octet-stream
Size: 9686 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20091005/9d98b171/attachment.obj

May I ask how did you solve the problem of flickering text when blitting
text on YUV surface?
(if you use YUV)-------
Dario

----- Original Message -----
From: ono@java.pl (Adam Strzelecki)
To: “SDL Development List”
Sent: Friday, October 02, 2009 12:07 AM
Subject: [SDL] SDL_ttf outline font rendering patch [RFC]

Hello,

I am working on surveillance video player project. Since I needed font
outlining I had to extend SDL_ttf functionality with ability of using
FreeType’s FT_Glyph_Stroke to create outlined rendering for overlay
text (white text with black outline).

Attaching patch for SDL_ttf SVN. (below)

Here’s some screenshot of rendering:
http://www.nanoant.com/screenshots/sdl/SDL_ttf-outline-patch.png

… with following function:

TTF_SetFontOutline(font_outline, 2);
(…)
SDL_Color white = {0xFF, 0xFF, 0xFF};
SDL_Color black = {0x00, 0x00, 0x00};
SDL_Surface *black_text_surface = TTF_RenderText_Blended
(font_outline, text, black);
SDL_Surface *white_text_surface = TTF_RenderText_Blended(font, text,
white);
SDL_TextureID black_text = SDL_CreateTextureFromSurface(0,
black_text_surface);
SDL_TextureID white_text = SDL_CreateTextureFromSurface(0,
white_text_surface);
SDL_Rect black_rect = {x, y, black_text_surface->w,
black_text_surface->h};
SDL_Rect white_rect = {x + 2, y + 2, white_text_surface->w,
white_text_surface->h};
SDL_RenderCopy(black_text, NULL, &black_rect);
SDL_RenderCopy(white_text, NULL, &white_rect);
SDL_FreeSurface(black_text_surface); SDL_FreeSurface
(white_text_surface);
SDL_DestroyTexture(black_text); SDL_DestroyTexture(white_text);

… where font & font_outline are same TTF and size, but the second
has TTF_SetFontOutline set.

Hope it can find a way into SVN,

Waiting for comments,

Adam Strzelecki | nanoant.com



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

ELMA Kurtalj d.o.o. (ELMA Kurtalj ltd.)
Vitezi?eva 1a, 10000 Zagreb, Hrvatska (Viteziceva 1a, 10000 Zagreb, Croatia)
Tel: 01/3035555, Faks: 01/3035599 (Tel: ++385-1-3035555, Fax: ++385-1-3035599 )

Www:
www.elma.hr; shop.elma.hr

E-mail:
elma at elma.hr (elma at elma.hr)
pitanje at elma.hr (questions at elma.hr)
primjedbe at elma.hr (complaints at elma.hr)
prodaja at elma.hr (sales at elma.hr)
servis at elma.hr (servicing at elma.hr)
shop at elma.hr (shop at elma.hr)
skladiste at elma.hr (warehouse at elma.hr)

May I ask how did you solve the problem of flickering text when
blitting
text on YUV surface?
(if you use YUV)

Actually I don’t. I use SDL 1.3 and YUV either planar or packed
(depending which one is supported by renderer) textures for video. I
render them, then I put on the top textures out of SDL_ttf, then
finally I flip the backbuffer.

But if you use real overlay (the way it is in 1.2) you have no other
choice IMHO but just blit the text on the top of overlay surface, just
before you render it on the screen.

Regards,–
Adam

Wow, I didn’t know they implemented that.
Is there any info anywhere about sdl textures instead of surfaces, how to
use it?
Some sample code maybe, or do I have to dig through svn?

P.S. I appologize for that big ugly maillist in the end of my mails but
company mail server does that automatically.-------
Dario.

----- Original Message -----
From: ono@java.pl (Adam Strzelecki)
To: “SDL Development List”
Sent: Wednesday, October 14, 2009 12:01 PM
Subject: Re: [SDL] SDL_ttf outline font rendering patch [RFC]

May I ask how did you solve the problem of flickering text when blitting
text on YUV surface?
(if you use YUV)

Actually I don’t. I use SDL 1.3 and YUV either planar or packed
(depending which one is supported by renderer) textures for video. I
render them, then I put on the top textures out of SDL_ttf, then finally
I flip the backbuffer.

But if you use real overlay (the way it is in 1.2) you have no other
choice IMHO but just blit the text on the top of overlay surface, just
before you render it on the screen.

Regards,

Adam


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

ELMA Kurtalj d.o.o. (ELMA Kurtalj ltd.)
Vitezi?eva 1a, 10000 Zagreb, Hrvatska (Viteziceva 1a, 10000 Zagreb, Croatia)
Tel: 01/3035555, Faks: 01/3035599 (Tel: ++385-1-3035555, Fax: ++385-1-3035599 )

Www:
www.elma.hr; shop.elma.hr

E-mail:
elma at elma.hr (elma at elma.hr)
pitanje at elma.hr (questions at elma.hr)
primjedbe at elma.hr (complaints at elma.hr)
prodaja at elma.hr (sales at elma.hr)
servis at elma.hr (servicing at elma.hr)
shop at elma.hr (shop at elma.hr)
skladiste at elma.hr (warehouse at elma.hr)

Wow, I didn’t know they implemented that.
Is there any info anywhere about sdl textures instead of surfaces,
how to
use it?

Well documentation for 1.3 is pretty inexistent. Yet there .h files
inside include are pretty well documented, so I bet you can just gen
1.3 docs via doxygen or sthing on fly.

Some sample code maybe, or do I have to dig through svn?

Yes, plenty. Just head to “test” subfolder of 1.3 (SVN trunk). All
(most of them?) the samples are using 1.3 API.
Of course 1.3 is transparently compatible with 1.2 via compat.c which
wraps 1.2 depracated functions, such YUV surfaces that are implemented
as YUV textures.

Regards,–
Adam Strzelecki | nanoant.com

2009/10/14 Adam Strzelecki :

Wow, I didn’t know they implemented that.
Is there any info anywhere about sdl textures instead of surfaces, how to
use it?

Well documentation for 1.3 is pretty inexistent. Yet there .h files inside
include are pretty well documented, so I bet you can just gen 1.3 docs via
doxygen or sthing on fly.

Some sample code maybe, or do I have to dig through svn?

Yes, plenty. Just head to “test” subfolder of 1.3 (SVN trunk). All (most of
them?) the samples are using 1.3 API.
Of course 1.3 is transparently compatible with 1.2 via compat.c which wraps
1.2 depracated functions, such YUV surfaces that are implemented as YUV
textures.

There’s a bit of documentation here (including some examples):
http://www.libsdl.org/cgi/docwiki.cgi/SDL-1.3

Not a whole lot though. Doxygen and trunk/SDL/test are still your best bet.
NeHe has some SDL 1.3 tutorials apparently, but you’ll have to find them first.