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

Fix text editing for layouts which contain inline boxes #299

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
92 changes: 54 additions & 38 deletions parley/src/layout/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright 2021 the Parley Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use super::{BreakReason, Brush, Cluster, ClusterInfo, Glyph, Layout, Line, Range, Run, Style};
use super::{
BreakReason, Brush, Cluster, ClusterInfo, Glyph, Layout, Line, LineItem, Range, Run, Style,
};
use swash::text::cluster::Whitespace;

/// Defines the visual side of the cluster for hit testing.
Expand All @@ -21,8 +23,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,32 +46,39 @@ 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() {
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;
continue;
}
let last_cluster_index = run.cluster_range().len().saturating_sub(1);
for (visual_index, cluster) in run.visual_clusters().enumerate() {
let is_last_cluster = is_last_run && visual_index == last_cluster_index;
path.logical_index =
run.visual_to_logical(visual_index).unwrap_or_default() as u32;
let cluster_advance = cluster.advance();
let edge = offset;
offset += cluster_advance;
if x > offset && !is_last_cluster {
continue;
for item in line.items_nonpositioned() {
match item {
LineItem::Run(run) => {
let is_last_run = run.index as usize == last_run_index;
let run_advance = run.advance();
path.run_index = run.index;
path.logical_index = 0;
if x > offset + run_advance && !is_last_run {
offset += run_advance;
continue;
}
let last_cluster_index = run.cluster_range().len().saturating_sub(1);
for (visual_index, cluster) in run.visual_clusters().enumerate() {
let is_last_cluster = is_last_run && visual_index == last_cluster_index;
path.logical_index =
run.visual_to_logical(visual_index).unwrap_or_default() as u32;
let cluster_advance = cluster.advance();
let edge = offset;
offset += cluster_advance;
if x > offset && !is_last_cluster {
continue;
}
let side = if x <= edge + cluster_advance * 0.5 {
ClusterSide::Left
} else {
ClusterSide::Right
};
return Some((path.cluster(layout)?, side));
}
}
LineItem::InlineBox(inline_box) => {
offset += inline_box.width;
}
let side = if x <= edge + cluster_advance * 0.5 {
ClusterSide::Left
} else {
ClusterSide::Right
};
return Some((path.cluster(layout)?, side));
}
}
}
Expand Down Expand Up @@ -246,7 +255,7 @@ impl<'a, B: Brush> Cluster<'a, B> {
for line_index in self.path.line_index()..layout.len() {
let line = layout.get(line_index)?;
for run_index in run_index..line.len() {
if let Some(run) = line.run(run_index) {
if let Some(run) = line.item(run_index).and_then(|item| item.run()) {
if !run.cluster_range().is_empty() {
return ClusterPath {
line_index: line_index as u32,
Expand Down Expand Up @@ -287,7 +296,7 @@ impl<'a, B: Brush> Cluster<'a, B> {
let line = layout.get(line_index)?;
let first_run = run_index.unwrap_or(line.len());
for run_index in (0..first_run).rev() {
if let Some(run) = line.run(run_index) {
if let Some(run) = line.item(run_index).and_then(|item| item.run()) {
let range = run.cluster_range();
if !range.is_empty() {
return ClusterPath {
Expand Down Expand Up @@ -361,13 +370,20 @@ impl<'a, B: Brush> Cluster<'a, B> {
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();
let item = line.item(run_index)?;
match item {
LineItem::Run(run) => {
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();
}
}
}
LineItem::InlineBox(inline_box) => {
offset += inline_box.width;
}
}
}
Expand Down Expand Up @@ -441,7 +457,7 @@ impl ClusterPath {

/// Returns the run for this path and the specified layout.
pub fn run<'a, B: Brush>(&self, layout: &'a Layout<B>) -> Option<Run<'a, B>> {
self.line(layout)?.run(self.run_index())
self.line(layout)?.item(self.run_index())?.run()
}

/// Returns the cluster for this path and the specified layout.
Expand Down
80 changes: 57 additions & 23 deletions parley/src/layout/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#[cfg(feature = "accesskit")]
use super::LayoutAccessibility;
use super::{Affinity, BreakReason, Brush, Cluster, ClusterSide, Layout, Line};
use super::{Affinity, BreakReason, Brush, Cluster, ClusterSide, Layout, Line, LineItem};
#[cfg(feature = "accesskit")]
use accesskit::TextPosition;
use alloc::vec::Vec;
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Cursor {
) -> Option<Self> {
let (line_index, run_index) = *layout_access.run_paths_by_access_id.get(&pos.node)?;
let line = layout.get(line_index)?;
let run = line.run(run_index)?;
let run = line.item(run_index)?.run()?;
let index = run
.get(pos.character_index)
.map(|cluster| cluster.text_range().start)
Expand Down Expand Up @@ -799,10 +799,7 @@ impl Selection {
/// This avoids allocation if the intent is to render the rectangles
/// immediately.
pub fn geometry_with<B: Brush>(&self, layout: &Layout<B>, mut f: impl FnMut(Rect)) {
// Ensure we add some visual indicator for selected empty
// lines.
// Make this configurable?
const MIN_RECT_WIDTH: f64 = 8.0;
const NEWLINE_WHITESPACE_WIDTH_RATIO: f64 = 0.25;
if self.is_collapsed() {
return;
}
Expand All @@ -824,36 +821,73 @@ impl Selection {
let metrics = line.metrics();
let line_min = metrics.min_coord as f64;
let line_max = metrics.max_coord as f64;
// Trailing whitespace to indicate that the newline character at the
// end of this line is selected. It's based on the ascent and
// descent so it doesn't change with the line height.
//
// TODO: the width of this whitespace should be the width of a space
// (U+0020) character.
let newline_whitespace = if line.break_reason() == BreakReason::Explicit {
(metrics.ascent as f64 + metrics.descent as f64) * NEWLINE_WHITESPACE_WIDTH_RATIO
} else {
0.0
};
if line_ix == line_start_ix || line_ix == line_end_ix {
// We only need to run the expensive logic on the first and
// last lines
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() {
let advance = cluster.advance() as f64;
if text_range.contains(&cluster.text_range().start) {
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, line_min, start_x + width, line_max));
let mut box_advance = 0.0;
let mut have_seen_any_runs = false;
for item in line.items_nonpositioned() {
match item {
LineItem::Run(run) => {
have_seen_any_runs = true;
for cluster in run.visual_clusters() {
let advance = cluster.advance() as f64 + box_advance;
box_advance = 0.0;
if text_range.contains(&cluster.text_range().start) {
cluster_count += 1;
cur_x += advance;
} else {
if cur_x != start_x {
f(Rect::new(start_x, line_min, cur_x, line_max));
}
cur_x += advance;
start_x = cur_x;
}
}
}
LineItem::InlineBox(inline_box) => {
box_advance += inline_box.width as f64;
// HACK: Don't display selections for inline boxes
// if they're the first thing in the line. This
// makes the selection match the cursor position.
if !have_seen_any_runs {
cur_x += box_advance;
box_advance = 0.0;
start_x = cur_x;
}
cur_x += advance;
start_x = cur_x;
}
}
}
if cur_x != start_x || (cluster_count != 0 && line.metrics().advance == 0.0) {
let width = (cur_x - start_x).max(MIN_RECT_WIDTH);
f(Rect::new(start_x, line_min, start_x + width, line_max));
let mut end_x = cur_x;
if line_ix != line_end_ix || (cluster_count != 0 && metrics.advance == 0.0) {
end_x += newline_whitespace;
}
if end_x != start_x {
f(Rect::new(start_x, line_min, end_x, line_max));
}
} else {
let x = metrics.offset as f64;
let width = (metrics.advance as f64).max(MIN_RECT_WIDTH);
f(Rect::new(x, line_min, x + width, line_max));
let width = metrics.advance as f64;
f(Rect::new(
x,
line_min,
x + width + newline_whitespace,
line_max,
));
}
}
}
Expand Down
Loading