OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLStyle.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLSTYLE_HPP
12#define OPENXLSX_XLSTYLE_HPP
13
14#include "OpenXLSX-Exports.hpp"
15#include "XLColor.hpp"
16#include "XLConstants.hpp"
17#include "XLStyles.hpp"
18#include <cstdint>
19#include <optional>
20#include <string>
21
22namespace OpenXLSX
23{
28 struct XLStyle
29 {
30 struct Font
31 {
32 std::optional<std::string> name;
33 std::optional<uint32_t> size;
34 std::optional<XLColor> color;
35 std::optional<bool> bold;
36 std::optional<bool> italic;
37 std::optional<bool> underline;
38 std::optional<bool> strikethrough;
40
41 struct Fill
42 {
43 std::optional<XLPatternType> pattern;
44 std::optional<XLColor> fgColor;
45 std::optional<XLColor> bgColor;
47
49 {
50 std::optional<XLLineStyle> style;
51 std::optional<XLColor> color;
52 };
53
64
65 struct Alignment
66 {
67 std::optional<XLAlignmentStyle> horizontal;
68 std::optional<XLAlignmentStyle> vertical;
69 std::optional<bool> wrapText;
70 std::optional<uint16_t> textRotation;
71 std::optional<uint32_t> indent;
73
74 std::optional<std::string> numberFormat;
75 };
76
77 // ===== Equality Operators ===== //
78
79 inline bool operator==(const XLStyle::Font& lhs, const XLStyle::Font& rhs) noexcept
80 {
81 return lhs.name == rhs.name && lhs.size == rhs.size && lhs.color == rhs.color &&
82 lhs.bold == rhs.bold && lhs.italic == rhs.italic && lhs.underline == rhs.underline &&
83 lhs.strikethrough == rhs.strikethrough;
84 }
85
86 inline bool operator!=(const XLStyle::Font& lhs, const XLStyle::Font& rhs) noexcept { return !(lhs == rhs); }
87
88 inline bool operator==(const XLStyle::Fill& lhs, const XLStyle::Fill& rhs) noexcept
89 {
90 return lhs.pattern == rhs.pattern && lhs.fgColor == rhs.fgColor && lhs.bgColor == rhs.bgColor;
91 }
92
93 inline bool operator!=(const XLStyle::Fill& lhs, const XLStyle::Fill& rhs) noexcept { return !(lhs == rhs); }
94
95 inline bool operator==(const XLStyle::BorderElement& lhs, const XLStyle::BorderElement& rhs) noexcept
96 {
97 return lhs.style == rhs.style && lhs.color == rhs.color;
98 }
99
100 inline bool operator!=(const XLStyle::BorderElement& lhs, const XLStyle::BorderElement& rhs) noexcept { return !(lhs == rhs); }
101
102 inline bool operator==(const XLStyle::Border& lhs, const XLStyle::Border& rhs) noexcept
103 {
104 return lhs.left == rhs.left && lhs.right == rhs.right && lhs.top == rhs.top &&
105 lhs.bottom == rhs.bottom && lhs.diagonal == rhs.diagonal &&
106 lhs.diagonalUp == rhs.diagonalUp && lhs.diagonalDown == rhs.diagonalDown;
107 }
108
109 inline bool operator!=(const XLStyle::Border& lhs, const XLStyle::Border& rhs) noexcept { return !(lhs == rhs); }
110
111 inline bool operator==(const XLStyle::Alignment& lhs, const XLStyle::Alignment& rhs) noexcept
112 {
113 return lhs.horizontal == rhs.horizontal && lhs.vertical == rhs.vertical &&
114 lhs.wrapText == rhs.wrapText && lhs.textRotation == rhs.textRotation &&
115 lhs.indent == rhs.indent;
116 }
117
118 inline bool operator!=(const XLStyle::Alignment& lhs, const XLStyle::Alignment& rhs) noexcept { return !(lhs == rhs); }
119
120 inline bool operator==(const XLStyle& lhs, const XLStyle& rhs) noexcept
121 {
122 return lhs.font == rhs.font && lhs.fill == rhs.fill && lhs.border == rhs.border &&
123 lhs.alignment == rhs.alignment && lhs.numberFormat == rhs.numberFormat;
124 }
125
126 inline bool operator!=(const XLStyle& lhs, const XLStyle& rhs) noexcept { return !(lhs == rhs); }
127
128 // ===== Hash Functor ===== //
129
131 {
132 static void hashCombine(size_t& seed, size_t val) noexcept
133 {
134 seed ^= val + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2);
135 }
136
137 static size_t hashColor(const XLColor& c) noexcept
138 {
139 uint32_t val = (static_cast<uint32_t>(c.alpha()) << 24) |
140 (static_cast<uint32_t>(c.red()) << 16) |
141 (static_cast<uint32_t>(c.green()) << 8) |
142 static_cast<uint32_t>(c.blue());
143 return std::hash<uint32_t>{}(val);
144 }
145
146 size_t operator()(const XLStyle& s) const noexcept
147 {
148 size_t seed = 0;
149
150 // Font
151 if (s.font.name) { hashCombine(seed, 1); hashCombine(seed, std::hash<std::string>{}(*s.font.name)); }
152 if (s.font.size) { hashCombine(seed, 2); hashCombine(seed, std::hash<uint32_t>{}(*s.font.size)); }
153 if (s.font.color) { hashCombine(seed, 3); hashCombine(seed, hashColor(*s.font.color)); }
154 if (s.font.bold) { hashCombine(seed, *s.font.bold ? 4 : 5); }
155 if (s.font.italic) { hashCombine(seed, *s.font.italic ? 6 : 7); }
156 if (s.font.underline) { hashCombine(seed, *s.font.underline ? 8 : 9); }
157 if (s.font.strikethrough) { hashCombine(seed, *s.font.strikethrough ? 10 : 11); }
158
159 // Fill
160 if (s.fill.pattern) { hashCombine(seed, 12); hashCombine(seed, static_cast<size_t>(*s.fill.pattern)); }
161 if (s.fill.fgColor) { hashCombine(seed, 13); hashCombine(seed, hashColor(*s.fill.fgColor)); }
162 if (s.fill.bgColor) { hashCombine(seed, 14); hashCombine(seed, hashColor(*s.fill.bgColor)); }
163
164 // Border
165 auto hashBorderElem = [&](size_t offset, const XLStyle::BorderElement& b) {
166 if (b.style) { hashCombine(seed, offset); hashCombine(seed, static_cast<size_t>(*b.style)); }
167 if (b.color) { hashCombine(seed, offset + 1); hashCombine(seed, hashColor(*b.color)); }
168 };
169 hashBorderElem(20, s.border.left);
170 hashBorderElem(30, s.border.right);
171 hashBorderElem(40, s.border.top);
172 hashBorderElem(50, s.border.bottom);
173 hashBorderElem(60, s.border.diagonal);
174 if (s.border.diagonalUp) hashCombine(seed, *s.border.diagonalUp ? 70 : 71);
175 if (s.border.diagonalDown) hashCombine(seed, *s.border.diagonalDown ? 72 : 73);
176
177 // Alignment
178 if (s.alignment.horizontal) { hashCombine(seed, 80); hashCombine(seed, static_cast<size_t>(*s.alignment.horizontal)); }
179 if (s.alignment.vertical) { hashCombine(seed, 81); hashCombine(seed, static_cast<size_t>(*s.alignment.vertical)); }
180 if (s.alignment.wrapText) hashCombine(seed, *s.alignment.wrapText ? 82 : 83);
181 if (s.alignment.textRotation) { hashCombine(seed, 84); hashCombine(seed, std::hash<uint16_t>{}(*s.alignment.textRotation)); }
182 if (s.alignment.indent) { hashCombine(seed, 85); hashCombine(seed, std::hash<uint32_t>{}(*s.alignment.indent)); }
183
184 // NumberFormat
185 if (s.numberFormat) { hashCombine(seed, 90); hashCombine(seed, std::hash<std::string>{}(*s.numberFormat)); }
186
187 return seed;
188 }
189 };
190
191} // namespace OpenXLSX
192
193namespace std
194{
195 template<>
196 struct hash<OpenXLSX::XLStyle>
197 {
198 size_t operator()(const OpenXLSX::XLStyle& s) const noexcept
199 {
200 return OpenXLSX::XLStyleHash{}(s);
201 }
202 };
203}
204
205#endif // OPENXLSX_XLSTYLE_HPP
Definition XLColor.hpp:32
Definition IXLCellProvider.hpp:24
bool operator==(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:314
bool operator!=(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:321
Definition XLCellIterator.hpp:131
Definition XLStyle.hpp:131
size_t operator()(const XLStyle &s) const noexcept
Definition XLStyle.hpp:146
static void hashCombine(size_t &seed, size_t val) noexcept
Definition XLStyle.hpp:132
static size_t hashColor(const XLColor &c) noexcept
Definition XLStyle.hpp:137
Definition XLStyle.hpp:66
std::optional< bool > wrapText
Definition XLStyle.hpp:69
std::optional< uint32_t > indent
Definition XLStyle.hpp:71
std::optional< uint16_t > textRotation
Definition XLStyle.hpp:70
std::optional< XLAlignmentStyle > vertical
Definition XLStyle.hpp:68
std::optional< XLAlignmentStyle > horizontal
Definition XLStyle.hpp:67
Definition XLStyle.hpp:49
std::optional< XLColor > color
Definition XLStyle.hpp:51
std::optional< XLLineStyle > style
Definition XLStyle.hpp:50
Definition XLStyle.hpp:55
BorderElement left
Definition XLStyle.hpp:56
BorderElement top
Definition XLStyle.hpp:58
BorderElement right
Definition XLStyle.hpp:57
BorderElement bottom
Definition XLStyle.hpp:59
std::optional< bool > diagonalDown
Definition XLStyle.hpp:62
BorderElement diagonal
Definition XLStyle.hpp:60
std::optional< bool > diagonalUp
Definition XLStyle.hpp:61
Definition XLStyle.hpp:42
std::optional< XLColor > bgColor
Definition XLStyle.hpp:45
std::optional< XLColor > fgColor
Definition XLStyle.hpp:44
std::optional< XLPatternType > pattern
Definition XLStyle.hpp:43
Definition XLStyle.hpp:31
std::optional< XLColor > color
Definition XLStyle.hpp:34
std::optional< bool > bold
Definition XLStyle.hpp:35
std::optional< bool > italic
Definition XLStyle.hpp:36
std::optional< std::string > name
Definition XLStyle.hpp:32
std::optional< uint32_t > size
Definition XLStyle.hpp:33
std::optional< bool > underline
Definition XLStyle.hpp:37
std::optional< bool > strikethrough
Definition XLStyle.hpp:38
A high-level, human-ergonomic structure representing the styling of a cell or range....
Definition XLStyle.hpp:29
struct OpenXLSX::XLStyle::Font font
struct OpenXLSX::XLStyle::Fill fill
std::optional< std::string > numberFormat
Definition XLStyle.hpp:74
struct OpenXLSX::XLStyle::Border border
struct OpenXLSX::XLStyle::Alignment alignment
size_t operator()(const OpenXLSX::XLStyle &s) const noexcept
Definition XLStyle.hpp:198