bool ttext::set_text(const std::string& text, const bool markedup) { if(markedup != markedup_text_ || text != text_) { assert(layout_); const wide_string wide = utils::string_to_wstring(text); const std::string narrow = utils::wstring_to_string(wide); if(text != narrow) { ERR_GUI_L << "ttext::" << __func__ << " text '" << text << "' contains invalid utf-8, trimmed the invalid parts.\n"; } if(markedup) { if(!set_markup(narrow)) { return false; } } else { /* * pango_layout_set_text after pango_layout_set_markup might * leave the layout in an undefined state regarding markup so * clear it unconditionally. */ pango_layout_set_attributes(layout_, NULL); pango_layout_set_text(layout_, narrow.c_str(), narrow.size()); } text_ = narrow; length_ = wide.size(); markedup_text_ = markedup; calculation_dirty_ = true; surface_dirty_ = true; } return true; }
std::string WideToUTF8(const wide_string& wide) { char * narrow = new char[wide.length()*3+2]; int ret = (int)WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), (int)wide.size(), narrow, (int)wide.length()*3 - 1, NULL, NULL); narrow[ret] = 0; std::string str = narrow; delete[] narrow; return str; }
static void add_prefixes(const wide_string& str, size_t length, markov_prefix_map& res) { for(size_t i = 0; i <= str.size(); ++i) { const size_t start = i > length ? i - length : 0; const wide_string key(str.begin() + start, str.begin() + i); const wchar_t c = i != str.size() ? str[i] : 0; res[key].push_back(c); } }
std::string WideToMultibyte(const wide_string& wide) { char * narrow = new char[wide.length()*3+2]; BOOL defaultCharUsed; int ret = (int)WideCharToMultiByte(CP_ACP, 0, wide.c_str(), (int)wide.size(), narrow, (int)wide.length()*3 - 1, ".", &defaultCharUsed); narrow[ret] = 0; std::string str = narrow; delete[] narrow; return str; }
std::string CUnicodeUtils::StdGetUTF8(const wide_string& wide) { int len = (int)wide.size(); if (len==0) return std::string(); int size = len*4; char * narrow = new char[size]; int ret = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), len, narrow, size-1, NULL, NULL); narrow[ret] = 0; std::string sRet = std::string(narrow); delete [] narrow; return sRet; }
unsigned ttext::insert_unicode(const unsigned offset, const wide_string& unicode) { assert(offset <= length_); if(length_ == maximum_length_) { return 0; } const unsigned len = length_ + unicode.size() > maximum_length_ ? maximum_length_ - length_ : unicode.size(); wide_string tmp = utils::string_to_wstring(text_); tmp.insert(tmp.begin() + offset, unicode.begin(), unicode.begin() + len); set_text(utils::wstring_to_string(tmp), false); return len; }
surface textbox::add_text_line(const wide_string& text, const SDL_Color& color) { line_height_ = font::get_max_height(font_size); if(char_y_.empty()) { char_y_.push_back(0); } else { char_y_.push_back(char_y_.back() + line_height_); } char_x_.push_back(0); // Re-calculate the position of each glyph. We approximate this by asking the // width of each substring, but this is a flawed assumption which won't work with // some more complex scripts (that is, RTL languages). This part of the work should // actually be done by the font-rendering system. std::string visible_string; wide_string wrapped_text; wide_string::const_iterator backup_itor = text.end(); wide_string::const_iterator itor = text.begin(); while(itor != text.end()) { //If this is a space, save copies of the current state so we can roll back if(char(*itor) == ' ') { backup_itor = itor; } visible_string.append(utils::wchar_to_string(*itor)); if(char(*itor) == '\n') { backup_itor = text.end(); visible_string = ""; } int w = font::line_width(visible_string, font_size); if(wrap_ && w >= inner_location().w) { if(backup_itor != text.end()) { int backup = itor - backup_itor; itor = backup_itor + 1; if(backup > 0) { char_x_.erase(char_x_.end()-backup, char_x_.end()); char_y_.erase(char_y_.end()-backup, char_y_.end()); wrapped_text.erase(wrapped_text.end()-backup, wrapped_text.end()); } } backup_itor = text.end(); wrapped_text.push_back(wchar_t('\n')); char_x_.push_back(0); char_y_.push_back(char_y_.back() + line_height_); visible_string = ""; } else { wrapped_text.push_back(*itor); char_x_.push_back(w); char_y_.push_back(char_y_.back() + (char(*itor) == '\n' ? line_height_ : 0)); ++itor; } } const std::string s = utils::wstring_to_string(wrapped_text); const surface res(font::get_rendered_text(s, font_size, color)); return res; }