Merge pull request #1663 from storytold/codex/shared-line-settle

Share measured line break selection
This commit is contained in:
FredApps
2026-11-16 00:14:33 +01:00
committed by GitHub
5 changed files with 88 additions and 0 deletions
Generated
+1
View File
@@ -1766,6 +1766,7 @@ dependencies = [
"skrifa 0.47.0",
"thiserror 2.0.21",
"unicode-bidi",
"unicode-segmentation",
"windows-registry",
"write-fonts 0.54.1",
]
+1
View File
@@ -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 }
+4
View File
@@ -24,6 +24,8 @@ mod catalog;
mod env;
mod fallback;
mod fontconfig;
mod ligature;
mod line_break;
mod outline;
mod registry;
mod scan;
@@ -37,6 +39,8 @@ 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};
pub use segments::SegmentBoundary;
+33
View File
@@ -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<f64>>) -> 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<usize> = 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());
}
}
+49
View File
@@ -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));
}
}