80 #include "render/graphics.h"
90 #include "utils/timer.h"
117 if (TTF_Init() == -1)
120 std::string(SDL_GetError()));
126 reportAlways(
"Error: requested load font %s with size %d",
145 if (
mFont ==
nullptr)
147 logger->
log(
"Error normal loading font " + filename);
149 filename =
"fonts/dejavusans.ttf";
151 if (
mFont ==
nullptr)
158 std::string(SDL_GetError()));
162 logger->
log(
"Loaded fallback font %s, %d",
174 TTF_SetFontStyle(
mFont, style);
179 TTF_CloseFont(
mFont);
198 logger->
log(
"Loading virtfs font file: %s",
200 return TTF_OpenFontIndexRW(rw, 1,
size, 0);
213 logger->
log(
"Loading physical font file: %s",
215 return TTF_OpenFontIndex(path.c_str(),
225 logger->
log(
"Unable to initialize SDL_ttf: " +
226 std::string(SDL_GetError()));
231 TTF_Font *
const font =
openFont(filename.c_str(),
size);
236 std::string(SDL_GetError()));
240 if (
mFont !=
nullptr)
241 TTF_CloseFont(
mFont);
244 TTF_SetFontStyle(
mFont, style);
257 const std::string &text,
258 const int x,
const int y)
269 const float alpha =
static_cast<float>(col.
a) / 255.0F;
276 const unsigned char chr = text[0];
279 std::map<TextChunkSmall, TextChunk*> &search = cache->
search;
280 std::map<TextChunkSmall, TextChunk*>::iterator i
282 if (i != search.end())
287 if (image !=
nullptr)
289 image->setAlpha(alpha);
297 #ifdef DEBUG_FONT_COUNTERS
303 #ifdef DEBUG_FONT_COUNTERS
312 const Image *
const image = chunk2->
img;
313 if (image !=
nullptr)
339 const unsigned char chr = text[0];
342 std::map<std::string, TextChunk*> &search = cache->
searchWidth;
343 std::map<std::string, TextChunk*>::iterator i = search.find(text);
344 if (i != search.end())
348 const Image *
const image = chunk->
img;
349 if (image !=
nullptr)
350 return image->getWidth();
364 return TTF_FontHeight(
mFont);
373 #ifdef DEBUG_FONT_COUNTERS
379 #ifdef DEBUG_FONT_COUNTERS
384 #ifdef DEBUG_FONT_COUNTERS
390 #ifdef DEBUG_FONT_COUNTERS
395 #ifdef DEBUG_FONT_COUNTERS
401 #ifdef DEBUG_FONT_COUNTERS
406 #ifdef DEBUG_FONT_COUNTERS
415 const size_t sz = text.size();
416 for (
size_t i = 0; i < sz; ++i)
432 const std::string &text = chunk.
text;
436 const unsigned char chr = text[0];
440 const int oldAlpha = col.
a;
444 std::map<TextChunkSmall, TextChunk*> &search = cache->
search;
445 std::map<TextChunkSmall, TextChunk*>::iterator i = search.find(key);
446 if (i != search.end())
453 chunk2->
img =
nullptr;
461 #ifdef DEBUG_FONT_COUNTERS
467 #ifdef DEBUG_FONT_COUNTERS
471 const float alpha =
static_cast<float>(chunk.
color.
a) / 255.0F;
480 if ((chunk ==
nullptr) || chunk->
text.empty() || (chunk->
img ==
nullptr))
484 const unsigned char chr = chunk->
text[0];
487 std::map<TextChunkSmall, TextChunk*> &search = cache->
search;
488 std::map<TextChunkSmall, TextChunk*>::iterator i
491 if (i != search.end())
#define reportAlways(...)
const TextChunkList * getCache() const A_CONST
void slowLogic(const int rnd)
void loadFont(std::string filename, const int size, const int style)
unsigned int mDeleteCounter
unsigned int mCreateCounter
void generate(TextChunk &chunk)
int getStringIndexAt(const std::string &text, const int x) const
void insertChunk(TextChunk *const chunk)
Font(std::string filename, int size, const int style)
int getWidth(const std::string &text) const
TextChunkList mCache[CACHES_NUMBER]
static TTF_Font * openFont(const char *const name, const int size)
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
virtual RenderType useOpenGL() const
void log(const char *const log_text,...)
void error(const std::string &error_text) __attribute__((noreturn))
void insertFirst(TextChunk *const item)
void remove(const TextChunk *const item)
std::map< TextChunkSmall, TextChunk * > search
std::map< std::string, TextChunk * > searchWidth
void moveToFirst(TextChunk *const item)
void generate(TTF_Font *const font, const float alpha)
const unsigned int CACHE_SIZE
const unsigned int CACHE_SIZE_SMALL1
const unsigned int CACHE_SIZE_SMALL3
const unsigned int CACHE_SIZE_SMALL2
const unsigned int CLEAN_TIME
const unsigned int CACHES_NUMBER
ImageHelper * imageHelper
bool existsLocal(const std::string &path)
std::string getPath(const std::string &file)
SDL_RWops * rwopsOpenRead(const std::string &fname)
std::string & fixDirSeparators(std::string &str)
#define BLOCK_START(name)
const char * getSafeUtf8String(const std::string &text)