Serenity Operating System
at hosted 81 lines 3.0 kB view raw
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 27#include <AK/Utf8View.h> 28#include <LibGUI/Painter.h> 29#include <LibWeb/Layout/LayoutDocument.h> 30#include <LibWeb/Layout/LayoutText.h> 31#include <LibWeb/Layout/LineBoxFragment.h> 32#include <LibWeb/RenderingContext.h> 33 34namespace Web { 35 36void LineBoxFragment::render(RenderingContext& context) 37{ 38 for (auto* ancestor = layout_node().parent(); ancestor; ancestor = ancestor->parent()) { 39 if (!ancestor->is_visible()) 40 return; 41 } 42 43 if (is<LayoutText>(layout_node())) { 44 to<LayoutText>(layout_node()).render_fragment(context, *this); 45 } 46} 47 48bool LineBoxFragment::is_justifiable_whitespace() const 49{ 50 return text() == " "; 51} 52 53StringView LineBoxFragment::text() const 54{ 55 if (!is<LayoutText>(layout_node())) 56 return {}; 57 return to<LayoutText>(layout_node()).text_for_rendering().substring_view(m_start, m_length); 58} 59 60int 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 68 float relative_x = x - m_rect.location().x(); 69 float glyph_spacing = font.glyph_spacing(); 70 71 float width_so_far = 0; 72 for (auto it = view.begin(); it != view.end(); ++it) { 73 float glyph_width = font.glyph_or_emoji_width(*it); 74 if ((width_so_far + glyph_width + glyph_spacing) > relative_x) 75 return m_start + view.byte_offset_of(it); 76 width_so_far += glyph_width + glyph_spacing; 77 } 78 return m_start + m_length - 1; 79} 80 81}