Exemplo n.º 1
0
shared_string make_text_ellipsis(const shared_string &text, int font_size,
		int max_width, bool with_tags, bool parse_for_style)
{
	static const std::string ellipsis = "...";

	SDL_Color unused_color;
	int unused_int;
	int style = TTF_STYLE_NORMAL;
	if(parse_for_style) parse_markup(text.begin(), text.end(), &unused_int, &unused_color, &style);

	if(line_width(with_tags ? text : shared_string(del_tags(text)), font_size, style) <= max_width)
		return text;
	if(line_width(ellipsis, font_size, style) > max_width)
		return "";

	std::string current_substring;

	utils::utf8_iterator itor(text);

	for(; itor != utils::utf8_iterator::end(text); ++itor) {
		std::string tmp = current_substring;
		tmp.append(itor.substr().first, itor.substr().second);
		tmp += ellipsis;

		if (line_width(with_tags ? tmp : del_tags(tmp), font_size, style) > max_width) {
			return current_substring + ellipsis;
		}

		current_substring.append(itor.substr().first, itor.substr().second);
	}

	return text; // Should not happen
}
Exemplo n.º 2
0
inline std::string to_string( const shared_string& str ) { return std::string( str.begin(), str.end() ); }