OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLRichText.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLRICHTEXT_HPP
2#define OPENXLSX_XLRICHTEXT_HPP
3
4// ===== External Includes ===== //
5#include <optional>
6#include <string>
7#include <vector>
8
9// ===== OpenXLSX Includes ===== //
10#include "OpenXLSX-Exports.hpp"
11#include "XLColor.hpp"
12#include "XLStyles.hpp"
13
14namespace OpenXLSX
15{
19 class OPENXLSX_EXPORT XLRichTextRun
20 {
21 public:
22 XLRichTextRun() = default;
23 explicit XLRichTextRun(const std::string& text) : m_text(text) {}
24
25 const std::string& text() const { return m_text; }
26 XLRichTextRun& setText(const std::string& text)
27 {
28 m_text = text;
29 return *this;
30 }
31
32 // Font properties
33 std::optional<std::string> fontName() const { return m_fontName; }
34 XLRichTextRun& setFontName(const std::string& name)
35 {
36 m_fontName = name;
37 return *this;
38 }
39
40 std::optional<size_t> fontSize() const { return m_fontSize; }
42 {
43 m_fontSize = size;
44 return *this;
45 }
46
47 std::optional<XLColor> fontColor() const { return m_fontColor; }
49 {
50 m_fontColor = color;
51 return *this;
52 }
53
54 std::optional<bool> bold() const { return m_bold; }
55 XLRichTextRun& setBold(bool bold = true)
56 {
57 m_bold = bold;
58 return *this;
59 }
60
61 std::optional<bool> italic() const { return m_italic; }
62 XLRichTextRun& setItalic(bool italic = true)
63 {
64 m_italic = italic;
65 return *this;
66 }
67
68 // Returns true if any underline style is set (for backward compatibility)
69 std::optional<bool> underline() const { return m_underlineStyle.has_value() ? m_underlineStyle.value() != XLUnderlineNone : std::optional<bool>{}; }
70
71 XLRichTextRun& setUnderline(bool underline = true)
72 {
73 m_underlineStyle = underline ? XLUnderlineSingle : XLUnderlineNone;
74 return *this;
75 }
76
77 std::optional<XLUnderlineStyle> underlineStyle() const { return m_underlineStyle; }
79 {
80 m_underlineStyle = style;
81 return *this;
82 }
83
84 std::optional<bool> strikethrough() const { return m_strikethrough; }
85 XLRichTextRun& setStrikethrough(bool strikethrough = true)
86 {
87 m_strikethrough = strikethrough;
88 return *this;
89 }
90
91 std::optional<XLVerticalAlignRunStyle> vertAlign() const { return m_vertAlign; }
93 {
94 m_vertAlign = align;
95 return *this;
96 }
97
98 // Convenience methods for Superscript and Subscript
99 XLRichTextRun& setSuperscript(bool enable = true)
100 {
101 if (enable) m_vertAlign = XLSuperscript;
102 else if (m_vertAlign == XLSuperscript) m_vertAlign.reset();
103 return *this;
104 }
105
106 XLRichTextRun& setSubscript(bool enable = true)
107 {
108 if (enable) m_vertAlign = XLSubscript;
109 else if (m_vertAlign == XLSubscript) m_vertAlign.reset();
110 return *this;
111 }
112
113 private:
114 std::string m_text;
115 std::optional<std::string> m_fontName;
116 std::optional<size_t> m_fontSize;
117 std::optional<XLColor> m_fontColor;
118 std::optional<bool> m_bold;
119 std::optional<bool> m_italic;
120 std::optional<XLUnderlineStyle> m_underlineStyle;
121 std::optional<bool> m_strikethrough;
122 std::optional<XLVerticalAlignRunStyle> m_vertAlign;
123 };
124
128 class OPENXLSX_EXPORT XLRichText
129 {
130 public:
131 XLRichText() = default;
132 explicit XLRichText(const std::string& plainText) { m_runs.emplace_back(plainText); }
133
135 {
136 m_runs.push_back(run);
137 return *this;
138 }
139 XLRichTextRun& addRun(const std::string& text)
140 {
141 m_runs.emplace_back(text);
142 return m_runs.back();
143 }
144 const std::vector<XLRichTextRun>& runs() const { return m_runs; }
145 std::vector<XLRichTextRun>& runs() { return m_runs; }
146
150 std::string plainText() const
151 {
152 std::string result;
153 for (const auto& run : m_runs) { result += run.text(); }
154 return result;
155 }
156
157 bool empty() const { return m_runs.empty(); }
158 void clear() { m_runs.clear(); }
159
161 {
162 for (const auto& run : other.runs()) {
163 m_runs.push_back(run);
164 }
165 return *this;
166 }
167
168 XLRichText& operator+=(const std::string& text)
169 {
170 m_runs.emplace_back(text);
171 return *this;
172 }
173
175 {
176 m_runs.push_back(run);
177 return *this;
178 }
179
180 private:
181 std::vector<XLRichTextRun> m_runs;
182 };
183
184 inline XLRichText operator+(XLRichText lhs, const XLRichText& rhs) {
185 lhs += rhs;
186 return lhs;
187 }
188
189 inline XLRichText operator+(XLRichText lhs, const std::string& rhs) {
190 lhs += rhs;
191 return lhs;
192 }
193
195 lhs += rhs;
196 return lhs;
197 }
198
199 // Equality operators
200 inline bool operator==(const XLRichTextRun& lhs, const XLRichTextRun& rhs)
201 {
202 return lhs.text() == rhs.text() && lhs.fontName() == rhs.fontName() && lhs.fontSize() == rhs.fontSize() &&
203 lhs.fontColor() == rhs.fontColor() && lhs.bold() == rhs.bold() && lhs.italic() == rhs.italic() &&
204 lhs.underlineStyle() == rhs.underlineStyle() && lhs.strikethrough() == rhs.strikethrough() &&
205 lhs.vertAlign() == rhs.vertAlign();
206 }
207
208 inline bool operator!=(const XLRichTextRun& lhs, const XLRichTextRun& rhs) { return !(lhs == rhs); }
209
210 inline bool operator==(const XLRichText& lhs, const XLRichText& rhs) { return lhs.runs() == rhs.runs(); }
211
212 inline bool operator!=(const XLRichText& lhs, const XLRichText& rhs) { return !(lhs == rhs); }
213
214} // namespace OpenXLSX
215
216#endif // OPENXLSX_XLRICHTEXT_HPP
Definition XLColor.hpp:22
A class representing a single run of rich text.
Definition XLRichText.hpp:20
XLRichTextRun & setFontSize(size_t size)
Definition XLRichText.hpp:41
XLRichTextRun & setSuperscript(bool enable=true)
Definition XLRichText.hpp:99
XLRichTextRun & setFontColor(const XLColor &color)
Definition XLRichText.hpp:48
XLRichTextRun(const std::string &text)
Definition XLRichText.hpp:23
std::optional< std::string > fontName() const
Definition XLRichText.hpp:33
std::optional< size_t > fontSize() const
Definition XLRichText.hpp:40
std::optional< XLColor > fontColor() const
Definition XLRichText.hpp:47
XLRichTextRun & setSubscript(bool enable=true)
Definition XLRichText.hpp:106
XLRichTextRun & setText(const std::string &text)
Definition XLRichText.hpp:26
std::optional< bool > strikethrough() const
Definition XLRichText.hpp:84
XLRichTextRun & setUnderline(bool underline=true)
Definition XLRichText.hpp:71
std::optional< bool > italic() const
Definition XLRichText.hpp:61
std::optional< XLVerticalAlignRunStyle > vertAlign() const
Definition XLRichText.hpp:91
XLRichTextRun & setBold(bool bold=true)
Definition XLRichText.hpp:55
XLRichTextRun & setStrikethrough(bool strikethrough=true)
Definition XLRichText.hpp:85
XLRichTextRun & setVertAlign(XLVerticalAlignRunStyle align)
Definition XLRichText.hpp:92
XLRichTextRun & setItalic(bool italic=true)
Definition XLRichText.hpp:62
std::optional< bool > underline() const
Definition XLRichText.hpp:69
std::optional< XLUnderlineStyle > underlineStyle() const
Definition XLRichText.hpp:77
std::optional< bool > bold() const
Definition XLRichText.hpp:54
XLRichTextRun & setUnderlineStyle(XLUnderlineStyle style)
Definition XLRichText.hpp:78
XLRichTextRun & setFontName(const std::string &name)
Definition XLRichText.hpp:34
const std::string & text() const
Definition XLRichText.hpp:25
A class representing rich text in a cell.
Definition XLRichText.hpp:129
XLRichText & operator+=(const XLRichText &other)
Definition XLRichText.hpp:160
XLRichText(const std::string &plainText)
Definition XLRichText.hpp:132
void clear()
Definition XLRichText.hpp:158
bool empty() const
Definition XLRichText.hpp:157
std::string plainText() const
Get the plain text representation of the rich text.
Definition XLRichText.hpp:150
XLRichText & operator+=(const XLRichTextRun &run)
Definition XLRichText.hpp:174
std::vector< XLRichTextRun > & runs()
Definition XLRichText.hpp:145
XLRichText & operator+=(const std::string &text)
Definition XLRichText.hpp:168
const std::vector< XLRichTextRun > & runs() const
Definition XLRichText.hpp:144
XLRichText & addRun(const XLRichTextRun &run)
Definition XLRichText.hpp:134
XLRichTextRun & addRun(const std::string &text)
Definition XLRichText.hpp:139
Definition IZipArchive.hpp:18
XLUnderlineStyle
Definition XLStyles.hpp:80
@ XLUnderlineSingle
Definition XLStyles.hpp:82
@ XLUnderlineNone
Definition XLStyles.hpp:81
bool operator==(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:304
bool operator!=(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:311
XLRichText operator+(XLRichText lhs, const XLRichText &rhs)
Definition XLRichText.hpp:184
XLVerticalAlignRunStyle
Definition XLStyles.hpp:96
@ XLSubscript
Definition XLStyles.hpp:98
@ XLSuperscript
Definition XLStyles.hpp:99