From 7beecaa43d1f3b9da5827d7ea616546dc3b29fba Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Thu, 12 Jun 2025 14:49:47 +0200 Subject: [PATCH] LibWeb: Rename `PaintableFragment::m_start` and `::m_length` Make it extra clear that we're dealing with byte offsets here. No functional changes. --- Libraries/LibWeb/Dump.cpp | 6 ++-- Libraries/LibWeb/Painting/PaintableBox.cpp | 10 +++---- .../LibWeb/Painting/PaintableFragment.cpp | 28 +++++++++---------- Libraries/LibWeb/Painting/PaintableFragment.h | 8 +++--- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Libraries/LibWeb/Dump.cpp b/Libraries/LibWeb/Dump.cpp index ea54cc9b2bf..c4b2d235652 100644 --- a/Libraries/LibWeb/Dump.cpp +++ b/Libraries/LibWeb/Dump.cpp @@ -386,15 +386,15 @@ void dump_tree(StringBuilder& builder, Layout::Node const& layout_node, bool sho color_off, fragment.layout_node().class_name()); builder.appendff("start: {}, length: {}, rect: {} baseline: {}\n", - fragment.start(), - fragment.length(), + fragment.start_byte_offset(), + fragment.length_in_bytes(), fragment.absolute_rect(), fragment.baseline()); if (is(fragment.layout_node())) { for (size_t i = 0; i < indent; ++i) builder.append(" "sv); auto const& layout_text = static_cast(fragment.layout_node()); - auto fragment_text = MUST(layout_text.text_for_rendering().substring_from_byte_offset(fragment.start(), fragment.length())); + auto fragment_text = MUST(layout_text.text_for_rendering().substring_from_byte_offset(fragment.start_byte_offset(), fragment.length_in_bytes())); builder.appendff(" \"{}\"\n", fragment_text); } }; diff --git a/Libraries/LibWeb/Painting/PaintableBox.cpp b/Libraries/LibWeb/Painting/PaintableBox.cpp index d6d3452f246..d974a37cf51 100644 --- a/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -677,7 +677,7 @@ void paint_cursor_if_needed(PaintContext& context, TextPaintable const& paintabl auto cursor_position_code_point_offset = fragment.utf16_view().code_point_offset_of(cursor_position->offset()); cursor_position_byte_offset = fragment.utf8_view().byte_offset_of(cursor_position_code_point_offset); } - if (cursor_position_byte_offset < fragment.start() || cursor_position_byte_offset > (fragment.start() + fragment.length())) + if (cursor_position_byte_offset < fragment.start_byte_offset() || cursor_position_byte_offset > (fragment.start_byte_offset() + fragment.length_in_bytes())) return; auto active_element = document.active_element(); @@ -696,7 +696,7 @@ void paint_cursor_if_needed(PaintContext& context, TextPaintable const& paintabl auto const& font = fragment.glyph_run() ? fragment.glyph_run()->font() : fragment.layout_node().first_available_font(); auto utf8_text = fragment.utf8_view(); - auto cursor_offset = font.width(utf8_text.substring_view(fragment.start(), cursor_position_byte_offset - fragment.start())); + auto cursor_offset = font.width(utf8_text.substring_view(fragment.start_byte_offset(), cursor_position_byte_offset - fragment.start_byte_offset())); CSSPixelRect cursor_rect { fragment_rect.x() + CSSPixels::nearest_value_for(cursor_offset), @@ -1217,7 +1217,7 @@ TraversalDecision PaintableWithLines::hit_test(CSSPixelPoint position, HitTestTy if (fragment_absolute_rect.bottom() - 1 <= transformed_position_adjusted_by_scroll_offset.y()) { // fully below the fragment HitTestResult hit_test_result { .paintable = const_cast(fragment.paintable()), - .index_in_node = fragment.index_in_node_for_byte_offset(fragment.start() + fragment.length()), + .index_in_node = fragment.index_in_node_for_byte_offset(fragment.start_byte_offset() + fragment.length_in_bytes()), .vertical_distance = transformed_position_adjusted_by_scroll_offset.y() - fragment_absolute_rect.bottom(), }; if (callback(hit_test_result) == TraversalDecision::Break) @@ -1226,7 +1226,7 @@ TraversalDecision PaintableWithLines::hit_test(CSSPixelPoint position, HitTestTy if (transformed_position_adjusted_by_scroll_offset.x() < fragment_absolute_rect.left()) { HitTestResult hit_test_result { .paintable = const_cast(fragment.paintable()), - .index_in_node = fragment.index_in_node_for_byte_offset(fragment.start()), + .index_in_node = fragment.index_in_node_for_byte_offset(fragment.start_byte_offset()), .vertical_distance = 0, .horizontal_distance = fragment_absolute_rect.left() - transformed_position_adjusted_by_scroll_offset.x(), }; @@ -1235,7 +1235,7 @@ TraversalDecision PaintableWithLines::hit_test(CSSPixelPoint position, HitTestTy } else if (transformed_position_adjusted_by_scroll_offset.x() > fragment_absolute_rect.right()) { HitTestResult hit_test_result { .paintable = const_cast(fragment.paintable()), - .index_in_node = fragment.index_in_node_for_byte_offset(fragment.start() + fragment.length()), + .index_in_node = fragment.index_in_node_for_byte_offset(fragment.start_byte_offset() + fragment.length_in_bytes()), .vertical_distance = 0, .horizontal_distance = transformed_position_adjusted_by_scroll_offset.x() - fragment_absolute_rect.right(), }; diff --git a/Libraries/LibWeb/Painting/PaintableFragment.cpp b/Libraries/LibWeb/Painting/PaintableFragment.cpp index 1c17b9a1856..5992695146a 100644 --- a/Libraries/LibWeb/Painting/PaintableFragment.cpp +++ b/Libraries/LibWeb/Painting/PaintableFragment.cpp @@ -19,8 +19,8 @@ PaintableFragment::PaintableFragment(Layout::LineBoxFragment const& fragment) , m_offset(fragment.offset()) , m_size(fragment.size()) , m_baseline(fragment.baseline()) - , m_start(fragment.start()) - , m_length(fragment.length()) + , m_start_byte_offset(fragment.start()) + , m_length_in_bytes(fragment.length()) , m_glyph_run(fragment.glyph_run()) , m_writing_mode(fragment.writing_mode()) { @@ -37,9 +37,9 @@ CSSPixelRect const PaintableFragment::absolute_rect() const size_t PaintableFragment::index_in_node_for_byte_offset(size_t byte_offset) const { - if (m_length == 0) + if (m_length_in_bytes == 0) return 0; - if (byte_offset >= m_start + m_length) + if (byte_offset >= m_start_byte_offset + m_length_in_bytes) return utf16_view().length_in_code_units(); auto code_point_offset = utf8_view().code_point_offset_of(byte_offset); return utf16_view().code_unit_offset_of(code_point_offset); @@ -63,7 +63,7 @@ size_t PaintableFragment::index_in_node_for_point(CSSPixelPoint position) const return 0; // Find the code point offset of the glyph matching the position. - auto code_point_offset = utf8_view().code_point_offset_of(m_start); + auto code_point_offset = utf8_view().code_point_offset_of(m_start_byte_offset); auto const& glyphs = m_glyph_run->glyphs(); auto smallest_distance = AK::NumericLimits::max(); for (size_t i = 0; i < glyphs.size(); ++i) { @@ -97,25 +97,25 @@ CSSPixelRect PaintableFragment::range_rect(size_t start_offset_in_code_units, si auto code_unit_to_byte_offset = [&](size_t offset_in_code_units) -> size_t { auto text_in_utf16 = utf16_view(); if (offset_in_code_units >= text_in_utf16.length_in_code_units()) - return m_length; + return m_length_in_bytes; auto offset_code_point = text_in_utf16.code_point_offset_of(offset_in_code_units); auto byte_offset = utf8_view().byte_offset_of(offset_code_point); - if (byte_offset <= m_start) + if (byte_offset <= m_start_byte_offset) return 0; - if (byte_offset > m_start + m_length) - return m_length; - return byte_offset - m_start; + if (byte_offset > m_start_byte_offset + m_length_in_bytes) + return m_length_in_bytes; + return byte_offset - m_start_byte_offset; }; // We operate on the UTF-8 string that is part of this fragment. - auto text = utf8_view().substring_view(m_start, m_length); + auto text = utf8_view().substring_view(m_start_byte_offset, m_length_in_bytes); if (paintable().selection_state() == Paintable::SelectionState::StartAndEnd) { auto selection_start_in_this_fragment = code_unit_to_byte_offset(start_offset_in_code_units); auto selection_end_in_this_fragment = code_unit_to_byte_offset(end_offset_in_code_units); // we are in the start/end node (both the same) - if (selection_start_in_this_fragment >= m_length) + if (selection_start_in_this_fragment >= m_length_in_bytes) return {}; if (selection_end_in_this_fragment == 0) return {}; @@ -143,10 +143,10 @@ CSSPixelRect PaintableFragment::range_rect(size_t start_offset_in_code_units, si } if (paintable().selection_state() == Paintable::SelectionState::Start) { auto selection_start_in_this_fragment = code_unit_to_byte_offset(start_offset_in_code_units); - auto selection_end_in_this_fragment = m_length; + auto selection_end_in_this_fragment = m_length_in_bytes; // we are in the start node - if (selection_start_in_this_fragment >= m_length) + if (selection_start_in_this_fragment >= m_length_in_bytes) return {}; auto pixel_distance_to_first_selected_character = CSSPixels::nearest_value_for(font.width(text.substring_view(0, selection_start_in_this_fragment))); diff --git a/Libraries/LibWeb/Painting/PaintableFragment.h b/Libraries/LibWeb/Painting/PaintableFragment.h index 9b1ae7c5c51..91d670d0b47 100644 --- a/Libraries/LibWeb/Painting/PaintableFragment.h +++ b/Libraries/LibWeb/Painting/PaintableFragment.h @@ -22,8 +22,8 @@ public: Layout::Node const& layout_node() const { return m_layout_node; } Paintable const& paintable() const { return *m_layout_node->first_paintable(); } - size_t start() const { return m_start; } - size_t length() const { return m_length; } + size_t start_byte_offset() const { return m_start_byte_offset; } + size_t length_in_bytes() const { return m_length_in_bytes; } CSSPixels baseline() const { return m_baseline; } CSSPixelPoint offset() const { return m_offset; } @@ -58,8 +58,8 @@ private: CSSPixelPoint m_offset; CSSPixelSize m_size; CSSPixels m_baseline; - size_t m_start; - size_t m_length; + size_t m_start_byte_offset; + size_t m_length_in_bytes; RefPtr m_glyph_run; CSS::WritingMode m_writing_mode; Vector m_shadows;