LineBoxFragment.cpp 5.1 KB

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