diff -u -r SDL_ttf/SDL_ttf.c SDL_ttf-2.0.5/SDL_ttf.c --- SDL_ttf/SDL_ttf.c Fri Jan 18 22:46:04 2002 +++ SDL_ttf-2.0.5/SDL_ttf.c Sun Feb 2 20:19:32 2003 @@ -1281,6 +1281,242 @@ return(textbuf); } +/* Convert the Latin-1 text to UNICODE and render it +*/ +SDL_Surface *TTF_RenderText_Blended_Shaded(TTF_Font *font, + const char *text, SDL_Color fg, SDL_Color bg) +{ + SDL_Surface *textbuf; + Uint16 *unicode_text; + int unicode_len; + + /* Copy the Latin-1 text to a UNICODE text buffer */ + unicode_len = strlen(text); + unicode_text = (Uint16 *)malloc((unicode_len+1)*(sizeof *unicode_text)); + if ( unicode_text == NULL ) { + TTF_SetError("Out of memory"); + return(NULL); + } + ASCII_to_UNICODE(unicode_text, text, unicode_len); + + /* Render the new text */ + textbuf = TTF_RenderUNICODE_Blended_Shaded(font, unicode_text, fg, bg); + + /* Free the text buffer and return */ + free(unicode_text); + return(textbuf); +} + +/* Convert the UTF-8 text to UNICODE and render it +*/ +SDL_Surface *TTF_RenderUTF8_Blended_Shaded(TTF_Font *font, + const char *text, SDL_Color fg, SDL_Color bg) +{ + SDL_Surface *textbuf; + Uint16 *unicode_text; + int unicode_len; + + /* Copy the UTF-8 text to a UNICODE text buffer */ + unicode_len = strlen(text); + unicode_text = (Uint16 *)malloc((unicode_len+1)*(sizeof *unicode_text)); + if ( unicode_text == NULL ) { + TTF_SetError("Out of memory"); + return(NULL); + } + UTF8_to_UNICODE(unicode_text, text, unicode_len); + + /* Render the new text */ + textbuf = TTF_RenderUNICODE_Blended_Shaded(font, unicode_text, fg, bg); + + /* Free the text buffer and return */ + free(unicode_text); + return(textbuf); +} + +SDL_Surface *TTF_RenderUNICODE_Blended_Shaded(TTF_Font *font, + const Uint16 *text, SDL_Color fg, SDL_Color bg) +{ + int xstart; + int width, height; + SDL_Surface *textbuf; + Uint32 alpha; + Uint32 dst_pixel; + Uint32 pixel; + Uint32 s, d, buf; + const Uint16 *ch; + Uint8 *src; + Uint32 *dst; + int row, col; + c_glyph *glyph; + FT_Error error; + + if(!bg.unused) { /* background ALPHA = 0 */ + return TTF_RenderUNICODE_Blended(font, text, fg); + } + + /* Get the dimensions of the text surface */ + if ( (TTF_SizeUNICODE(font, text, &width, NULL) < 0) || !width ) { + TTF_SetError("Text has zero width"); + return(NULL); + } + height = font->height; + + textbuf = SDL_AllocSurface(SDL_SWSURFACE, width, height, 32, + 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); + if ( textbuf == NULL ) { + return(NULL); + } + + /* Load and render each character */ + xstart = 0; + pixel = (fg.r<<16)|(fg.g<<8)|fg.b|0xFF000000; + dst_pixel = (bg.unused<<24)|(bg.r<<16)|(bg.g<<8)|bg.b; + + SDL_FillRect(textbuf, NULL , dst_pixel); + + for ( ch=text; *ch; ++ch ) { + error = Find_Glyph(font, *ch, CACHED_METRICS|CACHED_PIXMAP); + if( error ) { + SDL_FreeSurface( textbuf ); + return NULL; + } + glyph = font->current; + + width = glyph->pixmap.width; + src = (Uint8 *)glyph->pixmap.buffer; + for ( row = 0; row < glyph->pixmap.rows; ++row ) { + dst = (Uint32*) textbuf->pixels + + (row+glyph->yoffset) * textbuf->pitch/4 + + xstart + glyph->minx; + for ( col=width; col>0; --col ) { + alpha = *src++; + if(alpha) + { + if(alpha == 255) + { + *dst++ = pixel; + } else { + s = pixel & 0x00FF00FF; + d = dst_pixel & 0x00FF00FF; + buf = (d + ((s - d) * alpha >> 8)) & 0x00FF00FF; + s = (pixel & 0xFF00FF00) >> 8; + d = (dst_pixel & 0xFF00FF00) >> 8; + d += (s - d) * alpha >> 8; + d = (d & 0x00FF00FF) << 8; + *dst++ = buf | d; + } + } else { + /* alpha == 0 */ + dst++; + } + } + } + + xstart += glyph->advance; + if ( font->style & TTF_STYLE_BOLD ) { + xstart += font->glyph_overhang; + } + } + + /* Handle the underline style */ + if( font->style & TTF_STYLE_UNDERLINE ) { + row = font->ascent - font->underline_offset - 1; + if ( row >= textbuf->h) { + row = (textbuf->h-1) - font->underline_height; + } + dst = (Uint32 *)textbuf->pixels + row * textbuf->pitch/4; + pixel |= 0xFF000000; /* Amask */ + for ( row=font->underline_height; row>0; --row ) { + for ( col=0; col < textbuf->w; ++col ) { + dst[col] = pixel; + } + dst += textbuf->pitch/4; + } + } + return(textbuf); +} + +SDL_Surface *TTF_RenderGlyph_Blended_Shaded(TTF_Font *font, + Uint16 ch, SDL_Color fg, SDL_Color bg) +{ + SDL_Surface *textbuf; + Uint32 alpha; + Uint32 dst_pixel; + Uint32 pixel; + Uint32 s, d, buf; + Uint8 *src; + Uint32 *dst; + int row, col; + FT_Error error; + c_glyph *glyph; + + if(!bg.unused) { + return TTF_RenderGlyph_Blended(font, ch, fg); + } + + /* Get the glyph itself */ + error = Find_Glyph(font, ch, CACHED_METRICS|CACHED_PIXMAP); + if ( error ) { + return(NULL); + } + glyph = font->current; + + textbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, + glyph->pixmap.width, glyph->pixmap.rows, 32, + 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); + if ( ! textbuf ) { + return(NULL); + } + + /* Copy the character from the pixmap */ + pixel = (fg.r<<16)|(fg.g<<8)|fg.b|0xFF000000; + dst_pixel = (bg.unused<<24)|(bg.r<<16)|(bg.g<<8)|bg.b; + for ( row=0; rowh; ++row ) { + src = glyph->pixmap.buffer + row * glyph->pixmap.width; + dst = (Uint32 *)textbuf->pixels + row * textbuf->pitch/4; + for ( col=0; colpixmap.width; ++col ) { + alpha = *src++; + if(alpha) + { + if(alpha == 255) + { + *dst++ |= pixel; + } else { + s = pixel & 0x00FF00FF; + d = dst_pixel & 0x00FF00FF; + buf = (d + ((s - d) * alpha >> 8)) & 0x00FF00FF; + s = (pixel & 0xFF00FF00) >> 8; + d = (dst_pixel & 0xFF00FF00) >> 8; + d += (s - d) * alpha >> 8; + d = (d & 0x00FF00FF) << 8; + *dst++ |= buf | d; + } + } else { + /* alpha == 0 */ + *dst++ |= dst_pixel; + } + } + } + + /* Handle the underline style */ + if( font->style & TTF_STYLE_UNDERLINE ) { + row = font->ascent - font->underline_offset - 1; + if ( row >= textbuf->h) { + row = (textbuf->h-1) - font->underline_height; + } + dst = (Uint32 *)textbuf->pixels + row * textbuf->pitch/4; + pixel |= 0xFF000000; /* Amask */ + for ( row=font->underline_height; row>0; --row ) { + for ( col=0; col < textbuf->w; ++col ) { + dst[col] = pixel; + } + dst += textbuf->pitch/4; + } + } + return(textbuf); +} + + void TTF_SetFontStyle( TTF_Font* font, int style ) { font->style = style; diff -u -r SDL_ttf/SDL_ttf.h SDL_ttf-2.0.5/SDL_ttf.h --- SDL_ttf/SDL_ttf.h Sat Apr 13 17:08:49 2002 +++ SDL_ttf-2.0.5/SDL_ttf.h Sun Feb 2 20:12:32 2003 @@ -158,6 +158,19 @@ extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Blended(TTF_Font *font, Uint16 ch, SDL_Color fg); + +/* */ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended_Shaded( + TTF_Font *font, const char *text, SDL_Color fg, SDL_Color bg); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended_Shaded( + TTF_Font *font, const char *text, SDL_Color fg, SDL_Color bg); +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended_Shaded( + TTF_Font *font, const Uint16 *text, SDL_Color fg, SDL_Color bg); + +/* */ +extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Blended_Shaded( + TTF_Font *font, Uint16 ch, SDL_Color fg, SDL_Color bg); + /* For compatibility with previous versions, here are the old functions */ #define TTF_RenderText(font, text, fg, bg) \ TTF_RenderText_Shaded(font, text, fg, bg)