-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathrich_text.cpp
More file actions
174 lines (143 loc) · 4.45 KB
/
rich_text.cpp
File metadata and controls
174 lines (143 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) 2014-2021 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <numeric>
#include <xlnt/cell/rich_text.hpp>
#include <xlnt/cell/rich_text_run.hpp>
namespace {
bool has_trailing_whitespace(const std::string &s)
{
return !s.empty() && (s.front() == ' ' || s.back() == ' ');
};
} // namespace
namespace xlnt {
rich_text::rich_text(const std::string &plain_text)
: rich_text(rich_text_run{plain_text, optional<font>(), has_trailing_whitespace(plain_text)})
{
}
rich_text::rich_text(const std::string &plain_text, const class font &text_font)
: rich_text(rich_text_run{plain_text, optional<font>(text_font), has_trailing_whitespace(plain_text)})
{
}
rich_text::rich_text(const rich_text &other)
{
*this = other;
}
rich_text &rich_text::operator=(const rich_text &rhs)
{
clear();
runs_ = rhs.runs_;
phonetic_runs_ = rhs.phonetic_runs_;
phonetic_properties_ = rhs.phonetic_properties_;
unique_index = rhs.unique_index;
return *this;
}
rich_text::rich_text(const rich_text_run &single_run)
{
add_run(single_run);
}
void rich_text::clear()
{
runs_.clear();
phonetic_runs_.clear();
phonetic_properties_.clear();
unique_index = 0;
}
void rich_text::plain_text(const std::string &s, bool preserve_space = false)
{
clear();
add_run(rich_text_run{s, {}, preserve_space});
}
std::string rich_text::plain_text() const
{
if (runs_.size() == 1)
{
return runs_.begin()->first;
}
return std::accumulate(runs_.begin(), runs_.end(), std::string(),
[](const std::string &a, const rich_text_run &run) { return a + run.first; });
}
std::vector<rich_text_run> rich_text::runs() const
{
return runs_;
}
void rich_text::runs(const std::vector<rich_text_run> &new_runs)
{
runs_ = new_runs;
}
void rich_text::add_run(const rich_text_run &t)
{
runs_.push_back(t);
}
std::vector<phonetic_run> rich_text::phonetic_runs() const
{
return phonetic_runs_;
}
void rich_text::phonetic_runs(const std::vector<phonetic_run> &new_phonetic_runs)
{
phonetic_runs_ = new_phonetic_runs;
}
void rich_text::add_phonetic_run(const phonetic_run &r)
{
phonetic_runs_.push_back(r);
}
bool rich_text::has_phonetic_properties() const
{
return phonetic_properties_.is_set();
}
const phonetic_pr &rich_text::phonetic_properties() const
{
return phonetic_properties_.get();
}
void rich_text::phonetic_properties(const phonetic_pr &phonetic_props)
{
phonetic_properties_.set(phonetic_props);
}
bool rich_text::operator==(const rich_text &rhs) const
{
if (runs_.size() != rhs.runs_.size()) return false;
for (std::size_t i = 0; i < runs_.size(); i++)
{
if (runs_[i] != rhs.runs_[i]) return false;
}
if (phonetic_runs_.size() != rhs.phonetic_runs_.size()) return false;
for (std::size_t i = 0; i < phonetic_runs_.size(); i++)
{
if (phonetic_runs_[i] != rhs.phonetic_runs_[i]) return false;
}
if (phonetic_properties_ != rhs.phonetic_properties_) return false;
return unique_index == rhs.unique_index;
}
bool rich_text::operator==(const std::string &rhs) const
{
return *this == rich_text(rhs);
}
bool rich_text::operator!=(const rich_text &rhs) const
{
return !(*this == rhs);
}
bool rich_text::operator!=(const std::string &rhs) const
{
return !(*this == rhs);
}
} // namespace xlnt