unsigned int getTextHeight(const fontParams_s * par, const std::string & t) { std::vector<std::string> words = split(t.c_str(), ' '); unsigned int word = 0; int height = 0; while (word < words.size()) { std::string curLine = words[word]; word++; while (word < words.size()) { int w; TTF_SizeUTF8(fonts[par->font], (curLine+words[word]).c_str(), &w, 0); if (w > par->box.w) break; curLine = curLine + " " + words[word]; word++; } int h, w; TTF_SizeUTF8(fonts[par->font], curLine.c_str(), &w, &h); height += h; } return height; }
int MLIFont::GetKernedWidth(uint32_t c1, uint32_t c2) { EnsureUIThread(); CheckScale(); pair<uint32_t, uint32_t> charPair(c1, c2); map<pair<uint32_t, uint32_t>, int>::const_iterator it = kernedWidthCache.find(charPair); if (it != kernedWidthCache.end()) { return it->second; } else { int h; int combinedWidth, w2; string str1, str2; utf8::unchecked::append(c1, back_inserter(str1)); utf8::unchecked::append(c2, back_inserter(str2)); TTF_SizeUTF8(pTtfFont, str2.c_str(), &w2, &h); TTF_SizeUTF8(pTtfFont, (str1 + str2).c_str(), &combinedWidth, &h); int kernedWidth1 = (combinedWidth - w2) + strokeWidth/* * 2*/; kernedWidthCache[charPair] = kernedWidth1; return kernedWidth1; } }
void Redraw() { int w = 0, h = textRect.h; SDL_Rect cursorRect, underlineRect; SDL_FillRect(screen, &textRect, backColor); #ifdef HAVE_SDL_TTF if (strlen(text)) { RenderText(screen, font, text, textRect.x, textRect.y, textColor); TTF_SizeUTF8(font, text, &w, &h); } #endif markedRect.x = textRect.x + w; markedRect.w = textRect.w - w; if (markedRect.w < 0) { SDL_Flip(screen); // Stop text input because we cannot hold any more characters SDL_StopTextInput(); return; } else { SDL_StartTextInput(); } cursorRect = markedRect; cursorRect.w = 2; cursorRect.h = h; SDL_FillRect(screen, &markedRect, backColor); if (markedText) { #ifdef HAVE_SDL_TTF RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); TTF_SizeUTF8(font, markedText, &w, &h); #endif underlineRect = markedRect; underlineRect.y += (h - 2); underlineRect.h = 2; underlineRect.w = w; cursorRect.x += w + 1; SDL_FillRect(screen, &underlineRect, lineColor); } SDL_FillRect(screen, &cursorRect, lineColor); SDL_Flip(screen); SDL_SetTextInputRect(&markedRect); }
FontHelper::FontHelper(const string &font, int size, RGBAColor textColor, RGBAColor outlineColor) { this->textColor = textColor; this->outlineColor = outlineColor; if (!TTF_WasInit()) { DEBUG("Initializing font"); if (TTF_Init() == -1) { ERROR("TTF_Init: %s", TTF_GetError()); exit(2); } } this->font = TTF_OpenFont(font.c_str(), size); if (!this->font) { ERROR("TTF_OpenFont %s: %s", font.c_str(), TTF_GetError()); exit(2); } fontOutline = TTF_OpenFont(font.c_str(), size); if (!fontOutline) { ERROR("TTF_OpenFont %s: %s", font.c_str(), TTF_GetError()); exit(2); } TTF_SetFontHinting(this->font, TTF_HINTING_NORMAL); TTF_SetFontHinting(fontOutline, TTF_HINTING_NORMAL); TTF_SetFontOutline(fontOutline, 1); height = 0; // Get maximum line height with a sample text TTF_SizeUTF8(fontOutline, "AZ|¹0987654321", NULL, &height); halfHeight = height/2; }
std::pair<int, int> Typeface::getTextBounds(const std::string& s) { int w, h; if (TTF_SizeUTF8(font, s.c_str(), &w, &h) == -1) { throw std::runtime_error(TTF_GetError()); } return std::make_pair(w, h); }
IntRect Bitmap::textSize(const char *str) { guardDisposed(); GUARD_MEGA; TTF_Font *font = p->font->getSdlFont(); std::string fixed = fixupString(str); str = fixed.c_str(); int w, h; TTF_SizeUTF8(font, str, &w, &h); /* If str is one character long, *endPtr == 0 */ const char *endPtr; uint16_t ucs2 = utf8_to_ucs2(str, &endPtr); /* For cursive characters, returning the advance * as width yields better results */ if (p->font->getItalic() && *endPtr == '\0') TTF_GlyphMetrics(font, ucs2, 0, 0, 0, 0, &w); return IntRect(0, 0, w, h); }
bool Renderer::renderText(const std::string &message, const std::string &font, SDL_Color color, int size, int top, int left) { TTF_Font *ttffont = nullptr; ttffont = TTF_OpenFont(font.c_str(), size); if (ttffont == nullptr) throw std::runtime_error("Failed to load font: " + font + TTF_GetError()); SDL_Surface *surface = TTF_RenderUTF8_Blended(ttffont, message.c_str(), color); SDL_Texture *texture = SDL_CreateTextureFromSurface(Renderer::g_renderer, surface); SDL_Rect destination = { 0, 0, 0, 0 }; destination.x = left; destination.y = top; TTF_SizeUTF8(ttffont, message.c_str(), &destination.w, &destination.h); // Draw SDL_RenderCopy(Renderer::g_renderer, texture, NULL, &destination); // Close SDL_FreeSurface(surface); TTF_CloseFont(ttffont); return true; }
void SDL2Renderer::metrics(const NP::Font &font, const char *text, int *width, int *height) { SDLFontData *data = static_cast<SDLFontData *>(font.get()); TTF_SizeUTF8(data->m_font, text, width, height); }
static int draw_part_text(SDL_Surface *sur, int state, const char *text, int len, int x, int y, SDL_Color color) { char buf[MAX_CHAR_BUF]; TTF_Font *font = state ? chinese_font : english_font; int w = 0, h = 0; if (len >= MAX_CHAR_BUF || len <= 0) return 0; strncpy(buf, text, len); buf[len] = 0; render_text(sur, font, buf, x, y, color); TTF_SizeUTF8(font, buf, &w, &h); fprintf(stderr, "render_text(%s, %s, %d, %d, [%d, %d, %d])\n", state == STATE_CHINESE ? "ch" : "en", buf, x, y, color.r, color.g, color.b); return w; }
int Font::GetWidth(const std::string & txt) const { int width=-1; TTF_SizeUTF8(m_font, txt.c_str(), &width, NULL); return width; }
JNIEXPORT jint JNICALL Java_sdljava_x_swig_SWIG_1SDLTTFJNI_TTF_1SizeUTF8(JNIEnv *jenv, jclass jcls, jlong jarg1, jstring jarg2, jintArray jarg3, jintArray jarg4) { jint jresult = 0 ; TTF_Font *arg1 = (TTF_Font *) 0 ; char *arg2 ; int *arg3 = (int *) 0 ; int *arg4 = (int *) 0 ; int result; int temp3 ; int temp4 ; (void)jenv; (void)jcls; arg1 = *(TTF_Font **)&jarg1; { arg2 = 0; if (jarg2) { arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); if (!arg2) return 0; } } { if (!jarg3) { SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null"); return 0; } if ((*jenv)->GetArrayLength(jenv, jarg3) == 0) { SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element"); return 0; } arg3 = &temp3; } { if (!jarg4) { SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null"); return 0; } if ((*jenv)->GetArrayLength(jenv, jarg4) == 0) { SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element"); return 0; } arg4 = &temp4; } result = (int)TTF_SizeUTF8(arg1,(char const *)arg2,arg3,arg4); jresult = (jint)result; { jint jvalue = (jint)temp3; (*jenv)->SetIntArrayRegion(jenv, jarg3, 0, 1, &jvalue); } { jint jvalue = (jint)temp4; (*jenv)->SetIntArrayRegion(jenv, jarg4, 0, 1, &jvalue); } { if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, arg2); } return jresult; }
int getTextWidth(XFontStruct* font_struct, const char* string) { int width, height; if (TTF_SizeUTF8(GET_FONT(font_struct->fid), string, &width, &height) != 0) { LOG("Failed to calculate the text with in XTextWidth[16]: %s! " "Returning max width of font.\n", TTF_GetError()); return (int) (font_struct->max_bounds.rbearing * strlen(string)); } return width; }
double MLIFont::GetHeight(const string &s) { EnsureUIThread(); CheckScale(); int w, h; TTF_SizeUTF8(pTtfFont, s.c_str(), &w, &h); return (double)h / GetFontScale(); }
/******************************************************************************\ Returns the bounding box of the rendered text. \******************************************************************************/ c_vec2_t R_font_size(r_font_t font, const char *text) { int w, h; if (!fonts[font].ttf_font) return C_vec2(0.f, 0.f); TTF_SizeUTF8(fonts[font].ttf_font, text, &w, &h); return C_vec2((float)w, (float)h); }
static Point vtxtdims(const Txt *t, const char *fmt, va_list ap) { char s[Bufsize + 1]; vsnprintf(s, Bufsize + 1, fmt, ap); int w, h; (void)TTF_SizeUTF8(t->font, s, &w, &h); return (Point){ w, h }; }
float font_impl::calc_width(const string & str) const { int w, h; if (TTF_SizeUTF8(static_cast<TTF_Font *>(ttf_font_ptr), str.c_string(), &w, &h) != -1) return static_cast<float>(w); else throw runtime_exception(L"%hs", TTF_GetError()); } // font_impl::calc_width()
std::string renderer::format(const std::string& text, int font_size, int width) { const font_ptr font(get_font(font_size)); if(!font) { return text; } int space_width, junk; TTF_SizeUTF8(font.get(), " ", &space_width, &junk); std::string formatted; std::vector<std::string> lines = util::split(text, '\n'); std::vector<std::string>::iterator itor; for(itor = lines.begin(); itor != lines.end(); ++itor) { int line_words = 0; int line_width = 0; std::string& text_line = *itor; std::vector<std::string> words = util::split(text_line, "\n\t "); foreach(const std::string& word, words) { int w; if(TTF_SizeUTF8(font.get(), word.c_str(), &w, &junk) < 0) { std::cerr << "Warning: error rendering text \"" << text_line << "\"\n"; return text; } if(line_words > 0) { if(line_width + w > width) { formatted.append(1, '\n'); line_width = 0; line_words = 0; } else { formatted.append(1, ' '); } } formatted.append(word); line_width += w + space_width; ++line_words; } if(!formatted.empty() && itor+1 != lines.end()) { formatted.append(1,'\n'); } }
void TFont_ttf::dimensions(const char *s,int *w, int *h) { if (ttf) { if (is_utf) TTF_SizeUTF8(ttf,s,w,h); else TTF_SizeText(ttf,s,w,h); } else { TFont::dimensions(s,w,h); } }
void TamanoTexto(GRF_Fuente fuente, const char *mensaje, int &ancho, int &alto) { assert(fuente); ancho = alto = 0; if (mensaje) { if (TTF_SizeUTF8(fuente,mensaje,&ancho,&alto)) { cerr << "No se puede calcular el tamaño del texto ("<<TTF_GetError()<<")"<<endl; } } }
//------------------------------------------------------------------------------ Vector2ui Font::estimate(const std::string& text) const { if (! data) return Vector2ui(0, 0); int result = 0; int w = 0; int h = 0; if (text.empty()) result = TTF_SizeUTF8(data->font, " ", &w, &h); else result = TTF_SizeUTF8(data->font, text.c_str(), &w, &h); if (result == 0) return Vector2ui(w, h); else throw std::logic_error(TTF_GetError()); }
void game_render(SDL_Surface *buffer, camera *camera, PLAYERS *players, float interpolation) { int w, h; SDL_Rect terrain_rect = {gsl_vector_get(camera->vector, 0), gsl_vector_get(camera->vector, 1), WIDTH * ZOOM_LEVEL, HEIGHT * ZOOM_LEVEL}; SDL_BlitSurface(background, &terrain_rect, buffer, NULL); player *pl; division *div; unit *un; for(int i = 0; i < players->num; i++) { pl = players->players[i]; for(int j = 0; j < pl->num_divisions; j++) { div = pl->divisions[j]; for(int k = 0; k < div->size; k++) { un = div->units[k]; display_unit(buffer, camera, un, pl->color, interpolation); /* displays the closest node ai_node *closest = find_closest_node(un->vector); if(closest == NULL) { printf("couldn't find a node for (%f, %f)\n", gsl_vector_get(un->vector, 0), gsl_vector_get(un->vector, 1)); } else { //printf("closest to (%f, %f) -> (%d, %d)\n", // gsl_vector_get(un->vector, 0), // gsl_vector_get(un->vector, 1), // closest->x, closest->y); gsl_vector *v = calculate_display_position(closest->x, closest->y, camera); filledCircleRGBA(buffer, gsl_vector_get(v, 0), gsl_vector_get(v, 1), 3, 0, 0xff, 0, 0xff); gsl_vector_free(v); }*/ } } } // display the title in the upper left SDL_BlitSurface(title, NULL, buffer, NULL); // display the zoom level in the bottom left char zoom[7]; snprintf(zoom, 7, "Zoom %i", ZOOM_LEVEL); TTF_SizeUTF8(font, zoom, &w, &h); SDL_Surface *zoom_surf = draw_text(zoom); SDL_Rect zo = { 0, HEIGHT - h, w, h }; SDL_BlitSurface(zoom_surf, NULL, buffer, &zo); SDL_FreeSurface(zoom_surf); if(paused) { paused_state.render(buffer, camera, players, interpolation); } else { update_camera_position(camera); } }
int Font::textHeight(const std::string& text, FontSize size) { if (text.empty()) return 0; int h; switch (size) { case SMALL: TTF_SizeUTF8(smallFont, text.c_str(), NULL, &h); break; case NORMAL: TTF_SizeUTF8(normalFont, text.c_str(), NULL, &h); break; case LARGE: break; } return h; }
static int hiragana_A_height(TTF_Font* wfont) { int w, h; //HIRAGANA LETTER A utf-8 is 0xe3,0x81, 0x82 //not the small A. char s[10]; s[0]=0xe3; s[1]=0x81; s[2]=0x82; s[3]=0; TTF_SizeUTF8(wfont, s, &w, &h); return h; }
int Font::textWidth(const std::string& text, FontSize size) { if (text.empty()) return 0; int w; switch (size) { case SMALL: TTF_SizeUTF8(smallFont, text.c_str(), &w, NULL); break; case NORMAL: TTF_SizeUTF8(normalFont, text.c_str(), &w, NULL); break; case LARGE: break; } return w; }
float CText::GetStringWidth(const std::string &text, FontType font, float size) { assert(font != FONT_BUTTON); // TODO: special chars? CachedFont* cf = GetOrOpenFont(font, size); assert(cf != nullptr); Math::IntPoint wndSize; TTF_SizeUTF8(cf->font, text.c_str(), &wndSize.x, &wndSize.y); Math::Point ifSize = m_engine->WindowToInterfaceSize(wndSize); return ifSize.x; }
void WrapSingleLine ( const std::string& line, std::vector<std::string>& result, TTF_Font* textFont, int maxWidth ) { int w; if ( TTF_SizeUTF8 ( textFont, line.c_str(), &w, 0 ) ) return; // Silently ignoring errors ? if ( w <= maxWidth ) { result.push_back ( line ); return; } std::string right_part = line; std::string::size_type pos = right_part.size(); while ( pos > 0 ) { pos = right_part.find_last_of ( ' ', pos ); if ( pos == std::string::npos ) { // Word wrap failed, bailing out result.push_back ( right_part ); return; } pos = right_part.find_last_not_of ( ' ', pos ); if ( pos == std::string::npos ) { // Word wrap failed, bailing out result.push_back ( right_part ); return; } std::string left_part = right_part.substr ( 0, pos+1 ); if ( TTF_SizeUTF8 ( textFont, left_part.c_str(), &w, 0 ) ) return; // Silently ignoring errors ? if ( w <= maxWidth ) { result.push_back ( left_part ); right_part = right_part.substr ( pos+2, std::string::npos ); pos = std::string::npos; } } }
static VALUE Font_get_size(VALUE self, VALUE rbText) { const Font* font; Data_Get_Struct(self, Font, font); const char* text = StringValueCStr(rbText); int width, height; if (TTF_SizeUTF8(font->sdlFont, text, &width, &height)) { rb_raise_sdl_ttf_error(); } volatile VALUE rbSize = rb_assoc_new(INT2NUM(width), INT2NUM(height)); OBJ_FREEZE(rbSize); return rbSize; }
virtual SDL_Point StringSize(std::string text, int width) const { SDL_Point p; int numLines; if (width < 0) { TTF_SizeUTF8(ttfFont.get(), text.c_str(), &p.x, &p.y); } else { TTF_SizeUTF8_Wrapped(ttfFont.get(), text.c_str(), width, &p.x, &p.y, &numLines); } return p; }
int BEE::Font::get_string_height(std::string text, int size) { if (is_loaded) { int h = 0; if (size == font_size) { TTF_SizeUTF8(font, text.c_str(), nullptr, &h); } return h; } if (!has_draw_failed) { game->messenger_send({"engine", "font"}, BEE_MESSAGE_WARNING, "Failed to draw text with \"" + name + "\" because it is not loaded"); has_draw_failed = true; } return -1; }
static int text_width_in_state(int state, const char *text, int len) { char buf[MAX_CHAR_BUF]; TTF_Font *font = state ? chinese_font : english_font; int w, h; if (len >= MAX_CHAR_BUF || len <= 0) return 0; strncpy(buf, text, len); buf[len] = 0; TTF_SizeUTF8(font, buf, &w, &h); return w; }