mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-16 07:12:43 +00:00
Traverse IDML stories through shared model tree
This commit is contained in:
3 files changed
+264
-249
No files matched your search
Generated
+26
-26
@@ -1242,10 +1242,10 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-idml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=abad7d575#abad7d575355261383c733ab65ccba3fddeca72d"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=121bd72ba#121bd72ba6a12c5b53da8acd2b3d95bd3c9c664d"
|
||||
dependencies = [
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=abad7d575)",
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=abad7d575)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=121bd72ba)",
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=121bd72ba)",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
@@ -1644,6 +1644,15 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=121bd72ba#121bd72ba6a12c5b53da8acd2b3d95bd3c9c664d"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
@@ -1662,15 +1671,6 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=abad7d575#abad7d575355261383c733ab65ccba3fddeca72d"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
@@ -1717,6 +1717,16 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=121bd72ba#121bd72ba6a12c5b53da8acd2b3d95bd3c9c664d"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
"zip 9.1.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
@@ -1737,16 +1747,6 @@ dependencies = [
|
||||
"zip 9.1.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=abad7d575#abad7d575355261383c733ab65ccba3fddeca72d"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
"zip 9.1.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
@@ -2021,7 +2021,7 @@ dependencies = [
|
||||
"craft-prepress",
|
||||
"craft-print",
|
||||
"craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=abad7d575)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=121bd72ba)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f23b0f33f0d00b157476533a43cbbcfd901aa1f1)",
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -2103,9 +2103,9 @@ name = "designcraft-idml"
|
||||
version = "0.20.1"
|
||||
dependencies = [
|
||||
"craft-file-formats",
|
||||
"craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=abad7d575)",
|
||||
"craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=121bd72ba)",
|
||||
"craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=abad7d575)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=121bd72ba)",
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
"designcraft-doc",
|
||||
@@ -2222,7 +2222,7 @@ version = "0.20.1"
|
||||
dependencies = [
|
||||
"craft-docx",
|
||||
"craft-file-formats",
|
||||
"craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=abad7d575)",
|
||||
"craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=121bd72ba)",
|
||||
"craft-rtf",
|
||||
"craft-wordmodel",
|
||||
"craft-xlsx",
|
||||
|
||||
+2
-2
@@ -16,10 +16,10 @@ craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "4052591363
|
||||
craft-designcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "19692b6190465671e41967c75d0cd4c439d0c84c", version = "=0.1.0" }
|
||||
craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" }
|
||||
craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" }
|
||||
craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "abad7d575", version = "=0.1.0" }
|
||||
craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "121bd72ba", version = "=0.1.0" }
|
||||
craft-indd = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" }
|
||||
craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "cc23ffd4d26f3074808e2187d332564817624f8f", version = "=0.1.0", features = ["tree-render", "tree-text"] }
|
||||
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "abad7d575", version = "=0.1.0" }
|
||||
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "121bd72ba", version = "=0.1.0" }
|
||||
craft-xml-dtd = { package = "craft-xml", git = "https://github.com/storytold/craft-libs", rev = "f23b0f33f0d00b157476533a43cbbcfd901aa1f1", version = "=0.1.0" }
|
||||
craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" }
|
||||
craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" }
|
||||
|
||||
+236
-221
@@ -2,8 +2,6 @@ use super::text_attrs::{ace_char, clear_story_drop_cap_styles, inherits_drop_cap
|
||||
use super::{
|
||||
Arc, BTreeMap, CharAttrs, CharFormat, CharRun, El, HashMap, HashSet, Importer, Item, ItemId, ParaFormat, Story, StoryId, Table, names, st,
|
||||
};
|
||||
use crate::xml::Node;
|
||||
|
||||
impl<'r> Importer<'r> {
|
||||
// ---------- stories ----------
|
||||
|
||||
@@ -12,6 +10,7 @@ impl<'r> Importer<'r> {
|
||||
}
|
||||
|
||||
pub(super) fn story_with_para(&mut self, id: StoryId, e: &El, default_para: ParaFormat) -> Story {
|
||||
let source = craft_idml::model::Story::from_el(&crate::xml::to_craft(e));
|
||||
let mut b = StoryBuilder {
|
||||
text: String::new(),
|
||||
paras: vec![default_para.clone()],
|
||||
@@ -27,7 +26,7 @@ impl<'r> Importer<'r> {
|
||||
index_refs: Vec::new(),
|
||||
objects: Vec::new(),
|
||||
};
|
||||
self.walk_story(e, &mut b, &default_para, &CharAttrs::default(), &CharFormat::default(), None);
|
||||
self.walk_story_nodes(&source.children, &mut b, &default_para, &CharAttrs::default(), &CharFormat::default(), None);
|
||||
let StoryBuilder { text, mut paras, runs, last, tables: tbls, notes, anchors, xrefs, index_refs, objects, link_srcs, .. } = b;
|
||||
for (me, start, end) in link_srcs {
|
||||
// A cell's or a footnote's story is built under the `StoryId(0)` placeholder, so a
|
||||
@@ -75,7 +74,6 @@ impl<'r> Importer<'r> {
|
||||
let saved = !x.text.is_empty();
|
||||
xref_list.push(Arc::new(designcraft_doc::CrossRef { target: 0, format, text: saved.then_some(x.text), page: x.page.filter(|_| saved) }));
|
||||
}
|
||||
let source = craft_idml::model::Story::from_el(&crate::xml::to_craft(e));
|
||||
let optical_margin = designcraft_doc::OpticalMargin {
|
||||
on: source.optical_margin_enabled(),
|
||||
size: source
|
||||
@@ -126,230 +124,247 @@ impl<'r> Importer<'r> {
|
||||
CharFormat { style, over }
|
||||
}
|
||||
|
||||
fn walk_story(&mut self, e: &El, b: &mut StoryBuilder, pf: &ParaFormat, pchars: &CharAttrs, cf: &CharFormat, brk: Option<&str>) {
|
||||
for n in &e.children {
|
||||
match n {
|
||||
Node::El(c) => match craft_idml::model::TextNode::from_el(&crate::xml::to_craft(c)) {
|
||||
craft_idml::model::TextNode::Paragraph(range) => {
|
||||
let style = range.paragraph_style().map(|r| self.para_style_ref(r)).unwrap_or_else(|| pf.style.clone());
|
||||
let para = self.para_attrs(c);
|
||||
let mut chars = self.char_attrs(c);
|
||||
// A paragraph range inside a tracked change is part of that change.
|
||||
chars.change = chars.change.or(cf.over.change);
|
||||
let npf = ParaFormat { style, para, chars: CharAttrs::default(), table: None };
|
||||
if b.fresh
|
||||
&& let Some(last) = b.paras.last_mut()
|
||||
{
|
||||
*last = npf.clone();
|
||||
}
|
||||
// Text and markers outside any character range (a destination or a
|
||||
// cross-reference source at paragraph level) take the paragraph's format.
|
||||
let pcf = CharFormat { style: st::NO_CHAR_STYLE.into(), over: chars.clone() };
|
||||
self.walk_story(c, b, &npf, &chars, &pcf, None);
|
||||
fn walk_story_nodes(
|
||||
&mut self,
|
||||
nodes: &[craft_idml::model::TextNode],
|
||||
b: &mut StoryBuilder,
|
||||
pf: &ParaFormat,
|
||||
pchars: &CharAttrs,
|
||||
cf: &CharFormat,
|
||||
brk: Option<&str>,
|
||||
) {
|
||||
for node in nodes {
|
||||
if let craft_idml::model::TextNode::ProcessingInstruction { target, data } = node {
|
||||
if target == "ACE"
|
||||
&& let Some(ch) = ace_char(data)
|
||||
{
|
||||
b.push(&ch.to_string(), cf);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let c = crate::xml::from_craft(node.to_el());
|
||||
match node {
|
||||
craft_idml::model::TextNode::Paragraph(range) => {
|
||||
let style = range.paragraph_style().map(|r| self.para_style_ref(r)).unwrap_or_else(|| pf.style.clone());
|
||||
let para = self.para_attrs(&c);
|
||||
let mut chars = self.char_attrs(&c);
|
||||
// A paragraph range inside a tracked change is part of that change.
|
||||
chars.change = chars.change.or(cf.over.change);
|
||||
let npf = ParaFormat { style, para, chars: CharAttrs::default(), table: None };
|
||||
if b.fresh
|
||||
&& let Some(last) = b.paras.last_mut()
|
||||
{
|
||||
*last = npf.clone();
|
||||
}
|
||||
craft_idml::model::TextNode::Character(range) => {
|
||||
let ncf = self.range_format(c, pchars, cf);
|
||||
let bt = range.paragraph_break_type();
|
||||
b.last = ncf.clone();
|
||||
self.walk_story(c, b, pf, pchars, &ncf, bt);
|
||||
}
|
||||
craft_idml::model::TextNode::Content(parts) => {
|
||||
for part in parts {
|
||||
match part {
|
||||
craft_idml::model::ContentPart::Text(text) => {
|
||||
let text: String = text
|
||||
.chars()
|
||||
.filter(|c| *c != '\u{FEFF}')
|
||||
.map(|c| if c == '\n' || c == '\r' || c == '\u{2029}' { st::FORCED_LINE_BREAK } else { c })
|
||||
.collect();
|
||||
b.push(&text, cf);
|
||||
}
|
||||
craft_idml::model::ContentPart::Ace(ace) => {
|
||||
if let Some(ch) = ace_char(&ace.code().to_string()) {
|
||||
b.push(&ch.to_string(), cf);
|
||||
}
|
||||
}
|
||||
craft_idml::model::ContentPart::Pi { .. } => {}
|
||||
// Text and markers outside any character range (a destination or a
|
||||
// cross-reference source at paragraph level) take the paragraph's format.
|
||||
let pcf = CharFormat { style: st::NO_CHAR_STYLE.into(), over: chars.clone() };
|
||||
self.walk_story_nodes(&range.children, b, &npf, &chars, &pcf, None);
|
||||
}
|
||||
craft_idml::model::TextNode::Character(range) => {
|
||||
let ncf = self.range_format(&c, pchars, cf);
|
||||
let bt = range.paragraph_break_type();
|
||||
b.last = ncf.clone();
|
||||
self.walk_story_nodes(&range.children, b, pf, pchars, &ncf, bt);
|
||||
}
|
||||
craft_idml::model::TextNode::Content(parts) => {
|
||||
for part in parts {
|
||||
match part {
|
||||
craft_idml::model::ContentPart::Text(text) => {
|
||||
let text: String = text
|
||||
.chars()
|
||||
.filter(|c| *c != '\u{FEFF}')
|
||||
.map(|c| if c == '\n' || c == '\r' || c == '\u{2029}' { st::FORCED_LINE_BREAK } else { c })
|
||||
.collect();
|
||||
b.push(&text, cf);
|
||||
}
|
||||
}
|
||||
}
|
||||
craft_idml::model::TextNode::Break => {
|
||||
// A column, frame or page break ends its paragraph like a return does.
|
||||
if let Some(t) = brk {
|
||||
let ch = match t {
|
||||
"NextColumn" => st::COLUMN_BREAK,
|
||||
"NextFrame" => st::FRAME_BREAK,
|
||||
"NextOddPage" => st::ODD_PAGE_BREAK,
|
||||
"NextEvenPage" => st::EVEN_PAGE_BREAK,
|
||||
_ => st::PAGE_BREAK,
|
||||
};
|
||||
b.push(&ch.to_string(), cf);
|
||||
}
|
||||
b.push("\n", cf);
|
||||
b.paras.push(pf.clone());
|
||||
b.fresh = true;
|
||||
}
|
||||
craft_idml::model::TextNode::Table(_) => {
|
||||
// `None` once the file-wide cell budget is spent: the table is not read
|
||||
// at all rather than kept as a 1 x 1 (noted in review).
|
||||
if let Some(t) = self.table(c) {
|
||||
b.push_table(t, pf, cf);
|
||||
}
|
||||
}
|
||||
craft_idml::model::TextNode::Container(container) => {
|
||||
let container_el = crate::xml::from_craft(container.to_el());
|
||||
match container.kind() {
|
||||
craft_idml::model::ContainerKind::HyperlinkTextSource => {
|
||||
let start = b.text.len();
|
||||
self.walk_story(&container_el, b, pf, pchars, cf, brk);
|
||||
if b.text.len() > start {
|
||||
b.link_srcs.push((container.self_id().unwrap_or("").to_string(), start, b.text.len()));
|
||||
craft_idml::model::ContentPart::Ace(ace) => {
|
||||
if let Some(ch) = ace_char(&ace.code().to_string()) {
|
||||
b.push(&ch.to_string(), cf);
|
||||
}
|
||||
}
|
||||
craft_idml::model::ContainerKind::Footnote => {
|
||||
let mut note = self.story(StoryId(0), &container_el);
|
||||
let sep = self.footnote_options.separator.clone();
|
||||
if !sep.is_empty() && note.text.starts_with(&sep) {
|
||||
note.delete(0..sep.len());
|
||||
}
|
||||
b.notes.push(note);
|
||||
let mut rcf = cf.clone();
|
||||
rcf.over.position = None;
|
||||
b.push(&designcraft_doc::FOOTNOTE_REF.to_string(), &rcf);
|
||||
}
|
||||
craft_idml::model::ContainerKind::Change => {
|
||||
let mut ncf = cf.clone();
|
||||
match container.change_kind() {
|
||||
Some(craft_idml::model::TrackedChangeKind::Deleted) => {
|
||||
ncf.over.change = Some(designcraft_doc::ChangeMark::Deleted)
|
||||
}
|
||||
Some(craft_idml::model::TrackedChangeKind::Inserted) => {
|
||||
ncf.over.change = Some(designcraft_doc::ChangeMark::Inserted)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.walk_story(&container_el, b, pf, pchars, &ncf, brk);
|
||||
}
|
||||
craft_idml::model::ContainerKind::XMLElement => self.walk_story(&container_el, b, pf, pchars, cf, brk),
|
||||
craft_idml::model::ContainerKind::CrossReferenceSource => {
|
||||
let page_variables: Vec<_> = self.xref_page_vars.iter().cloned().collect();
|
||||
let saved = craft_idml::model::SavedCrossReference::from_el(&crate::xml::to_craft(&container_el), &page_variables);
|
||||
let mut text = String::new();
|
||||
let mut page = None;
|
||||
for part in saved.parts {
|
||||
match part {
|
||||
craft_idml::model::SavedCrossReferencePart::Text(value) => text.push_str(&value),
|
||||
craft_idml::model::SavedCrossReferencePart::PageNumber { result_text } => {
|
||||
text.push_str(designcraft_doc::xref::PAGE_NUM_BLOCK);
|
||||
if page.is_none() {
|
||||
page = result_text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
b.xrefs.push(XrefSource {
|
||||
me: saved.self_id.unwrap_or_default(),
|
||||
format: saved.applied_format.unwrap_or_default(),
|
||||
text,
|
||||
page,
|
||||
});
|
||||
let f = container_el.find("CharacterStyleRange").map_or_else(|| cf.clone(), |r| self.range_format(r, pchars, cf));
|
||||
b.push(&designcraft_doc::XREF_MARK.to_string(), &f);
|
||||
}
|
||||
craft_idml::model::ContainerKind::Note | craft_idml::model::ContainerKind::EndnoteRange => {}
|
||||
craft_idml::model::ContentPart::Pi { .. } => {}
|
||||
}
|
||||
}
|
||||
craft_idml::model::TextNode::Anchor(anchor) => {
|
||||
b.anchors.push((anchor.self_id().unwrap_or("").to_string(), anchor.label().unwrap_or("Anchor").to_string()));
|
||||
b.push(&designcraft_doc::ANCHOR_MARK.to_string(), cf);
|
||||
}
|
||||
craft_idml::model::TextNode::PageReference(reference) => {
|
||||
use designcraft_doc::index::IndexRange as R;
|
||||
let topic = reference.referenced_topic().and_then(|r| self.index_topics.get(r)).cloned().unwrap_or_default();
|
||||
let range = match reference.reference_type() {
|
||||
Some("ToEndOfStory" | "ToEndOfDocument" | "ToEndOfSection") => R::ToEndOfStory,
|
||||
Some("ForNextNParagraphs") => R::NextParagraphs(reference.limit().unwrap_or(1.0).max(1.0) as u32),
|
||||
Some("SuppressPageNumbers") => R::SuppressPageRange,
|
||||
_ => R::CurrentPage,
|
||||
};
|
||||
b.index_refs.push(designcraft_doc::IndexRef { topics: topic.path, sort: topic.sort, range });
|
||||
b.push(&designcraft_doc::INDEX_MARK.to_string(), cf);
|
||||
}
|
||||
craft_idml::model::TextNode::TextVariable(values) => {
|
||||
let text = self.text_variable_instance(&values);
|
||||
b.push(&text, cf);
|
||||
}
|
||||
_ => match c.local() {
|
||||
// Anchored page items, including groups containing text frames.
|
||||
"Rectangle" | "Oval" | "Polygon" | "GraphicLine" | "Group" | "TextFrame" => {
|
||||
if let Some(item) = self.item(c, designcraft_geom::Affine::IDENTITY) {
|
||||
use designcraft_doc::anchored::AnchorAlign as A;
|
||||
// Each setting the item leaves unset comes from its object style chain,
|
||||
// then from the IDML defaults.
|
||||
let levels: Vec<&El> = std::iter::once(c).chain(self.object_style_chain(c)).collect();
|
||||
let s = |k: &str| {
|
||||
let set = levels.iter().filter_map(|l| l.find("AnchoredObjectSetting")).find_map(|x| x.get(k));
|
||||
set.unwrap_or(match k {
|
||||
"AnchoredPosition" => "InlinePosition",
|
||||
"AnchorPoint" => "BottomRightAnchor",
|
||||
"HorizontalAlignment" => "LeftAlign",
|
||||
"HorizontalReferencePoint" => "TextFrame",
|
||||
"VerticalAlignment" => "BottomAlign",
|
||||
"VerticalReferencePoint" => "LineBaseline",
|
||||
"PinPosition" => "true",
|
||||
_ => "",
|
||||
})
|
||||
};
|
||||
let get = |k: &str| s(k).trim().parse::<f64>().ok().filter(|v| v.is_finite()).unwrap_or(0.0);
|
||||
let position = match s("AnchoredPosition") {
|
||||
"AboveLine" => designcraft_doc::AnchorPosition::AboveLine {
|
||||
align: match s("HorizontalAlignment") {
|
||||
"CenterAlign" => A::Center,
|
||||
"RightAlign" => A::Right,
|
||||
_ => A::Left,
|
||||
},
|
||||
space_before: get("AnchorSpaceAbove"),
|
||||
space_after: get("AnchorYoffset"),
|
||||
},
|
||||
"Anchored" => {
|
||||
let col = match s("HorizontalAlignment") {
|
||||
"CenterAlign" => 1,
|
||||
"RightAlign" => 2,
|
||||
_ => 0,
|
||||
};
|
||||
let row = match s("VerticalAlignment") {
|
||||
"CenterAlign" => 1,
|
||||
"BottomAlign" => 2,
|
||||
_ => 0,
|
||||
};
|
||||
designcraft_doc::AnchorPosition::Custom {
|
||||
x_relative: names::anchor_rel_in(s("HorizontalReferencePoint")),
|
||||
y_relative: names::anchor_rel_in(s("VerticalReferencePoint")),
|
||||
// IDML measures a left-aligned object's X offset leftwards.
|
||||
x_offset: if col == 0 { 0.0 - get("AnchorXoffset") } else { get("AnchorXoffset") },
|
||||
y_offset: get("AnchorYoffset"),
|
||||
object_point: names::anchor_in(s("AnchorPoint")),
|
||||
ref_point: row * 3 + col,
|
||||
keep_within_column: s("PinPosition") == "true",
|
||||
spine_relative: s("SpineRelative") == "true",
|
||||
}
|
||||
}
|
||||
_ => designcraft_doc::AnchorPosition::Inline { y_offset: get("AnchorYoffset") },
|
||||
};
|
||||
b.objects.push(designcraft_doc::AnchoredObject::new(item, position));
|
||||
b.push(&designcraft_doc::OBJECT_MARK.to_string(), cf);
|
||||
}
|
||||
}
|
||||
"Properties" | "Note" | "StoryPreference" | "InCopyExportOption" => {}
|
||||
_ => self.walk_story(c, b, pf, pchars, cf, brk),
|
||||
},
|
||||
},
|
||||
Node::Pi(t, v) if t == "ACE" => {
|
||||
if let Some(ch) = ace_char(v) {
|
||||
b.push(&ch.to_string(), cf);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
craft_idml::model::TextNode::Break => {
|
||||
// A column, frame or page break ends its paragraph like a return does.
|
||||
if let Some(t) = brk {
|
||||
let ch = match t {
|
||||
"NextColumn" => st::COLUMN_BREAK,
|
||||
"NextFrame" => st::FRAME_BREAK,
|
||||
"NextOddPage" => st::ODD_PAGE_BREAK,
|
||||
"NextEvenPage" => st::EVEN_PAGE_BREAK,
|
||||
_ => st::PAGE_BREAK,
|
||||
};
|
||||
b.push(&ch.to_string(), cf);
|
||||
}
|
||||
b.push("\n", cf);
|
||||
b.paras.push(pf.clone());
|
||||
b.fresh = true;
|
||||
}
|
||||
craft_idml::model::TextNode::Table(_) => {
|
||||
// `None` once the file-wide cell budget is spent: the table is not read
|
||||
// at all rather than kept as a 1 x 1 (noted in review).
|
||||
if let Some(t) = self.table(&c) {
|
||||
b.push_table(t, pf, cf);
|
||||
}
|
||||
}
|
||||
craft_idml::model::TextNode::Container(container) => {
|
||||
let container_el = crate::xml::from_craft(container.to_el());
|
||||
match container.kind() {
|
||||
craft_idml::model::ContainerKind::HyperlinkTextSource => {
|
||||
let start = b.text.len();
|
||||
self.walk_story_nodes(&container.children, b, pf, pchars, cf, brk);
|
||||
if b.text.len() > start {
|
||||
b.link_srcs.push((container.self_id().unwrap_or("").to_string(), start, b.text.len()));
|
||||
}
|
||||
}
|
||||
craft_idml::model::ContainerKind::Footnote => {
|
||||
let mut note = self.story(StoryId(0), &container_el);
|
||||
let sep = self.footnote_options.separator.clone();
|
||||
if !sep.is_empty() && note.text.starts_with(&sep) {
|
||||
note.delete(0..sep.len());
|
||||
}
|
||||
b.notes.push(note);
|
||||
let mut rcf = cf.clone();
|
||||
rcf.over.position = None;
|
||||
b.push(&designcraft_doc::FOOTNOTE_REF.to_string(), &rcf);
|
||||
}
|
||||
craft_idml::model::ContainerKind::Change => {
|
||||
let mut ncf = cf.clone();
|
||||
match container.change_kind() {
|
||||
Some(craft_idml::model::TrackedChangeKind::Deleted) => ncf.over.change = Some(designcraft_doc::ChangeMark::Deleted),
|
||||
Some(craft_idml::model::TrackedChangeKind::Inserted) => ncf.over.change = Some(designcraft_doc::ChangeMark::Inserted),
|
||||
_ => {}
|
||||
}
|
||||
self.walk_story_nodes(&container.children, b, pf, pchars, &ncf, brk);
|
||||
}
|
||||
craft_idml::model::ContainerKind::XMLElement => self.walk_story_nodes(&container.children, b, pf, pchars, cf, brk),
|
||||
craft_idml::model::ContainerKind::CrossReferenceSource => {
|
||||
let page_variables: Vec<_> = self.xref_page_vars.iter().cloned().collect();
|
||||
let saved = craft_idml::model::SavedCrossReference::from_container(container, &page_variables);
|
||||
let mut text = String::new();
|
||||
let mut page = None;
|
||||
for part in saved.parts {
|
||||
match part {
|
||||
craft_idml::model::SavedCrossReferencePart::Text(value) => text.push_str(&value),
|
||||
craft_idml::model::SavedCrossReferencePart::PageNumber { result_text } => {
|
||||
text.push_str(designcraft_doc::xref::PAGE_NUM_BLOCK);
|
||||
if page.is_none() {
|
||||
page = result_text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
b.xrefs.push(XrefSource {
|
||||
me: saved.self_id.unwrap_or_default(),
|
||||
format: saved.applied_format.unwrap_or_default(),
|
||||
text,
|
||||
page,
|
||||
});
|
||||
let f = saved.character_range.map_or_else(
|
||||
|| cf.clone(),
|
||||
|range| {
|
||||
let el = crate::xml::from_craft(craft_idml::model::TextNode::Character(range).to_el());
|
||||
self.range_format(&el, pchars, cf)
|
||||
},
|
||||
);
|
||||
b.push(&designcraft_doc::XREF_MARK.to_string(), &f);
|
||||
}
|
||||
craft_idml::model::ContainerKind::Note | craft_idml::model::ContainerKind::EndnoteRange => {}
|
||||
}
|
||||
}
|
||||
craft_idml::model::TextNode::Anchor(anchor) => {
|
||||
b.anchors.push((anchor.self_id().unwrap_or("").to_string(), anchor.label().unwrap_or("Anchor").to_string()));
|
||||
b.push(&designcraft_doc::ANCHOR_MARK.to_string(), cf);
|
||||
}
|
||||
craft_idml::model::TextNode::PageReference(reference) => {
|
||||
use designcraft_doc::index::IndexRange as R;
|
||||
let topic = reference.referenced_topic().and_then(|r| self.index_topics.get(r)).cloned().unwrap_or_default();
|
||||
let range = match reference.reference_type() {
|
||||
Some("ToEndOfStory" | "ToEndOfDocument" | "ToEndOfSection") => R::ToEndOfStory,
|
||||
Some("ForNextNParagraphs") => R::NextParagraphs(reference.limit().unwrap_or(1.0).max(1.0) as u32),
|
||||
Some("SuppressPageNumbers") => R::SuppressPageRange,
|
||||
_ => R::CurrentPage,
|
||||
};
|
||||
b.index_refs.push(designcraft_doc::IndexRef { topics: topic.path, sort: topic.sort, range });
|
||||
b.push(&designcraft_doc::INDEX_MARK.to_string(), cf);
|
||||
}
|
||||
craft_idml::model::TextNode::TextVariable(values) => {
|
||||
let text = self.text_variable_instance(values);
|
||||
b.push(&text, cf);
|
||||
}
|
||||
craft_idml::model::TextNode::Item(_) | craft_idml::model::TextNode::Other(_) => match c.local() {
|
||||
// Anchored page items, including groups containing text frames.
|
||||
"Rectangle" | "Oval" | "Polygon" | "GraphicLine" | "Group" | "TextFrame" => {
|
||||
if let Some(item) = self.item(&c, designcraft_geom::Affine::IDENTITY) {
|
||||
use designcraft_doc::anchored::AnchorAlign as A;
|
||||
// Each setting the item leaves unset comes from its object style chain,
|
||||
// then from the IDML defaults.
|
||||
let levels: Vec<&El> = std::iter::once(&c).chain(self.object_style_chain(&c)).collect();
|
||||
let s = |k: &str| {
|
||||
let set = levels.iter().filter_map(|l| l.find("AnchoredObjectSetting")).find_map(|x| x.get(k));
|
||||
set.unwrap_or(match k {
|
||||
"AnchoredPosition" => "InlinePosition",
|
||||
"AnchorPoint" => "BottomRightAnchor",
|
||||
"HorizontalAlignment" => "LeftAlign",
|
||||
"HorizontalReferencePoint" => "TextFrame",
|
||||
"VerticalAlignment" => "BottomAlign",
|
||||
"VerticalReferencePoint" => "LineBaseline",
|
||||
"PinPosition" => "true",
|
||||
_ => "",
|
||||
})
|
||||
};
|
||||
let get = |k: &str| s(k).trim().parse::<f64>().ok().filter(|v| v.is_finite()).unwrap_or(0.0);
|
||||
let position = match s("AnchoredPosition") {
|
||||
"AboveLine" => designcraft_doc::AnchorPosition::AboveLine {
|
||||
align: match s("HorizontalAlignment") {
|
||||
"CenterAlign" => A::Center,
|
||||
"RightAlign" => A::Right,
|
||||
_ => A::Left,
|
||||
},
|
||||
space_before: get("AnchorSpaceAbove"),
|
||||
space_after: get("AnchorYoffset"),
|
||||
},
|
||||
"Anchored" => {
|
||||
let col = match s("HorizontalAlignment") {
|
||||
"CenterAlign" => 1,
|
||||
"RightAlign" => 2,
|
||||
_ => 0,
|
||||
};
|
||||
let row = match s("VerticalAlignment") {
|
||||
"CenterAlign" => 1,
|
||||
"BottomAlign" => 2,
|
||||
_ => 0,
|
||||
};
|
||||
designcraft_doc::AnchorPosition::Custom {
|
||||
x_relative: names::anchor_rel_in(s("HorizontalReferencePoint")),
|
||||
y_relative: names::anchor_rel_in(s("VerticalReferencePoint")),
|
||||
// IDML measures a left-aligned object's X offset leftwards.
|
||||
x_offset: if col == 0 { 0.0 - get("AnchorXoffset") } else { get("AnchorXoffset") },
|
||||
y_offset: get("AnchorYoffset"),
|
||||
object_point: names::anchor_in(s("AnchorPoint")),
|
||||
ref_point: row * 3 + col,
|
||||
keep_within_column: s("PinPosition") == "true",
|
||||
spine_relative: s("SpineRelative") == "true",
|
||||
}
|
||||
}
|
||||
_ => designcraft_doc::AnchorPosition::Inline { y_offset: get("AnchorYoffset") },
|
||||
};
|
||||
b.objects.push(designcraft_doc::AnchoredObject::new(item, position));
|
||||
b.push(&designcraft_doc::OBJECT_MARK.to_string(), cf);
|
||||
}
|
||||
}
|
||||
"Properties" | "Note" | "StoryPreference" | "InCopyExportOption" => {}
|
||||
_ => {
|
||||
if let craft_idml::model::TextNode::Other(other) = node {
|
||||
let nested = craft_idml::model::Story::from_el(other);
|
||||
self.walk_story_nodes(&nested.children, b, pf, pchars, cf, brk);
|
||||
}
|
||||
}
|
||||
},
|
||||
craft_idml::model::TextNode::ProcessingInstruction { .. } => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user