xlnt - community edition
font.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024-2025 xlnt-community
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE
22 //
23 // @license: http://www.opensource.org/licenses/mit-license.php
24 // @author: see AUTHORS file
25 
26 #pragma once
27 
28 #include <cstring>
29 #include <functional>
30 
31 #include <xlnt/xlnt_config.hpp>
32 #include <xlnt/styles/color.hpp>
33 #include <xlnt/utils/optional.hpp>
34 #include <xlnt/utils/hash_combine.hpp>
35 
36 namespace xlnt {
37 
38 class style;
39 
43 class XLNT_API font
44 {
45 public:
49  enum class underline_style
50  {
51  none,
52  double_,
53  double_accounting,
54  single,
55  single_accounting
56  };
57 
61  font();
62 
66  font &bold(bool bold);
67 
71  bool bold() const;
72 
76  font &subscript(bool value);
77 
81  bool subscript() const;
82 
86  font &superscript(bool value);
87 
91  bool superscript() const;
92 
96  font &italic(bool italic);
97 
101  bool italic() const;
102 
106  font &strikethrough(bool strikethrough);
107 
111  bool strikethrough() const;
112 
116  font &outline(bool outline);
117 
121  bool outline() const;
122 
126  font &shadow(bool shadow);
127 
131  bool shadow() const;
132 
136  font &underline(underline_style new_underline);
137 
141  bool underlined() const;
142 
146  underline_style underline() const;
147 
151  bool has_size() const;
152 
156  font &size(double size);
157 
161  double size() const;
162 
166  bool has_name() const;
167 
171  font &name(const std::string &name);
172 
176  const std::string &name() const;
177 
181  bool has_color() const;
182 
186  font &color(const color &c);
187 
191  xlnt::color color() const;
192 
196  bool has_family() const;
197 
201  font &family(std::size_t family);
202 
206  std::size_t family() const;
207 
211  bool has_charset() const;
212 
213  // TODO: charset should be an enum, not a number
214 
218  font &charset(std::size_t charset);
219 
223  std::size_t charset() const;
224 
228  bool has_scheme() const;
229 
233  font &scheme(const std::string &scheme);
234 
238  const std::string &scheme() const;
239 
243  bool operator==(const font &other) const;
244 
248  bool operator!=(const font &other) const
249  {
250  return !operator==(other);
251  }
252 
253 private:
254  friend class style;
255 
259  optional<std::string> name_;
260 
264  optional<double> size_;
265 
269  bool bold_ = false;
270 
274  bool italic_ = false;
275 
279  bool superscript_ = false;
280 
284  bool subscript_ = false;
285 
289  bool strikethrough_ = false;
290 
294  bool outline_ = false;
295 
299  bool shadow_ = false;
300 
304  underline_style underline_ = underline_style::none;
305 
309  optional<xlnt::color> color_;
310 
314  optional<std::size_t> family_;
315 
319  optional<std::size_t> charset_;
320 
324  optional<std::string> scheme_;
325 };
326 
327 } // namespace xlnt
328 
329 namespace std {
330 
331 template<>
332 struct hash<xlnt::font>
333 {
334  size_t operator()(const xlnt::font& f) const
335  {
336  size_t seed = 0;
337 
338  // Hash name
339  if (f.has_name())
340  {
341  xlnt::detail::hash_combine(seed, f.name());
342  }
343 
344  // Hash size
345  if (f.has_size())
346  {
347  xlnt::detail::hash_combine(seed, f.size());
348  }
349 
350  // Hash all boolean properties
351  xlnt::detail::hash_combine(seed, f.bold());
352  xlnt::detail::hash_combine(seed, f.italic());
353  xlnt::detail::hash_combine(seed, f.superscript());
354  xlnt::detail::hash_combine(seed, f.subscript());
355  xlnt::detail::hash_combine(seed, f.strikethrough());
356  xlnt::detail::hash_combine(seed, static_cast<int>(f.underline()));
357  xlnt::detail::hash_combine(seed, f.outline());
358  xlnt::detail::hash_combine(seed, f.shadow());
359 
360  // Hash scheme
361  if (f.has_scheme())
362  {
363  xlnt::detail::hash_combine(seed, f.scheme());
364  }
365 
366  // Hash color (importantly, this re-uses the std::hash<xlnt::color> we just defined)
367  if (f.has_color())
368  {
369  xlnt::detail::hash_combine(seed, f.color());
370  }
371 
372  // Hash family
373  if (f.has_family())
374  {
375  xlnt::detail::hash_combine(seed, f.family());
376  }
377 
378  // Hash charset
379  if (f.has_charset())
380  {
381  xlnt::detail::hash_combine(seed, f.charset());
382  }
383 
384  return seed;
385  }
386 };
387 
388 } // namespace std
underline_style
Text can be underlined in the enumerated ways
Definition: font.hpp:49
font & superscript(bool value)
Sets the vertical alignment of the font to superscript and returns a reference to the font...
bool has_name() const
Returns true if this font has a particular face applied (e.g. "Comic Sans").
font & bold(bool bold)
Sets the bold state of the font to bold and returns a reference to the font.
font & outline(bool outline)
Sets the bold state of the font to bold and returns a reference to the font.
bool has_scheme() const
Returns true if this font has a scheme.
Definition: cell_reference.hpp:261
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:36
font & underline(underline_style new_underline)
Sets the underline state of the font to new_underline and returns a reference to the font...
Describes the font style of a particular cell.
Definition: font.hpp:43
Colors can be applied to many parts of a cell&#39;s style.
Definition: color.hpp:167
font & strikethrough(bool strikethrough)
Sets the bold state of the font to bold and returns a reference to the font.
font & subscript(bool value)
Sets the vertical alignment of the font to subscript and returns a reference to the font...
font & size(double size)
Sets the size of the font to size and returns a reference to the font.
bool operator==(std::nullptr_t, const cell &cell)
Returns true if this cell is uninitialized.
bool has_size() const
Returns true if this font has a defined size.
font & name(const std::string &name)
Sets the font face to name and returns a reference to the font.
bool has_color() const
Returns true if this font has a color applied.
bool operator!=(const font &other) const
Returns true if left is not exactly equal to right.
Definition: font.hpp:248
font & shadow(bool shadow)
Sets the shadow state of the font to shadow and returns a reference to the font.
font & italic(bool italic)
Sets the bold state of the font to bold and returns a reference to the font.
bool has_family() const
Returns true if this font has a family defined.
font & color(const color &c)
Sets the color of the font to c and returns a reference to the font.
font & scheme(const std::string &scheme)
Sets the scheme of the font to scheme and returns a reference to the font.
font & family(std::size_t family)
Sets the family index of the font to family and returns a reference to the font.
bool has_charset() const
Returns true if this font has a charset defined.
font & charset(std::size_t charset)
Sets the charset of the font to charset and returns a reference to the font.
Describes a style which has a name and can be applied to multiple individual formats. In Excel this is a "Cell Style".
Definition: style.hpp:55