LineBox.cpp 3.6 KB

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