diff --git a/crates/engine/src/cmd/kinsokusets/tests.rs b/crates/engine/src/cmd/kinsokusets/tests.rs index 1d1c8980c0..04456872b2 100644 --- a/crates/engine/src/cmd/kinsokusets/tests.rs +++ b/crates/engine/src/cmd/kinsokusets/tests.rs @@ -332,6 +332,36 @@ fn pasted_type_brings_its_kinsoku_sets() { assert_eq!(style_set(&s), Value::Null); } +/// Type pasted back after its set was edited breaks its lines by the set as it is now: with B in +/// Cannot End Line the first line of `A B C` ends at A, and once the set holds nothing, it ends at B. +#[test] +fn pasted_type_relayouts_after_its_kinsoku_set_was_edited() { + let mut s = session(); + s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "Tight", "cannotEnd": "B"}]})).unwrap(); + let create = json!({"x": 10, "y": 10, "text": "A B C", "size": 20, "font": "JetBrains Mono", "area": {"width": 40, "height": 120}}); + let id = s.execute("text.create", &create).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [id]})).unwrap(); + s.execute("text.setFormat", &json!({"kinsoku": "Tight"})).unwrap(); + let lines = |t: &vectorcraft_doc::TextObject| t.baselines().collect::>(); + let first_width = |t: &vectorcraft_doc::TextObject| t.baselines().next().map(|(a, b)| b.x - a.x).unwrap_or_default(); + let copied = text(&s, id); + s.execute("edit.copy", &json!({})).unwrap(); + s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "Tight"}]})).unwrap(); + // The set's edit lays the type in the document out anew, and its first line takes B. + let edited = text(&s, id); + assert_eq!(lines(&edited).len(), 2); + assert!(first_width(&edited) > first_width(&copied), "{:?} against {:?}", lines(&edited), lines(&copied)); + + let v = s.execute("edit.paste", &json!({})).unwrap(); + let t = text(&s, v["ids"][0].as_u64().unwrap()); + let now = s.doc().unwrap().doc.kinsoku_sets[0].clone(); + assert_eq!(t.para_at(0).kinsoku_set.as_deref(), Some(&now)); + let mut fresh = t.clone(); + crate::cmd::typecmd::refresh_bounds(&mut fresh); + assert_eq!(lines(&t), lines(&fresh), "the pasted type is laid out with the edited set"); + assert_eq!(first_width(&t), first_width(&edited)); +} + /// A pasted set whose name, at the longest a name can be, the document's set with other /// characters already has comes in under a free name that fits: the number takes the place of the /// name's last characters, and a space left at the end of them is dropped. A second paste uses diff --git a/crates/text/src/layout/wrapping_tests.rs b/crates/text/src/layout/wrapping_tests.rs index 96ccc3e5c7..225e58a053 100644 --- a/crates/text/src/layout/wrapping_tests.rs +++ b/crates/text/src/layout/wrapping_tests.rs @@ -1312,6 +1312,58 @@ fn shaped_composition_searches_share_the_frame_budget() { assert_eq!(three.steps_left.get(), breaking::MAX_TOTAL_STEPS, "the layout's own steps"); } +/// The every-line composer places a line where its first word fits, and measures that word by the +/// paragraph's custom Kinsoku set: with A in the set's Cannot End Line, the first word of `A BC` +/// runs past the space to the end, so the line `A` skips the frame's 20 pt top and takes the +/// 50 pt band below it. Without the set the word is `A` alone and the top holds it. +#[test] +fn shaped_first_word_uses_the_custom_kinsoku_set() { + use super::shaped::{Budget, Shaped}; + use crate::composer::Measure; + use std::cell::Cell; + use std::rc::Rc; + let text = "A BC"; + let glyphs = full_width(text); + let set = custom_set("", "A", "", &[]); + assert_eq!(breaking::word_width(&glyphs, 0, &[], breaking::KinsokuRules::Custom(&set), Burasagari::None), 40.0); + assert_eq!(breaking::word_width(&glyphs, 0, &[], Kinsoku::Hard, Burasagari::None), 10.0); + // 20 pt wide down to 40 pt, and 50 pt wide below. + let frame = BezPath::from_vec(vec![ + PathEl::MoveTo((0.0, 0.0).into()), + PathEl::LineTo((20.0, 0.0).into()), + PathEl::LineTo((20.0, 40.0).into()), + PathEl::LineTo((50.0, 40.0).into()), + PathEl::LineTo((50.0, 120.0).into()), + PathEl::LineTo((0.0, 120.0).into()), + PathEl::ClosePath, + ]); + let regions = region::Region::cells(&frame, &LayoutOptions::default(), &[]); + let pen = breaking::Pen { + regions: Some(®ions), + ri: 0, + prev: None, + pending: 0.0, + fb: FirstBaseline::Ascent, + fb_min: 0.0, + queued: vec![], + above: Default::default(), + model: LeadingModel::default(), + next_top: None, + no_room_for: f64::INFINITY, + steps_left: Rc::new(Cell::new(breaking::MAX_TOTAL_STEPS)), + search: Rc::new(Budget::default()), + }; + let first_line = |para: &ParaStyle| { + let mut measure = Shaped::new(&glyphs, &[], para, &|_| 0.0, false, false, &pen); + let state = measure.first(); + measure.line(&state, 0, 1).ok().flatten().map(|(width, _)| width) + }; + let hard = ParaStyle { kinsoku: Kinsoku::Hard, ..ParaStyle::default() }; + let custom = ParaStyle { kinsoku_set: Some(Arc::new(set)), ..hard.clone() }; + assert_eq!(first_line(&hard), Some(20.0), "the frame's top holds the word A"); + assert_eq!(first_line(&custom), Some(50.0), "the word A BC needs the band below"); +} + /// The glyphs of `text`, each 10 pt wide, with Korean words kept whole unless `anywhere` (Korean /// Line Break Anywhere on). fn korean(text: &str, anywhere: bool) -> Vec {