InlineLevelIterator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/BreakNode.h>
  7. #include <LibWeb/Layout/InlineLevelIterator.h>
  8. #include <LibWeb/Layout/InlineNode.h>
  9. #include <LibWeb/Layout/ReplacedBox.h>
  10. namespace Web::Layout {
  11. void InlineLevelIterator::skip_to_next()
  12. {
  13. VERIFY(m_current_node);
  14. do {
  15. m_current_node = m_current_node->next_in_pre_order(&m_container);
  16. } while (m_current_node && !m_current_node->is_inline());
  17. }
  18. Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_width)
  19. {
  20. if (!m_current_node)
  21. return {};
  22. if (is<Layout::TextNode>(*m_current_node)) {
  23. auto& text_node = static_cast<Layout::TextNode&>(*m_current_node);
  24. if (!m_text_node_context.has_value())
  25. enter_text_node(text_node);
  26. auto chunk_opt = m_text_node_context->chunk_iterator.next();
  27. if (!chunk_opt.has_value()) {
  28. m_text_node_context = {};
  29. skip_to_next();
  30. return next(available_width);
  31. }
  32. auto& chunk = chunk_opt.value();
  33. float chunk_width = text_node.font().width(chunk.view) + text_node.font().glyph_spacing();
  34. Item item {
  35. .type = Item::Type::Text,
  36. .node = &text_node,
  37. .offset_in_node = chunk.start,
  38. .length_in_node = chunk.length,
  39. .width = chunk_width,
  40. };
  41. return item;
  42. }
  43. if (is<Layout::BreakNode>(*m_current_node)) {
  44. skip_to_next();
  45. return Item {
  46. .type = Item::Type::ForcedBreak,
  47. };
  48. }
  49. if (!is<Layout::Box>(*m_current_node)) {
  50. skip_to_next();
  51. return next(available_width);
  52. }
  53. if (is<Layout::ReplacedBox>(*m_current_node)) {
  54. auto& replaced_box = static_cast<Layout::ReplacedBox&>(*m_current_node);
  55. replaced_box.prepare_for_replaced_layout();
  56. }
  57. auto& box = verify_cast<Layout::Box>(*m_current_node);
  58. skip_to_next();
  59. return Item {
  60. .type = Item::Type::Element,
  61. .node = &box,
  62. .offset_in_node = 0,
  63. .length_in_node = 0,
  64. .width = box.width(),
  65. };
  66. }
  67. void InlineLevelIterator::enter_text_node(Layout::TextNode& text_node)
  68. {
  69. bool do_collapse = true;
  70. bool do_wrap_lines = true;
  71. bool do_respect_linebreaks = false;
  72. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  73. do_collapse = true;
  74. do_wrap_lines = false;
  75. do_respect_linebreaks = false;
  76. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  77. do_collapse = false;
  78. do_wrap_lines = false;
  79. do_respect_linebreaks = true;
  80. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  81. do_collapse = true;
  82. do_wrap_lines = true;
  83. do_respect_linebreaks = true;
  84. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  85. do_collapse = false;
  86. do_wrap_lines = true;
  87. do_respect_linebreaks = true;
  88. }
  89. // FIXME: Pass the correct value for the last boolean!
  90. text_node.compute_text_for_rendering(do_collapse, true);
  91. m_text_node_context = TextNodeContext {
  92. .do_collapse = do_collapse,
  93. .do_wrap_lines = do_wrap_lines,
  94. .do_respect_linebreaks = do_respect_linebreaks,
  95. .chunk_iterator = TextNode::ChunkIterator { text_node.text_for_rendering(), m_layout_mode, do_wrap_lines, do_respect_linebreaks },
  96. };
  97. }
  98. }