Branch data TLA Line data Source code
1 : : // Copyright (c) 2014-2022 Thomas Fussell
2 : : // Copyright (c) 2010-2015 openpyxl
3 : : // Copyright (c) 2024-2025 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 : : #include <numeric>
26 : :
27 : : #include <xlnt/cell/rich_text.hpp>
28 : : #include <xlnt/cell/rich_text_run.hpp>
29 : :
30 : : namespace {
31 :CBC 326 : bool has_trailing_whitespace(const std::string &s)
32 : : {
33 [ + + + + : 326 : return !s.empty() && (s.front() == ' ' || s.back() == ' ');
- + ]
34 : : };
35 : : } // namespace
36 : :
37 : : namespace xlnt {
38 : :
39 : 322 : rich_text::rich_text(const std::string &plain_text)
40 [ + + - - : 322 : : rich_text(rich_text_run{plain_text, optional<font>(), has_trailing_whitespace(plain_text)})
- - ]
41 : : {
42 : 322 : }
43 : :
44 : 4 : rich_text::rich_text(const std::string &plain_text, const class font &text_font)
45 [ + + + - : 4 : : rich_text(rich_text_run{plain_text, optional<font>(text_font), has_trailing_whitespace(plain_text)})
- - - ]
46 : : {
47 : 4 : }
48 : :
49 : 7190 : rich_text::rich_text(const rich_text &other)
50 : : {
51 [ + ]: 7190 : *this = other;
52 : 7190 : }
53 : :
54 : 7341 : rich_text &rich_text::operator=(const rich_text &rhs)
55 : : {
56 : 7341 : clear();
57 : 7341 : runs_ = rhs.runs_;
58 : 7341 : phonetic_runs_ = rhs.phonetic_runs_;
59 : 7341 : phonetic_properties_ = rhs.phonetic_properties_;
60 : 7341 : return *this;
61 : : }
62 : :
63 : 326 : rich_text::rich_text(const rich_text_run &single_run)
64 : : {
65 [ + ]: 326 : add_run(single_run);
66 : 326 : }
67 : :
68 : 8873 : void rich_text::clear()
69 : : {
70 : 8873 : runs_.clear();
71 : 8873 : phonetic_runs_.clear();
72 : 8873 : phonetic_properties_.clear();
73 : 8873 : }
74 : :
75 : 765 : void rich_text::plain_text(const std::string &s, bool preserve_space = false)
76 : : {
77 : 765 : clear();
78 [ + + - - : 765 : add_run(rich_text_run{s, {}, preserve_space});
- - ]
79 : 765 : }
80 : :
81 : 895 : std::string rich_text::plain_text() const
82 : : {
83 [ + + ]: 895 : if (runs_.size() == 1)
84 : : {
85 [ + ]: 882 : return runs_.begin()->first;
86 : : }
87 : :
88 : 26 : return std::accumulate(runs_.begin(), runs_.end(), std::string(),
89 [ + ]: 49 : [](const std::string &a, const rich_text_run &run) { return a + run.first; });
90 : : }
91 : :
92 : 2333 : std::vector<rich_text_run> rich_text::runs() const
93 : : {
94 : 2333 : return runs_;
95 : : }
96 : :
97 : 1 : void rich_text::runs(const std::vector<rich_text_run> &new_runs)
98 : : {
99 : 1 : runs_ = new_runs;
100 : 1 : }
101 : :
102 : 1213 : void rich_text::add_run(const rich_text_run &t)
103 : : {
104 : 1213 : runs_.push_back(t);
105 : 1213 : }
106 : :
107 : 356 : std::vector<phonetic_run> rich_text::phonetic_runs() const
108 : : {
109 : 356 : return phonetic_runs_;
110 : : }
111 : :
112 :UBC 0 : void rich_text::phonetic_runs(const std::vector<phonetic_run> &new_phonetic_runs)
113 : : {
114 : 0 : phonetic_runs_ = new_phonetic_runs;
115 : 0 : }
116 : :
117 :CBC 3 : void rich_text::add_phonetic_run(const phonetic_run &r)
118 : : {
119 : 3 : phonetic_runs_.push_back(r);
120 : 3 : }
121 : :
122 : 351 : bool rich_text::has_phonetic_properties() const
123 : : {
124 : 351 : return phonetic_properties_.is_set();
125 : : }
126 : :
127 : 11 : const phonetic_pr &rich_text::phonetic_properties() const
128 : : {
129 : 11 : return phonetic_properties_.get();
130 : : }
131 : :
132 : 35 : void rich_text::phonetic_properties(const phonetic_pr &phonetic_props)
133 : : {
134 : 35 : phonetic_properties_.set(phonetic_props);
135 : 35 : }
136 : :
137 : 50 : bool rich_text::operator==(const rich_text &rhs) const
138 : : {
139 [ + + ]: 50 : if (runs_.size() != rhs.runs_.size()) return false;
140 : :
141 [ + + ]: 90 : for (std::size_t i = 0; i < runs_.size(); i++)
142 : : {
143 [ + + ]: 46 : if (runs_[i] != rhs.runs_[i]) return false;
144 : : }
145 : :
146 [ + + ]: 44 : if (phonetic_runs_.size() != rhs.phonetic_runs_.size()) return false;
147 : :
148 [ - + ]: 42 : for (std::size_t i = 0; i < phonetic_runs_.size(); i++)
149 : : {
150 [ # # ]:UBC 0 : if (phonetic_runs_[i] != rhs.phonetic_runs_[i]) return false;
151 : : }
152 : :
153 [ - + ]:CBC 42 : if (phonetic_properties_ != rhs.phonetic_properties_) return false;
154 : :
155 : 42 : return true;
156 : : }
157 : :
158 : 6 : bool rich_text::operator==(const std::string &rhs) const
159 : : {
160 [ + + ]: 6 : return *this == rich_text(rhs);
161 : : }
162 : :
163 : 6 : bool rich_text::operator!=(const rich_text &rhs) const
164 : : {
165 : 6 : return !(*this == rhs);
166 : : }
167 : :
168 :UBC 0 : bool rich_text::operator!=(const std::string &rhs) const
169 : : {
170 : 0 : return !(*this == rhs);
171 : : }
172 : :
173 : : } // namespace xlnt
|