PREFIX void spFontDraw( Sint32 x, Sint32 y, Sint32 z, const char* text, spFontPointer font ) { int l = 0; int pos = x; while ( text[l] != 0 ) { if (text[l] == '\n') { pos = x; y+=font->maxheight; l++; continue; } spLetterPointer letter; //first we look for buttons if (text[l]==spFontButtonLeft && (letter = spLetterFind( font->buttonRoot, text[l+1])) && text[l+2]==spFontButtonRight) l+=2; else letter = spFontGetLetter( font, text[l] ); //TODO utf8 if ( letter ) { pos += letter->width >> 1; spBlitSurface( pos, y + letter->height / 2, z, letter->surface ); pos += letter->width - ( letter->width >> 1 ); } l++; } }
PREFIX void spFontDrawMiddle( Sint32 x, Sint32 y, Sint32 z,const char* text, spFontPointer font ) { int l = 0; int width; spLetterIterPointer first = NULL; spLetterIterPointer last = NULL; int again = 1; while (again) { width = 0; again = 0; while ( text[l] != 0 ) { if (text[l] == '\n') { again = 1; l++; break; } spLetterPointer letter; //first we look for buttons if (text[l]==spFontButtonLeft && (letter = spLetterFind( font->buttonRoot, text[l+1])) && text[l+2]==spFontButtonRight) l+=2; else letter = spFontGetLetter( font, text[l] ); //TODO utf8 if ( letter ) { spLetterIterPointer iter = ( spLetterIterPointer )malloc( sizeof( spLetterIterStruct ) ); iter->letter = letter; if ( first == NULL ) { first = iter; last = iter; } else { last->next = iter; last = iter; } width += letter->width; iter->next = NULL; } l++; } int pos = x - width / 2; while ( first != NULL ) { pos += first->letter->width >> 1; spBlitSurface( pos, y + first->letter->height / 2, z, first->letter->surface ); pos += first->letter->width - ( first->letter->width >> 1 ); spLetterIterPointer mom = first; first = first->next; free( mom ); } if (again) y+=font->maxheight; } }
PREFIX int spFontWidth(const char* text, spFontPointer font ) { int width = 0; int l = 0; while ( text[l] != 0 ) { spLetterPointer letter = spFontGetLetter( font, text[l] ); //TODO utf8 if ( letter ) width += letter->width; l++; } return width; }
PREFIX void spFontChangeLetter( spFontPointer font, spLetterPointer letter, Uint32 character, Uint16 color ) { letter->color = color; Uint16 buffer[2]; buffer[0] = character; buffer[1] = 0; SDL_Color sdlcolor = {( color >> 11 ) << 3, ( ( color << 5 ) >> 10 ) << 2, ( ( color & 31 ) << 3 )}; SDL_Surface* surface; if (spFontBackgroundColor == SP_FONT_NO_BORDER) surface = TTF_RenderUNICODE_Solid( font->font, buffer, sdlcolor ); else { SDL_Color background = {( spFontBackgroundColor >> 11 ) << 3, ( ( spFontBackgroundColor << 5 ) >> 10 ) << 2, ( ( spFontBackgroundColor & 31 ) << 3 )}; surface = TTF_RenderUNICODE_Shaded( font->font, buffer, sdlcolor ,background ); } if (!surface) surface = spCreateSurface(0,font->maxheight); int width = surface->w + SP_FONT_EXTRASPACE * 2; if ( width & 1 ) width++; letter->surface = spCreateSurface(width, surface->h + SP_FONT_EXTRASPACE * 2); SDL_FillRect( letter->surface, NULL, SP_ALPHA_COLOR ); SDL_Rect DestR; DestR.x = SP_FONT_EXTRASPACE; DestR.y = SP_FONT_EXTRASPACE; DestR.w = surface->w; DestR.h = surface->h; SDL_BlitSurface( surface, NULL, letter->surface, &DestR ); SDL_FreeSurface( surface ); //Setting every spFontBackgroundColor to the alpha color SDL_LockSurface( letter->surface ); Uint16* pixel = ( Uint16* )( letter->surface->pixels ); int scanline = letter->surface->pitch/letter->surface->format->BytesPerPixel; int x, y; for ( x = 0; x < letter->surface->w; x++ ) for ( y = 0; y < letter->surface->h; y++ ) if ( pixel[x + y * scanline] == spFontBackgroundColor ) pixel[x + y * scanline] = SP_ALPHA_COLOR; SDL_UnlockSurface( letter->surface ); TTF_SizeUNICODE( font->font, buffer, &( letter->width ), &( letter->height ) ); if ( font->maxheight < letter->height ) font->maxheight = letter->height; } static void spFontInternalAddOneCharacter( spFontPointer font, Uint32 character, Uint16 color ) { if (spFontGetLetter(font,character)) return; spLetterPointer letter = ( spLetterPointer )malloc( sizeof( spLetter ) ); spFontChangeLetter( font, letter, character, color ); letter->character = character; //tree insert font->root = spFontInsert( letter, font->root ); } PREFIX void spFontAdd( spFontPointer font, char* characters, Uint16 color ) { //parsing the characters string. int pos = 0; while (1) { Uint32 character = spFontGetUnicodeFromUTF8(&(characters[pos])); if (character == 0) break; pos+=spFontLastUTF8Length; spFontInternalAddOneCharacter(font,character,color); } } PREFIX void spFontAddRange( spFontPointer font, char* from, char* to, Uint16 color ) { //getting the characters strings Uint32 c_from = spFontGetUnicodeFromUTF8(from); if (c_from == 0) return; Uint32 c_to = spFontGetUnicodeFromUTF8(to); if (c_to == 0) return; Uint32 character; if (c_from > c_to) { character = c_from; c_from = c_to; c_to = character; } for (character = c_from; character <= c_to; character++) spFontInternalAddOneCharacter(font,character,color); }