Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int RenderText(SDL_Renderer *Renderer, FontSet *Font, int X, int Y, RenderTextMode *Mode, const char *Text) {
- SDL_Surface *TextSurface, *RenderSurface;
- int Bold=0, Italic=0, Underline=0, Sup=0, Sub=0, Strike=0, Hidden=0, Link=0;
- int BG=IRCCOLOR_BG, FG=1, Swap;
- SDL_Color FontFG=IRCColors[IRCCOLOR_FG], FontBG=IRCColors[IRCCOLOR_BG];
- int CurDrawX = 0;
- char Text2[strlen(Text)+1], *Poke;
- int SurfaceWidth, SurfaceHeight;
- int WhichFont, TempW, TempH, i;
- char TempHex[7];
- int MaxWidth=0, LineVSpace=0;
- int Flags = 0;
- if(Mode) {
- MaxWidth = Mode->MaxWidth;
- LineVSpace = Mode->LineVSpace;
- //MaxHeight = Mode->MaxHeight;
- Flags = Mode->Flags;
- }
- char *Stripped = NULL;
- if(Mode && Mode->AlreadyStripped) Stripped = Mode->AlreadyStripped;
- else {
- Stripped = (char*)malloc(strlen(Text)+1);
- StripIRCText(Stripped,Text,0);
- }
- TTF_SizeUTF8(Font->Font[0], Stripped, &SurfaceWidth, &SurfaceHeight);
- if(Flags & RENDERMODE_RIGHT_JUSTIFY)
- X -= SurfaceWidth;
- RenderSurface = SDL_CreateRGBSurface(0, SurfaceWidth+8, SurfaceHeight+8, 32, 0, 0, 0, 0);
- if(!RenderSurface) return 0;
- SDL_FillRect(RenderSurface, NULL, SDL_MapRGB(RenderSurface->format, IRCColors[IRCCOLOR_BG].r, IRCColors[IRCCOLOR_BG].g, IRCColors[IRCCOLOR_BG].b));
- char *Find;
- while(1) {
- unsigned char k = *Text;
- switch(k) {
- case 0xfe:
- break;
- case 0x02:
- Bold ^= 1;
- break;
- case 0x04:
- for(i=1;i<7;i++)
- if(!isxdigit(Text[i])) break;
- if(i==7) {
- memcpy(TempHex, Text+1, 6); // only copy 7
- TempHex[6] = 0; // null terminator
- i = strtol(TempHex, NULL, 16);
- FontFG.r = (i >> 16) & 255;
- FontFG.g = (i >> 8) & 255;
- FontFG.b = i & 255;
- Text+=6;
- }
- break;
- case 0x03:
- FG=0;
- if(isdigit(Text[1])) {
- FG = Text[1]-'0';
- Text++;
- if(Text[1] == ',') {
- Text++;
- if(isdigit(Text[1])) {
- BG = Text[1]-'0';
- Text++;
- if(isdigit(Text[1])) {
- BG = ((BG*10)+(Text[1]-'0'))&31;
- Text++;
- }
- }
- } else if(isdigit(Text[1])) {
- FG = (FG*10)+(Text[1]-'0');
- Text++;
- if(Text[1] == ',') {
- Text++;
- if(isdigit(Text[1])) {
- BG = Text[1]-'0';
- Text++;
- if(isdigit(Text[1])) {
- BG = ((BG*10)+(Text[1]-'0'))&31;
- Text++;
- }
- }
- }
- }
- }
- FontFG=IRCColors[FG&31];
- FontBG=IRCColors[BG];
- break;
- case 0x0f:
- Bold = 0; Italic = 0;
- Underline = 0; Sup = 0;
- Sub = 0; Strike = 0;
- FontFG=IRCColors[1]; FontBG=IRCColors[IRCCOLOR_BG];
- break;
- case 0x16:
- Swap = BG;
- BG = FG;
- FG = Swap;
- break;
- case 0x1d:
- Italic ^= 1;
- break;
- case 0x1f:
- Underline ^= 1;
- break;
- case 0xff:
- switch((unsigned char)*(++Text)) {
- case FORMAT_SUBSCRIPT:
- Sub ^= 1;
- FontFG=Sub?IRCColors[IRCCOLOR_FADED]:IRCColors[IRCCOLOR_FG];
- break;
- case FORMAT_SUPERSCRIPT:
- Sup ^= 1;
- FontFG=Sub?IRCColors[IRCCOLOR_FADED]:IRCColors[IRCCOLOR_FG];
- break;
- case FORMAT_STRIKEOUT:
- Strike ^= 1;
- break;
- case FORMAT_HIDDEN:
- Hidden ^= 1;
- break;
- case FORMAT_PLAIN_ICON:
- break;
- case FORMAT_ICON_LINK:
- break;
- case FORMAT_URL_LINK:
- Link ^= 1;
- if(Link) {
- Find = strchr(Text, ']');
- if(Find) Text = Find+1;
- }
- break;
- case FORMAT_NICK_LINK:
- case FORMAT_COMMAND_LINK:
- Link ^= 1;
- if(Link) {
- Find = strchr(Text, 0xfe);
- if(Find) Text = Find;
- }
- break;
- case FORMAT_CANCEL_COLORS:
- FontFG=IRCColors[1]; FontBG=IRCColors[IRCCOLOR_BG];
- break;
- }
- break;
- default:
- if(k>=0x20) {
- Poke = Text2;
- while((unsigned char)Text[0]>=0x20 && (unsigned char)Text[0]<0xfe)
- *(Poke++) = *(Text++);
- *Poke = 0;
- WhichFont = Bold|(Italic<<1);
- TTF_SizeUTF8(Font->Font[WhichFont], Text2, &TempW, &TempH);
- TextSurface=TTF_RenderUTF8_Shaded(Font->Font[WhichFont],Text2,FontFG,FontBG);
- int PixelWidth = UnicodeLen(Text2, NULL)*Font->Width;
- if(Underline) {
- SDL_Rect Fill = {0, TextSurface->h-2, TextSurface->w, 1};
- SDL_FillRect(TextSurface, &Fill, SDL_MapRGB(TextSurface->format, 0, 0, 0));
- }
- if(Strike) {
- SDL_Rect Fill = {0, TextSurface->h/2, TextSurface->w, 1};
- SDL_FillRect(TextSurface, &Fill, SDL_MapRGB(TextSurface->format, 0, 0, 0));
- }
- sblit(TextSurface, RenderSurface, 0, 0, CurDrawX, 0, TextSurface->w, TextSurface->h);
- CurDrawX += PixelWidth;
- SDL_FreeSurface(TextSurface);
- Text--; // will ++ after the loop
- }
- }
- if(*Text)
- Text++;
- else break;
- }
- SDL_Texture *Texture;
- Texture = SDL_CreateTextureFromSurface(Renderer, RenderSurface);
- if(Mode)
- Mode->DrawnHeight = Font->Height;
- if(!MaxWidth || (MaxWidth >= RenderSurface->w)) { // no wrap
- blit(Texture, Renderer, 0, 0, X, Y, SurfaceWidth, SurfaceHeight);
- } else { // wrap
- int CharsPerRow = MaxWidth/Font->Width; // floor rounding
- //int RowWidth = CharsPerRow * Font->Width;
- int CurRow = 0;
- char *Peek = Stripped;
- int RenderIndex = 0; // character in Texture
- int CharCount = 0; // characters read by Peek that count as characters
- int CharCountWord = 0; // index to the last space
- while(*Peek) {
- if(*Peek<0x80||*Peek>0xbf)
- CharCount++;
- if(*Peek == ' ')
- CharCountWord = CharCount;
- if(!((CharCount-RenderIndex) % CharsPerRow)) {
- blit(Texture, Renderer, RenderIndex*Font->Width, 0, X, Y+((Font->Height+LineVSpace)*CurRow), (CharCountWord-RenderIndex)*Font->Width, SurfaceHeight);
- RenderIndex = CharCountWord;
- CurRow++;
- }
- Peek++;
- }
- blit(Texture, Renderer, RenderIndex*Font->Width, 0, X, Y+((Font->Height+LineVSpace)*CurRow), (CharCount-RenderIndex)*Font->Width, SurfaceHeight);
- if(Mode)
- Mode->DrawnHeight = (Font->Height+LineVSpace)*(CurRow+1);
- }
- SDL_FreeSurface(RenderSurface);
- SDL_DestroyTexture(Texture);
- if(!Mode || !Mode->AlreadyStripped) free(Stripped);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement