LineBoxFragment.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/Layout/LayoutState.h>
  8. #include <LibWeb/Layout/TextNode.h>
  9. #include <ctype.h>
  10. namespace Web::Layout {
  11. 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)
  12. : m_layout_node(layout_node)
  13. , m_start(start)
  14. , m_length(length)
  15. , m_inline_offset(inline_offset)
  16. , m_block_offset(block_offset)
  17. , m_inline_length(inline_length)
  18. , m_block_length(block_length)
  19. , m_border_box_top(border_box_top)
  20. , m_direction(direction)
  21. , m_writing_mode(writing_mode)
  22. , m_glyph_run(move(glyph_run))
  23. {
  24. if (m_glyph_run) {
  25. m_current_insert_direction = resolve_glyph_run_direction(m_glyph_run->text_type());
  26. if (m_direction == CSS::Direction::Rtl)
  27. m_insert_position = m_inline_length.to_float();
  28. }
  29. }
  30. CSSPixelPoint LineBoxFragment::offset() const
  31. {
  32. if (m_writing_mode != CSS::WritingMode::HorizontalTb)
  33. return { m_block_offset, m_inline_offset };
  34. return { m_inline_offset, m_block_offset };
  35. }
  36. CSSPixelSize LineBoxFragment::size() const
  37. {
  38. if (m_writing_mode != CSS::WritingMode::HorizontalTb)
  39. return { m_block_length, m_inline_length };
  40. return { m_inline_length, m_block_length };
  41. }
  42. bool LineBoxFragment::ends_in_whitespace() const
  43. {
  44. auto text = this->text();
  45. if (text.is_empty())
  46. return false;
  47. return isspace(text[text.length() - 1]);
  48. }
  49. bool LineBoxFragment::is_justifiable_whitespace() const
  50. {
  51. return text() == " ";
  52. }
  53. StringView LineBoxFragment::text() const
  54. {
  55. if (!is<TextNode>(layout_node()))
  56. return {};
  57. return verify_cast<TextNode>(layout_node()).text_for_rendering().bytes_as_string_view().substring_view(m_start, m_length);
  58. }
  59. bool LineBoxFragment::is_atomic_inline() const
  60. {
  61. return layout_node().is_replaced_box() || (layout_node().display().is_inline_outside() && !layout_node().display().is_flow_inside());
  62. }
  63. CSS::Direction LineBoxFragment::resolve_glyph_run_direction(Gfx::GlyphRun::TextType text_type) const
  64. {
  65. switch (text_type) {
  66. case Gfx::GlyphRun::TextType::Common:
  67. case Gfx::GlyphRun::TextType::ContextDependent:
  68. case Gfx::GlyphRun::TextType::EndPadding:
  69. return m_direction;
  70. case Gfx::GlyphRun::TextType::Ltr:
  71. return CSS::Direction::Ltr;
  72. case Gfx::GlyphRun::TextType::Rtl:
  73. return CSS::Direction::Rtl;
  74. default:
  75. VERIFY_NOT_REACHED();
  76. }
  77. }
  78. void LineBoxFragment::append_glyph_run(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  79. {
  80. switch (m_direction) {
  81. case CSS::Direction::Ltr:
  82. append_glyph_run_ltr(glyph_run, run_width);
  83. break;
  84. case CSS::Direction::Rtl:
  85. append_glyph_run_rtl(glyph_run, run_width);
  86. break;
  87. }
  88. }
  89. void LineBoxFragment::append_glyph_run_ltr(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  90. {
  91. auto run_direction = resolve_glyph_run_direction(glyph_run->text_type());
  92. if (m_current_insert_direction != run_direction) {
  93. if (run_direction == CSS::Direction::Rtl)
  94. m_insert_position = m_inline_length.to_float();
  95. m_current_insert_direction = run_direction;
  96. }
  97. switch (run_direction) {
  98. case CSS::Direction::Ltr:
  99. for (auto& glyph : glyph_run->glyphs()) {
  100. glyph.position.translate_by(m_inline_length.to_float(), 0);
  101. m_glyph_run->append(glyph);
  102. }
  103. break;
  104. case CSS::Direction::Rtl:
  105. for (auto& glyph : m_glyph_run->glyphs()) {
  106. if (glyph.position.x() >= m_insert_position)
  107. glyph.position.translate_by(run_width.to_float(), 0);
  108. }
  109. for (auto& glyph : glyph_run->glyphs()) {
  110. glyph.position.translate_by(m_insert_position, 0);
  111. m_glyph_run->append(glyph);
  112. }
  113. break;
  114. }
  115. m_inline_length += run_width;
  116. }
  117. void LineBoxFragment::append_glyph_run_rtl(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
  118. {
  119. auto run_direction = resolve_glyph_run_direction(glyph_run->text_type());
  120. if (m_current_insert_direction != run_direction) {
  121. if (run_direction == CSS::Direction::Ltr)
  122. m_insert_position = 0;
  123. m_current_insert_direction = run_direction;
  124. }
  125. switch (run_direction) {
  126. case CSS::Direction::Ltr:
  127. for (auto& glyph : m_glyph_run->glyphs()) {
  128. if (glyph.position.x() >= m_insert_position)
  129. glyph.position.translate_by(run_width.to_float(), 0);
  130. }
  131. for (auto& glyph : glyph_run->glyphs()) {
  132. glyph.position.translate_by(m_insert_position, 0);
  133. m_glyph_run->append(glyph);
  134. }
  135. break;
  136. case CSS::Direction::Rtl:
  137. if (glyph_run->text_type() != Gfx::GlyphRun::TextType::EndPadding) {
  138. for (auto& glyph : m_glyph_run->glyphs()) {
  139. glyph.position.translate_by(run_width.to_float(), 0);
  140. }
  141. }
  142. for (auto& glyph : glyph_run->glyphs()) {
  143. m_glyph_run->append(glyph);
  144. }
  145. break;
  146. }
  147. m_inline_length += run_width;
  148. m_insert_position += run_width.to_float();
  149. }
  150. }