bool CSSInlineLayoutRenderLayerInline::node(CSSInlineGeneratedBox *cur) { CSSBoxNode *node = cur->box_node; CSSBoxText *text = dynamic_cast<CSSBoxText*>(node); if (text) { const CSSComputedValues &properties = text->get_properties(); if (properties.get_misc_inherit().visibility.type == CSSValueVisibility::type_visible) { Font font = graphics->get_font(properties); FontMetrics metrics = graphics->get_font_metrics(font); int pos_x = used_to_actual(cur->relative_x) + formatting_context->get_x(); int pos_y = used_to_actual(cur->relative_y) + formatting_context->get_y(); graphics->draw_text(font, pos_x + cur->x, pos_y + cur->y + used_to_actual(metrics.get_ascent()), text->processed_text.substr(cur->text_start, cur->text_end - cur->text_start), properties.get_text_inherit().color.color); } } else if (cur->layout_node) { CSSLayoutTreeNode *object_node = cur->layout_node; bool is_same_stacking_context = (stacking_context == object_node->get_stacking_context()); bool is_positioned = (object_node->get_element_node()->computed_values.get_box().position.type != CSSValuePosition::type_static); if (is_same_stacking_context && !is_positioned) object_node->render_layer_inline(graphics, resources); } return true; }
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; }
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; } } } }
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; }
int GlyphCache::get_character_index(FontEngine *font_engine, GraphicContext &gc, const std::string &text, const Point &point) { int dest_x = 0; int dest_y = 0; int character_counter = 0; FontMetrics fm = get_font_metrics(); int font_height = fm.get_height(); int font_ascent = fm.get_ascent(); int font_external_leading = 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++) { int xpos = dest_x; int ypos = dest_y; std::string &textline = lines[i]; std::string::size_type string_length = textline.length(); // Scan the string UTF8_Reader reader(textline.data(), textline.length()); while(!reader.is_end()) { unsigned int glyph = reader.get_char(); std::string::size_type glyph_pos = reader.get_position(); reader.next(); Font_TextureGlyph *gptr = get_glyph(font_engine, gc, glyph); if (gptr == NULL) continue; Rect position(xpos, ypos - font_ascent, Size(gptr->increment.x, gptr->increment.y + font_height + font_external_leading)); if (position.contains(point)) { return glyph_pos + character_counter; } xpos += gptr->increment.x; ypos += gptr->increment.y; } dest_y += font_height + font_external_leading; character_counter += string_length + 1; // (Including the '\n') } return -1; // Not found }