Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
TextElement.cpp
1#include "TextElement.hpp"
2#include "RootDisplay.hpp"
3#include <fstream>
4
5const char *TextElement::fontPaths[] = {
6 RAMFS "./res/fonts/OpenSans-Regular.ttf", // 0 = NORMAL
7 RAMFS "./res/fonts/UbuntuMono-Regular.ttf", // 1 = MONOSPACED
8 RAMFS "./res/fonts/oldmono.ttf", // 2 = OLD_MONOSPACED
9 RAMFS "./res/fonts/PTSerif-Regular.ttf", // 3 = SERIF
10 RAMFS "./res/fonts/NotoSansSC-Regular.ttf", // 4 = SIMPLIFIED_CHINESE
11};
12
13std::unordered_map<std::string, std::string> TextElement::i18nCache = {};
14
15bool TextElement::useSimplifiedChineseFont = false;
16
17TextElement::TextElement()
18{
19}
20
21// static method to load i18n cache
22void TextElement::loadI18nCache(std::string locale) {
23 // en-us, zh-cn
24 std::string localePath = RAMFS "res/i18n/" + locale + ".ini";
25 std::ifstream file(localePath);
26 // printf("Loading i18n cache from %s\n", localePath.c_str());
27 if (file.is_open()) {
28 std::string line;
29 while (std::getline(file, line)) {
30 size_t pos = line.find(" =");
31 if (pos == std::string::npos) {
32 continue; // bad format
33 }
34 std::string key = line.substr(0, pos);
35 pos = line.find("= ");
36 if (pos == std::string::npos) {
37 continue;
38 }
39 std::string value = line.substr(pos + 2);
40 TextElement::i18nCache[key] = value;
41 // printf("Loaded i18n key %s with value %s\n", key.c_str(), value.c_str());
42 }
43 file.close();
44
45 // if locale is zh-cn, we need to force the simple chinese font
46 if (locale == "zh-cn") {
47 printf("Overriding font choice\n");
48 TextElement::useSimplifiedChineseFont = true;
49 }
50 }
51}
52
53TextElement::TextElement(std::string text, int size, CST_Color* color, int font_type, int wrapped_width)
54{
55 std::string sText = text;
56 setText(sText);
57 setSize(size);
58 if (color) setColor(*color);
59 setFont(font_type);
60 setWrappedWidth(wrapped_width);
61 update();
62}
63
64void TextElement::setText(const std::string& text)
65{
66 this->text = text;
67}
68
69void TextElement::setSize(int size)
70{
71 this->textSize = size;
72}
73
74void TextElement::setColor(const CST_Color& color)
75{
76 this->textColor = color;
77}
78
79void TextElement::setFont(int font_type)
80{
81 this->textFont = font_type;
82}
83
84void TextElement::setWrappedWidth(int wrapped_width)
85{
86 this->textWrappedWidth = wrapped_width;
87}
88
89void TextElement::update(bool forceUpdate)
90{
91 std::string key = text + std::to_string(textSize);
92
93 clear();
94
95 if (!loadFromCache(key) || forceUpdate)
96 {
97 if (TextElement::useSimplifiedChineseFont && textFont == NORMAL) {
98 textFont = SIMPLIFIED_CHINESE;
99 }
100 auto fontPath = fontPaths[textFont % 5];
101 if (customFontPath != "") {
102 fontPath = customFontPath.c_str();
103 }
104 TTF_Font* font = TTF_OpenFont(fontPath, textSize);
105
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());
110
111 loadFromSurfaceSaveToCache(key, textSurface);
112
113 CST_FreeSurface(textSurface);
114 TTF_CloseFont(font);
115 }
116
117 getTextureSize(&width, &height);
118}
119
120std::string i18n(std::string key) {
121 if (const auto& keyItr = TextElement::i18nCache.find(key); keyItr != TextElement::i18nCache.end()) {
122 return keyItr->second;
123 }
124 return key;
125}
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:118
void update(bool forceUpdate=false)
update TextElement with changes
Definition: TextElement.cpp:89
void getTextureSize(int *w, int *h)
Return texture's original size.
Definition: Texture.cpp:198
bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface)
Loads the texture from a surface and saves the results in caches Returns true if successful.
Definition: Texture.cpp:91
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:76
void clear(void)
Reinitialize Texture Resets texture content, size and color.
Definition: Texture.cpp:9