Ejemplo n.º 1
0
Size Font::get_text_size(Canvas &canvas, const std::string &text)
{
	Size total_size;

	if (impl)
	{
		FontMetrics fm = get_font_metrics();
		int line_spacing = fm.get_external_leading();
		std::vector<std::string> lines = StringHelp::split_text(text, "\n", false);
		for (std::vector<std::string>::size_type i=0; i<lines.size(); i++)
		{
			Size line_size = impl->get_text_size(canvas, lines[i]);

			if ((line_size.width == 0) && (line_size.height == 0) && (lines.size() > 1)) // blank line
				line_size.height = fm.get_descent() + fm.get_ascent(); 

			if ((i+1) != lines.size())	// Do not add the line spacing on the last line
				line_size.height += line_spacing;

			if (total_size.width < line_size.width)	// Find the widest line
				total_size.width = line_size.width;

			total_size.height += line_size.height;
		}
	}
	
	return total_size;
}
Ejemplo n.º 2
0
void Font::draw_text_ellipsis(Canvas &canvas, float dest_x, float dest_y, Rectf content_box, const std::string &text, const Colorf &color)
{
	if (impl)
	{
		FontMetrics fm = get_font_metrics();
		int ascent = fm.get_ascent();
		int descent = fm.get_descent();
		int line_spacing = fm.get_height() + fm.get_external_leading();
		std::vector<std::string> lines = StringHelp::split_text(text, "\n", false);
		for (std::vector<std::string>::size_type i=0; i<lines.size(); i++)
		{
			if (i == 0 || (dest_y - ascent >= content_box.top && dest_y + descent < content_box.bottom))
			{
				Size size = get_text_size(canvas, lines[i]);
				if (dest_x + size.width <= content_box.right)
				{
					draw_text(canvas, dest_x, dest_y, lines[i], color);
				}
				else
				{
					Size ellipsis = get_text_size(canvas, "...");

					int seek_start = 0;
					int seek_end = lines[i].size();
					int seek_center = (seek_start + seek_end) / 2;

					UTF8_Reader utf8_reader(lines[i].data(), lines[i].length());
					while (true)
					{
						utf8_reader.set_position(seek_center);
						utf8_reader.move_to_leadbyte();
						if (seek_center != utf8_reader.get_position())
							utf8_reader.next();
						seek_center = utf8_reader.get_position();
						if (seek_center == seek_end)
							break;

						utf8_reader.set_position(seek_start);
						utf8_reader.next();
						if (utf8_reader.get_position() == seek_end)
							break;

						Size text_size = get_text_size(canvas, lines[i].substr(0, seek_center));

						if (dest_x + text_size.width + ellipsis.width >= content_box.right)
							seek_end = seek_center;
						else
							seek_start = seek_center;
						seek_center = (seek_start+seek_end)/2;
					}

					draw_text(canvas, dest_x, dest_y, lines[i].substr(0, seek_center) + "...", color);
				}

				dest_y += line_spacing;
			}
		}
	}
}
Ejemplo n.º 3
0
VerticalTextPosition GUIComponent::get_vertical_text_align(Canvas &canvas, const Rect &content_rect)
{
	Font font = get_font();

	// See diagram in: Documentation\Overview\fonts.html (Font Metrics)

	FontMetrics metrics = font.get_font_metrics();
	float align_height = metrics.get_ascent() - metrics.get_internal_leading();
	float content_height = content_rect.get_height();
	float baseline = (content_height + align_height) / 2.0f;

	VerticalTextPosition result;
	result.baseline = baseline + content_rect.top;
	result.top = result.baseline - metrics.get_ascent();
	result.bottom = result.baseline + metrics.get_descent();
	return result;
}
Ejemplo n.º 4
0
	std::string Font::get_clipped_text(Canvas &canvas, const Sizef &box_size, const std::string &text, const std::string &ellipsis_text)
	{
		std::string out_string;
		out_string.reserve(text.length());

		if (impl)
		{
			Pointf pos;

			FontMetrics fm = get_font_metrics(canvas);
			float descent = fm.get_descent();
			float line_spacing = fm.get_line_height();
			std::vector<std::string> lines = StringHelp::split_text(text, "\n", false);
			for (std::vector<std::string>::size_type i = 0; i < lines.size(); i++)
			{
				if (i == 0 || pos.y + descent < box_size.height)
				{
					Sizef size = measure_text(canvas, lines[i]).bbox_size;
					if (pos.x + size.width <= box_size.width)
					{
						if (!out_string.empty())
							out_string += "\n";
						out_string += lines[i];
					}
					else
					{
						Sizef ellipsis = measure_text(canvas, ellipsis_text).bbox_size;

						int seek_start = 0;
						int seek_end = lines[i].size();
						int seek_center = (seek_start + seek_end) / 2;

						UTF8_Reader utf8_reader(lines[i].data(), lines[i].length());
						while (true)
						{
							utf8_reader.set_position(seek_center);
							utf8_reader.move_to_leadbyte();
							if (seek_center != utf8_reader.get_position())
								utf8_reader.next();
							seek_center = utf8_reader.get_position();
							if (seek_center == seek_end)
								break;

							utf8_reader.set_position(seek_start);
							utf8_reader.next();
							if (utf8_reader.get_position() == seek_end)
								break;

							Sizef text_size = measure_text(canvas, lines[i].substr(0, seek_center)).bbox_size;

							if (pos.x + text_size.width + ellipsis.width >= box_size.width)
								seek_end = seek_center;
							else
								seek_start = seek_center;
							seek_center = (seek_start + seek_end) / 2;
						}

						if (!out_string.empty())
							out_string += "\n";
						out_string += lines[i].substr(0, seek_center) + ellipsis_text;
					}
					pos.y += line_spacing;
				}
			}
		}
		return out_string;
	}