LineBoxFragment.cpp 5.0 KB

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