Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle inline boxes in cluster geometry and path #203

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions parley/src/layout/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl<'a, B: Brush> Cluster<'a, B> {
let mut path = ClusterPath::default();
if let Some((line_index, line)) = layout.line_for_byte_index(byte_index) {
path.line_index = line_index as u32;
for (run_index, run) in line.runs().enumerate() {
path.run_index = run_index as u32;
for run in line.runs() {
path.run_index = run.index;
if !run.text_range().contains(&byte_index) {
continue;
}
Expand All @@ -44,13 +44,17 @@ impl<'a, B: Brush> Cluster<'a, B> {
path.line_index = line_index as u32;
let mut offset = line.metrics().offset;
let last_run_index = line.len().saturating_sub(1);
for (run_index, run) in line.runs().enumerate() {
for run_index in 0..line.len() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we can't just use line.items(), as GlyphRunIter currently appears to skip the run on empty lines.

let advance = line.item(run_index).unwrap().advance;
let Some(run) = line.run(run_index) else {
offset += advance;
continue;
};
let is_last_run = run_index == last_run_index;
let run_advance = run.advance();
path.run_index = run_index as u32;
path.logical_index = 0;
if x > offset + run_advance && !is_last_run {
offset += run_advance;
if x > offset + advance && !is_last_run {
offset += advance;
continue;
}
let last_cluster_index = run.cluster_range().len().saturating_sub(1);
Expand Down Expand Up @@ -354,16 +358,15 @@ impl<'a, B: Brush> Cluster<'a, B> {
pub fn visual_offset(&self) -> Option<f32> {
let line = self.path.line(self.run.layout)?;
let mut offset = line.metrics().offset;
for run_index in 0..=self.path.run_index() {
let run = line.run(run_index)?;
if run_index != self.path.run_index() {
offset += run.advance();
} else {
let visual_index = run.logical_to_visual(self.path.logical_index())?;
for cluster in run.visual_clusters().take(visual_index) {
offset += cluster.advance();
}
}
for run_index in 0..self.path.run_index() {
let item = line.item(run_index)?;
offset += item.advance;
}

let run = line.run(self.path.run_index())?;
let visual_index = run.logical_to_visual(self.path.logical_index())?;
for cluster in run.visual_clusters().take(visual_index) {
offset += cluster.advance();
}
Some(offset)
}
Expand Down
19 changes: 16 additions & 3 deletions parley/src/layout/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#[cfg(feature = "accesskit")]
use super::LayoutAccessibility;
use super::{Affinity, BreakReason, Brush, Cluster, ClusterSide, Layout, Line};
use super::{
Affinity, BreakReason, Brush, Cluster, ClusterSide, Layout, Line, PositionedLayoutItem,
};
#[cfg(feature = "accesskit")]
use accesskit::TextPosition;
use alloc::vec::Vec;
Expand Down Expand Up @@ -826,17 +828,28 @@ impl Selection {
let mut start_x = metrics.offset as f64;
let mut cur_x = start_x;
let mut cluster_count = 0;
for run in line.runs() {
for cluster in run.visual_clusters() {
for item in line.items() {
let PositionedLayoutItem::GlyphRun(run) = item else {
continue;
};
let offset = run.offset();
let run = run.run();
for (ix, cluster) in run.visual_clusters().enumerate() {
let advance = cluster.advance() as f64;
if text_range.contains(&cluster.text_range().start) {
if ix == 0 {
cur_x = offset as f64;
}
cluster_count += 1;
cur_x += advance;
} else {
if cur_x != start_x {
let width = (cur_x - start_x).max(MIN_RECT_WIDTH);
f(Rect::new(start_x as _, line_min, start_x + width, line_max));
}
if ix == 0 {
cur_x = offset as f64;
}
cur_x += advance;
start_x = cur_x;
}
Expand Down
Loading