mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-24 11:10:45 +01:00
feat: write articles, group article children and EPUB accessibility values
Article elements with ArticleMember children from document chunk 0x21B03 and the articles' chunks 0x13C and 0x21B02; Group ArticleChildrenList from chunk 0x21B05; the nine empty EpubAccessibility attributes from INDD 19.3 and EpubCreatePageNavigation from 19.4.
This commit is contained in:
1 parent
6fe7f19654
commit
8fe6eaf05d
9 files changed
+250
No files matched your search
@@ -381,6 +381,7 @@ fn document(seed: u64, ids: &Ids) -> Objects {
|
||||
(chunk::DOC_MASTER_SPREADS, class::MASTER_SPREAD),
|
||||
(chunk::DOC_STORIES, class::STORY),
|
||||
(chunk::DOC_SECTIONS, class::SECTION),
|
||||
(chunk::DOC_ARTICLES, class::ARTICLE),
|
||||
] {
|
||||
let n = g.rng.below(4);
|
||||
let members = take(&mut g, n);
|
||||
|
||||
@@ -541,10 +541,45 @@ impl Writer<'_> {
|
||||
a.write(&mut x);
|
||||
}
|
||||
}
|
||||
self.articles(&mut x);
|
||||
x.end();
|
||||
x.finish()
|
||||
}
|
||||
|
||||
/// Articles, after the assignments (objects.md, articles).
|
||||
/// `ArticleExportStatus` has no stored field; it is `true` in every
|
||||
/// reference (idml-values.md, defaults). A member that is not written
|
||||
/// as an element is left out with a warning.
|
||||
fn articles(&self, x: &mut Xml) {
|
||||
if self.doc.articles.is_empty() {
|
||||
return;
|
||||
}
|
||||
let written = self.written_items();
|
||||
for a in &self.doc.articles {
|
||||
let id = uref(Some(a.uid));
|
||||
x.start("Article")
|
||||
.attr("Self", id.as_str())
|
||||
.attr("Name", a.name.as_str())
|
||||
.attr("ArticleExportStatus", "true");
|
||||
let mut i = 0;
|
||||
for &m in &a.members {
|
||||
if !written.contains(&m) {
|
||||
self.warnings.borrow_mut().push(format!(
|
||||
"article {}: member {m} left out: not written",
|
||||
a.uid
|
||||
));
|
||||
continue;
|
||||
}
|
||||
x.start("ArticleMember")
|
||||
.attr("Self", format!("{id}ArticleMember{i}"))
|
||||
.attr("ItemRef", uref(Some(m)));
|
||||
x.end();
|
||||
i += 1;
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
}
|
||||
|
||||
/// The IDML reference of a TOC style.
|
||||
fn named_grid_name(ng: &crate::model::NamedGrid) -> String {
|
||||
if ng.builtin {
|
||||
|
||||
@@ -764,6 +764,21 @@ impl Writer<'_> {
|
||||
ItemKind::Form(f) => form_tag(f.kind),
|
||||
};
|
||||
x.start(tag).attr("Self", uref(Some(item.uid)));
|
||||
if let Some(list) = &item.article_children {
|
||||
// Only the group's own children: a reference to an item that is
|
||||
// not written would dangle.
|
||||
let (kept, dropped): (Vec<u32>, Vec<u32>) = list
|
||||
.iter()
|
||||
.partition(|u| item.children.iter().any(|c| c.uid == **u));
|
||||
if !dropped.is_empty() {
|
||||
self.warnings.borrow_mut().push(format!(
|
||||
"group {}: article children {dropped:?} left out: not written in the group",
|
||||
item.uid
|
||||
));
|
||||
}
|
||||
let refs: Vec<String> = kept.iter().map(|&u| uref(Some(u))).collect();
|
||||
x.attr("ArticleChildrenList", refs.join(" "));
|
||||
}
|
||||
if let ItemKind::TextFrame {
|
||||
story,
|
||||
previous,
|
||||
|
||||
@@ -54,6 +54,8 @@ pub struct Document {
|
||||
/// The name of the first assignment, in the language of the edition
|
||||
/// that made the document.
|
||||
pub assignment_name: Option<String>,
|
||||
/// Articles, in the order of document chunk 0x21B03.
|
||||
pub articles: Vec<Article>,
|
||||
/// Named grids (class 0xCD12, chunk 0xCD28: u32, a flag byte, 1 for
|
||||
/// a built-in key, and the name), in UID order.
|
||||
pub named_grids: Vec<NamedGrid>,
|
||||
@@ -122,6 +124,15 @@ pub struct Document {
|
||||
pub black_name: Option<String>,
|
||||
}
|
||||
|
||||
/// An article (class 0x21B01): its name (chunk 0x13C) and the UIDs of
|
||||
/// its member page items (chunk 0x21B02), in order.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Article {
|
||||
pub uid: u32,
|
||||
pub name: String,
|
||||
pub members: Vec<u32>,
|
||||
}
|
||||
|
||||
/// The stories of the text frames anchored in `story`, in text order
|
||||
/// (nested items included).
|
||||
fn anchored_stories(story: &Story) -> Vec<u32> {
|
||||
@@ -311,6 +322,7 @@ impl<'a> Reader<'a> {
|
||||
}),
|
||||
None => None,
|
||||
};
|
||||
let articles = self.articles();
|
||||
let users_script = self.users_script(DOC);
|
||||
let last_session = self.last_session(DOC);
|
||||
let last_session_platform = self.last_session_platform(DOC);
|
||||
@@ -387,6 +399,7 @@ impl<'a> Reader<'a> {
|
||||
axial_shades,
|
||||
assignments,
|
||||
assignment_name,
|
||||
articles,
|
||||
style_groups: objects.style_groups,
|
||||
object_styles: objects.object_styles,
|
||||
cell_styles: objects.cell_styles,
|
||||
@@ -498,6 +511,44 @@ impl<'a> Reader<'a> {
|
||||
a
|
||||
}
|
||||
|
||||
/// The articles listed in document chunk 0x21B03, each once. A listed
|
||||
/// object that is not an article is skipped; an article without a
|
||||
/// readable name or member list is left out with a warning.
|
||||
pub(super) fn articles(&self) -> Vec<Article> {
|
||||
let order = self.uid_list(DOC, chunk::DOC_ARTICLES).unwrap_or_else(|e| {
|
||||
self.warn(format!("articles left out: {e}"));
|
||||
Vec::new()
|
||||
});
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut out = Vec::new();
|
||||
for uid in order {
|
||||
if self.class(uid) != Some(class::ARTICLE) || !seen.insert(uid) {
|
||||
continue;
|
||||
}
|
||||
match self.article(uid) {
|
||||
Ok(Some(a)) => out.push(a),
|
||||
Ok(None) => self.warn(format!("article {uid} left out: no name")),
|
||||
Err(e) => self.warn(format!("article {uid} left out: {e}")),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn article(&self, uid: u32) -> Result<Option<Article>, Error> {
|
||||
let obj = self.object(uid)?;
|
||||
let Some(d) = obj.chunk(chunk::ARTICLE_NAME) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let mut c = self.cursor(d);
|
||||
c.flag()?;
|
||||
let name = c.string()?;
|
||||
let members = match obj.chunk(chunk::ARTICLE_MEMBERS) {
|
||||
Some(d) => self.cursor(d).u32_list()?,
|
||||
None => Vec::new(),
|
||||
};
|
||||
Ok(Some(Article { uid, name, members }))
|
||||
}
|
||||
|
||||
/// Read every object of the class list whose class the converter
|
||||
/// knows. An object that cannot be read is left out with a warning.
|
||||
fn class_objects(&self) -> Result<ClassObjects, Error> {
|
||||
|
||||
@@ -48,6 +48,8 @@ pub mod class {
|
||||
/// A motion preset (IDML `MotionPreset`).
|
||||
pub const MOTION_PRESET: u32 = 0x20517;
|
||||
pub const ASSIGNMENT: u32 = 0x1BE01;
|
||||
/// An article (objects.md, articles).
|
||||
pub const ARTICLE: u32 = 0x21B01;
|
||||
pub const SMOOTH_SHADE: u32 = 0x5533;
|
||||
/// Form fields and multi-state objects (IDML `Button`, `CheckBox`,
|
||||
/// `RadioButton`, `TextBox`, `ComboBox`, `SignatureField`,
|
||||
@@ -231,6 +233,14 @@ pub mod chunk {
|
||||
pub const LANGUAGE_QUOTES: u32 = 0x2D26;
|
||||
/// The name of an assignment, then two strings.
|
||||
pub const ASSIGNMENT_NAME: u32 = 0x1BE1B;
|
||||
/// The articles of the document, in order: a UID list.
|
||||
pub const DOC_ARTICLES: u32 = 0x21B03;
|
||||
/// The name of an article: a flag byte and an in-object string.
|
||||
pub const ARTICLE_NAME: u32 = 0x13C;
|
||||
/// The members of an article, in order: a UID list of page items.
|
||||
pub const ARTICLE_MEMBERS: u32 = 0x21B02;
|
||||
/// A group's article children list (IDML `ArticleChildrenList`).
|
||||
pub const GROUP_ARTICLE_CHILDREN: u32 = 0x21B05;
|
||||
pub const ANCHOR_CHILDREN: u32 = 0x2C8;
|
||||
/// Anchored object settings, of an anchor or an object style.
|
||||
pub const ANCHOR_SETTINGS: u32 = 0x2800;
|
||||
|
||||
@@ -195,6 +195,9 @@ pub struct PageItem {
|
||||
pub label: Vec<(String, String)>,
|
||||
/// Animation (chunk 0x2055F).
|
||||
pub animation: Option<Animation>,
|
||||
/// A group's article children list (chunk 0x21B05), in stored order;
|
||||
/// `None` without the chunk.
|
||||
pub article_children: Option<Vec<u32>>,
|
||||
}
|
||||
|
||||
/// Text on the path of a shape or frame (class 0xB320). See
|
||||
@@ -1250,6 +1253,15 @@ impl<'a> Reader<'a> {
|
||||
Vec::new()
|
||||
}),
|
||||
animation: self.animation(uid),
|
||||
article_children: if cls == Some(class::GROUP) {
|
||||
self.uid_list_or_none(uid, chunk::GROUP_ARTICLE_CHILDREN)
|
||||
.unwrap_or_else(|e| {
|
||||
self.warn(format!("group {uid}: article children list left out: {e}"));
|
||||
None
|
||||
})
|
||||
} else {
|
||||
None
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -453,4 +453,93 @@ mod tests {
|
||||
assert_eq!(classify(&[off], None), Shape::Oval);
|
||||
assert_eq!(classify(&[round(1)], Some((1, 4))), Shape::Oval);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_articles_and_article_children() {
|
||||
let list = |uids: &[u32]| {
|
||||
let mut d = (uids.len() as u32).to_le_bytes().to_vec();
|
||||
uids.iter().for_each(|u| d.extend(u.to_le_bytes()));
|
||||
d
|
||||
};
|
||||
let name = |s| synthetic::flagged_string(Encoding::default(), 2, s);
|
||||
// The document lists article 5 twice and group 30, which is not an
|
||||
// article; article 7 is not listed. Member 99 is not written.
|
||||
let mut group30 = group(30, &[31, 32]);
|
||||
group30.2.extend(synthetic::chunks(&[(
|
||||
chunk::GROUP_ARTICLE_CHILDREN,
|
||||
list(&[32, 31]),
|
||||
)]));
|
||||
let objects = [
|
||||
(
|
||||
1,
|
||||
class::DOCUMENT,
|
||||
synthetic::chunks(&[(chunk::DOC_ARTICLES, list(&[5, 6, 30, 5]))]),
|
||||
),
|
||||
(
|
||||
5,
|
||||
class::ARTICLE,
|
||||
synthetic::chunks(&[
|
||||
(chunk::ARTICLE_NAME, name("Main")),
|
||||
(chunk::ARTICLE_MEMBERS, list(&[30, 31, 99])),
|
||||
]),
|
||||
),
|
||||
(
|
||||
6,
|
||||
class::ARTICLE,
|
||||
synthetic::chunks(&[(chunk::ARTICLE_NAME, name("Side"))]),
|
||||
),
|
||||
(
|
||||
7,
|
||||
class::ARTICLE,
|
||||
synthetic::chunks(&[(chunk::ARTICLE_NAME, name("Unlisted"))]),
|
||||
),
|
||||
group30,
|
||||
group(31, &[]),
|
||||
group(32, &[]),
|
||||
];
|
||||
let bytes = synthetic::image(&objects);
|
||||
let db = synthetic::database(&bytes, &objects);
|
||||
let reader = Reader::new(&db);
|
||||
let articles = reader.articles();
|
||||
let item = reader.page_item(30, None).unwrap().unwrap();
|
||||
assert_eq!(item.article_children, Some(vec![32, 31]));
|
||||
assert_eq!(item.children[0].article_children, None);
|
||||
let doc = Document {
|
||||
articles,
|
||||
spreads: vec![Spread {
|
||||
uid: 2,
|
||||
master_name: None,
|
||||
transform: Matrix::IDENTITY,
|
||||
binding_location: 0,
|
||||
pages: Vec::new(),
|
||||
items: vec![item],
|
||||
guides: Vec::new(),
|
||||
shuffle: None,
|
||||
flattener_resolution: None,
|
||||
show_master_items: None,
|
||||
primary_story: None,
|
||||
transition: None,
|
||||
timing: Vec::new(),
|
||||
}],
|
||||
..Document::default()
|
||||
};
|
||||
let mut out = Vec::new();
|
||||
let warnings = crate::idml::write(&doc, "a.indd", &mut out).unwrap();
|
||||
let out = String::from_utf8_lossy(&out);
|
||||
let articles = &out[out.find("<Article ").unwrap()..];
|
||||
let articles: String = articles[..articles.find("</Document>").unwrap()]
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
articles,
|
||||
"<Article Self=\"u5\" Name=\"Main\" ArticleExportStatus=\"true\">\
|
||||
<ArticleMember Self=\"u5ArticleMember0\" ItemRef=\"u1e\" />\
|
||||
<ArticleMember Self=\"u5ArticleMember1\" ItemRef=\"u1f\" />\
|
||||
</Article>\
|
||||
<Article Self=\"u6\" Name=\"Side\" ArticleExportStatus=\"true\" />"
|
||||
);
|
||||
assert!(out.contains("<Group Self=\"u1e\" ArticleChildrenList=\"u20 u1f\""));
|
||||
assert!(warnings.iter().any(|w| w.contains("member 99 left out")));
|
||||
}
|
||||
}
|
||||
@@ -1130,6 +1130,19 @@ fn publish_export(enc: Encoding, d: &[u8]) -> Option<Vec<(&'static str, String)>
|
||||
read().ok()
|
||||
}
|
||||
|
||||
/// The EPUB accessibility attributes, in IDML order.
|
||||
const EPUB_ACCESSIBILITY: [&str; 9] = [
|
||||
"EpubAccessibilityFeature",
|
||||
"EpubAccessibilityHazard",
|
||||
"EpubAccessibilityMode",
|
||||
"EpubAccessibilityModeSufficient",
|
||||
"EpubAccessibilitySummary",
|
||||
"EpubAccessibilityConformsTo",
|
||||
"EpubAccessibilityCertifiedBy",
|
||||
"EpubAccessibilityCredentials",
|
||||
"EpubAccessibilityReportLink",
|
||||
];
|
||||
|
||||
/// The EPUB identifier of documents without EPUB export options.
|
||||
const EPUB_ID: &str = "urn:uuid:29d919dd-24f5-4384-be78-b447c9dc299b";
|
||||
|
||||
@@ -2336,6 +2349,22 @@ impl Reader<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// EPUB accessibility from INDD 19.3, page navigation from 19.4
|
||||
// (preferences.md, EPUB accessibility). No sample has a value.
|
||||
let v = (major, version.minor);
|
||||
if v >= (19, 3) {
|
||||
for name in EPUB_ACCESSIBILITY {
|
||||
set("EPubExportPreference", name, String::new());
|
||||
set("EPubFixedLayoutExportPreference", name, String::new());
|
||||
}
|
||||
}
|
||||
if v >= (19, 4) {
|
||||
set(
|
||||
"EPubExportPreference",
|
||||
"EpubCreatePageNavigation",
|
||||
"true".into(),
|
||||
);
|
||||
}
|
||||
// Fixed layout EPUB export, from DOM 10.
|
||||
if major >= 10 {
|
||||
match get(id::EPUB_FIXED_LAYOUT)? {
|
||||
|
||||
@@ -108,6 +108,14 @@ impl<'a> Reader<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The UID list of chunk `id`, or `None` without the chunk.
|
||||
pub(super) fn uid_list_or_none(&self, uid: u32, id: u32) -> Result<Option<Vec<u32>>, Error> {
|
||||
match self.chunk(uid, id)? {
|
||||
Some(data) => self.cursor(&data).u32_list().map(Some),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Children listed in a hierarchy chunk: parent, owner, then a u32 list.
|
||||
pub(super) fn children(&self, uid: u32, id: u32) -> Result<Vec<u32>, Error> {
|
||||
match self.chunk(uid, id)? {
|
||||
|
||||
Reference in new issue
Block a user