xlnt - community edition
font.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024-2026 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 
193  xlnt::color color() const;
194 
198  bool has_family() const;
199 
203  font &family(std::size_t family);
204 
210  std::size_t family() const;
211 
215  bool has_charset() const;
216 
217  // TODO: charset should be an enum, not a number
218 
222  font &charset(std::size_t charset);
223 
229  std::size_t charset() const;
230 
234  bool has_scheme() const;
235 
239  font &scheme(const std::string &scheme);
240 
246  const std::string &scheme() const;
247 
251  bool operator==(const font &other) const;
252 
256  bool operator!=(const font &other) const
257  {
258  return !operator==(other);
259  }
260 
261 private:
262  friend class style;
263 
267  optional<std::string> name_;
268 
272  optional<double> size_;
273 
277  bool bold_ = false;
278 
282  bool italic_ = false;
283 
287  bool superscript_ = false;
288 
292  bool subscript_ = false;
293 
297  bool strikethrough_ = false;
298 
302  bool outline_ = false;
303 
307  bool shadow_ = false;
308 
312  underline_style underline_ = underline_style::none;
313 
317  optional<xlnt::color> color_;
318 
322  optional<std::size_t> family_;
323 
327  optional<std::size_t> charset_;
328 
332  optional<std::string> scheme_;
333 };
334 
335 } // namespace xlnt
336 
337 namespace std {
338 
339 template<>
340 struct hash<xlnt::font>
341 {
342  size_t operator()(const xlnt::font& f) const
343  {
344  size_t seed = 0;
345 
346  // Hash name
347  if (f.has_name())
348  {
349  xlnt::detail::hash_combine(seed, f.name());
350  }
351 
352  // Hash size
353  if (f.has_size())
354  {
355  xlnt::detail::hash_combine(seed, f.size());
356  }
357 
358  // Hash all boolean properties
359  xlnt::detail::hash_combine(seed, f.bold());
360  xlnt::detail::hash_combine(seed, f.italic());
361  xlnt::detail::hash_combine(seed, f.superscript());
362  xlnt::detail::hash_combine(seed, f.subscript());
363  xlnt::detail::hash_combine(seed, f.strikethrough());
364  xlnt::detail::hash_combine(seed, static_cast<int>(f.underline()));
365  xlnt::detail::hash_combine(seed, f.outline());
366  xlnt::detail::hash_combine(seed, f.shadow());
367 
368  // Hash scheme
369  if (f.has_scheme())
370  {
371  xlnt::detail::hash_combine(seed, f.scheme());
372  }
373 
374  // Hash color (importantly, this re-uses the std::hash<xlnt::color> we just defined)
375  if (f.has_color())
376  {
377  xlnt::detail::hash_combine(seed, f.color());
378  }
379 
380  // Hash family
381  if (f.has_family())
382  {
383  xlnt::detail::hash_combine(seed, f.family());
384  }
385 
386  // Hash charset
387  if (f.has_charset())
388  {
389  xlnt::detail::hash_combine(seed, f.charset());
390  }
391 
392  return seed;
393  }
394 };
395 
396 } // 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:288
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:174
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:256
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