LineBox.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, float leading_size, float trailing_size, float content_width, float content_height, LineBoxFragment::Type fragment_type)
  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. m_fragments.append(make<LineBoxFragment>(layout_node, start, length, Gfx::FloatPoint(m_width + leading_size, 0.0f), Gfx::FloatSize(content_width, content_height), fragment_type));
  25. }
  26. m_width += content_width + leading_size + trailing_size;
  27. if (is<Box>(layout_node)) {
  28. // FIXME: Move this to FormattingContext!
  29. const_cast<Box&>(static_cast<Box const&>(layout_node)).set_containing_line_box_fragment(m_fragments.last());
  30. }
  31. }
  32. void LineBox::trim_trailing_whitespace()
  33. {
  34. while (!m_fragments.is_empty() && m_fragments.last().is_justifiable_whitespace()) {
  35. auto fragment = m_fragments.take_last();
  36. m_width -= fragment->width();
  37. }
  38. if (m_fragments.is_empty())
  39. return;
  40. auto& last_fragment = m_fragments.last();
  41. auto last_text = last_fragment.text();
  42. if (last_text.is_null())
  43. return;
  44. while (last_fragment.length()) {
  45. auto last_character = last_text[last_fragment.length() - 1];
  46. if (!is_ascii_space(last_character))
  47. break;
  48. int last_character_width = last_fragment.layout_node().font().glyph_width(last_character);
  49. last_fragment.m_length -= 1;
  50. last_fragment.set_width(last_fragment.width() - last_character_width);
  51. m_width -= last_character_width;
  52. }
  53. }
  54. bool LineBox::is_empty_or_ends_in_whitespace() const
  55. {
  56. if (m_fragments.is_empty())
  57. return true;
  58. return m_fragments.last().ends_in_whitespace();
  59. }
  60. bool LineBox::ends_with_forced_line_break() const
  61. {
  62. return is<BreakNode>(m_fragments.last().layout_node());
  63. }
  64. }