mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
docx: preserve partial table margins in current and tracked properties (#1924)
This commit is contained in:
1 parent
136c34ae4c
commit
443a2b05ff
5 files changed
+89
-1
No files matched your search
@@ -562,6 +562,17 @@ pub fn margins(e: &El) -> [f32; 4] {
|
||||
margin_sides(e).map(|side| side.unwrap_or(0.0))
|
||||
}
|
||||
|
||||
/// Word's own cell margins (the Normal Table style): 0.08" left and right, none above or below.
|
||||
pub const WORD_CELL_MARGINS: [f32; 4] = [0.0, 5.4, 0.0, 5.4];
|
||||
|
||||
/// A table's `w:tblCellMar` over the margins it would have without one (its
|
||||
/// style's, else Word's). A side the element leaves out keeps the underlying margin, as in Word:
|
||||
/// a form table that sets only `w:left` keeps its style's right margin, not none.
|
||||
pub fn margins_over(e: &El, base: [f32; 4]) -> [f32; 4] {
|
||||
let sides = margin_sides(e);
|
||||
std::array::from_fn(|i| sides.get(i).copied().flatten().or_else(|| base.get(i).copied()).unwrap_or(0.0))
|
||||
}
|
||||
|
||||
fn margin_sides(e: &El) -> [Option<f32>; 4] {
|
||||
let get = |names: &[&str]| names.iter().find_map(|n| e.child(n)).and_then(|c| tw(c, "w:w")).map(|v| v.clamp(0.0, 1584.0));
|
||||
[get(&["w:top"]), get(&["w:left", "w:start"]), get(&["w:bottom"]), get(&["w:right", "w:end"])]
|
||||
|
||||
@@ -333,12 +333,37 @@ impl Reader<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Omitted table-margin sides inherit from that property set's style.
|
||||
fn table_margin_base(&self, style: Option<&str>) -> [f32; 4] {
|
||||
style.and_then(|id| self.doc.styles.table_style(id)).and_then(|st| st.parts.cell_margins).unwrap_or(if self.no_default_table_style {
|
||||
[0.0; 4]
|
||||
} else {
|
||||
crate::read::props::WORD_CELL_MARGINS
|
||||
})
|
||||
}
|
||||
|
||||
fn read_table(&mut self, sc: &mut StoryCtx, t: &El, rels: &Rels, depth: usize) -> Option<Table> {
|
||||
let mut table = Table::default();
|
||||
if let Some(p) = t.child("w:tblPr") {
|
||||
table.props = self.pc.tblpr(p);
|
||||
// Sides `w:tblCellMar` leaves out keep the style's margins; without a style, Word's
|
||||
// 0.08" left and right, or none in a file with no default table style.
|
||||
if let Some(m) = p.child("w:tblCellMar") {
|
||||
let base = self.table_margin_base(table.props.style.as_deref());
|
||||
table.props.cell_margins = Some(crate::read::props::margins_over(m, base));
|
||||
}
|
||||
if let Some(ch) = p.child("w:tblPrChange") {
|
||||
let mut old = ch.child("w:tblPr").map(|o| self.pc.tblpr(o)).unwrap_or_default();
|
||||
let mut old = ch
|
||||
.child("w:tblPr")
|
||||
.map(|o| {
|
||||
let mut props = self.pc.tblpr(o);
|
||||
if let Some(m) = o.child("w:tblCellMar") {
|
||||
let base = self.table_margin_base(props.style.as_deref());
|
||||
props.cell_margins = Some(crate::read::props::margins_over(m, base));
|
||||
}
|
||||
props
|
||||
})
|
||||
.unwrap_or_default();
|
||||
// Rejecting the change brings back these properties: with the same margins.
|
||||
self.no_default_margins(&mut old);
|
||||
table.props.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, ch), old);
|
||||
|
||||
@@ -98,3 +98,18 @@ fn no_default_table_style_means_no_cell_margins() {
|
||||
let reopened = craft_docx::read(&bytes).unwrap();
|
||||
assert_eq!(reopened.body[0].as_table().unwrap().props.cell_margins, Some([0.0; 4]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracked_partial_table_margins_keep_the_old_styles_other_sides() {
|
||||
let body = r#"<w:tbl><w:tblPr><w:tblStyle w:val="Current"/><w:tblCellMar><w:left w:w="100" w:type="dxa"/></w:tblCellMar><w:tblPrChange w:id="1" w:author="A"><w:tblPr><w:tblStyle w:val="Old"/><w:tblCellMar><w:left w:w="0" w:type="dxa"/></w:tblCellMar></w:tblPr></w:tblPrChange></w:tblPr><w:tblGrid><w:gridCol w:w="4320"/></w:tblGrid><w:tr><w:tc><w:p/></w:tc></w:tr></w:tbl>"#;
|
||||
let styles = r#"<w:style w:type="table" w:styleId="Current"><w:name w:val="Current"/><w:tblPr><w:tblCellMar><w:top w:w="60"/><w:right w:w="180"/></w:tblCellMar></w:tblPr></w:style><w:style w:type="table" w:styleId="Old"><w:name w:val="Old"/><w:tblPr><w:tblCellMar><w:top w:w="20"/><w:left w:w="108"/><w:bottom w:w="40"/><w:right w:w="160"/></w:tblCellMar></w:tblPr></w:style>"#;
|
||||
let doc = craft_docx::read(&package(body, Some(styles))).unwrap();
|
||||
let table = doc.body[0].as_table().unwrap();
|
||||
assert_eq!(table.props.cell_margins, Some([3.0, 5.0, 0.0, 9.0]));
|
||||
let old = &table.props.fmt_change.as_ref().unwrap().old;
|
||||
assert_eq!(
|
||||
old.cell_margins,
|
||||
Some([1.0, 0.0, 2.0, 8.0]),
|
||||
"explicit zero overrides left, omitted sides come from the old style, not the current one"
|
||||
);
|
||||
}
|
||||
@@ -29,6 +29,7 @@ mod malformed;
|
||||
mod missing_pictures;
|
||||
mod nested_block_controls;
|
||||
mod note_comments;
|
||||
mod partial_table_margins;
|
||||
mod passthrough;
|
||||
mod picture_links;
|
||||
mod positional_tabs;
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//! a table's partial cell margins keep the other sides.
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
#[test]
|
||||
fn a_left_only_table_margin_keeps_the_other_sides() {
|
||||
let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#;
|
||||
let root = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="r1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>"#;
|
||||
let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="s1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#;
|
||||
let styles = format!(
|
||||
r#"<w:styles {ns}><w:style w:type="table" w:styleId="Grid"><w:name w:val="Table Grid"/><w:tblPr><w:tblCellMar><w:top w:w="20" w:type="dxa"/><w:left w:w="108" w:type="dxa"/><w:bottom w:w="40" w:type="dxa"/><w:right w:w="160" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style></w:styles>"#
|
||||
);
|
||||
let table = |style: &str| {
|
||||
format!(
|
||||
r#"<w:tbl><w:tblPr>{style}<w:tblCellMar><w:left w:w="107" w:type="dxa"/></w:tblCellMar></w:tblPr><w:tblGrid><w:gridCol w:w="4320"/></w:tblGrid><w:tr><w:tc><w:p><w:r><w:t>Cell</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p/>"#
|
||||
)
|
||||
};
|
||||
let document = format!(r#"<w:document {ns}><w:body>{}{}</w:body></w:document>"#, table(r#"<w:tblStyle w:val="Grid"/>"#), table(""));
|
||||
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
for (name, data) in [
|
||||
("_rels/.rels", root),
|
||||
("word/_rels/document.xml.rels", rels),
|
||||
("word/document.xml", document.as_str()),
|
||||
("word/styles.xml", styles.as_str()),
|
||||
] {
|
||||
zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap();
|
||||
zip.write_all(data.as_bytes()).unwrap();
|
||||
}
|
||||
let doc = craft_docx::read(&zip.finish().unwrap().into_inner()).unwrap();
|
||||
let margins: Vec<_> = doc.body.iter().filter_map(|b| b.as_table()).map(|t| t.props.cell_margins).collect();
|
||||
assert_eq!(
|
||||
margins,
|
||||
vec![Some([1.0, 5.35, 2.0, 8.0]), Some([0.0, 5.35, 0.0, 0.0])],
|
||||
"the left side is the table's, the rest the style's (none without one: the file has no default table style)"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user