Skip to main content

umya_spreadsheet/structs/
font.rs

1// front
2use std::{
3    io::Cursor,
4    str::FromStr,
5};
6
7use quick_xml::{
8    Reader,
9    Writer,
10    events::{
11        BytesStart,
12        Event,
13    },
14};
15
16use super::{
17    Bold,
18    Color,
19    FontCharSet,
20    FontFamilyNumbering,
21    FontName,
22    FontScheme,
23    FontSchemeValues,
24    FontSize,
25    Italic,
26    Strike,
27    Underline,
28    UnderlineValues,
29    VerticalTextAlignment,
30};
31use crate::writer::driver::{
32    write_end_tag,
33    write_start_tag,
34};
35
36#[derive(Clone, Default, Debug, PartialEq, PartialOrd)]
37pub struct Font {
38    font_name:               FontName,
39    font_size:               FontSize,
40    font_family_numbering:   FontFamilyNumbering,
41    font_bold:               Bold,
42    font_italic:             Italic,
43    font_underline:          Underline,
44    font_strike:             Strike,
45    color:                   Color,
46    font_char_set:           FontCharSet,
47    font_scheme:             FontScheme,
48    vertical_text_alignment: VerticalTextAlignment,
49}
50impl Font {
51    // Charset
52    pub const CHARSET_ANSI: i32 = 0;
53    pub const CHARSET_ARABIC: i32 = 178;
54    pub const CHARSET_BALTIC: i32 = 186;
55    pub const CHARSET_CHINESEBIG5: i32 = 136;
56    pub const CHARSET_DEFAULT: i32 = 1;
57    pub const CHARSET_EASTEUROPE: i32 = 238;
58    pub const CHARSET_GB2312: i32 = 134;
59    pub const CHARSET_GREEK: i32 = 161;
60    pub const CHARSET_HANGEUL: i32 = 129;
61    pub const CHARSET_HANGUL: i32 = 129;
62    pub const CHARSET_HEBREW: i32 = 177;
63    pub const CHARSET_JOHAB: i32 = 130;
64    pub const CHARSET_MAC: i32 = 77;
65    pub const CHARSET_OEM: i32 = 255;
66    pub const CHARSET_RUSSIAN: i32 = 204;
67    pub const CHARSET_SHIFTJIS: i32 = 128;
68    pub const CHARSET_SYMBOL: i32 = 2;
69    pub const CHARSET_THAI: i32 = 222;
70    pub const CHARSET_TURKISH: i32 = 162;
71    pub const CHARSET_VIETNAMESE: i32 = 163;
72    pub const UNDERLINE_DOUBLE: &'static str = "double";
73    pub const UNDERLINE_DOUBLEACCOUNTING: &'static str = "doubleAccounting";
74    // Underline types
75    pub const UNDERLINE_NONE: &'static str = "none";
76    pub const UNDERLINE_SINGLE: &'static str = "single";
77    pub const UNDERLINE_SINGLEACCOUNTING: &'static str = "singleAccounting";
78
79    #[inline]
80    #[must_use]
81    pub fn font_name(&self) -> &FontName {
82        &self.font_name
83    }
84
85    #[inline]
86    #[must_use]
87    #[deprecated(since = "3.0.0", note = "Use font_name()")]
88    pub fn get_font_name(&self) -> &FontName {
89        self.font_name()
90    }
91
92    #[inline]
93    pub fn font_name_mut(&mut self) -> &mut FontName {
94        &mut self.font_name
95    }
96
97    #[inline]
98    #[deprecated(since = "3.0.0", note = "Use font_name_mut()")]
99    pub fn get_font_name_mut(&mut self) -> &mut FontName {
100        self.font_name_mut()
101    }
102
103    #[inline]
104    pub fn set_font_name(&mut self, value: FontName) -> &mut Self {
105        self.font_name = value;
106        self
107    }
108
109    #[inline]
110    #[must_use]
111    pub fn name(&self) -> &str {
112        self.font_name.val()
113    }
114
115    #[inline]
116    #[must_use]
117    #[deprecated(since = "3.0.0", note = "Use name()")]
118    pub fn get_name(&self) -> &str {
119        self.name()
120    }
121
122    #[inline]
123    pub fn set_name<S: Into<String>>(&mut self, value: S) -> &mut Self {
124        self.font_name.set_val(value);
125        self.set_scheme("none");
126        self
127    }
128
129    #[inline]
130    pub fn set_name_with_scheme<S: Into<String>>(&mut self, name: S, scheme: S) -> &mut Self {
131        self.set_name(name);
132        self.set_scheme(scheme);
133        self
134    }
135
136    #[inline]
137    #[must_use]
138    pub fn font_size(&self) -> &FontSize {
139        &self.font_size
140    }
141
142    #[inline]
143    #[must_use]
144    #[deprecated(since = "3.0.0", note = "Use font_size()")]
145    pub fn get_font_size(&self) -> &FontSize {
146        self.font_size()
147    }
148
149    #[inline]
150    pub fn font_size_mut(&mut self) -> &mut FontSize {
151        &mut self.font_size
152    }
153
154    #[inline]
155    #[deprecated(since = "3.0.0", note = "Use font_size_mut()")]
156    pub fn get_font_size_mut(&mut self) -> &mut FontSize {
157        self.font_size_mut()
158    }
159
160    #[inline]
161    pub fn set_font_size(&mut self, value: FontSize) -> &mut Self {
162        self.font_size = value;
163        self
164    }
165
166    #[inline]
167    #[must_use]
168    pub fn size(&self) -> f64 {
169        self.font_size.val()
170    }
171
172    #[inline]
173    #[must_use]
174    #[deprecated(since = "3.0.0", note = "Use size()")]
175    pub fn get_size(&self) -> f64 {
176        self.size()
177    }
178
179    #[inline]
180    pub fn set_size(&mut self, value: f64) -> &mut Self {
181        self.font_size.set_val(value);
182        self
183    }
184
185    #[inline]
186    #[must_use]
187    pub fn font_family_numbering(&self) -> &FontFamilyNumbering {
188        &self.font_family_numbering
189    }
190
191    #[inline]
192    #[must_use]
193    #[deprecated(since = "3.0.0", note = "Use font_family_numbering()")]
194    pub fn get_font_family_numbering(&self) -> &FontFamilyNumbering {
195        self.font_family_numbering()
196    }
197
198    #[inline]
199    pub fn font_family_numbering_mut(&mut self) -> &mut FontFamilyNumbering {
200        &mut self.font_family_numbering
201    }
202
203    #[inline]
204    #[deprecated(since = "3.0.0", note = "Use font_family_numbering_mut()")]
205    pub fn get_font_family_numbering_mut(&mut self) -> &mut FontFamilyNumbering {
206        self.font_family_numbering_mut()
207    }
208
209    #[inline]
210    pub fn set_font_family_numbering(&mut self, value: FontFamilyNumbering) -> &mut Self {
211        self.font_family_numbering = value;
212        self
213    }
214
215    #[inline]
216    #[must_use]
217    pub fn family(&self) -> i32 {
218        self.font_family_numbering.val()
219    }
220
221    #[inline]
222    #[must_use]
223    #[deprecated(since = "3.0.0", note = "Use family()")]
224    pub fn get_family(&self) -> i32 {
225        self.family()
226    }
227
228    #[inline]
229    pub fn set_family(&mut self, value: i32) -> &mut Self {
230        self.font_family_numbering.set_val(value);
231        self
232    }
233
234    #[inline]
235    #[must_use]
236    pub fn font_bold(&self) -> &Bold {
237        &self.font_bold
238    }
239
240    #[inline]
241    #[must_use]
242    #[deprecated(since = "3.0.0", note = "Use font_bold()")]
243    pub fn get_font_bold(&self) -> &Bold {
244        self.font_bold()
245    }
246
247    #[inline]
248    pub fn font_bold_mut(&mut self) -> &mut Bold {
249        &mut self.font_bold
250    }
251
252    #[inline]
253    #[deprecated(since = "3.0.0", note = "Use font_bold_mut()")]
254    pub fn get_font_bold_mut(&mut self) -> &mut Bold {
255        self.font_bold_mut()
256    }
257
258    #[inline]
259    pub fn set_font_bold(&mut self, value: Bold) -> &mut Self {
260        self.font_bold = value;
261        self
262    }
263
264    #[inline]
265    #[must_use]
266    pub fn bold(&self) -> bool {
267        self.font_bold.val()
268    }
269
270    #[inline]
271    #[must_use]
272    #[deprecated(since = "3.0.0", note = "Use bold()")]
273    pub fn get_bold(&self) -> bool {
274        self.bold()
275    }
276
277    #[inline]
278    pub fn set_bold(&mut self, value: bool) -> &mut Self {
279        self.font_bold.set_val(value);
280        self
281    }
282
283    #[inline]
284    #[must_use]
285    pub fn font_italic(&self) -> &Italic {
286        &self.font_italic
287    }
288
289    #[inline]
290    #[must_use]
291    #[deprecated(since = "3.0.0", note = "Use font_italic()")]
292    pub fn get_font_italic(&self) -> &Italic {
293        self.font_italic()
294    }
295
296    #[inline]
297    pub fn font_italic_mut(&mut self) -> &mut Italic {
298        &mut self.font_italic
299    }
300
301    #[inline]
302    #[deprecated(since = "3.0.0", note = "Use font_italic_mut()")]
303    pub fn get_font_italic_mut(&mut self) -> &mut Italic {
304        self.font_italic_mut()
305    }
306
307    #[inline]
308    pub fn set_font_italic(&mut self, value: Italic) -> &mut Self {
309        self.font_italic = value;
310        self
311    }
312
313    #[inline]
314    #[must_use]
315    pub fn italic(&self) -> bool {
316        self.font_italic.val()
317    }
318
319    #[inline]
320    #[must_use]
321    #[deprecated(since = "3.0.0", note = "Use italic()")]
322    pub fn get_italic(&self) -> bool {
323        self.italic()
324    }
325
326    #[inline]
327    pub fn set_italic(&mut self, value: bool) -> &mut Self {
328        self.font_italic.set_val(value);
329        self
330    }
331
332    #[inline]
333    #[must_use]
334    pub fn font_underline(&self) -> &Underline {
335        &self.font_underline
336    }
337
338    #[inline]
339    #[must_use]
340    #[deprecated(since = "3.0.0", note = "Use font_underline()")]
341    pub fn get_font_underline(&self) -> &Underline {
342        self.font_underline()
343    }
344
345    #[inline]
346    pub fn font_underline_mut(&mut self) -> &mut Underline {
347        &mut self.font_underline
348    }
349
350    #[inline]
351    #[deprecated(since = "3.0.0", note = "Use font_underline_mut()")]
352    pub fn get_font_underline_mut(&mut self) -> &mut Underline {
353        self.font_underline_mut()
354    }
355
356    #[inline]
357    pub fn set_font_underline(&mut self, value: Underline) -> &mut Self {
358        self.font_underline = value;
359        self
360    }
361
362    #[inline]
363    #[must_use]
364    pub fn underline(&self) -> &str {
365        self.font_underline.val.value_string()
366    }
367
368    #[inline]
369    #[must_use]
370    #[deprecated(since = "3.0.0", note = "Use underline()")]
371    pub fn get_underline(&self) -> &str {
372        self.underline()
373    }
374
375    #[inline]
376    pub fn set_underline<S: Into<String>>(&mut self, value: S) -> &mut Self {
377        let obj = value.into();
378        self.font_underline
379            .set_val(UnderlineValues::from_str(&obj).unwrap());
380        self
381    }
382
383    #[inline]
384    #[must_use]
385    pub fn font_strike(&self) -> &Strike {
386        &self.font_strike
387    }
388
389    #[inline]
390    #[must_use]
391    #[deprecated(since = "3.0.0", note = "Use font_strike()")]
392    pub fn get_font_strike(&self) -> &Strike {
393        self.font_strike()
394    }
395
396    #[inline]
397    pub fn font_strike_mut(&mut self) -> &mut Strike {
398        &mut self.font_strike
399    }
400
401    #[inline]
402    #[deprecated(since = "3.0.0", note = "Use font_strike_mut()")]
403    pub fn get_font_strike_mut(&mut self) -> &mut Strike {
404        self.font_strike_mut()
405    }
406
407    #[inline]
408    pub fn set_font_strike(&mut self, value: Strike) -> &mut Self {
409        self.font_strike = value;
410        self
411    }
412
413    #[inline]
414    #[must_use]
415    pub fn strikethrough(&self) -> bool {
416        self.font_strike.val()
417    }
418
419    #[inline]
420    #[must_use]
421    #[deprecated(since = "3.0.0", note = "Use strikethrough()")]
422    pub fn get_strikethrough(&self) -> bool {
423        self.strikethrough()
424    }
425
426    #[inline]
427    pub fn set_strikethrough(&mut self, value: bool) -> &mut Self {
428        self.font_strike.set_val(value);
429        self
430    }
431
432    #[inline]
433    #[must_use]
434    pub fn color(&self) -> &Color {
435        &self.color
436    }
437
438    #[inline]
439    #[must_use]
440    #[deprecated(since = "3.0.0", note = "Use color()")]
441    pub fn get_color(&self) -> &Color {
442        self.color()
443    }
444
445    #[inline]
446    pub fn color_mut(&mut self) -> &mut Color {
447        &mut self.color
448    }
449
450    #[inline]
451    #[deprecated(since = "3.0.0", note = "Use color_mut()")]
452    pub fn get_color_mut(&mut self) -> &mut Color {
453        self.color_mut()
454    }
455
456    #[inline]
457    pub fn set_color(&mut self, value: Color) -> &mut Self {
458        self.color = value;
459        self
460    }
461
462    #[inline]
463    #[must_use]
464    pub fn font_char_set(&self) -> &FontCharSet {
465        &self.font_char_set
466    }
467
468    #[inline]
469    #[must_use]
470    #[deprecated(since = "3.0.0", note = "Use font_char_set()")]
471    pub fn get_font_char_set(&self) -> &FontCharSet {
472        self.font_char_set()
473    }
474
475    #[inline]
476    pub fn font_char_set_mut(&mut self) -> &mut FontCharSet {
477        &mut self.font_char_set
478    }
479
480    #[inline]
481    #[deprecated(since = "3.0.0", note = "Use font_char_set_mut()")]
482    pub fn get_font_char_set_mut(&mut self) -> &mut FontCharSet {
483        self.font_char_set_mut()
484    }
485
486    #[inline]
487    pub fn set_font_char_set(&mut self, value: FontCharSet) -> &mut Self {
488        self.font_char_set = value;
489        self
490    }
491
492    #[inline]
493    #[must_use]
494    pub fn charset(&self) -> i32 {
495        self.font_char_set.val()
496    }
497
498    #[inline]
499    #[must_use]
500    #[deprecated(since = "3.0.0", note = "Use charset()")]
501    pub fn get_charset(&self) -> i32 {
502        self.charset()
503    }
504
505    #[inline]
506    pub fn set_charset(&mut self, value: i32) -> &mut Self {
507        self.font_char_set.set_val(value);
508        self
509    }
510
511    #[inline]
512    #[must_use]
513    pub fn font_scheme(&self) -> &FontScheme {
514        &self.font_scheme
515    }
516
517    #[inline]
518    #[must_use]
519    #[deprecated(since = "3.0.0", note = "Use font_scheme()")]
520    pub fn get_font_scheme(&self) -> &FontScheme {
521        self.font_scheme()
522    }
523
524    #[inline]
525    pub fn font_scheme_mut(&mut self) -> &mut FontScheme {
526        &mut self.font_scheme
527    }
528
529    #[inline]
530    #[deprecated(since = "3.0.0", note = "Use font_scheme_mut()")]
531    pub fn get_font_scheme_mut(&mut self) -> &mut FontScheme {
532        self.font_scheme_mut()
533    }
534
535    #[inline]
536    pub fn set_font_scheme(&mut self, value: FontScheme) -> &mut Self {
537        self.font_scheme = value;
538        self
539    }
540
541    #[inline]
542    #[must_use]
543    pub fn scheme(&self) -> &str {
544        self.font_scheme.val.value_string()
545    }
546
547    #[inline]
548    #[must_use]
549    #[deprecated(since = "3.0.0", note = "Use scheme()")]
550    pub fn get_scheme(&self) -> &str {
551        self.scheme()
552    }
553
554    #[inline]
555    pub fn set_scheme<S: Into<String>>(&mut self, value: S) -> &mut Self {
556        let obj = value.into();
557        self.font_scheme
558            .set_val(FontSchemeValues::from_str(&obj).unwrap());
559        self
560    }
561
562    #[inline]
563    #[must_use]
564    pub fn vertical_text_alignment(&self) -> &VerticalTextAlignment {
565        &self.vertical_text_alignment
566    }
567
568    #[inline]
569    #[must_use]
570    #[deprecated(since = "3.0.0", note = "Use vertical_text_alignment()")]
571    pub fn get_vertical_text_alignment(&self) -> &VerticalTextAlignment {
572        self.vertical_text_alignment()
573    }
574
575    #[inline]
576    pub fn vertical_text_alignment_mut(&mut self) -> &mut VerticalTextAlignment {
577        &mut self.vertical_text_alignment
578    }
579
580    #[inline]
581    #[deprecated(since = "3.0.0", note = "Use vertical_text_alignment_mut()")]
582    pub fn get_vertical_text_alignment_mut(&mut self) -> &mut VerticalTextAlignment {
583        self.vertical_text_alignment_mut()
584    }
585
586    #[inline]
587    pub fn set_vertical_text_alignment(&mut self, value: VerticalTextAlignment) -> &mut Self {
588        self.vertical_text_alignment = value;
589        self
590    }
591
592    #[inline]
593    pub(crate) fn default_value() -> Self {
594        let mut def = Self::default();
595        def.set_size(11.0);
596        def.set_name_with_scheme("Calibri", "minor");
597        def.color_mut().set_theme_index(1);
598        def.set_family(2);
599        def
600    }
601
602    #[inline]
603    #[deprecated(since = "3.0.0", note = "Use default_value()")]
604    pub(crate) fn get_default_value() -> Self {
605        let mut def = Self::default();
606        def.set_size(11.0);
607        def.set_name_with_scheme("Calibri", "minor");
608        def.color_mut().set_theme_index(1);
609        def.set_family(2);
610        def
611    }
612
613    pub(crate) fn hash_code(&self) -> String {
614        crate::helper::utils::md5_hash(format!(
615            "{}{}{}{}{}{}{}{}{}{}{}",
616            self.font_name.val.hash_string(),
617            self.font_size.val.hash_string(),
618            self.font_family_numbering.val.hash_string(),
619            self.font_bold.val.hash_string(),
620            self.font_italic.val.hash_string(),
621            self.font_underline.val.hash_string(),
622            self.font_strike.val.hash_string(),
623            self.color.hash_code(),
624            self.font_char_set.val.hash_string(),
625            self.font_scheme.val.hash_string(),
626            self.vertical_text_alignment.val.hash_string(),
627        ))
628    }
629
630    #[deprecated(since = "3.0.0", note = "Use hash_code()")]
631    pub(crate) fn get_hash_code(&self) -> String {
632        self.hash_code()
633    }
634
635    pub(crate) fn set_attributes<R: std::io::BufRead>(
636        &mut self,
637        reader: &mut Reader<R>,
638        _e: &BytesStart,
639    ) {
640        let mut buf = Vec::new();
641        loop {
642            match reader.read_event_into(&mut buf) {
643                Ok(Event::Empty(ref e)) => match e.name().into_inner() {
644                    b"rFont" | b"name" => {
645                        self.font_name.set_attributes(reader, e);
646                    }
647                    b"sz" => {
648                        self.font_size.set_attributes(reader, e);
649                    }
650                    b"family" => {
651                        self.font_family_numbering.set_attributes(reader, e);
652                    }
653                    b"b" => {
654                        self.font_bold.set_attributes(reader, e);
655                    }
656                    b"i" => {
657                        self.font_italic.set_attributes(reader, e);
658                    }
659                    b"u" => {
660                        self.font_underline.set_attributes(reader, e);
661                    }
662                    b"strike" => {
663                        self.font_strike.set_attributes(reader, e);
664                    }
665                    b"color" => {
666                        self.color.set_attributes(reader, e, true);
667                    }
668                    b"charset" => {
669                        self.font_char_set.set_attributes(reader, e);
670                    }
671                    b"scheme" => {
672                        self.font_scheme.set_attributes(reader, e);
673                    }
674                    b"vertAlign" => {
675                        self.vertical_text_alignment.set_attributes(reader, e);
676                    }
677                    _ => (),
678                },
679                Ok(Event::End(ref e)) => match e.name().into_inner() {
680                    b"font" | b"rPr" => return,
681                    _ => (),
682                },
683                Ok(Event::Eof) => panic!("Error: Could not find {} end element", "font, rPr"),
684                Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
685                _ => (),
686            }
687            buf.clear();
688        }
689    }
690
691    #[inline]
692    pub(crate) fn write_to_font(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
693        // font
694        self.write_to(writer, "font", "name");
695    }
696
697    #[inline]
698    pub(crate) fn write_to_rpr(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
699        // rPr
700        self.write_to(writer, "rPr", "rFont");
701    }
702
703    pub(crate) fn write_to(
704        &self,
705        writer: &mut Writer<Cursor<Vec<u8>>>,
706        tag_name: &str,
707        tag_font_name: &str,
708    ) {
709        // font
710        write_start_tag(writer, tag_name, vec![], false);
711
712        // bold
713        self.font_bold.write_to(writer);
714
715        // italic
716        self.font_italic.write_to(writer);
717
718        // underline
719        self.font_underline.write_to(writer);
720
721        // strike
722        self.font_strike.write_to(writer);
723
724        // vertAlign
725        self.vertical_text_alignment.write_to(writer);
726
727        // sz
728        self.font_size.write_to(writer);
729
730        // color
731        self.color.write_to_color(writer);
732
733        // name
734        self.font_name.write_to(writer, tag_font_name);
735
736        // family
737        self.font_family_numbering.write_to(writer);
738
739        // charset
740        self.font_char_set.write_to(writer);
741
742        // scheme
743        self.font_scheme.write_to(writer);
744
745        write_end_tag(writer, tag_name);
746    }
747}