void draw_text(GuiCanvas *canvas, GuiText *text) { #ifdef USE_TTF SDL_Surface *surface; SDL_Color color; SDL_Rect rect; if (text == NULL || gui == NULL) return; if (text->font == NULL) return; surface = (SDL_Surface *)canvas; COLOR_SDL(color, text->color); RECT_SDL (rect, text->rect); if (text->font->fonthandle == NULL) text->font->fonthandle = TTF_OpenFont(text->font->filename, FONT_SIZE); if (text->font->fonthandle == NULL) return; if (text->surface == NULL && text->unicode) { uint16_t buffer[BUF_LEN]; UTF8_to_UNICODE(buffer, text->unicode); text->surface = TTF_RenderUNICODE_Blended((TTF_Font *)text->font->fonthandle, buffer, color); } if (text->surface) SDL_BlitSurface(text->surface, NULL, surface, &rect); #endif }
void sge_TTF_SizeText( _sge_TTFont*font, const char* text, int* x, int* y ) { #ifdef MSZ_USES_UTF8 Uint16 *unicode_text; int unicode_len; /* Copy the UTF-8 text to a UNICODE text buffer */ unicode_len = strlen(text); unicode_text = new Uint16[unicode_len+1]; // (Uint16 *)malloc( (unicode_len+1) * sizeof (Uint16) ); if ( unicode_text == NULL ) { SDL_SetError("SGE - Out of memory"); *x = *y = 0; return; } UTF8_to_UNICODE(unicode_text, text, unicode_len); /* Render the new text */ SDL_Rect r = sge_TTF_TextSizeUNI(font, unicode_text); /* Free the text buffer and return */ delete[] unicode_text; //free(unicode_text); #else SDL_Rect r = sge_TTF_TextSize( font, text ); #endif *x = r.w; *y = r.h; }
static int GetCharacterOffsets(void *_font, const char *text, int *byteOffsets, int *pixelOffsets, int maxOffsets) { TTF_Font *font = (TTF_Font *)_font; int i = 0; int bytes = 0; int pixels = 0; int advance; Uint16 ch; while ( *text && i < maxOffsets ) { byteOffsets[i] = bytes; pixelOffsets[i] = pixels; ++i; ch = UTF8_to_UNICODE(text, &advance); text += advance; bytes += advance; TTF_GlyphMetrics(font, ch, NULL, NULL, NULL, NULL, &advance); pixels += advance; } if ( i < maxOffsets ) { byteOffsets[i] = bytes; pixelOffsets[i] = pixels; } return i; }
wchar_t* charToWideChar(const char* strChar) { wchar_t *strWChar = new wchar_t[strlen(strChar) + 1]; if (!strWChar) return NULL; UTF8_to_UNICODE(strWChar,strChar,strlen(strChar)); return strWChar; }
void rtf_update() { static char buf[1032]; char *s, *p, *q; int i, j, k; unsigned int u; FILE *f; if (NULL == (f=fopen("/tmp/route.rtf","w"))) return; // Set Code Page 1252 for latin1. Not sure if that or utf8 here... fprintf(f,"{\\rtf1\\ansi\\ansicpg1252\\deff0 {\\fonttbl {\\f0 Courier;}}\n"); fprintf(f,"{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green0\\blue255;}\n"); for (s = strtok(txtbuf, "\n"); s; s = strtok(NULL, "\n")){ //j = strlen(s); if (j >1031) j = 1031; q = buf; for (k = 0; *s; s+=i) { u = UTF8_to_UNICODE(s, &i); if (u < 0x80) q[k++] = u; // ok for latin1, although not really RTF standard. else{ if (u < 0x100) sprintf(q+k, "\\u%d%c", u, u); // Maybe try a codepage escape? else sprintf(q+k, "\\u%d?", u); k += strlen(q+k); } } q[k] = 0; // // Need to change utf8 to \\uNNNN? sequence and \,{.} to \\,\{,\} sequences. //if ((*p == '\\') ||(*p == '}') ||(*p == '}')) // *q++ = '\\'; // p = buf; if (p[0] == '(') fprintf(f, "\\cf3\n%s\\line\n\\cf1\n",p); else if (p[0] == ' ') fprintf(f, "\\cf2\n%s\\line\n\\cf1\n",p); else fprintf(f, "%s\\line\n",p); p[strlen(p)] = '\n'; // Replace nulls added by strtok. } fprintf(f,"}\n"); fclose(f); }
int TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h) { Uint16 *unicode_text; int unicode_len; int status; /* 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 ) { SDL_SetError("Out of memory"); return -1; } UTF8_to_UNICODE(unicode_text, text, unicode_len); /* Render the new text */ status = TTF_SizeUNICODE(font, unicode_text, w, h); /* Free the text buffer and return */ free(unicode_text); return status; }
int get_text_width(FontDesc *font, const char *text) { if (font == NULL) return 0; #ifdef USE_TTF uint16_t cBuf[BUF_LEN]; const char *i18n_text = i18n(text); memset(cBuf, '\0', BUF_LEN); UTF8_to_UNICODE(cBuf, i18n_text); if (font->fonthandle == NULL) font->fonthandle = TTF_OpenFont(font->filename, FONT_SIZE); if (font->fonthandle) { int w = 0, h = 0; TTF_SizeUNICODE((TTF_Font *)font->fonthandle, cBuf, &w, &h); return w; } #else return 24; #endif return 0; }
/* Convert the UTF-8 text to UNICODE and render it */ SDL_Surface *TTF_RenderUTF8_Solid(TTF_Font *font, const char *text, SDL_Color fg) { 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 ) { SDL_SetError("Out of memory"); return(NULL); } UTF8_to_UNICODE(unicode_text, text, unicode_len); /* Render the new text */ textbuf = TTF_RenderUNICODE_Solid(font, unicode_text, fg); /* Free the text buffer and return */ free(unicode_text); return(textbuf); }