umya_spreadsheet/structs/
font.rs1use super::Bold;
3use super::Color;
4use super::FontCharSet;
5use super::FontFamilyNumbering;
6use super::FontName;
7use super::FontScheme;
8use super::FontSchemeValues;
9use super::FontSize;
10use super::Italic;
11use super::Strike;
12use super::Underline;
13use super::UnderlineValues;
14use super::VerticalTextAlignment;
15use crate::writer::driver::*;
16use md5::Digest;
17use quick_xml::events::{BytesStart, Event};
18use quick_xml::Reader;
19use quick_xml::Writer;
20use std::io::Cursor;
21use std::str::FromStr;
22
23#[derive(Clone, Default, Debug, PartialEq, PartialOrd)]
24pub struct Font {
25 font_name: FontName,
26 font_size: FontSize,
27 font_family_numbering: FontFamilyNumbering,
28 font_bold: Bold,
29 font_italic: Italic,
30 font_underline: Underline,
31 font_strike: Strike,
32 color: Color,
33 font_char_set: FontCharSet,
34 font_scheme: FontScheme,
35 vertical_text_alignment: VerticalTextAlignment,
36}
37impl Font {
38 pub const CHARSET_ANSI: i32 = 0;
40 pub const CHARSET_DEFAULT: i32 = 1;
41 pub const CHARSET_SYMBOL: i32 = 2;
42 pub const CHARSET_SHIFTJIS: i32 = 128;
43 pub const CHARSET_HANGEUL: i32 = 129;
44 pub const CHARSET_HANGUL: i32 = 129;
45 pub const CHARSET_GB2312: i32 = 134;
46 pub const CHARSET_CHINESEBIG5: i32 = 136;
47 pub const CHARSET_OEM: i32 = 255;
48 pub const CHARSET_JOHAB: i32 = 130;
49 pub const CHARSET_HEBREW: i32 = 177;
50 pub const CHARSET_ARABIC: i32 = 178;
51 pub const CHARSET_GREEK: i32 = 161;
52 pub const CHARSET_TURKISH: i32 = 162;
53 pub const CHARSET_VIETNAMESE: i32 = 163;
54 pub const CHARSET_THAI: i32 = 222;
55 pub const CHARSET_EASTEUROPE: i32 = 238;
56 pub const CHARSET_RUSSIAN: i32 = 204;
57 pub const CHARSET_MAC: i32 = 77;
58 pub const CHARSET_BALTIC: i32 = 186;
59
60 pub const UNDERLINE_NONE: &'static str = "none";
62 pub const UNDERLINE_DOUBLE: &'static str = "double";
63 pub const UNDERLINE_DOUBLEACCOUNTING: &'static str = "doubleAccounting";
64 pub const UNDERLINE_SINGLE: &'static str = "single";
65 pub const UNDERLINE_SINGLEACCOUNTING: &'static str = "singleAccounting";
66
67 #[inline]
68 pub fn get_font_name(&self) -> &FontName {
69 &self.font_name
70 }
71
72 #[inline]
73 pub fn get_font_name_mut(&mut self) -> &mut FontName {
74 &mut self.font_name
75 }
76
77 #[inline]
78 pub fn set_font_name(&mut self, value: FontName) -> &mut Self {
79 self.font_name = value;
80 self
81 }
82
83 #[inline]
84 pub fn get_name(&self) -> &str {
85 self.font_name.get_val()
86 }
87
88 #[inline]
89 pub fn set_name<S: Into<String>>(&mut self, value: S) -> &mut Self {
90 self.font_name.set_val(value);
91 self.set_scheme("none");
92 self
93 }
94
95 #[inline]
96 pub fn set_name_with_scheme<S: Into<String>>(&mut self, name: S, scheme: S) -> &mut Self {
97 self.set_name(name);
98 self.set_scheme(scheme);
99 self
100 }
101
102 #[inline]
103 pub fn get_font_size(&self) -> &FontSize {
104 &self.font_size
105 }
106
107 #[inline]
108 pub fn get_font_size_mut(&mut self) -> &mut FontSize {
109 &mut self.font_size
110 }
111
112 #[inline]
113 pub fn set_font_size(&mut self, value: FontSize) -> &mut Self {
114 self.font_size = value;
115 self
116 }
117
118 #[inline]
119 pub fn get_size(&self) -> &f64 {
120 self.font_size.get_val()
121 }
122
123 #[inline]
124 pub fn set_size(&mut self, value: f64) -> &mut Self {
125 self.font_size.set_val(value);
126 self
127 }
128
129 #[inline]
130 pub fn get_font_family_numbering(&self) -> &FontFamilyNumbering {
131 &self.font_family_numbering
132 }
133
134 #[inline]
135 pub fn get_font_family_numbering_mut(&mut self) -> &mut FontFamilyNumbering {
136 &mut self.font_family_numbering
137 }
138
139 #[inline]
140 pub fn set_font_family_numbering(&mut self, value: FontFamilyNumbering) -> &mut Self {
141 self.font_family_numbering = value;
142 self
143 }
144
145 #[inline]
146 pub fn get_family(&self) -> &i32 {
147 self.font_family_numbering.get_val()
148 }
149
150 #[inline]
151 pub fn set_family(&mut self, value: i32) -> &mut Self {
152 self.font_family_numbering.set_val(value);
153 self
154 }
155
156 #[inline]
157 pub fn get_font_bold(&self) -> &Bold {
158 &self.font_bold
159 }
160
161 #[inline]
162 pub fn get_font_bold_mut(&mut self) -> &mut Bold {
163 &mut self.font_bold
164 }
165
166 #[inline]
167 pub fn set_font_bold(&mut self, value: Bold) -> &mut Self {
168 self.font_bold = value;
169 self
170 }
171
172 #[inline]
173 pub fn get_bold(&self) -> &bool {
174 self.font_bold.get_val()
175 }
176
177 #[inline]
178 pub fn set_bold(&mut self, value: bool) -> &mut Self {
179 self.font_bold.set_val(value);
180 self
181 }
182
183 #[inline]
184 pub fn get_font_italic(&self) -> &Italic {
185 &self.font_italic
186 }
187
188 #[inline]
189 pub fn get_font_italic_mut(&mut self) -> &mut Italic {
190 &mut self.font_italic
191 }
192
193 #[inline]
194 pub fn set_font_italic(&mut self, value: Italic) -> &mut Self {
195 self.font_italic = value;
196 self
197 }
198
199 #[inline]
200 pub fn get_italic(&self) -> &bool {
201 self.font_italic.get_val()
202 }
203
204 #[inline]
205 pub fn set_italic(&mut self, value: bool) -> &mut Self {
206 self.font_italic.set_val(value);
207 self
208 }
209
210 #[inline]
211 pub fn get_font_underline(&self) -> &Underline {
212 &self.font_underline
213 }
214
215 #[inline]
216 pub fn get_font_underline_mut(&mut self) -> &mut Underline {
217 &mut self.font_underline
218 }
219
220 #[inline]
221 pub fn set_font_underline(&mut self, value: Underline) -> &mut Self {
222 self.font_underline = value;
223 self
224 }
225
226 #[inline]
227 pub fn get_underline(&self) -> &str {
228 self.font_underline.val.get_value_string()
229 }
230
231 #[inline]
232 pub fn set_underline<S: Into<String>>(&mut self, value: S) -> &mut Self {
233 let obj = value.into();
234 self.font_underline
235 .set_val(UnderlineValues::from_str(&obj).unwrap());
236 self
237 }
238
239 #[inline]
240 pub fn get_font_strike(&self) -> &Strike {
241 &self.font_strike
242 }
243
244 #[inline]
245 pub fn get_font_strike_mut(&mut self) -> &mut Strike {
246 &mut self.font_strike
247 }
248
249 #[inline]
250 pub fn set_font_strike(&mut self, value: Strike) -> &mut Self {
251 self.font_strike = value;
252 self
253 }
254
255 #[inline]
256 pub fn get_strikethrough(&self) -> &bool {
257 self.font_strike.get_val()
258 }
259
260 #[inline]
261 pub fn set_strikethrough(&mut self, value: bool) -> &mut Self {
262 self.font_strike.set_val(value);
263 self
264 }
265
266 #[inline]
267 pub fn get_color(&self) -> &Color {
268 &self.color
269 }
270
271 #[inline]
272 pub fn get_color_mut(&mut self) -> &mut Color {
273 &mut self.color
274 }
275
276 #[inline]
277 pub fn set_color(&mut self, value: Color) -> &mut Self {
278 self.color = value;
279 self
280 }
281
282 #[inline]
283 pub fn get_font_char_set(&self) -> &FontCharSet {
284 &self.font_char_set
285 }
286
287 #[inline]
288 pub fn get_font_char_set_mut(&mut self) -> &mut FontCharSet {
289 &mut self.font_char_set
290 }
291
292 #[inline]
293 pub fn set_font_char_set(&mut self, value: FontCharSet) -> &mut Self {
294 self.font_char_set = value;
295 self
296 }
297
298 #[inline]
299 pub fn get_charset(&self) -> &i32 {
300 self.font_char_set.get_val()
301 }
302
303 #[inline]
304 pub fn set_charset(&mut self, value: i32) -> &mut Self {
305 self.font_char_set.set_val(value);
306 self
307 }
308
309 #[inline]
310 pub fn get_font_scheme(&self) -> &FontScheme {
311 &self.font_scheme
312 }
313
314 #[inline]
315 pub fn get_font_scheme_mut(&mut self) -> &mut FontScheme {
316 &mut self.font_scheme
317 }
318
319 #[inline]
320 pub fn set_font_scheme(&mut self, value: FontScheme) -> &mut Self {
321 self.font_scheme = value;
322 self
323 }
324
325 #[inline]
326 pub fn get_scheme(&self) -> &str {
327 self.font_scheme.val.get_value_string()
328 }
329
330 #[inline]
331 pub fn set_scheme<S: Into<String>>(&mut self, value: S) -> &mut Self {
332 let obj = value.into();
333 self.font_scheme
334 .set_val(FontSchemeValues::from_str(&obj).unwrap());
335 self
336 }
337
338 #[inline]
339 pub fn get_vertical_text_alignment(&self) -> &VerticalTextAlignment {
340 &self.vertical_text_alignment
341 }
342
343 #[inline]
344 pub fn get_vertical_text_alignment_mut(&mut self) -> &mut VerticalTextAlignment {
345 &mut self.vertical_text_alignment
346 }
347
348 #[inline]
349 pub fn set_vertical_text_alignment(&mut self, value: VerticalTextAlignment) -> &mut Self {
350 self.vertical_text_alignment = value;
351 self
352 }
353
354 #[inline]
355 pub(crate) fn get_default_value() -> Self {
356 let mut def = Self::default();
357 def.set_size(11.0);
358 def.set_name_with_scheme("Calibri", "minor");
359 def.get_color_mut().set_theme_index(1);
360 def.set_family(2);
361 def
362 }
363
364 pub(crate) fn get_hash_code(&self) -> String {
365 format!(
366 "{:x}",
367 md5::Md5::digest(format!(
368 "{}{}{}{}{}{}{}{}{}{}{}",
369 &self.font_name.val.get_hash_string(),
370 &self.font_size.val.get_hash_string(),
371 &self.font_family_numbering.val.get_hash_string(),
372 &self.font_bold.val.get_hash_string(),
373 &self.font_italic.val.get_hash_string(),
374 &self.font_underline.val.get_hash_string(),
375 &self.font_strike.val.get_hash_string(),
376 &self.color.get_hash_code(),
377 &self.font_char_set.val.get_hash_string(),
378 &self.font_scheme.val.get_hash_string(),
379 &self.vertical_text_alignment.val.get_hash_string(),
380 ))
381 )
382 }
383
384 pub(crate) fn set_attributes<R: std::io::BufRead>(
385 &mut self,
386 reader: &mut Reader<R>,
387 _e: &BytesStart,
388 ) {
389 let mut buf = Vec::new();
390 loop {
391 match reader.read_event_into(&mut buf) {
392 Ok(Event::Empty(ref e)) => match e.name().into_inner() {
393 b"name" => {
394 self.font_name.set_attributes(reader, e);
395 }
396 b"rFont" => {
397 self.font_name.set_attributes(reader, e);
398 }
399 b"sz" => {
400 self.font_size.set_attributes(reader, e);
401 }
402 b"family" => {
403 self.font_family_numbering.set_attributes(reader, e);
404 }
405 b"b" => {
406 self.font_bold.set_attributes(reader, e);
407 }
408 b"i" => {
409 self.font_italic.set_attributes(reader, e);
410 }
411 b"u" => {
412 self.font_underline.set_attributes(reader, e);
413 }
414 b"strike" => {
415 self.font_strike.set_attributes(reader, e);
416 }
417 b"color" => {
418 self.color.set_attributes(reader, e, true);
419 }
420 b"charset" => {
421 self.font_char_set.set_attributes(reader, e);
422 }
423 b"scheme" => {
424 self.font_scheme.set_attributes(reader, e);
425 }
426 b"vertAlign" => {
427 self.vertical_text_alignment.set_attributes(reader, e);
428 }
429 _ => (),
430 },
431 Ok(Event::End(ref e)) => match e.name().into_inner() {
432 b"font" => return,
433 b"rPr" => return,
434 _ => (),
435 },
436 Ok(Event::Eof) => panic!("Error: Could not find {} end element", "font, rPr"),
437 Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
438 _ => (),
439 }
440 buf.clear();
441 }
442 }
443
444 #[inline]
445 pub(crate) fn write_to_font(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
446 self.write_to(writer, "font", "name");
448 }
449
450 #[inline]
451 pub(crate) fn write_to_rpr(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
452 self.write_to(writer, "rPr", "rFont");
454 }
455
456 pub(crate) fn write_to(
457 &self,
458 writer: &mut Writer<Cursor<Vec<u8>>>,
459 tag_name: &str,
460 tag_font_name: &str,
461 ) {
462 write_start_tag(writer, tag_name, vec![], false);
464
465 self.font_bold.write_to(writer);
467
468 self.font_italic.write_to(writer);
470
471 self.font_underline.write_to(writer);
473
474 self.font_strike.write_to(writer);
476
477 self.vertical_text_alignment.write_to(writer);
479
480 self.font_size.write_to(writer);
482
483 self.color.write_to_color(writer);
485
486 self.font_name.write_to(writer, tag_font_name);
488
489 self.font_family_numbering.write_to(writer);
491
492 self.font_char_set.write_to(writer);
494
495 self.font_scheme.write_to(writer);
497
498 write_end_tag(writer, tag_name);
499 }
500}