mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
craft-odf: an object without a stored stacking order is written above every stored one (wordcraft#2638)
This commit is contained in:
1 parent
5f453efa9a
commit
f8388e2293
6 files changed
+58
-12
No files matched your search
@@ -443,9 +443,20 @@ pub(super) fn z_of(a: &Attrs) -> u32 {
|
||||
get(a, "draw:z-index").and_then(|v| v.trim().parse::<u32>().ok()).map_or(0, |z| z.saturating_add(1))
|
||||
}
|
||||
|
||||
/// `float`'s place in the stacking order as `draw:z-index` (see [`z_of`]): 0 when unknown.
|
||||
pub(crate) fn z_index(float: Option<&Float>) -> u32 {
|
||||
float.map_or(0, |f| f.z.saturating_sub(1))
|
||||
/// `float`'s place in the stacking order as `draw:z-index` (see [`z_of`]). One without a stored
|
||||
/// order (made in WordCraft, or nothing to place) takes `unset`, which the writer puts above every
|
||||
/// stored order, where [`Float::z`] stacks it.
|
||||
pub(crate) fn z_index(float: Option<&Float>, unset: u32) -> u32 {
|
||||
match float.map(|f| f.z) {
|
||||
Some(z) if z > 0 => z - 1,
|
||||
_ => unset,
|
||||
}
|
||||
}
|
||||
|
||||
/// The `draw:z-index` the objects of `doc` without a stored order take: past the highest stored
|
||||
/// one (a stored order is one more than its `draw:z-index`), 0 when none has one.
|
||||
pub(crate) fn unset_z_index(doc: &craft_wordmodel::Document) -> u32 {
|
||||
doc.stacking_orders().0.last().copied().unwrap_or(0)
|
||||
}
|
||||
|
||||
/// How far a frame `h` points tall in line with the text is raised from standing on the
|
||||
|
||||
@@ -1038,6 +1038,7 @@ fn export_as(doc: &Document, mime: &str) -> Result<Vec<u8>, String> {
|
||||
// Paragraphs kept as read name their own automatic styles: WordCraft's take another prefix.
|
||||
let kept = doc.kept.of("odt");
|
||||
let mut w = if kept.is_some() { Writer::with_prefix(KEPT_PREFIX) } else { Writer::default() };
|
||||
w.unset_z = super::frames::unset_z_index(doc);
|
||||
w.note_numbers = Some([false, true].map(|endnote| {
|
||||
let o = doc.note_options(&doc.last_section, endnote);
|
||||
(o.num_fmt, o.num_start)
|
||||
@@ -1166,6 +1167,7 @@ fn export_as(doc: &Document, mime: &str) -> Result<Vec<u8>, String> {
|
||||
let mut hw = Writer::with_prefix(if kept.is_some() { "wcM" } else { "M" });
|
||||
// Headers' and footers' new pictures and objects skip the kept files' names too.
|
||||
hw.reserved = std::sync::Arc::clone(&w.reserved);
|
||||
hw.unset_z = w.unset_z;
|
||||
hw.accents = accents;
|
||||
hw.variables.clone_from(&w.variables);
|
||||
let parts: Vec<PageParts> = pages.iter().map(|(_, sec)| page_parts(doc, sec, &mut hw)).collect();
|
||||
|
||||
@@ -362,12 +362,41 @@ fn a_frame_keeps_its_place_in_the_stacking_order() {
|
||||
let saved = crate::text::tests::export_model(&d).unwrap();
|
||||
assert!(crate::text::tests::entry(&saved, "content.xml").contains(r#"draw:z-index="3""#));
|
||||
assert_eq!(float(&import(&saved).unwrap()).z, 4);
|
||||
// Unknown (inserted in WordCraft): at the bottom, in document order.
|
||||
// Unknown (inserted in WordCraft), with nothing else stored: 0.
|
||||
let d = open(r#"style:wrap="run-through""#, r#"text:anchor-type="paragraph""#);
|
||||
assert_eq!(float(&d).z, 0);
|
||||
assert!(crate::text::tests::entry(&crate::text::tests::export_model(&d).unwrap(), "content.xml").contains(r#"draw:z-index="0""#));
|
||||
}
|
||||
|
||||
/// An object without a stored order (made in WordCraft) stacks above every object that has one
|
||||
/// (`Float::z`), so it is written above them, not at the bottom.
|
||||
#[test]
|
||||
fn an_object_made_here_is_written_above_the_stored_orders() {
|
||||
let png = [b"\x89PNG\r\n\x1a\n".as_slice(), &[7u8; 64]].concat();
|
||||
let frame = |z: &str| {
|
||||
format!(
|
||||
r#"<draw:frame draw:style-name="fr1" svg:width="2in" svg:height="1in" text:anchor-type="paragraph"{z}><draw:image xlink:href="Pictures/a.png"/></draw:frame>"#
|
||||
)
|
||||
};
|
||||
let content = format!(
|
||||
r#"<office:document-content {NS_T} {NS_D}><office:automatic-styles><style:style style:name="fr1" style:family="graphic"><style:graphic-properties style:wrap="run-through"/></style:style></office:automatic-styles><office:body><office:text><text:p>{}{}Text beside.</text:p></office:text></office:body></office:document-content>"#,
|
||||
frame(r#" draw:z-index="3""#),
|
||||
frame("")
|
||||
);
|
||||
let d = import(&zip_of(&[("content.xml", content.as_bytes()), ("Pictures/a.png", &png)])).unwrap();
|
||||
let zs: Vec<u32> = d.body[0]
|
||||
.as_para()
|
||||
.unwrap()
|
||||
.objects
|
||||
.iter()
|
||||
.filter_map(|o| if let InlineObject::Image { float, .. } = o { Some(float.z) } else { None })
|
||||
.collect();
|
||||
assert_eq!(zs, [4, 0], "one stored, one made here");
|
||||
let content = crate::text::tests::entry(&crate::text::tests::export_model(&d).unwrap(), "content.xml");
|
||||
let z: Vec<&str> = content.split(r#"draw:z-index=""#).skip(1).filter_map(|s| s.split('"').next()).collect();
|
||||
assert_eq!(z, ["3", "4"], "{content}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_frame_anchored_to_a_page_stays_on_that_page() {
|
||||
// LibreOffice's page-anchored frames name their page. Written without it, a frame on page 3
|
||||
|
||||
@@ -176,6 +176,8 @@ pub(super) struct Writer {
|
||||
pub(super) named_text: std::collections::BTreeSet<String>,
|
||||
/// Text boxes written so far (their frames' names).
|
||||
boxes: usize,
|
||||
/// The `draw:z-index` of objects without a stored stacking order (see `frames::unset_z_index`).
|
||||
pub(super) unset_z: u32,
|
||||
/// Notes written so far (their ids), and footnotes and endnotes among them (their numbers).
|
||||
notes: usize,
|
||||
footnotes: u32,
|
||||
|
||||
@@ -200,7 +200,7 @@ impl Writer {
|
||||
// Always placed by a style (see `image_frame`).
|
||||
let (anchor, props, xy) = placement(place, c.w, c.h);
|
||||
let (anchor, style) = (anchor, format!(" draw:style-name=\"{}\"{xy}", self.graphic_style(props)));
|
||||
let z = place.z_index();
|
||||
let z = place.z_index(self.unset_z);
|
||||
out.push_str(&format!(
|
||||
"<draw:frame draw:name=\"{}Chart{n}\"{style}{anchor} svg:width=\"{}\" svg:height=\"{}\" draw:z-index=\"{z}\">{inner}",
|
||||
self.prefix,
|
||||
|
||||
@@ -70,9 +70,10 @@ pub(in crate::text) enum Place<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Place<'a> {
|
||||
/// Its place in the stacking order (§19.231 `draw:z-index`, see `frames::z_of`).
|
||||
pub(in crate::text) fn z_index(self) -> u32 {
|
||||
crate::text::frames::z_index(self.float())
|
||||
/// Its place in the stacking order (§19.231 `draw:z-index`, see `frames::z_of`), `unset`
|
||||
/// without a stored one.
|
||||
pub(in crate::text) fn z_index(self, unset: u32) -> u32 {
|
||||
crate::text::frames::z_index(self.float(), unset)
|
||||
}
|
||||
|
||||
/// What turns or mirrors the object, and places it in the text.
|
||||
@@ -323,7 +324,7 @@ impl Writer {
|
||||
let (anchor, props, xy) = placement(place, img.w, img.h);
|
||||
let (anchor, style) =
|
||||
(anchor, format!(" draw:style-name=\"{}\"{xy}", self.graphic_style(format!("{}{props}{clip}", super::GRAPHICS_PARENT))));
|
||||
let z = place.z_index();
|
||||
let z = place.z_index(self.unset_z);
|
||||
out.push_str(&format!(
|
||||
"<draw:frame draw:name=\"{}Image{n}\"{style}{anchor} svg:width=\"{}\" svg:height=\"{}\" draw:z-index=\"{z}\">{inner}",
|
||||
self.prefix,
|
||||
@@ -360,7 +361,8 @@ impl Writer {
|
||||
let (anchor, props, _) = placement(Place::Text(float.filter(|f| f.wrap != craft_wordmodel::para::Wrap::Inline)), size.0, size.1);
|
||||
let style = self.graphic_style(props);
|
||||
self.boxes += 1;
|
||||
out.push_str(&format!("<draw:g draw:name=\"{}Group{}\" draw:style-name=\"{style}\"{anchor} draw:z-index=\"0\">", self.prefix, self.boxes));
|
||||
let z = crate::text::frames::z_index(float, self.unset_z);
|
||||
out.push_str(&format!("<draw:g draw:name=\"{}Group{}\" draw:style-name=\"{style}\"{anchor} draw:z-index=\"{z}\">", self.prefix, self.boxes));
|
||||
let at = Place::Text(float).origin();
|
||||
self.members(members, at, 0, out);
|
||||
out.push_str("</draw:g>");
|
||||
@@ -419,7 +421,7 @@ impl Writer {
|
||||
.and_then(|c| c.next.as_deref())
|
||||
.map(|n| format!(" draw:chain-next-name=\"{}{}\"", self.prefix, x(n)))
|
||||
.unwrap_or_default();
|
||||
let z = place.z_index();
|
||||
let z = place.z_index(self.unset_z);
|
||||
out.push_str(&format!(
|
||||
"<draw:frame draw:name=\"{}\" draw:style-name=\"{style}\"{anchor} svg:width=\"{}\"{height}{xy} draw:z-index=\"{z}\"><draw:text-box{min}{next}>{body}</draw:text-box></draw:frame>",
|
||||
x(&name),
|
||||
@@ -522,7 +524,7 @@ impl Writer {
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let conn = self.conn_attrs(kind, look);
|
||||
let z = place.z_index();
|
||||
let z = place.z_index(self.unset_z);
|
||||
out.push_str(&format!(
|
||||
"<{element} draw:name=\"{}Shape{}\" draw:style-name=\"{style}\"{anchor}{at}{conn} draw:z-index=\"{z}\">{body}{geometry}</{element}>",
|
||||
self.prefix, self.boxes,
|
||||
|
||||
Reference in new issue
Block a user