Skip to content

Commit 08a3056

Browse files
committed
Use the parley cursor
1 parent 94cda8e commit 08a3056

File tree

3 files changed

+25
-52
lines changed

3 files changed

+25
-52
lines changed

internal/renderers/femtovg/fonts.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -134,38 +134,6 @@ pub fn layout(text: &str, options: LayoutOptions) -> parley::Layout<sharedfontiq
134134
layout
135135
}
136136

137-
pub fn get_cursor_location_and_size(
138-
layout: &parley::Layout<sharedfontique::Brush>,
139-
cursor_byte_offset: usize,
140-
offset: f32,
141-
) -> Option<(PhysicalPoint, f32)> {
142-
let mut cursor_point = None;
143-
144-
for line in layout.lines() {
145-
for item in line.items() {
146-
match item {
147-
parley::PositionedLayoutItem::GlyphRun(glyph_run) => {
148-
let range = glyph_run.run().text_range();
149-
let size = glyph_run.run().font_size();
150-
if range.contains(&cursor_byte_offset) {
151-
cursor_point = glyph_run
152-
.positioned_glyphs()
153-
.nth(cursor_byte_offset - range.start)
154-
.map(|glyph| (PhysicalPoint::new(glyph.x, glyph.y + offset), size));
155-
} else if cursor_byte_offset == range.end {
156-
cursor_point = glyph_run.positioned_glyphs().last().map(|glyph| {
157-
(PhysicalPoint::new(glyph.x + glyph.advance, glyph.y + offset), size)
158-
});
159-
}
160-
}
161-
parley::PositionedLayoutItem::InlineBox(_inline_box) => {}
162-
};
163-
}
164-
}
165-
166-
cursor_point
167-
}
168-
169137
pub fn get_offset(
170138
vertical_align: TextVerticalAlignment,
171139
max_height: PhysicalLength,

internal/renderers/femtovg/itemrenderer.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,21 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
532532
text_input.selection_background_color(),
533533
);
534534

535-
if let Some((cursor_point, cursor_size)) = cursor_visible
536-
.then_some(cursor_pos)
537-
.and_then(|cursor_pos| fonts::get_cursor_location_and_size(&layout, cursor_pos, offset))
538-
{
535+
if cursor_visible {
536+
let cursor = parley::layout::cursor::Cursor::from_byte_index(
537+
&layout,
538+
cursor_pos,
539+
Default::default(),
540+
);
541+
let rect = cursor
542+
.geometry(&layout, (text_input.text_cursor_width() * self.scale_factor).get());
543+
539544
let mut cursor_rect = femtovg::Path::new();
540545
cursor_rect.rect(
541-
cursor_point.x,
542-
cursor_point.y - cursor_size,
543-
(text_input.text_cursor_width() * self.scale_factor).get(),
544-
cursor_size,
546+
rect.center().x as _,
547+
rect.center().y as _,
548+
rect.width() as _,
549+
rect.height() as _,
545550
);
546551
canvas.fill_path(
547552
&cursor_rect,

internal/renderers/femtovg/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::num::NonZeroU32;
99
use std::pin::Pin;
1010
use std::rc::{Rc, Weak};
1111

12-
use i_slint_common::sharedfontique;
12+
use i_slint_common::sharedfontique::{self, parley};
1313
use i_slint_core::api::{RenderingNotifier, RenderingState, SetRenderingNotifierError};
1414
use i_slint_core::graphics::{euclid, rendering_metrics_collector::RenderingMetricsCollector};
1515
use i_slint_core::graphics::{BorderRadius, Rgba8Pixel};
@@ -332,14 +332,11 @@ impl<B: GraphicsBackend> RendererSealed for FemtoVGRenderer<B> {
332332
..Default::default()
333333
},
334334
);
335-
let _offset = fonts::get_offset(text_input.vertical_alignment(), height, &layout);
336-
337-
// TODO
338-
let result = text.len();
335+
let offset = fonts::get_offset(text_input.vertical_alignment(), height, &layout);
336+
let cursor = parley::layout::cursor::Cursor::from_point(&layout, pos.x, pos.y - offset);
339337

340338
let visual_representation = text_input.visual_representation(None);
341-
342-
visual_representation.map_byte_offset_from_byte_offset_in_visual_text(result)
339+
visual_representation.map_byte_offset_from_byte_offset_in_visual_text(cursor.index())
343340
}
344341

345342
fn text_input_cursor_rect_for_byte_offset(
@@ -366,12 +363,15 @@ impl<B: GraphicsBackend> RendererSealed for FemtoVGRenderer<B> {
366363
&text,
367364
fonts::LayoutOptions { max_width: Some(width), ..Default::default() },
368365
);
369-
let cursor_position = fonts::get_cursor_location_and_size(&layout, byte_offset, 0.0)
370-
.map(|location| location.0);
371-
366+
let cursor = parley::layout::cursor::Cursor::from_byte_index(
367+
&layout,
368+
byte_offset,
369+
Default::default(),
370+
);
371+
let rect = cursor.geometry(&layout, (text_input.text_cursor_width() * scale_factor).get());
372372
LogicalRect::new(
373-
cursor_position.unwrap_or_default() / scale_factor,
374-
LogicalSize::from_lengths(LogicalLength::new(1.0), font_size),
373+
LogicalPoint::new(rect.min_x() as _, rect.min_y() as _),
374+
LogicalSize::new(rect.width() as _, rect.height() as _),
375375
)
376376
}
377377

0 commit comments

Comments
 (0)