DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge PR #887
Brandon Thomas067b339991d5
diff --cc crates/engine/src/cmd/text.rsindex 1c6b6ba3c,3ff557daa..a2a034b9b--- a/crates/engine/src/cmd/text.rs+++ b/crates/engine/src/cmd/text.rs@@@ -2741,170 -2595,66 +2777,234 @@@ mod sentence_selection_tests}}+#[cfg(test)]+mod open_type_features_tests {+ use serde_json::{Value, json};++ use crate::Session;++ fn feature<'a>(v: &'a Value, tag: &str) -> &'a Value {+ v["features"].as_array().unwrap().iter().find(|f| f["tag"] == tag).unwrap_or(&Value::Null)+ }++ #[test]+ fn the_selected_fonts_features_are_listed_with_their_state() {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let r = s.execute("frame.create", &json!({"rect": [72, 72, 400, 200], "content": "text", "text": "Hello fi"})).unwrap();+ s.execute("text.select", &json!({"story": r["story"], "anchor": 0, "focus": 8})).unwrap();+ let v = s.execute("type.openTypeFeatures", &json!({})).unwrap();+ assert_eq!(v["fonts"][0]["family"], "Source Serif 4", "{v}");+ assert_eq!(feature(&v, "liga")["applied"], "all", "liga is on by default: {v}");+ assert_eq!(feature(&v, "kern")["available"], "all", "{v}");+ }++ #[test]+ fn a_feature_on_part_of_the_selection_is_some_and_on_all_of_it_is_all() {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let r = s.execute("frame.create", &json!({"rect": [72, 72, 400, 200], "content": "text", "text": "Hello world"})).unwrap();+ let story = r["story"].clone();+ s.execute("text.select", &json!({"story": story, "anchor": 0, "focus": 5})).unwrap();+ s.execute("type.feature", &json!({"features": {"lnum": true}})).unwrap();+ s.execute("text.select", &json!({"story": story, "anchor": 0, "focus": 11})).unwrap();+ let v = s.execute("type.openTypeFeatures", &json!({})).unwrap();+ assert_eq!(feature(&v, "lnum")["applied"], "some", "{v}");+ s.execute("text.select", &json!({"story": story, "anchor": 0, "focus": 5})).unwrap();+ let v = s.execute("type.openTypeFeatures", &json!({})).unwrap();+ assert_eq!(feature(&v, "lnum")["applied"], "all", "{v}");+ s.execute("type.feature", &json!({"features": {"lnum": false}})).unwrap();+ s.execute("text.select", &json!({"story": story, "anchor": 0, "focus": 11})).unwrap();+ let v = s.execute("type.openTypeFeatures", &json!({})).unwrap();+ assert_eq!(feature(&v, "lnum")["applied"], "none", "{v}");+ }+}++#[cfg(test)]+mod gradient_text_tests {+ use serde_json::json;++ /// Mean red and blue of the ink (pixels whose green is low: red → blue has none) in columns+ /// `x0..x1`.+ fn ink(img: &designcraft_render::Rendered, x0: u32, x1: u32) -> (f64, f64) {+ let (mut r, mut b, mut n) = (0.0, 0.0, 0.0);+ for x in x0..x1 {+ for y in 0..img.height {+ let p = img.pixel(x, y);+ if p[1] < 100 {+ (r, b, n) = (r + p[0] as f64, b + p[2] as f64, n + 1.0);+ }+ }+ }+ assert!(n > 50.0, "no ink in {x0}..{x1}");+ (r / n, b / n)+ }++ /// Red ink at the frame's left, blue at its right.+ fn check(img: &designcraft_render::Rendered, what: &str) {+ let (lr, lb) = ink(img, 0, 60);+ let (rr, rb) = ink(img, 200, 300);+ assert!(lr > lb + 80.0, "{what}: the left ink is red ({lr:.0}, {lb:.0})");+ assert!(rb > rr + 80.0, "{what}: the right ink is blue ({rr:.0}, {rb:.0})");+ }++ #[test]+ fn a_gradient_on_text_spans_its_frame_on_screen_and_in_pdf() {+ let mut s = crate::Session::new();+ s.execute("file.new", &json!({"width": 300, "height": 120, "facingPages": false, "margins": 0})).unwrap();+ // A gradient swatch (an off-page object's unnamed one), red → blue.+ let shape = s.execute("frame.create", &json!({"rect": [-60, 0, -50, 10], "content": "unassigned"})).unwrap()["id"].clone();+ s.execute("object.gradient", &json!({"ids": [shape], "stops": [{"location": 0, "color": "#ff0000"}, {"location": 100, "color": "#0000ff"}]}))+ .unwrap();+ let id = designcraft_doc::ItemId(shape.as_u64().unwrap());+ let swatch = s.doc().unwrap().doc.item(id).unwrap().fill.swatch.clone();+ let r = s.execute("frame.create", &json!({"rect": [0, 0, 300, 120], "content": "text", "text": "IIIIIIIIIIIIIIIIIIIIIIIIIIIIII"})).unwrap();+ s.execute("text.select", &json!({"story": r["story"], "anchor": 0, "focus": 30})).unwrap();+ s.execute("type.char", &json!({"size": 60, "tracking": 0, "fill": swatch})).unwrap();+ let d = s.doc().unwrap().doc.clone();+ let mut rr = designcraft_render::Renderer::new();+ check(&rr.render_page(&d, &s.cache, 0, 1.0, false, &Default::default()).unwrap(), "screen");+ // The PDF, placed back and drawn: the same gradient.+ let pdf = designcraft_pdf::export_pdf(&d, &s.cache, &Default::default()).unwrap();+ let mut p = crate::Session::new();+ p.execute("file.new", &json!({"width": 300, "height": 120, "facingPages": false})).unwrap();+ p.execute("file.place", &json!({"base64": super::super::file::base64_encode(&pdf), "name": "text.pdf", "x": 0, "y": 0, "width": 300}))+ .unwrap();+ let d = p.doc().unwrap().doc.clone();+ check(&rr.render_page(&d, &p.cache, 0, 1.0, false, &Default::default()).unwrap(), "pdf");+ }++ /// The columns the red → blue ink spans (black table edges left out).+ fn ink_span(img: &designcraft_render::Rendered) -> (u32, u32) {+ let cols: Vec<u32> = (0..img.width)+ .filter(|&x| {+ (0..img.height).any(|y| {+ let p = img.pixel(x, y);+ p[1] < 100 && p[0].max(p[2]) > 100+ })+ })+ .collect();+ (*cols.first().unwrap(), *cols.last().unwrap())+ }++ /// Mean red and blue of the red → blue ink in columns `x0..x1`.+ fn coloured_ink(img: &designcraft_render::Rendered, x0: u32, x1: u32) -> (f64, f64) {+ let (mut r, mut b, mut n) = (0.0, 0.0, 0.0);+ for x in x0..x1 {+ for y in 0..img.height {+ let p = img.pixel(x, y);+ if p[1] < 100 && p[0].max(p[2]) > 100 {+ (r, b, n) = (r + p[0] as f64, b + p[2] as f64, n + 1.0);+ }+ }+ }+ assert!(n > 20.0, "no ink in {x0}..{x1}");+ (r / n, b / n)+ }++ #[test]+ fn a_gradient_on_table_cell_text_spans_the_cells_text_in_pdf_as_on_screen() {+ let mut s = crate::Session::new();+ s.execute("file.new", &json!({"width": 300, "height": 120, "facingPages": false, "margins": 0})).unwrap();+ // A gradient swatch (an off-page object's unnamed one), red → blue.+ let shape = s.execute("frame.create", &json!({"rect": [-60, 0, -50, 10], "content": "unassigned"})).unwrap()["id"].clone();+ s.execute("object.gradient", &json!({"ids": [shape], "stops": [{"location": 0, "color": "#ff0000"}, {"location": 100, "color": "#0000ff"}]}))+ .unwrap();+ let g = s.doc().unwrap().doc.item(designcraft_doc::ItemId(shape.as_u64().unwrap())).unwrap().fill.swatch.clone();+ let r = s.execute("frame.create", &json!({"rect": [0, 0, 300, 120], "content": "text", "text": ""})).unwrap();+ s.execute("text.select", &json!({"story": r["story"], "anchor": 0, "focus": 0})).unwrap();+ s.execute("table.insert", &json!({"rows": 1, "cols": 3})).unwrap();+ // A third of the frame's width: the cell's text, red → blue.+ let mut d: designcraft_doc::Document = (*s.doc().unwrap().doc).clone();+ let sid = d.stories.values().find(|st| !st.tables.is_empty()).unwrap().id;+ let t = std::sync::Arc::make_mut(d.story_mut(sid).unwrap().tables.values_mut().next().unwrap());+ let text = &mut t.cell_mut(0, 0).unwrap().text;+ text.insert(0, "HHHH");+ text.format_chars(0..4, |f| {+ f.over.fill = Some(g.clone());+ f.over.size = Some(24.0);+ });+ let mut rr = designcraft_render::Renderer::new();+ let cache = designcraft_compose::Cache::new();+ let screen = rr.render_page(&d, &cache, 0, 1.0, false, &Default::default()).unwrap();+ let pdf = designcraft_pdf::export_pdf(&d, &cache, &Default::default()).unwrap();+ let mut p = crate::Session::new();+ p.execute("file.new", &json!({"width": 300, "height": 120, "facingPages": false})).unwrap();+ p.execute("file.place", &json!({"base64": super::super::file::base64_encode(&pdf), "name": "cell.pdf", "x": 0, "y": 0, "width": 300}))+ .unwrap();+ let placed = rr.render_page(&p.doc().unwrap().doc.clone(), &p.cache, 0, 1.0, false, &Default::default()).unwrap();+ let (x0, x1) = ink_span(&screen);+ assert!(x1 < 120, "the text stays in the first column: {x0}..{x1}");+ let q = (x1 - x0) / 4;+ for (what, img) in [("screen", &screen), ("pdf", &placed)] {+ let (lr, lb) = coloured_ink(img, x0, x0 + q);+ let (rr, rb) = coloured_ink(img, x1 + 1 - q, x1 + 1);+ assert!(lr > lb + 50.0, "{what}: the left ink is red ({lr:.0}, {lb:.0})");+ assert!(rb > rr + 50.0, "{what}: the right ink is blue ({rr:.0}, {rb:.0})");+ }+ }+}+++ #[cfg(test)]+ mod compose_tests {+ use serde_json::json;++ use crate::Session;++ /// A story "Ta" with the caret after "T" and 30 pt chosen for the next typing.+ fn session() -> (Session, u64) {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let r = s.execute("frame.create", &json!({"rect": [72, 72, 500, 200], "content": "text", "text": "Ta"})).unwrap();+ let sid = r["story"].as_u64().unwrap();+ s.set_tool("type");+ s.execute("text.select", &json!({"story": sid, "anchor": 1})).unwrap();+ s.execute("type.char", &json!({"attrs": {"size": 30}})).unwrap();+ (s, sid)+ }++ fn text(s: &Session, sid: u64) -> String {+ s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().text.clone()+ }++ /// ⌥E shows the accent in the story, in the format it will be typed in, then I types "í":+ /// one undo step, and the accent was never part of the document.+ #[test]+ fn composed_text_shows_in_the_typing_format_until_it_is_typed() {+ let (mut s, sid) = session();+ let r = s.execute("text.compose", &json!({"text": "´"})).unwrap();+ assert_eq!(r["range"], json!([1, 3]));+ assert_eq!(text(&s, sid), "T´a");+ let size_at = |s: &Session, b| s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().char_format_at(b).over.size;+ assert_eq!(size_at(&s, 2), Some(30.0), "the accent is shown in the format chosen at the caret");+ assert_eq!(s.composition().map(|c| c.2), Some(1..3));+ s.execute("text.compose", &json!({"text": "¨"})).unwrap();+ assert_eq!(text(&s, sid), "T¨a", "a new composition replaces the last one");+ s.execute("text.compose", &json!({"text": ""})).unwrap();+ assert_eq!(text(&s, sid), "Ta");+ assert!(s.composition().is_none());+ s.execute("text.insert", &json!({"text": "í"})).unwrap();+ assert_eq!(text(&s, sid), "Tía");+ assert_eq!(size_at(&s, 2), Some(30.0), "the typing format survived the composition");+ s.execute("edit.undo", &json!({})).unwrap();+ assert_eq!(text(&s, sid), "Ta");+ }++ /// Anything else that edits, a tool change or a drag drops the uncommitted accent.+ #[test]+ fn other_edits_drop_the_composed_text() {+ for act in 0..3 {+ let (mut s, sid) = session();+ s.execute("text.compose", &json!({"text": "´"})).unwrap();+ match act {+ 0 => {+ s.execute("text.move", &json!({"dir": "right"})).unwrap();+ }+ 1 => s.set_tool("selection"),+ _ => s.begin_interaction("Move").unwrap(),+ }+ assert_eq!(text(&s, sid), "Ta", "case {act}");+ assert!(s.composition().is_none());+ }+ }+ }diff --cc crates/engine/src/tooling.rsindex 58d8adb58,98868b7d6..820c270f4--- a/crates/engine/src/tooling.rs+++ b/crates/engine/src/tooling.rs@@@ -60,11 -59,8 +61,12 @@@ impl Sessionif self.tool.id() == id {return;}+ self.end_composition();self.tool = designcraft_tools::create(id);+ // Any other tool unloads the place cursor.+ if id != "placeGun" {+ self.loaded = None;+ }// Leaving the Type tool keeps the text frame selected.if id != "type"&& let Some(st) = self.active_mut()