LineBoxFragment.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <LibGUI/Painter.h>
  28. #include <LibWeb/Layout/LayoutDocument.h>
  29. #include <LibWeb/Layout/LayoutText.h>
  30. #include <LibWeb/Layout/LineBoxFragment.h>
  31. #include <LibWeb/RenderingContext.h>
  32. namespace Web {
  33. void LineBoxFragment::render(RenderingContext& context)
  34. {
  35. for (auto* ancestor = layout_node().parent(); ancestor; ancestor = ancestor->parent()) {
  36. if (!ancestor->is_visible())
  37. return;
  38. }
  39. if (is<LayoutText>(layout_node())) {
  40. to<LayoutText>(layout_node()).render_fragment(context, *this);
  41. }
  42. }
  43. bool LineBoxFragment::is_justifiable_whitespace() const
  44. {
  45. return text() == " ";
  46. }
  47. StringView LineBoxFragment::text() const
  48. {
  49. if (!is<LayoutText>(layout_node()))
  50. return {};
  51. return to<LayoutText>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
  52. }
  53. const Gfx::FloatRect LineBoxFragment::absolute_rect() const
  54. {
  55. Gfx::FloatRect rect { {}, size() };
  56. rect.set_location(m_layout_node.containing_block()->absolute_position());
  57. rect.move_by(offset());
  58. return rect;
  59. }
  60. int LineBoxFragment::text_index_at(float x) const
  61. {
  62. if (!layout_node().is_text())
  63. return 0;
  64. auto& layout_text = to<LayoutText>(layout_node());
  65. auto& font = layout_text.style().font();
  66. Utf8View view(text());
  67. float relative_x = x - absolute_x();
  68. float glyph_spacing = font.glyph_spacing();
  69. float width_so_far = 0;
  70. for (auto it = view.begin(); it != view.end(); ++it) {
  71. float glyph_width = font.glyph_or_emoji_width(*it);
  72. if ((width_so_far + glyph_width + glyph_spacing) > relative_x)
  73. return m_start + view.byte_offset_of(it);
  74. width_so_far += glyph_width + glyph_spacing;
  75. }
  76. return m_start + m_length - 1;
  77. }
  78. }