feat: read the start of the print and booklet print settings

This commit is contained in:
aiongg committed 2026-10-07 18:07:03 +00:00
1 parent 59e7f6d168
commit 8d71e09a6b
3 files changed
+288

No files matched your search

+37
View File
@@ -224,3 +224,40 @@ analysis script found the list; over all pairs the converter reproduces
`StrokeWeight`, `CornerRadius` and `MiterLimit` of every file, and the
frame fitting attributes of the list are written to the preference
`FrameFittingOption`.
## Print settings (`PrintPreference`, `PrintBookletPrintPreference`)
Chunk 0xA4C holds the print settings and chunk 0xAF2 those of booklet
printing, in the same layout. Its start, in order (a "string" is a flag
byte, 1 for a built-in key, then an in-object string):
| Field | IDML |
|---|---|
| u8 1 if a print record follows, u8, u32 length *n*, *n* bytes | `PrintRecord`: `$ID/` and the bytes in base64, 76 characters per line, a line feed after every full line |
| 6 bytes with a print record, 2 without | |
| String | `ActivePrinterPreset`: built-in `kPrSt_DefaultName` is `Default`, built-in empty `Custom`, otherwise the name (string) |
| u32 | `PrintTo`; `PrintToDisk` is `true` when it is 2 |
| String | `Printer`: built-in `kPrepress File` is `PostscriptFile`, otherwise the name |
| String | not identified (empty in every file) |
| String | `PPD`: built-in `kDevice Independent` is `DeviceIndependent`, built-in empty the string `$ID/`, otherwise the name |
| String | `PPDFile` (`$ID/` and the text for a built-in string) |
| u32 | `PostScriptLevel`: 2 `Level2`, 3 `Level3` |
| f64 | `PrintResolution` |
| 4 f64 | `PaperSizeRect`: left, top, right, bottom |
| 4 f64 | `ImageablePaperSizeRect`: left, top, right, bottom |
| i32, then a string | `PaperSize`: −3 the string, −2 `DefinedByDriver`, −1 `Custom` |
After that string, at these offsets from its end: f64 −1 at 0 and 24
(`PaperWidth` and `PaperHeight` are `Auto` in every file), f64 pairs at 8
and 32 (`PaperWidthRange`, `PaperHeightRange`), u16 at 84
(`PrintPageOrientation`: 0 `Portrait`, 1 `Landscape`), u32 at 104
(`Copies`), u8 at 118 (`PrintBlankPages`). The rest of the chunk (marks,
bleeds, screening, colour output, scaling and the paper size selector) is
not decoded.
Evidence: every field above matches in all 654 pairs for both chunks,
except `PPD` (653, one built-in key written as a string in IDML),
`Printer` of booklets (653) and `PPDFile` of booklets (652). The paper rectangles are written as IDML
writes them: with the digits of the shortest form that reads back, the
last rounded half to even from the exact value (583.2000122070312, not
…313).
+41
View File
@@ -7,6 +7,7 @@ pub mod zip;
use std::collections::BTreeMap;
use crate::model::prefs::PrefProp;
use crate::model::{
Attrs, Document, Graphic, GraphicKind, Guide, ItemKind, Matrix, Orientation, Page, PageItem,
Path, Section, Shape, Spread, Story, Style, StyleGroup, Table, TextFramePreferences, TextRun,
@@ -207,6 +208,8 @@ const PREFERENCE_TAGS: &[&str] = &[
"TextPreference",
"PasteboardPreference",
"XMLPreference",
"PrintPreference",
"PrintBookletPrintPreference",
];
/// A `Properties` child as a values node.
@@ -2803,6 +2806,44 @@ impl Writer<'_> {
.extend(fitting.into_iter().map(|(k, v)| (k.to_string(), v)));
}
}
for (tag, data) in &prefs.print_records {
let mut text = base64_lines(data);
// IDML ends a last line of full length with a line feed.
if text.rsplit('\n').next().is_some_and(|l| l.len() == 76) {
text.push('\n');
}
let i = ours_of(&mut ours, tag);
ours[i]
.attrs
.push(("PrintRecord".into(), format!("$ID/{text}")));
}
for (tag, name, value) in &prefs.props {
let mut n = Node {
tag: name.to_string(),
..Node::default()
};
match value {
PrefProp::Text(ty, text) => {
n.attrs.push(("type".into(), ty.to_string()));
n.text = Some(text.clone());
}
PrefProp::Attrs(a) => {
n.attrs = a.iter().map(|(k, v)| (k.to_string(), v.clone())).collect();
}
}
let i = ours_of(&mut ours, tag);
match ours[i].children.iter_mut().find(|c| c.tag == "Properties") {
Some(p) => p.children.push(n),
None => ours[i].children.insert(
0,
Node {
tag: "Properties".into(),
children: vec![n],
..Node::default()
},
),
}
}
for &(tag, name, rgb) in &prefs.colors {
if !PREFERENCE_TAGS.contains(&tag) {
continue;
+210
View File
@@ -31,6 +31,130 @@ pub struct Prefs {
pub anchor: Option<Vec<u8>>,
/// Page item defaults (class 0x6E07), a page item attribute list.
pub item_defaults: Option<Attrs>,
/// `Properties` children: element, name, value.
pub props: Vec<(&'static str, &'static str, PrefProp)>,
/// Print records (element, bytes), written in base64 as `PrintRecord`.
pub print_records: Vec<(&'static str, Vec<u8>)>,
}
/// The value of a `Properties` child of a preference element.
#[derive(Debug, Clone, PartialEq)]
pub enum PrefProp {
/// Text of the given IDML type (`string`, `enumeration`).
Text(&'static str, String),
/// An empty element with these attributes.
Attrs(Vec<(&'static str, String)>),
}
/// A flagged in-object string: a flag byte (1 for a built-in key) and the
/// string.
fn flagged(c: &mut Cursor) -> Result<(u8, String), Error> {
let flag = c.u8()?;
Ok((flag, c.string()?))
}
/// Print settings (chunk 0xA4C, and 0xAF2 for booklets): see
/// `docs/format/preferences.md`, print preferences.
fn print_prefs(
d: &[u8],
element: &'static str,
values: &mut Vec<PrefValue>,
props: &mut Vec<(&'static str, &'static str, PrefProp)>,
records: &mut Vec<(&'static str, Vec<u8>)>,
) -> Result<(), Error> {
let mut set = |name: &'static str, value: String| {
values.push(PrefValue {
element,
name,
value,
})
};
let mut c = Cursor::new(d);
let has_record = c.u8()? != 0;
c.u8()?;
let n = c.u32()? as usize;
records.push((element, c.bytes(n)?.to_vec()));
c.skip(if has_record { 6 } else { 2 })?;
let preset = flagged(&mut c)?;
let to = c.u32()?;
let printer = flagged(&mut c)?;
flagged(&mut c)?;
let ppd = flagged(&mut c)?;
let ppd_file = flagged(&mut c)?;
let mut prop = |name: &'static str, ty: &'static str, text: String| {
props.push((element, name, PrefProp::Text(ty, text)))
};
match (preset.0, preset.1.as_str()) {
(1, "kPrSt_DefaultName") => prop("ActivePrinterPreset", "enumeration", "Default".into()),
(1, "") => prop("ActivePrinterPreset", "enumeration", "Custom".into()),
(1, _) => {}
(_, name) => prop("ActivePrinterPreset", "string", name.into()),
}
match (printer.0, printer.1.as_str()) {
(1, "kPrepress File") => prop("Printer", "enumeration", "PostscriptFile".into()),
(1, _) => {}
(_, name) => prop("Printer", "string", name.into()),
}
match (ppd.0, ppd.1.as_str()) {
(1, "kDevice Independent") => prop("PPD", "enumeration", "DeviceIndependent".into()),
(1, "") => prop("PPD", "string", "$ID/".into()),
(1, _) => {}
(_, name) => prop("PPD", "string", name.into()),
}
set("PrintTo", to.to_string());
set("PrintToDisk", (to == 2).to_string());
let file = if ppd_file.0 == 1 {
format!("$ID/{}", ppd_file.1)
} else {
ppd_file.1
};
set("PPDFile", file);
match c.u32()? {
2 => set("PostScriptLevel", "Level2".into()),
3 => set("PostScriptLevel", "Level3".into()),
_ => {}
}
set("PrintResolution", num(c.f64()?));
let mut rect = |name: &'static str, c: &mut Cursor| -> Result<(), Error> {
let mut a = Vec::new();
for side in ["Left", "Top", "Right", "Bottom"] {
a.push((side, num_half_even(c.f64()?)));
}
props.push((element, name, PrefProp::Attrs(a)));
Ok(())
};
rect("PaperSizeRect", &mut c)?;
rect("ImageablePaperSizeRect", &mut c)?;
let size = c.i32()?;
let name = flagged(&mut c)?;
let paper = match size {
-3 => Some(("string", name.1)),
-2 => Some(("enumeration", "DefinedByDriver".to_string())),
-1 => Some(("enumeration", "Custom".to_string())),
_ => None,
};
if let Some((ty, text)) = paper {
props.push((element, "PaperSize", PrefProp::Text(ty, text)));
}
let q = c.pos();
let f = |o: usize| Cursor::new(&d[q + o..]).f64();
set("PaperWidthRange", format!("{} {}", num(f(8)?), num(f(16)?)));
set(
"PaperHeightRange",
format!("{} {}", num(f(32)?), num(f(40)?)),
);
match Cursor::new(&d[q + 84..]).u16()? {
0 => set("PrintPageOrientation", "Portrait".into()),
1 => set("PrintPageOrientation", "Landscape".into()),
_ => {}
}
set("Copies", Cursor::new(&d[q + 104..]).u32()?.to_string());
match d.get(q + 118) {
Some(0) => set("PrintBlankPages", "false".into()),
Some(1) => set("PrintBlankPages", "true".into()),
_ => {}
}
Ok(())
}
/// Chunks of the preferences object.
@@ -53,6 +177,8 @@ mod id {
pub const MARGINS: u32 = 0x550;
pub const COLUMNS: u32 = 0x555;
pub const ANCHOR: u32 = 0x2800;
pub const PRINT: u32 = 0xA4C;
pub const PRINT_BOOKLET: u32 = 0xAF2;
pub const PASTEBOARD: u32 = 0x5D2;
pub const XML_TAGS: u32 = 0xBF4F;
pub const GRIDS_IN_BACK: u32 = 0x567;
@@ -93,6 +219,51 @@ fn num(v: f64) -> String {
crate::idml::num(v)
}
/// A number as IDML writes the paper rectangles of the print settings:
/// the digits of the shortest form that reads back, the last one rounded
/// half to even from the exact value (583.2000122070312, where the
/// shortest form is …313).
fn num_half_even(v: f64) -> String {
if v == 0.0 || !v.is_finite() {
return num(v);
}
// As many digits as the shortest form that reads back, rounded half
// to even from the exact value.
let shortest = format!("{v:e}");
let n = shortest
.split('e')
.next()
.unwrap_or("")
.chars()
.filter(|c| c.is_ascii_digit())
.count()
.max(1);
let e = format!("{v:.*e}", n - 1);
let (mant, exp) = e.split_once('e').unwrap_or((&e, "0"));
let exp: i32 = exp.parse().unwrap_or(0);
let neg = mant.starts_with('-');
let digits: String = mant.chars().filter(|c| c.is_ascii_digit()).collect();
let point = exp + 1;
let mut out = if point <= 0 {
format!("0.{}{}", "0".repeat((-point) as usize), digits)
} else if point as usize >= digits.len() {
format!("{}{}", digits, "0".repeat(point as usize - digits.len()))
} else {
format!(
"{}.{}",
&digits[..point as usize],
&digits[point as usize..]
)
};
if out.contains('.') {
out = out.trim_end_matches('0').trim_end_matches('.').to_string();
}
if neg {
out.insert(0, '-');
}
out
}
impl Reader<'_> {
pub(super) fn prefs(&self, major: u32) -> Result<Prefs, Error> {
let mut values = Vec::new();
@@ -424,6 +595,27 @@ impl Reader<'_> {
None => None,
};
// Print settings.
let mut props = Vec::new();
let mut print_records = Vec::new();
for (id, element) in [
(id::PRINT, "PrintPreference"),
(id::PRINT_BOOKLET, "PrintBookletPrintPreference"),
] {
if let Some(d) = get(id)? {
let (mut v, mut p, mut r) = (Vec::new(), Vec::new(), Vec::new());
// A layout the corpus does not show leaves the element as
// observed.
if print_prefs(&d, element, &mut v, &mut p, &mut r).is_ok() {
for pv in v {
set(pv.element, pv.name, pv.value);
}
props.extend(p);
print_records.extend(r);
}
}
}
// Grids.
if let Some(d) = get(id::BASELINE_GRID)?.filter(|d| d.len() >= 26) {
let f = |o: usize| Cursor::new(&d[o..]).f64();
@@ -485,6 +677,24 @@ impl Reader<'_> {
text_defaults,
anchor,
item_defaults,
props,
print_records,
})
}
}
#[cfg(test)]
mod tests {
use super::num_half_even;
#[test]
fn rounds_shortest_digits_half_even() {
assert_eq!(num_half_even(f64::from(583.2_f32)), "583.2000122070312");
assert_eq!(num_half_even(f64::from(841.92_f32)), "841.9199829101562");
assert_eq!(num_half_even(612.0), "612");
assert_eq!(num_half_even(0.0), "0");
assert_eq!(num_half_even(-12.5), "-12.5");
assert_eq!(num_half_even(0.0625), "0.0625");
assert_eq!(num_half_even(f64::from(16.6_f32)), "16.600000381469727");
}
}