2021-08-22 20:23:18 +08:00
|
|
|
// Copyright (c) 2014-2021 Thomas Fussell
|
2016-12-23 19:52:02 +08:00
|
|
|
// 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
|
2021-08-22 20:23:18 +08:00
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2016-12-23 19:52:02 +08:00
|
|
|
// 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>
|
|
|
|
|
2018-07-14 15:52:21 +08:00
|
|
|
namespace {
|
2019-12-27 01:03:12 +08:00
|
|
|
bool has_trailing_whitespace(const std::string &s)
|
2018-07-14 15:52:21 +08:00
|
|
|
{
|
|
|
|
return !s.empty() && (s.front() == ' ' || s.back() == ' ');
|
|
|
|
};
|
2019-12-27 01:03:12 +08:00
|
|
|
} // namespace
|
2018-07-14 15:52:21 +08:00
|
|
|
|
2016-12-23 19:52:02 +08:00
|
|
|
namespace xlnt {
|
|
|
|
|
|
|
|
rich_text::rich_text(const std::string &plain_text)
|
2018-07-14 15:52:21 +08:00
|
|
|
: rich_text(rich_text_run{plain_text, optional<font>(), has_trailing_whitespace(plain_text)})
|
2016-12-23 19:52:02 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
rich_text::rich_text(const std::string &plain_text, const class font &text_font)
|
2018-07-14 15:52:21 +08:00
|
|
|
: rich_text(rich_text_run{plain_text, optional<font>(text_font), has_trailing_whitespace(plain_text)})
|
2016-12-23 19:52:02 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-08 14:24:58 +08:00
|
|
|
rich_text::rich_text(const rich_text &other)
|
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
*this = other;
|
2018-02-08 14:24:58 +08:00
|
|
|
}
|
|
|
|
|
2018-07-23 11:51:33 +08:00
|
|
|
rich_text &rich_text::operator=(const rich_text &rhs)
|
2018-02-08 14:24:58 +08:00
|
|
|
{
|
2018-11-21 21:43:31 +08:00
|
|
|
clear();
|
2018-07-23 11:51:33 +08:00
|
|
|
runs_ = rhs.runs_;
|
2018-11-21 21:43:31 +08:00
|
|
|
phonetic_runs_ = rhs.phonetic_runs_;
|
|
|
|
phonetic_properties_ = rhs.phonetic_properties_;
|
2018-07-23 11:51:33 +08:00
|
|
|
return *this;
|
2018-02-08 14:24:58 +08:00
|
|
|
}
|
|
|
|
|
2016-12-23 19:52:02 +08:00
|
|
|
rich_text::rich_text(const rich_text_run &single_run)
|
|
|
|
{
|
|
|
|
add_run(single_run);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rich_text::clear()
|
|
|
|
{
|
|
|
|
runs_.clear();
|
2018-11-21 21:43:31 +08:00
|
|
|
phonetic_runs_.clear();
|
|
|
|
phonetic_properties_.clear();
|
2016-12-23 19:52:02 +08:00
|
|
|
}
|
|
|
|
|
2018-05-29 11:13:57 +08:00
|
|
|
void rich_text::plain_text(const std::string &s, bool preserve_space = false)
|
2016-12-23 19:52:02 +08:00
|
|
|
{
|
|
|
|
clear();
|
2018-06-26 16:32:59 +08:00
|
|
|
add_run(rich_text_run{s, {}, preserve_space});
|
2016-12-23 19:52:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string rich_text::plain_text() const
|
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
if (runs_.size() == 1)
|
|
|
|
{
|
|
|
|
return runs_.begin()->first;
|
|
|
|
}
|
2018-02-08 14:24:58 +08:00
|
|
|
|
2016-12-23 19:52:02 +08:00
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2018-07-02 06:29:58 +08:00
|
|
|
void rich_text::runs(const std::vector<rich_text_run> &new_runs)
|
|
|
|
{
|
|
|
|
runs_ = new_runs;
|
|
|
|
}
|
|
|
|
|
2016-12-23 19:52:02 +08:00
|
|
|
void rich_text::add_run(const rich_text_run &t)
|
|
|
|
{
|
|
|
|
runs_.push_back(t);
|
|
|
|
}
|
|
|
|
|
2018-11-21 21:43:31 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
const phonetic_pr &rich_text::phonetic_properties() const
|
2018-11-21 21:43:31 +08:00
|
|
|
{
|
|
|
|
return phonetic_properties_.get();
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
void rich_text::phonetic_properties(const phonetic_pr &phonetic_props)
|
2018-11-21 21:43:31 +08:00
|
|
|
{
|
|
|
|
phonetic_properties_.set(phonetic_props);
|
|
|
|
}
|
|
|
|
|
2016-12-23 19:52:02 +08:00
|
|
|
bool rich_text::operator==(const rich_text &rhs) const
|
|
|
|
{
|
|
|
|
if (runs_.size() != rhs.runs_.size()) return false;
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-12-23 19:52:02 +08:00
|
|
|
for (std::size_t i = 0; i < runs_.size(); i++)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (runs_[i] != rhs.runs_[i]) return false;
|
2016-12-23 19:52:02 +08:00
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2018-11-21 21:43:31 +08:00
|
|
|
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;
|
|
|
|
|
2016-12-23 19:52:02 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rich_text::operator==(const std::string &rhs) const
|
|
|
|
{
|
|
|
|
return *this == rich_text(rhs);
|
|
|
|
}
|
|
|
|
|
2017-04-14 07:01:30 +08:00
|
|
|
bool rich_text::operator!=(const rich_text &rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rich_text::operator!=(const std::string &rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
2016-12-23 19:52:02 +08:00
|
|
|
} // namespace xlnt
|