LineBox.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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;
  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. m_height = max(m_height, content_height + border_box_top + border_box_bottom);
  30. }
  31. void LineBox::trim_trailing_whitespace()
  32. {
  33. auto should_trim = [](LineBoxFragment* fragment) {
  34. auto ws = fragment->layout_node().computed_values().white_space();
  35. return ws == CSS::WhiteSpace::Normal || ws == CSS::WhiteSpace::Nowrap || ws == CSS::WhiteSpace::PreLine;
  36. };
  37. LineBoxFragment* last_fragment = nullptr;
  38. for (;;) {
  39. if (m_fragments.is_empty())
  40. return;
  41. // last_fragment cannot be null from here on down, as m_fragments is not empty.
  42. last_fragment = &m_fragments.last();
  43. if (!should_trim(last_fragment))
  44. return;
  45. if (last_fragment->is_justifiable_whitespace()) {
  46. m_width -= last_fragment->width();
  47. m_fragments.remove(m_fragments.size() - 1);
  48. } else {
  49. break;
  50. }
  51. }
  52. auto last_text = last_fragment->text();
  53. if (last_text.is_null())
  54. return;
  55. while (last_fragment->length()) {
  56. auto last_character = last_text[last_fragment->length() - 1];
  57. if (!is_ascii_space(last_character))
  58. break;
  59. int last_character_width = last_fragment->layout_node().font().glyph_width(last_character);
  60. last_fragment->m_length -= 1;
  61. last_fragment->set_width(last_fragment->width() - last_character_width);
  62. m_width -= last_character_width;
  63. }
  64. }
  65. bool LineBox::is_empty_or_ends_in_whitespace() const
  66. {
  67. if (m_fragments.is_empty())
  68. return true;
  69. return m_fragments.last().ends_in_whitespace();
  70. }
  71. }