d2-rs-math 0.1.13

Rust port of D2's d2latex package and mathjax-go: LaTeX layout and SVG.
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
// SPDX-License-Identifier: MPL-2.0
// Source: D2 d2renderers/d2svgimport/compile.go, boilerplate.go,
// stylesheet.go. Parsed-tree compilation admission, before compileElement.
use super::{
    svg_clip,
    svg_declarations::{Rule, TransformOriginRule},
    svg_geometry::trim_space,
    svg_gradient_tree, svg_stylesheet,
    svg_xml_document::Document,
};
fn context(cancel: Option<&d2_rs_core::CancellationToken>) -> Result<(), String> {
    match cancel.and_then(d2_rs_core::CancellationToken::error) {
        Some(d2_rs_core::LayoutError::DeadlineExceeded) => Err("context deadline exceeded".into()),
        Some(_) => Err("context canceled".into()),
        None => Ok(()),
    }
}
pub fn non_rendering(name: &str) -> bool {
    matches!(
        name,
        "animate"
            | "animateColor"
            | "animateTransform"
            | "animateMotion"
            | "set"
            | "mpath"
            | "title"
            | "desc"
            | "dc:title"
            | "metadata"
            | "rdf:RDF"
            | "cc:Work"
            | "dc:format"
            | "dc:type"
            | "sodipodi:namedview"
    )
}
fn display(name: &str) -> &str {
    if name.len() > 64 {
        "<oversized-name>"
    } else {
        name
    }
}
/// The source performs topology/text validation in child order, then a separate
/// cardinality walk whose sibling order is reversed. Keep their error precedence.
pub fn validate_metadata(
    source: &str,
    document: &Document,
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> Result<(), String> {
    let error = |reason: String| format!("d2svgimport: {source}: {reason}");
    let mut stack = vec![(0, None)];
    while let Some((index, parent)) = stack.pop() {
        context(cancel)?;
        let element = &document.elements[index];
        let parent_name = parent.map(|i: usize| document.elements[i].name.as_str());
        if let Some(name) = parent_name {
            match name {
                "metadata" if element.name != "rdf:RDF" => {
                    return Err(error(format!(
                        "element <metadata> has unsupported child <{}>",
                        display(&element.name)
                    )));
                }
                "rdf:RDF" if element.name != "cc:Work" => {
                    return Err(error(format!(
                        "element <rdf:RDF> has unsupported child <{}>",
                        display(&element.name)
                    )));
                }
                "cc:Work"
                    if !matches!(element.name.as_str(), "dc:format" | "dc:type" | "dc:title") =>
                {
                    return Err(error(format!(
                        "element <cc:Work> has unsupported child <{}>",
                        display(&element.name)
                    )));
                }
                "title" | "desc" | "dc:format" | "dc:type" | "dc:title" | "sodipodi:namedview" => {
                    return Err(error(format!(
                        "non-rendering element <{name}> cannot contain child elements"
                    )));
                }
                _ => {}
            }
        }
        match element.name.as_str() {
            "metadata" | "sodipodi:namedview" if parent != Some(0) => {
                return Err(error(format!(
                    "non-rendering element <{}> must be a direct child of the root <svg>",
                    element.name
                )));
            }
            "rdf:RDF" if parent_name != Some("metadata") => {
                return Err(error(
                    "element <rdf:RDF> is only supported inside <metadata>".into(),
                ));
            }
            "cc:Work" if parent_name != Some("rdf:RDF") => {
                return Err(error(
                    "element <cc:Work> is only supported inside <rdf:RDF>".into(),
                ));
            }
            "dc:format" | "dc:type" | "dc:title" if parent_name != Some("cc:Work") => {
                return Err(error(format!(
                    "element <{}> is only supported inside <cc:Work>",
                    element.name
                )));
            }
            _ => {}
        }
        if element.name == "dc:format" {
            let text = String::from_utf8_lossy(&element.text);
            if trim_space(&text, cancel)? != "image/svg+xml" {
                return Err(error(
                    "element <dc:format> has unsupported metadata text".into(),
                ));
            }
        }
        stack.extend(
            element
                .children
                .iter()
                .rev()
                .map(|&child| (child, Some(index))),
        );
    }
    let mut stack = vec![0];
    while let Some(index) = stack.pop() {
        context(cancel)?;
        let element = &document.elements[index];
        let name = |position: usize| document.elements[element.children[position]].name.as_str();
        match element.name.as_str() {
            "metadata" if element.children.len() != 1 || name(0) != "rdf:RDF" => {
                return Err(error(
                    "element <metadata> must contain exactly one <rdf:RDF>".into(),
                ));
            }
            "rdf:RDF" if element.children.len() != 1 || name(0) != "cc:Work" => {
                return Err(error(
                    "element <rdf:RDF> must contain exactly one <cc:Work>".into(),
                ));
            }
            "cc:Work" => {
                let mut seen = std::collections::HashSet::new();
                for &child in &element.children {
                    let name = &document.elements[child].name;
                    if !seen.insert(name) {
                        return Err(error(format!(
                            "element <cc:Work> contains duplicate <{name}>"
                        )));
                    }
                }
            }
            _ => {}
        }
        stack.extend(element.children.iter().copied());
    }
    Ok(())
}
/// Source compile's first four passes. Earlier stylesheet rules survive a later
/// failure, as in the importer. Returned resource records retain parsed identity,
/// attribute order and IDs for subsequent element/resource compilation.
pub fn prepare(
    source: &str,
    document: &Document,
    rules: &mut Vec<Rule>,
    origins: &mut Vec<TransformOriginRule>,
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> Result<Vec<svg_gradient_tree::Element>, String> {
    validate_metadata(source, document, cancel)?;
    let mut elements = Vec::with_capacity(document.elements.len());
    for node in &document.elements {
        context(cancel)?;
        elements.push(svg_gradient_tree::Element {
            name: node.name.clone(),
            id: node.attributes.id.clone(),
            children: node.children.clone(),
            attrs: node.attributes.values.clone(),
            order: node.attributes.order.clone(),
            ..Default::default()
        });
    }
    svg_gradient_tree::validate(source, &elements, 0, cancel)?;
    svg_clip::validate_tree(source, &elements, 0, cancel)?;
    let mut stack = vec![0];
    while let Some(index) = stack.pop() {
        context(cancel)?;
        let node = &document.elements[index];
        if node.name == "style" {
            svg_stylesheet::validate_element(
                source,
                &node.attributes.values,
                &node.attributes.order,
                node.children.len(),
                cancel,
            )?;
            let text = String::from_utf8_lossy(&node.text);
            let parsed = svg_stylesheet::parse(source, &text, cancel)?;
            rules.extend(parsed.rules);
            origins.extend(parsed.transform_origin);
        }
        stack.extend(node.children.iter().rev().copied());
    }
    Ok(elements)
}
#[cfg(all(test, feature = "oracle-fixtures"))]
mod tests {
    use super::*;
    #[cfg(feature = "oracle-fixtures")]
    #[test]
    fn original_parsed_tree_compilation_admission() {
        let rows: serde_json::Value = serde_json::from_str(d2_rs_testdata::oracle_fixture!(
            "tests/fixtures/svg-compile-admission-v090.json"
        ))
        .unwrap();
        for row in rows.as_array().unwrap() {
            let mut rules = Vec::new();
            let mut origins = Vec::new();
            let result = super::super::svg_xml_document::parse(
                "math",
                row["input"].as_str().unwrap(),
                [10000, 100, 100, 100, 10000, 100, 100, 100, 100],
                None,
            )
            .and_then(|document| prepare("math", &document, &mut rules, &mut origins, None));
            assert_eq!(
                result.as_ref().err().map(String::as_str).unwrap_or(""),
                row["error"].as_str().unwrap(),
                "{row}"
            );
            assert_eq!(serde_json::to_value(rules).unwrap(), row["rules"], "{row}");
            if let Ok(elements) = result {
                assert_eq!(
                    elements.len() as u64,
                    row["elements"].as_u64().unwrap(),
                    "{row}"
                );
            }
        }
    }
}
/// Owned state of the source element pass. An image continuation preserves the
/// exact point where asset resolution must run before later element work.
pub struct Compilation {
    pub image_assets: Option<super::svg_image::Assets>,
    pub clips: std::collections::BTreeMap<usize, super::svg_clip::Template>,
    pub elements: Vec<svg_gradient_tree::Element>,
    pub states: Vec<super::svg_element::State>,
    pub geometry: Vec<super::svg_geometry::Geometry>,
    pub use_targets: Vec<Option<usize>>,
    pub use_positions: Vec<[f64; 2]>,
    pub rules: Vec<Rule>,
    pub transform_origin: Vec<TransformOriginRule>,
    pub work: super::svg_element::Work,
    pub parsed_commands: usize,
}
#[derive(Debug, PartialEq)]
pub enum Progress {
    Complete,
    Image(usize),
}
impl Compilation {
    pub fn new(document: &Document) -> Self {
        Self {
            image_assets: None,
            clips: Default::default(),
            elements: document
                .elements
                .iter()
                .map(|node| svg_gradient_tree::Element {
                    name: node.name.clone(),
                    id: node.attributes.id.clone(),
                    children: node.children.clone(),
                    attrs: node.attributes.values.clone(),
                    order: node.attributes.order.clone(),
                    ..Default::default()
                })
                .collect(),
            states: (0..document.elements.len())
                .map(|_| Default::default())
                .collect(),
            geometry: (0..document.elements.len())
                .map(|_| Default::default())
                .collect(),
            use_targets: vec![None; document.elements.len()],
            use_positions: vec![[0.; 2]; document.elements.len()],
            rules: Vec::new(),
            transform_origin: Vec::new(),
            work: Default::default(),
            parsed_commands: 0,
        }
    }
}
impl Compilation {
    /// Complete image admission; `new` retains the historical phase boundary.
    pub fn with_images(document: &Document) -> Self {
        let mut output = Self::new(document);
        output.image_assets = Some(Default::default());
        output
    }
}
pub fn compile_elements(
    source: &str,
    document: &Document,
    output: &mut Compilation,
    limits: [usize; 9],
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> Result<Progress, String> {
    output.elements = prepare(
        source,
        document,
        &mut output.rules,
        &mut output.transform_origin,
        cancel,
    )?;
    let ids = document
        .ids
        .iter()
        .map(|(id, &index)| {
            (
                id.clone(),
                (
                    index,
                    document
                        .elements
                        .get(index)
                        .map(|e| e.name.clone())
                        .unwrap_or_default(),
                ),
            )
        })
        .collect();
    let mut stack = vec![0];
    while let Some(index) = stack.pop() {
        context(cancel)?;
        let rendering = document.elements[index]
            .children
            .iter()
            .any(|&child| !non_rendering(&document.elements[child].name));
        let next = super::svg_element::admit(
            source,
            &mut output.elements[index],
            &mut output.states[index],
            index == 0,
            rendering,
            &output.rules,
            &output.transform_origin,
            limits[4],
            limits[6],
            &mut output.work,
            cancel,
        )?;
        if matches!(
            output.elements[index].name.as_str(),
            "filter" | "feGaussianBlur"
        ) {
            super::svg_effects::compile_filter(
                source,
                document,
                &output.elements,
                &mut output.states,
                index,
                cancel,
            )?;
        }
        if next == super::svg_element::Continuation::Geometry {
            let name = output.elements[index].name.as_str();
            if name == "mask" {
                output.states[index].mask_region = Some(super::svg_effects::compile_mask(
                    source,
                    &output.elements[index],
                )?);
                stack.extend(document.elements[index].children.iter().rev().copied());
                continue;
            }
            if name == "image" {
                let (_, aspect) = super::svg_image::geometry(
                    source,
                    &output.elements[index].attrs,
                    document.elements[index].children.len(),
                    cancel,
                )?;
                // image.go:62: only a trimmed "visible" overflow without slice fit.
                if output.elements[index]
                    .declarations
                    .get("overflow")
                    .is_some_and(|raw| raw.trim() != "visible" || aspect[1] == 1)
                {
                    return Err(format!(
                        "d2svgimport: {source}: unsupported overflow on <image>"
                    ));
                }
                if !output.elements[index].attrs["href"].starts_with("data:") {
                    let mut ancestor = Some(index);
                    let mut hidden = false;
                    while let Some(current) = ancestor {
                        hidden |= output.elements[current]
                            .declarations
                            .get("display")
                            .is_some_and(|v| v.eq_ignore_ascii_case("none"));
                        ancestor = document.elements[current].parent;
                    }
                    if hidden {
                        output.geometry[index] = super::svg_geometry::Geometry {
                            kind: 6,
                            ..Default::default()
                        };
                        continue;
                    }
                }
            }
            if name == "image" && output.image_assets.is_none() {
                return Ok(Progress::Image(index));
            }
            output.geometry[index] = if name == "image" {
                let image = super::svg_image::compile(
                    source,
                    &output.elements[index].attrs,
                    document.elements[index].children.len(),
                    limits[0],
                    limits[8],
                    document.declared_ids(),
                    output.image_assets.as_mut().unwrap(),
                    cancel,
                )?;
                super::svg_geometry::Geometry {
                    kind: 4,
                    bounds: image.bounds,
                    asset: image.content_id,
                    aspect: image.aspect,
                    ..Default::default()
                }
            } else if matches!(name, "text" | "tspan") {
                if !super::svg_text::frozen(document, output.work.mathjax_root, index) {
                    super::svg_text::validate(source, document, index)?;
                    output.geometry[index].kind = 5;
                    stack.extend(document.elements[index].children.iter().rev().copied());
                    continue;
                }
                let commands = super::svg_frozen_text::commands(
                    source,
                    document,
                    &output.elements,
                    &output.states,
                    output.work.mathjax_root,
                    index,
                    limits[5],
                    cancel,
                )?;
                super::svg_geometry::Geometry {
                    kind: 3,
                    commands,
                    ..Default::default()
                }
            } else {
                super::svg_geometry::compile(
                    source,
                    name,
                    &output.elements[index].attrs,
                    limits[4],
                    limits[5],
                    &mut output.parsed_commands,
                    cancel,
                )?
            };
            if name == "use" {
                let attrs = &output.elements[index].attrs;
                let target = super::svg_use::resolve(
                    source,
                    attrs.get("href").map(String::as_str),
                    &ids,
                    cancel,
                )?;
                output.use_targets[index] = Some(target);
                for (axis, key) in ["x", "y"].iter().enumerate() {
                    output.use_positions[index][axis] = 0.;
                    if let Some(raw) = attrs.get(*key) {
                        output.use_positions[index][axis]=super::svg_length::length(raw,true).map_err(|reason|format!("d2svgimport: {source}: element <use> has invalid {key}: {reason}"))?;
                    }
                }
                if let Some(raw) = attrs.get("overflow") {
                    if trim_space(raw, cancel)? != "visible" {
                        return Err(format!(
                            "d2svgimport: {source}: element <use> supports only overflow=\"visible\""
                        ));
                    }
                }
            }
        }
        stack.extend(document.elements[index].children.iter().rev().copied());
    }
    Ok(Progress::Complete)
}
#[cfg(all(test, feature = "oracle-fixtures"))]
mod elements_pass_tests {
    use super::*;
    #[cfg(feature = "oracle-fixtures")]
    #[test]
    fn original_ordered_element_geometry_use_pass_and_partial_state() {
        let rows: serde_json::Value = serde_json::from_str(d2_rs_testdata::oracle_fixture!(
            "tests/fixtures/svg-elements-pass-v090.json"
        ))
        .unwrap();
        for row in rows.as_array().unwrap() {
            let maximum = row["maximum"].as_u64().unwrap() as usize;
            let limits = [10000, 100, 100, 100, 10000, maximum, maximum, 100, 100];
            let document = super::super::svg_xml_document::parse(
                "math",
                row["input"].as_str().unwrap(),
                limits,
                None,
            )
            .unwrap();
            let mut output = Compilation::new(&document);
            let result = compile_elements("math", &document, &mut output, limits, None);
            assert_eq!(
                result.as_ref().err().map(String::as_str).unwrap_or(""),
                row["error"].as_str().unwrap(),
                "{row}"
            );
            let pending = match result {
                Ok(Progress::Image(index)) => index as i64,
                _ => -1,
            };
            assert_eq!(pending, row["pending_image"].as_i64().unwrap(), "{row}");
            assert_eq!(
                output.parsed_commands as u64,
                row["commands"].as_u64().unwrap(),
                "{row}"
            );
            assert_eq!(
                output.work.transforms as u64,
                row["transforms"].as_u64().unwrap(),
                "{row}"
            );
            assert_eq!(
                output.work.mathjax_root,
                row["mathjax"].as_bool().unwrap(),
                "{row}"
            );
            for (index, expected) in row["states"].as_array().unwrap().iter().enumerate() {
                let element = &output.elements[index];
                assert_eq!(
                    serde_json::to_value(&element.classes).unwrap(),
                    expected["classes"],
                    "{row}: {index}"
                );
                assert_eq!(
                    serde_json::to_value(&element.declarations).unwrap(),
                    expected["declarations"],
                    "{row}: {index}"
                );
                let state: super::super::svg_element::State =
                    serde_json::from_value(expected["state"].clone()).unwrap();
                assert_eq!(output.states[index], state, "{row}: {index}");
                let geometry: super::super::svg_geometry::Geometry =
                    serde_json::from_value(expected["geometry"].clone()).unwrap();
                assert_eq!(output.geometry[index], geometry, "{row}: {index}");
                let target: Option<usize> =
                    serde_json::from_value(expected["target"].clone()).unwrap();
                assert_eq!(output.use_targets[index], target, "{row}: {index}");
                let position: [f64; 2] =
                    serde_json::from_value(expected["position"].clone()).unwrap();
                assert_eq!(output.use_positions[index], position, "{row}: {index}");
                let gradient: Option<super::super::svg_primitive::Paint> =
                    serde_json::from_value(expected["gradient"].clone()).unwrap();
                assert_eq!(element.gradient, gradient, "{row}: {index}");
                let stop: Option<super::super::svg_primitive::Stop> =
                    serde_json::from_value(expected["stop"].clone()).unwrap();
                assert_eq!(element.stop, stop, "{row}: {index}");
            }
        }
    }
}
/// Complete source compile stages after the ordered element pass. Images remain
/// an explicit asset-resolution continuation, with no later work performed.
pub fn compile(
    source: &str,
    document: &Document,
    output: &mut Compilation,
    limits: [usize; 9],
    current_color: [u8; 4],
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> Result<Progress, String> {
    let progress = compile_elements(source, document, output, limits, cancel)?;
    if progress != Progress::Complete {
        return Ok(progress);
    }
    let mut stack = vec![0];
    while let Some(index) = stack.pop() {
        context(cancel)?;
        if matches!(
            output.elements[index].name.as_str(),
            "linearGradient" | "radialGradient"
        ) {
            let stops = output.elements[index]
                .children
                .iter()
                .map(|&child| output.elements[child].stop.clone())
                .collect::<Vec<_>>();
            let gradient = output.elements[index].gradient.as_mut().ok_or_else(|| {
                format!("d2svgimport: {source}: internal uncompiled linear gradient")
            })?;
            super::svg_gradient::attach_stops(source, gradient, &stops, cancel)?;
        }
        stack.extend(document.elements[index].children.iter().rev().copied());
    }
    let resources = document
        .ids
        .iter()
        .map(|(id, &index)| {
            (
                id.clone(),
                super::svg_resources::Resource {
                    name: output
                        .elements
                        .get(index)
                        .map(|e| e.name.clone())
                        .unwrap_or_default(),
                    gradient: output.elements.get(index).and_then(|e| e.gradient.clone()),
                },
            )
        })
        .collect();
    super::svg_gradient_stop::validate_rules(source, &output.rules, &resources, cancel)?;
    let uses = output
        .elements
        .iter()
        .enumerate()
        .map(|(index, element)| super::svg_use::Element {
            children: element.children.clone(),
            is_use: element.name == "use",
            target: output.use_targets[index],
        })
        .collect::<Vec<_>>();
    super::svg_use::validate(source, &uses, 0, limits[7], cancel)?;
    validate_style_tree(source, document, output, &resources, current_color, cancel)?;
    let inputs = output
        .elements
        .iter()
        .enumerate()
        .map(|(index, element)| super::svg_clip::Input {
            name: element.name.clone(),
            id: element.id.clone(),
            children: element.children.clone(),
            declarations: element.declarations.clone(),
            transform: output.states[index].transform,
            geometry: output.geometry[index].clone(),
            href: if output.use_targets[index].is_some() {
                element.attrs["href"][1..].into()
            } else {
                String::new()
            },
            use_position: output.use_positions[index],
        })
        .collect::<Vec<_>>();
    super::svg_clip::compile_tree(
        source,
        &inputs,
        0,
        &document.ids,
        &mut output.clips,
        limits[7],
        limits[8],
        limits[5],
        cancel,
    )?;
    Ok(Progress::Complete)
}
fn validate_style_tree(
    source: &str,
    document: &Document,
    output: &Compilation,
    resources: &std::collections::BTreeMap<String, super::svg_resources::Resource>,
    current_color: [u8; 4],
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> Result<(), String> {
    let mut parent = super::svg_style::Style::default();
    parent.paint.color = current_color;
    let mut stack = vec![(0, parent)];
    while let Some((index, parent)) = stack.pop() {
        context(cancel)?;
        let element = &output.elements[index];
        let name = element.name.as_str();
        if non_rendering(name)
            || matches!(
                name,
                "linearGradient"
                    | "radialGradient"
                    | "stop"
                    | "clipPath"
                    | "filter"
                    | "feGaussianBlur"
            )
        {
            continue;
        }
        let style = super::svg_style::compute(
            source,
            name,
            &parent,
            &element.declarations,
            resources,
            cancel,
        )?;
        if name == "text" && super::svg_text::frozen(document, output.work.mathjax_root, index) {
            super::svg_frozen_text::validate_style(source, &style)?;
        }
        validate_stroke_caps(source, name, &output.geometry[index], &style, cancel)?;
        for &child in document.elements[index].children.iter().rev() {
            stack.push((child, style.clone()));
        }
    }
    Ok(())
}
/// Source rejects zero-length open stroked subpaths for round/square caps.
/// Closed paths and curves whose control points have extent remain supported.
pub fn validate_stroke_caps(
    source: &str,
    name: &str,
    geometry: &super::svg_geometry::Geometry,
    style: &super::svg_style::Style,
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> Result<(), String> {
    if geometry.kind != 3
        || style.paint.cap == 0
        || style.paint.stroke_width <= 0.
        || (style.paint.stroke.is_none() && !style.paint.stroke_current)
    {
        return Ok(());
    }
    let error = || {
        format!(
            "d2svgimport: {source}: element <{name}> has a zero-length open stroked subpath with unsupported round or square linecap"
        )
    };
    let mut current = [0.; 2];
    let mut start = [0.; 2];
    let mut have = false;
    let mut extent = false;
    let mut closed = false;
    for (index, command) in geometry.commands.iter().enumerate() {
        if index & 255 == 0 {
            context(cancel)?;
        }
        match command.kind {
            0 => {
                if have && !extent && !closed {
                    return Err(error());
                }
                current = command.points[0];
                start = current;
                have = true;
                extent = false;
                closed = false;
            }
            1 | 4 => {
                extent |= command.points[0] != current;
                current = command.points[0];
            }
            2 => {
                extent |= command.points[0] != current || command.points[1] != current;
                current = command.points[1];
            }
            3 => {
                extent |= command.points.iter().any(|point| *point != current);
                current = command.points[2];
            }
            5 => {
                extent |= current != start;
                current = start;
                closed = true;
            }
            _ => {}
        }
    }
    if have && !extent && !closed {
        return Err(error());
    }
    Ok(())
}

#[cfg(all(test, feature = "oracle-fixtures"))]
mod pipeline_tests {
    use super::*;
    #[test]
    fn original_connected_compile_pipeline_and_partial_resources() {
        let rows: serde_json::Value = serde_json::from_str(d2_rs_testdata::oracle_fixture!(
            "tests/fixtures/svg-compile-pipeline-v090.json"
        ))
        .unwrap();
        for row in rows.as_array().unwrap() {
            let maximum = row["maximum"].as_u64().unwrap() as usize;
            let limits = [10000, 100, 100, 100, 10000, maximum, maximum, maximum, 100];
            let document = super::super::svg_xml_document::parse(
                "math",
                row["input"].as_str().unwrap(),
                limits,
                None,
            )
            .unwrap();
            let mut output = Compilation::new(&document);
            let result = compile("math", &document, &mut output, limits, [1, 2, 3, 200], None);
            assert_eq!(
                result.as_ref().err().map(String::as_str).unwrap_or(""),
                row["error"].as_str().unwrap(),
                "{row}"
            );
            let pending = match result {
                Ok(Progress::Image(index)) => index as i64,
                _ => -1,
            };
            assert_eq!(pending, row["pending_image"].as_i64().unwrap(), "{row}");
            assert_eq!(
                output.parsed_commands as u64,
                row["commands"].as_u64().unwrap(),
                "{row}"
            );
            assert_eq!(
                output.work.transforms as u64,
                row["transforms"].as_u64().unwrap(),
                "{row}"
            );
            assert_eq!(
                output.work.mathjax_root,
                row["mathjax"].as_bool().unwrap(),
                "{row}"
            );
            for (index, expected) in row["states"].as_array().unwrap().iter().enumerate() {
                let element = &output.elements[index];
                let clip: Option<super::super::svg_clip::Template> =
                    serde_json::from_value(expected["clip"].clone()).unwrap();
                assert_eq!(output.clips.get(&index), clip.as_ref(), "{row}: {index}");
                assert_eq!(
                    serde_json::to_value(&element.classes).unwrap(),
                    expected["classes"],
                    "{row}: {index}"
                );
                assert_eq!(
                    serde_json::to_value(&element.declarations).unwrap(),
                    expected["declarations"],
                    "{row}: {index}"
                );
                let state: super::super::svg_element::State =
                    serde_json::from_value(expected["state"].clone()).unwrap();
                assert_eq!(output.states[index], state, "{row}: {index}");
                let geometry: super::super::svg_geometry::Geometry =
                    serde_json::from_value(expected["geometry"].clone()).unwrap();
                assert_eq!(output.geometry[index], geometry, "{row}: {index}");
                let target: Option<usize> =
                    serde_json::from_value(expected["target"].clone()).unwrap();
                assert_eq!(output.use_targets[index], target, "{row}: {index}");
                let position: [f64; 2] =
                    serde_json::from_value(expected["position"].clone()).unwrap();
                assert_eq!(output.use_positions[index], position, "{row}: {index}");
                let gradient: Option<super::super::svg_primitive::Paint> =
                    serde_json::from_value(expected["gradient"].clone()).unwrap();
                assert_eq!(element.gradient, gradient, "{row}: {index}");
                let stop: Option<super::super::svg_primitive::Stop> =
                    serde_json::from_value(expected["stop"].clone()).unwrap();
                assert_eq!(element.stop, stop, "{row}: {index}");
            }
        }
    }
}