LineBox.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/TypeCasts.h>
  8. #include <AK/Utf8View.h>
  9. #include <LibWeb/DOM/Position.h>
  10. #include <LibWeb/Layout/Box.h>
  11. #include <LibWeb/Layout/BreakNode.h>
  12. #include <LibWeb/Layout/LineBox.h>
  13. #include <LibWeb/Layout/Node.h>
  14. #include <LibWeb/Layout/TextNode.h>
  15. namespace Web::Layout {
  16. CSSPixels LineBox::width() const
  17. {
  18. if (m_writing_mode != CSS::WritingMode::HorizontalTb)
  19. return m_block_length;
  20. return m_inline_length;
  21. }
  22. CSSPixels LineBox::height() const
  23. {
  24. if (m_writing_mode != CSS::WritingMode::HorizontalTb)
  25. return m_inline_length;
  26. return m_block_length;
  27. }
  28. CSSPixels LineBox::bottom() const
  29. {
  30. if (m_writing_mode != CSS::WritingMode::HorizontalTb)
  31. return m_inline_length;
  32. return m_bottom;
  33. }
  34. void LineBox::add_fragment(Node const& layout_node, int start, int length, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, CSSPixels border_box_top, CSSPixels border_box_bottom, RefPtr<Gfx::GlyphRun> glyph_run)
  35. {
  36. bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify;
  37. if (glyph_run && !text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node && &m_fragments.last().m_glyph_run->font() == &glyph_run->font()) {
  38. // The fragment we're adding is from the last Layout::Node on the line.
  39. // Expand the last fragment instead of adding a new one with the same Layout::Node.
  40. m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
  41. m_fragments.last().append_glyph_run(glyph_run, content_width);
  42. } else {
  43. CSSPixels inline_offset = leading_margin + leading_size + m_inline_length;
  44. CSSPixels block_offset = 0;
  45. m_fragments.append(LineBoxFragment { layout_node, start, length, inline_offset, block_offset, content_width, content_height, border_box_top, m_direction, m_writing_mode, move(glyph_run) });
  46. }
  47. m_inline_length += leading_margin + leading_size + content_width + trailing_size + trailing_margin;
  48. m_block_length = max(m_block_length, content_height + border_box_top + border_box_bottom);
  49. }
  50. void LineBox::trim_trailing_whitespace()
  51. {
  52. auto should_trim = [](LineBoxFragment* fragment) {
  53. auto ws = fragment->layout_node().computed_values().white_space();
  54. return ws == CSS::WhiteSpace::Normal || ws == CSS::WhiteSpace::Nowrap || ws == CSS::WhiteSpace::PreLine;
  55. };
  56. LineBoxFragment* last_fragment = nullptr;
  57. for (;;) {
  58. if (m_fragments.is_empty())
  59. return;
  60. // last_fragment cannot be null from here on down, as m_fragments is not empty.
  61. last_fragment = &m_fragments.last();
  62. auto const* dom_node = last_fragment->layout_node().dom_node();
  63. if (dom_node) {
  64. auto cursor_position = dom_node->document().cursor_position();
  65. if (cursor_position && cursor_position->node() == dom_node)
  66. return;
  67. }
  68. if (!should_trim(last_fragment))
  69. return;
  70. if (last_fragment->is_justifiable_whitespace()) {
  71. m_inline_length -= last_fragment->inline_length();
  72. m_fragments.remove(m_fragments.size() - 1);
  73. } else {
  74. break;
  75. }
  76. }
  77. auto last_text = last_fragment->text();
  78. if (last_text.is_null())
  79. return;
  80. while (last_fragment->length()) {
  81. auto last_character = last_text[last_fragment->length() - 1];
  82. if (!is_ascii_space(last_character))
  83. break;
  84. // FIXME: Use fragment's glyph run to determine the width of the last character.
  85. int last_character_width = last_fragment->layout_node().first_available_font().glyph_width(last_character);
  86. last_fragment->m_length -= 1;
  87. last_fragment->set_inline_length(last_fragment->inline_length() - last_character_width);
  88. m_inline_length -= last_character_width;
  89. }
  90. }
  91. bool LineBox::is_empty_or_ends_in_whitespace() const
  92. {
  93. if (m_fragments.is_empty())
  94. return true;
  95. return m_fragments.last().ends_in_whitespace();
  96. }
  97. }