GUI based dynamic font stroking

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
kthuul
Posts: 3
Joined: Thu Feb 05, 2009 11:13 pm

GUI based dynamic font stroking

Post by kthuul »

Hi, I needed to draw stroked fonts in my GUI, since I couldn't predict what the background image would look like, and therefore contrast issues would crop up.

Now I'm pretty sure there is a much more effective way of achieving this effect, and if you have one, please please post it. Anyway, here is my little hack:

Code: Select all

void drawStrokedFont(IGUIFont* pFont, const wchar_t* text, const SColor& strokeCol, const SColor& textCol, const rect<s32>& rect)
{
	core::rect<s32> tRect = rect;
	struct offset_values
	{
		s32 xOff;
		s32 yOff;
	} offsets[] =
	{	{  0, -1 },	{ -1, -1 }, { -1,  0 },	{ -1,  1 },	{  0,  1 },	{  1,  1 },	{  1,  0 },	{  1, -1 } };

	for ( int i = 0; i < 8; i++ )
	{
		tRect = rect;
		tRect.UpperLeftCorner.X += offsets[i].xOff; tRect.UpperLeftCorner.Y += offsets[i].yOff;
		tRect.LowerRightCorner.X += offsets[i].xOff; tRect.UpperLeftCorner.Y += offsets[i].yOff;
		pFont->draw( text, tRect, strokeCol, true, true, &tRect);

	}

	// draw the white text
  pFont->draw( text, rect, textCol, true, true, &rect);
}
I hope someone has a better/quicker hack/method for doing this :)

Cheers
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

It would be 9 times faster to do this inside the font generator instead.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
kthuul
Posts: 3
Joined: Thu Feb 05, 2009 11:13 pm

Post by kthuul »

True, but this will work for TT and OT fonts as well.
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

The other option would be to make the font stroked up front like hybrid suggested, or if thats undesirable you can make two fonts, one "solid" and one "outline" and align them code side... we did this in one project and it looks great (using two fonts, simply drawing the text with two fonts, the solid and THEN the edges)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Eh, what did I say?
kthuul
Posts: 3
Joined: Thu Feb 05, 2009 11:13 pm

Post by kthuul »

I was thinking that the other way of doing this 'dynamically' (without pregenerated font sets) is to render the text to an offscreen buffer and then do edge detection to create the stroke. The benefit of doing this would be that the stroke could be wider than 1 pixel (current limitation of my hack).

So, I guess no one has a better algorithm for doing this dynamically?
Post Reply