mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
Add phonetic runs and properties to rich text
This commit is contained in:
parent
e9118cff24
commit
e9c23c3fc7
46
include/xlnt/cell/phonetic_run.hpp
Normal file
46
include/xlnt/cell/phonetic_run.hpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
// Copyright (c) 2016-2018
|
||||
//
|
||||
// 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, WRISING 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <xlnt/xlnt_config.hpp>
|
||||
|
||||
namespace xlnt {
|
||||
|
||||
/// <summary>
|
||||
/// Encapsulates a run of text that
|
||||
/// </summary>
|
||||
struct XLNT_API phonetic_run
|
||||
{
|
||||
std::string text;
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
bool preserve_space;
|
||||
|
||||
bool operator==(const phonetic_run &other) const;
|
||||
bool operator!=(const phonetic_run &other) const;
|
||||
};
|
||||
|
||||
} // namespace xlnt
|
|
@ -28,6 +28,8 @@
|
|||
|
||||
#include <xlnt/xlnt_config.hpp>
|
||||
#include <xlnt/cell/rich_text_run.hpp>
|
||||
#include <xlnt/cell/phonetic_run.hpp>
|
||||
#include <xlnt/worksheet/phonetic_pr.hpp>
|
||||
|
||||
namespace xlnt {
|
||||
|
||||
|
@ -94,6 +96,36 @@ public:
|
|||
/// </summary>
|
||||
void add_run(const rich_text_run &t);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a copy of the individual runs that comprise this text.
|
||||
/// </summary>
|
||||
std::vector<phonetic_run> phonetic_runs() const;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the runs of this text all at once.
|
||||
/// </summary>
|
||||
void phonetic_runs(const std::vector<phonetic_run> &new_phonetic_runs);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new run to the end of the set of runs.
|
||||
/// </summary>
|
||||
void add_phonetic_run(const phonetic_run &t);
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this text has phonetic properties
|
||||
/// </summary>
|
||||
bool has_phonetic_properties() const;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the phonetic properties of this text.
|
||||
/// </summary>
|
||||
const phonetic_pr &phonetic_properties() const;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the phonetic properties of this text to phonetic_props
|
||||
/// </summary>
|
||||
void phonetic_properties(const phonetic_pr& phonetic_props);
|
||||
|
||||
/// <summary>
|
||||
/// Copies rich text object from other
|
||||
/// </summary>
|
||||
|
@ -124,6 +156,8 @@ private:
|
|||
/// The runs that make up this rich text.
|
||||
/// </summary>
|
||||
std::vector<rich_text_run> runs_;
|
||||
std::vector<phonetic_run> phonetic_runs_;
|
||||
optional<phonetic_pr> phonetic_properties_;
|
||||
};
|
||||
|
||||
class XLNT_API rich_text_hash
|
||||
|
|
40
source/cell/phonetic_run.cpp
Normal file
40
source/cell/phonetic_run.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Copyright (c) 2014-2018
|
||||
//
|
||||
// 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, WRISING 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 <xlnt/cell/phonetic_run.hpp>
|
||||
#include <tuple>
|
||||
|
||||
namespace xlnt {
|
||||
|
||||
bool phonetic_run::operator==(const phonetic_run &other) const
|
||||
{
|
||||
return std::tie(text, start, end, preserve_space) ==
|
||||
std::tie(other.text, other.start, other.end, other.preserve_space);
|
||||
}
|
||||
|
||||
bool phonetic_run::operator!=(const phonetic_run &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
} // namespace xlnt
|
|
@ -52,8 +52,10 @@ rich_text::rich_text(const rich_text &other)
|
|||
|
||||
rich_text &rich_text::operator=(const rich_text &rhs)
|
||||
{
|
||||
runs_.clear();
|
||||
clear();
|
||||
runs_ = rhs.runs_;
|
||||
phonetic_runs_ = rhs.phonetic_runs_;
|
||||
phonetic_properties_ = rhs.phonetic_properties_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -65,6 +67,8 @@ rich_text::rich_text(const rich_text_run &single_run)
|
|||
void rich_text::clear()
|
||||
{
|
||||
runs_.clear();
|
||||
phonetic_runs_.clear();
|
||||
phonetic_properties_.clear();
|
||||
}
|
||||
|
||||
void rich_text::plain_text(const std::string &s, bool preserve_space = false)
|
||||
|
@ -99,6 +103,36 @@ 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;
|
||||
|
@ -108,6 +142,15 @@ bool rich_text::operator==(const rich_text &rhs) const
|
|||
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 true;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,8 @@ public:
|
|||
{
|
||||
register_test(test_operators);
|
||||
register_test(test_runs);
|
||||
register_test(test_phonetic_runs);
|
||||
register_test(test_phonetic_properties);
|
||||
}
|
||||
|
||||
void test_operators()
|
||||
|
@ -115,5 +117,30 @@ public:
|
|||
rt.runs(test_runs);
|
||||
xlnt_assert_equals(test_runs, rt.runs());
|
||||
}
|
||||
|
||||
void test_phonetic_runs()
|
||||
{
|
||||
xlnt::rich_text rt;
|
||||
rt.plain_text("取引", true);
|
||||
rt.add_phonetic_run({"トリヒキ", 0, 2});
|
||||
|
||||
xlnt_assert_equals(rt.phonetic_runs().size(), 1);
|
||||
xlnt_assert_equals(rt.phonetic_runs()[0].text, "トリヒキ");
|
||||
xlnt_assert_equals(rt.phonetic_runs()[0].start, 0);
|
||||
xlnt_assert_equals(rt.phonetic_runs()[0].end, 2);
|
||||
}
|
||||
|
||||
void test_phonetic_properties()
|
||||
{
|
||||
xlnt::rich_text rt;
|
||||
xlnt::phonetic_pr ph(1);
|
||||
ph.type(xlnt::phonetic_pr::type_from_string("fullwidthKatakana"));
|
||||
ph.alignment(xlnt::phonetic_pr::alignment_from_string("Center"));
|
||||
rt.phonetic_properties(ph);
|
||||
|
||||
xlnt_assert_equals(rt.has_phonetic_properties(), true);
|
||||
xlnt_assert_equals(rt.phonetic_properties().has_type(), true);
|
||||
xlnt_assert_equals(rt.phonetic_properties().has_alignment(), true);
|
||||
}
|
||||
};
|
||||
static rich_text_test_suite x{};
|
Loading…
Reference in New Issue
Block a user