Skip to main content

umya_spreadsheet/structs/
font.rs

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