DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Keep style export tags and classes on IDML import and export
NetterSilver7d1340a3dff6
diff --git a/crates/idml/src/export/styles.rs b/crates/idml/src/export/styles.rsindex bf156c680..8977af787 100644--- a/crates/idml/src/export/styles.rs+++ b/crates/idml/src/export/styles.rs@@ -28,7 +28,11 @@ impl<'a> Ex<'a> {}self.char_attrs(&mut el, &mut props, &cs.chars);}- cg.insert(&cs.name, with_props(el, props));+ let mut el = with_props(el, props);+ if let Some(t) = styles.export_tag(&cs.name, true) {+ el.push(self.export_tag_map(t));+ }+ cg.insert(&cs.name, el);}let mut rc = El::new("RootCharacterStyleGroup").attr("Self", self.fresh());cg.emit(&mut rc, "CharacterStyleGroup", "");@@ -65,10 +69,15 @@ impl<'a> Ex<'a> {let pa = with_list_tab(pa, || (styles.resolve_para_style(&ps.name).0, parent_props));self.para_attrs(&mut el, &mut props, &pa, parent);self.char_attrs(&mut el, &mut props, &ca);- if styles.export_tag(&ps.name, false).is_some_and(|t| t.split) {+ let tag = styles.export_tag(&ps.name, false);+ if tag.is_some_and(|t| t.split) {el.set("SplitDocument", "true");}- pg.insert(&ps.name, with_props(el, props));+ let mut el = with_props(el, props);+ if let Some(t) = tag {+ el.push(self.export_tag_map(t));+ }+ pg.insert(&ps.name, el);}let mut rp = El::new("RootParagraphStyleGroup").attr("Self", self.fresh());pg.emit(&mut rp, "ParagraphStyleGroup", "");@@ -159,6 +168,18 @@ impl<'a> Ex<'a> {document(&root)}+ /// A style's Export Tagging as its EPUB export tag map.+ fn export_tag_map(&mut self, t: &designcraft_doc::ExportTag) -> El {+ El::new("StyleExportTagMap")+ .attr("Self", self.fresh())+ .attr("ExportType", "EPUB")+ .attr("ExportTag", &t.tag)+ .attr("ExportClass", &t.class)+ .attr("ExportAttributes", "")+ .attr("SplitDocument", bool_s(t.split))+ .attr("EmitCss", "true")+ }+fn append_cell_styles(&mut self, root: &mut El, styles: &designcraft_doc::Styles) {let mut cells = El::new("RootCellStyleGroup").attr("Self", self.fresh())diff --git a/crates/idml/src/import/styles.rs b/crates/idml/src/import/styles.rsindex fd40a6d96..4519d8931 100644--- a/crates/idml/src/import/styles.rs+++ b/crates/idml/src/import/styles.rs@@ -86,6 +86,7 @@ impl<'r> Importer<'r> {let split = e.get("SplitDocument") == Some("true")|| e.elements().any(|m| m.local() == "StyleExportTagMap" && m.get("ExportType") == Some("EPUB") && m.get("SplitDocument") == Some("true"));+ self.export_tag(e, &format!("p:{name}"));if split {self.styles.export_tags.entry(format!("p:{name}")).or_default().split = true;}@@ -99,6 +100,7 @@ impl<'r> Importer<'r> {Some(slot) => *slot = s,None => self.styles.character.push(s),}+ self.export_tag(e, &format!("c:{name}"));}// Keep built-ins as well: [Basic Table] is editable, and [No table style]// supplies the defaults used by named styles in ordinary IDML files.@@ -201,6 +203,19 @@ impl<'r> Importer<'r> {}}+ /// Export Tagging (tag and class) of style `e` from its EPUB export tag map, under `key`.+ fn export_tag(&mut self, e: &El, key: &str) {+ let Some(m) = e.elements().find(|m| m.local() == "StyleExportTagMap" && m.get("ExportType") == Some("EPUB")) else { return };+ let tag = m.get("ExportTag").unwrap_or("").to_string();+ let class = m.get("ExportClass").unwrap_or("").to_string();+ if tag.is_empty() && class.is_empty() {+ return;+ }+ let t = self.styles.export_tags.entry(key.to_string()).or_default();+ t.tag = tag;+ t.class = class;+ }+fn based_on(&self, e: &El, para: bool) -> Option<String> {let b = e.prop("BasedOn")?;let b = b.trim();diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rsindex d9e00975d..050c07fa2 100644--- a/crates/idml/src/tests.rs+++ b/crates/idml/src/tests.rs@@ -4894,6 +4894,41 @@ fn split_document_round_trips() {assert!(split(&back, "Part") && split(&back, "Chapter") && !split(&back, "Body"));}+/// Export Tagging (tag and class) of paragraph and character styles comes from the style's EPUB+/// export tag map, not another export type's, and is written back.+#[test]+fn export_tags_round_trip() {+ let xml = r#"<?xml version="1.0" encoding="UTF-8"?>+<idPkg:Styles xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="16.0">+ <RootCharacterStyleGroup Self="rc">+ <CharacterStyle Self="CharacterStyle/$ID/[No character style]" Name="$ID/[No character style]"/>+ <CharacterStyle Self="CharacterStyle/Lead" Name="Lead">+ <StyleExportTagMap Self="cs1" ExportType="PDF" ExportTag="Artifact" ExportClass="" ExportAttributes=""/>+ <StyleExportTagMap Self="cs2" ExportType="EPUB" ExportTag="em" ExportClass="lead" ExportAttributes="" SplitDocument="false" EmitCss="true"/>+ </CharacterStyle>+ </RootCharacterStyleGroup>+ <RootParagraphStyleGroup Self="rp">+ <ParagraphStyle Self="ParagraphStyle/$ID/[No paragraph style]" Name="$ID/[No paragraph style]" PointSize="12"/>+ <ParagraphStyle Self="ParagraphStyle/Chapter" Name="Chapter" SplitDocument="false">+ <StyleExportTagMap Self="ps1" ExportType="PDF" ExportTag="H1" ExportClass="" ExportAttributes=""/>+ <StyleExportTagMap Self="ps2" ExportType="EPUB" ExportTag="h1" ExportClass="title" ExportAttributes="" SplitDocument="false" EmitCss="true"/>+ </ParagraphStyle>+ <ParagraphStyle Self="ParagraphStyle/Body" Name="Body"/>+ </RootParagraphStyleGroup>+</idPkg:Styles>"#;+ let d = import_idml_with(&fixture_with_styles(xml), &|_| None).unwrap();+ let tag = |d: &designcraft_doc::Document, name: &str, character: bool| {+ d.styles.export_tag(name, character).map(|t| (t.tag.clone(), t.class.clone(), t.split))+ };+ let check = |d: &designcraft_doc::Document| {+ assert_eq!(tag(d, "Chapter", false), Some(("h1".into(), "title".into(), false)));+ assert_eq!(tag(d, "Lead", true), Some(("em".into(), "lead".into(), false)));+ assert_eq!(tag(d, "Body", false), None);+ };+ check(&d);+ check(&import_idml(&export_idml(&d)).unwrap());+}+/// EPUB Export Options › Split Document: off, one style (built in or not) or the styles' export/// tags, read from the file's `EPubExportPreference` and written back.#[test]