LineBox.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Utf8View.h>
  27. #include <LibWeb/Layout/LayoutNode.h>
  28. #include <LibWeb/Layout/LayoutText.h>
  29. #include <LibWeb/Layout/LayoutBox.h>
  30. #include <LibWeb/Layout/LineBox.h>
  31. #include <ctype.h>
  32. namespace Web {
  33. void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length, int width, int height)
  34. {
  35. bool text_align_is_justify = layout_node.style().string_or_fallback(CSS::PropertyID::TextAlign, "left") == "justify";
  36. if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) {
  37. // The fragment we're adding is from the last LayoutNode on the line.
  38. // Expand the last fragment instead of adding a new one with the same LayoutNode.
  39. m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
  40. m_fragments.last().set_width(m_fragments.last().width() + width);
  41. } else {
  42. m_fragments.append(make<LineBoxFragment>(layout_node, start, length, Gfx::FloatPoint(m_width, 0), Gfx::FloatSize(width, height)));
  43. }
  44. m_width += width;
  45. if (is<LayoutBox>(layout_node))
  46. const_cast<LayoutBox&>(to<LayoutBox>(layout_node)).set_containing_line_box_fragment(m_fragments.last());
  47. }
  48. void LineBox::trim_trailing_whitespace()
  49. {
  50. while (!m_fragments.is_empty() && m_fragments.last().is_justifiable_whitespace()) {
  51. auto fragment = m_fragments.take_last();
  52. m_width -= fragment->width();
  53. }
  54. if (m_fragments.is_empty())
  55. return;
  56. auto last_text = m_fragments.last().text();
  57. if (last_text.is_null())
  58. return;
  59. auto& last_fragment = m_fragments.last();
  60. int space_width = last_fragment.layout_node().style().font().glyph_width(' ');
  61. while (last_fragment.length() && isspace(last_text[last_fragment.length() - 1])) {
  62. last_fragment.m_length -= 1;
  63. last_fragment.set_width(last_fragment.width() - space_width);
  64. m_width -= space_width;
  65. }
  66. }
  67. }