From add90a18c802a08392e89622a263b31dc5124853 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 21 Jun 2026 20:41:08 -0700 Subject: [PATCH] More byte index --- crates/egui_extras/src/syntax_highlighting.rs | 2 +- crates/epaint/src/text/text_layout.rs | 6 ++++-- crates/epaint/src/text/text_layout_types.rs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/egui_extras/src/syntax_highlighting.rs b/crates/egui_extras/src/syntax_highlighting.rs index 899e08fda..c3981b0e2 100644 --- a/crates/egui_extras/src/syntax_highlighting.rs +++ b/crates/egui_extras/src/syntax_highlighting.rs @@ -604,7 +604,7 @@ impl Highlighter { } #[cfg(feature = "syntect")] -fn as_byte_range(whole: &str, range: &str) -> std::ops::Range { +fn as_byte_range(whole: &str, range: &str) -> egui::text::ByteRange { use egui::text::ByteIndex; let whole_start = whole.as_ptr() as usize; let range_start = range.as_ptr() as usize; diff --git a/crates/epaint/src/text/text_layout.rs b/crates/epaint/src/text/text_layout.rs index 73878ed93..34ef60111 100644 --- a/crates/epaint/src/text/text_layout.rs +++ b/crates/epaint/src/text/text_layout.rs @@ -9,6 +9,7 @@ use crate::{ Color32, Mesh, Stroke, Vertex, stroke::PathStroke, text::{ + ByteIndex, ByteRange, font::{StyledMetrics, UvRect, is_cjk, is_cjk_break_allowed}, fonts::FontFaceKey, }, @@ -217,7 +218,7 @@ struct TextRun { font_key: FontFaceKey, /// Byte range within the section text. - byte_range: std::ops::Range, + byte_range: ByteRange, } /// Emit shaped glyphs from a [`harfrust::GlyphBuffer`] into a [`Paragraph`]. @@ -484,7 +485,7 @@ fn layout_section( let num_runs = runs.len(); for (run_idx, run) in runs.iter().enumerate() { - let run_text = &segment[run.byte_range.clone()]; + let run_text = &segment[run.byte_range.as_usize()]; let Some(font_face) = font.fonts_by_id.get(&run.font_key) else { continue; }; @@ -1373,6 +1374,7 @@ fn segment_into_runs(font: &mut Font<'_>, text: &str, out: &mut Vec) { out.clear(); for (byte_offset, grapheme_str) in text.grapheme_indices(true) { + let byte_offset = ByteIndex(byte_offset); let byte_end = byte_offset + grapheme_str.len(); let base_char = grapheme_str.chars().next().unwrap_or(' '); diff --git a/crates/epaint/src/text/text_layout_types.rs b/crates/epaint/src/text/text_layout_types.rs index 45b7f201d..5ae784468 100644 --- a/crates/epaint/src/text/text_layout_types.rs +++ b/crates/epaint/src/text/text_layout_types.rs @@ -342,7 +342,7 @@ pub struct LayoutSection { pub leading_space: f32, /// Range into [`LayoutJob::text`]. - pub byte_range: Range, + pub byte_range: ByteRange, /// How to format the text in this section (font, color, etc). pub format: TextFormat,