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