mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-30 18:14:33 +01:00
feat: write page item values only where they differ from the object style
IDML leaves out the values of a page item that equal those of its applied object style. The writer now compares with the style's effective values (its own, then the styles it is based on, then [None]): - TextFramePreference: an attribute is written when its category is off in the style's category list (chunk 0x1B92E, also for [None]) or its value differs; TextColumnMaxWidth (DOM 8), TextColumnCount (DOM 10) and InsetSpacing (DOM 11) are always written; footnote values from 13.1 (objects.md, text frame preferences). - Fill, stroke, miter and corner attributes (attributes.md). - Transparency effects: only differing values, and an effect element only when it has one or its opacity stops differ (transparency.md). New decoding: object style chunk 0x1B924 fields for balance, fixed width, justification, first baseline offset (code 4 = XHeight, also for frames) and auto-sizing; chunk 0x1B92C (style transparency). Object styles get the DOM 12 text frame values from DOM 8 and FootnotesEnableOverrides from 13.1 (idml-values.md).
This commit is contained in:
1 parent
17c6ace935
commit
09e87fadea
13 files changed
+590
-68
No files matched your search
@@ -30,6 +30,8 @@ pub enum List {
|
||||
ObjectStyle,
|
||||
/// An object style's frame fitting attributes.
|
||||
ObjectStyleFitting,
|
||||
/// An object style's transparency attributes.
|
||||
ObjectStyleTransparency,
|
||||
/// A paragraph or character style's text attributes.
|
||||
Style,
|
||||
/// The text attributes of a run of text.
|
||||
@@ -47,6 +49,7 @@ impl List {
|
||||
List::Item => "page item",
|
||||
List::ObjectStyle => "object style",
|
||||
List::ObjectStyleFitting => "object style fitting",
|
||||
List::ObjectStyleTransparency => "object style transparency",
|
||||
List::Style => "style",
|
||||
List::Text => "text",
|
||||
List::Table => "table",
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
//! The values of an applied object style that a page item is compared
|
||||
//! with. IDML writes some values of a page item only when they differ
|
||||
//! from its object style (`docs/format/objects.md`, page item settings and
|
||||
//! text frame preferences).
|
||||
|
||||
use super::*;
|
||||
|
||||
/// The effective values of an object style as the converter writes them:
|
||||
/// its own, then those of the styles it is based on, then the root
|
||||
/// `[None]`.
|
||||
#[derive(Debug, Default)]
|
||||
pub(super) struct StyleValues {
|
||||
/// Page item attributes (`FillColor`, `StrokeWeight`, …).
|
||||
pub item: Vec<(String, String)>,
|
||||
/// `TextFramePreference` attributes.
|
||||
pub frame: Vec<(String, String)>,
|
||||
/// The style's category list (chunk 0x1B92E); `None` without it.
|
||||
pub enabled: Option<Vec<u32>>,
|
||||
/// Transparency attributes (chunk 0x1B92C) by attribute-list ID.
|
||||
pub transparency: Vec<(u32, crate::model::Value)>,
|
||||
}
|
||||
|
||||
impl StyleValues {
|
||||
pub(super) fn item(&self, name: &str) -> Option<&str> {
|
||||
self.item
|
||||
.iter()
|
||||
.find(|(k, _)| k == name)
|
||||
.map(|(_, v)| v.as_str())
|
||||
}
|
||||
|
||||
pub(super) fn transparency(&self, id: u32) -> Option<&crate::model::Value> {
|
||||
self.transparency
|
||||
.iter()
|
||||
.find(|(i, _)| *i == id)
|
||||
.map(|(_, v)| v)
|
||||
}
|
||||
|
||||
pub(super) fn frame(&self, name: &str) -> Option<&str> {
|
||||
self.frame
|
||||
.iter()
|
||||
.find(|(k, _)| k == name)
|
||||
.map(|(_, v)| v.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether two IDML values are the same: equal text, or numbers equal to
|
||||
/// 6 significant digits (as `tools/compare.py` compares them).
|
||||
pub(super) fn same_value(a: &str, b: &str) -> bool {
|
||||
if a == b {
|
||||
return true;
|
||||
}
|
||||
match (a.parse::<f64>(), b.parse::<f64>()) {
|
||||
(Ok(x), Ok(y)) => (x - y).abs() <= 5e-7 * x.abs().max(y.abs()),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn add_missing(to: &mut Vec<(String, String)>, from: &[(String, String)]) {
|
||||
for (k, v) in from {
|
||||
if !to.iter().any(|(n, _)| n == k) {
|
||||
to.push((k.clone(), v.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer<'_> {
|
||||
/// Whether `os` is the root object style `[None]`.
|
||||
pub(super) fn is_root_object_style(os: &crate::model::ObjectStyle) -> bool {
|
||||
os.builtin && os.name == "[None]" && os.based_on.is_none()
|
||||
}
|
||||
|
||||
/// The effective values of object style `uid`, or `None` if the
|
||||
/// document has no such style.
|
||||
pub(super) fn style_values(&self, uid: u32) -> Option<std::rc::Rc<StyleValues>> {
|
||||
if let Some(v) = self.style_values.borrow().get(&uid) {
|
||||
return Some(v.clone());
|
||||
}
|
||||
let styles = &self.doc.object_styles;
|
||||
let os = styles.get(&uid)?;
|
||||
let root = styles.values().find(|s| Self::is_root_object_style(s));
|
||||
let mut out = StyleValues {
|
||||
enabled: os.enabled.clone(),
|
||||
..StyleValues::default()
|
||||
};
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut cur = Some(os);
|
||||
while let Some(s) = cur {
|
||||
if !seen.insert(s.uid) {
|
||||
break;
|
||||
}
|
||||
let is_root = Self::is_root_object_style(s);
|
||||
let node = self.object_style_node(s, is_root);
|
||||
add_missing(&mut out.item, &node.attrs);
|
||||
for (id, v) in &s.transparency.values {
|
||||
if out.transparency.iter().all(|(i, _)| i != id) {
|
||||
out.transparency.push((*id, v.clone()));
|
||||
}
|
||||
}
|
||||
// A style without text frame settings takes them from the
|
||||
// style it is based on.
|
||||
if (s.frame.is_some() || is_root)
|
||||
&& let Some(t) = node.child("TextFramePreference")
|
||||
{
|
||||
add_missing(&mut out.frame, &t.attrs);
|
||||
}
|
||||
cur = match s.based_on.and_then(|b| styles.get(&b)) {
|
||||
Some(b) => Some(b),
|
||||
None if !is_root => root,
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
// A chain with no text frame settings: the style's own written
|
||||
// values.
|
||||
if out.frame.is_empty()
|
||||
&& let Some(t) = self
|
||||
.object_style_node(os, false)
|
||||
.child("TextFramePreference")
|
||||
{
|
||||
out.frame = t.attrs.clone();
|
||||
}
|
||||
let v = std::rc::Rc::new(out);
|
||||
self.style_values.borrow_mut().insert(uid, v.clone());
|
||||
Some(v)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::same_value;
|
||||
|
||||
#[test]
|
||||
fn compares_numbers_to_six_digits() {
|
||||
assert!(same_value("12", "12.0000001"));
|
||||
assert!(same_value("Swatch/None", "Swatch/None"));
|
||||
assert!(!same_value("0", "1e-13"));
|
||||
assert!(!same_value("12", "12.01"));
|
||||
assert!(!same_value("TopAlign", "CenterAlign"));
|
||||
}
|
||||
}
|
||||
@@ -746,13 +746,6 @@ impl Writer<'_> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Write page item attributes from the item's attribute list.
|
||||
pub(super) fn item_attrs(&self, x: &mut Xml, attrs: &Attrs) {
|
||||
for (name, text) in self.item_attr_values(attrs) {
|
||||
x.attr(name, text);
|
||||
}
|
||||
}
|
||||
|
||||
/// Page item attributes from an attribute list, as IDML values.
|
||||
pub(super) fn item_attr_values(&self, attrs: &Attrs) -> Vec<(&'static str, String)> {
|
||||
self.attr_values(attrs, ITEM_ATTRS)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//! the text forms of values, `pages` page numbering, `values` the values
|
||||
//! every IDML has, and `transparency` the effect settings.
|
||||
|
||||
mod applied;
|
||||
mod attrs;
|
||||
mod designmap;
|
||||
mod export;
|
||||
@@ -65,6 +66,9 @@ struct Writer<'a> {
|
||||
page_sections: Vec<(Section, u32)>,
|
||||
/// Observed attributes by element path (`values::element_attrs`).
|
||||
observed: std::cell::RefCell<std::collections::HashMap<String, Observed>>,
|
||||
/// Effective values of object styles, by UID (`applied`).
|
||||
style_values:
|
||||
std::cell::RefCell<std::collections::HashMap<u32, std::rc::Rc<applied::StyleValues>>>,
|
||||
}
|
||||
|
||||
/// Attributes observed on an element path (`values::element_attrs`).
|
||||
@@ -118,6 +122,7 @@ pub fn write(doc: &Document, name: &str, out: impl std::io::Write) -> std::io::R
|
||||
group_path: group_paths(doc),
|
||||
warnings: Default::default(),
|
||||
observed: Default::default(),
|
||||
style_values: Default::default(),
|
||||
page_layouts: alternate_layouts(doc).1,
|
||||
page_sections: page_sections(doc),
|
||||
};
|
||||
@@ -171,6 +176,7 @@ impl<'a> Writer<'a> {
|
||||
group_path: Default::default(),
|
||||
warnings: Default::default(),
|
||||
observed: Default::default(),
|
||||
style_values: Default::default(),
|
||||
page_layouts: Vec::new(),
|
||||
page_sections: Vec::new(),
|
||||
}
|
||||
|
||||
+170
-54
@@ -19,6 +19,91 @@ pub(super) fn spread_origin(pages: &[Page]) -> (f64, f64) {
|
||||
.unwrap_or((0.0, 0.0))
|
||||
}
|
||||
|
||||
/// Page item attributes that IDML writes only when they differ from the
|
||||
/// applied object style (attributes.md, page item attributes).
|
||||
const STYLE_COMPARED: &[&str] = &[
|
||||
"FillColor",
|
||||
"FillTint",
|
||||
"StrokeColor",
|
||||
"StrokeWeight",
|
||||
"MiterLimit",
|
||||
"CornerOption",
|
||||
"CornerRadius",
|
||||
"TopLeftCornerRadius",
|
||||
"TopRightCornerRadius",
|
||||
"BottomLeftCornerRadius",
|
||||
"BottomRightCornerRadius",
|
||||
"TopLeftCornerOption",
|
||||
"TopRightCornerOption",
|
||||
"BottomLeftCornerOption",
|
||||
"BottomRightCornerOption",
|
||||
];
|
||||
|
||||
/// The category of a `TextFramePreference` attribute: the ID that turns
|
||||
/// it on in an object style's category list (objects.md, text frame
|
||||
/// preferences).
|
||||
fn frame_category(name: &str) -> Option<u32> {
|
||||
Some(match name {
|
||||
"TextColumnCount"
|
||||
| "TextColumnGutter"
|
||||
| "TextColumnFixedWidth"
|
||||
| "UseFixedColumnWidth"
|
||||
| "UseFlexibleColumnWidth"
|
||||
| "TextColumnMaxWidth"
|
||||
| "VerticalJustification"
|
||||
| "VerticalThreshold"
|
||||
| "IgnoreWrap"
|
||||
| "VerticalBalanceColumns" => 0x1B93E,
|
||||
"FirstBaselineOffset" | "MinimumFirstBaselineOffset" => 0xADC8,
|
||||
n if n.starts_with("Footnotes") => 0xADCA,
|
||||
n if n.starts_with("ColumnRule") => 0xADCB,
|
||||
n if n.contains("AutoSizing") => 0xADC9,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// `TextFramePreference` attributes that IDML writes on every frame of
|
||||
/// the version, whatever the object style (objects.md, text frame
|
||||
/// preferences). `InsetSpacing` (DOM 11 on) is written separately.
|
||||
fn frame_always(name: &str, major: u32) -> bool {
|
||||
match name {
|
||||
"TextColumnMaxWidth" => major >= 8,
|
||||
"TextColumnCount" => major >= 10,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// `AutoSizingReferencePoint` codes of frames and object styles.
|
||||
pub(super) const POINTS: [&str; 9] = [
|
||||
"TopLeftPoint",
|
||||
"TopCenterPoint",
|
||||
"TopRightPoint",
|
||||
"LeftCenterPoint",
|
||||
"CenterPoint",
|
||||
"RightCenterPoint",
|
||||
"BottomLeftPoint",
|
||||
"BottomCenterPoint",
|
||||
"BottomRightPoint",
|
||||
];
|
||||
/// `VerticalJustification` codes.
|
||||
pub(super) const JUSTIFY: [&str; 4] = ["TopAlign", "CenterAlign", "BottomAlign", "JustifyAlign"];
|
||||
/// `FirstBaselineOffset` codes (objects.md, text frame preferences).
|
||||
pub(super) const FIRST_BASELINE: [&str; 5] = [
|
||||
"LeadingOffset",
|
||||
"AscentOffset",
|
||||
"CapHeight",
|
||||
"EmboxHeight",
|
||||
"XHeight",
|
||||
];
|
||||
/// `AutoSizingType` codes.
|
||||
pub(super) const SIZING: [&str; 5] = [
|
||||
"Off",
|
||||
"HeightOnly",
|
||||
"WidthOnly",
|
||||
"HeightAndWidth",
|
||||
"HeightAndWidthProportionally",
|
||||
];
|
||||
|
||||
impl Writer<'_> {
|
||||
pub(super) fn path_geometry(x: &mut Xml, paths: &[Path]) {
|
||||
if paths.is_empty() {
|
||||
@@ -44,92 +129,108 @@ impl Writer<'_> {
|
||||
x.end().end();
|
||||
}
|
||||
|
||||
/// `TextFramePreference` of a text frame. IDML writes an attribute
|
||||
/// only when its category is off in the frame's object style or its
|
||||
/// value differs from the style's, apart from a few attributes it
|
||||
/// always writes (objects.md, text frame preferences). `style` is the
|
||||
/// applied style's values; without a style every value is written.
|
||||
pub(super) fn text_frame_preference(
|
||||
&self,
|
||||
x: &mut Xml,
|
||||
p: &TextFramePreferences,
|
||||
major: u32,
|
||||
swatch: impl Fn(u32) -> Option<String>,
|
||||
style: Option<&applied::StyleValues>,
|
||||
) {
|
||||
const POINTS: [&str; 9] = [
|
||||
"TopLeftPoint",
|
||||
"TopCenterPoint",
|
||||
"TopRightPoint",
|
||||
"LeftCenterPoint",
|
||||
"CenterPoint",
|
||||
"RightCenterPoint",
|
||||
"BottomLeftPoint",
|
||||
"BottomCenterPoint",
|
||||
"BottomRightPoint",
|
||||
let v = self.doc.version;
|
||||
let major = v.major;
|
||||
let swatch = |u: u32| match u {
|
||||
0 => Some("n".to_string()),
|
||||
u => self.doc.swatches.get(&u).cloned(),
|
||||
};
|
||||
let mut attrs: Vec<(&str, String)> = vec![
|
||||
("TextColumnCount", p.column_count.to_string()),
|
||||
("TextColumnGutter", num(p.column_gutter)),
|
||||
("TextColumnFixedWidth", num(p.column_fixed_width)),
|
||||
];
|
||||
const JUSTIFY: [&str; 4] = ["TopAlign", "CenterAlign", "BottomAlign", "JustifyAlign"];
|
||||
const FIRST_BASELINE: [&str; 4] =
|
||||
["LeadingOffset", "AscentOffset", "CapHeight", "EmboxHeight"];
|
||||
const SIZING: [&str; 5] = [
|
||||
"Off",
|
||||
"HeightOnly",
|
||||
"WidthOnly",
|
||||
"HeightAndWidth",
|
||||
"HeightAndWidthProportionally",
|
||||
];
|
||||
x.start("TextFramePreference")
|
||||
.attr("TextColumnCount", p.column_count.to_string())
|
||||
.attr("TextColumnGutter", num(p.column_gutter))
|
||||
.attr("TextColumnFixedWidth", num(p.column_fixed_width));
|
||||
if let Some(v) = FIRST_BASELINE.get(p.first_baseline_offset as usize) {
|
||||
x.attr("FirstBaselineOffset", *v);
|
||||
attrs.push(("FirstBaselineOffset", v.to_string()));
|
||||
}
|
||||
if let Some(v) = JUSTIFY.get(p.vertical_justification as usize) {
|
||||
x.attr("VerticalJustification", *v);
|
||||
attrs.push(("VerticalJustification", v.to_string()));
|
||||
}
|
||||
x.attr(
|
||||
attrs.push((
|
||||
"VerticalBalanceColumns",
|
||||
p.vertical_balance_columns.to_string(),
|
||||
);
|
||||
));
|
||||
if let Some(v) = SIZING.get(p.auto_sizing_type as usize) {
|
||||
x.attr("AutoSizingType", *v);
|
||||
attrs.push(("AutoSizingType", v.to_string()));
|
||||
}
|
||||
if let Some(v) = POINTS.get(p.auto_sizing_reference_point as usize) {
|
||||
x.attr("AutoSizingReferencePoint", *v);
|
||||
attrs.push(("AutoSizingReferencePoint", v.to_string()));
|
||||
}
|
||||
if let Some(w) = p.max_width {
|
||||
x.attr("TextColumnMaxWidth", num(w));
|
||||
attrs.push(("TextColumnMaxWidth", num(w)));
|
||||
}
|
||||
x.attr("UseFixedColumnWidth", p.use_fixed_width.to_string());
|
||||
attrs.push(("UseFixedColumnWidth", p.use_fixed_width.to_string()));
|
||||
if let Some(([use_height, use_width], [height, width], no_breaks)) = p.minimum_sizes {
|
||||
x.attr("UseMinimumHeightForAutoSizing", use_height.to_string())
|
||||
.attr("MinimumHeightForAutoSizing", num(height))
|
||||
.attr("UseMinimumWidthForAutoSizing", use_width.to_string())
|
||||
.attr("MinimumWidthForAutoSizing", num(width))
|
||||
.attr("UseNoLineBreaksForAutoSizing", no_breaks.to_string());
|
||||
attrs.push(("UseMinimumHeightForAutoSizing", use_height.to_string()));
|
||||
attrs.push(("MinimumHeightForAutoSizing", num(height)));
|
||||
attrs.push(("UseMinimumWidthForAutoSizing", use_width.to_string()));
|
||||
attrs.push(("MinimumWidthForAutoSizing", num(width)));
|
||||
attrs.push(("UseNoLineBreaksForAutoSizing", no_breaks.to_string()));
|
||||
}
|
||||
if let Some(v) = p.ignore_wrap {
|
||||
x.attr("IgnoreWrap", v.to_string());
|
||||
attrs.push(("IgnoreWrap", v.to_string()));
|
||||
}
|
||||
// Values every IDML of the version has where it writes them; they
|
||||
// also say from which version the column rule and footnote
|
||||
// settings exist (idml-values.md).
|
||||
// settings exist (idml-values.md). Footnote values from 13.1.
|
||||
let written = values::when_written("TextFrame/TextFramePreference", major);
|
||||
let has = |k: &str| written.iter().any(|(n, _)| n == k);
|
||||
let footnotes = (major, v.minor) >= (13, 1);
|
||||
if has("ColumnRuleOffset") {
|
||||
let (width, color) = match p.column_rule {
|
||||
Some((w, c)) => (w, swatch(c)),
|
||||
// Without the chunk (objects.md).
|
||||
None => (1.0, Some("Color/Black".to_string())),
|
||||
};
|
||||
x.attr("ColumnRuleStrokeWidth", num(width));
|
||||
attrs.push(("ColumnRuleStrokeWidth", num(width)));
|
||||
if let Some(c) = color {
|
||||
x.attr("ColumnRuleStrokeColor", c);
|
||||
attrs.push(("ColumnRuleStrokeColor", c));
|
||||
}
|
||||
if p.column_rule_override != Some(true) {
|
||||
x.attr("ColumnRuleOverride", "false");
|
||||
attrs.push(("ColumnRuleOverride", "false".into()));
|
||||
}
|
||||
}
|
||||
if has("FootnotesEnableOverrides") {
|
||||
if has("FootnotesEnableOverrides") && footnotes {
|
||||
let [spacing, between] = p.footnotes.unwrap_or([12.0, 6.0]);
|
||||
x.attr("FootnotesMinimumSpacing", num(spacing))
|
||||
.attr("FootnotesSpaceBetween", num(between));
|
||||
attrs.push(("FootnotesMinimumSpacing", num(spacing)));
|
||||
attrs.push(("FootnotesSpaceBetween", num(between)));
|
||||
}
|
||||
for (k, val) in written.iter() {
|
||||
if !footnotes && k.starts_with("Footnotes") {
|
||||
continue;
|
||||
}
|
||||
if !attrs.iter().any(|(n, _)| n == k) {
|
||||
attrs.push((k.as_str(), val.clone()));
|
||||
}
|
||||
}
|
||||
if let Some(s) = style {
|
||||
attrs.retain(|(k, val)| {
|
||||
frame_always(k, major)
|
||||
|| !s
|
||||
.enabled
|
||||
.as_ref()
|
||||
.is_some_and(|on| frame_category(k).is_some_and(|c| on.contains(&c)))
|
||||
|| !s.frame(k).is_some_and(|sv| applied::same_value(sv, val))
|
||||
});
|
||||
}
|
||||
if attrs.is_empty() && major < 11 {
|
||||
return;
|
||||
}
|
||||
x.start("TextFramePreference");
|
||||
for (k, val) in attrs {
|
||||
x.attr(k, val);
|
||||
}
|
||||
x.attrs_missing(written.iter());
|
||||
if major >= 11 {
|
||||
let [top, left, bottom, right] = p.inset;
|
||||
let item = |v: f64| Node {
|
||||
@@ -273,7 +374,19 @@ impl Writer<'_> {
|
||||
};
|
||||
x.attr("ContentType", content);
|
||||
}
|
||||
self.item_attrs(x, &item.attrs);
|
||||
let applied = item.object_style.and_then(|u| self.style_values(u));
|
||||
for (name, v) in self.item_attr_values(&item.attrs) {
|
||||
// Fill, stroke and corner values equal to the object style's
|
||||
// are left out (attributes.md, page item attributes).
|
||||
let same = STYLE_COMPARED.contains(&name)
|
||||
&& applied
|
||||
.as_ref()
|
||||
.and_then(|s| s.item(name))
|
||||
.is_some_and(|s| applied::same_value(s, &v));
|
||||
if !same {
|
||||
x.attr(name, v);
|
||||
}
|
||||
}
|
||||
let style = item
|
||||
.object_style
|
||||
.and_then(|u| self.doc.object_styles.get(&u));
|
||||
@@ -303,10 +416,7 @@ impl Writer<'_> {
|
||||
..
|
||||
} = &item.kind
|
||||
{
|
||||
Self::text_frame_preference(x, p, self.doc.version.major, |u| match u {
|
||||
0 => Some("n".to_string()),
|
||||
u => self.doc.swatches.get(&u).cloned(),
|
||||
});
|
||||
self.text_frame_preference(x, p, applied.as_deref());
|
||||
}
|
||||
let frame = matches!(
|
||||
item.kind,
|
||||
@@ -342,7 +452,13 @@ impl Writer<'_> {
|
||||
],
|
||||
);
|
||||
}
|
||||
for (effect, attr, v) in transparency::write(self, x, &item.attrs, &uref(Some(item.uid))) {
|
||||
for (effect, attr, v) in transparency::write(
|
||||
self,
|
||||
x,
|
||||
&item.attrs,
|
||||
applied.as_deref(),
|
||||
&uref(Some(item.uid)),
|
||||
) {
|
||||
self.warnings.borrow_mut().push(format!(
|
||||
"item {}: {effect} {attr} {v} is outside the IDML range; left out",
|
||||
item.uid
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
//! Evidence: `docs/format/objects.md` (styles and style groups),
|
||||
//! `attributes.md`, `tables.md` and `idml-values.md`.
|
||||
|
||||
use super::spread::{FIRST_BASELINE, JUSTIFY, POINTS, SIZING};
|
||||
use super::*;
|
||||
|
||||
/// `KeyboardShortcut` and `ExtendedKeyboardShortcut` of a style, from its
|
||||
@@ -453,6 +454,42 @@ impl Writer<'_> {
|
||||
if let Some(v) = fr.column_fixed_width {
|
||||
tf.push(("TextColumnFixedWidth", num(v)));
|
||||
}
|
||||
// Fields of the text frame settings that use the frame codes
|
||||
// (objects.md, object style settings).
|
||||
if let Some(v) = fr.vertical_balance_columns {
|
||||
tf.push(("VerticalBalanceColumns", v.to_string()));
|
||||
}
|
||||
if let Some(v) = fr.use_fixed_width {
|
||||
tf.push(("UseFixedColumnWidth", v.to_string()));
|
||||
}
|
||||
if let Some(v) = fr
|
||||
.vertical_justification
|
||||
.and_then(|c| JUSTIFY.get(c as usize))
|
||||
{
|
||||
tf.push(("VerticalJustification", v.to_string()));
|
||||
}
|
||||
if let Some(v) = fr
|
||||
.first_baseline_offset
|
||||
.and_then(|c| FIRST_BASELINE.get(c as usize))
|
||||
{
|
||||
tf.push(("FirstBaselineOffset", v.to_string()));
|
||||
}
|
||||
if major >= 8
|
||||
&& let Some((ty, point)) = fr.auto_sizing
|
||||
&& let (Some(ty), Some(point)) =
|
||||
(SIZING.get(ty as usize), POINTS.get(point as usize))
|
||||
{
|
||||
tf.push(("AutoSizingType", ty.to_string()));
|
||||
tf.push(("AutoSizingReferencePoint", point.to_string()));
|
||||
}
|
||||
if major >= 8
|
||||
&& let Some(([use_height, use_width], [height, width])) = fr.minimum_sizes
|
||||
{
|
||||
tf.push(("UseMinimumHeightForAutoSizing", use_height.to_string()));
|
||||
tf.push(("MinimumHeightForAutoSizing", num(height)));
|
||||
tf.push(("UseMinimumWidthForAutoSizing", use_width.to_string()));
|
||||
tf.push(("MinimumWidthForAutoSizing", num(width)));
|
||||
}
|
||||
let mut footnote = Vec::new();
|
||||
if let Some((span, min, between)) = fr.footnotes
|
||||
&& span <= 1
|
||||
@@ -516,6 +553,34 @@ impl Writer<'_> {
|
||||
node.set(&["TextFrameFootnoteOptionsObject"], footnote);
|
||||
}
|
||||
}
|
||||
// The text frame values every object style has from DOM 12 hold
|
||||
// from DOM 8 (idml-values.md, object styles other than the root).
|
||||
if (8..12).contains(&major)
|
||||
&& let Some(t) = values::object_style(12).child("TextFramePreference")
|
||||
{
|
||||
node.merge(&Node {
|
||||
tag: "ObjectStyle".into(),
|
||||
children: vec![Node {
|
||||
tag: "TextFramePreference".into(),
|
||||
attrs: t.attrs.clone(),
|
||||
..Node::default()
|
||||
}],
|
||||
..Node::default()
|
||||
});
|
||||
}
|
||||
// Every object style has it from 13.1 (idml-values.md, object
|
||||
// styles other than the root).
|
||||
if version >= (13, 1) {
|
||||
node.merge(&Node {
|
||||
tag: "ObjectStyle".into(),
|
||||
children: vec![Node {
|
||||
tag: "TextFramePreference".into(),
|
||||
attrs: vec![("FootnotesEnableOverrides".into(), "false".into())],
|
||||
..Node::default()
|
||||
}],
|
||||
..Node::default()
|
||||
});
|
||||
}
|
||||
let mut story = Vec::new();
|
||||
if let Some(st) = &os.story {
|
||||
match st.frame_type {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Transparency settings of page items. See `docs/format/transparency.md`.
|
||||
|
||||
use super::applied::{StyleValues, same_value};
|
||||
use super::kind::Kind;
|
||||
use super::xml::Xml;
|
||||
use super::{Writer, num};
|
||||
@@ -236,13 +237,16 @@ fn stops(raw: &[[f64; 3]]) -> Option<Vec<Stop>> {
|
||||
}
|
||||
|
||||
/// Write the transparency settings found in a page item's attribute list.
|
||||
/// `item` is the item's `Self`, which names the opacity stops. Returns the
|
||||
/// values left out because they are outside the schema's range, as
|
||||
/// (effect, attribute, value).
|
||||
/// IDML writes only the values that differ from the object style `style`
|
||||
/// (all values without a style), and an effect element only when one of
|
||||
/// its values or its opacity stops differ. `item` is the item's `Self`,
|
||||
/// which names the opacity stops. Returns the values left out because
|
||||
/// they are outside the schema's range, as (effect, attribute, value).
|
||||
pub(super) fn write(
|
||||
w: &Writer,
|
||||
x: &mut Xml,
|
||||
attrs: &Attrs,
|
||||
style: Option<&StyleValues>,
|
||||
item: &str,
|
||||
) -> Vec<(&'static str, &'static str, f64)> {
|
||||
let mut left_out = Vec::new();
|
||||
@@ -267,7 +271,16 @@ pub(super) fn write(
|
||||
continue;
|
||||
}
|
||||
match w.value_text(kind, v) {
|
||||
Some(t) => values.push((name, t)),
|
||||
Some(t) => {
|
||||
let same = style.is_some_and(|s| {
|
||||
s.transparency(id)
|
||||
.and_then(|sv| w.value_text(kind, sv))
|
||||
.is_some_and(|st| same_value(&st, &t))
|
||||
});
|
||||
if !same {
|
||||
values.push((name, t));
|
||||
}
|
||||
}
|
||||
None if kind.is_code() => attrs.unknown_code(id, v),
|
||||
None => {}
|
||||
}
|
||||
@@ -276,9 +289,17 @@ pub(super) fn write(
|
||||
STOPS
|
||||
.iter()
|
||||
.find(|(_, s)| *s == setting)
|
||||
.and_then(|(id, _)| match attrs.get(*id) {
|
||||
Some(Value::Stops(raw)) => stops(raw),
|
||||
.and_then(|(id, _)| {
|
||||
match attrs.get(*id) {
|
||||
Some(Value::Stops(raw))
|
||||
if !style.is_some_and(|s| {
|
||||
matches!(s.transparency(*id), Some(Value::Stops(st)) if st == raw)
|
||||
}) =>
|
||||
{
|
||||
stops(raw)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
|
||||
@@ -203,6 +203,7 @@ pub mod chunk {
|
||||
pub const OBJECT_STYLE_FITTING: u32 = 0x1B956;
|
||||
/// Page item attributes of an object style (u16 count, records).
|
||||
pub const OBJECT_STYLE_ATTRS: u32 = 0x1B92B;
|
||||
pub const OBJECT_STYLE_TRANSPARENCY: u32 = 0x1B92C;
|
||||
pub const OBJECT_STYLE_FRAME: u32 = 0x1B924;
|
||||
pub const OBJECT_STYLE_STORY: u32 = 0x285B;
|
||||
pub const OBJECT_STYLE_DIRECTION: u32 = 0x50F28;
|
||||
|
||||
@@ -61,6 +61,8 @@ pub struct ObjectStyle {
|
||||
pub fitting: Attrs,
|
||||
/// Page item attributes (chunk 0x1B92B): fill, stroke, corners.
|
||||
pub attrs: Attrs,
|
||||
/// Transparency attributes (chunk 0x1B92C), the IDs of page items.
|
||||
pub transparency: Attrs,
|
||||
/// Text frame settings (chunk 0x1B924).
|
||||
pub frame: Option<ObjectStyleFrame>,
|
||||
/// Story settings (chunk 0x285B).
|
||||
@@ -321,6 +323,20 @@ impl<'a> Reader<'a> {
|
||||
.unwrap_or_default(),
|
||||
None => Attrs::default(),
|
||||
},
|
||||
transparency: match self.chunk(uid, chunk::OBJECT_STYLE_TRANSPARENCY)? {
|
||||
Some(d) => self
|
||||
.attrs_or_warn(
|
||||
|| format!("object style {uid}, transparency"),
|
||||
Attrs::parse_short(
|
||||
self.enc(),
|
||||
&d,
|
||||
List::ObjectStyleTransparency,
|
||||
self.db.recorder(),
|
||||
),
|
||||
)
|
||||
.unwrap_or_default(),
|
||||
None => Attrs::default(),
|
||||
},
|
||||
// The layout is known for these sizes only
|
||||
// (`docs/format/big-endian.md`).
|
||||
frame: match self.chunk(uid, chunk::OBJECT_STYLE_FRAME)? {
|
||||
@@ -397,11 +413,30 @@ pub struct ObjectStyleFrame {
|
||||
/// Column rule: f64 stroke width at 190, u32 colour at 198 (0 for
|
||||
/// none) and f64 tint at 210.
|
||||
pub column_rule: Option<(f64, u32, f64)>,
|
||||
/// u16 at 76: first baseline offset code, as for frames (the 106-byte
|
||||
/// layout too).
|
||||
pub first_baseline_offset: Option<u16>,
|
||||
/// The fields below are read from the 142-, 162- and 222-byte layouts.
|
||||
/// u16 at 32, 1 = true.
|
||||
pub vertical_balance_columns: Option<bool>,
|
||||
/// u16 at 70, 1 = true.
|
||||
pub use_fixed_width: Option<bool>,
|
||||
/// u16 at 74: vertical justification code, as for frames.
|
||||
pub vertical_justification: Option<u16>,
|
||||
/// u16 at 116 and 118: auto-sizing type and reference point codes, as
|
||||
/// for frames.
|
||||
pub auto_sizing: Option<(u16, u16)>,
|
||||
/// u16 at 120 and f64 at 122 (minimum height), u16 at 130 and f64 at
|
||||
/// 132 (minimum width).
|
||||
pub minimum_sizes: Option<([bool; 2], [f64; 2])>,
|
||||
}
|
||||
|
||||
impl ObjectStyleFrame {
|
||||
pub(super) fn read(enc: Encoding, d: &[u8]) -> ObjectStyleFrame {
|
||||
let f = |o: usize| enc.f64_at(d, o);
|
||||
let long = d.len() >= 142;
|
||||
let flag = |o: usize| enc.u16_at(d, o).filter(|_| long).map(|v| v == 1);
|
||||
let code = |o: usize| enc.u16_at(d, o).filter(|_| long);
|
||||
ObjectStyleFrame {
|
||||
column_fixed_width: f(0),
|
||||
column_gutter: f(8),
|
||||
@@ -409,6 +444,12 @@ impl ObjectStyleFrame {
|
||||
column_count: enc.u32_at(d, 66),
|
||||
footnotes: (|| Some((enc.u16_at(d, 144)?, f(146)?, f(154)?)))(),
|
||||
column_rule: (|| Some((f(190)?, enc.u32_at(d, 198)?, f(210)?)))(),
|
||||
first_baseline_offset: enc.u16_at(d, 76),
|
||||
vertical_balance_columns: flag(32),
|
||||
use_fixed_width: flag(70),
|
||||
vertical_justification: code(74),
|
||||
auto_sizing: (|| Some((code(116)?, code(118)?)))(),
|
||||
minimum_sizes: (|| Some(([flag(120)?, flag(130)?], [f(122)?, f(132)?])))(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user