diff --git a/docs/format/attributes.md b/docs/format/attributes.md new file mode 100644 index 0000000..dbf3df4 --- /dev/null +++ b/docs/format/attributes.md @@ -0,0 +1,42 @@ +# Attribute lists + +Formatting is stored as attribute lists (on page items: chunk 0x6E03). +Implemented in `src/model/attrs.rs`. + +## Layout + +u32 count, then for each attribute: + +| Size | Field | +|---|---| +| 4 | Attribute ID | +| 2 | Payload size *n* | +| *n* | Payload: u16 value count, then values | + +Each value: u32 type, u16 length, data. The first value is the attribute's +value. Every attribute in the samples has a second value of type 0x6E63 +with 6 bytes, meaning unknown. + +| Type | Length | Value | +|---|---|---| +| 0x6E68 | 8 | f64 | +| 0x6E67 | 4 | i32 | +| 0x6E65 | 2 | u16 (enumeration or boolean) | +| 0x6E69 | 16 | two f64 (a point) | +| 0x117 | 4 | UID reference | + +All 2,730 page item chunks of this layout in the samples parse exactly. + +## Page item attributes + +The list holds the item's local values. IDML writes an attribute only when +it differs from the applied object style, so a value can be in the INDD +list and absent from the IDML. + +| ID | IDML attribute | Evidence: IDML value equals INDD value | +|---|---|---| +| 0x6E68 | `FillColor` (swatch UID) | 1,190 of 1,194 | +| 0x6E69 | `FillTint` | 10 of 10 | +| 0x6E64 | `StrokeColor` (swatch UID) | 78 of 82 | +| 0x6E65 | `StrokeWeight` | 171 of 171 | +| 0x6E6D | `MiterLimit` | 778 of 778 | diff --git a/docs/format/objects.md b/docs/format/objects.md new file mode 100644 index 0000000..6beabf8 --- /dev/null +++ b/docs/format/objects.md @@ -0,0 +1,165 @@ +# INDD objects + +How the byte stream of a database object (see `database.md`) is +structured, and what the classes and chunks used by the converter mean. +Implemented in `src/object.rs` and `src/model/`. + +## How this was learned + +IDML exported by InDesign names most objects `Self="u"`, and that hex +number is the object's UID in the INDD file. So each corpus pair gives, for +every such IDML element, the INDD object, its class, and the attribute +values its chunks must encode. Facts below were found by comparing the two, +and are checked by `tools/compare.py`, which converts every pair (75 unique +pairs, InDesign 12–21) and compares the output with the reference IDML +attribute by attribute. + +## Chunks + +Most objects (99.9 % of 773,753 in the little-endian corpus) are a sequence +of chunks: + +| Size | Field | +|---|---| +| 4 | Chunk ID | +| 4 | Data length *n* | +| *n* | Data | + +The rest are plain byte streams: embedded image files (TIFF, PNG, SVG) and +the XMP packet (UID 0x80000001). Chunk IDs are in the same number space as +class IDs; a chunk often lists objects of the class with the same number +(for example chunk 0x501 of the document lists spreads, class 0x501). + +## Primitive encodings + +All little-endian. + +- **UID lists:** u32 count, then that many u32 UIDs. +- **Matrices:** six f64, in IDML `ItemTransform` order (a b c d tx ty). +- **Text segments:** a u16 header, flags in bits 14–15 and a count in bits + 0–13. 0x4000: *count* single-byte characters follow. 0x8000: *count* + UTF-16LE code units follow. A text of *n* code units is a sequence of + segments adding up to *n*. Example: "WOMEN’S\r" is + `05 40 "WOMEN" 01 80 19 20 02 40 "S\r"`. +- **In-object strings:** u8 2, u8 (usually 0, meaning unknown), u16 length + in code units, then segments. 4,135 occurrences in three sample files. + +## Document (class 0xE01, always UID 1) + +| Chunk | Contents | +|---|---| +| 0x501 | UID list: spreads, in order | +| 0x1401 | UID list: master spreads | +| 0x301 | UID list: layers (the first is an internal layer that holds pages) | +| 0x313 | u32: active layer (IDML `ActiveLayer`) | +| 0x222 | UID lists: stories (IDML `StoryList`) | +| 0x4C01 | UID list: sections | + +## Spreads (0x501) and master spreads (0x1401) + +| Chunk | Contents | +|---|---| +| 0x503 | u32 self, u32 0, UID list of spread layers | +| 0x56E | Matrix: `ItemTransform` | +| 0x1B8 | u32: `BindingLocation` | + +**Spread layers (0x301)** hold the items of one document layer on one +spread. Chunk 0x302: u32 document layer UID, u16 1 for the layer's guide +part. Chunk 0x303: u32 spread, u32 spread, UID list of children. Pages are +children of the spread layer for the internal pages layer. + +## Layers (0x302) + +Chunk 0x304: u16 locked, u16 visible, then fields not yet identified, then +the name as an in-object string. The internal layer is named +`Internal_pages_layer_name` and has no IDML element. + +## Pages (0x50F) + +| Chunk | Contents | +|---|---| +| 0x140F | u32 applied master spread, u16 (unknown), matrix `MasterPageTransform` | +| 0x5CC | Matrix: `ItemTransform` | +| 0x5DD | Four f64: left, top, right, bottom. IDML `GeometricBounds` is top, left, bottom, right. | +| 0x51A | Four f64: margins (36 in the blank document) | + +## Page items + +Frames, shapes and lines are all class 0x6201; groups are 0x401. + +| Chunk | Contents | +|---|---| +| 0x151 | Matrix: `ItemTransform` | +| 0x162B | Path geometry (below) | +| 0x15B | Hierarchy: u32 spread, u32 parent, UID list of children | +| 0x6E03 | Attribute list: local formatting (see `attributes.md`) | +| 0x1B916 | u32: applied object style | + +**Path geometry:** u32 path count; per path: u32 point count, points, u16 +1 if the path is open. Each point is a u32 type, then f64 values: type 2 = +anchor (x, y) only; types 0 and 1 = left direction, anchor, right +direction. All 3,191 path chunks in the samples parse exactly, and the +points of every page item path match the IDML `PathPointType` values. + +**IDML element type** is not stored. A frame is a `TextFrame` if a child of +class 0x263 exists. Otherwise the converter uses the path: two points and +open → `GraphicLine`; four corner points on two distinct x and y values → +`Rectangle`; four smooth points → `Oval`; anything else → `Polygon`. In the +samples this gives 123/123 lines, 671/671 rectangles, 10/10 ovals and +396/402 polygons (6 polygons are ellipse-like). + +**Text frames:** the frame's child of class 0x263 (multi-column frame) has +a child of class 0x227 (frame column). Chunk 0x220 of the column holds the +frame list (class 0x228). Its chunk 0x205: u32 story, then a UID list of +the columns of all threaded frames in order, which gives +`PreviousTextFrame` and `NextTextFrame`. + +## Stories (0x201) + +Chunk 0x223: u32 length, u16, u32 first strand, UID list of more strands, +then other fields. Each strand has chunk 0x261: u16 count, then (u32 +length, u32 data object UID) pairs. Each data object has chunk 0x262: + +| Field | Contents | +|---|---| +| u32 | Kind: 0x202 text, 0x203 character style runs, 0x204 paragraph style runs | +| u32 | Owning strand | +| u16 | Run count | +| runs | Each: u32 record size, then the record | +| u32 | Total length | + +Each record starts with a u32 run length in UTF-16 code units. Text +records continue with text segments. Style records continue with the +style UID. + +INDD stores a forced line break as U+000A; IDML writes it as U+2028. The +last paragraph return of a story is not written to IDML. + +## Styles (0x205) + +Paragraph and character styles share the class. Chunk 0x230: u32 next +style (0 = itself), u32 based-on style, fields not yet identified, u32 1 +for paragraph styles or 0 for character styles, u8 1 if the name is a +built-in key (`$ID/` in IDML), the name as an in-object string, then a GUID +string in newer files. The name's offset varies (23–26 bytes), so the +converter locates it as a flag byte followed by a valid in-object string. +All 486 style names and 291 `NextStyle` values in the pairs match. + +IDML writes a `BasedOn` of the root `[No paragraph style]` or +`[No character style]` as a string (`$ID/[No paragraph style]`), and any +other base as an object reference. + +## Colours (0x1F05) + +| Chunk | Contents | +|---|---| +| 0x1F10 | u8 1 if the name is a built-in key; name; u32 flags: bit 0 removable, bit 1 visible, bit 2 editable | +| 0x1F01 | u32 space (5 RGB, 6 CMYK), u16 count, f64 components as fractions | +| 0x1F09 | u32 model: 0 Process, 2 Registration | +| 0x1F24 | f64 (−1), then u32 `ColorOverride`: 0 Normal, 1 Specialpaper, 2 Specialblack, 3 Specialregistration, 4 Hiddenreserved | + +Unnamed colours are referenced by UID (`Color/u93`). All 1,296 colours in +the pairs match on `Model`, `Space`, `ColorValue`, `ColorOverride`, +`Name` and the three flags. + +The `None` swatch is class 0x6E0B (name in chunk 0x1F30). diff --git a/src/idml/mod.rs b/src/idml/mod.rs index a8925c6..59ad85c 100644 --- a/src/idml/mod.rs +++ b/src/idml/mod.rs @@ -5,7 +5,25 @@ pub mod zip; use std::collections::BTreeMap; -use crate::model::{Document, ItemKind, Matrix, PageItem, Path, Shape, Spread, Story, Style}; +use crate::model::{ + Attrs, Document, ItemKind, Matrix, PageItem, Path, Shape, Spread, Story, Style, +}; + +#[derive(Clone, Copy)] +enum AttrKind { + Number, + Swatch, +} + +/// Page item attributes: attribute-list ID, IDML name, value kind. +/// See `docs/format/attributes.md` for the evidence behind each entry. +const ITEM_ATTRS: &[(u32, &str, AttrKind)] = &[ + (0x6E68, "FillColor", AttrKind::Swatch), + (0x6E69, "FillTint", AttrKind::Number), + (0x6E64, "StrokeColor", AttrKind::Swatch), + (0x6E65, "StrokeWeight", AttrKind::Number), + (0x6E6D, "MiterLimit", AttrKind::Number), +]; use xml::Xml; const PACKAGING_NS: &str = "http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging"; @@ -139,30 +157,23 @@ impl Writer<'_> { fn graphic(&self) -> String { let mut x = Xml::new(); self.package_root(&mut x, "Graphic"); - let color = |x: &mut Xml, name: &str, model: &str, value: &str, editable: bool| { + for c in &self.doc.colors { + let name = c.idml_name(); x.empty( "Color", &[ - ("Self", format!("Color/{name}")), - ("Model", model.into()), - ("Space", "CMYK".into()), - ("ColorValue", value.into()), - ("Name", name.into()), - ("ColorEditable", editable.to_string()), - ("ColorRemovable", "false".into()), - ("Visible", "true".into()), + ("Self", c.reference()), + ("Model", c.model_name().into()), + ("Space", c.space_name().into()), + ("ColorValue", nums(&c.idml_values())), + ("ColorOverride", c.override_name().into()), + ("Name", name), + ("ColorEditable", c.editable.to_string()), + ("ColorRemovable", c.removable.to_string()), + ("Visible", c.visible.to_string()), ], ); - }; - color(&mut x, "Black", "Process", "0 0 0 100", false); - color(&mut x, "Paper", "Process", "0 0 0 0", true); - color( - &mut x, - "Registration", - "Registration", - "100 100 100 100", - false, - ); + } x.empty( "Swatch", &[ @@ -184,6 +195,20 @@ impl Writer<'_> { x.finish() } + /// Write page item attributes from the item's attribute list. + fn item_attrs(&self, x: &mut Xml, attrs: &Attrs) { + for &(id, name, kind) in ITEM_ATTRS { + let Some(v) = attrs.get(id) else { continue }; + let text = match kind { + AttrKind::Number => v.as_f64().map(num), + AttrKind::Swatch => v.as_ref().and_then(|u| self.doc.swatches.get(&u).cloned()), + }; + if let Some(t) = text { + x.attr(name, t); + } + } + } + fn fonts(&self) -> String { let mut x = Xml::new(); self.package_root(&mut x, "Fonts"); @@ -226,11 +251,16 @@ impl Writer<'_> { x.attr("NextStyle", self.style_ref(s.next.or(Some(s.uid)), true)); } if let Some(base) = s.based_on.and_then(|b| doc.styles.get(&b)) { + // The root "[No ... style]" is written as a string. + let base_name = style_name(base); + let root = base.builtin && base.name.starts_with("[No "); + let (kind, value) = if root { + ("string", base_name) + } else { + ("object", format!("{tag}/{}", self_name(&base_name))) + }; x.start("Properties"); - x.start("BasedOn") - .attr("type", "object") - .text(&format!("{tag}/{}", self_name(&style_name(base)))) - .end(); + x.start("BasedOn").attr("type", kind).text(&value).end(); x.end(); } x.end(); @@ -302,6 +332,7 @@ impl Writer<'_> { .attr("NextTextFrame", uref(*next)) .attr("ContentType", "TextType"); } + self.item_attrs(x, &item.attrs); x.attr("ItemLayer", uref(Some(item.layer))) .attr("ItemTransform", matrix(&item.transform)); Self::path_geometry(x, &item.paths); @@ -379,7 +410,10 @@ impl Writer<'_> { let mut parts = text.split('\r').peekable(); while let Some(part) = parts.next() { if !part.is_empty() { - x.start("Content").text(part).end(); + // INDD stores a forced line break as LF; IDML as U+2028. + x.start("Content") + .text(&part.replace('\n', "\u{2028}")) + .end(); } if parts.peek().is_some() { x.start("Br").end(); diff --git a/src/model/attrs.rs b/src/model/attrs.rs new file mode 100644 index 0000000..a237f77 --- /dev/null +++ b/src/model/attrs.rs @@ -0,0 +1,113 @@ +//! Attribute lists (chunk 0x6E03 and others). See `docs/format/objects.md`. +//! +//! A list is a u32 count, then records: u32 attribute ID, u16 payload size, +//! payload. The payload is a u16 value count and that many values, each a +//! u32 type, u16 length and data. The first value is the attribute's value. + +use crate::Error; +use crate::object::Cursor; + +/// Value type codes. +pub mod ty { + pub const DOUBLE: u32 = 0x6E68; + pub const INT: u32 = 0x6E67; + pub const ENUM: u32 = 0x6E65; + pub const POINT: u32 = 0x6E69; + pub const REF: u32 = 0x117; +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Value { + Double(f64), + Int(i32), + Enum(u16), + Ref(u32), + Point(f64, f64), + Other(u32, Vec), +} + +impl Value { + pub fn as_f64(&self) -> Option { + match *self { + Value::Double(v) => Some(v), + Value::Int(v) => Some(v as f64), + Value::Enum(v) => Some(v as f64), + _ => None, + } + } + + pub fn as_ref(&self) -> Option { + match *self { + Value::Ref(v) => Some(v), + _ => None, + } + } +} + +#[derive(Debug, Clone, Default, PartialEq)] +pub struct Attrs(pub Vec<(u32, Value)>); + +impl Attrs { + pub fn get(&self, id: u32) -> Option<&Value> { + self.0.iter().find(|(a, _)| *a == id).map(|(_, v)| v) + } + + pub fn parse(data: &[u8]) -> Result { + let mut c = Cursor::new(data); + let n = c.u32()?; + let mut out = Vec::with_capacity(n.min(1024) as usize); + for _ in 0..n { + let id = c.u32()?; + let size = c.u16()? as usize; + let mut p = Cursor::new(c.bytes(size)?); + let count = p.u16()?; + let mut first = None; + for _ in 0..count { + let t = p.u32()?; + let len = p.u16()? as usize; + let data = p.bytes(len)?; + if first.is_none() { + first = Some(decode(t, data)); + } + } + if let Some(v) = first { + out.push((id, v)); + } + } + Ok(Attrs(out)) + } +} + +fn decode(t: u32, data: &[u8]) -> Value { + let f64_at = |o: usize| f64::from_le_bytes(data[o..o + 8].try_into().unwrap()); + match (t, data.len()) { + (ty::DOUBLE, 8) => Value::Double(f64_at(0)), + (ty::INT, 4) => Value::Int(i32::from_le_bytes(data.try_into().unwrap())), + (ty::ENUM, 2) => Value::Enum(u16::from_le_bytes(data.try_into().unwrap())), + (ty::REF, 4) => Value::Ref(u32::from_le_bytes(data.try_into().unwrap())), + (ty::POINT, 16) => Value::Point(f64_at(0), f64_at(8)), + _ => Value::Other(t, data.to_vec()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_a_stroke_weight() { + // One attribute: 0x6E65 = double 0.25, plus a 6-byte trailer value. + let mut d = 1u32.to_le_bytes().to_vec(); + d.extend_from_slice(&0x6E65u32.to_le_bytes()); + d.extend_from_slice(&28u16.to_le_bytes()); + d.extend_from_slice(&2u16.to_le_bytes()); + d.extend_from_slice(&ty::DOUBLE.to_le_bytes()); + d.extend_from_slice(&8u16.to_le_bytes()); + d.extend_from_slice(&0.25f64.to_le_bytes()); + d.extend_from_slice(&0x6E63u32.to_le_bytes()); + d.extend_from_slice(&6u16.to_le_bytes()); + d.extend_from_slice(&[0; 6]); + let a = Attrs::parse(&d).unwrap(); + assert_eq!(a.get(0x6E65), Some(&Value::Double(0.25))); + } +} diff --git a/src/model/color.rs b/src/model/color.rs new file mode 100644 index 0000000..038536f --- /dev/null +++ b/src/model/color.rs @@ -0,0 +1,142 @@ +//! Colours and swatches. See `docs/format/objects.md`. + +use crate::Error; +use crate::object::Cursor; + +pub mod class { + pub const COLOR: u32 = 0x1F05; + pub const SWATCH_NONE: u32 = 0x6E0B; +} + +pub mod chunk { + pub const COLOR_VALUE: u32 = 0x1F01; + pub const COLOR_MODEL: u32 = 0x1F09; + pub const COLOR_NAME: u32 = 0x1F10; + pub const COLOR_OVERRIDE: u32 = 0x1F24; +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Space { + Rgb, + Cmyk, + Lab, + Other(u32), +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Color { + pub uid: u32, + /// Empty for unnamed colours, which IDML names by UID. + pub name: String, + /// The name is an InDesign key, written with `$ID/` in IDML. + pub builtin_name: bool, + pub model: u32, + pub space: Space, + /// Component values as fractions (0–1) for RGB and CMYK. + pub values: Vec, + pub editable: bool, + pub removable: bool, + pub visible: bool, + pub color_override: u32, +} + +impl Color { + /// The IDML reference for this colour (`Color/` or `Color/u`). + pub fn reference(&self) -> String { + if self.name.is_empty() { + format!("Color/u{:x}", self.uid) + } else { + format!( + "Color/{}", + self.name.replace('%', "%25").replace(':', "%3a") + ) + } + } + + /// The `Name` attribute. + pub fn idml_name(&self) -> String { + if self.builtin_name { + format!("$ID/{}", self.name) + } else { + self.name.clone() + } + } + + pub fn model_name(&self) -> &'static str { + match self.model { + 1 => "Spot", + 2 => "Registration", + 3 => "MixedInkModel", + _ => "Process", + } + } + + pub fn space_name(&self) -> &'static str { + match self.space { + Space::Rgb => "RGB", + Space::Lab => "LAB", + _ => "CMYK", + } + } + + /// Component values as IDML writes them: percentages for CMYK, 0–255 + /// for RGB, raw values otherwise. + pub fn idml_values(&self) -> Vec { + match self.space { + Space::Cmyk => self.values.iter().map(|v| v * 100.0).collect(), + Space::Rgb => self.values.iter().map(|v| v * 255.0).collect(), + _ => self.values.clone(), + } + } + + pub fn override_name(&self) -> &'static str { + match self.color_override { + 1 => "Specialpaper", + 2 => "Specialblack", + 3 => "Specialregistration", + 4 => "Hiddenreserved", + _ => "Normal", + } + } + + pub fn read(uid: u32, obj: &crate::Object) -> Result, Error> { + let (Some(name), Some(value)) = + (obj.chunk(chunk::COLOR_NAME), obj.chunk(chunk::COLOR_VALUE)) + else { + return Ok(None); + }; + let mut c = Cursor::new(name); + let builtin_name = c.u8()? == 1; + let name_str = c.string()?; + let flags = c.u32()?; + let mut v = Cursor::new(value); + let space = match v.u32()? { + 5 => Space::Rgb, + 6 => Space::Cmyk, + 7 => Space::Lab, + other => Space::Other(other), + }; + let n = v.u16()?; + let values = (0..n).map(|_| v.f64()).collect::, _>>()?; + let model = match obj.chunk(chunk::COLOR_MODEL) { + Some(d) => Cursor::new(d).u32()?, + None => 0, + }; + let color_override = match obj.chunk(chunk::COLOR_OVERRIDE) { + Some(d) if d.len() >= 12 => Cursor::new(&d[8..]).u32()?, + _ => 0, + }; + Ok(Some(Color { + uid, + name: name_str, + builtin_name, + model, + space, + values, + removable: flags & 1 != 0, + visible: flags & 2 != 0, + editable: flags & 4 != 0, + color_override, + })) + } +} diff --git a/src/model.rs b/src/model/mod.rs similarity index 92% rename from src/model.rs rename to src/model/mod.rs index 0e84beb..6ec9779 100644 --- a/src/model.rs +++ b/src/model/mod.rs @@ -1,8 +1,14 @@ //! A document model built from database objects. See //! `docs/format/objects.md` for the class and chunk layouts used here. +pub mod attrs; +pub mod color; + use std::collections::{BTreeMap, HashMap}; +pub use attrs::{Attrs, Value}; +pub use color::Color; + use crate::object::{Cursor, Object}; use crate::{Database, Error, Version}; @@ -50,6 +56,8 @@ pub mod chunk { pub const STRAND_DATA: u32 = 0x261; pub const STRAND_RUNS: u32 = 0x262; pub const STYLE_INFO: u32 = 0x230; + pub const ITEM_ATTRS: u32 = 0x6E03; + pub const SWATCH_NAME: u32 = 0x1F30; } /// Kinds of strand run data (first u32 of chunk 0x262). @@ -136,6 +144,8 @@ pub struct PageItem { pub transform: Matrix, pub paths: Vec, pub layer: u32, + /// Local formatting (fill, stroke, corners, ...). + pub attrs: Attrs, pub children: Vec, } @@ -182,6 +192,9 @@ pub struct Document { pub master_spreads: Vec, pub stories: Vec, pub styles: BTreeMap, + pub colors: Vec, + /// IDML reference (`Color/...`, `Swatch/None`) for each swatch UID. + pub swatches: BTreeMap, } /// Reads typed objects from a database, caching them. @@ -263,11 +276,28 @@ impl<'a> Reader<'a> { .map(|uid| self.story(uid)) .collect::, _>>()?; let mut styles = BTreeMap::new(); + let mut colors = Vec::new(); + let mut swatches = BTreeMap::new(); for &(uid, cls) in self.db.classes() { - if cls == class::STYLE - && let Some(style) = self.style(uid)? - { - styles.insert(uid, style); + match cls { + class::STYLE => { + if let Some(style) = self.style(uid)? { + styles.insert(uid, style); + } + } + color::class::COLOR => { + if self.db.object(uid)?.is_none() { + continue; + } + if let Some(c) = Color::read(uid, &*self.object(uid)?)? { + swatches.insert(uid, c.reference()); + colors.push(c); + } + } + color::class::SWATCH_NONE => { + swatches.insert(uid, "Swatch/None".into()); + } + _ => {} } } Ok(Document { @@ -278,6 +308,8 @@ impl<'a> Reader<'a> { master_spreads, stories, styles, + colors, + swatches, }) } @@ -443,6 +475,10 @@ impl<'a> Reader<'a> { } } let paths = self.paths(uid)?; + let attrs = match self.chunk(uid, chunk::ITEM_ATTRS)? { + Some(d) => Attrs::parse(&d).unwrap_or_default(), + None => Attrs::default(), + }; let kind = if cls == Some(class::GROUP) { ItemKind::Group } else if let Some(column) = text_column { @@ -456,6 +492,7 @@ impl<'a> Reader<'a> { transform, paths, layer, + attrs, children, })) } @@ -539,8 +576,7 @@ impl<'a> Reader<'a> { c.skip(6)?; let mut strands = vec![c.u32()?]; strands.extend(c.u32_list()?); - let mut text = String::new(); - let mut text_units = 0usize; + let mut text: Vec = Vec::new(); let mut para: Vec<(usize, u32)> = Vec::new(); let mut chars: Vec<(usize, u32)> = Vec::new(); for strand in strands { @@ -564,8 +600,7 @@ impl<'a> Reader<'a> { let len = rc.u32()? as usize; match kind { strand::TEXT => { - text.push_str(&rc.segments(len)?); - text_units += len; + text.extend(rc.segment_units(len)?); } strand::PARAGRAPH_STYLE => para.push((len, rc.u32()?)), strand::CHARACTER_STYLE => chars.push((len, rc.u32()?)), @@ -576,7 +611,7 @@ impl<'a> Reader<'a> { } Ok(Story { uid, - runs: split_runs(&text, text_units, ¶, &chars), + runs: split_runs(&text, ¶, &chars), }) } } @@ -600,14 +635,7 @@ fn find_string(data: &[u8], from: usize) -> Result { /// Combine text with paragraph-style and character-style run lengths /// (counted in UTF-16 code units) into runs with both styles constant. -fn split_runs( - text: &str, - units: usize, - para: &[(usize, u32)], - chars: &[(usize, u32)], -) -> Vec { - let utf16: Vec = text.encode_utf16().collect(); - debug_assert_eq!(utf16.len(), units); +fn split_runs(utf16: &[u16], para: &[(usize, u32)], chars: &[(usize, u32)]) -> Vec { let mut cuts: Vec = vec![0, utf16.len()]; for list in [para, chars] { let mut pos = 0; @@ -674,7 +702,8 @@ mod tests { #[test] fn splits_runs_at_style_boundaries() { - let runs = split_runs("abcdef", 6, &[(4, 10), (2, 11)], &[(2, 20), (4, 21)]); + let text: Vec = "abcdef".encode_utf16().collect(); + let runs = split_runs(&text, &[(4, 10), (2, 11)], &[(2, 20), (4, 21)]); let texts: Vec<_> = runs.iter().map(|r| r.text.as_str()).collect(); assert_eq!(texts, ["ab", "cd", "ef"]); assert_eq!(runs[1].paragraph_style, Some(10)); diff --git a/src/object.rs b/src/object.rs index 9cb4267..7750bda 100644 --- a/src/object.rs +++ b/src/object.rs @@ -123,6 +123,12 @@ impl<'a> Cursor<'a> { /// the rest: 0x4000 = single-byte characters, 0x8000 = UTF-16LE code /// units. pub fn segments(&mut self, chars: usize) -> Result { + Ok(String::from_utf16_lossy(&self.segment_units(chars)?)) + } + + /// Like [`Cursor::segments`], but returns the UTF-16 code units, so a + /// surrogate pair split between two runs can be joined by the caller. + pub fn segment_units(&mut self, chars: usize) -> Result, Error> { let mut units: Vec = Vec::with_capacity(chars); while units.len() < chars { let header = self.u16()?; @@ -154,7 +160,7 @@ impl<'a> Cursor<'a> { units.len() ))); } - Ok(String::from_utf16_lossy(&units)) + Ok(units) } /// A string stored inside object data: u8 2, a u8 whose meaning is