1#include "TextElement.hpp" 
    2#include "RootDisplay.hpp" 
    6const char *TextElement::fontPaths[] = {
 
    7    RAMFS 
"./res/fonts/OpenSans-Regular.ttf", 
 
    8    RAMFS 
"./res/fonts/UbuntuMono-Regular.ttf", 
 
    9    RAMFS 
"./res/fonts/oldmono.ttf", 
 
   10    RAMFS 
"./res/fonts/PTSerif-Regular.ttf", 
 
   11    RAMFS 
"./res/fonts/NotoSansSC-Regular.ttf", 
 
   14std::unordered_map<std::string, std::string> TextElement::i18nCache = {};
 
   16bool TextElement::useSimplifiedChineseFont = 
false;
 
   18TextElement::TextElement()
 
   23void TextElement::loadI18nCache(std::string locale) {
 
   25    std::string localePath = RAMFS 
"res/i18n/" + locale + 
".ini";
 
   26    std::ifstream file(localePath);
 
   30        while (std::getline(file, line)) {
 
   31            size_t pos = line.find(
" =");
 
   32            if (pos == std::string::npos) {
 
   35            std::string key = line.substr(0, pos);
 
   36            pos = line.find(
"= ");
 
   37            if (pos == std::string::npos) {
 
   40            std::string value = line.substr(pos + 2);
 
   41            TextElement::i18nCache[key] = value;
 
   47        if (locale == 
"zh-cn") {
 
   48            printf(
"Overriding font choice\n");
 
   49            TextElement::useSimplifiedChineseFont = 
true;
 
   54TextElement::TextElement(std::string text, 
int size, CST_Color* color, 
int font_type, 
int wrapped_width)
 
   56    std::string sText = text;
 
   59    if (color) setColor(*color);
 
   61    setWrappedWidth(wrapped_width);
 
   65void TextElement::setText(
const std::string& text)
 
   70void TextElement::setSize(
int size)
 
   72    this->textSize = size;
 
   75void TextElement::setColor(
const CST_Color& color)
 
   77    this->textColor = color;
 
   80void TextElement::setFont(
int font_type)
 
   82    this->textFont = font_type;
 
   85void TextElement::setWrappedWidth(
int wrapped_width)
 
   87    this->textWrappedWidth = wrapped_width;
 
   92    std::string key = text + std::to_string(textSize);
 
   98        if (TextElement::useSimplifiedChineseFont && textFont == NORMAL) {
 
   99            textFont = SIMPLIFIED_CHINESE;
 
  101        auto fontPath = fontPaths[textFont % 5];
 
  102        if (customFontPath != 
"") {
 
  103            fontPath = customFontPath.c_str();
 
  105        TTF_Font* font = TTF_OpenFont(fontPath, textSize);
 
  107        CST_Surface *textSurface = ((textFont == ICON) || (textWrappedWidth == 0)) ?
 
  108            TTF_RenderUTF8_Blended(font, text.c_str(), textColor) :
 
  109            TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), textColor, textWrappedWidth);
 
  110        if(textSurface==NULL) printf(
"TTF_GetError: %s\n", TTF_GetError());
 
  114        CST_FreeSurface(textSurface);
 
  121std::string i18n(std::string key) {
 
  122    if (
const auto& keyItr = TextElement::i18nCache.find(key); keyItr != TextElement::i18nCache.end()) {
 
  123        return keyItr->second;
 
  128std::string i18n_number(
int number) {
 
  129    std::string decimalSeparator = i18n(
"number.decimal");
 
  130    std::string thousandsSeparator = i18n(
"number.thousands");
 
  131    if (decimalSeparator.empty()) {
 
  132        decimalSeparator = 
".";
 
  134    if (thousandsSeparator.empty()) {
 
  135        thousandsSeparator = 
",";
 
  137    std::string numberString = std::to_string(number);
 
  138    size_t decimalPos = numberString.find(
".");
 
  139    if (decimalPos == std::string::npos) {
 
  140        decimalPos = numberString.length();
 
  142    std::string integerPart = numberString.substr(0, decimalPos);
 
  143    std::string decimalPart = numberString.substr(decimalPos);
 
  144    if (decimalPart.length() > 0) {
 
  145        decimalPart = decimalSeparator + decimalPart.substr(1);
 
  147    for (
int i = integerPart.length() - 3; i > 0; i -= 3) {
 
  148        integerPart.insert(i, thousandsSeparator);
 
  150    return integerPart + decimalPart;
 
  153std::string i18n_date(
int timestamp) {
 
  154    std::string dateFormatString = i18n(
"date.format");
 
  155    if (dateFormatString.empty()) {
 
  156        dateFormatString = 
"%Y-%m-%d";
 
  159    time_t timestamp2 = 
static_cast<time_t
>(timestamp);
 
  161    struct tm* timeinfo = localtime(×tamp2);
 
  163    strftime(buffer, 
sizeof(buffer), dateFormatString.c_str(), timeinfo);
 
  164    return std::string(buffer);
 
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.