LineBoxFragment.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf8View.h>
  7. #include <LibWeb/DOM/Range.h>
  8. #include <LibWeb/Layout/LayoutState.h>
  9. #include <LibWeb/Layout/TextNode.h>
  10. #include <LibWeb/Layout/Viewport.h>
  11. #include <ctype.h>
  12. namespace Web::Layout {
  13. LineBoxFragment::LineBoxFragment(Node const& layout_node, int start, int length, CSSPixels inline_offset, CSSPixels block_offset, CSSPixels inline_length, CSSPixels block_length, CSSPixels border_box_top, CSS::Direction direction, CSS::WritingMode writing_mode, RefPtr<Gfx::GlyphRun> glyph_run)
  14. : m_layout_node(layout_node)
  15. , m_start(start)
  16. , m_length(length)
  17. , m_inline_offset(inline_offset)
  18. , m_block_offset(block_offset)
  19. , m_inline_length(inline_length)
  20. , m_block_length(block_length)
  21. , m_border_box_top(border_box_top)
  22. , m_direction(direction)
  23. , m_writing_mode(writing_mode)
  24. , m_glyph_run(move(glyph_run))
  25. {
  26. if (m_glyph_run) {
  27. m_current_insert_direction = resolve_glyph_run_direction(m_glyph_run->text_type());
  28. if (m_direction == CSS::Direction::Rtl)
  29. m_insert_position = m_inline_length.to_float();
  30. }
  31. }
  32. CSSPixelPoint LineBoxFragment::offset() const
  33. {
  34. if (m_writing_mode != CSS::WritingMode::HorizontalTb)
  35. return { m_block_offset, m_inline_offset };
  36. return { m_inline_offset, m_block_offset };
  37. }
  38. CSSPixelSize LineBoxFragment::size() const
  39. {
  40. if (m_writing_mode != CSS::WritingMode::HorizontalTb)
  41. return { m_block_length, m_inline_length };
  42. return { m_inline_length, m_block_length };
  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<TextNode>(layout_node()))
  58. return {};
  59. return verify_cast<TextNode>(layout_node()).text_for_rendering().bytes_as_string_view().substring_view(m_start, m_length);
  60. }
  61. CSSPixelRect const LineBoxFragment::absolute_rect() const
  62. {
  63. CSSPixelRect rect { {}, size() };
  64. rect.set_location(m_layout_node->containing_block()->paintable_box()->absolute_position());
  65. rect.translate_by(offset());
  66. return rect;
  67. }
  68. bool LineBoxFragment::is_atomic_inline() const
  69. {
  70. return layout_node().is_replaced_box() || (layout_node().display().is_inline_outside() && !layout_node().display().is_flow_inside());
  71. }
  72. CSS::Direction LineBoxFragment::resolve_glyph_run_direction(Gfx::GlyphRun::TextType text_type) const
  73. {
  74. switch (text_type) {
  75. case Gfx::GlyphRun::TextType::Common:
  76. case Gfx::GlyphRun::TextType::ContextDependent:
  77. case Gfx::GlyphRun::TextType::EndPadding:
  78. return m_direction;
  79. case Gfx::GlyphRun::TextType::Ltr:
  80. return CSS::Direction::Ltr;
  81. case Gfx::GlyphRun::TextType::Rtl:
  82. return CSS::Direction::Rtl;
  83. default:
  84. VERIFY_NOT_REACHED();
  85. }
  86. }
  87. void LineBoxFragment::append_glyph_run(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  88. {
  89. switch (m_direction) {
  90. case CSS::Direction::Ltr:
  91. append_glyph_run_ltr(glyph_run, run_width);
  92. break;
  93. case CSS::Direction::Rtl:
  94. append_glyph_run_rtl(glyph_run, run_width);
  95. break;
  96. }
  97. }
  98. void LineBoxFragment::append_glyph_run_ltr(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  99. {
  100. auto run_direction = resolve_glyph_run_direction(glyph_run->text_type());
  101. if (m_current_insert_direction != run_direction) {
  102. if (run_direction == CSS::Direction::Rtl)
  103. m_insert_position = m_inline_length.to_float();
  104. m_current_insert_direction = run_direction;
  105. }
  106. switch (run_direction) {
  107. case CSS::Direction::Ltr:
  108. for (auto& glyph : glyph_run->glyphs()) {
  109. glyph.position.translate_by(m_inline_length.to_float(), 0);
  110. m_glyph_run->append(glyph);
  111. }
  112. break;
  113. case CSS::Direction::Rtl:
  114. for (auto& glyph : m_glyph_run->glyphs()) {
  115. if (glyph.position.x() >= m_insert_position)
  116. glyph.position.translate_by(run_width.to_float(), 0);
  117. }
  118. for (auto& glyph : glyph_run->glyphs()) {
  119. glyph.position.translate_by(m_insert_position, 0);
  120. m_glyph_run->append(glyph);
  121. }
  122. break;
  123. }
  124. m_inline_length += run_width;
  125. }
  126. void LineBoxFragment::append_glyph_run_rtl(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  127. {
  128. auto run_direction = resolve_glyph_run_direction(glyph_run->text_type());
  129. if (m_current_insert_direction != run_direction) {
  130. if (run_direction == CSS::Direction::Ltr)
  131. m_insert_position = 0;
  132. m_current_insert_direction = run_direction;
  133. }
  134. switch (run_direction) {
  135. case CSS::Direction::Ltr:
  136. for (auto& glyph : m_glyph_run->glyphs()) {
  137. if (glyph.position.x() >= m_insert_position)
  138. glyph.position.translate_by(run_width.to_float(), 0);
  139. }
  140. for (auto& glyph : glyph_run->glyphs()) {
  141. glyph.position.translate_by(m_insert_position, 0);
  142. m_glyph_run->append(glyph);
  143. }
  144. break;
  145. case CSS::Direction::Rtl:
  146. if (glyph_run->text_type() != Gfx::GlyphRun::TextType::EndPadding) {
  147. for (auto& glyph : m_glyph_run->glyphs()) {
  148. glyph.position.translate_by(run_width.to_float(), 0);
  149. }
  150. }
  151. for (auto& glyph : glyph_run->glyphs()) {
  152. m_glyph_run->append(glyph);
  153. }
  154. break;
  155. }
  156. m_inline_length += run_width;
  157. m_insert_position += run_width.to_float();
  158. }
  159. }