feat: write vertical story orientation

A text frame's multi-column frame stores a matrix (chunk 0x2DE) that is
the identity for horizontal text and a quarter turn for vertical text.
A story whose frames all have the quarter turn is written Vertical
instead of the Horizontal every sample's IDML has.
This commit is contained in:
aiongg committed 2026-10-07 04:27:07 +00:00
1 parent 6a357f4287
commit 4bc6768fb0
4 files changed
+126 -7

No files matched your search

+15 -1
View File
@@ -118,6 +118,13 @@ alignment is off in every corpus story, so the field that turns it on is
not known and a document that uses it is converted without it.
`AppliedTOCStyle` is left out: it names a TOC style in 4 stories.
`StoryOrientation` is read from the story's frames when they agree
(`objects.md`, text orientation); the observed `Horizontal` is written
only for other stories. For a sample typeset vertically, no field was
found that sets `StoryDirection` or `FrameType` apart from horizontal
samples, so these keep the observed values there too. Its object styles
store direction code 1, as horizontal samples do.
## Graphic settings without an INDD field
All 496 `Image` elements in the 240 corpus IDML files (119 files) have
@@ -241,4 +248,11 @@ For example `TextDefault` keeps 264 of its 317 values,
in a later version is kept only when at least 10 files show it.
Some of these values are probably stored in the INDD and may differ in a
document outside the corpus; none of them is read from the INDD yet.
document outside the corpus; none of them is read from the INDD yet. A
sample typeset vertically shows this: two 2-byte fields of the
preferences object (chunks 0xCD14 and 0xCD1C) are 1 there and 0 in all
250 distinct little-endian public files, and the values most likely to
depend on vertical layout (`StoryPreference` `StoryOrientation`,
`DocumentPreference` and `MarginPreference` `ColumnDirection`) are
written as observed. The print PDF cannot show which preference each
field holds, so they are not mapped.
+25
View File
@@ -799,6 +799,31 @@ From the frame's multi-column frame object (class 0x263):
Inset spacing is 0 in every sample and not located.
**Text orientation (chunk 0x2DE).** The multi-column frame holds a
matrix (six f64) in chunk 0x2DE. Its first four values are 1 0 0 1 for
horizontal text and 0 1 −1 0 (a quarter turn) for vertical text. IDML
has no frame attribute for this; it writes the orientation on the story
(`StoryPreference`, `StoryOrientation`). Evidence:
- All 18,207 multi-column frames in the 239 distinct little-endian
public files that have text frames have 1 0 0 1. All 2,094 stories in
the corpus IDML files have `StoryOrientation="Horizontal"`.
- A sample typeset vertically has 0 1 −1 0 in the frames of some
stories and 1 0 0 1 in the others. In its print PDF, the text of every
frame with the quarter turn is set in vertical columns ordered right to
left (glyphs of a column share an x position and follow each other
downwards), and the text of every frame with 1 0 0 1 is set in
horizontal lines, such as the running heads. The frames of one story
always agree.
- In the rotated frames, `TextColumnFixedWidth` (chunk 0x2D1) equals
the frame's height, not its width: the columns run down the page.
The last two values of the matrix are not identified. The converter
writes `Vertical` for a story whose frames all have the quarter turn,
and otherwise `Horizontal`, the value every IDML has. A story whose
frames disagree or have another matrix gets a warning. Two frames in
the InDesign 3.0 file have no chunk 0x2DE; they do not count.
**First baseline offset.** IDML writes `FirstBaselineOffset` on a frame's
`TextFramePreference` only when it differs from the frame's object style.
Over the frames of the corpus pairs, compared with the value written on
+15 -5
View File
@@ -8,10 +8,10 @@ pub mod zip;
use std::collections::BTreeMap;
use crate::model::{
Attrs, Document, Graphic, GraphicKind, Guide, ItemKind, Matrix, Page, PageItem, Path, Section,
Shape, Spread, Story, Style, StyleGroup, Table, TextFramePreferences, TextRun, TextVariable,
TextWrap, Value, XmlElement, XmlMarker, hyperlink::DestinationKind, numbering, root_kind,
variable::Instance, wrap_mode, xml::Key as XmlKey,
Attrs, Document, Graphic, GraphicKind, Guide, ItemKind, Matrix, Orientation, Page, PageItem,
Path, Section, Shape, Spread, Story, Style, StyleGroup, Table, TextFramePreferences, TextRun,
TextVariable, TextWrap, Value, XmlElement, XmlMarker, hyperlink::DestinationKind, numbering,
root_kind, variable::Instance, wrap_mode, xml::Key as XmlKey,
};
#[derive(Clone, Copy)]
@@ -2976,7 +2976,16 @@ impl Writer<'_> {
("OpticalMarginAlignment", "false".into()),
("OpticalMarginSize", "12".into()),
("FrameType", "TextFrameType".into()),
("StoryOrientation", "Horizontal".into()),
// Read from the story's frames; otherwise the value every
// exported IDML has (`idml-values.md`).
(
"StoryOrientation",
match s.orientation {
Some(Orientation::Vertical) => "Vertical",
_ => "Horizontal",
}
.into(),
),
("StoryDirection", "LeftToRightDirection".into()),
],
);
@@ -3433,6 +3442,7 @@ mod tests {
sources: Vec::new(),
xml_markers: markers.into_iter().collect(),
xml_element: None,
orientation: None,
};
let doc = Document {
xml: XmlStructure {
+71 -1
View File
@@ -120,6 +120,9 @@ pub mod chunk {
pub const BULLETS: u32 = 0x1A488;
pub const FRAME_COLUMNS: u32 = 0x2D1;
pub const FRAME_JUSTIFICATION: u32 = 0x2CE;
/// Matrix of a multi-column frame; its first four values give the
/// text orientation.
pub const FRAME_TEXT_TRANSFORM: u32 = 0x2DE;
pub const GRAPHIC_BOUNDS: u32 = 0x1633;
pub const GRAPHIC_LINK: u32 = 0x8CBC;
pub const LINK_INFO: u32 = 0x8C9B;
@@ -439,6 +442,16 @@ pub struct Story {
pub xml_markers: BTreeMap<usize, XmlMarker>,
/// The XML element whose content is this story.
pub xml_element: Option<xml::Key>,
/// Text orientation, from the frames that show the story; `None` when
/// it has no frame or its frames disagree.
pub orientation: Option<Orientation>,
}
/// Text orientation of a story (IDML `StoryOrientation`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Orientation {
Horizontal,
Vertical,
}
/// What an XML marker character (U+FEFF) in story text stands for.
@@ -648,6 +661,8 @@ pub struct Reader<'a> {
warnings: std::cell::RefCell<Vec<String>>,
/// XML nodes found in the stories read so far.
xml_nodes: std::cell::RefCell<BTreeMap<xml::Key, xml::Node>>,
/// Text orientation of each text frame read so far, by story.
frame_orientations: std::cell::RefCell<BTreeMap<u32, Vec<Option<Orientation>>>>,
}
fn uid_or_none(v: u32) -> Option<u32> {
@@ -661,6 +676,7 @@ impl<'a> Reader<'a> {
cache: Default::default(),
warnings: Default::default(),
xml_nodes: Default::default(),
frame_orientations: Default::default(),
}
}
@@ -1047,6 +1063,10 @@ impl<'a> Reader<'a> {
}
_ => None,
};
let mut stories = stories;
for story in &mut stories {
story.orientation = self.story_orientation(story.uid);
}
let xml = XmlStructure {
story: xml_story,
elements: self.xml_elements(&xml_tag_names)?,
@@ -1128,6 +1148,22 @@ impl<'a> Reader<'a> {
})
}
/// The text orientation shared by all frames of a story. A story
/// whose frames disagree, or have an orientation that is not known,
/// gets none, with a warning.
fn story_orientation(&self, story: u32) -> Option<Orientation> {
let map = self.frame_orientations.borrow();
let frames = map.get(&story)?;
let first = frames[0];
if first.is_none() || frames.iter().any(|&o| o != first) {
self.warn(format!(
"story {story}: text orientation of its frames is not known or differs; written as horizontal"
));
return None;
}
first
}
/// An interface colour (class 0x1F11): chunk 0x1F01 holds u32 space
/// (5 = RGB), u16 count and the components as f64 fractions.
fn ui_color(&self, uid: u32) -> Result<Option<[f64; 3]>, Error> {
@@ -1235,6 +1271,25 @@ impl<'a> Reader<'a> {
}))
}
/// Text orientation of a multi-column frame (chunk 0x2DE): the
/// identity gives horizontal text, the rotation 0 1 −1 0 vertical
/// text (`docs/format/objects.md`). `None` without the chunk;
/// `Some(None)` for another matrix.
fn frame_orientation(&self, mcf: u32) -> Result<Option<Option<Orientation>>, Error> {
let Some(d) = self.chunk(mcf, chunk::FRAME_TEXT_TRANSFORM)? else {
return Ok(None);
};
if d.len() < 48 {
return Ok(Some(None));
}
let Matrix([a, b, c, dd, _, _]) = Matrix::read(&mut Cursor::new(&d))?;
Ok(Some(match [a, b, c, dd] {
[1.0, 0.0, 0.0, 1.0] => Some(Orientation::Horizontal),
[0.0, 1.0, -1.0, 0.0] => Some(Orientation::Vertical),
_ => None,
}))
}
fn text_frame_preferences(&self, mcf: u32) -> Result<Option<TextFramePreferences>, Error> {
let (Some(cols), Some(just)) = (
self.chunk(mcf, chunk::FRAME_COLUMNS)?,
@@ -1586,11 +1641,13 @@ impl<'a> Reader<'a> {
let mut graphics = Vec::new();
let mut text_column = None;
let mut frame_prefs = None;
let mut frame_mcf = None;
for &child in &child_uids {
if let Some(g) = self.graphic(child)? {
graphics.push(g);
} else if self.class(child) == Some(class::MULTI_COLUMN_FRAME) {
frame_prefs = self.text_frame_preferences(child)?;
frame_mcf = Some(child);
text_column = self
.children(child, chunk::ITEM_HIERARCHY)?
.into_iter()
@@ -1612,8 +1669,20 @@ impl<'a> Reader<'a> {
ItemKind::Group
} else if let Some(column) = text_column {
let mut kind = self.text_frame_links(column)?;
if let ItemKind::TextFrame { preferences, .. } = &mut kind {
if let ItemKind::TextFrame {
preferences, story, ..
} = &mut kind
{
*preferences = frame_prefs;
if let (Some(story), Some(mcf)) = (*story, frame_mcf)
&& let Some(o) = self.frame_orientation(mcf)?
{
self.frame_orientations
.borrow_mut()
.entry(story)
.or_default()
.push(o);
}
}
kind
} else {
@@ -2059,6 +2128,7 @@ impl<'a> Reader<'a> {
sources,
xml_markers,
xml_element,
orientation: None,
})
}