f-office-docx 0.2.4

DOCX model and Word's layout rules.
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
use f_office_paint::{FillRule, Rgb};

use super::{
    ART_PICTURES_BYTE_LIMIT, ART_PICTURES_CLIP_DEPTH_LIMIT, ART_PICTURES_COMMAND_LIMIT,
    ART_PICTURES_REACH_LIMIT, ART_PICTURES_SIZE_LIMIT, ArtClip, ArtItem, ArtPictures,
    ArtPicturesError, ArtPicturesFault, ArtRect, ArtShape, UnitCommand, UnitPoint,
};
use crate::props::ArtStyle;

/// A file of one picture of `apples` at `size` points: a square on the top side, a triangle in
/// the top left corner and a long bar along the left side.
fn apples(size: u32) -> String {
    format!(
        "f-office art pictures 3\n\
         style apples {size}\n\
         part top repeat\n\
         shape 0 0 0 evenodd\n\
         ring 0 0 L 1000000 0 L 1000000 1000000 L 0 1000000\n\
         part top-left corner\n\
         shape 255 0 0 nonzero\n\
         ring 0 0 Q 500000 0 1000000 1000000 C 0 1000000 0 500000 0 250000\n\
         part left span\n\
         shape 0 0 0 nonzero\n\
         ring 0 0 L 1000000 0 L 1000000 1000000\n"
    )
}

fn style(name: &str) -> ArtStyle {
    ArtStyle::of_name(name).unwrap()
}

fn fault(text: &str) -> ArtPicturesError {
    ArtPictures::new().add(text.as_bytes()).unwrap_err()
}

#[test]
fn names_the_164_art_styles_after_the_line_styles() {
    let names: Vec<&str> = crate::props::BORDER_NAMES
        .iter()
        .copied()
        .filter(|name| ArtStyle::of_name(name).is_some())
        .collect();
    assert_eq!(names.len(), 164);
    assert_eq!(names.first(), Some(&"apples"));
    assert_eq!(names.last(), Some(&"zigZagStitch"));
    for name in names {
        assert_eq!(ArtStyle::of_name(name).map(ArtStyle::name), Some(name));
    }
    for line in ["single", "inset", "nil", "Apples", ""] {
        assert_eq!(ArtStyle::of_name(line), None, "{line}");
    }
}

/// The set of the one picture of `apples` at 16 points.
fn apples_16() -> ArtPictures {
    let mut pictures = ArtPictures::new();
    assert_eq!(pictures.add(apples(16).as_bytes()), Ok(1));
    pictures
}

fn point(x: i32, y: i32) -> UnitPoint {
    UnitPoint { x, y }
}

/// The shape of `item`, which must be one.
fn shape(item: Option<&ArtItem>) -> &ArtShape {
    match item {
        Some(ArtItem::Shape(shape)) => shape,
        other => panic!("not a shape: {other:?}"),
    }
}

#[test]
fn reads_the_shapes_that_each_square_of_a_side_repeats() {
    let pictures = apples_16();
    assert_eq!(pictures.len(), 1);
    assert!(pictures.picture(style("apples"), 8).is_none());
    assert!(pictures.picture(style("holly"), 16).is_none());
    let picture = pictures.picture(style("apples"), 16).unwrap();
    let top = shape(picture.sides.top.repeat.first());
    assert_eq!((top.color, top.fill_rule), (Rgb::BLACK, FillRule::EvenOdd));
    assert_eq!(
        top.commands,
        [
            UnitCommand::Move(point(0, 0)),
            UnitCommand::Line(point(1_000_000, 0)),
            UnitCommand::Line(point(1_000_000, 1_000_000)),
            UnitCommand::Line(point(0, 1_000_000)),
            UnitCommand::Close,
        ]
    );
}

#[test]
fn reads_the_curves_of_a_corner_and_the_run_of_a_side() {
    let pictures = apples_16();
    let picture = pictures.picture(style("apples"), 16).unwrap();
    let corner = shape(picture.corners.top_left.first());
    assert_eq!(corner.color, Rgb { r: 255, g: 0, b: 0 });
    assert_eq!(
        corner.commands.get(1..3),
        Some(
            [
                UnitCommand::Quad(point(500_000, 0), point(1_000_000, 1_000_000)),
                UnitCommand::Cubic(point(0, 1_000_000), point(0, 500_000), point(0, 250_000)),
            ]
            .as_slice()
        )
    );
    assert_eq!(
        [
            picture.sides.left.span.len(),
            picture.sides.left.repeat.len(),
            picture.sides.bottom.repeat.len(),
            picture.corners.bottom_right.len(),
        ],
        [1, 0, 0, 0]
    );
}

#[test]
fn takes_a_later_picture_of_a_style_and_size_in_place_of_the_earlier_one() {
    let mut pictures = ArtPictures::new();
    pictures.add(apples(16).as_bytes()).unwrap();
    pictures.add(apples(8).as_bytes()).unwrap();
    let red = apples(16).replace("shape 0 0 0 evenodd", "shape 9 9 9 evenodd");
    pictures.add(red.as_bytes()).unwrap();
    assert_eq!(pictures.len(), 2);
    let top = |size| {
        shape(
            pictures
                .picture(style("apples"), size)
                .unwrap()
                .sides
                .top
                .repeat
                .first(),
        )
        .color
    };
    assert_eq!(top(16), Rgb { r: 9, g: 9, b: 9 });
    assert_eq!(top(8), Rgb::BLACK);
}

#[test]
fn refuses_a_file_that_does_not_read_and_names_the_line() {
    let header = "f-office art pictures 3\n";
    let after = |rest: &str| format!("{header}{rest}");
    let cases = [
        (
            "f-office art pictures 1\n".to_owned(),
            1,
            ArtPicturesFault::Header,
        ),
        (
            "f-office art pictures 2\n".to_owned(),
            1,
            ArtPicturesFault::Header,
        ),
        (after("style inset 16\n"), 2, ArtPicturesFault::Style),
        (after("style apples 0\n"), 2, ArtPicturesFault::Style),
        (after("style apples 256\n"), 2, ArtPicturesFault::Style),
        (after("part top repeat\n"), 2, ArtPicturesFault::Part),
        (
            after("style apples 1\npart top corner\n"),
            3,
            ArtPicturesFault::Part,
        ),
        (
            after("style apples 1\npart top-left repeat\n"),
            3,
            ArtPicturesFault::Part,
        ),
        (
            after("style apples 1\nshape 0 0 0 evenodd\n"),
            3,
            ArtPicturesFault::Shape,
        ),
        (
            after("style apples 1\npart top span\nshape 0 0 256 evenodd\n"),
            4,
            ArtPicturesFault::Shape,
        ),
        (
            after("style apples 1\npart top span\nshape 0 0 0 even\n"),
            4,
            ArtPicturesFault::Shape,
        ),
        (
            after("style apples 1\npart top span\nring 0 0\n"),
            4,
            ArtPicturesFault::Ring,
        ),
        (
            after("style apples 1\npart top span\nshape 0 0 0 nonzero\nring 0 0 Z 1 1\n"),
            5,
            ArtPicturesFault::Ring,
        ),
        (
            after("style apples 1\npart top span\nshape 0 0 0 nonzero\nring 0 0 L 1\n"),
            5,
            ArtPicturesFault::Ring,
        ),
        (
            after(&format!(
                "style apples 1\npart top span\nshape 0 0 0 nonzero\nring 0 {}\n",
                ART_PICTURES_REACH_LIMIT + 1
            )),
            5,
            ArtPicturesFault::Ring,
        ),
        (after("dots 1 2\n"), 2, ArtPicturesFault::Unknown),
    ];
    check_faults(&cases);
    assert_eq!(
        ArtPictures::new().add(&[0xFF, 0xFE]).unwrap_err().fault,
        ArtPicturesFault::NotText
    );
}

#[test]
fn refuses_a_rect_line_that_does_not_read_and_names_the_line() {
    let after = |rest: &str| format!("f-office art pictures 3\n{rest}");
    let cases = [
        (
            after("style apples 1\nrect 0 0 0 0 0 1 1 140\n"),
            3,
            ArtPicturesFault::Rect,
        ),
        (
            after("style apples 1\npart top span\nrect 0 0 0 0 0 1 1\n"),
            4,
            ArtPicturesFault::Rect,
        ),
        (
            after("style apples 1\npart top span\nrect 0 0 0 0 0 1 1 140 2\n"),
            4,
            ArtPicturesFault::Rect,
        ),
        (
            after("style apples 1\npart top span\nrect 0 0 256 0 0 1 1 140\n"),
            4,
            ArtPicturesFault::Rect,
        ),
        (
            after("style apples 1\npart top span\nrect 0 0 0 5 0 4 1 140\n"),
            4,
            ArtPicturesFault::Rect,
        ),
        (
            after("style apples 1\npart top span\nrect 0 0 0 0 5 1 4 140\n"),
            4,
            ArtPicturesFault::Rect,
        ),
        (
            after("style apples 1\npart top span\nrect 0 0 0 0 0 1 1 -1\n"),
            4,
            ArtPicturesFault::Rect,
        ),
        (
            after(&format!(
                "style apples 1\npart top span\nrect 0 0 0 0 0 1 1 {}\n",
                ART_PICTURES_SIZE_LIMIT * 1_000 + 1
            )),
            4,
            ArtPicturesFault::Rect,
        ),
        (
            after(&format!(
                "style apples 1\npart top span\nrect 0 0 0 0 0 {} 1 140\n",
                ART_PICTURES_REACH_LIMIT + 1
            )),
            4,
            ArtPicturesFault::Rect,
        ),
    ];
    check_faults(&cases);
}

/// Checks that each file of `cases` fails on its line with its fault.
fn check_faults(cases: &[(String, usize, ArtPicturesFault)]) {
    for (text, line, expected) in cases {
        assert_eq!(
            fault(text),
            ArtPicturesError {
                line: *line,
                fault: *expected
            },
            "{text}"
        );
    }
}

#[test]
fn reads_a_stroked_rectangle_with_its_corners_and_its_stroke_width() {
    let text = "f-office art pictures 3\n\
                style twistedLines1 1\n\
                part top repeat\n\
                rect 0 0 0 125000 0 250000 1000000 140\n\
                rect 255 0 0 0 0 0 0 0\n";
    let mut pictures = ArtPictures::new();
    assert_eq!(pictures.add(text.as_bytes()), Ok(1));
    let picture = pictures.picture(style("twistedLines1"), 1).unwrap();
    assert_eq!(
        picture.sides.top.repeat,
        [
            ArtItem::Rect(ArtRect {
                color: Rgb::BLACK,
                top_left: point(125_000, 0),
                bottom_right: point(250_000, 1_000_000),
                stroke: 140,
            }),
            ArtItem::Rect(ArtRect {
                color: Rgb { r: 255, g: 0, b: 0 },
                top_left: point(0, 0),
                bottom_right: point(0, 0),
                stroke: 0,
            }),
        ]
    );
}

#[test]
fn reads_a_clip_with_its_rings_and_the_items_up_to_its_end() {
    let text = "f-office art pictures 3\n\
                style holly 8\n\
                part top repeat\n\
                clip evenodd\n\
                ring 0 0 L 10 0 L 10 10\n\
                ring 2 2 L 4 2 L 4 4\n\
                shape 0 0 0 nonzero\n\
                ring 0 0 L 1 1 L 0 1\n\
                end\n";
    let mut pictures = ArtPictures::new();
    assert_eq!(pictures.add(text.as_bytes()), Ok(1));
    let picture = pictures.picture(style("holly"), 8).unwrap();
    let items = &picture.sides.top.repeat;
    assert_eq!(items.len(), 3);
    let Some(ArtItem::Clip(ArtClip {
        fill_rule,
        commands,
    })) = items.first()
    else {
        panic!("a clip first: {items:?}");
    };
    assert_eq!((*fill_rule, commands.len()), (FillRule::EvenOdd, 8));
    assert_eq!(shape(items.get(1)).commands.len(), 4);
    assert_eq!(items.get(2), Some(&ArtItem::End));
}

#[test]
fn refuses_a_clip_that_does_not_read_or_does_not_end_in_its_part() {
    let header = "f-office art pictures 3\nstyle apples 1\npart top span\n";
    let after = |rest: &str| format!("{header}{rest}");
    let deep = "clip nonzero\n".repeat(ART_PICTURES_CLIP_DEPTH_LIMIT + 1);
    let cases = [
        (
            "f-office art pictures 3\nstyle apples 1\nclip nonzero\n".to_owned(),
            3,
            ArtPicturesFault::Clip,
        ),
        (after("clip even\n"), 4, ArtPicturesFault::Clip),
        (after("end\n"), 4, ArtPicturesFault::End),
        (after("clip nonzero\nend 1\n"), 5, ArtPicturesFault::End),
        (
            after("clip nonzero\npart top repeat\n"),
            5,
            ArtPicturesFault::End,
        ),
        (
            after("clip nonzero\nstyle apples 2\n"),
            5,
            ArtPicturesFault::End,
        ),
        (
            after("clip nonzero\nend\nring 0 0\n"),
            6,
            ArtPicturesFault::Ring,
        ),
        (after("clip nonzero\n"), 0, ArtPicturesFault::End),
        (
            after(&deep),
            4 + ART_PICTURES_CLIP_DEPTH_LIMIT,
            ArtPicturesFault::TooDeep,
        ),
    ];
    check_faults(&cases);
}

#[test]
fn refuses_a_file_beyond_its_limits_and_adds_nothing_of_it() {
    let mut pictures = ArtPictures::new();
    let large = vec![b' '; ART_PICTURES_BYTE_LIMIT + 1];
    assert_eq!(
        pictures.add(&large).unwrap_err().fault,
        ArtPicturesFault::TooLarge
    );
    let ring = "L 1 1 ".repeat(ART_PICTURES_COMMAND_LIMIT);
    let long = format!("{}ring 0 0 {ring}\n", apples(4));
    assert_eq!(
        pictures.add(long.as_bytes()).unwrap_err(),
        ArtPicturesError {
            line: 12,
            fault: ArtPicturesFault::TooManyCommands
        }
    );
    assert!(pictures.is_empty());
}