1#include "TextElement.hpp"
2#include "RootDisplay.hpp"
5const char *TextElement::fontPaths[] = {
6 RAMFS
"./res/fonts/OpenSans-Regular.ttf",
7 RAMFS
"./res/fonts/UbuntuMono-Regular.ttf",
8 RAMFS
"./res/fonts/oldmono.ttf",
9 RAMFS
"./res/fonts/PTSerif-Regular.ttf",
10 RAMFS
"./res/fonts/NotoSansSC-Regular.ttf",
13std::unordered_map<std::string, std::string> TextElement::i18nCache = {};
15bool TextElement::useSimplifiedChineseFont =
false;
17TextElement::TextElement()
22void TextElement::loadI18nCache(std::string locale) {
24 std::string localePath = RAMFS
"res/i18n/" + locale +
".ini";
25 std::ifstream file(localePath);
29 while (std::getline(file, line)) {
30 size_t pos = line.find(
" =");
31 if (pos == std::string::npos) {
34 std::string key = line.substr(0, pos);
35 pos = line.find(
"= ");
36 if (pos == std::string::npos) {
39 std::string value = line.substr(pos + 2);
40 TextElement::i18nCache[key] = value;
46 if (locale ==
"zh-cn") {
47 printf(
"Overriding font choice\n");
48 TextElement::useSimplifiedChineseFont =
true;
53TextElement::TextElement(std::string text,
int size, CST_Color* color,
int font_type,
int wrapped_width)
55 std::string sText = text;
58 if (color) setColor(*color);
60 setWrappedWidth(wrapped_width);
64void TextElement::setText(
const std::string& text)
69void TextElement::setSize(
int size)
71 this->textSize = size;
74void TextElement::setColor(
const CST_Color& color)
76 this->textColor = color;
79void TextElement::setFont(
int font_type)
81 this->textFont = font_type;
84void TextElement::setWrappedWidth(
int wrapped_width)
86 this->textWrappedWidth = wrapped_width;
91 std::string key = text + std::to_string(textSize);
97 if (TextElement::useSimplifiedChineseFont && textFont == NORMAL) {
98 textFont = SIMPLIFIED_CHINESE;
100 auto fontPath = fontPaths[textFont % 5];
101 if (customFontPath !=
"") {
102 fontPath = customFontPath.c_str();
104 TTF_Font* font = TTF_OpenFont(fontPath, textSize);
106 CST_Surface *textSurface = ((textFont == ICON) || (textWrappedWidth == 0)) ?
107 TTF_RenderUTF8_Blended(font, text.c_str(), textColor) :
108 TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), textColor, textWrappedWidth);
109 if(textSurface==NULL) printf(
"TTF_GetError: %s\n", TTF_GetError());
113 CST_FreeSurface(textSurface);
120std::string i18n(std::string key) {
121 if (
const auto& keyItr = TextElement::i18nCache.find(key); keyItr != TextElement::i18nCache.end()) {
122 return keyItr->second;
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
void update(bool forceUpdate=false)
update TextElement with changes
void getTextureSize(int *w, int *h)
Return texture's original size.
bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface)
Loads the texture from a surface and saves the results in caches Returns true if successful.
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
void clear(void)
Reinitialize Texture Resets texture content, size and color.