feat: write the custom text wrap path

A wrap path object (class 0x3702) flagged custom in chunk 0x3709 has its
0x162B paths written as TextWrapPreference/Properties/PathGeometry, before
TextWrapOffset.
This commit is contained in:
aiongg committed 2026-10-11 01:25:42 +00:00
1 parent bf7c2c4081
commit dd2b3f07f3
4 files changed
+130 -8

No files matched your search

+40 -2
View File
@@ -1205,8 +1205,46 @@ chunk 0x3703 (44 bytes):
| 40 | u16 master page flag: 1 `ApplyToMasterPageOnly="false"`, 0 `true` |
| 42 | u16 `TextWrapSide`: 0 `BothSides`, 1 `LeftSide`, 2 `RightSide`, 3 `SideTowardsSpine`, 4 `SideAwayFromSpine`, 5 `LargestArea` |
The wrap path object holds the contour as path geometry (chunk 0x162B,
as for page items) and names the item in chunk 0x3709.
**Wrap path.** Every item with wrap mode 1, 2, 3 or 6 (and the codes
0x10003 and 0x10006) has a wrap path object; mode 0 items have none
(9,061 objects in 735 files of the corpus of 2026-10). The u32 at 4
equals the object's UID in 8,824 of the 8,824 objects whose owner was
found. Its chunks:
| Chunk | Contents |
|---|---|
| 0x162B | Path geometry, the layout of page items. Parses in 8,846 of the 8,846 little-endian objects that have it; 18 have none |
| 0x3709 | 10 bytes: u32 UID of the item (9,061 of 9,061), u32 0 (9,061 of 9,061), u16 custom path flag |
| 0x2C0B, 0x2C2D, 0x377A | On 4,813 of 9,061; not read |
IDML writes the path as `TextWrapPreference/Properties/PathGeometry`,
before `TextWrapOffset`, only when the custom path flag is 1. Over the
2,342 IDML wrap elements that have a wrap path object (1,412 in
trustworthy pairs):
| u16 at 8 of 0x3709 | IDML `PathGeometry` | All pairs | Trustworthy |
|---|---|---|---|
| 1 | present | 35 of 35 | 32 of 32 |
| 0 | absent | 2,307 of 2,307 | 1,380 of 1,380 |
The 35 are 28 polygons (22 `BoundingBoxTextWrap`, 6 `Contour`), one
rectangle, one group, one text frame (`JumpObjectTextWrap`) and 4 images
(`Contour`). In all 35 the IDML paths equal the 0x162B paths: number of
paths (1 to 3), point count, `Anchor`, `LeftDirection` and
`RightDirection` from the point's anchor, left and right, and `PathOpen`
from the u16 after the points (0 `false`). The numbers are the same
(tolerance 1e-6), so no transform applies: the path is in the item's
inner coordinates, those of its own `PathGeometry` or, for a placed
graphic, of its `GraphicBounds` (before the graphic's `ItemTransform`).
It already includes the wrap offsets: the path of an image with a 8.5 pt
top offset and a scale of 0.40885 extends 20.79 (8.5 / 0.40885) beyond
the graphic bounds on each side.
With flag 0 the object still stores a path: for frames and groups the
item's path bounding box grown by the offsets (1,538 of 1,538 cases
compared; in about 500 more the box is also grown by 0.25 to 1.5 pt on
each side), for contour wraps of images the computed contour. IDML omits
these, and the converter writes the path only for flag 1.
Placed graphics also have chunk 0x373D, the contour (IDML
`ContourOption`). See "Contour" below.
+46 -4
View File
@@ -264,7 +264,14 @@ impl Writer<'_> {
if paths.is_empty() {
return;
}
x.start("Properties").start("PathGeometry");
x.start("Properties");
Self::path_geometry_element(x, paths);
x.end();
}
/// The `PathGeometry` element of `paths`, without `Properties`.
pub(super) fn path_geometry_element(x: &mut Xml, paths: &[Path]) {
x.start("PathGeometry");
for p in paths {
x.start("GeometryPathType")
.attr("PathOpen", p.open.to_string());
@@ -281,7 +288,7 @@ impl Writer<'_> {
}
x.end().end();
}
x.end().end();
x.end();
}
/// `TextFramePreference` of a text frame. IDML writes an attribute
@@ -562,8 +569,16 @@ impl Writer<'_> {
}
}
x.attr("TextWrapMode", mode);
x.start("Properties")
.empty("TextWrapOffset", &text_wrap_offsets(wrap));
x.start("Properties");
// IDML has the wrap path only when it is custom (objects.md, text
// wrap); the schema puts it before `TextWrapOffset`.
if let Some(w) = wrap
&& w.mode != wrap_mode::NONE
&& !w.custom_path.is_empty()
{
Self::path_geometry_element(x, &w.custom_path);
}
x.empty("TextWrapOffset", &text_wrap_offsets(wrap));
x.end();
if let Some(c) = contour
&& let Some(t) = contour_type(c.kind)
@@ -1702,6 +1717,7 @@ mod tests {
use super::*;
use crate::Encoding;
use crate::header::{ByteOrder, Version};
use crate::model::PathPoint;
#[test]
fn stroke_categories_turn_off_their_own_attributes() {
@@ -1748,6 +1764,8 @@ mod tests {
offsets: [1.0, 2.0, 3.0, 4.0],
master_flag: 0,
side: 5,
path_uid: 0,
custom_path: Vec::new(),
};
let mut x = Xml::new();
Writer::text_wrap_preference(&mut x, Some(&wrap), None);
@@ -1756,6 +1774,30 @@ mod tests {
assert!(out.contains("ApplyToMasterPageOnly=\"true\" TextWrapSide=\"LargestArea\""));
assert!(out.contains("<TextWrapOffset Top=\"2\" Left=\"1\" Bottom=\"4\" Right=\"3\" />"));
assert!(!out.contains("ContourOption"));
assert!(!out.contains("PathGeometry"));
// A custom wrap path comes first in `Properties`.
let pt = |a| PathPoint {
anchor: a,
left: a,
right: a,
kind: 2,
};
let path = Path {
points: vec![pt((0.0, 0.0)), pt((1.5, 0.0)), pt((1.5, 2.0))],
open: false,
};
let custom = TextWrap {
custom_path: vec![path],
..wrap
};
let mut x = Xml::new();
Writer::text_wrap_preference(&mut x, Some(&custom), None);
let out = x.finish();
let at = out.find("<PathGeometry>").unwrap();
assert!(out.find("<Properties>").unwrap() < at);
assert!(out[at..].contains("<PathPointType Anchor=\"1.5 2\""));
assert!(out.find("</PathGeometry>").unwrap() < out.find("<TextWrapOffset").unwrap());
}
#[test]
+4
View File
@@ -92,6 +92,8 @@ pub mod class {
/// The page item attribute list that items take the attributes
/// they do not store from (chunk 0x6E03).
pub const ITEM_BASE: u32 = 0x6E02;
/// The wrap path of an item's text wrap (chunk 0x3703 at offset 4).
pub const TEXT_WRAP_PATH: u32 = 0x3702;
}
/// Chunk IDs.
@@ -294,6 +296,8 @@ pub mod chunk {
pub const PDF_DATA: u32 = 0x2521;
pub const SWATCH_NAME: u32 = 0x1F30;
pub const TEXT_WRAP: u32 = 0x3703;
/// Of a wrap path: u32 owner item, u32 0, u16 custom path flag.
pub const TEXT_WRAP_PATH_INFO: u32 = 0x3709;
pub const CONTOUR_OPTION: u32 = 0x373D;
pub const CLIPPING_PATH: u32 = 0x2C1A;
/// Image properties: record list (pixels, colour space, resolution).
+40 -2
View File
@@ -273,6 +273,12 @@ pub struct TextWrap {
/// The u16 at offset 42: the `TextWrapSide` code (`TEXT_WRAP_SIDES`
/// of the writer).
pub side: u16,
/// The u32 at offset 4: the wrap path object (class 0x3702), 0 for
/// none.
pub path_uid: u32,
/// The wrap path when its object is flagged custom; empty otherwise
/// and for object styles.
pub custom_path: Vec<Path>,
}
/// A list of keys with string values (chunk 0x1E22B): u32 count; per
@@ -856,7 +862,37 @@ impl<'a> Reader<'a> {
/// u32 contour path object, four f64 offsets, u16 master page flag,
/// u16 side.
pub(super) fn text_wrap(&self, uid: u32) -> Result<Option<TextWrap>, Error> {
self.wrap_chunk(uid, chunk::TEXT_WRAP)
let mut wrap = self.wrap_chunk(uid, chunk::TEXT_WRAP)?;
if let Some(w) = &mut wrap {
w.custom_path = self.custom_wrap_path(uid, w.path_uid)?;
}
Ok(wrap)
}
/// The paths (chunk 0x162B) of wrap path object `path` when chunk
/// 0x3709 flags it custom (u16 at offset 8 = 1); see
/// `docs/format/objects.md`, text wrap. A path that cannot be read is
/// left out with a warning.
fn custom_wrap_path(&self, uid: u32, path: u32) -> Result<Vec<Path>, Error> {
if path == 0 || self.class(path) != Some(class::TEXT_WRAP_PATH) {
return Ok(Vec::new());
}
let Some(d) = self.chunk(path, chunk::TEXT_WRAP_PATH_INFO)? else {
return Ok(Vec::new());
};
if d.len() != 10 || self.cursor(&d[8..]).u16()? != 1 {
return Ok(Vec::new());
}
match self.paths(path) {
Ok(mut paths) => {
paths.retain(|p| !p.points.is_empty());
Ok(paths)
}
Err(e) => {
self.warn(format!("item {uid}: text wrap path not read ({e})"));
Ok(Vec::new())
}
}
}
/// A text wrap record in chunk `id` of object `uid`.
@@ -869,7 +905,7 @@ impl<'a> Reader<'a> {
}
let mut c = self.cursor(&d);
let mode = c.u32()?;
c.skip(4)?;
let path_uid = c.u32()?;
let offsets = [c.f64()?, c.f64()?, c.f64()?, c.f64()?];
let master_flag = c.u16()?;
let side = c.u16()?;
@@ -886,6 +922,8 @@ impl<'a> Reader<'a> {
offsets,
master_flag,
side,
path_uid,
custom_path: Vec::new(),
}))
}