odox-core 0.4.2

Read and write OpenDocument text, spreadsheet and presentation packages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Working out the outline of a custom shape.
//!
//! The inputs here are paths and formulas taken verbatim from the presentation
//! templates LibreOffice ships, because the point of the exercise is that a
//! document says `M ?f7 0 X 0 ?f8 …` and something has to turn that into points.
//
// Author: David M. Anderson
// Built with AI assistance (Claude, Anthropic)

use odox_core::draw::Geometry;
use odox_core::xml;

/// Wrap an enhanced-geometry element in the namespaces it uses.
fn geometry(inner: &str) -> Geometry {
    let document = format!(
        r#"<draw:enhanced-geometry
             xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
             xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
             {inner}"#
    );
    let element = xml::parse(document.as_bytes(), "test").expect("well-formed XML");
    Geometry::read(&element).expect("a readable geometry")
}

#[test]
fn a_full_ellipse_is_the_commonest_shape_there_is() {
    // `U` and this exact path is 176 of the 576 custom shapes in the templates.
    let shape = geometry(
        r#"svg:viewBox="0 0 21600 21600"
           draw:enhanced-path="U 10800 10800 10800 10800 0 360 Z N"/>"#,
    );

    assert_eq!(shape.paths.len(), 1);
    let path = &shape.paths[0];
    assert!(path.closed && path.fill && path.stroke);
    assert!(path.points.len() > 30, "{} points", path.points.len());

    // Every point is on the circle, which is the whole claim.
    for (x, y) in &path.points {
        let radius = ((x - 10800.0).powi(2) + (y - 10800.0).powi(2)).sqrt();
        assert!(
            (radius - 10800.0).abs() < 1.0,
            "({x}, {y}) is off the circle"
        );
    }
}

#[test]
fn a_rectangle_is_four_lines_written_once() {
    // `L` takes its two arguments over and over: one letter, four corners.
    let shape = geometry(
        r#"svg:viewBox="0 0 21600 21600"
           draw:enhanced-path="M 0 0 L 21600 0 21600 21600 0 21600 0 0 Z N"/>"#,
    );

    assert_eq!(shape.paths.len(), 1);
    assert!(shape.paths[0].closed);
    assert_eq!(
        shape.paths[0].points,
        vec![
            (0.0, 0.0),
            (21600.0, 0.0),
            (21600.0, 21600.0),
            (0.0, 21600.0),
            (0.0, 0.0),
        ]
    );
}

#[test]
fn a_rounded_rectangle_resolves_its_formulas_and_rounds_its_corners() {
    // Verbatim from Candy.odp, handle and text areas dropped. The corner radius
    // is the modifier, and the four corners are quadrant arcs.
    let shape = geometry(
        r#"svg:viewBox="0 0 21600 21600" draw:type="round-rectangle"
           draw:modifiers="10800"
           draw:enhanced-path="M ?f7 0 X 0 ?f8 L 0 ?f9 Y ?f7 21600 L ?f10 21600 X 21600 ?f9 L 21600 ?f8 Y ?f10 0 Z N">
             <draw:equation draw:name="f0" draw:formula="45"/>
             <draw:equation draw:name="f1" draw:formula="$0 *sin(?f0 *(pi/180))"/>
             <draw:equation draw:name="f2" draw:formula="?f1 *3163/7636"/>
             <draw:equation draw:name="f7" draw:formula="left+$0 "/>
             <draw:equation draw:name="f8" draw:formula="top+$0 "/>
             <draw:equation draw:name="f9" draw:formula="bottom-$0 "/>
             <draw:equation draw:name="f10" draw:formula="right-$0 "/>
           </draw:enhanced-geometry>"#,
    );

    assert_eq!(shape.paths.len(), 1);
    let path = &shape.paths[0];
    assert!(path.closed);

    // `?f7` is left + the modifier, so the path begins halfway along the top.
    assert_eq!(path.points[0], (10800.0, 0.0));

    // With a radius of half the side, every corner is a quarter circle and the
    // whole outline is a circle inscribed in the box. What that proves is that
    // the formulas were evaluated: unresolved, every `?f7` would be nought and
    // the shape would collapse into the corner.
    let (mut left, mut right, mut top, mut bottom) = (f32::MAX, f32::MIN, f32::MAX, f32::MIN);
    for (x, y) in &path.points {
        left = left.min(*x);
        right = right.max(*x);
        top = top.min(*y);
        bottom = bottom.max(*y);
    }
    assert!(left.abs() < 1.0 && top.abs() < 1.0, "{left}, {top}");
    assert!(
        (right - 21600.0).abs() < 1.0 && (bottom - 21600.0).abs() < 1.0,
        "{right}, {bottom}"
    );
    assert!(path.points.len() > 40, "the corners are not arcs");
}

#[test]
fn a_formula_may_name_another_and_the_chain_is_followed() {
    // `?f2` is `?f1 * 3163/7636`, `?f1` is `$0 * sin(?f0 * pi/180)`, `?f0` is 45.
    // A path point of `?f2` is therefore 10800 * sin(45°) * 3163/7636 ≈ 3163.5.
    let shape = geometry(
        r#"svg:viewBox="0 0 21600 21600" draw:modifiers="10800"
           draw:enhanced-path="M ?f2 0 L 21600 0 Z N">
             <draw:equation draw:name="f0" draw:formula="45"/>
             <draw:equation draw:name="f1" draw:formula="$0 *sin(?f0 *(pi/180))"/>
             <draw:equation draw:name="f2" draw:formula="?f1 *3163/7636"/>
           </draw:enhanced-geometry>"#,
    );
    let x = shape.paths[0].points[0].0;
    assert!((x - 3163.5).abs() < 1.0, "{x}");
}

#[test]
fn a_formula_that_refers_to_itself_gives_up_rather_than_hanging() {
    // No producer writes this. A hand-edited file can, and the alternative to a
    // limit is a window that stops responding.
    let shape = geometry(
        r#"svg:viewBox="0 0 100 100"
           draw:enhanced-path="M ?f0 0 L 100 100 Z N">
             <draw:equation draw:name="f0" draw:formula="?f1 + 1"/>
             <draw:equation draw:name="f1" draw:formula="?f0 + 1"/>
           </draw:enhanced-geometry>"#,
    );
    assert!(shape.paths[0].points[0].0.is_finite());
}

#[test]
fn a_shape_may_be_several_strokes_and_say_which_are_filled() {
    let shape = geometry(
        r#"svg:viewBox="0 0 100 100"
           draw:enhanced-path="M 0 0 L 100 0 100 100 Z N F M 10 10 L 90 10 Z N"/>"#,
    );
    assert_eq!(shape.paths.len(), 2);
    assert!(shape.paths[0].fill, "the body should be filled");
    assert!(
        !shape.paths[1].fill,
        "F turns the fill off for what follows"
    );
}

#[test]
fn a_mirrored_shape_is_flipped_within_its_own_space() {
    let plain = geometry(r#"svg:viewBox="0 0 100 50" draw:enhanced-path="M 10 5 L 90 5 Z N"/>"#);
    let flipped = geometry(
        r#"svg:viewBox="0 0 100 50" draw:mirror-horizontal="true"
           draw:enhanced-path="M 10 5 L 90 5 Z N"/>"#,
    );
    assert_eq!(plain.paths[0].points[0], (10.0, 5.0));
    assert_eq!(flipped.paths[0].points[0], (90.0, 5.0));
}

#[test]
fn every_custom_shape_in_the_corpus_yields_an_outline() {
    // The whole point of the exercise, over the documents that motivated it: no
    // shape may come back empty, and every point must be a number.
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/libreoffice");
    let Ok(entries) = std::fs::read_dir(&root) else {
        return;
    };

    let mut shapes = 0;
    for entry in entries.filter_map(Result::ok) {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("odp") {
            continue;
        }
        let bytes = std::fs::read(&path).expect("the fixture");
        let package = odox_core::Package::read(&bytes).expect("a readable package");
        for part in ["content.xml", "styles.xml"] {
            let Ok(Some(tree)) = package.optional_xml(part) else {
                continue;
            };
            for element in descendants(&tree) {
                if !element.is(&odox_core::Ns::Draw, "enhanced-geometry") {
                    continue;
                }
                shapes += 1;
                let geometry = Geometry::read(element)
                    .unwrap_or_else(|| panic!("{} in {part}", path.display()));
                assert!(
                    !geometry.paths.is_empty(),
                    "{} in {part} drew nothing",
                    path.display()
                );
                for stroke in &geometry.paths {
                    for (x, y) in &stroke.points {
                        assert!(
                            x.is_finite() && y.is_finite(),
                            "{} gave ({x}, {y})",
                            path.display()
                        );
                    }
                }
            }
        }
    }
    assert!(shapes > 0, "the corpus has no custom shapes to read");
}

fn descendants(element: &odox_core::Element) -> Vec<&odox_core::Element> {
    let mut found = vec![element];
    for child in element.elements() {
        found.extend(descendants(child));
    }
    found
}

/// SVG path data, which is how a `draw:path` states its outline.
mod svg {
    use odox_core::draw::Geometry;
    use odox_core::xml;

    fn path(attrs: &str) -> Geometry {
        let document = format!(
            r#"<draw:path
                 xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
                 xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
                 {attrs}/>"#
        );
        let element = xml::parse(document.as_bytes(), "test").expect("well-formed XML");
        Geometry::read_path(&element).expect("a readable path")
    }

    #[test]
    fn a_sign_separates_two_numbers_with_no_space_between_them() {
        // `0-571` is two numbers, and every path in the corpus is written this
        // way. Split on whitespace and the shape collapses.
        let shape = path(r#"svg:viewBox="0 0 100 100" svg:d="M0 0h10v10-5-5z""#);
        assert_eq!(
            shape.paths[0].points,
            vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (10.0, 5.0),
                (10.0, 0.0)
            ]
        );
        assert!(shape.paths[0].closed);
    }

    #[test]
    fn lower_case_is_relative_and_upper_case_is_not() {
        let relative = path(r#"svg:viewBox="0 0 100 100" svg:d="M10 10 l5 5 l5 5""#);
        assert_eq!(
            relative.paths[0].points,
            vec![(10.0, 10.0), (15.0, 15.0), (20.0, 20.0)]
        );
        let absolute = path(r#"svg:viewBox="0 0 100 100" svg:d="M10 10 L5 5 L5 5""#);
        assert_eq!(
            absolute.paths[0].points,
            vec![(10.0, 10.0), (5.0, 5.0), (5.0, 5.0)]
        );
    }

    #[test]
    fn the_pairs_after_a_moveto_are_lines() {
        // SVG's one irregularity: `M` repeated is `L`, not another move.
        let shape = path(r#"svg:viewBox="0 0 100 100" svg:d="M0 0 10 0 10 10z""#);
        assert_eq!(shape.paths.len(), 1);
        assert_eq!(
            shape.paths[0].points,
            vec![(0.0, 0.0), (10.0, 0.0), (10.0, 10.0)]
        );
    }

    #[test]
    fn a_relative_cubic_is_flattened_and_ends_where_it_should() {
        let shape = path(r#"svg:viewBox="0 0 100 100" svg:d="M0 0c10 0 20 10 20 20""#);
        let points = &shape.paths[0].points;
        assert_eq!(points[0], (0.0, 0.0));
        let (x, y) = *points.last().expect("an end");
        assert!(
            (x - 20.0).abs() < 0.01 && (y - 20.0).abs() < 0.01,
            "({x}, {y})"
        );
        assert!(points.len() > 8, "a curve is more than its ends");
    }

    #[test]
    fn a_smooth_curve_continues_the_one_before_it() {
        // `s` reflects the previous second control, so the join has no corner:
        // the two segments either side of it run in the same direction.
        let shape = path(r#"svg:viewBox="0 0 100 100" svg:d="M0 0c0 10 10 10 10 10s10 0 10-10""#);
        let points = &shape.paths[0].points;
        let join = points.len() / 2;
        let before = (
            points[join].0 - points[join - 1].0,
            points[join].1 - points[join - 1].1,
        );
        let after = (
            points[join + 1].0 - points[join].0,
            points[join + 1].1 - points[join].1,
        );
        let angle = |v: (f32, f32)| v.1.atan2(v.0);
        assert!(
            (angle(before) - angle(after)).abs() < 0.3,
            "the join turns a corner: {before:?} then {after:?}"
        );
    }

    #[test]
    fn every_path_in_the_corpus_yields_an_outline() {
        let root =
            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/libreoffice");
        let Ok(entries) = std::fs::read_dir(&root) else {
            return;
        };
        let mut found = 0;
        for entry in entries.filter_map(Result::ok) {
            let file = entry.path();
            if file.extension().and_then(|e| e.to_str()) != Some("odp") {
                continue;
            }
            let bytes = std::fs::read(&file).expect("the fixture");
            let package = odox_core::Package::read(&bytes).expect("a readable package");
            for part in ["content.xml", "styles.xml"] {
                let Ok(Some(tree)) = package.optional_xml(part) else {
                    continue;
                };
                for element in super::descendants(&tree) {
                    if !element.is(&odox_core::Ns::Draw, "path") {
                        continue;
                    }
                    found += 1;
                    let geometry = Geometry::read_path(element)
                        .unwrap_or_else(|| panic!("{} in {part}", file.display()));
                    assert!(
                        !geometry.paths.is_empty(),
                        "{} drew nothing",
                        file.display()
                    );
                    for stroke in &geometry.paths {
                        for (x, y) in &stroke.points {
                            assert!(x.is_finite() && y.is_finite(), "({x}, {y})");
                        }
                    }
                }
            }
        }
        assert!(found > 0, "the corpus has no draw:path to read");
    }

    #[test]
    fn a_refit_box_holds_the_outline_it_was_measured_from() {
        // What a connector writes: page coordinates in the path and an origin
        // of zero in the view box, which do not describe the same space.
        let mut shape =
            path(r#"svg:viewBox="0 0 3947 2706" svg:d="M5280 8808c2959 0 987-2705 3946-2705""#);
        shape.refit();
        assert!((shape.view.x - 5280.0).abs() < 1.0, "{:?}", shape.view);
        assert!((shape.view.y - 6103.0).abs() < 1.0, "{:?}", shape.view);
        assert!((shape.view.width - 3946.0).abs() < 1.0, "{:?}", shape.view);
        assert!((shape.view.height - 2705.0).abs() < 1.0, "{:?}", shape.view);
        for (x, y) in shape.paths.iter().flat_map(|p| &p.points) {
            assert!(
                *x >= shape.view.x && *x <= shape.view.x + shape.view.width,
                "({x}, {y}) is outside {:?}",
                shape.view
            );
        }
    }

    #[test]
    fn a_straight_run_still_refits_to_a_box_with_a_size() {
        let mut shape = path(r#"svg:viewBox="0 0 10 10" svg:d="M0 40H100""#);
        shape.refit();
        assert!(shape.view.height > 0.0, "{:?}", shape.view);
        assert!((shape.view.width - 100.0).abs() < 0.01, "{:?}", shape.view);
    }

    #[test]
    fn every_connector_in_the_corpus_yields_a_route() {
        let root =
            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/libreoffice");
        let Ok(entries) = std::fs::read_dir(&root) else {
            return;
        };
        let mut found = 0;
        for entry in entries.filter_map(Result::ok) {
            let file = entry.path();
            if file.extension().and_then(|e| e.to_str()) != Some("odp") {
                continue;
            }
            let bytes = std::fs::read(&file).expect("the fixture");
            let package = odox_core::Package::read(&bytes).expect("a readable package");
            let Ok(Some(tree)) = package.optional_xml("content.xml") else {
                continue;
            };
            for element in super::descendants(&tree) {
                if !element.is(&odox_core::Ns::Draw, "connector") {
                    continue;
                }
                found += 1;
                let mut geometry =
                    Geometry::read_path(element).expect("a connector states its route");
                geometry.refit();
                assert!(geometry.view.width > 0.0 && geometry.view.height > 0.0);
                assert!(
                    geometry.paths.iter().any(|p| p.points.len() > 2),
                    "{} routed a bare line",
                    file.display()
                );
            }
        }
        assert!(found > 0, "the corpus has no draw:connector to read");
    }
}