LineBoxFragment.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/Layout/LayoutDocument.h>
  3. #include <LibHTML/Layout/LayoutText.h>
  4. #include <LibHTML/Layout/LineBoxFragment.h>
  5. #include <LibHTML/RenderingContext.h>
  6. void LineBoxFragment::render(RenderingContext& context)
  7. {
  8. for (auto* ancestor = layout_node().parent(); ancestor; ancestor = ancestor->parent()) {
  9. if (!ancestor->is_visible())
  10. return;
  11. }
  12. if (is<LayoutText>(layout_node())) {
  13. to<LayoutText>(layout_node()).render_fragment(context, *this);
  14. }
  15. }
  16. bool LineBoxFragment::is_justifiable_whitespace() const
  17. {
  18. return text() == " ";
  19. }
  20. StringView LineBoxFragment::text() const
  21. {
  22. if (!is<LayoutText>(layout_node()))
  23. return {};
  24. return to<LayoutText>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
  25. }
  26. int LineBoxFragment::text_index_at(float x) const
  27. {
  28. if (!layout_node().is_text())
  29. return 0;
  30. auto& layout_text = to<LayoutText>(layout_node());
  31. auto& font = layout_text.style().font();
  32. Utf8View view(text());
  33. float relative_x = x - m_rect.location().x();
  34. float glyph_spacing = font.glyph_spacing();
  35. float width_so_far = 0;
  36. for (auto it = view.begin(); it != view.end(); ++it) {
  37. float glyph_width = font.glyph_or_emoji_width(*it);
  38. if ((width_so_far + glyph_width + glyph_spacing) > relative_x)
  39. return m_start + view.byte_offset_of(it);
  40. width_so_far += glyph_width + glyph_spacing;
  41. }
  42. return m_start + m_length - 1;
  43. }