LineBoxFragment.cpp 5.2 KB

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