Merge pull request #1643 from storytold/codex/shared-shape-segments

Share shaping segment boundary state
This commit is contained in:
FredApps
2026-11-16 00:14:45 +01:00
committed by GitHub
5 changed files with 130 additions and 0 deletions
Generated
+1
View File
@@ -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",
]
+1
View File
@@ -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 }
+4
View File
@@ -27,9 +27,11 @@ mod fontconfig;
mod outline;
mod registry;
mod scan;
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};
@@ -37,7 +39,9 @@ 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};
pub use visual_order::{VisualGlyph, visual_order};
#[cfg(test)]
mod tests;
+55
View File
@@ -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<F, L, S> {
face: F,
level: L,
small_caps: bool,
strong_script: Option<S>,
}
impl<F: Copy + Eq, L: Copy + Eq, S: Copy + Eq> SegmentBoundary<F, L, S> {
/// 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<S>, 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::<u8, u8, u8>::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::<u8, u8, u8>::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));
}
}
+69
View File
@@ -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<Item = VisualGlyph>, paragraph_start: usize, levels: &[Level]) -> Vec<usize> {
let mut clusters: Vec<(Range<usize>, 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]);
}
}