LineBox.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.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/Layout/Box.h>
  10. #include <LibWeb/Layout/BreakNode.h>
  11. #include <LibWeb/Layout/LineBox.h>
  12. #include <LibWeb/Layout/Node.h>
  13. #include <LibWeb/Layout/TextNode.h>
  14. namespace Web::Layout {
  15. 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)
  16. {
  17. bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify;
  18. if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) {
  19. // The fragment we're adding is from the last Layout::Node on the line.
  20. // Expand the last fragment instead of adding a new one with the same Layout::Node.
  21. m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
  22. m_fragments.last().set_width(m_fragments.last().width() + content_width);
  23. } else {
  24. CSSPixels x_offset = leading_margin + leading_size + m_width;
  25. CSSPixels y_offset = 0.0f;
  26. m_fragments.append(LineBoxFragment { layout_node, start, length, CSSPixelPoint(x_offset, y_offset), CSSPixelSize(content_width, content_height), border_box_top, border_box_bottom });
  27. }
  28. m_width += leading_margin + leading_size + content_width + trailing_size + trailing_margin;
  29. }
  30. void LineBox::trim_trailing_whitespace()
  31. {
  32. auto should_trim = [](LineBoxFragment* fragment) {
  33. auto ws = fragment->layout_node().computed_values().white_space();
  34. return ws == CSS::WhiteSpace::Normal || ws == CSS::WhiteSpace::Nowrap || ws == CSS::WhiteSpace::PreLine;
  35. };
  36. LineBoxFragment* last_fragment = nullptr;
  37. for (;;) {
  38. if (m_fragments.is_empty())
  39. return;
  40. // last_fragment cannot be null from here on down, as m_fragments is not empty.
  41. last_fragment = &m_fragments.last();
  42. if (!should_trim(last_fragment))
  43. return;
  44. if (last_fragment->is_justifiable_whitespace()) {
  45. m_width -= last_fragment->width();
  46. m_fragments.remove(m_fragments.size() - 1);
  47. } else {
  48. break;
  49. }
  50. }
  51. auto last_text = last_fragment->text();
  52. if (last_text.is_null())
  53. return;
  54. while (last_fragment->length()) {
  55. auto last_character = last_text[last_fragment->length() - 1];
  56. if (!is_ascii_space(last_character))
  57. break;
  58. int last_character_width = last_fragment->layout_node().font().glyph_width(last_character);
  59. last_fragment->m_length -= 1;
  60. last_fragment->set_width(last_fragment->width() - last_character_width);
  61. m_width -= last_character_width;
  62. }
  63. }
  64. bool LineBox::is_empty_or_ends_in_whitespace() const
  65. {
  66. if (m_fragments.is_empty())
  67. return true;
  68. return m_fragments.last().ends_in_whitespace();
  69. }
  70. }