umya_spreadsheet/structs/drawing/
fill_style_list.rs

1use super::GradientFill;
2use super::SolidFill;
3use crate::reader::driver::*;
4use crate::writer::driver::*;
5use quick_xml::events::{BytesStart, Event};
6use quick_xml::Reader;
7use quick_xml::Writer;
8use std::io::Cursor;
9use thin_vec::ThinVec;
10
11#[derive(Clone, Default, Debug)]
12pub struct FillStyleList {
13    solid_fill: ThinVec<SolidFill>,
14    gradient_fill_collection: ThinVec<GradientFill>,
15}
16
17impl FillStyleList {
18    #[inline]
19    pub fn get_solid_fill(&self) -> &[SolidFill] {
20        &self.solid_fill
21    }
22
23    #[inline]
24    pub fn get_solid_fill_mut(&mut self) -> &mut ThinVec<SolidFill> {
25        &mut self.solid_fill
26    }
27
28    #[inline]
29    pub fn set_solid_fill(&mut self, value: impl Into<ThinVec<SolidFill>>) -> &mut Self {
30        self.solid_fill = value.into();
31        self
32    }
33
34    #[inline]
35    pub fn add_solid_fill(&mut self, value: SolidFill) -> &mut Self {
36        self.solid_fill.push(value);
37        self
38    }
39
40    #[inline]
41    pub fn get_gradient_fill_collection(&self) -> &[GradientFill] {
42        &self.gradient_fill_collection
43    }
44
45    #[inline]
46    pub fn get_gradient_fill_collectionl_mut(&mut self) -> &mut ThinVec<GradientFill> {
47        &mut self.gradient_fill_collection
48    }
49
50    #[inline]
51    pub fn set_gradient_fill_collection(
52        &mut self,
53        value: impl Into<ThinVec<GradientFill>>,
54    ) -> &mut Self {
55        self.gradient_fill_collection = value.into();
56        self
57    }
58
59    #[inline]
60    pub fn add_gradient_fill_collection(&mut self, value: GradientFill) -> &mut Self {
61        self.gradient_fill_collection.push(value);
62        self
63    }
64
65    pub(crate) fn set_attributes<R: std::io::BufRead>(
66        &mut self,
67        reader: &mut Reader<R>,
68        _e: &BytesStart,
69    ) {
70        xml_read_loop!(
71            reader,
72            Event::Start(ref e) => {
73                match e.name().into_inner() {
74                    b"a:solidFill" => {
75                        let mut obj = SolidFill::default();
76                        obj.set_attributes(reader, e);
77                        self.solid_fill.push(obj);
78                    }
79                    b"a:gradFill" => {
80                        let mut obj = GradientFill::default();
81                        obj.set_attributes(reader, e);
82                        self.gradient_fill_collection.push(obj);
83                    }
84                    _ => (),
85                }
86            },
87            Event::End(ref e) => {
88                if e.name().into_inner() == b"a:fillStyleLst" {
89                    return
90                }
91            },
92            Event::Eof => panic!("Error: Could not find {} end element", "fillStyleLst")
93        );
94    }
95
96    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
97        // a:fillStyleLst
98        write_start_tag(writer, "a:fillStyleLst", vec![], false);
99
100        // a:solidFill
101        for v in &self.solid_fill {
102            v.write_to(writer);
103        }
104
105        // a:gradFill
106        for v in &self.gradient_fill_collection {
107            v.write_to(writer);
108        }
109
110        write_end_tag(writer, "a:fillStyleLst");
111    }
112}