LineBox.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, Span<Gfx::DrawGlyphOrEmoji const> glyph_run)
  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. auto const fragment_width = m_fragments.last().width();
  20. // The fragment we're adding is from the last Layout::Node on the line.
  21. // Expand the last fragment instead of adding a new one with the same Layout::Node.
  22. m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
  23. m_fragments.last().set_width(m_fragments.last().width() + content_width);
  24. for (auto glyph : glyph_run) {
  25. glyph.visit([&](auto& glyph) { glyph.position.translate_by(fragment_width.to_float(), 0); });
  26. m_fragments.last().m_glyph_run->append(glyph);
  27. }
  28. } else {
  29. Vector<Gfx::DrawGlyphOrEmoji> glyph_run_copy { glyph_run };
  30. CSSPixels x_offset = leading_margin + leading_size + m_width;
  31. CSSPixels y_offset = 0;
  32. m_fragments.append(LineBoxFragment { layout_node, start, length, CSSPixelPoint(x_offset, y_offset), CSSPixelSize(content_width, content_height), border_box_top, border_box_bottom, move(glyph_run_copy) });
  33. }
  34. m_width += leading_margin + leading_size + content_width + trailing_size + trailing_margin;
  35. m_height = max(m_height, content_height + border_box_top + border_box_bottom);
  36. }
  37. void LineBox::trim_trailing_whitespace()
  38. {
  39. auto should_trim = [](LineBoxFragment* fragment) {
  40. auto ws = fragment->layout_node().computed_values().white_space();
  41. return ws == CSS::WhiteSpace::Normal || ws == CSS::WhiteSpace::Nowrap || ws == CSS::WhiteSpace::PreLine;
  42. };
  43. LineBoxFragment* last_fragment = nullptr;
  44. for (;;) {
  45. if (m_fragments.is_empty())
  46. return;
  47. // last_fragment cannot be null from here on down, as m_fragments is not empty.
  48. last_fragment = &m_fragments.last();
  49. if (!should_trim(last_fragment))
  50. return;
  51. if (last_fragment->is_justifiable_whitespace()) {
  52. m_width -= last_fragment->width();
  53. m_fragments.remove(m_fragments.size() - 1);
  54. } else {
  55. break;
  56. }
  57. }
  58. auto last_text = last_fragment->text();
  59. if (last_text.is_null())
  60. return;
  61. while (last_fragment->length()) {
  62. auto last_character = last_text[last_fragment->length() - 1];
  63. if (!is_ascii_space(last_character))
  64. break;
  65. // FIXME: Use fragment's glyph run to determine the width of the last character.
  66. int last_character_width = last_fragment->layout_node().first_available_font().glyph_width(last_character);
  67. last_fragment->m_length -= 1;
  68. last_fragment->set_width(last_fragment->width() - last_character_width);
  69. m_width -= last_character_width;
  70. }
  71. }
  72. bool LineBox::is_empty_or_ends_in_whitespace() const
  73. {
  74. if (m_fragments.is_empty())
  75. return true;
  76. return m_fragments.last().ends_in_whitespace();
  77. }
  78. }