DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge PR #780
Brandon Thomase192fc17d9d4
diff --cc crates/epub/src/lib.rsindex ebb9d3de5,06745d16c..162686eb0--- a/crates/epub/src/lib.rs+++ b/crates/epub/src/lib.rs@@@ -839,62 -827,61 +878,117 @@@ mod testsassert!(z.by_name("OEBPS/nav.xhtml").is_ok());}+ /// #766: anchored graphics are exported where they are anchored (inline in the text, an+ /// above-line object as a block of its own), and their images go into the package.+ #[test]+ fn anchored_objects_are_exported_in_place() {+ let mut d = Document::new(&NewDocument::default());+ let lid = d.default_layer();+ let (_, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 300.0, 200.0), lid, "AB\nCD", ParaFormat::default()).unwrap();+ let aid = AssetId(d.alloc());+ d.assets.insert(+ aid,+ Arc::new(Asset {+ page: 0,+ id: aid,+ name: "dot.png".into(),+ mime: "image/png".into(),+ link: None,+ data: Arc::new(vec![7]),+ pixels: Some((1, 1)),+ }),+ );+ let graphic = |d: &mut Document, alt: &str| {+ let mut item = Item::new(ItemId(d.alloc()), lid, Shape::Rectangle, shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)));+ item.alt_text = alt.into();+ item.content = Content::Graphic(Graphic {+ asset: aid,+ size: (1.0, 1.0),+ xf: Affine::IDENTITY,+ auto_fit: Default::default(),+ fit_align: 4,+ crop: [0.0; 4],+ });+ item+ };+ let inline = designcraft_doc::AnchoredObject::new(graphic(&mut d, "inline"), designcraft_doc::AnchorPosition::Inline { y_offset: 0.0 });+ let above = designcraft_doc::AnchoredObject::new(+ graphic(&mut d, "above"),+ designcraft_doc::AnchorPosition::AboveLine { align: designcraft_doc::anchored::AnchorAlign::Center, space_before: 0.0, space_after: 0.0 },+ );+ let st = d.story_mut(sid).unwrap();+ st.insert_object(1, inline);+ // Byte offsets: "A" (1), the mark (3), "B\nC" (3): the second object goes between C and D.+ st.insert_object(7, above);+ let bytes = export_epub(&d, &EpubOptions::default()).unwrap();+ let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap();+ let mut html = String::new();+ z.by_name("OEBPS/content.xhtml").unwrap().read_to_string(&mut html).unwrap();+ let img = |alt: &str| format!("<img src=\"images/{}.png\" alt=\"{alt}\"/>", aid.0);+ assert!(html.contains(&format!("A{}B</p>", img("inline"))), "{html}");+ assert!(html.contains(&format!("C<span class=\"anchored\" style=\"display:block;text-align:center\">{}</span>D", img("above"))), "{html}");+ assert!(z.by_name(&format!("OEBPS/images/{}.png", aid.0)).is_ok(), "the image is in the package");+ let page = export_html(&d, &HtmlOptions::default());+ assert!(page.contains("alt=\"inline\"") && page.contains("alt=\"above\""), "{page}");+ // story_html on its own still leaves objects out.+ assert!(!story_html(&d, sid).contains("<img"));+ }++ fn st_typed(d: &mut Document, sid: StoryId) {+ if let Some(st) = d.story_mut(sid) {+ st.paras[8].style = "Typed".into();+ }+ }++ /// #767: consecutive `li` paragraphs share one list (`ol` for a numbered style), and+ /// consecutive `pre` paragraphs of one style one block with their lines on separate lines.+ #[test]+ fn lists_and_preformatted_blocks_are_grouped() {+ let mut d = Document::new(&NewDocument::default());+ let lid = d.default_layer();+ for (name, tag, list) in [+ ("Bullet", "li", Some(designcraft_doc::ListType::Bullets)),+ ("Number", "li", Some(designcraft_doc::ListType::Numbers)),+ ("Typed", "li", None),+ ("Code", "pre", None),+ ] {+ let para = designcraft_doc::ParaAttrs { list_type: list, ..Default::default() };+ d.styles_mut().paragraph.push(designcraft_doc::ParagraphStyle {+ name: name.into(),+ based_on: None,+ next_style: None,+ para,+ chars: CharAttrs::default(),+ shortcut: String::new(),+ });+ d.styles_mut().export_tags.insert(format!("p:{name}"), designcraft_doc::ExportTag { tag: tag.into(), class: String::new() });+ }+ let text = "Intro\nfirst\nsecond\none\ntwo\nx = 1\ny = 2\nEnd\n• typed";+ let (_, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 300.0, 300.0), lid, text, ParaFormat::default()).unwrap();+ let st = d.story_mut(sid).unwrap();+ for (i, style) in ["Bullet", "Bullet", "Number", "Number", "Code", "Code"].into_iter().enumerate() {+ st.paras[i + 1].style = style.into();+ }+ let html = story_html(&d, sid);+ let li = |c: &str, t: &str| format!("<li class=\"{c}\">{t}</li>");+ assert!(+ html.contains(&format!(+ "<ul>\n{}\n{}\n</ul>\n<ol>\n{}\n{}\n</ol>\n",+ li("bullet", "first"),+ li("bullet", "second"),+ li("number", "one"),+ li("number", "two")+ )),+ "{html}"+ );+ assert!(html.contains("<pre class=\"code\">x = 1\ny = 2</pre>\n<p class=\"basic-paragraph\">End</p>"), "{html}");+ assert_eq!(html.matches("<pre").count(), 1, "{html}");+ // A style without a list type of its own (typed bullets) gets no second marker.+ st_typed(&mut d, sid);+ let html = story_html(&d, sid);+ assert!(html.contains("<ul style=\"list-style:none\">\n<li class=\"typed\">• typed</li>\n</ul>\n"), "{html}");+ }+#[test]fn styles_whose_names_slug_alike_keep_their_own_classes() {let mut d = Document::new(&NewDocument::default());