sparrowFont

SparrowFont is for loading truetype fonts with SDL_ttf and creating bitmap-lookup tables for them.  SDL_ttf and especially blending are slow like hell.  Not good for e.g. handhelds like the gp2x.  So the basic idea is to render every needed sign just once in a SDL surface and then just blit it - which is pretty fast.  Internal sparrowFont uses a binary tree and a cache for easy finding the right surface.  Because it depends, which texts you want to draw, you have to determine first, which sign you want.  Furthermore sparrowFont is able to parse button-description, which will be drawn in another way (like buttons or keys), and you can create text blocks for an easy implementation of scrolling text.

Summary
sparrowFontSparrowFont is for loading truetype fonts with SDL_ttf and creating bitmap-lookup tables for them.
Macros
Defines for the look of the buttons and keys
Default ranges for spFontAdd
SP_FONT_DEFAULT_CACHEPer default 16384 signs (needs 64 kilobytes) are cached.
SP_FONT_EXTRASPACEDefines some extra space for the border ;-)
SP_FONT_NO_BORDERUse this in spFontSetShadeColor, if you don’t want / need aliasing
Types
spLettera letter in a binary tree of a font
spFontCachethe font cache
spFontRoot of a binary tree of all letters in this font
spTextLineA line of a spTextBlock struct.
spTextBlockA text block is used to store a block of text with a maximum width and a resultung count of lines.
Enumerations
spTextBlockAlignmentDefines the orientation of a text block, when drawn.
Functions
spFontLoadLoads a ttf font
spFontLoadDeletes a spFont and loads a new font in the same struct.
spFontAddAdds characters to the font.
spFontAddRangeAdds a range of utf8 characters to the font defined by a start and end character
spFontGetUnicodeFromUTF8Converts a utf8 sign to unicode
spFontGetUTF8FromUnicodeConverts a unicode character to utf8
spFontAddButtonAdds a button the the font.
spFontAddArrowButtonAdds an arrow button the the font.
spFontSetButtonBoderSignsSets the starting and end sign for buttons, the defaults are ‘[‘ and ‘]’, which means, that “[A]” will be drawn as a button with the caption A, if you called spFontAddButton(font,’A’,”A”,fg,bg) before.
spFontSetSemanticButtonBoderSignsSets the starting and end sign for semantic buttons from sparrowMapping, the defaults are ‘{‘ and ‘}’, which means, that “{fire}” will be drawn as the button mapping to “fire”.
spFontSetButtonStrategySets the strategy used to determine, whether buttons are round (like normal buttons on handhelds) or quadric (like keyboard buttons).
spFontAddBorderFor better viewing on different backgrounds, most of the time it is useful to have a border around the letters.
spFontReplaceColorIf you want to change the color of every letter in the font, use this function.
spFontMulWidthThis function changes the width, which is similar to the distance of the letters, of the whole font.
spFontChangeLetterOnce I had a bug in sparrowFont.
spFontChangeButtonSame like spFontChangeLetter, but for the Buttons.
spFontChangeArrowButtonSame like spFontChangeLetter, but for the arrow buttons.
spFontGetLetterReturns the letter struct of a letter
spFontGetButtonWorks like spFontGetLetter but for buttons.
spFontDrawDraws a left aligned text to the render target
spFontDrawRightDraws a right aligned text to the render target
spFontDrawMiddleDraws a center aligned text to the render target
spFontWidthWidth of a text
spFontDeleteDeletes a font
spFontChangeCacheSizeChanges to size of the cache.
spFontChangeCacheSizeChanges to start of the cache.
spFontGetCacheStartReturns the start of the cache.
spFontSetShadeColorSets the “background” color, if you want to shade smoothly.
spFontShadeButtonsDesides, whether the button font should be shaded or not.
spFontAddEveryLetterOfTextBundleAdds every letter used in the given sparrowText Bundle of Translations.
spCreateTextBlockCreates an spTextBlock out of an text.
spDeleteTextBlockDeletes a text block created by spCreateTextBlock.
spFontDrawTextBlockDraws a previously created text block with a maximum height.
spFontGetLetterStringGenerates a string to use maybe for another call of spFontAdd.

Macros

Defines for the look of the buttons and keys

SP_FONT_INTELLIGENTUses keys or buttons as it should make sense on the target, e.g. keys for the PC, buttons for handhelds buttons, but “keys” for the Start or Select button
SP_FONT_BUTTONshows always buttons
SP_FONT_KEYshows always keys

Default ranges for spFontAdd

SP_FONT_GROUP_ASCIIwhole ASCII set (sign 32 (space) until 127 (~))
SP_FONT_GROUP_GERMANGerman letters like umlauts
SP_FONT_GROUP_ALPHABETAll 26 latin letters big and small
SP_FONT_GROUP_NUMBERSthe arab numbers (0 to 9), dot, comma and minus

SP_FONT_DEFAULT_CACHE

Per default 16384 signs (needs 64 kilobytes) are cached.  Thats enough most of the time.  In fact - because SDL_ttf is just able to use plane 0 (the first 65536 letters) of unicode, nearly everything you will need is cached, and the tree is useles...  But it was fun to implement it!

SP_FONT_EXTRASPACE

Defines some extra space for the border ;-)

SP_FONT_NO_BORDER

Use this in spFontSetShadeColor, if you don’t want / need aliasing

Types

spLetter

a letter in a binary tree of a font

Variables

character (Uint32)unicode character
surface (SDL_Surface*)the character’s surface
width (Sint32)the character’s width in pixels
height (Sint32)the character’s height in pixels
binary_height (Sint32)internal value for the binary tree.  Not the height in pixels
color (Uint16)16 Bit color of the character
left (spLetter*)internal pointer for the tree
right (spLetter*)internal pointer for the tree

spFontCache

the font cache

Variables

size (Uint32)size of the cache
cache (spLetter*)an array of pointers of spLetter

spFont

Root of a binary tree of all letters in this font

Variables

font (TTF_Font*)the SDL_ttf font struct
maxheight (Sint32)the height of the heighest letter in the tree
root (spLetter*)the root of the binary letter tree
cacheOffset (Uint32)the offset of the cache, where it “starts”
cache (spFontCache)the cache
size (Uint32)the size of the font
buttonRoot (spLetter*)the root of the buttons of the binary letter tree

spTextLine

A line of a spTextBlock struct.

Variables

count (int)the number of letters in this line
width (int)the width in pixel of this line
text (char*)the content of the line
font (spFontPointer>the used font

spTextBlock

A text block is used to store a block of text with a maximum width and a resultung count of lines.  Made to use for scrollable text.

Variables

line_count (int)the total number of lines in this text block
max_width (int)the maximum width of the text block
line (spTextLine*)array of the lines in the block.  See spTextLine

Enumerations

spTextBlockAlignment

Defines the orientation of a text block, when drawn.  Used by spFontDrawTextBlock.

Values

leftleft orientation
middlecentered orientation
rightright orientation

Functions

spFontLoad

PREFIX spFontPointer spFontLoad(const char *fontname,
Uint32 size)

Loads a ttf font

Parameters

fontnamethe name of the ttf font file
sizethe size to load the font

Returns

spFont*a pointer to a spFont for later use

spFontLoad

Deletes a spFont and loads a new font in the same struct.

Parameters

fontpointer to spFont to reload
fontnamethe name of the ttf font file
sizethe size to load the font

Returns

int0 no error, 1 load failed

spFontAdd

PREFIX void spFontAdd(spFontPointer font,
char *characters,
Uint16 color)

Adds characters to the font.  You need to call this once after spFontLoad and before drawing, with every character you eventually want to draw

Parameters

fontthe font pointer, which shall be filled with new stuff
characterscharacters, that shall be added.  It adds every utf8 character passed in this char* string.  Some defines (SP_FONT_GROUP_ASCII, SP_FONT_GROUP_GERMAN, etc.  ) are provided for ease of use.  However: If a letter already is in the range, the later mentions will be ignored
colorcolor of the added letters

spFontAddRange

PREFIX void spFontAddRange(spFontPointer font,
char *from,
char *to,
Uint16 color)

Adds a range of utf8 characters to the font defined by a start and end character

Parameters

fontthe font pointer, which shall be filled with new stuff
fromstart character
toend character.  All characters between from and to will be added, e.g. usw from = 0 and to = 9 to add all numbers.  See ASCII/utf8 tables for more usefull ranges.  If to smaller than from, the values are switched
colorcolor of the added characters

spFontGetUnicodeFromUTF8

PREFIX Uint32 spFontGetUnicodeFromUTF8(const char *sign)

Converts a utf8 sign to unicode

Parameters

signutf8 character

Returns

Uint32unicode value

spFontGetUTF8FromUnicode

PREFIX char* spFontGetUTF8FromUnicode(Uint32 sign,
char *buffer,
int len)

Converts a unicode character to utf8

Parameters

signunicode sign
bufferbuffer for the resulting utf8 character, you have to alloc it!  However, 5 bytes are always enough
lenmaximal length of the buffer

Returns

char*the same pointer as buffer points, too.  Just for better usement.

spFontAddButton

PREFIX void spFontAddButton(spFontPointer font,
Uint32 character,
char *caption,
Uint16 fgColor,
Uint16 bgColor)

Adds a button the the font.

Parameters

fontthe font pointer, which shall be filled with new stuff
characterthis unicode character will identify the button, e.g.  ‘A’ for the Button A or ‘S’ for Select.  Use whatever you want.
captionthe name of the button.  If you use the button design, use only names of one letter, with the key design more letters are fine.
fgColora 16 bit color value identifying the foreground color of the button, that means the color of the caption and the border
bgColora 16 bit color value identifying the background color of the button

spFontAddArrowButton

PREFIX void spFontAddArrowButton(spFontPointer font,
Uint32 character,
int direction,
Uint16 fgColor,
Uint16 bgColor)

Adds an arrow button the the font.

Parameters

fontthe font pointer, which shall be filled with new stuff
characterthis unicode character will identify the arrow button, e.g.  ‘<’ for the left arrow or ‘v’ for down.  Use whatever you want.
directionPossible values are 1 (left), 2 (up), 3 (right) and 4 (down), also called SP_BUTTON_ARROW_LEFT, SP_BUTTON_ARROW_UP, SP_BUTTON_ARROW_RIGHT and SP_BUTTON_ARROW_DOWN
fgColora 16 bit color value identifying the foreground color of the button, that means the color of the caption and the border
bgColora 16 bit color value identifying the background color of the button

spFontSetButtonBoderSigns

Sets the starting and end sign for buttons, the defaults are ‘[‘ and ‘]’, which means, that “[A]” will be drawn as a button with the caption A, if you called spFontAddButton(font,’A’,”A”,fg,bg) before.

Parameters

leftthe start of a button
rightthe end of a button

spFontSetSemanticButtonBoderSigns

Sets the starting and end sign for semantic buttons from sparrowMapping, the defaults are ‘{‘ and ‘}’, which means, that “{fire}” will be drawn as the button mapping to “fire”.

Parameters

leftthe start of a semantic button
rightthe end of a semantic button

spFontSetButtonStrategy

PREFIX void spFontSetButtonStrategy(int strategy)

Sets the strategy used to determine, whether buttons are round (like normal buttons on handhelds) or quadric (like keyboard buttons).

Parameters

strategySP_FONT_INTELLIGENT uses rounds buttons for normal button, but quadric buttons for Start or Select and always quadric buttons for keyboard keys.  With SP_FONT_BUTTON or SP_FONT_KEY you can force another design.

spFontAddBorder

PREFIX void spFontAddBorder(spFontPointer font,
Uint16 bordercolor)

For better viewing on different backgrounds, most of the time it is useful to have a border around the letters.  With this function you can do this.  It adds a border to every currently loaded letter.  (Every letter loaded afterwards will have no border by default ) Note: Works on a pixel level, therefore needs a non-transparent font to work.

Parameters

fontthe font, that shall get a border
bordercolorthe 16 bit color of the border

spFontReplaceColor

PREFIX void spFontReplaceColor(spFontPointer font,
Uint16 oldcolor,
Uint16 newcolor)

If you want to change the color of every letter in the font, use this function.  It might be useful to draw just the border and no letter inside: First draw the letter in a color of your choice (e.g. black), than add a border (e.g. white) and than call spFontReplaceColor and replace the black with SP_ALPHA_COLOR (pink)

Parameters

fontthe font, which color shall be changed
oldcolor16 Bit color to be changed
newcolor16 Bit color to be set

spFontMulWidth

PREFIX void spFontMulWidth(spFontPointer font,
Sint32 factor)

This function changes the width, which is similar to the distance of the letters, of the whole font.

Parameters

fontthe font, which width will be changed
factorfixed point factor for the width of the output. factor = SP_ONE does nothing, greater does increase the distance and smaller ones will decrease the distance.

spFontChangeLetter

PREFIX void spFontChangeLetter(spFontPointer font,
spLetterPointer letter,
Uint32 character,
Uint16 color)

Once I had a bug in sparrowFont.  The “0” just looked ugly and wrong and it seemed to be the failure of SDL_ttf.  So I wrote this function to replace the surface, but not the NAME of a letter.  I called spFontChangeLetter(font,spFontGetLetter(font,’0’),’O’,color); and my 0 was O.  Looked a bit funny, but worked.

Parameters

fontthe font to be changed
letterthe letter in the font to be changed, use spFontGetLetter
characterthe character to be replaced.  The character value of the letter will not be changed, this is only for the redrawing of the surface
colorcolor of the letter

See Also

spFontChangeButton

spFontChangeButton

PREFIX void spFontChangeButton(spFontPointer font,
spLetterPointer letter,
Uint32 character,
char *caption,
Uint16 fgColor,
Uint16 bgColor)

Same like spFontChangeLetter, but for the Buttons.

spFontChangeArrowButton

PREFIX void spFontChangeArrowButton(spFontPointer font,
spLetterPointer letter,
Uint32 character,
int direction,
Uint16 fgColor,
Uint16 bgColor)

Same like spFontChangeLetter, but for the arrow buttons.

spFontGetLetter

PREFIX spLetterPointer spFontGetLetter(spFontPointer font,
Uint32 character)

Returns the letter struct of a letter

Parameters

fontthe font to be searched
characterthe unicode character, which shall be found

Returns

spLetter*NULL if not found, else the found letter struct

See Also

spFontGetButton

spFontGetButton

PREFIX spLetterPointer spFontGetButton(spFontPointer font,
Uint32 character)

Works like spFontGetLetter but for buttons.  ;-)

spFontDraw

PREFIX int spFontDraw(Sint32 x,
Sint32 y,
Sint32 z,
const unsigned char *text,
spFontPointer font)

Draws a left aligned text to the render target

Parameters

x,yposition on the render target
zz value for z-test, z-set, etc.
texttext to be drawn
fontfont, that shall be used for drawing the text

Returns

intthe printed width in pixels

spFontDrawRight

PREFIX int spFontDrawRight(Sint32 x,
Sint32 y,
Sint32 z,
const unsigned char *text,
spFontPointer font)

Draws a right aligned text to the render target

Parameters

x,yposition on the render target
zz value for z-test, z-set, etc.
texttext to be drawn
fontfont, that shall be used for drawing the text

Returns

intthe printed width in pixels

spFontDrawMiddle

PREFIX int spFontDrawMiddle(Sint32 x,
Sint32 y,
Sint32 z,
const unsigned char *text,
spFontPointer font)

Draws a center aligned text to the render target

Parameters

x,yposition on the render target
zz value for z-test, z-set, etc.
texttext to be drawn
fontfont, that shall be used for drawing the text

Returns

intthe printed width in pixels

spFontWidth

PREFIX int spFontWidth(const unsigned char *text,
spFontPointer font)

Width of a text

Parameters

textthe text, which size you want to know
fontthe font, which defines the size of the text

Returns

intthe width in pixel, if you would draw the text with the font

spFontDelete

PREFIX void spFontDelete(spFontPointer font)

Deletes a font

Parameters

fontthe font to be deleted

spFontChangeCacheSize

PREFIX void spFontChangeCacheSize(spFontPointer font,
Sint32 size)

Changes to size of the cache.

Parameters

fontthe font, which cache you want to modify
sizenew size of the cache

spFontChangeCacheSize

Changes to start of the cache.

Parameters

fontthe font, which cache you want to modify
letterthe new start of the cache.  Default is 0.  The whole cache goes from
the start to start + cache size1.  Everything IN the cache has a finding time of 1.

spFontGetCacheStart

PREFIX Sint32 spFontGetCacheStart(spFontPointer font)

Returns the start of the cache.

Parameters

fontthe font, which cache property you want to get

Returns

Sint32the start of the cache

spFontSetShadeColor

PREFIX void spFontSetShadeColor(int value)

Sets the “background” color, if you want to shade smoothly.  It is still no alpha blending, but the blended font will be look better.  However the font might look ugly at least with a shitty background color.  So the default is SP_FONT_NO_BORDER, which means, that no shading is done and the font looks normal pixelized.

Parameters

valuethe 16 bit alpha color to which it shall be shaded

spFontShadeButtons

PREFIX void spFontShadeButtons(int value)

Desides, whether the button font should be shaded or not.  For compatibility (especially with transparent background color of the buttons) the default is false (0).  But in most cases true (1) should look much better.  :)

Parameters

value1 enabled, 0 disabled

spFontAddEveryLetterOfTextBundle

PREFIX void spFontAddEveryLetterOfTextBundle(spFontPointer font,
spBundlePointer bundle,
Uint16 color)

Adds every letter used in the given sparrowText Bundle of Translations.  See sparrowText for more.

Parameters

fontthe font to be changed
bundlethe bundle, to be scaned for every used character
colorthe color of the added characters

spCreateTextBlock

PREFIX spTextBlockPointer spCreateTextBlock(const unsigned char *text,
int max_width,
spFontPointer font)

Creates an spTextBlock out of an text.  Use it in spFontDrawTextBlock to draw scrollable text.

Parameters

textthe text to convert to a text block
max_widththe maximal width one line should have at all.  If a line consists
of one word, which is longer than max_width the word is splitted andis added.  Otherwise a line break always happens between words
fontthe font (pointer to spFont) to use to determine the width of the rendered text lines

Returns

spTextBlockPointerpointer to the created spTextBlock struct.

spDeleteTextBlock

PREFIX void spDeleteTextBlock(spTextBlockPointer block)

Deletes a text block created by spCreateTextBlock.

Parameters

blockpointer to spTextBlock to free

spFontDrawTextBlock

PREFIX int spFontDrawTextBlock(spTextBlockAlignment alignment,
Sint32 x,
Sint32 y,
Sint32 z,
spTextBlockPointer block,
Sint32 height,
Sint32 position,
spFontPointer font)

Draws a previously created text block with a maximum height.  Also supports scrolling.

Parameters

aligmentdetermines, whether the text should be aligned left, centered or right.  Possible values (spTextBlockAlignment) are left, middle and right
x,ypossion of the text on the screen
zz value to set and test (if enabled) while drawing
blockthe text block created by spCreateTextBlock to draw
heightthe maximum height of the text to be drawn.
positionfixed point value between 0 and SP_ONE, which determines which part of the block shall be drawn.  Use it for scrolling.  If the whole block fits in the block->max_width X height block, of course this parameters doesn’t make any sense and is ignored
fontthe font to draw the text.  Should be the same as used in spCreateTextBlock or NULL, but however don’t have to.  If NULL the font of spCreateTextBlock is used.

Returns

intthe number of lines drawn

spFontGetLetterString

PREFIX char* spFontGetLetterString(spFontPointer font,
unsigned char *buffer,
int length)

Generates a string to use maybe for another call of spFontAdd.

Parameters

fontspFontPointer to use
bufferchar* buffer to fill
lengthsize of the buffer

Returns

char*the pointer to buffer
A text block is used to store a block of text with a maximum width and a resultung count of lines.
PREFIX spFontPointer spFontLoad(const char *fontname,
Uint32 size)
Loads a ttf font
Root of a binary tree of all letters in this font
PREFIX void spFontAdd(spFontPointer font,
char *characters,
Uint16 color)
Adds characters to the font.
PREFIX void spFontAddRange(spFontPointer font,
char *from,
char *to,
Uint16 color)
Adds a range of utf8 characters to the font defined by a start and end character
PREFIX Uint32 spFontGetUnicodeFromUTF8(const char *sign)
Converts a utf8 sign to unicode
PREFIX char* spFontGetUTF8FromUnicode(Uint32 sign,
char *buffer,
int len)
Converts a unicode character to utf8
PREFIX void spFontAddButton(spFontPointer font,
Uint32 character,
char *caption,
Uint16 fgColor,
Uint16 bgColor)
Adds a button the the font.
PREFIX void spFontAddArrowButton(spFontPointer font,
Uint32 character,
int direction,
Uint16 fgColor,
Uint16 bgColor)
Adds an arrow button the the font.
sparrowMapping is for handling different mappings of controls.
PREFIX void spFontSetButtonStrategy(int strategy)
Sets the strategy used to determine, whether buttons are round (like normal buttons on handhelds) or quadric (like keyboard buttons).
PREFIX void spFontAddBorder(spFontPointer font,
Uint16 bordercolor)
For better viewing on different backgrounds, most of the time it is useful to have a border around the letters.
PREFIX void spFontReplaceColor(spFontPointer font,
Uint16 oldcolor,
Uint16 newcolor)
If you want to change the color of every letter in the font, use this function.
PREFIX void spFontMulWidth(spFontPointer font,
Sint32 factor)
This function changes the width, which is similar to the distance of the letters, of the whole font.
PREFIX void spFontChangeLetter(spFontPointer font,
spLetterPointer letter,
Uint32 character,
Uint16 color)
Once I had a bug in sparrowFont.
PREFIX void spFontChangeButton(spFontPointer font,
spLetterPointer letter,
Uint32 character,
char *caption,
Uint16 fgColor,
Uint16 bgColor)
Same like spFontChangeLetter, but for the Buttons.
PREFIX void spFontChangeArrowButton(spFontPointer font,
spLetterPointer letter,
Uint32 character,
int direction,
Uint16 fgColor,
Uint16 bgColor)
Same like spFontChangeLetter, but for the arrow buttons.
PREFIX spLetterPointer spFontGetLetter(spFontPointer font,
Uint32 character)
Returns the letter struct of a letter
PREFIX spLetterPointer spFontGetButton(spFontPointer font,
Uint32 character)
Works like spFontGetLetter but for buttons.
PREFIX int spFontDraw(Sint32 x,
Sint32 y,
Sint32 z,
const unsigned char *text,
spFontPointer font)
Draws a left aligned text to the render target
PREFIX int spFontDrawRight(Sint32 x,
Sint32 y,
Sint32 z,
const unsigned char *text,
spFontPointer font)
Draws a right aligned text to the render target
PREFIX int spFontDrawMiddle(Sint32 x,
Sint32 y,
Sint32 z,
const unsigned char *text,
spFontPointer font)
Draws a center aligned text to the render target
PREFIX int spFontWidth(const unsigned char *text,
spFontPointer font)
Width of a text
PREFIX void spFontDelete(spFontPointer font)
Deletes a font
PREFIX void spFontChangeCacheSize(spFontPointer font,
Sint32 size)
Changes to size of the cache.
PREFIX Sint32 spFontGetCacheStart(spFontPointer font)
Returns the start of the cache.
PREFIX void spFontSetShadeColor(int value)
Sets the “background” color, if you want to shade smoothly.
PREFIX void spFontShadeButtons(int value)
Desides, whether the button font should be shaded or not.
PREFIX void spFontAddEveryLetterOfTextBundle(spFontPointer font,
spBundlePointer bundle,
Uint16 color)
Adds every letter used in the given sparrowText Bundle of Translations.
PREFIX spTextBlockPointer spCreateTextBlock(const unsigned char *text,
int max_width,
spFontPointer font)
Creates an spTextBlock out of an text.
PREFIX void spDeleteTextBlock(spTextBlockPointer block)
Deletes a text block created by spCreateTextBlock.
PREFIX int spFontDrawTextBlock(spTextBlockAlignment alignment,
Sint32 x,
Sint32 y,
Sint32 z,
spTextBlockPointer block,
Sint32 height,
Sint32 position,
spFontPointer font)
Draws a previously created text block with a maximum height.
PREFIX char* spFontGetLetterString(spFontPointer font,
unsigned char *buffer,
int length)
Generates a string to use maybe for another call of spFontAdd.
a letter in a binary tree of a font
A line of a spTextBlock struct.
whole ASCII set (sign 32 (space) until 127 (~))
German letters like umlauts
Uses keys or buttons as it should make sense on the target, e.g.
shows always buttons
shows always keys
Use this in spFontSetShadeColor, if you don’t want / need aliasing
SparrowText is for easy maintaining different translations of texts used by your application.
Defines the orientation of a text block, when drawn.
Close