LineBoxFragment.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. int LineBoxFragment::text_index_at(float x) const
  54. {
  55. if (!layout_node().is_text())
  56. return 0;
  57. auto& layout_text = to<LayoutText>(layout_node());
  58. auto& font = layout_text.style().font();
  59. Utf8View view(text());
  60. float relative_x = x - m_rect.location().x();
  61. float glyph_spacing = font.glyph_spacing();
  62. float width_so_far = 0;
  63. for (auto it = view.begin(); it != view.end(); ++it) {
  64. float glyph_width = font.glyph_or_emoji_width(*it);
  65. if ((width_so_far + glyph_width + glyph_spacing) > relative_x)
  66. return m_start + view.byte_offset_of(it);
  67. width_so_far += glyph_width + glyph_spacing;
  68. }
  69. return m_start + m_length - 1;
  70. }
  71. }