diff --git a/include/xlnt/cell/rich_text.hpp b/include/xlnt/cell/rich_text.hpp new file mode 100644 index 00000000..8cce0b96 --- /dev/null +++ b/include/xlnt/cell/rich_text.hpp @@ -0,0 +1,109 @@ +// Copyright (c) 2016 Thomas Fussell +// +// 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 + +#include +#include + +namespace xlnt { + +/// +/// Encapsulates zero or more formatted text runs where a text run +/// is a string of text with the same defined formatting. +/// +class XLNT_API rich_text +{ +public: + /// + /// + /// + rich_text() = default; + + /// + /// + /// + rich_text(const std::string &plain_text); + + /// + /// + /// + rich_text(const std::string &plain_text, const class font &text_font); + + /// + /// + /// + rich_text(const rich_text_run &single_run); + + /// + /// Remove all text runs from this text. + /// + void clear(); + + /// + /// Clear any runs in this text and add a single run with default formatting and + /// the given string as its textual content. + /// + void plain_text(const std::string &s); + + /// + /// Combine the textual content of each text run in order and return the result. + /// + std::string plain_text() const; + + /// + /// Returns a copy of the individual runs that comprise this text. + /// + std::vector runs() const; + + /// + /// Set the runs of this text all at once. + /// + void runs(const std::vector &new_runs); + + /// + /// Add a new run to the end of the set of runs. + /// + void add_run(const rich_text_run &t); + + /// + /// Returns true if the runs that make up this text are identical to those in rhs. + /// + bool operator==(const rich_text &rhs) const; + + /// + /// Returns true if this text has a single unformatted run with text equal to rhs. + /// + bool operator==(const std::string &rhs) const; + +private: + /// + /// + /// + std::vector runs_; +}; + +} // namespace xlnt diff --git a/include/xlnt/cell/rich_text_run.hpp b/include/xlnt/cell/rich_text_run.hpp new file mode 100644 index 00000000..d2101c5e --- /dev/null +++ b/include/xlnt/cell/rich_text_run.hpp @@ -0,0 +1,36 @@ +// Copyright (c) 2016 Thomas Fussell +// +// 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 +#include +#include + +namespace xlnt { + +using rich_text_run = std::pair>; + +} // namespace xlnt diff --git a/source/cell/rich_text.cpp b/source/cell/rich_text.cpp new file mode 100644 index 00000000..316f52ed --- /dev/null +++ b/source/cell/rich_text.cpp @@ -0,0 +1,90 @@ +// Copyright (c) 2014-2016 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, 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 +#include + +namespace xlnt { + +rich_text::rich_text(const std::string &plain_text) + : rich_text(rich_text_run{plain_text,{}}) +{ +} + +rich_text::rich_text(const std::string &plain_text, const class font &text_font) + : rich_text(rich_text_run{plain_text,text_font}) +{ +} + +rich_text::rich_text(const rich_text_run &single_run) +{ + add_run(single_run); +} + +void rich_text::clear() +{ + runs_.clear(); +} + +void rich_text::plain_text(const std::string &s) +{ + clear(); + add_run(rich_text_run{s,{}}); +} + +std::string rich_text::plain_text() const +{ + 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::runs() const +{ + return runs_; +} + +void rich_text::add_run(const rich_text_run &t) +{ + runs_.push_back(t); +} + +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; + } + + return true; +} + +bool rich_text::operator==(const std::string &rhs) const +{ + return *this == rich_text(rhs); +} + +} // namespace xlnt diff --git a/source/cell/rich_text_run.cpp b/source/cell/rich_text_run.cpp new file mode 100644 index 00000000..d8a775fe --- /dev/null +++ b/source/cell/rich_text_run.cpp @@ -0,0 +1,28 @@ +// Copyright (c) 2014-2016 Thomas Fussell +// +// 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 + +namespace xlnt { + +} // namespace xlnt