drawingml 1.0.0

Shared DrawingML content model (shapes, fills, colors, lines) for the WordprocessingML/SpreadsheetML/PresentationML crates.
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Round-trip tests: write a [`ShapeProperties`] to a self-contained XML fragment (a synthetic
//! `<spPr xmlns:a=".">.</spPr>` root, standing in for whatever host-specific wrapper element —
//! `<xdr:spPr>`, `<p:spPr>`, `<wps:spPr>` — actually surrounds this crate's output in each host
//! format), then parse it back and compare against the original.

use drawingml::{
    BlipFill, BlipFillMode, Color, EffectList, Fill, Geometry, GeometryAdjustment, GradientFill,
    GradientStop, Line, LineCap, LineCompound, LineEnd, LineEndSize, LineEndType, LineJoin,
    OuterShadow, PatternFill, PresetLineDash, PresetPattern, PresetShape, Reader, ShapeProperties,
    TextAlign, TextAnchor, TextBody, TextBodyProperties, TextCaps, TextParagraph,
    TextParagraphProperties, TextRun, TextRunProperties, TextStrike, TextUnderline,
    TextVerticalType, TextWrap, Transform2D, read_shape_properties, read_text_body,
    write_geometry_fill_line_effects, write_shape_properties, write_text_body,
};
use xml_core::{BytesStart, Event, Writer};

const SPPR_XMLNS_A: &str = "http://schemas.openxmlformats.org/drawingml/2006/main";

/// Writes `properties` into a synthetic `<spPr xmlns:a="..">..</spPr>` root, then parses it
/// straight back — the shared harness every test below uses.
fn roundtrip(properties: &ShapeProperties) -> ShapeProperties {
    let mut writer = Writer::new(Vec::new());
    let mut root = BytesStart::new("spPr");
    root.push_attribute(("xmlns:a", SPPR_XMLNS_A));
    writer.write_event(Event::Start(root)).unwrap();
    write_shape_properties(&mut writer, properties).unwrap();
    writer
        .write_event(Event::End(xml_core::BytesEnd::new("spPr")))
        .unwrap();

    let xml = String::from_utf8(writer.into_inner()).unwrap();

    let mut reader = Reader::from_xml_str(&xml);
    reader.read_event().unwrap(); // consume the synthetic `<spPr>` root's own start tag
    read_shape_properties(&mut reader).unwrap()
}

#[test]
fn round_trips_a_transform() {
    let properties = ShapeProperties::new().with_transform(
        Transform2D::new()
            .with_offset(914_400, 457_200)
            .with_extent(1_828_800, 914_400)
            .with_rotation_degrees(45.0)
            .with_flip_horizontal(true),
    );

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_transform_with_no_offset_or_extent() {
    // `<a:xfrm>` with only its own attributes set (no `<a:off>`/`<a:ext>` children at all) must
    // still write/read as a self-closing element — exercises the `Event::Empty` path in both
    // `write_transform` and `parse_transform`.
    let properties =
        ShapeProperties::new().with_transform(Transform2D::new().with_rotation_degrees(90.0));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_preset_geometry_shape() {
    let properties =
        ShapeProperties::new().with_geometry(Geometry::Preset(PresetShape::RoundedRectangle));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_preset_geometry_not_in_the_closed_enum() {
    // `PresetShape::Other` preserves round-trip fidelity for the ~150 `ST_ShapeType` tokens this
    // crate doesn't give a named variant to.
    let properties = ShapeProperties::new().with_geometry(Geometry::Preset(PresetShape::Other(
        "irregularSeal1".to_string(),
    )));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_solid_rgb_fill() {
    let properties =
        ShapeProperties::new().with_fill(Fill::Solid(Color::Rgb("FF9900".to_string())));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_no_fill() {
    let properties = ShapeProperties::new().with_fill(Fill::None);

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_an_rgb_percent_color() {
    let properties = ShapeProperties::new().with_fill(Fill::Solid(Color::RgbPercent {
        red: 50_000,
        green: 25_000,
        blue: 100_000,
    }));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_an_hsl_color() {
    let properties = ShapeProperties::new().with_fill(Fill::Solid(Color::Hsl {
        hue_60000ths: 10_800_000,
        saturation_1000ths_percent: 80_000,
        luminance_1000ths_percent: 50_000,
    }));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_system_color_with_a_fallback() {
    let properties = ShapeProperties::new().with_fill(Fill::Solid(Color::System {
        value: "windowText".to_string(),
        last_color: Some("000000".to_string()),
    }));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_preset_named_color() {
    let properties =
        ShapeProperties::new().with_fill(Fill::Solid(Color::Preset("tomato".to_string())));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_gradient_fill() {
    let gradient = GradientFill::new()
        .with_stop(GradientStop::new(0.0, Color::Rgb("FFFFFF".to_string())))
        .with_stop(GradientStop::new(100.0, Color::Rgb("000000".to_string())))
        .with_angle_degrees(90.0);
    let properties = ShapeProperties::new().with_fill(Fill::Gradient(gradient));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_pattern_fill() {
    let pattern = PatternFill::new(PresetPattern::LightDiagonalDown)
        .with_foreground(Color::Rgb("FF0000".to_string()))
        .with_background(Color::Rgb("FFFFFF".to_string()));
    let properties = ShapeProperties::new().with_fill(Fill::Pattern(pattern));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_pattern_fill_with_no_explicit_colors() {
    let properties =
        ShapeProperties::new().with_fill(Fill::Pattern(PatternFill::new(PresetPattern::Weave)));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_line_with_dash_cap_join_and_arrowheads() {
    let line = Line::new()
        .with_width_emu(25_400)
        .with_fill(Fill::Solid(Color::Rgb("2E75B6".to_string())))
        .with_dash(PresetLineDash::DashDot)
        .with_cap(LineCap::Round)
        .with_join(LineJoin::Miter {
            limit_1000ths_percent: Some(800_000),
        })
        .with_head_end(
            LineEnd::new()
                .with_kind(LineEndType::Triangle)
                .with_width(LineEndSize::Medium)
                .with_length(LineEndSize::Small),
        )
        .with_tail_end(LineEnd::new().with_kind(LineEndType::Arrow));
    let properties = ShapeProperties::new().with_line(line);

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_bare_line_with_only_a_width() {
    // No children at all — exercises `write_line`'s `Event::Empty` path.
    let properties = ShapeProperties::new().with_line(Line::new().with_width_emu(9_525));

    assert_eq!(roundtrip(&properties), properties);
}

// ================================================================================================
// Preset geometry adjustment values (`<a:avLst>`) and compound line style (`a:ln@cmpd`)
// ================================================================================================

#[test]
fn round_trips_preset_geometry_with_a_single_adjustment_value() {
    let properties = ShapeProperties::new()
        .with_geometry(Geometry::Preset(PresetShape::Chevron))
        .with_geometry_adjustments(vec![GeometryAdjustment::new("adj", "val 15000")]);

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_preset_geometry_with_several_adjustment_values() {
    let properties = ShapeProperties::new()
        .with_geometry(Geometry::Preset(PresetShape::RoundedRectangle))
        .with_geometry_adjustments(vec![
            GeometryAdjustment::new("adj1", "val 10000"),
            GeometryAdjustment::new("adj2", "val 20000"),
        ]);

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_preset_geometry_with_no_adjustment_values() {
    // No `geometry_adjustments` at all — exercises `write_geometry`'s `Event::Empty` path for
    // `<a:prstGeom>` (no `<a:avLst>` child), same as previously.
    let properties = ShapeProperties::new().with_geometry(Geometry::Preset(PresetShape::Rectangle));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_compound_line_style() {
    let properties = ShapeProperties::new().with_line(
        Line::new()
            .with_width_emu(38_100)
            .with_fill(Fill::Solid(Color::Rgb("2E2E2E".to_string())))
            .with_compound(LineCompound::ThickThin),
    );

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_fully_combined_shape() {
    let properties = ShapeProperties::new()
        .with_transform(
            Transform2D::new()
                .with_offset(0, 0)
                .with_extent(1_000_000, 500_000),
        )
        .with_geometry(Geometry::Preset(PresetShape::Ellipse))
        .with_fill(Fill::Solid(Color::Rgb("4472C4".to_string())))
        .with_line(Line::new().with_width_emu(12_700).with_cap(LineCap::Flat));

    assert_eq!(roundtrip(&properties), properties);
}

// ================================================================================================
// Image fills (`Fill::Image`/`BlipFill`)
// ================================================================================================

#[test]
fn round_trips_an_image_fill_with_a_relationship_id_and_stretch_mode() {
    let properties = ShapeProperties::new().with_fill(Fill::Image(
        BlipFill::new("rId3").with_mode(BlipFillMode::Stretch),
    ));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_an_image_fill_with_tile_mode() {
    let properties = ShapeProperties::new().with_fill(Fill::Image(
        BlipFill::new("rId7").with_mode(BlipFillMode::Tile),
    ));

    assert_eq!(roundtrip(&properties), properties);
}

#[test]
fn round_trips_a_bare_image_fill_with_no_relationship_or_mode() {
    // Exercises `write_blip_fill`'s `Event::Empty` path.
    let properties = ShapeProperties::new().with_fill(Fill::Image(BlipFill::default()));

    assert_eq!(roundtrip(&properties), properties);
}

// ================================================================================================
// Text-in-shape (`TextBody`)
// ================================================================================================

const TXBODY_XMLNS_A: &str = "http://schemas.openxmlformats.org/drawingml/2006/main";

/// Writes `body` into a synthetic `<txBody xmlns:a="..">..</txBody>` root, then parses it straight
/// back — mirrors [`roundtrip`] above, for [`TextBody`] instead of [`ShapeProperties`].
fn roundtrip_text_body(body: &TextBody) -> TextBody {
    let mut writer = Writer::new(Vec::new());
    let mut root = BytesStart::new("txBody");
    root.push_attribute(("xmlns:a", TXBODY_XMLNS_A));
    writer.write_event(Event::Start(root)).unwrap();
    write_text_body(&mut writer, body).unwrap();
    writer
        .write_event(Event::End(xml_core::BytesEnd::new("txBody")))
        .unwrap();

    let xml = String::from_utf8(writer.into_inner()).unwrap();

    let mut reader = Reader::from_xml_str(&xml);
    reader.read_event().unwrap(); // consume the synthetic `<txBody>` root's own start tag
    read_text_body(&mut reader).unwrap()
}

#[test]
fn round_trips_an_empty_text_body_with_body_properties() {
    let body = TextBody::new().with_properties(
        TextBodyProperties::new()
            .with_wrap(TextWrap::Square)
            .with_anchor(TextAnchor::Center)
            .with_insets_emu(91_440, 45_720, 91_440, 45_720),
    );

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_a_paragraph_with_alignment_and_margins() {
    let body = TextBody::new().with_paragraph(
        TextParagraph::new()
            .with_properties(
                TextParagraphProperties::new()
                    .with_alignment(TextAlign::Center)
                    .with_margins_emu(0, 0)
                    .with_indent_emu(-228_600),
            )
            .with_run(TextRun::text("Titre centre")),
    );

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_a_run_with_full_character_formatting() {
    let run_properties = TextRunProperties::new()
        .with_bold(true)
        .with_italic(true)
        .with_underline(TextUnderline::Single)
        .with_strike(TextStrike::Double)
        .with_fill(Fill::Solid(Color::Rgb("C00000".to_string())))
        .with_font_size_points(18.0)
        .with_font_family("Calibri");
    let body = TextBody::new().with_paragraph(
        TextParagraph::new().with_run(TextRun::text_with_properties("Attention", run_properties)),
    );

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_a_plain_run_with_no_formatting() {
    // Exercises `write_text_run_properties`'s "nothing to write" path (`TextRunProperties::new()`'s
    // all-default value).
    let body = TextBody::new()
        .with_paragraph(TextParagraph::new().with_run(TextRun::text("Texte simple")));

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_a_line_break_with_no_formatting_override() {
    let body = TextBody::new().with_paragraph(
        TextParagraph::new()
            .with_run(TextRun::text("Premiere ligne"))
            .with_run(TextRun::line_break())
            .with_run(TextRun::text("Deuxieme ligne")),
    );

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_a_line_break_with_a_formatting_override() {
    let break_properties = TextRunProperties::new().with_bold(true);
    let body = TextBody::new().with_paragraph(
        TextParagraph::new()
            .with_run(TextRun::text("Avant"))
            .with_run(TextRun::LineBreak {
                properties: Some(break_properties),
            }),
    );

    assert_eq!(roundtrip_text_body(&body), body);
}

// ================================================================================================
// Text casing/spacing, text direction/rotation, and dynamic text fields (`<a:fld>`)
// ================================================================================================

#[test]
fn round_trips_text_caps_and_character_spacing() {
    let run_properties = TextRunProperties::new()
        .with_text_caps(TextCaps::All)
        .with_character_spacing_points(2.5);
    let body = TextBody::new().with_paragraph(TextParagraph::new().with_run(
        TextRun::text_with_properties("MAJUSCULES ESPACEES", run_properties),
    ));

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_small_caps_with_no_character_spacing() {
    let run_properties = TextRunProperties::new().with_text_caps(TextCaps::Small);
    let body = TextBody::new().with_paragraph(TextParagraph::new().with_run(
        TextRun::text_with_properties("petites capitales", run_properties),
    ));

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_negative_character_spacing() {
    // Negative `spc` (tightened spacing) — exercises the signed-integer path, distinct from the
    // positive case above.
    let run_properties = TextRunProperties::new().with_character_spacing_points(-1.5);
    let body = TextBody::new().with_paragraph(
        TextParagraph::new().with_run(TextRun::text_with_properties("serre", run_properties)),
    );

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_text_body_vertical_direction_and_rotation() {
    let body = TextBody::new()
        .with_properties(
            TextBodyProperties::new()
                .with_vertical_direction(TextVerticalType::Vertical270)
                .with_rotation_degrees(-15.0),
        )
        .with_paragraph(TextParagraph::new().with_run(TextRun::text("Texte vertical pivote")));

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_a_dynamic_text_field() {
    let body = TextBody::new().with_paragraph(TextParagraph::new().with_run(TextRun::field(
        "{5C4A1234-0000-0000-0000-000000000000}",
        Some("slidenum".to_string()),
        "1",
    )));

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_a_dynamic_text_field_with_no_type_and_with_formatting() {
    // `field_type: None` (schema-optional `type` attribute omitted) plus explicit run formatting —
    // exercises both branches together.
    let body = TextBody::new().with_paragraph(TextParagraph::new().with_run(TextRun::Field {
        id: "{5C4A1234-1111-1111-1111-111111111111}".to_string(),
        field_type: None,
        cached_text: "12/07/2026".to_string(),
        properties: TextRunProperties::new().with_bold(true),
    }));

    assert_eq!(roundtrip_text_body(&body), body);
}

#[test]
fn round_trips_multiple_paragraphs_with_a_gradient_filled_run() {
    let gradient = GradientFill::new()
        .with_stop(GradientStop::new(0.0, Color::Rgb("FFFFFF".to_string())))
        .with_stop(GradientStop::new(100.0, Color::Rgb("000000".to_string())));
    let run_properties = TextRunProperties::new().with_fill(Fill::Gradient(gradient));

    let body = TextBody::new()
        .with_paragraph(TextParagraph::new().with_run(TextRun::text("Premier paragraphe")))
        .with_paragraph(TextParagraph::new().with_run(TextRun::text_with_properties(
            "Second paragraphe, degrade",
            run_properties,
        )));

    assert_eq!(roundtrip_text_body(&body), body);
}

// ================================================================================================
// `write_geometry_fill_line_effects`, the shared geometry-with-fallback + fill/line/effects helper
// used by `word-ooxml`, `excel-ooxml`, and `powerpoint-ooxml`
// ================================================================================================

/// Writes `properties` via `write_geometry_fill_line_effects` (with `default_preset`) into a
/// synthetic `<spPr xmlns:a="..">..</spPr>` root, then parses it straight back with the same
/// [`read_shape_properties`] the `roundtrip` harness above uses — this function never writes
/// `<a:xfrm>` itself (that part is deliberately each host crate's own responsibility), so
/// `properties.transform` is never exercised here.
fn roundtrip_geometry_fill_line_effects(
    properties: &ShapeProperties,
    default_preset: &str,
) -> ShapeProperties {
    let mut writer = Writer::new(Vec::new());
    let mut root = BytesStart::new("spPr");
    root.push_attribute(("xmlns:a", SPPR_XMLNS_A));
    writer.write_event(Event::Start(root)).unwrap();
    write_geometry_fill_line_effects(&mut writer, properties, default_preset).unwrap();
    writer
        .write_event(Event::End(xml_core::BytesEnd::new("spPr")))
        .unwrap();

    let xml = String::from_utf8(writer.into_inner()).unwrap();

    let mut reader = Reader::from_xml_str(&xml);
    reader.read_event().unwrap(); // consume the synthetic `<spPr>` root's own start tag
    read_shape_properties(&mut reader).unwrap()
}

/// When `properties.geometry` is unset, a fixed `<a:prstGeom prst="{default_preset}">` fallback is
/// written instead of omitting the geometry element entirely — the exact behavior the three host
/// crates' former hand-rolled copies of this logic depended on (real Office silently refuses to
/// render a shape/picture with no geometry element at all).
#[test]
fn round_trips_geometry_fill_line_effects_with_a_default_geometry_fallback() {
    let properties =
        ShapeProperties::new().with_fill(Fill::Solid(Color::Rgb("336699".to_string())));

    let read_back = roundtrip_geometry_fill_line_effects(&properties, "rect");

    assert_eq!(
        read_back.geometry,
        Some(Geometry::Preset(PresetShape::Rectangle))
    );
    assert_eq!(
        read_back.fill,
        Some(Fill::Solid(Color::Rgb("336699".to_string())))
    );
}

/// When `properties.geometry` is set, it is written as-is — the default preset fallback is never
/// consulted.
#[test]
fn round_trips_geometry_fill_line_effects_with_an_explicit_geometry() {
    let properties = ShapeProperties::new().with_geometry(Geometry::Preset(PresetShape::Ellipse));

    let read_back = roundtrip_geometry_fill_line_effects(&properties, "rect");

    assert_eq!(
        read_back.geometry,
        Some(Geometry::Preset(PresetShape::Ellipse))
    );
}

/// `effects` — the field whose silent write-side loss in `word-ooxml`'s and `excel-ooxml`'s own
/// former hand-rolled copies motivated factoring this function out in the first place — round-trips
/// alongside the geometry fallback and a fill.
#[test]
fn round_trips_geometry_fill_line_effects_with_effects() {
    let effects = EffectList::new().with_outer_shadow(
        OuterShadow::new(Color::Rgb("000000".to_string()))
            .with_blur_radius_emu(90_000)
            .with_distance_emu(50_000),
    );
    let properties = ShapeProperties::new()
        .with_fill(Fill::Solid(Color::Rgb("336699".to_string())))
        .with_effects(effects);

    let read_back = roundtrip_geometry_fill_line_effects(&properties, "rect");

    assert_eq!(
        read_back.geometry,
        Some(Geometry::Preset(PresetShape::Rectangle))
    );
    let read_effects = read_back.effects.expect("effects should round-trip");
    assert_eq!(
        read_effects.outer_shadow.as_ref().map(|s| s.color.clone()),
        Some(Color::Rgb("000000".to_string()))
    );
}