2019-10-03 15:20:13 +02:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-11-05 22:13:26 +01:00
|
|
|
#include <LibHTML/Layout/LayoutDocument.h>
|
2019-10-03 15:20:13 +02:00
|
|
|
#include <LibHTML/Layout/LayoutText.h>
|
|
|
|
#include <LibHTML/Layout/LineBoxFragment.h>
|
|
|
|
#include <LibHTML/RenderingContext.h>
|
|
|
|
|
|
|
|
void LineBoxFragment::render(RenderingContext& context)
|
|
|
|
{
|
2019-10-09 21:25:29 +02:00
|
|
|
for (auto* ancestor = layout_node().parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
if (!ancestor->is_visible())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:19:52 +02:00
|
|
|
if (is<LayoutText>(layout_node())) {
|
|
|
|
to<LayoutText>(layout_node()).render_fragment(context, *this);
|
2019-10-03 15:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 12:30:25 +02:00
|
|
|
|
|
|
|
bool LineBoxFragment::is_justifiable_whitespace() const
|
2019-10-20 17:18:28 +02:00
|
|
|
{
|
|
|
|
return text() == " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView LineBoxFragment::text() const
|
2019-10-20 12:30:25 +02:00
|
|
|
{
|
|
|
|
if (!is<LayoutText>(layout_node()))
|
2019-10-20 17:18:28 +02:00
|
|
|
return {};
|
|
|
|
return to<LayoutText>(layout_node()).node().data().substring_view(m_start, m_length);
|
2019-10-20 12:30:25 +02:00
|
|
|
}
|
2019-11-05 22:13:26 +01:00
|
|
|
|
|
|
|
int LineBoxFragment::text_index_at(float x) const
|
|
|
|
{
|
|
|
|
if (!layout_node().is_text())
|
|
|
|
return 0;
|
|
|
|
auto& layout_text = to<LayoutText>(layout_node());
|
|
|
|
auto& font = layout_text.style().font();
|
|
|
|
Utf8View view(text());
|
|
|
|
|
|
|
|
float relative_x = x - m_rect.location().x();
|
|
|
|
float glyph_spacing = font.glyph_spacing();
|
|
|
|
|
|
|
|
float width_so_far = 0;
|
|
|
|
for (auto it = view.begin(); it != view.end(); ++it) {
|
|
|
|
float glyph_width = font.glyph_or_emoji_width(*it);
|
|
|
|
if ((width_so_far + glyph_width + glyph_spacing) > relative_x)
|
|
|
|
return m_start + view.byte_offset_of(it);
|
|
|
|
width_so_far += glyph_width + glyph_spacing;
|
|
|
|
}
|
|
|
|
return m_start + m_length - 1;
|
|
|
|
}
|