umya-spreadsheet 3.0.0

umya-spreadsheet is a library written in pure Rust to read and write xlsx file.
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
// front
use std::{
    io::Cursor,
    str::FromStr,
};

use md5::Digest;
use quick_xml::{
    Reader,
    Writer,
    events::{
        BytesStart,
        Event,
    },
};

use super::{
    Bold,
    Color,
    FontCharSet,
    FontFamilyNumbering,
    FontName,
    FontScheme,
    FontSchemeValues,
    FontSize,
    Italic,
    Strike,
    Underline,
    UnderlineValues,
    VerticalTextAlignment,
};
use crate::writer::driver::{
    write_end_tag,
    write_start_tag,
};

#[derive(Clone, Default, Debug, PartialEq, PartialOrd)]
pub struct Font {
    font_name:               FontName,
    font_size:               FontSize,
    font_family_numbering:   FontFamilyNumbering,
    font_bold:               Bold,
    font_italic:             Italic,
    font_underline:          Underline,
    font_strike:             Strike,
    color:                   Color,
    font_char_set:           FontCharSet,
    font_scheme:             FontScheme,
    vertical_text_alignment: VerticalTextAlignment,
}
impl Font {
    // Charset
    pub const CHARSET_ANSI: i32 = 0;
    pub const CHARSET_ARABIC: i32 = 178;
    pub const CHARSET_BALTIC: i32 = 186;
    pub const CHARSET_CHINESEBIG5: i32 = 136;
    pub const CHARSET_DEFAULT: i32 = 1;
    pub const CHARSET_EASTEUROPE: i32 = 238;
    pub const CHARSET_GB2312: i32 = 134;
    pub const CHARSET_GREEK: i32 = 161;
    pub const CHARSET_HANGEUL: i32 = 129;
    pub const CHARSET_HANGUL: i32 = 129;
    pub const CHARSET_HEBREW: i32 = 177;
    pub const CHARSET_JOHAB: i32 = 130;
    pub const CHARSET_MAC: i32 = 77;
    pub const CHARSET_OEM: i32 = 255;
    pub const CHARSET_RUSSIAN: i32 = 204;
    pub const CHARSET_SHIFTJIS: i32 = 128;
    pub const CHARSET_SYMBOL: i32 = 2;
    pub const CHARSET_THAI: i32 = 222;
    pub const CHARSET_TURKISH: i32 = 162;
    pub const CHARSET_VIETNAMESE: i32 = 163;
    pub const UNDERLINE_DOUBLE: &'static str = "double";
    pub const UNDERLINE_DOUBLEACCOUNTING: &'static str = "doubleAccounting";
    // Underline types
    pub const UNDERLINE_NONE: &'static str = "none";
    pub const UNDERLINE_SINGLE: &'static str = "single";
    pub const UNDERLINE_SINGLEACCOUNTING: &'static str = "singleAccounting";

    #[inline]
    #[must_use]
    pub fn font_name(&self) -> &FontName {
        &self.font_name
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_name()")]
    pub fn get_font_name(&self) -> &FontName {
        self.font_name()
    }

    #[inline]
    pub fn font_name_mut(&mut self) -> &mut FontName {
        &mut self.font_name
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_name_mut()")]
    pub fn get_font_name_mut(&mut self) -> &mut FontName {
        self.font_name_mut()
    }

    #[inline]
    pub fn set_font_name(&mut self, value: FontName) -> &mut Self {
        self.font_name = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn name(&self) -> &str {
        self.font_name.val()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use name()")]
    pub fn get_name(&self) -> &str {
        self.name()
    }

    #[inline]
    pub fn set_name<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.font_name.set_val(value);
        self.set_scheme("none");
        self
    }

    #[inline]
    pub fn set_name_with_scheme<S: Into<String>>(&mut self, name: S, scheme: S) -> &mut Self {
        self.set_name(name);
        self.set_scheme(scheme);
        self
    }

    #[inline]
    #[must_use]
    pub fn font_size(&self) -> &FontSize {
        &self.font_size
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_size()")]
    pub fn get_font_size(&self) -> &FontSize {
        self.font_size()
    }

    #[inline]
    pub fn font_size_mut(&mut self) -> &mut FontSize {
        &mut self.font_size
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_size_mut()")]
    pub fn get_font_size_mut(&mut self) -> &mut FontSize {
        self.font_size_mut()
    }

    #[inline]
    pub fn set_font_size(&mut self, value: FontSize) -> &mut Self {
        self.font_size = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn size(&self) -> f64 {
        self.font_size.val()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use size()")]
    pub fn get_size(&self) -> f64 {
        self.size()
    }

    #[inline]
    pub fn set_size(&mut self, value: f64) -> &mut Self {
        self.font_size.set_val(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn font_family_numbering(&self) -> &FontFamilyNumbering {
        &self.font_family_numbering
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_family_numbering()")]
    pub fn get_font_family_numbering(&self) -> &FontFamilyNumbering {
        self.font_family_numbering()
    }

    #[inline]
    pub fn font_family_numbering_mut(&mut self) -> &mut FontFamilyNumbering {
        &mut self.font_family_numbering
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_family_numbering_mut()")]
    pub fn get_font_family_numbering_mut(&mut self) -> &mut FontFamilyNumbering {
        self.font_family_numbering_mut()
    }

    #[inline]
    pub fn set_font_family_numbering(&mut self, value: FontFamilyNumbering) -> &mut Self {
        self.font_family_numbering = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn family(&self) -> i32 {
        self.font_family_numbering.val()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use family()")]
    pub fn get_family(&self) -> i32 {
        self.family()
    }

    #[inline]
    pub fn set_family(&mut self, value: i32) -> &mut Self {
        self.font_family_numbering.set_val(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn font_bold(&self) -> &Bold {
        &self.font_bold
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_bold()")]
    pub fn get_font_bold(&self) -> &Bold {
        self.font_bold()
    }

    #[inline]
    pub fn font_bold_mut(&mut self) -> &mut Bold {
        &mut self.font_bold
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_bold_mut()")]
    pub fn get_font_bold_mut(&mut self) -> &mut Bold {
        self.font_bold_mut()
    }

    #[inline]
    pub fn set_font_bold(&mut self, value: Bold) -> &mut Self {
        self.font_bold = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn bold(&self) -> bool {
        self.font_bold.val()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use bold()")]
    pub fn get_bold(&self) -> bool {
        self.bold()
    }

    #[inline]
    pub fn set_bold(&mut self, value: bool) -> &mut Self {
        self.font_bold.set_val(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn font_italic(&self) -> &Italic {
        &self.font_italic
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_italic()")]
    pub fn get_font_italic(&self) -> &Italic {
        self.font_italic()
    }

    #[inline]
    pub fn font_italic_mut(&mut self) -> &mut Italic {
        &mut self.font_italic
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_italic_mut()")]
    pub fn get_font_italic_mut(&mut self) -> &mut Italic {
        self.font_italic_mut()
    }

    #[inline]
    pub fn set_font_italic(&mut self, value: Italic) -> &mut Self {
        self.font_italic = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn italic(&self) -> bool {
        self.font_italic.val()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use italic()")]
    pub fn get_italic(&self) -> bool {
        self.italic()
    }

    #[inline]
    pub fn set_italic(&mut self, value: bool) -> &mut Self {
        self.font_italic.set_val(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn font_underline(&self) -> &Underline {
        &self.font_underline
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_underline()")]
    pub fn get_font_underline(&self) -> &Underline {
        self.font_underline()
    }

    #[inline]
    pub fn font_underline_mut(&mut self) -> &mut Underline {
        &mut self.font_underline
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_underline_mut()")]
    pub fn get_font_underline_mut(&mut self) -> &mut Underline {
        self.font_underline_mut()
    }

    #[inline]
    pub fn set_font_underline(&mut self, value: Underline) -> &mut Self {
        self.font_underline = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn underline(&self) -> &str {
        self.font_underline.val.value_string()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use underline()")]
    pub fn get_underline(&self) -> &str {
        self.underline()
    }

    #[inline]
    pub fn set_underline<S: Into<String>>(&mut self, value: S) -> &mut Self {
        let obj = value.into();
        self.font_underline
            .set_val(UnderlineValues::from_str(&obj).unwrap());
        self
    }

    #[inline]
    #[must_use]
    pub fn font_strike(&self) -> &Strike {
        &self.font_strike
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_strike()")]
    pub fn get_font_strike(&self) -> &Strike {
        self.font_strike()
    }

    #[inline]
    pub fn font_strike_mut(&mut self) -> &mut Strike {
        &mut self.font_strike
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_strike_mut()")]
    pub fn get_font_strike_mut(&mut self) -> &mut Strike {
        self.font_strike_mut()
    }

    #[inline]
    pub fn set_font_strike(&mut self, value: Strike) -> &mut Self {
        self.font_strike = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn strikethrough(&self) -> bool {
        self.font_strike.val()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use strikethrough()")]
    pub fn get_strikethrough(&self) -> bool {
        self.strikethrough()
    }

    #[inline]
    pub fn set_strikethrough(&mut self, value: bool) -> &mut Self {
        self.font_strike.set_val(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn color(&self) -> &Color {
        &self.color
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use color()")]
    pub fn get_color(&self) -> &Color {
        self.color()
    }

    #[inline]
    pub fn color_mut(&mut self) -> &mut Color {
        &mut self.color
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use color_mut()")]
    pub fn get_color_mut(&mut self) -> &mut Color {
        self.color_mut()
    }

    #[inline]
    pub fn set_color(&mut self, value: Color) -> &mut Self {
        self.color = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn font_char_set(&self) -> &FontCharSet {
        &self.font_char_set
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_char_set()")]
    pub fn get_font_char_set(&self) -> &FontCharSet {
        self.font_char_set()
    }

    #[inline]
    pub fn font_char_set_mut(&mut self) -> &mut FontCharSet {
        &mut self.font_char_set
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_char_set_mut()")]
    pub fn get_font_char_set_mut(&mut self) -> &mut FontCharSet {
        self.font_char_set_mut()
    }

    #[inline]
    pub fn set_font_char_set(&mut self, value: FontCharSet) -> &mut Self {
        self.font_char_set = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn charset(&self) -> i32 {
        self.font_char_set.val()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use charset()")]
    pub fn get_charset(&self) -> i32 {
        self.charset()
    }

    #[inline]
    pub fn set_charset(&mut self, value: i32) -> &mut Self {
        self.font_char_set.set_val(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn font_scheme(&self) -> &FontScheme {
        &self.font_scheme
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use font_scheme()")]
    pub fn get_font_scheme(&self) -> &FontScheme {
        self.font_scheme()
    }

    #[inline]
    pub fn font_scheme_mut(&mut self) -> &mut FontScheme {
        &mut self.font_scheme
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use font_scheme_mut()")]
    pub fn get_font_scheme_mut(&mut self) -> &mut FontScheme {
        self.font_scheme_mut()
    }

    #[inline]
    pub fn set_font_scheme(&mut self, value: FontScheme) -> &mut Self {
        self.font_scheme = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn scheme(&self) -> &str {
        self.font_scheme.val.value_string()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use scheme()")]
    pub fn get_scheme(&self) -> &str {
        self.scheme()
    }

    #[inline]
    pub fn set_scheme<S: Into<String>>(&mut self, value: S) -> &mut Self {
        let obj = value.into();
        self.font_scheme
            .set_val(FontSchemeValues::from_str(&obj).unwrap());
        self
    }

    #[inline]
    #[must_use]
    pub fn vertical_text_alignment(&self) -> &VerticalTextAlignment {
        &self.vertical_text_alignment
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use vertical_text_alignment()")]
    pub fn get_vertical_text_alignment(&self) -> &VerticalTextAlignment {
        self.vertical_text_alignment()
    }

    #[inline]
    pub fn vertical_text_alignment_mut(&mut self) -> &mut VerticalTextAlignment {
        &mut self.vertical_text_alignment
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use vertical_text_alignment_mut()")]
    pub fn get_vertical_text_alignment_mut(&mut self) -> &mut VerticalTextAlignment {
        self.vertical_text_alignment_mut()
    }

    #[inline]
    pub fn set_vertical_text_alignment(&mut self, value: VerticalTextAlignment) -> &mut Self {
        self.vertical_text_alignment = value;
        self
    }

    #[inline]
    pub(crate) fn default_value() -> Self {
        let mut def = Self::default();
        def.set_size(11.0);
        def.set_name_with_scheme("Calibri", "minor");
        def.color_mut().set_theme_index(1);
        def.set_family(2);
        def
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use default_value()")]
    pub(crate) fn get_default_value() -> Self {
        let mut def = Self::default();
        def.set_size(11.0);
        def.set_name_with_scheme("Calibri", "minor");
        def.color_mut().set_theme_index(1);
        def.set_family(2);
        def
    }

    pub(crate) fn hash_code(&self) -> String {
        format!(
            "{:x}",
            md5::Md5::digest(format!(
                "{}{}{}{}{}{}{}{}{}{}{}",
                self.font_name.val.hash_string(),
                self.font_size.val.hash_string(),
                self.font_family_numbering.val.hash_string(),
                self.font_bold.val.hash_string(),
                self.font_italic.val.hash_string(),
                self.font_underline.val.hash_string(),
                self.font_strike.val.hash_string(),
                self.color.hash_code(),
                self.font_char_set.val.hash_string(),
                self.font_scheme.val.hash_string(),
                self.vertical_text_alignment.val.hash_string(),
            ))
        )
    }

    #[deprecated(since = "3.0.0", note = "Use hash_code()")]
    pub(crate) fn get_hash_code(&self) -> String {
        self.hash_code()
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        _e: &BytesStart,
    ) {
        let mut buf = Vec::new();
        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Empty(ref e)) => match e.name().into_inner() {
                    b"rFont" | b"name" => {
                        self.font_name.set_attributes(reader, e);
                    }
                    b"sz" => {
                        self.font_size.set_attributes(reader, e);
                    }
                    b"family" => {
                        self.font_family_numbering.set_attributes(reader, e);
                    }
                    b"b" => {
                        self.font_bold.set_attributes(reader, e);
                    }
                    b"i" => {
                        self.font_italic.set_attributes(reader, e);
                    }
                    b"u" => {
                        self.font_underline.set_attributes(reader, e);
                    }
                    b"strike" => {
                        self.font_strike.set_attributes(reader, e);
                    }
                    b"color" => {
                        self.color.set_attributes(reader, e, true);
                    }
                    b"charset" => {
                        self.font_char_set.set_attributes(reader, e);
                    }
                    b"scheme" => {
                        self.font_scheme.set_attributes(reader, e);
                    }
                    b"vertAlign" => {
                        self.vertical_text_alignment.set_attributes(reader, e);
                    }
                    _ => (),
                },
                Ok(Event::End(ref e)) => match e.name().into_inner() {
                    b"font" | b"rPr" => return,
                    _ => (),
                },
                Ok(Event::Eof) => panic!("Error: Could not find {} end element", "font, rPr"),
                Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
                _ => (),
            }
            buf.clear();
        }
    }

    #[inline]
    pub(crate) fn write_to_font(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
        // font
        self.write_to(writer, "font", "name");
    }

    #[inline]
    pub(crate) fn write_to_rpr(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
        // rPr
        self.write_to(writer, "rPr", "rFont");
    }

    pub(crate) fn write_to(
        &self,
        writer: &mut Writer<Cursor<Vec<u8>>>,
        tag_name: &str,
        tag_font_name: &str,
    ) {
        // font
        write_start_tag(writer, tag_name, vec![], false);

        // bold
        self.font_bold.write_to(writer);

        // italic
        self.font_italic.write_to(writer);

        // underline
        self.font_underline.write_to(writer);

        // strike
        self.font_strike.write_to(writer);

        // vertAlign
        self.vertical_text_alignment.write_to(writer);

        // sz
        self.font_size.write_to(writer);

        // color
        self.color.write_to_color(writer);

        // name
        self.font_name.write_to(writer, tag_font_name);

        // family
        self.font_family_numbering.write_to(writer);

        // charset
        self.font_char_set.write_to(writer);

        // scheme
        self.font_scheme.write_to(writer);

        write_end_tag(writer, tag_name);
    }
}