LineBox.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, RefPtr<Gfx::GlyphRun> glyph_run)
  16. {
  17. bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify;
  18. 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()) {
  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->glyphs()) {
  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. auto dom_node = last_fragment->layout_node().dom_node();
  49. if (dom_node && dom_node->is_editable() && dom_node->document().cursor_position())
  50. return;
  51. if (!should_trim(last_fragment))
  52. return;
  53. if (last_fragment->is_justifiable_whitespace()) {
  54. m_width -= last_fragment->width();
  55. m_fragments.remove(m_fragments.size() - 1);
  56. } else {
  57. break;
  58. }
  59. }
  60. auto last_text = last_fragment->text();
  61. if (last_text.is_null())
  62. return;
  63. while (last_fragment->length()) {
  64. auto last_character = last_text[last_fragment->length() - 1];
  65. if (!is_ascii_space(last_character))
  66. break;
  67. // FIXME: Use fragment's glyph run to determine the width of the last character.
  68. int last_character_width = last_fragment->layout_node().first_available_font().glyph_width(last_character);
  69. last_fragment->m_length -= 1;
  70. last_fragment->set_width(last_fragment->width() - last_character_width);
  71. m_width -= last_character_width;
  72. }
  73. }
  74. bool LineBox::is_empty_or_ends_in_whitespace() const
  75. {
  76. if (m_fragments.is_empty())
  77. return true;
  78. return m_fragments.last().ends_in_whitespace();
  79. }
  80. }