LineBoxFragment.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. bool LineBoxFragment::is_atomic_inline() const
  62. {
  63. return layout_node().is_replaced_box() || (layout_node().display().is_inline_outside() && !layout_node().display().is_flow_inside());
  64. }
  65. CSS::Direction LineBoxFragment::resolve_glyph_run_direction(Gfx::GlyphRun::TextType text_type) const
  66. {
  67. switch (text_type) {
  68. case Gfx::GlyphRun::TextType::Common:
  69. case Gfx::GlyphRun::TextType::ContextDependent:
  70. case Gfx::GlyphRun::TextType::EndPadding:
  71. return m_direction;
  72. case Gfx::GlyphRun::TextType::Ltr:
  73. return CSS::Direction::Ltr;
  74. case Gfx::GlyphRun::TextType::Rtl:
  75. return CSS::Direction::Rtl;
  76. default:
  77. VERIFY_NOT_REACHED();
  78. }
  79. }
  80. void LineBoxFragment::append_glyph_run(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  81. {
  82. switch (m_direction) {
  83. case CSS::Direction::Ltr:
  84. append_glyph_run_ltr(glyph_run, run_width);
  85. break;
  86. case CSS::Direction::Rtl:
  87. append_glyph_run_rtl(glyph_run, run_width);
  88. break;
  89. }
  90. }
  91. void LineBoxFragment::append_glyph_run_ltr(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  92. {
  93. auto run_direction = resolve_glyph_run_direction(glyph_run->text_type());
  94. if (m_current_insert_direction != run_direction) {
  95. if (run_direction == CSS::Direction::Rtl)
  96. m_insert_position = m_inline_length.to_float();
  97. m_current_insert_direction = run_direction;
  98. }
  99. switch (run_direction) {
  100. case CSS::Direction::Ltr:
  101. for (auto& glyph : glyph_run->glyphs()) {
  102. glyph.position.translate_by(m_inline_length.to_float(), 0);
  103. m_glyph_run->append(glyph);
  104. }
  105. break;
  106. case CSS::Direction::Rtl:
  107. for (auto& glyph : m_glyph_run->glyphs()) {
  108. if (glyph.position.x() >= m_insert_position)
  109. glyph.position.translate_by(run_width.to_float(), 0);
  110. }
  111. for (auto& glyph : glyph_run->glyphs()) {
  112. glyph.position.translate_by(m_insert_position, 0);
  113. m_glyph_run->append(glyph);
  114. }
  115. break;
  116. }
  117. m_inline_length += run_width;
  118. }
  119. void LineBoxFragment::append_glyph_run_rtl(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  120. {
  121. auto run_direction = resolve_glyph_run_direction(glyph_run->text_type());
  122. if (m_current_insert_direction != run_direction) {
  123. if (run_direction == CSS::Direction::Ltr)
  124. m_insert_position = 0;
  125. m_current_insert_direction = run_direction;
  126. }
  127. switch (run_direction) {
  128. case CSS::Direction::Ltr:
  129. for (auto& glyph : m_glyph_run->glyphs()) {
  130. if (glyph.position.x() >= m_insert_position)
  131. glyph.position.translate_by(run_width.to_float(), 0);
  132. }
  133. for (auto& glyph : glyph_run->glyphs()) {
  134. glyph.position.translate_by(m_insert_position, 0);
  135. m_glyph_run->append(glyph);
  136. }
  137. break;
  138. case CSS::Direction::Rtl:
  139. if (glyph_run->text_type() != Gfx::GlyphRun::TextType::EndPadding) {
  140. for (auto& glyph : m_glyph_run->glyphs()) {
  141. glyph.position.translate_by(run_width.to_float(), 0);
  142. }
  143. }
  144. for (auto& glyph : glyph_run->glyphs()) {
  145. m_glyph_run->append(glyph);
  146. }
  147. break;
  148. }
  149. m_inline_length += run_width;
  150. m_insert_position += run_width.to_float();
  151. }
  152. }