LineBoxFragment.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Utf8View.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  29. #include <LibWeb/Layout/LineBoxFragment.h>
  30. #include <LibWeb/Layout/TextNode.h>
  31. #include <LibWeb/Painting/PaintContext.h>
  32. #include <ctype.h>
  33. namespace Web::Layout {
  34. void LineBoxFragment::paint(PaintContext& context, PaintPhase phase)
  35. {
  36. for (auto* ancestor = layout_node().parent(); ancestor; ancestor = ancestor->parent()) {
  37. if (!ancestor->is_visible())
  38. return;
  39. }
  40. if (is<TextNode>(layout_node()))
  41. downcast<TextNode>(layout_node()).paint_fragment(context, *this, phase);
  42. }
  43. bool LineBoxFragment::ends_in_whitespace() const
  44. {
  45. auto text = this->text();
  46. if (text.is_empty())
  47. return false;
  48. return isspace(text[text.length() - 1]);
  49. }
  50. bool LineBoxFragment::is_justifiable_whitespace() const
  51. {
  52. return text() == " ";
  53. }
  54. StringView LineBoxFragment::text() const
  55. {
  56. if (!is<TextNode>(layout_node()))
  57. return {};
  58. return downcast<TextNode>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
  59. }
  60. const Gfx::FloatRect LineBoxFragment::absolute_rect() const
  61. {
  62. Gfx::FloatRect rect { {}, size() };
  63. rect.set_location(m_layout_node.containing_block()->absolute_position());
  64. rect.move_by(offset());
  65. return rect;
  66. }
  67. int LineBoxFragment::text_index_at(float x) const
  68. {
  69. if (!layout_node().is_text())
  70. return 0;
  71. auto& layout_text = downcast<TextNode>(layout_node());
  72. auto& font = layout_text.specified_style().font();
  73. Utf8View view(text());
  74. float relative_x = x - absolute_x();
  75. float glyph_spacing = font.glyph_spacing();
  76. if (relative_x < 0)
  77. return 0;
  78. float width_so_far = 0;
  79. for (auto it = view.begin(); it != view.end(); ++it) {
  80. float glyph_width = font.glyph_or_emoji_width(*it);
  81. if ((width_so_far + (glyph_width + glyph_spacing) / 2) > relative_x)
  82. return m_start + view.byte_offset_of(it);
  83. width_so_far += glyph_width + glyph_spacing;
  84. }
  85. return m_start + m_length;
  86. }
  87. Gfx::FloatRect LineBoxFragment::selection_rect(const Gfx::Font& font) const
  88. {
  89. if (layout_node().selection_state() == Node::SelectionState::None)
  90. return {};
  91. if (layout_node().selection_state() == Node::SelectionState::Full)
  92. return absolute_rect();
  93. auto selection = layout_node().root().selection().normalized();
  94. if (!selection.is_valid())
  95. return {};
  96. if (!layout_node().is_text())
  97. return {};
  98. const auto start_index = m_start;
  99. const auto end_index = m_start + m_length;
  100. auto text = this->text();
  101. if (layout_node().selection_state() == Node::SelectionState::StartAndEnd) {
  102. // we are in the start/end node (both the same)
  103. if (start_index > selection.end().index_in_node)
  104. return {};
  105. if (end_index < selection.start().index_in_node)
  106. return {};
  107. if (selection.start().index_in_node == selection.end().index_in_node)
  108. return {};
  109. auto selection_start_in_this_fragment = max(0, selection.start().index_in_node - m_start);
  110. auto selection_end_in_this_fragment = min(m_length, selection.end().index_in_node - m_start);
  111. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  112. 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;
  113. auto rect = absolute_rect();
  114. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  115. rect.set_width(pixel_width_of_selection);
  116. return rect;
  117. }
  118. if (layout_node().selection_state() == Node::SelectionState::Start) {
  119. // we are in the start node
  120. if (end_index < selection.start().index_in_node)
  121. return {};
  122. auto selection_start_in_this_fragment = max(0, selection.start().index_in_node - m_start);
  123. auto selection_end_in_this_fragment = m_length;
  124. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  125. 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;
  126. auto rect = absolute_rect();
  127. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  128. rect.set_width(pixel_width_of_selection);
  129. return rect;
  130. }
  131. if (layout_node().selection_state() == Node::SelectionState::End) {
  132. // we are in the end node
  133. if (start_index > selection.end().index_in_node)
  134. return {};
  135. auto selection_start_in_this_fragment = 0;
  136. auto selection_end_in_this_fragment = min(selection.end().index_in_node, m_length);
  137. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  138. 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;
  139. auto rect = absolute_rect();
  140. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  141. rect.set_width(pixel_width_of_selection);
  142. return rect;
  143. }
  144. return {};
  145. }
  146. }