From d0a9c79933a54883cd5bc3dfe839f1dd699f2c69 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:42:04 +0000 Subject: [PATCH 1/4] Share shaping segment boundary state --- crates/font-catalog/src/lib.rs | 2 ++ crates/font-catalog/src/segments.rs | 55 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 crates/font-catalog/src/segments.rs diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 043667ee4..21b51ba1a 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -27,6 +27,7 @@ mod fontconfig; mod outline; mod registry; mod scan; +mod segments; mod shape; mod suitcase; pub mod system; @@ -37,6 +38,7 @@ pub use fallback::{cluster_face, drawable_characters}; pub use fontconfig::fontconfig_dirs; pub use outline::{FontOutline, GlyphPen, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; +pub use segments::SegmentBoundary; pub use shape::{RunGlyph, RunInput, ShapeRunError, shape_run}; #[cfg(test)] diff --git a/crates/font-catalog/src/segments.rs b/crates/font-catalog/src/segments.rs new file mode 100644 index 000000000..e36a05144 --- /dev/null +++ b/crates/font-catalog/src/segments.rs @@ -0,0 +1,55 @@ +//! Boundaries between runs that need separate shaping. + +/// Tracks the properties of the current shaping segment while walking grapheme clusters. +/// The caller resolves font faces, bidi levels and script classification; marks stay with the +/// preceding segment even when their face, level or case differs. +pub struct SegmentBoundary { + face: F, + level: L, + small_caps: bool, + strong_script: Option, +} + +impl SegmentBoundary { + /// Begin at the first segment's face and bidi level. + pub fn new(face: F, level: L) -> Self { + Self { face, level, small_caps: false, strong_script: None } + } + + /// Observe a cluster and return whether it begins a new shaping segment. Pass `None` for + /// common or inherited script and `true` for marks, joiners and variation selectors. + pub fn advance(&mut self, face: F, level: L, small_caps: bool, strong_script: Option, mark: bool) -> bool { + let script_change = matches!((self.strong_script, strong_script), (Some(a), Some(b)) if a != b); + let split = (self.face != face || self.level != level || self.small_caps != small_caps || script_change) && !mark; + if split { + self.face = face; + self.level = level; + self.small_caps = small_caps; + } + if let Some(script) = strong_script { + self.strong_script = Some(script); + } + split + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn preserves_marks_and_splits_on_face_direction_case_and_script() { + let mut state = SegmentBoundary::::new(1, 0); + assert!(!state.advance(1, 0, false, Some(1), false)); + assert!(!state.advance(2, 1, true, None, true)); + assert!(state.advance(2, 1, true, Some(2), false)); + assert!(!state.advance(2, 1, true, None, false)); + assert!(state.advance(2, 1, true, Some(1), false)); + } + #[test] + fn strong_script_on_a_mark_updates_the_following_boundary() { + let mut state = SegmentBoundary::::new(1, 0); + assert!(!state.advance(1, 0, false, Some(1), false)); + assert!(!state.advance(1, 0, false, Some(2), true)); + assert!(!state.advance(1, 0, false, Some(2), false)); + } +} From 524aaa78b8a04c0497fd28b9f12438ca3711e648 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:18:05 +0000 Subject: [PATCH 2/4] Share visual ordering of shaped clusters --- Cargo.lock | 1 + crates/font-catalog/Cargo.toml | 1 + crates/font-catalog/src/lib.rs | 2 + crates/font-catalog/src/visual_order.rs | 69 +++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 crates/font-catalog/src/visual_order.rs diff --git a/Cargo.lock b/Cargo.lock index 3e1487dca..877ee914e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1765,6 +1765,7 @@ dependencies = [ "kurbo 0.13.1", "skrifa 0.47.0", "thiserror 2.0.21", + "unicode-bidi", "windows-registry", "write-fonts 0.54.1", ] diff --git a/crates/font-catalog/Cargo.toml b/crates/font-catalog/Cargo.toml index c53d871b8..55b4b1088 100644 --- a/crates/font-catalog/Cargo.toml +++ b/crates/font-catalog/Cargo.toml @@ -19,6 +19,7 @@ harfrust = "0.13" kurbo = { workspace = true } skrifa = { version = "0.47", default-features = false, features = ["std"] } thiserror = { workspace = true } +unicode-bidi = "0.3.18" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] fontique = { version = "0.11.1", default-features = false, features = ["system", "fontconfig-dlopen"], optional = true } diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 21b51ba1a..6489bae5c 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -31,6 +31,7 @@ mod segments; mod shape; mod suitcase; pub mod system; +mod visual_order; pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, Match, Watch}; pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; @@ -40,6 +41,7 @@ pub use outline::{FontOutline, GlyphPen, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; pub use segments::SegmentBoundary; pub use shape::{RunGlyph, RunInput, ShapeRunError, shape_run}; +pub use visual_order::{VisualGlyph, visual_order}; #[cfg(test)] mod tests; diff --git a/crates/font-catalog/src/visual_order.rs b/crates/font-catalog/src/visual_order.rs new file mode 100644 index 000000000..bb753df9a --- /dev/null +++ b/crates/font-catalog/src/visual_order.rs @@ -0,0 +1,69 @@ +//! Visual ordering of shaped clusters after line bidi levels are resolved. +use std::ops::Range; +use unicode_bidi::{BidiInfo, Level}; + +/// A glyph's source byte, source length and original resolved level. +/// Zero-length glyphs represent list markers. +#[derive(Clone, Copy)] +pub struct VisualGlyph { + /// Byte offset in the original text. + pub byte: usize, + /// Source length in bytes, or zero for a list marker. + pub len: usize, + /// Original level, also used when the line levels lack this byte. + pub level: Level, +} + +/// Order glyph indices by cluster using caller-resolved line levels (UAX #9 L1–L2). +/// `levels` is indexed relative to `paragraph_start`. Adjacent marker glyphs at the +/// same byte form their own cluster and reverse internally at a right-to-left level. +pub fn visual_order(glyphs: impl IntoIterator, paragraph_start: usize, levels: &[Level]) -> Vec { + let mut clusters: Vec<(Range, VisualGlyph)> = Vec::new(); + for (i, glyph) in glyphs.into_iter().enumerate() { + if let Some((range, _)) = clusters.last_mut().filter(|(_, prev)| prev.byte == glyph.byte && (prev.len == 0) == (glyph.len == 0)) { + range.end = i + 1; + } else { + clusters.push((i..i + 1, glyph)); + } + } + let cluster_levels: Vec<_> = clusters + .iter() + .map(|(_, g)| if g.len == 0 { g.level } else { levels.get(g.byte.saturating_sub(paragraph_start)).copied().unwrap_or(g.level) }) + .collect(); + BidiInfo::reorder_visual(&cluster_levels) + .into_iter() + .filter_map(|i| clusters.get(i)) + .flat_map(|(range, glyph)| { + let mut indices: Vec<_> = range.clone().collect(); + if glyph.len == 0 && glyph.level.is_rtl() { + indices.reverse(); + } + indices + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn clusters_and_markers_keep_their_distinct_order() { + let l = Level::ltr(); + let r = Level::rtl(); + let glyphs = [ + VisualGlyph { byte: 5, len: 0, level: r }, + VisualGlyph { byte: 5, len: 0, level: r }, + VisualGlyph { byte: 5, len: 2, level: r }, + VisualGlyph { byte: 5, len: 2, level: r }, + VisualGlyph { byte: 7, len: 1, level: l }, + ]; + assert_eq!(visual_order(glyphs, 5, &[r, r, l]), vec![2, 3, 1, 0, 4]); + assert!(visual_order([], 0, &[]).is_empty()); + } + #[test] + fn missing_levels_fall_back_to_glyph_level() { + let r = Level::rtl(); + let glyphs = [VisualGlyph { byte: 2, len: 1, level: r }, VisualGlyph { byte: 3, len: 1, level: r }]; + assert_eq!(visual_order(glyphs, 0, &[]), vec![1, 0]); + } +} From 3d1b0b5edfb55c056a4de539751d647ae60a3dcf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:21:24 +0000 Subject: [PATCH 3/4] Share measured line break selection --- crates/font-catalog/src/lib.rs | 2 ++ crates/font-catalog/src/line_break.rs | 49 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 crates/font-catalog/src/line_break.rs diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 6489bae5c..9d3b8050e 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -24,6 +24,7 @@ mod catalog; mod env; mod fallback; mod fontconfig; +mod line_break; mod outline; mod registry; mod scan; @@ -37,6 +38,7 @@ pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; pub use fallback::{cluster_face, drawable_characters}; pub use fontconfig::fontconfig_dirs; +pub use line_break::settle_line_break; pub use outline::{FontOutline, GlyphPen, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; pub use segments::SegmentBoundary; diff --git a/crates/font-catalog/src/line_break.rs b/crates/font-catalog/src/line_break.rs new file mode 100644 index 000000000..10983d717 --- /dev/null +++ b/crates/font-catalog/src/line_break.rs @@ -0,0 +1,49 @@ +//! Choose a line break after measuring candidate lines as drawn. + +/// Choose the latest candidate that fits when a tab changes the line's drawn width. +/// +/// `breaks` must be sorted by glyph index and the boolean marks hyphenated breaks. +/// `checks` limits the candidates measured individually from the end; older non-hyphenated +/// candidates are bisected, assuming their drawn widths are nondecreasing. If none fits, +/// the earliest eligible break is returned. Without a tab or a measurement callback the +/// original `chosen` break is returned. +pub fn settle_line_break( + tabbed: bool, + checks: usize, + start: usize, + chosen: (usize, bool), + breaks: &[(usize, bool)], + fits: Option<&dyn Fn(usize, bool) -> bool>, +) -> (usize, bool) { + let Some(fits) = fits.filter(|_| tabbed) else { return chosen }; + if fits(chosen.0, chosen.1) { + return chosen; + } + let earlier = breaks.get(breaks.partition_point(|b| b.0 <= start)..breaks.partition_point(|b| b.0 < chosen.0)).unwrap_or_default(); + if let Some(&b) = earlier.iter().rev().take(checks).find(|b| fits(b.0, b.1)) { + return b; + } + let rest = earlier.get(..earlier.len().saturating_sub(checks)).unwrap_or_default(); + let plain: Vec<&(usize, bool)> = rest.iter().filter(|b| !b.1).collect(); + let fitting = plain.partition_point(|b| fits(b.0, b.1)); + fitting.checked_sub(1).and_then(|k| plain.get(k).copied()).or(earlier.first()).copied().unwrap_or(chosen) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn measures_latest_candidates_and_preserves_hyphenation() { + let breaks = [(1, false), (2, false), (3, true), (4, false)]; + let fits = |i, hyphen| i <= 2 || (i == 3 && hyphen); + assert_eq!(settle_line_break(true, 2, 0, (4, false), &breaks, Some(&fits)), (3, true)); + assert_eq!(settle_line_break(false, 2, 0, (4, false), &breaks, Some(&fits)), (4, false)); + } + #[test] + fn bisects_older_plain_breaks_and_falls_back_to_first_eligible() { + let breaks = [(1, false), (2, true), (3, false), (4, false), (5, false)]; + assert_eq!(settle_line_break(true, 1, 0, (5, false), &breaks, Some(&|i, _| i <= 3)), (3, false)); + assert_eq!(settle_line_break(true, 1, 1, (5, false), &breaks, Some(&|_, _| false)), (2, true)); + assert_eq!(settle_line_break(true, 1, 5, (5, false), &breaks, Some(&|_, _| false)), (5, false)); + } +} From 70cfdb281775d226c2af592e36b1063e124a17a9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:25:04 +0000 Subject: [PATCH 4/4] Share ligature caret mapping --- Cargo.lock | 1 + crates/font-catalog/Cargo.toml | 1 + crates/font-catalog/src/lib.rs | 2 ++ crates/font-catalog/src/ligature.rs | 33 +++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 crates/font-catalog/src/ligature.rs diff --git a/Cargo.lock b/Cargo.lock index 877ee914e..42e7a51b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1766,6 +1766,7 @@ dependencies = [ "skrifa 0.47.0", "thiserror 2.0.21", "unicode-bidi", + "unicode-segmentation", "windows-registry", "write-fonts 0.54.1", ] diff --git a/crates/font-catalog/Cargo.toml b/crates/font-catalog/Cargo.toml index 55b4b1088..d4620862b 100644 --- a/crates/font-catalog/Cargo.toml +++ b/crates/font-catalog/Cargo.toml @@ -20,6 +20,7 @@ kurbo = { workspace = true } skrifa = { version = "0.47", default-features = false, features = ["std"] } thiserror = { workspace = true } unicode-bidi = "0.3.18" +unicode-segmentation = "1.13" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] fontique = { version = "0.11.1", default-features = false, features = ["system", "fontconfig-dlopen"], optional = true } diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 9d3b8050e..992b75080 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -24,6 +24,7 @@ mod catalog; mod env; mod fallback; mod fontconfig; +mod ligature; mod line_break; mod outline; mod registry; @@ -38,6 +39,7 @@ pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; pub use fallback::{cluster_face, drawable_characters}; pub use fontconfig::fontconfig_dirs; +pub use ligature::ligature_caret_stops; pub use line_break::settle_line_break; pub use outline::{FontOutline, GlyphPen, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; diff --git a/crates/font-catalog/src/ligature.rs b/crates/font-catalog/src/ligature.rs new file mode 100644 index 000000000..fa9efe5da --- /dev/null +++ b/crates/font-catalog/src/ligature.rs @@ -0,0 +1,33 @@ +//! Map ligature caret fractions to grapheme boundaries. +use unicode_segmentation::UnicodeSegmentation; + +/// Return logical source-byte caret stops inside a shaped cluster. `font_carets` receives the +/// number of stops and may return their normalized positions along the glyph's logical width. +/// Missing or incorrectly sized font data falls back to equal divisions of the width. +pub fn ligature_caret_stops(text: &str, byte: usize, len: usize, font_carets: impl FnOnce(usize) -> Option>) -> Vec<(usize, f64)> { + let Some(s) = byte.checked_add(len).and_then(|end| text.get(byte..end)).filter(|_| len > 1) else { return Vec::new() }; + let starts: Vec = s.grapheme_indices(true).skip(1).map(|(i, _)| byte + i).collect(); + if starts.is_empty() { + return Vec::new(); + } + let parts = starts.len() + 1; + let mut at = + font_carets(starts.len()).filter(|c| c.len() == starts.len()).unwrap_or_else(|| (1..parts).map(|j| j as f64 / parts as f64).collect()); + at.sort_by(f64::total_cmp); + starts.into_iter().zip(at).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn maps_graphemes_and_sorts_font_positions() { + assert_eq!(ligature_caret_stops("ábc", 0, 5, |_| Some(vec![0.8, 0.3])), vec![(3, 0.3), (4, 0.8)]); + } + #[test] + fn falls_back_and_rejects_invalid_ranges() { + assert_eq!(ligature_caret_stops("abc", 0, 3, |_| Some(vec![0.5])), vec![(1, 1.0 / 3.0), (2, 2.0 / 3.0)]); + assert!(ligature_caret_stops("abc", usize::MAX, 2, |_| None).is_empty()); + assert!(ligature_caret_stops("é", 1, 1, |_| None).is_empty()); + } +}