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