Branch data TLA Line data Source code
1 : : // Copyright (c) 2016-2022 Thomas Fussell
2 : : // Copyright (c) 2024-2025 xlnt-community
3 : : //
4 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : : // of this software and associated documentation files (the "Software"), to deal
6 : : // in the Software without restriction, including without limitation the rights
7 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : : // copies of the Software, and to permit persons to whom the Software is
9 : : // furnished to do so, subject to the following conditions:
10 : : //
11 : : // The above copyright notice and this permission notice shall be included in
12 : : // all copies or substantial portions of the Software.
13 : : //
14 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : : // THE SOFTWARE
21 : : //
22 : : // @license: http://www.opensource.org/licenses/mit-license.php
23 : : // @author: see AUTHORS file
24 : :
25 : : #pragma once
26 : :
27 : : #include <string>
28 : : #include <vector>
29 : :
30 : : #include <xlnt/xlnt_config.hpp>
31 : : #include <xlnt/cell/phonetic_run.hpp>
32 : : #include <xlnt/cell/rich_text_run.hpp>
33 : : #include <xlnt/worksheet/phonetic_pr.hpp>
34 : :
35 : : namespace xlnt {
36 : :
37 : : /// <summary>
38 : : /// Encapsulates zero or more formatted text runs where a text run
39 : : /// is a string of text with the same defined formatting.
40 : : /// </summary>
41 : : class XLNT_API rich_text
42 : : {
43 : : public:
44 : : /// <summary>
45 : : /// Constructs an empty rich text object with no font and empty text.
46 : : /// </summary>
47 :CBC 2322 : rich_text() = default;
48 : :
49 : : /// <summary>
50 : : /// Constructs a rich text object with the given text and no font.
51 : : /// </summary>
52 : : rich_text(const std::string &plain_text);
53 : :
54 : : /// <summary>
55 : : /// Constructs a rich text object from other
56 : : /// </summary>
57 : : rich_text(const rich_text &other);
58 : :
59 : : /// <summary>
60 : : /// Constructs a rich text object with the given text and font.
61 : : /// </summary>
62 : : rich_text(const std::string &plain_text, const class font &text_font);
63 : :
64 : : /// <summary>
65 : : /// Copy constructor.
66 : : /// </summary>
67 : : rich_text(const rich_text_run &single_run);
68 : :
69 : : /// <summary>
70 : : /// Removes all text runs from this text.
71 : : /// </summary>
72 : : void clear();
73 : :
74 : : /// <summary>
75 : : /// Clears any runs in this text and adds a single run with default formatting and
76 : : /// the given string as its textual content.
77 : : /// </summary>
78 : : void plain_text(const std::string &s, bool preserve_space);
79 : :
80 : : /// <summary>
81 : : /// Combines the textual content of each text run in order and returns the result.
82 : : /// </summary>
83 : : std::string plain_text() const;
84 : :
85 : : /// <summary>
86 : : /// Returns a copy of the individual runs that comprise this text.
87 : : /// </summary>
88 : : std::vector<rich_text_run> runs() const;
89 : :
90 : : /// <summary>
91 : : /// Sets the runs of this text all at once.
92 : : /// </summary>
93 : : void runs(const std::vector<rich_text_run> &new_runs);
94 : :
95 : : /// <summary>
96 : : /// Adds a new run to the end of the set of runs.
97 : : /// </summary>
98 : : void add_run(const rich_text_run &t);
99 : :
100 : : /// <summary>
101 : : /// Returns a copy of the individual runs that comprise this text.
102 : : /// </summary>
103 : : std::vector<phonetic_run> phonetic_runs() const;
104 : :
105 : : /// <summary>
106 : : /// Sets the runs of this text all at once.
107 : : /// </summary>
108 : : void phonetic_runs(const std::vector<phonetic_run> &new_phonetic_runs);
109 : :
110 : : /// <summary>
111 : : /// Adds a new run to the end of the set of runs.
112 : : /// </summary>
113 : : void add_phonetic_run(const phonetic_run &t);
114 : :
115 : : /// <summary>
116 : : /// Returns true if this text has phonetic properties
117 : : /// </summary>
118 : : bool has_phonetic_properties() const;
119 : :
120 : : /// <summary>
121 : : /// Returns the phonetic properties of this text.
122 : : /// </summary>
123 : : const phonetic_pr &phonetic_properties() const;
124 : :
125 : : /// <summary>
126 : : /// Sets the phonetic properties of this text to phonetic_props
127 : : /// </summary>
128 : : void phonetic_properties(const phonetic_pr &phonetic_props);
129 : :
130 : : /// <summary>
131 : : /// Copies rich text object from other
132 : : /// </summary>
133 : : rich_text &operator=(const rich_text &rhs);
134 : :
135 : : /// <summary>
136 : : /// Returns true if the runs that make up this text are identical to those in rhs.
137 : : /// </summary>
138 : : bool operator==(const rich_text &rhs) const;
139 : :
140 : : /// <summary>
141 : : /// Returns true if the runs that make up this text are identical to those in rhs.
142 : : /// </summary>
143 : : bool operator!=(const rich_text &rhs) const;
144 : :
145 : : /// <summary>
146 : : /// Returns true if this text has a single unformatted run with text equal to rhs.
147 : : /// </summary>
148 : : bool operator==(const std::string &rhs) const;
149 : :
150 : : /// <summary>
151 : : /// Returns true if this text has a single unformatted run with text equal to rhs.
152 : : /// </summary>
153 : : bool operator!=(const std::string &rhs) const;
154 : :
155 : : private:
156 : : /// <summary>
157 : : /// The runs that make up this rich text.
158 : : /// </summary>
159 : : std::vector<rich_text_run> runs_;
160 : : std::vector<phonetic_run> phonetic_runs_;
161 : : optional<phonetic_pr> phonetic_properties_;
162 : : };
163 : :
164 : : class XLNT_API rich_text_hash
165 : : {
166 : : public:
167 : 1249 : std::size_t operator()(const rich_text &k) const
168 : : {
169 : 1249 : std::size_t res = 0;
170 : :
171 [ + + + + ]: 2519 : for (auto r : k.runs())
172 : : {
173 : 1270 : res ^= std::hash<std::string>()(r.first);
174 : 2519 : }
175 : :
176 : 1249 : return res;
177 : : }
178 : : };
179 : :
180 : : } // namespace xlnt
|