adic_shape/svg_doc/
tree_svg_doc.rs

1use svg::node::element as svg_el;
2
3use crate::TreeShape;
4use super::SvgDocDisplay;
5// use super::tree_labeller::TreeLabeller;
6
7
8/// SVG for an adic tree
9///
10/// ```
11/// # use adic::radic;
12/// # use adic_shape::{TreeShape, TreeShapeOptions, SvgDocDisplay};
13/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
14/// let neg_one_fourth = radic!(5, [], [1]);
15/// let num_digits = 6;
16/// let adic_tree = TreeShape::full_tree(5, num_digits, TreeShapeOptions::default())?;
17/// let tree_svg = adic_tree.create_svg_doc();
18/// let adic_tree = TreeShape::adic_number_full_tree(&neg_one_fourth, num_digits, TreeShapeOptions::default())?;
19/// let tree_svg = adic_tree.create_svg_doc();
20/// let adic_tree = TreeShape::zoomed_tree(&neg_one_fourth, num_digits, TreeShapeOptions::default())?;
21/// let tree_svg = adic_tree.create_svg_doc();
22/// # Ok(()) }
23/// ```
24impl SvgDocDisplay for TreeShape {
25
26    fn shape_style_els(
27        &self,
28    ) -> impl Iterator<Item=svg_el::Element> {
29        tree_style_instructions()
30    }
31
32}
33
34
35
36fn tree_style_instructions() -> impl Iterator<Item=svg_el::Element> {
37    let style_el = svg_el::Style::new("
38svg {
39    background: var(--tree-svg-background, white);
40}
41svg .tree-path {
42    fill: transparent;
43    stroke: var(--tree-path, black);
44    stroke-width: 0.3;
45}
46svg .gold-path {
47    fill: transparent;
48    stroke: var(--gold-path, darkorange);
49    stroke-width: 0.5;
50}
51"
52    );
53    std::iter::once(svg_el::Element::from(style_el))
54}
55
56
57
58#[cfg(test)]
59mod test {
60
61    use adic::zadic_approx;
62    use crate::{TreeShape, TreeShapeOptions, SvgDocDisplay};
63
64    #[test]
65    fn basic_tree() {
66
67        let adic_data = zadic_approx!(5, 2, [1, 3]);
68        let num_digits = 2;
69        let shape_options = TreeShapeOptions::default();
70
71        // Create the tree
72        let tree_shape = TreeShape::adic_number_full_tree(&adic_data, num_digits, shape_options).unwrap();
73
74        let tree = tree_shape.create_svg_doc();
75
76        let expected = [
77            "<svg class=\"adic-tree\" viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">",
78                "<style>",
79                      "svg {",
80                      "    background: var(--tree-svg-background, white);",
81                      "}",
82                      "svg .tree-path {",
83                      "    fill: transparent;",
84                      "    stroke: var(--tree-path, black);",
85                      "    stroke-width: 0.3;",
86                      "}",
87                      "svg .gold-path {",
88                      "    fill: transparent;",
89                      "    stroke: var(--gold-path, darkorange);",
90                      "    stroke-width: 0.5;",
91                      "}",
92                  "</style>",
93                  &[
94                      "<path class=\"tree-path\" d=\"",
95                          "M 100 100 L 50 80 M 50 80 L 9.999999999999998 40 M 9.999999999999998 40 L 1.9999999999999962 0 M 9.999999999999998 40 L 5.999999999999997 0 ",
96                          "M 9.999999999999998 40 L 9.999999999999998 0 M 9.999999999999998 40 L 13.999999999999996 0 M 9.999999999999998 40 L 18 0 M 50 80 L 30 40 ",
97                          "M 30 40 L 21.999999999999996 0 M 30 40 L 26 0 M 30 40 L 30 0 M 30 40 L 34 0 M 30 40 L 38 0 M 50 80 L 50 40 M 50 40 L 42 0 M 50 40 L 46 0 ",
98                          "M 50 40 L 50 0 M 50 40 L 54 0 M 50 40 L 58.00000000000001 0 M 50 80 L 70 40 M 70 40 L 61.999999999999986 0 M 70 40 L 65.99999999999999 0 ",
99                          "M 70 40 L 70 0 M 70 40 L 74 0 M 70 40 L 78 0 M 50 80 L 90 40 M 90 40 L 82 0 M 90 40 L 86 0 M 90 40 L 90 0 M 90 40 L 94 0 M 90 40 L 98 0",
100                      "\"/>",
101                  ].join(""),
102                  "<path class=\"gold-path\" d=\"M 100 100 L 50 80 M 50 80 L 30 40 M 30 40 L 34 0\"/>",
103            "</svg>",
104        ].join("\n");
105
106        for (e, t) in expected.split('\n').zip(tree.to_string().split('\n')) {
107            assert_eq!(e, t);
108        }
109
110        assert_eq!(expected, tree.to_string());
111
112    }
113
114}