umya_spreadsheet/structs/
stylesheet.rs1use super::BordersCrate;
3use super::CellFormat;
4use super::CellFormats;
5use super::CellStyleFormats;
6use super::CellStyles;
7use super::Colors;
8use super::DifferentialFormats;
9use super::Fills;
10use super::Fonts;
11use super::NumberingFormats;
12use super::Protection;
13use super::Style;
14use crate::helper::const_str::*;
15use crate::reader::driver::*;
16use crate::writer::driver::*;
17use quick_xml::events::{BytesStart, Event};
18use quick_xml::Reader;
19use quick_xml::Writer;
20use std::io::Cursor;
21use thin_vec::ThinVec;
22
23#[derive(Clone, Default, Debug)]
24pub(crate) struct Stylesheet {
25 numbering_formats: NumberingFormats,
26 fonts: Fonts,
27 fills: Fills,
28 borders: BordersCrate,
29 cell_style_formats: CellStyleFormats,
30 cell_formats: CellFormats,
31 cell_styles: CellStyles,
32 differential_formats: DifferentialFormats,
33 colors: Colors,
34 maked_style_list: ThinVec<Style>,
35}
36
37impl Stylesheet {
38 #[inline]
39 pub(crate) fn _get_numbering_formats(&self) -> &NumberingFormats {
40 &self.numbering_formats
41 }
42
43 #[inline]
44 pub(crate) fn _get_numbering_formats_mut(&mut self) -> &mut NumberingFormats {
45 &mut self.numbering_formats
46 }
47
48 #[inline]
49 pub(crate) fn _set_numbering_formats(&mut self, value: NumberingFormats) -> &mut Self {
50 self.numbering_formats = value;
51 self
52 }
53
54 #[inline]
55 pub(crate) fn _get_fonts(&self) -> &Fonts {
56 &self.fonts
57 }
58
59 #[inline]
60 pub(crate) fn get_fonts_mut(&mut self) -> &mut Fonts {
61 &mut self.fonts
62 }
63
64 #[inline]
65 pub(crate) fn _set_fonts(&mut self, value: Fonts) -> &mut Self {
66 self.fonts = value;
67 self
68 }
69
70 #[inline]
71 pub(crate) fn _get_fills(&self) -> &Fills {
72 &self.fills
73 }
74
75 #[inline]
76 pub(crate) fn get_fills_mut(&mut self) -> &mut Fills {
77 &mut self.fills
78 }
79
80 #[inline]
81 pub(crate) fn _set_fills(&mut self, value: Fills) -> &mut Self {
82 self.fills = value;
83 self
84 }
85
86 #[inline]
87 pub(crate) fn _get_borders(&self) -> &BordersCrate {
88 &self.borders
89 }
90
91 #[inline]
92 pub(crate) fn get_borders_mut(&mut self) -> &mut BordersCrate {
93 &mut self.borders
94 }
95
96 #[inline]
97 pub(crate) fn _set_borders(&mut self, value: BordersCrate) -> &mut Self {
98 self.borders = value;
99 self
100 }
101
102 #[inline]
103 pub(crate) fn _get_cell_style_formats(&self) -> &CellStyleFormats {
104 &self.cell_style_formats
105 }
106
107 #[inline]
108 pub(crate) fn _get_cell_style_formats_mut(&mut self) -> &mut CellStyleFormats {
109 &mut self.cell_style_formats
110 }
111
112 #[inline]
113 pub(crate) fn _set_cell_style_formats(&mut self, value: CellStyleFormats) -> &mut Self {
114 self.cell_style_formats = value;
115 self
116 }
117
118 #[inline]
119 pub(crate) fn _get_cell_formats(&self) -> &CellFormats {
120 &self.cell_formats
121 }
122
123 #[inline]
124 pub(crate) fn _get_cell_formats_mut(&mut self) -> &mut CellFormats {
125 &mut self.cell_formats
126 }
127
128 #[inline]
129 pub(crate) fn _set_cell_formats(&mut self, value: CellFormats) -> &mut Self {
130 self.cell_formats = value;
131 self
132 }
133
134 #[inline]
135 pub(crate) fn _get_cell_styles(&self) -> &CellStyles {
136 &self.cell_styles
137 }
138
139 #[inline]
140 pub(crate) fn _get_cell_styles_mut(&mut self) -> &mut CellStyles {
141 &mut self.cell_styles
142 }
143
144 #[inline]
145 pub(crate) fn _set_cell_styles(&mut self, value: CellStyles) -> &mut Self {
146 self.cell_styles = value;
147 self
148 }
149
150 #[inline]
151 pub(crate) fn get_differential_formats(&self) -> &DifferentialFormats {
152 &self.differential_formats
153 }
154
155 #[inline]
156 pub(crate) fn get_differential_formats_mut(&mut self) -> &mut DifferentialFormats {
157 &mut self.differential_formats
158 }
159
160 #[inline]
161 pub(crate) fn _set_differential_formats(&mut self, value: DifferentialFormats) -> &mut Self {
162 self.differential_formats = value;
163 self
164 }
165
166 #[inline]
167 pub(crate) fn _get_colors(&self) -> &Colors {
168 &self.colors
169 }
170
171 #[inline]
172 pub(crate) fn _get_colors_mut(&mut self) -> &mut Colors {
173 &mut self.colors
174 }
175
176 #[inline]
177 pub(crate) fn _set_colors(&mut self, value: Colors) -> &mut Self {
178 self.colors = value;
179 self
180 }
181
182 #[inline]
183 pub(crate) fn get_style(&self, id: usize) -> Style {
184 self.maked_style_list.get(id).unwrap().clone()
185 }
186
187 pub(crate) fn make_style(&mut self) -> &mut Self {
188 for cell_format in self.cell_formats.get_cell_format() {
189 let def_cell_format = self
190 .cell_style_formats
191 .get_cell_format()
192 .get(*cell_format.get_format_id() as usize)
193 .cloned()
194 .unwrap_or_default();
195
196 let mut style = Style::default();
197 self.get_style_by_cell_format(&mut style, &def_cell_format, cell_format);
198 self.maked_style_list.push(style);
199 }
200
201 self
202 }
203
204 pub(crate) fn get_style_by_cell_format(
205 &self,
206 style: &mut Style,
207 def_cell_format: &CellFormat,
208 cell_format: &CellFormat,
209 ) {
210 let mut apply = true;
212 if def_cell_format.has_apply_number_format() {
213 apply = *def_cell_format.get_apply_number_format();
214 }
215 if cell_format.has_apply_number_format() {
216 apply = *cell_format.get_apply_number_format();
217 }
218 if apply {
219 let id = cell_format.get_number_format_id();
220 if let Some(obj) = self.numbering_formats.get_numbering_format().get(id) {
221 style.set_numbering_format(obj.clone());
222 }
223 }
224
225 let mut apply = true;
227 if def_cell_format.has_apply_font() {
228 apply = *def_cell_format.get_apply_font();
229 }
230 if cell_format.has_apply_font() {
231 apply = *cell_format.get_apply_font();
232 }
233 if apply {
234 let id = *cell_format.get_font_id() as usize;
235 let obj = self.fonts.get_font().get(id).unwrap();
236 style.set_font(obj.clone());
237 }
238
239 let mut apply = true;
241 if def_cell_format.has_apply_fill() {
242 apply = *def_cell_format.get_apply_fill();
243 }
244 if cell_format.has_apply_fill() {
245 apply = *cell_format.get_apply_fill();
246 }
247 if apply {
248 let id = *cell_format.get_fill_id() as usize;
249 let obj = self.fills.get_fill().get(id).unwrap();
250 style.set_fill(obj.clone());
251 }
252
253 let mut apply = true;
255 if def_cell_format.has_apply_border() {
256 apply = *def_cell_format.get_apply_border();
257 }
258 if cell_format.has_apply_border() {
259 apply = *cell_format.get_apply_border();
260 }
261 if apply {
262 let id = *cell_format.get_border_id() as usize;
263 let obj = self.borders.get_borders().get(id).unwrap();
264 style.set_borders(obj.clone());
265 }
266
267 style.set_format_id(*cell_format.get_format_id());
269
270 let mut apply = true;
272 if def_cell_format.has_apply_alignment() {
273 apply = *def_cell_format.get_apply_alignment();
274 }
275 if cell_format.has_apply_alignment() {
276 apply = *cell_format.get_apply_alignment();
277 }
278 if apply {
279 if let Some(v) = def_cell_format.get_alignment() {
280 style.set_alignment(v.clone());
281 }
282 if let Some(v) = cell_format.get_alignment() {
283 style.set_alignment(v.clone());
284 }
285 }
286
287 let mut apply = true;
289 if def_cell_format.has_apply_protection() {
290 apply = *def_cell_format.get_apply_protection();
291 }
292 if cell_format.has_apply_protection() {
293 apply = *cell_format.get_apply_protection();
294 }
295 if !apply {
296 return;
297 }
298
299 if let Some(v) = def_cell_format.get_protection() {
300 style.set_protection(v.clone());
301 }
302 if let Some(v) = cell_format.get_protection() {
303 style.set_protection(v.clone());
304 }
305 }
306
307 pub(crate) fn set_style(&mut self, style: &Style) -> u32 {
308 let mut index = 0;
309 let def_style = Style::default();
310 if style == &def_style {
311 return index;
312 }
313 for maked_style in &self.maked_style_list {
314 if style == maked_style {
315 return index;
316 }
317 index += 1;
318 }
319 let mut cell_format = CellFormat::default();
320
321 let number_format_id = self.numbering_formats.set_style(style);
322 let font_id = self.fonts.set_style(style);
323 let fill_id = self.fills.set_style(style);
324 let border_id = self.borders.set_style(style);
325 let format_id = 0;
326
327 cell_format.set_number_format_id(number_format_id);
328 cell_format.set_font_id(font_id);
329 cell_format.set_fill_id(fill_id);
330 cell_format.set_border_id(border_id);
331 cell_format.set_format_id(format_id);
332
333 if style.get_numbering_format().is_some() {
334 cell_format.set_apply_number_format(true);
335 }
336
337 if style.get_font().is_some() {
338 cell_format.set_apply_font(true);
339 }
340
341 if style.get_fill().is_some() {
342 cell_format.set_apply_fill(true);
343 }
344
345 if style.get_borders().is_some() {
346 cell_format.set_apply_border(true);
347 }
348
349 if let Some(v) = style.get_alignment() {
350 cell_format.set_alignment(v.clone());
351 cell_format.set_apply_alignment(true);
352 }
353
354 if let Some(v) = style.get_protection() {
355 cell_format.set_protection(v.clone());
356 cell_format.set_apply_protection(true);
357 }
358
359 self.maked_style_list.push(style.clone());
360 self.cell_formats.set_cell_format(cell_format);
361 index
362 }
363
364 pub(crate) fn set_defalut_value(&mut self) -> &mut Self {
365 let style = Style::get_default_value();
366 self.set_style(&style);
367 let style = Style::get_default_value_2();
368 self.set_style(&style);
369 self
370 }
371
372 pub(crate) fn set_attributes<R: std::io::BufRead>(
373 &mut self,
374 reader: &mut Reader<R>,
375 _e: &BytesStart,
376 ) {
377 self.numbering_formats.get_build_in_formats();
378
379 xml_read_loop!(
380 reader,
381 Event::Start(ref e) => {
382 match e.name().into_inner() {
383 b"numFmts" => {
384 self.numbering_formats.set_attributes(reader, e);
385 }
386 b"fonts" => {
387 self.fonts.set_attributes(reader, e);
388 }
389 b"fills" => {
390 self.fills.set_attributes(reader, e);
391 }
392 b"borders" => {
393 self.borders.set_attributes(reader, e);
394 }
395 b"cellStyleXfs" => {
396 self.cell_style_formats.set_attributes(reader, e);
397 }
398 b"cellXfs" => {
399 self.cell_formats.set_attributes(reader, e);
400 }
401 b"cellStyles" => {
402 self.cell_styles.set_attributes(reader, e);
403 }
404 b"dxfs" => {
405 self.differential_formats.set_attributes(reader, e);
406 }
407 b"colors" => {
408 self.colors.set_attributes(reader, e);
409 }
410 _ => (),
411 }
412 },
413 Event::End(ref e) => {
414 if e.name().into_inner() == b"styleSheet" {
415 return
416 }
417 },
418 Event::Eof => panic!("Error: Could not find {} end element", "styleSheet")
419 );
420 }
421
422 pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
423 write_start_tag(
425 writer,
426 "styleSheet",
427 vec![
428 ("xmlns", SHEET_MAIN_NS),
429 ("xmlns:mc", MC_NS),
430 ("mc:Ignorable", "x14ac"),
431 ("xmlns:x14ac", SHEETML_AC_NS),
432 ],
433 false,
434 );
435
436 self.numbering_formats.write_to(writer);
438
439 self.fonts.write_to(writer);
441
442 self.fills.write_to(writer);
444
445 self.borders.write_to(writer);
447
448 self.cell_style_formats.write_to(writer);
450
451 self.cell_formats.write_to(writer);
453
454 self.cell_styles.write_to(writer);
456
457 self.differential_formats.write_to(writer);
459
460 self.colors.write_to(writer);
462
463 write_start_tag(
465 writer,
466 "tableStyles",
467 vec![
468 ("count", "0"),
469 ("defaultTableStyle", "TableStyleMedium2"),
470 ("defaultPivotStyle", "PivotStyleMedium9"),
471 ],
472 true,
473 );
474
475 write_start_tag(writer, "extLst", vec![], false);
477
478 write_start_tag(
480 writer,
481 "ext",
482 vec![
483 ("uri", "{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}"),
484 ("xmlns:x14", SHEET_MS_MAIN_NS),
485 ],
486 false,
487 );
488
489 write_start_tag(
491 writer,
492 "x14:slicerStyles",
493 vec![("defaultSlicerStyle", "SlicerStyleLight1")],
494 true,
495 );
496
497 write_end_tag(writer, "ext");
498
499 write_end_tag(writer, "extLst");
500
501 write_end_tag(writer, "styleSheet");
502 }
503}