spreadsheet_ods/style/
cellstyle.rs

1use crate::attrmap2::AttrMap2;
2use crate::color::Rgb;
3use crate::format::ValueFormatRef;
4use crate::style::stylemap::StyleMap;
5use crate::style::units::{
6    Angle, Border, CellAlignVertical, CellProtect, FontSize, FontStyle, FontVariant, FontWeight,
7    GlyphOrientation, Hyphenation, HyphenationLadderCount, Indent, Length, LetterSpacing,
8    LineBreak, LineHeight, LineMode, LineStyle, LineType, LineWidth, Margin, PageBreak, PageNumber,
9    ParaAlignVertical, Percent, PunctuationWrap, RotationAlign, TextAlign, TextAlignLast,
10    TextAlignSource, TextAutoSpace, TextCombine, TextCondition, TextDisplay, TextEmphasize,
11    TextEmphasizePosition, TextKeep, TextPosition, TextRelief, TextTransform, WrapOption,
12    WritingDirection, WritingMode,
13};
14use crate::style::AnyStyleRef;
15use crate::style::{
16    border_line_width_string, border_string, color_string, shadow_string, text_position,
17    StyleOrigin, StyleUse, TextStyleRef,
18};
19use core::borrow::Borrow;
20use get_size2::GetSize;
21use icu_locale_core::Locale;
22
23style_ref2!(CellStyleRef);
24
25/// Describes the style information for a cell.
26///
27/// ```
28/// use spreadsheet_ods::{pt, Length, CellStyle, WorkBook, Sheet, CellStyleRef};
29/// use spreadsheet_ods::defaultstyles::DefaultFormat;
30/// use spreadsheet_ods::color::Rgb;
31/// use icu_locale_core::locale;
32///
33/// let mut book = WorkBook::new(locale!("en-US"));
34///
35/// let mut st_header = CellStyle::new("header", &DefaultFormat::default());
36/// st_header.set_font_bold();
37/// st_header.set_color(Rgb::new(255,255,0));
38/// st_header.set_font_size(pt!(18));
39/// let ref_header = book.add_cellstyle(st_header);
40///
41/// let mut sheet0 = Sheet::new("sheet 1");
42/// sheet0.set_styled_value(0,0, "title", &ref_header);
43///
44/// // use a style defined later or elsewhere:
45/// let ref_some = CellStyleRef::from("some_else");
46/// sheet0.set_styled_value(1,0, "some", &ref_some);
47///
48/// ```
49///
50#[derive(Debug, Clone, GetSize)]
51pub struct CellStyle {
52    /// From where did we get this style.
53    origin: StyleOrigin,
54    /// Which tag contains this style.
55    styleuse: StyleUse,
56    /// Style name.
57    name: String,
58    /// General attributes.
59    attr: AttrMap2,
60    /// Cell style attributes.
61    cellstyle: AttrMap2,
62    /// Paragraph style attributes.
63    paragraphstyle: AttrMap2,
64    /// Text style attributes.
65    textstyle: AttrMap2,
66    /// Style maps
67    stylemaps: Option<Vec<StyleMap>>,
68}
69
70styles_styles2!(CellStyle, CellStyleRef);
71
72impl CellStyle {
73    /// Creates an empty style.
74    pub fn new_empty() -> Self {
75        Self {
76            origin: Default::default(),
77            styleuse: Default::default(),
78            name: Default::default(),
79            attr: Default::default(),
80            cellstyle: Default::default(),
81            paragraphstyle: Default::default(),
82            textstyle: Default::default(),
83            stylemaps: None,
84        }
85    }
86
87    /// Creates an empty style with the given name and a reference to a
88    /// value format.
89    pub fn new<S: AsRef<str>>(name: S, value_format: &ValueFormatRef) -> Self {
90        let mut s = Self {
91            origin: Default::default(),
92            styleuse: Default::default(),
93            name: String::from(name.as_ref()),
94            attr: Default::default(),
95            cellstyle: Default::default(),
96            paragraphstyle: Default::default(),
97            textstyle: Default::default(),
98            stylemaps: None,
99        };
100        s.set_value_format(value_format);
101        s
102    }
103
104    /// Reference to the value format.
105    pub fn value_format(&self) -> Option<&str> {
106        self.attr.attr("style:data-style-name")
107    }
108
109    /// Reference to the value format.
110    pub fn set_value_format(&mut self, name: &ValueFormatRef) {
111        self.attr
112            .set_attr("style:data-style-name", name.as_ref().to_string());
113    }
114
115    /// Allows access to all attributes of the style itself.
116    pub fn attrmap(&self) -> &AttrMap2 {
117        &self.attr
118    }
119
120    /// Allows access to all attributes of the style itself.
121    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
122        &mut self.attr
123    }
124
125    /// Allows access to all cell-style like attributes.
126    pub fn cellstyle(&self) -> &AttrMap2 {
127        &self.cellstyle
128    }
129
130    /// Allows access to all cell-style like attributes.
131    pub fn cellstyle_mut(&mut self) -> &mut AttrMap2 {
132        &mut self.cellstyle
133    }
134
135    /// Allows access to all paragraph-style like attributes.
136    pub fn paragraphstyle(&self) -> &AttrMap2 {
137        &self.paragraphstyle
138    }
139
140    /// Allows access to all paragraph-style like attributes.
141    pub fn paragraphstyle_mut(&mut self) -> &mut AttrMap2 {
142        &mut self.paragraphstyle
143    }
144
145    /// Allows access to all text-style like attributes.
146    pub fn textstyle(&self) -> &AttrMap2 {
147        &self.textstyle
148    }
149
150    /// Allows access to all text-style like attributes.
151    pub fn textstyle_mut(&mut self) -> &mut AttrMap2 {
152        &mut self.textstyle
153    }
154
155    /// Adds a stylemap.
156    pub fn push_stylemap(&mut self, stylemap: StyleMap) {
157        self.stylemaps.get_or_insert_with(Vec::new).push(stylemap);
158    }
159
160    /// Returns the stylemaps
161    pub fn stylemaps(&self) -> Option<&Vec<StyleMap>> {
162        self.stylemaps.as_ref()
163    }
164
165    /// Returns the mutable stylemap.
166    pub fn stylemaps_mut(&mut self) -> &mut Vec<StyleMap> {
167        self.stylemaps.get_or_insert_with(Vec::new)
168    }
169
170    // Cell attributes.
171    fo_background_color!(cellstyle);
172    fo_border!(cellstyle);
173    fo_padding!(cellstyle);
174    fo_wrap_option!(cellstyle);
175    fo_border_line_width!(cellstyle);
176    style_cell_protect!(cellstyle);
177    style_decimal_places!(cellstyle);
178    style_diagonal!(cellstyle);
179    style_direction!(cellstyle);
180    style_glyph_orientation_vertical!(cellstyle);
181    style_print_content!(cellstyle);
182    style_repeat_content!(cellstyle);
183    style_rotation_align!(cellstyle);
184    style_rotation_angle!(cellstyle);
185    style_shadow!(cellstyle);
186    style_shrink_to_fit!(cellstyle);
187    style_text_align_source!(cellstyle);
188    style_vertical_align!(cellstyle);
189    style_writing_mode!(cellstyle);
190
191    // Paragraph attributes.
192
193    // NOTE: Some attributes exist as both cell and as paragraph properties.
194    //       They can't be mapped this way. On the other hand you cannot set
195    //       them via LibreOffice either.
196
197    // fo_background_color!(paragraphstyle);
198    // fo_border!(paragraphstyle);
199    fo_break!(paragraphstyle);
200    fo_hyphenation!(paragraphstyle);
201    fo_keep_together!(paragraphstyle);
202    fo_keep_with_next!(paragraphstyle);
203    fo_line_height!(paragraphstyle);
204    fo_margin!(paragraphstyle);
205    fo_orphans!(paragraphstyle);
206    // fo_padding!(paragraphstyle);
207    fo_text_align!(paragraphstyle);
208    fo_text_align_last!(paragraphstyle);
209    fo_text_indent!(paragraphstyle);
210    fo_widows!(paragraphstyle);
211    style_auto_text_indent!(paragraphstyle);
212    style_background_transparency!(paragraphstyle);
213    // fo_border_line_width!(paragraphstyle);
214    style_contextual_spacing!(paragraphstyle);
215    style_font_independent_line_spacing!(paragraphstyle);
216    style_join_border!(paragraphstyle);
217    style_justify_single_word!(paragraphstyle);
218    style_line_break!(paragraphstyle);
219    style_line_height_at_least!(paragraphstyle);
220    style_line_spacing!(paragraphstyle);
221    style_page_number!(paragraphstyle);
222    style_punctuation_wrap!(paragraphstyle);
223    style_register_true!(paragraphstyle);
224    // style_shadow!(paragraphstyle);
225    style_snap_to_layout_grid!(paragraphstyle);
226    style_tab_stop_distance!(paragraphstyle);
227    style_text_autospace!(paragraphstyle);
228    style_vertical_align_para!(paragraphstyle);
229    // style_writing_mode!(paragraphstyle);
230    style_writing_mode_automatic!(paragraphstyle);
231    style_line_number!(paragraphstyle);
232    style_number_lines!(paragraphstyle);
233
234    // NOTE: Some attributes exist as both cell and as text properties.
235    //       They can't be mapped this way. On the other hand you cannot set
236    //       them via LibreOffice either.
237
238    // fo_background_color!(textstyle);
239    fo_color!(textstyle);
240    fo_locale!(textstyle);
241    style_font_name!(textstyle);
242    fo_font_size!(textstyle);
243    fo_font_size_rel!(textstyle);
244    fo_font_style!(textstyle);
245    fo_font_weight!(textstyle);
246    fo_font_variant!(textstyle);
247    fo_font_attr!(textstyle);
248    style_locale_asian!(textstyle);
249    style_font_name_asian!(textstyle);
250    style_font_size_asian!(textstyle);
251    style_font_size_rel_asian!(textstyle);
252    style_font_style_asian!(textstyle);
253    style_font_weight_asian!(textstyle);
254    style_font_attr_asian!(textstyle);
255    style_locale_complex!(textstyle);
256    style_font_name_complex!(textstyle);
257    style_font_size_complex!(textstyle);
258    style_font_size_rel_complex!(textstyle);
259    style_font_style_complex!(textstyle);
260    style_font_weight_complex!(textstyle);
261    style_font_attr_complex!(textstyle);
262    fo_hyphenate!(textstyle);
263    fo_hyphenation_push_char_count!(textstyle);
264    fo_hyphenation_remain_char_count!(textstyle);
265    fo_letter_spacing!(textstyle);
266    fo_text_shadow!(textstyle);
267    fo_text_transform!(textstyle);
268    style_font_relief!(textstyle);
269    style_text_position!(textstyle);
270    // style_rotation_angle!(textstyle);
271    // style_rotation_scale!(textstyle);
272    style_letter_kerning!(textstyle);
273    style_text_combine!(textstyle);
274    style_text_combine_start_char!(textstyle);
275    style_text_combine_end_char!(textstyle);
276    style_text_emphasize!(textstyle);
277    style_text_line_through!(textstyle);
278    style_text_outline!(textstyle);
279    style_text_overline!(textstyle);
280    style_text_underline!(textstyle);
281    style_use_window_font_color!(textstyle);
282    text_condition!(textstyle);
283    text_display!(textstyle);
284
285    // TODO: background image
286}