LineBoxFragment.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf8View.h>
  7. #include <LibGfx/Painter.h>
  8. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  9. #include <LibWeb/Layout/LineBoxFragment.h>
  10. #include <LibWeb/Layout/TextNode.h>
  11. #include <LibWeb/Painting/PaintContext.h>
  12. #include <ctype.h>
  13. namespace Web::Layout {
  14. void LineBoxFragment::paint(PaintContext& context, PaintPhase phase)
  15. {
  16. for (auto* ancestor = layout_node().parent(); ancestor; ancestor = ancestor->parent()) {
  17. if (!ancestor->is_visible())
  18. return;
  19. }
  20. layout_node().paint_fragment(context, *this, phase);
  21. }
  22. bool LineBoxFragment::ends_in_whitespace() const
  23. {
  24. auto text = this->text();
  25. if (text.is_empty())
  26. return false;
  27. return isspace(text[text.length() - 1]);
  28. }
  29. bool LineBoxFragment::is_justifiable_whitespace() const
  30. {
  31. return text() == " ";
  32. }
  33. StringView LineBoxFragment::text() const
  34. {
  35. if (!is<TextNode>(layout_node()))
  36. return {};
  37. return verify_cast<TextNode>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
  38. }
  39. const Gfx::FloatRect LineBoxFragment::absolute_rect() const
  40. {
  41. Gfx::FloatRect rect { {}, size() };
  42. rect.set_location(m_layout_node.containing_block()->absolute_position());
  43. rect.translate_by(offset());
  44. return rect;
  45. }
  46. int LineBoxFragment::text_index_at(float x) const
  47. {
  48. if (!is<TextNode>(layout_node()))
  49. return 0;
  50. auto& layout_text = verify_cast<TextNode>(layout_node());
  51. auto& font = layout_text.font();
  52. Utf8View view(text());
  53. float relative_x = x - absolute_x();
  54. float glyph_spacing = font.glyph_spacing();
  55. if (relative_x < 0)
  56. return 0;
  57. float width_so_far = 0;
  58. for (auto it = view.begin(); it != view.end(); ++it) {
  59. float glyph_width = font.glyph_or_emoji_width(*it);
  60. if ((width_so_far + (glyph_width + glyph_spacing) / 2) > relative_x)
  61. return m_start + view.byte_offset_of(it);
  62. width_so_far += glyph_width + glyph_spacing;
  63. }
  64. return m_start + m_length;
  65. }
  66. Gfx::FloatRect LineBoxFragment::selection_rect(const Gfx::Font& font) const
  67. {
  68. if (layout_node().selection_state() == Node::SelectionState::None)
  69. return {};
  70. if (layout_node().selection_state() == Node::SelectionState::Full)
  71. return absolute_rect();
  72. auto selection = layout_node().root().selection().normalized();
  73. if (!selection.is_valid())
  74. return {};
  75. if (!is<TextNode>(layout_node()))
  76. return {};
  77. const auto start_index = m_start;
  78. const auto end_index = m_start + m_length;
  79. auto text = this->text();
  80. if (layout_node().selection_state() == Node::SelectionState::StartAndEnd) {
  81. // we are in the start/end node (both the same)
  82. if (start_index > selection.end().index_in_node)
  83. return {};
  84. if (end_index < selection.start().index_in_node)
  85. return {};
  86. if (selection.start().index_in_node == selection.end().index_in_node)
  87. return {};
  88. auto selection_start_in_this_fragment = max(0, selection.start().index_in_node - m_start);
  89. auto selection_end_in_this_fragment = min(m_length, selection.end().index_in_node - m_start);
  90. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  91. auto pixel_width_of_selection = font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment)) + 1;
  92. auto rect = absolute_rect();
  93. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  94. rect.set_width(pixel_width_of_selection);
  95. return rect;
  96. }
  97. if (layout_node().selection_state() == Node::SelectionState::Start) {
  98. // we are in the start node
  99. if (end_index < selection.start().index_in_node)
  100. return {};
  101. auto selection_start_in_this_fragment = max(0, selection.start().index_in_node - m_start);
  102. auto selection_end_in_this_fragment = m_length;
  103. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  104. auto pixel_width_of_selection = font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment)) + 1;
  105. auto rect = absolute_rect();
  106. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  107. rect.set_width(pixel_width_of_selection);
  108. return rect;
  109. }
  110. if (layout_node().selection_state() == Node::SelectionState::End) {
  111. // we are in the end node
  112. if (start_index > selection.end().index_in_node)
  113. return {};
  114. auto selection_start_in_this_fragment = 0;
  115. auto selection_end_in_this_fragment = min(selection.end().index_in_node, m_length);
  116. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  117. auto pixel_width_of_selection = font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment)) + 1;
  118. auto rect = absolute_rect();
  119. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  120. rect.set_width(pixel_width_of_selection);
  121. return rect;
  122. }
  123. return {};
  124. }
  125. }