LineBoxFragment.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/LayoutDocument.h>
  29. #include <LibWeb/Layout/LayoutText.h>
  30. #include <LibWeb/Layout/LineBoxFragment.h>
  31. #include <LibWeb/Painting/PaintContext.h>
  32. #include <ctype.h>
  33. namespace Web {
  34. void LineBoxFragment::paint(PaintContext& context)
  35. {
  36. for (auto* ancestor = layout_node().parent(); ancestor; ancestor = ancestor->parent()) {
  37. if (!ancestor->is_visible())
  38. return;
  39. }
  40. if (is<LayoutText>(layout_node())) {
  41. downcast<LayoutText>(layout_node()).paint_fragment(context, *this);
  42. }
  43. }
  44. bool LineBoxFragment::ends_in_whitespace() const
  45. {
  46. auto text = this->text();
  47. if (text.is_empty())
  48. return false;
  49. return isspace(text[text.length() - 1]);
  50. }
  51. bool LineBoxFragment::is_justifiable_whitespace() const
  52. {
  53. return text() == " ";
  54. }
  55. StringView LineBoxFragment::text() const
  56. {
  57. if (!is<LayoutText>(layout_node()))
  58. return {};
  59. return downcast<LayoutText>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
  60. }
  61. const Gfx::FloatRect LineBoxFragment::absolute_rect() const
  62. {
  63. Gfx::FloatRect rect { {}, size() };
  64. rect.set_location(m_layout_node.containing_block()->absolute_position());
  65. rect.move_by(offset());
  66. return rect;
  67. }
  68. int LineBoxFragment::text_index_at(float x) const
  69. {
  70. if (!layout_node().is_text())
  71. return 0;
  72. auto& layout_text = downcast<LayoutText>(layout_node());
  73. auto& font = layout_text.specified_style().font();
  74. Utf8View view(text());
  75. float relative_x = x - absolute_x();
  76. float glyph_spacing = font.glyph_spacing();
  77. if (relative_x < 0)
  78. return 0;
  79. float width_so_far = 0;
  80. for (auto it = view.begin(); it != view.end(); ++it) {
  81. float glyph_width = font.glyph_or_emoji_width(*it);
  82. if ((width_so_far + glyph_width + glyph_spacing) > relative_x)
  83. return m_start + view.byte_offset_of(it);
  84. width_so_far += glyph_width + glyph_spacing;
  85. }
  86. return m_start + m_length - 1;
  87. }
  88. Gfx::FloatRect LineBoxFragment::selection_rect(const Gfx::Font& font) const
  89. {
  90. auto selection = layout_node().root().selection().normalized();
  91. if (!selection.is_valid())
  92. return {};
  93. if (!layout_node().is_text())
  94. return {};
  95. const auto start_index = m_start;
  96. const auto end_index = m_start + m_length;
  97. auto text = this->text();
  98. if (&layout_node() == selection.start().layout_node && &layout_node() == selection.end().layout_node) {
  99. // we are in the start/end node (both the same)
  100. if (start_index > selection.end().index_in_node)
  101. return {};
  102. if (end_index < selection.start().index_in_node)
  103. return {};
  104. auto selection_start_in_this_fragment = max(0, selection.start().index_in_node - m_start);
  105. auto selection_end_in_this_fragment = min(m_length, selection.end().index_in_node - m_start + 1);
  106. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  107. 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;
  108. auto rect = absolute_rect();
  109. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  110. rect.set_width(pixel_width_of_selection);
  111. return rect;
  112. }
  113. if (&layout_node() == selection.start().layout_node) {
  114. // we are in the start node
  115. if (end_index < selection.start().index_in_node)
  116. return {};
  117. auto selection_start_in_this_fragment = max(0, selection.start().index_in_node - m_start);
  118. auto selection_end_in_this_fragment = m_length;
  119. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  120. 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;
  121. auto rect = absolute_rect();
  122. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  123. rect.set_width(pixel_width_of_selection);
  124. return rect;
  125. }
  126. if (&layout_node() == selection.end().layout_node) {
  127. // we are in the end node
  128. if (start_index > selection.end().index_in_node)
  129. return {};
  130. auto selection_start_in_this_fragment = 0;
  131. auto selection_end_in_this_fragment = min(selection.end().index_in_node + 1, m_length);
  132. auto pixel_distance_to_first_selected_character = font.width(text.substring_view(0, selection_start_in_this_fragment));
  133. 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;
  134. auto rect = absolute_rect();
  135. rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
  136. rect.set_width(pixel_width_of_selection);
  137. return rect;
  138. }
  139. // are we in between start and end?
  140. auto* node = selection.start().layout_node.ptr();
  141. bool is_fully_selected = false;
  142. for (; node && node != selection.end().layout_node.ptr(); node = node->next_in_pre_order()) {
  143. if (node == &layout_node()) {
  144. is_fully_selected = true;
  145. break;
  146. }
  147. }
  148. if (is_fully_selected)
  149. return absolute_rect();
  150. return {};
  151. }
  152. }