diff --git a/include/xlnt/cell/phonetic_run.hpp b/include/xlnt/cell/phonetic_run.hpp new file mode 100644 index 00000000..e5eee8cd --- /dev/null +++ b/include/xlnt/cell/phonetic_run.hpp @@ -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 + +#include + +namespace xlnt { + +/// +/// Encapsulates a run of text that +/// +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 diff --git a/include/xlnt/cell/rich_text.hpp b/include/xlnt/cell/rich_text.hpp index 31cb2790..c9cdcd6c 100644 --- a/include/xlnt/cell/rich_text.hpp +++ b/include/xlnt/cell/rich_text.hpp @@ -28,6 +28,8 @@ #include #include +#include +#include namespace xlnt { @@ -94,6 +96,36 @@ public: /// void add_run(const rich_text_run &t); + /// + /// Returns a copy of the individual runs that comprise this text. + /// + std::vector phonetic_runs() const; + + /// + /// Sets the runs of this text all at once. + /// + void phonetic_runs(const std::vector &new_phonetic_runs); + + /// + /// Adds a new run to the end of the set of runs. + /// + void add_phonetic_run(const phonetic_run &t); + + /// + /// Returns true if this text has phonetic properties + /// + bool has_phonetic_properties() const; + + /// + /// Returns the phonetic properties of this text. + /// + const phonetic_pr &phonetic_properties() const; + + /// + /// Sets the phonetic properties of this text to phonetic_props + /// + void phonetic_properties(const phonetic_pr& phonetic_props); + /// /// Copies rich text object from other /// @@ -124,6 +156,8 @@ private: /// The runs that make up this rich text. /// std::vector runs_; + std::vector phonetic_runs_; + optional phonetic_properties_; }; class XLNT_API rich_text_hash diff --git a/source/cell/phonetic_run.cpp b/source/cell/phonetic_run.cpp new file mode 100644 index 00000000..65da5a9a --- /dev/null +++ b/source/cell/phonetic_run.cpp @@ -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 +#include + +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 diff --git a/source/cell/rich_text.cpp b/source/cell/rich_text.cpp index 3cf13290..86b5e67e 100644 --- a/source/cell/rich_text.cpp +++ b/source/cell/rich_text.cpp @@ -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 rich_text::phonetic_runs() const +{ + return phonetic_runs_; +} + +void rich_text::phonetic_runs(const std::vector &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; } diff --git a/tests/cell/rich_text_test_suite.cpp b/tests/cell/rich_text_test_suite.cpp index 3af26383..af866c4e 100644 --- a/tests/cell/rich_text_test_suite.cpp +++ b/tests/cell/rich_text_test_suite.cpp @@ -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{}; \ No newline at end of file