From 2319223d3a61d8b58963e72e4db07e0707962af3 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Wed, 7 Oct 2026 23:49:40 +0000 Subject: [PATCH] feat: write cell edges along merged cells and the other cell attributes An edge value is written when every grid position along that edge has it with the same value; the priority comes from the cell's own set, or is 1 for the right (bottom) edge of a merged cell whose edge positions have one. New edge groups: gap tint, gap colour, overprint and gap overprint. Cell edge tints -1 are written as 100 and a weight is 0 when the edge colour is None. Stroke codes 0x5A3F (Japanese Dots) and 0xB007 (ThickThick) for cells and tables. New cell attributes: graphic insets, OverprintFill, VerticalJustification 0 and 3, FirstBaselineOffset, WritingDirection (not on cell styles: the schema lacks it), RotationAngle, DiagonalLineStrokeOverprint. Text*Inset only from DOM 11. Cell style edge IDs are rotated against the cell IDs (tables.md). compare.py --exclude own/, trustworthy pairs: value coverage 93.13 % -> 94.23 %, median 96.7 % -> 96.8 %, documents >= 99 % 14 -> 18; extra values 30,379 -> 30,237. Every Cell edge key is now right on every cell except one bottom weight (tables.md); CellStyle edge keys rise too. One unnamed colour (14 values) is no longer written: its only reference was a wrong BottomEdgeStrokeColor that is now right, and IDML writes the colour though nothing refers to it (objects.md). In stale pairs the extras of a table missing from its reference grow by 2,590. --- docs/format/tables.md | 172 +++++++++++++++------ src/idml/attrs.rs | 343 ++++++++++++++++++++++++++++++++++++++---- src/idml/kind.rs | 6 + src/idml/story.rs | 3 + src/idml/styles.rs | 6 +- src/model/table.rs | 23 +++ 6 files changed, 479 insertions(+), 74 deletions(-) diff --git a/docs/format/tables.md b/docs/format/tables.md index 8c52c41..2e6501f 100644 --- a/docs/format/tables.md +++ b/docs/format/tables.md @@ -202,63 +202,129 @@ covers that position. ## Cell formatting A cell's formatting is the attribute set that covers its position. IDML -writes these attributes on a `Cell` exactly when the set has them, so -they are the cell's local values. The exception is the text insets and -`ClipContentToTextCell`: from DOM 11 IDML writes them on every cell, -with the value in effect (above); where the set has the value, it is -that value. +writes the attributes below on a `Cell` exactly when the set has them, +so they are the cell's local values. The exceptions are the text insets +and `ClipContentToTextCell`, written on every cell from DOM 11 with the +value in effect (above), and the edges (below). -Evidence: the same-version pairs have 1,125 IDML cells with an INDD -cell, 952 of them in a set (5 files). For every attribute below, the INDD set has the -attribute on exactly the cells whose IDML `Cell` has it (no cell has one -without the other), and the values match on all of them. The 144 cells -of pairs whose IDML is from an older version agree as well. +Evidence: the INDD set has each attribute below on exactly the cells +whose IDML `Cell` has it, and the values match on all of them. The +counts of the insets, fills and 0xB6DE are from a first sample of 1,125 +IDML cells (952 in a set, 5 files); the others, and codes 0 and 3 of +0xB677, are counted over the trustworthy pairs (11,844 cells in 53 +files), where every attribute of the table now matches on every cell. -| ID | IDML | Encoding | Cells (files, distinct values) | +| ID | IDML | Encoding | Cells (files) | |---|---|---|---| -| 0xB62B | `LeftInset`, `TextLeftInset` | f64 | 601 (5, 5) | -| 0xB62C | `TopInset`, `TextTopInset` | f64 | 523 (5, 3) | -| 0xB62D | `RightInset`, `TextRightInset` | f64 | 631 (5, 3) | -| 0xB62E | `BottomInset`, `TextBottomInset` | f64 | 523 (5, 3) | -| 0xB63D | `FillColor` | swatch UID | 176 (4, 8) | -| 0xB63E | `FillTint` | f64 | 142 (2, 4) | -| 0xB677 | `VerticalJustification` | 1 `CenterAlign`, 2 `BottomAlign` | 457 (3) | +| 0xB62B | `LeftInset`, `TextLeftInset` | f64 | 601 (5) | +| 0xB62C | `TopInset`, `TextTopInset` | f64 | 523 (5) | +| 0xB62D | `RightInset`, `TextRightInset` | f64 | 631 (5) | +| 0xB62E | `BottomInset`, `TextBottomInset` | f64 | 523 (5) | +| 0x10470, 0x10471, 0x10472, 0x10473 | `GraphicLeftInset`, `GraphicTopInset`, `GraphicRightInset`, `GraphicBottomInset` | f64 | 96 (2), all 1; order below | +| 0xB63D | `FillColor` | swatch UID | 176 (4) | +| 0xB63E | `FillTint` | f64 | 142 (2) | +| 0xB639 | `OverprintFill` | u16, 0 = false | 153 (1); 6 cell styles | +| 0xB677 | `VerticalJustification` | 0 `TopAlign`, 1 `CenterAlign`, 2 `BottomAlign`, 3 `JustifyAlign` | 5,361 (21); 383 cells with 0 (1 file); 3 in 1 cell style | +| 0xB676 | `FirstBaselineOffset` | 0 `LeadingOffset`, 1 `AscentOffset` | 4,445 (3); 2 cell styles | +| 0xB6E1 | `WritingDirection` | 1 = true | 4,445 (3) | +| 0xB675 | `RotationAngle` | f64 | 1 (270) | | 0xB6DE | `ClipContentToCell` | 0 = false | 664 (5, one value) | -| 0xB645, 0xB646, 0xB647, 0xB648 | `Left`, `Right`, `Top`, `BottomEdgeStrokeWeight` | f64 | 426, 358, 412, 443 (2) | -| 0xB649, 0xB64A, 0xB64B, 0xB64C | `Left`, `Top`, `Right`, `BottomEdgeStrokeColor` | swatch UID | 449, 449, 410, 449 (2) | -| 0xB64D, 0xB64E, 0xB64F, 0xB650 | `Left`, `Right`, `Top`, `BottomEdgeStrokeType` | stroke style code (below) | 426, 358, 412, 443 (2) | -| 0xB6A8, 0xB6A9, 0xB6AA, 0xB6AB | `Left`, `Right`, `Top`, `BottomEdgeStrokeTint` | f64 | 429, 364, 443, 443 (2) | -| 0xB6F9, 0xB6FA, 0xB6FB, 0xB6FC | `Left`, `Right`, `Top`, `BottomEdgeStrokePriority` | u32 | 593, 583, 593, 593 (3, 11–16 values) | +| 0xB6DC | `DiagonalLineStrokeOverprint` | u16, 0 = false | 16 (1) | -Note the colours: their order is left, top, right, bottom, while the -other edge attributes are left, right, top, bottom. Each assignment was -checked against the alternatives; any swap gives mismatches (for -example 0xB648 as the top weight differs on 10 cells and is present on -31 cells without a top weight). The top and bottom stroke types have one -value (`Solid`), but they are told apart by which cells have them. +`TextLeftInset` and the other three exist from DOM 11. Before, IDML +writes only `LeftInset` and the others: the 453 cells of DOM 8 and 10 +have no `TextTopInset` (28 of them, in one DOM 8.1 pair, have +`TopInset`), and 5 named cell styles of DOM 8 and 10 with `TopInset` +have no `TextTopInset`, while all 25 of DOM 11 to 21 with `TopInset` +have both. -**Edge stroke type.** Eight bytes: a stroke style code and 0. Code -0x5A29 is `StrokeStyle/$ID/Solid` (1,570 cell edges), the same code as -for page items (`attributes.md`); 0x1040C is `n`, no stroke type (69 -cell edges). -The converter uses the page item stroke style codes for cells too. - -**Right edge priority.** 10 cells have a right edge priority in IDML -but not in their set. The converter writes priorities only from the -set. +0xB6E1 and 0xB676 always occur together on cells, both with value 1. +Cell styles can have 0xB6E1 too, but the IDML schema has no +`WritingDirection` on `CellStyle`, so it is not written there. +0xB676 is the baseline offset because cell styles have it alone (2 +styles). The graphic insets are all equal on cells; in cell styles +{0x10470, 0x10472} are left and right and {0x10471, 0x10473} top and +bottom (values 3 and 0); the order inside each pair follows the text +insets. **Applied cell style.** The set's two u32 after the attributes are the applied cell style priority and the cell style. All 952 cells in a set -match `AppliedCellStylePriority`, and all match `AppliedCellStyle` (a -cell style UID; 0 for 70 cells whose IDML style is `[None]`). The 173 -cells of the pairs in records without sets all have +of the first sample match `AppliedCellStylePriority`, and all match +`AppliedCellStyle` (a cell style UID; 0 for 70 cells whose IDML style is +`[None]`). The 173 cells of the pairs in records without sets all have `AppliedCellStyle="CellStyle/$ID/[None]"` and priority 0, and the converter writes those values for them. -**Not converted.** The gap colour, gap tint and overprint of the edges -(0x10420–0x10423, 0x1040F–0x10412, 0xB6BA–0xB6BD) have one value in -every cell and the same set of cells for three edges, so the edges -cannot be told apart. +## Cell edges + +| Attribute | Left | Right | Top | Bottom | Encoding | +|---|---|---|---|---|---| +| `…EdgeStrokeWeight` | 0xB645 | 0xB646 | 0xB647 | 0xB648 | f64 | +| `…EdgeStrokeColor` | 0xB649 | 0xB64B | 0xB64A | 0xB64C | swatch UID | +| `…EdgeStrokeType` | 0xB64D | 0xB64E | 0xB64F | 0xB650 | stroke style code | +| `…EdgeStrokeTint` | 0xB6A8 | 0xB6A9 | 0xB6AA | 0xB6AB | f64 | +| `…EdgeStrokePriority` | 0xB6F9 | 0xB6FA | 0xB6FB | 0xB6FC | u32 | +| `…EdgeStrokeGapTint` | 0x1040F | 0x10410 | 0x10411 | 0x10412 | f64 | +| `…EdgeStrokeGapColor` | 0x10420 | 0x10421 | 0x10422 | 0x10423 | swatch UID | +| `…EdgeStrokeOverprint` | 0xB6BA | 0xB6BB | 0xB6BC | 0xB6BD | u16, 0 = false | +| `…EdgeStrokeGapOverprint` | 0x10431 | 0x10432 | 0x10433 | 0x10434 | u16, 0 = false | + +Note the colours: their order is left, top, right, bottom, while the +other edge attributes are left, right, top, bottom. Each assignment is +the only one with no presence mismatch once the rule below is applied +(for example 0xB648 as the top weight differs on 10 cells and is present +on 31 cells without a top weight; 0x10410 as the left gap tint would be +present on 58 cells without one). Evidence for the last four groups +(trustworthy cells with the attribute): gap tint 7,767 / 7,713 / 8,190 / +8,150 (9 files), gap colour 7,698 / 7,646 / 7,922 / 7,880 (9 files), +overprint 7,540 / 7,514 / 7,962 / 7,947 (9 files), gap overprint 174 / +79 / 865 / 834 (2 files). + +**Which set holds an edge.** An edge is held by the grid positions along +it: the left edge by the positions of the cell's first column in all its +rows, the right edge by its last column, the top edge by its first row, +the bottom edge by its last row. IDML writes an edge attribute when +every position along the edge has it with the same value, and writes that +value. For a 1 × 1 cell this is its own set. Evidence: 47,376 edge +values per attribute kind (46,844 one-position edges, 532 longer edges): +presence and value match for weight, colour, type, tint, gap tint, gap +colour, overprint and gap overprint, with one exception (a bottom weight +of a 5-column cell in a row whose records are short; IDML writes 0 for +values 3, 1, 3, 0, 1). Taking the cell's own set for all edges misses +144 right colours, 45 bottom colours and their weights, types and tints +in 6 files. + +**Priority** is not taken along the edge. It is the value in the cell's +own set for all four edges. If the own set has no right (bottom) +priority, the cell spans more than one column (row), and a position +along the right (bottom) edge has one, IDML writes `1`. Evidence: 11,578 +one-position cells (all four edges), 266 merged cells; the `1` case is +157 right and 45 bottom edges, with no counter-example; 4 + 12 merged +cells whose edge positions have no priority have none in IDML. + +**Values.** + +- Tint and gap tint −1 are written as `100` (2,712 stroke tints in 4 + files, 1,200 gap tints in 1 file); other values as they are. + `FillTint` −1 stays `-1`. +- Weight: when the edge's colour, as written, is the `None` swatch, IDML + writes weight `0` whatever is stored (428 edges in 2 files with stored + weights 0.35 to 3; every other edge with a `None` colour stores 0). +- Neither rule holds for cell styles: one style stores tint −1 on all + four edges and IDML writes `-1`; two styles store weight 1 with the + `None` colour and IDML writes `1`. +- Colour or gap colour UID 0 is not a swatch and is not written (209 + edges in 1 file have it, none in IDML). +- Why −1 becomes 100 and why a `None` colour zeroes the weight is not + known; both are seen without exception. + +**Stroke type.** Eight bytes: a stroke style code and 0. Code 0x5A29 is +`StrokeStyle/$ID/Solid`, the same code as for page items +(`attributes.md`); 0x1040C is `n`, no stroke type. Code 0x5A3F is +`StrokeStyle/$ID/Japanese Dots` (13,409 edges in cell sets and cell +styles, 3 files) and 0xB007 `ThickThick` (one table border, all four +sides). These two are used for cells and tables only: page item stroke +types are not checked against them. ## Cell and table styles @@ -277,11 +343,23 @@ match an IDML style by name. Their based-on style matches IDML (`[None]`, `[No table style]`) is written as a string, any other as an object reference (one table style). -Cell styles use the cell attribute IDs above. All 7 named cell styles -of the pairs match their IDML on every attribute the converter writes. +Cell styles use the cell attribute IDs above, except the edges. Attribute 0x10463 is the paragraph style UID (`AppliedParagraphStyle`, 4 of 4 styles that have it). +**Edge IDs in cell styles are rotated.** A cell edge ID gives a +different edge in a cell style: cell left is style top, cell right is +style bottom, cell top is style right, cell bottom is style left. +Evidence: 8 named styles in 3 trustworthy files whose edges differ (a +header row style with top and bottom 0.25 Black and left and right 0 +None; a body style with right and bottom 1 and no left; others). Weights +show the rotation for all four edges, colours and tints for top and +bottom. The other IDs follow the same rotation by analogy: every sample +has equal left and right colours and tints, equal stroke types on all +four edges, and equal gap tints, gap colours and overprints (3 styles). +Text insets are not rotated (a style with left and right 10, top and +bottom 5 has 0xB62B = 10, 0xB62C = 5). + ## Table attributes The table's chunk 0xB668 and a table style's chunk 0xB667 use the same diff --git a/src/idml/attrs.rs b/src/idml/attrs.rs index 75332cf..d36c156 100644 --- a/src/idml/attrs.rs +++ b/src/idml/attrs.rs @@ -132,6 +132,11 @@ pub(super) const STROKE_TYPES: &[(u32, &str)] = &[ (0xB01A, "Triple_Stroke"), ]; +/// Codes of built-in stroke styles seen only in cells and tables, in +/// addition to `STROKE_TYPES`. See `docs/format/tables.md`. +pub(super) const TABLE_STROKE_TYPES: &[(u32, &str)] = + &[(0x5A3F, "Japanese Dots"), (0xB007, "ThickThick")]; + /// The stroke styles every corpus IDML lists in `Graphic.xml`, in order. /// See `docs/format/idml-values.md`. pub(super) const BUILTIN_STROKE_STYLES: &[&str] = &[ @@ -562,43 +567,150 @@ pub(super) fn join_numbers(v: &[u32]) -> String { v.iter().map(u32::to_string).collect::>().join(" ") } -/// Cell attributes of a cell attribute set: ID, IDML attributes, kind. -/// See `docs/format/tables.md`. +/// Cell attributes of a cell attribute set other than the edges: ID, +/// IDML attributes, kind. IDML names that start with `Text` are written +/// from DOM 11 only. See `docs/format/tables.md`. pub(super) const CELL_ATTRS: &[(u32, &[&str], Kind)] = &[ (0xB62C, &["TextTopInset", "TopInset"], Kind::Number), (0xB62B, &["TextLeftInset", "LeftInset"], Kind::Number), (0xB62E, &["TextBottomInset", "BottomInset"], Kind::Number), (0xB62D, &["TextRightInset", "RightInset"], Kind::Number), + (0x10470, &["GraphicLeftInset"], Kind::Number), + (0x10471, &["GraphicTopInset"], Kind::Number), + (0x10472, &["GraphicRightInset"], Kind::Number), + (0x10473, &["GraphicBottomInset"], Kind::Number), (0xB63D, &["FillColor"], Kind::Swatch), (0xB63E, &["FillTint"], Kind::Number), + (0xB639, &["OverprintFill"], Kind::Enum(&[(0, "false")])), ( 0xB677, &["VerticalJustification"], - Kind::Enum(&[(1, "CenterAlign"), (2, "BottomAlign")]), + Kind::Enum(&[ + (0, "TopAlign"), + (1, "CenterAlign"), + (2, "BottomAlign"), + (3, "JustifyAlign"), + ]), ), + ( + 0xB676, + &["FirstBaselineOffset"], + Kind::Enum(&[(0, "LeadingOffset"), (1, "AscentOffset")]), + ), + (0xB6E1, &["WritingDirection"], Kind::Enum(&[(1, "true")])), + (0xB675, &["RotationAngle"], Kind::Number), (0xB6DE, &["ClipContentToCell"], Kind::Enum(&[(0, "false")])), - (0xB645, &["LeftEdgeStrokeWeight"], Kind::Number), - (0xB64D, &["LeftEdgeStrokeType"], Kind::StrokeType), - (0xB649, &["LeftEdgeStrokeColor"], Kind::Swatch), - (0xB6A8, &["LeftEdgeStrokeTint"], Kind::Number), - (0xB6F9, &["LeftEdgeStrokePriority"], Kind::Integer), - (0xB647, &["TopEdgeStrokeWeight"], Kind::Number), - (0xB64F, &["TopEdgeStrokeType"], Kind::StrokeType), - (0xB64A, &["TopEdgeStrokeColor"], Kind::Swatch), - (0xB6AA, &["TopEdgeStrokeTint"], Kind::Number), - (0xB6FB, &["TopEdgeStrokePriority"], Kind::Integer), - (0xB646, &["RightEdgeStrokeWeight"], Kind::Number), - (0xB64E, &["RightEdgeStrokeType"], Kind::StrokeType), - (0xB64B, &["RightEdgeStrokeColor"], Kind::Swatch), - (0xB6A9, &["RightEdgeStrokeTint"], Kind::Number), - (0xB6FA, &["RightEdgeStrokePriority"], Kind::Integer), - (0xB648, &["BottomEdgeStrokeWeight"], Kind::Number), - (0xB650, &["BottomEdgeStrokeType"], Kind::StrokeType), - (0xB64C, &["BottomEdgeStrokeColor"], Kind::Swatch), - (0xB6AB, &["BottomEdgeStrokeTint"], Kind::Number), - (0xB6FC, &["BottomEdgeStrokePriority"], Kind::Integer), + ( + 0xB6DC, + &["DiagonalLineStrokeOverprint"], + Kind::Enum(&[(0, "false")]), + ), ]; +/// Cell edge attributes: the IDs for the left, right, top and bottom +/// edge, the IDML attributes for the same edges, and the kind. See +/// `docs/format/tables.md`. +pub(super) const CELL_EDGE_ATTRS: &[([u32; 4], [&str; 4], Kind)] = &[ + ( + [0xB645, 0xB646, 0xB647, 0xB648], + [ + "LeftEdgeStrokeWeight", + "RightEdgeStrokeWeight", + "TopEdgeStrokeWeight", + "BottomEdgeStrokeWeight", + ], + Kind::Number, + ), + ( + [0xB64D, 0xB64E, 0xB64F, 0xB650], + [ + "LeftEdgeStrokeType", + "RightEdgeStrokeType", + "TopEdgeStrokeType", + "BottomEdgeStrokeType", + ], + Kind::StrokeType, + ), + ( + [0xB649, 0xB64B, 0xB64A, 0xB64C], + [ + "LeftEdgeStrokeColor", + "RightEdgeStrokeColor", + "TopEdgeStrokeColor", + "BottomEdgeStrokeColor", + ], + Kind::Swatch, + ), + ( + [0xB6A8, 0xB6A9, 0xB6AA, 0xB6AB], + [ + "LeftEdgeStrokeTint", + "RightEdgeStrokeTint", + "TopEdgeStrokeTint", + "BottomEdgeStrokeTint", + ], + Kind::EdgeTint, + ), + ( + CELL_EDGE_PRIORITY, + [ + "LeftEdgeStrokePriority", + "RightEdgeStrokePriority", + "TopEdgeStrokePriority", + "BottomEdgeStrokePriority", + ], + Kind::Integer, + ), + ( + [0x1040F, 0x10410, 0x10411, 0x10412], + [ + "LeftEdgeStrokeGapTint", + "RightEdgeStrokeGapTint", + "TopEdgeStrokeGapTint", + "BottomEdgeStrokeGapTint", + ], + Kind::EdgeTint, + ), + ( + [0x10420, 0x10421, 0x10422, 0x10423], + [ + "LeftEdgeStrokeGapColor", + "RightEdgeStrokeGapColor", + "TopEdgeStrokeGapColor", + "BottomEdgeStrokeGapColor", + ], + Kind::Swatch, + ), + ( + [0xB6BA, 0xB6BB, 0xB6BC, 0xB6BD], + [ + "LeftEdgeStrokeOverprint", + "RightEdgeStrokeOverprint", + "TopEdgeStrokeOverprint", + "BottomEdgeStrokeOverprint", + ], + Kind::Enum(&[(0, "false")]), + ), + ( + [0x10431, 0x10432, 0x10433, 0x10434], + [ + "LeftEdgeStrokeGapOverprint", + "RightEdgeStrokeGapOverprint", + "TopEdgeStrokeGapOverprint", + "BottomEdgeStrokeGapOverprint", + ], + Kind::Enum(&[(0, "false")]), + ), +]; + +/// Edge stroke priority IDs (left, right, top, bottom). +pub(super) const CELL_EDGE_PRIORITY: [u32; 4] = [0xB6F9, 0xB6FA, 0xB6FB, 0xB6FC]; + +/// The edge of a cell style that each cell edge ID gives: cell left is +/// style top, right is bottom, top is right, bottom is left (indices +/// into the left, right, top, bottom order). +pub(super) const CELL_STYLE_EDGE: [usize; 4] = [2, 3, 1, 0]; + /// Table and table style attributes: ID, IDML attribute, kind. /// See `docs/format/tables.md`. pub(super) const TABLE_ATTRS: &[(u32, &str, Kind)] = &[ @@ -778,8 +890,8 @@ impl Writer<'_> { self.attr_values(attrs, TABLE_ATTRS) } - /// IDML attributes of a cell attribute set. Some cell attributes are - /// written as two IDML attributes. + /// IDML attributes of a cell attribute set other than its edges. + /// Some cell attributes are written as two IDML attributes. pub(super) fn cell_attrs(&self, attrs: &Attrs) -> Vec<(&'static str, String)> { let mut out = Vec::new(); for &(id, names, kind) in CELL_ATTRS { @@ -787,7 +899,10 @@ impl Writer<'_> { match self.value_text(kind, v) { Some(t) => { for &name in names { - out.push((name, t.clone())); + // Text cell values exist from DOM 11 (tables.md). + if self.doc.version.major >= 11 || !name.starts_with("Text") { + out.push((name, t.clone())); + } } } None if kind.is_code() => attrs.unknown_code(id, v), @@ -797,6 +912,102 @@ impl Writer<'_> { out } + /// The edge attributes of a cell. An edge value is written when every + /// grid position along the edge has it, with the same value; the + /// priority comes from the cell's own set (tables.md, "Cell edges"). + pub(super) fn cell_edge_attrs(&self, t: &Table, c: &Cell) -> Vec<(&'static str, String)> { + let edges = t.edge_formats(c); + let own = t.format(c); + let spans = [false, c.column_span > 1, false, c.row_span > 1]; + let mut values: Vec<[Option; 4]> = Vec::new(); + for &(ids, _, kind) in CELL_EDGE_ATTRS { + let mut row: [Option; 4] = Default::default(); + for e in 0..4 { + let id = ids[e]; + let value = if ids == CELL_EDGE_PRIORITY { + let v = own.and_then(|f| f.attrs.get(id)); + if v.is_none() + && spans[e] + && edges[e] + .iter() + .any(|f| f.is_some_and(|f| f.attrs.get(id).is_some())) + { + row[e] = Some("1".into()); + continue; + } + v + } else { + let mut vs = edges[e].iter().map(|f| f.and_then(|f| f.attrs.get(id))); + let first = vs.next().flatten(); + first.filter(|&v| vs.all(|w| w == Some(v))) + }; + let Some(v) = value else { continue }; + match self.value_text(kind, v) { + Some(text) => row[e] = Some(text), + None if kind.is_code() => { + if let Some(f) = own { + f.attrs.unknown_code(id, v); + } + } + None => {} + } + } + values.push(row); + } + Self::edge_values(&mut values); + Self::edge_list(values) + } + + /// Edge values as IDML attributes, by edge (left, top, right, bottom). + /// `values` follows `CELL_EDGE_ATTRS`. + fn edge_list(mut values: Vec<[Option; 4]>) -> Vec<(&'static str, String)> { + let mut out = Vec::new(); + for e in [0, 2, 1, 3] { + for (row, &(_, names, _)) in values.iter_mut().zip(CELL_EDGE_ATTRS) { + if let Some(v) = row[e].take() { + out.push((names[e], v)); + } + } + } + out + } + + /// Edge values of a cell written as IDML writes them: a weight is 0 + /// when the edge's colour is the `None` swatch. `values` follows + /// `CELL_EDGE_ATTRS` (weights first, colours third). + fn edge_values(values: &mut [[Option; 4]]) { + let (weights, rest) = values.split_at_mut(1); + for (weight, color) in weights[0].iter_mut().zip(&rest[1]) { + if color.as_deref() == Some("Swatch/None") && weight.is_some() { + *weight = Some("0".into()); + } + } + } + + /// The edge attributes of a cell style: the cell edge IDs, each + /// written for the rotated style edge (`CELL_STYLE_EDGE`). + pub(super) fn cell_style_edge_attrs(&self, attrs: &Attrs) -> Vec<(&'static str, String)> { + let mut values: Vec<[Option; 4]> = Vec::new(); + for &(ids, _, kind) in CELL_EDGE_ATTRS { + let mut row: [Option; 4] = Default::default(); + // Styles keep their stored tints and weights (tables.md). + let kind = match kind { + Kind::EdgeTint => Kind::Number, + k => k, + }; + for e in 0..4 { + let Some(v) = attrs.get(ids[e]) else { continue }; + match self.value_text(kind, v) { + Some(text) => row[CELL_STYLE_EDGE[e]] = Some(text), + None if kind.is_code() => attrs.unknown_code(ids[e], v), + None => {} + } + } + values.push(row); + } + Self::edge_list(values) + } + /// The gradient attributes of a page item: its own, or, for a group, /// the values all its children have. See `docs/format/objects.md`. pub(super) fn gradients(item: &PageItem) -> Vec<(&'static str, String)> { @@ -886,6 +1097,86 @@ mod tests { assert!(plain.is_empty()); } + #[test] + fn writes_cell_edges_along_the_edge() { + use crate::model::table::{Cell, CellFormat, CellKind, Row, Table}; + let mut doc = Document::default(); + doc.swatches.insert(9, "Swatch/None".into()); + doc.swatches.insert(10, "Color/u10".into()); + let w = Writer::for_test(&doc); + let format = |values: Vec<(u32, Value)>| { + let mut attrs = Attrs::default(); + attrs.values = values; + CellFormat { + attrs, + style_priority: 0, + style: 0, + } + }; + // A cell spanning two columns. Both positions have a top weight + // of 2 with the None colour and the same bottom colour; its own + // set has a left tint of -1 and a priority on the left only; the + // covered position has a right colour and priority. + let formats = vec![ + format(vec![ + (0xB647, Value::Double(2.0)), + (0xB64A, Value::Ref(9)), + (0xB6A8, Value::Double(-1.0)), + (0xB6F9, Value::Int(3)), + (0xB64C, Value::Ref(10)), + ]), + format(vec![ + (0xB647, Value::Double(2.0)), + (0xB64A, Value::Ref(9)), + (0xB64B, Value::Ref(10)), + (0xB6FA, Value::Int(5)), + (0xB64C, Value::Ref(10)), + ]), + ]; + let cell = Cell { + id: 1, + row: 0, + column: 0, + row_span: 1, + column_span: 2, + kind: CellKind::Text, + width: None, + runs: Vec::new(), + format: Some(0), + }; + let t = Table { + uid: 1, + style: None, + attrs: Attrs::default(), + right_to_left: false, + header_rows: 0, + footer_rows: 0, + rows: vec![Row { + height: None, + min_height: None, + attrs: Attrs::default(), + }], + columns: vec![10.0, 10.0], + cells: vec![cell.clone()], + formats, + grid: vec![vec![Some(0), Some(1)]], + }; + let mut got = w.cell_edge_attrs(&t, &cell); + got.sort(); + let want: Vec<(&str, String)> = vec![ + ("BottomEdgeStrokeColor", "Color/u10".into()), + ("LeftEdgeStrokeTint", "100".into()), + ("LeftEdgeStrokePriority", "3".into()), + ("RightEdgeStrokeColor", "Color/u10".into()), + ("RightEdgeStrokePriority", "1".into()), + ("TopEdgeStrokeColor", "Swatch/None".into()), + ("TopEdgeStrokeWeight", "0".into()), + ]; + let mut want = want; + want.sort(); + assert_eq!(got, want); + } + #[test] fn writes_bullet_char() { let Some(PropValue::Attributes(a)) = bullet_char(0, 0x2022) else { diff --git a/src/idml/kind.rs b/src/idml/kind.rs index 34ed139..8d83921 100644 --- a/src/idml/kind.rs +++ b/src/idml/kind.rs @@ -29,6 +29,8 @@ pub(super) enum Kind { Integer, /// A number, left out when 0. NonZero, + /// A cell edge stroke or gap tint: −1 is written as 100. + EdgeTint, /// The given value is written as the enumeration value, others as /// numbers of the given IDML type. NumberOr(f64, &'static str, &'static str), @@ -123,6 +125,9 @@ impl Writer<'_> { .filter(|&f| f != KERNING_NONE) .map(|f| text("unit", num(round(f * 1000.0)))), Kind::Integer => v.as_u32().map(|u| text("long", u.to_string())), + Kind::EdgeTint => { + number().map(|f| text("unit", num(if f == -1.0 { 100.0 } else { f }))) + } Kind::NonZero => v .as_u32() .filter(|&u| u != 0) @@ -162,6 +167,7 @@ impl Writer<'_> { Value::Words(CELL_NO_STROKE_TYPE, 0) => Some(text("object", "n".into())), Value::Words(code, 0) => STROKE_TYPES .iter() + .chain(TABLE_STROKE_TYPES) .find(|(k, _)| *k == code) .map(|(_, n)| text("object", format!("StrokeStyle/$ID/{n}"))), _ => None, diff --git a/src/idml/story.rs b/src/idml/story.rs index b171eb0..f319e0c 100644 --- a/src/idml/story.rs +++ b/src/idml/story.rs @@ -429,6 +429,9 @@ impl Writer<'_> { for (name, v) in self.text_cell_values(|a| t.cell_value(c, a, &styles)) { x.attr(name, v); } + for (name, v) in self.cell_edge_attrs(t, c) { + x.attr(name, v); + } if let Some(f) = t.format(c) { for (name, v) in self.cell_attrs(&f.attrs) { x.attr(name, v); diff --git a/src/idml/styles.rs b/src/idml/styles.rs index 8aaaaa3..341737a 100644 --- a/src/idml/styles.rs +++ b/src/idml/styles.rs @@ -113,7 +113,11 @@ impl Writer<'_> { .attr("Self", self.table_style_ref(tag, s)) .attr("Name", self.table_style_name(s)); let attrs = if tag == "CellStyle" { - self.cell_attrs(&s.attrs) + let mut a = self.cell_attrs(&s.attrs); + // The IDML schema has no `WritingDirection` on cell styles. + a.retain(|(n, _)| *n != "WritingDirection"); + a.extend(self.cell_style_edge_attrs(&s.attrs)); + a } else { self.table_attrs(&s.attrs) }; diff --git a/src/model/table.rs b/src/model/table.rs index 1641f3a..ae20764 100644 --- a/src/model/table.rs +++ b/src/model/table.rs @@ -560,6 +560,29 @@ impl Table { cell.format.and_then(|i| self.formats.get(i)) } + /// The attribute sets of the grid positions along each edge of a + /// cell, in the order left, right, top, bottom: the positions of its + /// first column, last column, first row and last row. `None` for a + /// position without a set. + pub fn edge_formats(&self, cell: &Cell) -> [Vec>; 4] { + let at = |r: usize, c: usize| { + self.grid + .get(r) + .and_then(|row| row.get(c).copied().flatten()) + .and_then(|i| self.formats.get(i)) + }; + let rows = cell.row..cell.row + cell.row_span; + let columns = cell.column..cell.column + cell.column_span; + let last_row = cell.row + cell.row_span - 1; + let last_column = cell.column + cell.column_span - 1; + [ + rows.clone().map(|r| at(r, cell.column)).collect(), + rows.map(|r| at(r, last_column)).collect(), + columns.clone().map(|c| at(cell.row, c)).collect(), + columns.map(|c| at(last_row, c)).collect(), + ] + } + /// The value in effect for the table: its own attributes, then its /// table style, the styles that is based on, and the root table /// style.