mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-10 04:09:53 -06:00
ODT: frames keep the page they are anchored to (gap 19) (#5705)
* ODT gap 19: frames keep the page they are anchored to A frame or shape anchored to a page was written without its text:anchor-page-number, so one on page 3 came back on page 1 in LibreOffice after a save. Keep the number and write it back. WordCraft's own layout still places such a frame on its anchor paragraph's page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ODT: a page number only counts on a page anchor, and groups keep theirs (gap 19) A paragraph anchor with a text:anchor-page-number was saved as a page anchor, which LibreOffice ignores the number on and so moved the drawing. A group anchored to a page lost its page. Found by CodeRabbit on this PR. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ODT: a frame moved in WordCraft loses its page number (gap 19) Nothing cleared it, so the save put the frame back on its old page. One placed by its paragraph is no page anchor any more, and one dropped on a page takes that page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ODT tests: put the moved frame's test with the other moves (gap 19) At the end of the file it collided with other ODT PRs' tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ODT: a frame moved onto its paragraph loses its page number (gap 19) The number was only hidden while the frame was placed by its paragraph, so placing it by the page again (arrange.position) brought back the old page. The merge of main also lost the test attribute on a test from main, put back here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Gap 19: docs status lines are main's, with this change in front Merging main had kept this branch's older status lines in ROADMAP.md and docs/file-format-parity.md, which set main's 2026-11-08 date back and dropped main's newer entries and the parity doc's CLI path note. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Gap 19: parity row says when a moved frame keeps its page number Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: FredApps <FredApps@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
FredApps
parent
52fbc7f068
commit
15b608660f
+3
-1
File diff suppressed because one or more lines are too long
@@ -186,6 +186,11 @@ pub struct Float {
|
||||
/// top. 0 = unknown, drawn in document order (objects inserted here, or from other formats).
|
||||
#[serde(skip_serializing_if = "is_zero_u32")]
|
||||
pub z: u32,
|
||||
/// The page a drawing anchored to a page is on, from 1 (ODF's `text:anchor-page-number`,
|
||||
/// §19.872); 0 = anchored to its paragraph or not said. Kept for saving: the layout places a
|
||||
/// drawing on its anchor paragraph's page.
|
||||
#[serde(skip_serializing_if = "is_zero_u32")]
|
||||
pub page: u32,
|
||||
/// Turned, but the file keeps no room for the turn (`wp:effectExtent` missing or zero, as
|
||||
/// files from other tools have): Word then keeps an inline one's line to its frame and lets
|
||||
/// the turned object overhang it (see [`Float::line_pad`]). Cleared by [`Float::set_spin`].
|
||||
|
||||
@@ -185,6 +185,10 @@ fn move_object_once(s: &mut Session, pos: &Pos, page: usize, x: f32, y: f32, (w,
|
||||
if f.wrap == Wrap::Inline {
|
||||
f.wrap = Wrap::Square;
|
||||
}
|
||||
// A drawing anchored to a page is on this one now.
|
||||
if f.page > 0 {
|
||||
f.page = u32::try_from(page).map_or(f.page, |p| p.saturating_add(1));
|
||||
}
|
||||
f.h_rel = Anchor::Page;
|
||||
f.v_rel = Anchor::Page;
|
||||
f.h_align = None;
|
||||
@@ -199,6 +203,8 @@ fn move_object_once(s: &mut Session, pos: &Pos, page: usize, x: f32, y: f32, (w,
|
||||
s.touch();
|
||||
let origin = s.layout().object(&at, page).ok_or_else(|| CmdError::Failed("the object isn't laid out".into()))?.origin;
|
||||
edit_float(s, &at, |f| {
|
||||
// On its paragraph now: a page it was anchored to is gone, not only hidden.
|
||||
f.page = 0;
|
||||
f.h_rel = Anchor::Column;
|
||||
f.v_rel = Anchor::Paragraph;
|
||||
f.x = (x - origin.x).clamp(-MAX_OFFSET, MAX_OFFSET);
|
||||
|
||||
@@ -5218,6 +5218,42 @@ fn arrange_bounds_resizes_and_moves_objects() {
|
||||
assert_eq!(box_area(&mut s).rect, n);
|
||||
}
|
||||
|
||||
/// A frame anchored to a page and moved in WordCraft is placed by its paragraph from then on: the
|
||||
/// file's page number goes, or the save would put it back. Only resized, it stays on that page.
|
||||
#[test]
|
||||
fn an_odt_frame_moved_from_its_page_loses_the_page_number() {
|
||||
let ns = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0""#;
|
||||
let content = format!(
|
||||
r#"<office:document-content {ns}><office:body><office:text><draw:frame text:anchor-type="page" text:anchor-page-number="3" svg:x="5cm" svg:y="5cm" svg:width="4cm" svg:height="2cm"><draw:text-box><text:p>On it</text:p></draw:text-box></draw:frame><text:p>One</text:p><text:p>Two</text:p></office:text></office:body></office:document-content>"#
|
||||
);
|
||||
let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||
for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] {
|
||||
z.start_file(name, o).unwrap();
|
||||
std::io::Write::write_all(&mut z, data.as_bytes()).unwrap();
|
||||
}
|
||||
let odt = z.finish().unwrap().into_inner();
|
||||
let frame_tag = |s: &Session| {
|
||||
let mut a = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_formats::odt::export(&s.doc).unwrap())).unwrap();
|
||||
let mut c = String::new();
|
||||
std::io::Read::read_to_string(&mut a.by_name("content.xml").unwrap(), &mut c).unwrap();
|
||||
c.split("<draw:frame").nth(1).and_then(|t| t.split('>').next()).unwrap_or_default().to_string()
|
||||
};
|
||||
let mut s = Session::new(crate::io::open_bytes("t.odt", &odt).unwrap());
|
||||
select_box(&mut s);
|
||||
run(&mut s, "arrange.bounds", json!({"width": 150, "height": 80}));
|
||||
assert!(frame_tag(&s).contains(r#"text:anchor-page-number="3""#), "resized: {}", frame_tag(&s));
|
||||
let mut s = Session::new(crate::io::open_bytes("t.odt", &odt).unwrap());
|
||||
select_box(&mut s);
|
||||
run(&mut s, "arrange.bounds", json!({"page": 0, "x": 200, "y": 300}));
|
||||
let tag = frame_tag(&s);
|
||||
assert!(!tag.contains("anchor-page-number") && !tag.contains(r#"text:anchor-type="page""#), "moved: {tag}");
|
||||
// Moved, then placed by the margins: the file's page number is gone, not only hidden.
|
||||
run(&mut s, "arrange.position", json!({"preset": "topLeft"}));
|
||||
let tag = frame_tag(&s);
|
||||
assert!(!tag.contains("anchor-page-number"), "moved, then placed: {tag}");
|
||||
}
|
||||
|
||||
/// An object dropped near the bottom of a page lands there, though at the top of the paragraph
|
||||
/// it anchors to it would push that paragraph onto the next page (#3169).
|
||||
#[test]
|
||||
|
||||
@@ -1259,6 +1259,7 @@ impl Body<'_> {
|
||||
if self.frame.is_none() && self.note.is_none() && self.del.is_none() && !self.in_tc {
|
||||
self.group = Some(OpenGroup {
|
||||
anchor: get(a, "text:anchor-type").map(String::from),
|
||||
page: get(a, "text:anchor-page-number").map(String::from),
|
||||
style: get(a, "draw:style-name").map(String::from),
|
||||
members: Vec::new(),
|
||||
});
|
||||
|
||||
@@ -61,9 +61,12 @@ pub(in crate::odt) fn read_formula(xml: &[u8]) -> Option<(String, bool)> {
|
||||
|
||||
/// A frame being read: its width and height, picture bytes, alt text and the equation read from
|
||||
/// an embedded formula object.
|
||||
/// A group being read (ODF 1.3 §10.3.15 `draw:g`): its anchor, its style, and its members so far.
|
||||
/// A group being read (ODF 1.3 §10.3.15 `draw:g`): its anchor and page, its style, and its members
|
||||
/// so far.
|
||||
pub(in crate::odt) struct OpenGroup {
|
||||
pub(in crate::odt) anchor: Option<String>,
|
||||
/// Its `text:anchor-page-number`.
|
||||
pub(in crate::odt) page: Option<String>,
|
||||
pub(in crate::odt) style: Option<String>,
|
||||
pub(in crate::odt) members: Vec<model::Member>,
|
||||
}
|
||||
@@ -338,7 +341,9 @@ impl Body<'_> {
|
||||
}
|
||||
let members = g.members.into_iter().map(|m| model::Member { x: fin(m.x) - x0, y: fin(m.y) - y0, ..m }).collect();
|
||||
let graphic = self.styles.shape_graphic(g.style.as_deref());
|
||||
let float = crate::odt::frames::float_of(g.anchor.as_deref(), Some(&crate::odt::writer::pt(x0)), Some(&crate::odt::writer::pt(y0)), &graphic);
|
||||
let mut float =
|
||||
crate::odt::frames::float_of(g.anchor.as_deref(), Some(&crate::odt::writer::pt(x0)), Some(&crate::odt::writer::pt(y0)), &graphic);
|
||||
float.page = crate::odt::frames::page_of(g.anchor.as_deref(), g.page.as_deref(), &graphic);
|
||||
let float = (float.wrap != wordcraft_doc::para::Wrap::Inline).then(|| Box::new(float));
|
||||
let fit = |v: f32| if v.is_finite() { v.clamp(1.0, MAX_LEN) } else { 1.0 };
|
||||
self.push_inline(Inline::Group { w: fit(x1 - x0), h: fit(y1 - y0), float, members });
|
||||
@@ -358,6 +363,7 @@ impl Body<'_> {
|
||||
let at = |v: Option<f32>| v.map(crate::odt::writer::pt);
|
||||
let mut f = crate::odt::frames::float_of(get(a, "text:anchor-type"), at(x).as_deref(), at(y).as_deref(), &g);
|
||||
f.z = crate::odt::frames::z_of(a);
|
||||
f.page = crate::odt::frames::page_of(get(a, "text:anchor-type"), get(a, "text:anchor-page-number"), &g);
|
||||
f
|
||||
} else {
|
||||
crate::odt::frames::place(a, w, h, &g, self.text_size())
|
||||
|
||||
@@ -417,12 +417,26 @@ pub(super) fn place(a: &Attrs, w: f32, h: f32, g: &Graphic, size: f32) -> Float
|
||||
f.rot = rot;
|
||||
}
|
||||
f.z = z_of(a);
|
||||
f.page = page_of(get(a, "text:anchor-type"), get(a, "text:anchor-page-number"), g);
|
||||
if f.wrap == Wrap::Inline {
|
||||
f.y = inline_raise(g, y.as_deref().and_then(length), h, size);
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
/// The page a drawing anchored as `anchor` (or as its style says) is on: `number`, its
|
||||
/// `text:anchor-page-number` (§19.872). 0 when it states none, or isn't anchored to a page, where
|
||||
/// LibreOffice ignores the number.
|
||||
pub(super) fn page_of(anchor: Option<&str>, number: Option<&str>, g: &Graphic) -> u32 {
|
||||
if anchor.or(g.anchor.as_deref()).map(str::trim) != Some("page") {
|
||||
return 0;
|
||||
}
|
||||
number.and_then(|v| v.trim().parse::<u32>().ok()).unwrap_or(0).min(MAX_ANCHOR_PAGE)
|
||||
}
|
||||
|
||||
/// Highest anchor page kept.
|
||||
const MAX_ANCHOR_PAGE: u32 = 1_000_000;
|
||||
|
||||
/// A drawing's place in the stacking order (§19.231 `draw:z-index`, from 0 at the bottom) as
|
||||
/// WordCraft's [`Float::z`] (higher on top, 0 for unknown): one more.
|
||||
pub(super) fn z_of(a: &Attrs) -> u32 {
|
||||
@@ -564,7 +578,10 @@ pub(super) fn turned(f: &Float) -> bool {
|
||||
/// `style:graphic-properties` attributes, and the frame's `svg:x`/`svg:y` attributes, or its
|
||||
/// `draw:transform` when it is turned (`w` × `h` its size).
|
||||
pub(super) fn frame_of(f: &Float, w: f32, h: f32) -> (&'static str, String, String) {
|
||||
let anchor = if f.h_rel == Anchor::Page && f.v_rel == Anchor::Page {
|
||||
// A page number holds until it is placed by its paragraph: moved in WordCraft.
|
||||
let by_paragraph =
|
||||
matches!(f.h_rel, Anchor::Column | Anchor::Character) || matches!(f.v_rel, Anchor::Paragraph | Anchor::Line | Anchor::Character);
|
||||
let anchor = if (f.page > 0 && !by_paragraph) || (f.h_rel == Anchor::Page && f.v_rel == Anchor::Page) {
|
||||
"page"
|
||||
} else if f.v_rel == Anchor::Line {
|
||||
"char"
|
||||
|
||||
@@ -368,6 +368,65 @@ fn a_frame_keeps_its_place_in_the_stacking_order() {
|
||||
assert!(crate::odt::tests::entry(&crate::odt::tests::export_model(&d).unwrap(), "content.xml").contains(r#"draw:z-index="0""#));
|
||||
}
|
||||
|
||||
#[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
|
||||
// came back on page 1 in LibreOffice.
|
||||
let frame = |attrs: &str| {
|
||||
let content = format!(
|
||||
r#"<office:document-content {NS_T} {NS_D}><office:body><office:text><draw:frame {attrs} svg:x="5cm" svg:y="5cm" svg:width="4cm" svg:height="2cm"><draw:text-box><text:p>On it</text:p></draw:text-box></draw:frame><text:p>One</text:p></office:text></office:body></office:document-content>"#
|
||||
);
|
||||
import(&crate::odt::tests::package(&content, None)).unwrap()
|
||||
};
|
||||
let d = frame(r#"text:anchor-type="page" text:anchor-page-number="3""#);
|
||||
let c = crate::odt::tests::content_of(&crate::odt::tests::export_model(&d).unwrap());
|
||||
assert!(c.contains(r#"text:anchor-type="page" text:anchor-page-number="3""#), "{c}");
|
||||
let back =
|
||||
crate::odt::tests::content_of(&crate::odt::tests::export_model(&import(&crate::odt::tests::export_model(&d).unwrap()).unwrap()).unwrap());
|
||||
assert!(back.contains(r#"text:anchor-page-number="3""#), "again: {back}");
|
||||
// A frame anchored to its paragraph names no page, and junk is none.
|
||||
let c = crate::odt::tests::content_of(&crate::odt::tests::export_model(&frame(r#"text:anchor-type="paragraph""#)).unwrap());
|
||||
assert!(!c.contains("anchor-page-number"), "{c}");
|
||||
for junk in ["-1", "abc", "", "99999999999999"] {
|
||||
let d = frame(&format!(r#"text:anchor-type="page" text:anchor-page-number="{junk}""#));
|
||||
let c = crate::odt::tests::content_of(&crate::odt::tests::export_model(&d).unwrap());
|
||||
assert!(!c.contains("anchor-page-number"), "{junk}: {c}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A page number only says something on a page anchor. On another LibreOffice ignores it, so it
|
||||
/// doesn't make a drawing anchored to a page when saved.
|
||||
#[test]
|
||||
fn a_page_number_on_another_anchor_is_no_page_anchor() {
|
||||
let saved = |styles: &str, body: &str| {
|
||||
let content = format!(
|
||||
r#"<office:document-content {NS_T} {NS_D}><office:automatic-styles>{styles}</office:automatic-styles><office:body><office:text>{body}</office:text></office:body></office:document-content>"#
|
||||
);
|
||||
crate::odt::tests::content_of(&crate::odt::tests::export_model(&import(&crate::odt::tests::package(&content, None)).unwrap()).unwrap())
|
||||
};
|
||||
let frame = |attrs: &str| {
|
||||
format!(
|
||||
r#"<text:p>One<draw:frame {attrs} svg:x="5cm" svg:y="5cm" svg:width="4cm" svg:height="2cm"><draw:text-box><text:p>On it</text:p></draw:text-box></draw:frame></text:p>"#
|
||||
)
|
||||
};
|
||||
for anchor in ["paragraph", "char"] {
|
||||
let c = saved("", &frame(&format!(r#"text:anchor-type="{anchor}" text:anchor-page-number="3""#)));
|
||||
assert!(!c.contains(r#"text:anchor-type="page""#) && !c.contains("anchor-page-number"), "{anchor}: {c}");
|
||||
}
|
||||
let c = saved(
|
||||
"",
|
||||
r#"<text:p>One<draw:line text:anchor-type="paragraph" text:anchor-page-number="3" svg:x1="1cm" svg:y1="1cm" svg:x2="3cm" svg:y2="3cm"/></text:p>"#,
|
||||
);
|
||||
assert!(!c.contains(r#"text:anchor-type="page""#) && !c.contains("anchor-page-number"), "a line: {c}");
|
||||
// An anchor its style gives counts as one the frame states.
|
||||
let by_style = r#"<style:style style:name="fr" style:family="graphic"><style:graphic-properties text:anchor-type="page"/></style:style>"#;
|
||||
let c = saved(by_style, &frame(r#"draw:style-name="fr" text:anchor-page-number="3""#));
|
||||
assert!(c.contains(r#"text:anchor-type="page" text:anchor-page-number="3""#), "{c}");
|
||||
let by_style = r#"<style:style style:name="fr" style:family="graphic"><style:graphic-properties text:anchor-type="paragraph"/></style:style>"#;
|
||||
let c = saved(by_style, &frame(r#"draw:style-name="fr" text:anchor-page-number="3""#));
|
||||
assert!(!c.contains("anchor-page-number"), "{c}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_frame_offering_several_formats_shows_the_first_it_can_read() {
|
||||
// LibreOffice's letter templates: its own metafile, then a PNG. Taking the first left nothing
|
||||
|
||||
@@ -152,3 +152,23 @@ fn a_diagram_is_saved_as_a_group_of_its_drawing() {
|
||||
assert!(kinds.contains(&ShapeKind::RoundedRectangle) && kinds.contains(&ShapeKind::Arrow), "{kinds:?}");
|
||||
assert!(members.iter().any(|m| m.4 == "Plan"), "its text: {members:?}");
|
||||
}
|
||||
|
||||
/// A group anchored to a page stays on that page, and a number on another anchor says nothing.
|
||||
#[test]
|
||||
fn a_group_anchored_to_a_page_stays_on_that_page() {
|
||||
let saved = |attrs: &str| {
|
||||
let content = format!(
|
||||
r#"<office:document-content {NS_T} {NS_D}><office:body><office:text><draw:g {attrs}><draw:rect svg:x="3cm" svg:y="3cm" svg:width="2cm" svg:height="2cm"/><draw:rect svg:x="6cm" svg:y="3cm" svg:width="2cm" svg:height="2cm"/></draw:g><text:p>One</text:p></office:text></office:body></office:document-content>"#
|
||||
);
|
||||
let d = import(&package(&content, None)).unwrap();
|
||||
crate::odt::tests::export_model(&d).unwrap()
|
||||
};
|
||||
let on_two = saved(r#"text:anchor-type="page" text:anchor-page-number="2""#);
|
||||
let c = content_of(&on_two);
|
||||
assert!(c.contains(r#"text:anchor-type="page" text:anchor-page-number="2""#), "{c}");
|
||||
let back = import(&on_two).unwrap();
|
||||
let again = content_of(&crate::odt::tests::export_model(&back).unwrap());
|
||||
assert!(again.contains(r#"text:anchor-page-number="2""#), "again: {again}");
|
||||
let c = content_of(&saved(r#"text:anchor-type="paragraph" text:anchor-page-number="2""#));
|
||||
assert!(!c.contains(r#"text:anchor-type="page""#) && !c.contains("anchor-page-number"), "{c}");
|
||||
}
|
||||
|
||||
@@ -114,7 +114,8 @@ pub(in crate::odt) fn placement(place: Place<'_>, w: f32, h: f32) -> (String, St
|
||||
match place {
|
||||
Place::Text(Some(f)) if f.wrap != wordcraft_doc::para::Wrap::Inline => {
|
||||
let (a, props, xy) = crate::odt::frames::frame_of(f, w, h);
|
||||
(anchor(a), props, xy)
|
||||
let page = if a == "page" && f.page > 0 { format!(" text:anchor-page-number=\"{}\"", f.page) } else { String::new() };
|
||||
(format!("{}{page}", anchor(a)), props, xy)
|
||||
}
|
||||
Place::Text(f) => {
|
||||
let turn = f.filter(|f| crate::odt::frames::turned(f)).map(|f| crate::odt::frames::transform_attr(0.0, 0.0, w, h, f.rot));
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user