LineBox.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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/DOM/Position.h>
  10. #include <LibWeb/Layout/Box.h>
  11. #include <LibWeb/Layout/BreakNode.h>
  12. #include <LibWeb/Layout/LineBox.h>
  13. #include <LibWeb/Layout/Node.h>
  14. #include <LibWeb/Layout/TextNode.h>
  15. namespace Web::Layout {
  16. 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, RefPtr<Gfx::GlyphRun> glyph_run)
  17. {
  18. bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify;
  19. if (glyph_run && !text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node && &m_fragments.last().m_glyph_run->font() == &glyph_run->font()) {
  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().append_glyph_run(glyph_run, content_width);
  24. } else {
  25. CSSPixels x_offset = leading_margin + leading_size + m_width;
  26. CSSPixels y_offset = 0;
  27. m_fragments.append(LineBoxFragment { layout_node, start, length, CSSPixelPoint(x_offset, y_offset), CSSPixelSize(content_width, content_height), border_box_top, m_direction, move(glyph_run) });
  28. }
  29. m_width += leading_margin + leading_size + content_width + trailing_size + trailing_margin;
  30. m_height = max(m_height, content_height + border_box_top + border_box_bottom);
  31. }
  32. void LineBox::trim_trailing_whitespace()
  33. {
  34. auto should_trim = [](LineBoxFragment* fragment) {
  35. auto ws = fragment->layout_node().computed_values().white_space();
  36. return ws == CSS::WhiteSpace::Normal || ws == CSS::WhiteSpace::Nowrap || ws == CSS::WhiteSpace::PreLine;
  37. };
  38. LineBoxFragment* last_fragment = nullptr;
  39. for (;;) {
  40. if (m_fragments.is_empty())
  41. return;
  42. // last_fragment cannot be null from here on down, as m_fragments is not empty.
  43. last_fragment = &m_fragments.last();
  44. auto const* dom_node = last_fragment->layout_node().dom_node();
  45. if (dom_node) {
  46. auto cursor_position = dom_node->document().cursor_position();
  47. if (cursor_position && cursor_position->node() == dom_node)
  48. return;
  49. }
  50. if (!should_trim(last_fragment))
  51. return;
  52. if (last_fragment->is_justifiable_whitespace()) {
  53. m_width -= last_fragment->width();
  54. m_fragments.remove(m_fragments.size() - 1);
  55. } else {
  56. break;
  57. }
  58. }
  59. auto last_text = last_fragment->text();
  60. if (last_text.is_null())
  61. return;
  62. while (last_fragment->length()) {
  63. auto last_character = last_text[last_fragment->length() - 1];
  64. if (!is_ascii_space(last_character))
  65. break;
  66. // FIXME: Use fragment's glyph run to determine the width of the last character.
  67. int last_character_width = last_fragment->layout_node().first_available_font().glyph_width(last_character);
  68. last_fragment->m_length -= 1;
  69. last_fragment->set_width(last_fragment->width() - last_character_width);
  70. m_width -= last_character_width;
  71. }
  72. }
  73. bool LineBox::is_empty_or_ends_in_whitespace() const
  74. {
  75. if (m_fragments.is_empty())
  76. return true;
  77. return m_fragments.last().ends_in_whitespace();
  78. }
  79. }