Skip to content

Commit

Permalink
rename formatted_text to rich_text to match the spec, also text_run t…
Browse files Browse the repository at this point in the history
…o rich_text_run
  • Loading branch information
tfussell committed Dec 23, 2016
1 parent 244314d commit 6c32563
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 0 deletions.
109 changes: 109 additions & 0 deletions include/xlnt/cell/rich_text.hpp
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <vector>

#include <xlnt/xlnt_config.hpp>
#include <xlnt/cell/rich_text_run.hpp>

namespace xlnt {

/// <summary>
/// Encapsulates zero or more formatted text runs where a text run
/// is a string of text with the same defined formatting.
/// </summary>
class XLNT_API rich_text
{
public:
/// <summary>
///
/// </summary>
rich_text() = default;

/// <summary>
///
/// </summary>
rich_text(const std::string &plain_text);

/// <summary>
///
/// </summary>
rich_text(const std::string &plain_text, const class font &text_font);

/// <summary>
///
/// </summary>
rich_text(const rich_text_run &single_run);

/// <summary>
/// Remove all text runs from this text.
/// </summary>
void clear();

/// <summary>
/// Clear any runs in this text and add a single run with default formatting and
/// the given string as its textual content.
/// </summary>
void plain_text(const std::string &s);

/// <summary>
/// Combine the textual content of each text run in order and return the result.
/// </summary>
std::string plain_text() const;

/// <summary>
/// Returns a copy of the individual runs that comprise this text.
/// </summary>
std::vector<rich_text_run> runs() const;

/// <summary>
/// Set the runs of this text all at once.
/// </summary>
void runs(const std::vector<rich_text_run> &new_runs);

/// <summary>
/// Add a new run to the end of the set of runs.
/// </summary>
void add_run(const rich_text_run &t);

/// <summary>
/// Returns true if the runs that make up this text are identical to those in rhs.
/// </summary>
bool operator==(const rich_text &rhs) const;

/// <summary>
/// Returns true if this text has a single unformatted run with text equal to rhs.
/// </summary>
bool operator==(const std::string &rhs) const;

private:
/// <summary>
///
/// </summary>
std::vector<rich_text_run> runs_;
};

} // namespace xlnt
36 changes: 36 additions & 0 deletions include/xlnt/cell/rich_text_run.hpp
Original file line number Diff line number Diff line change
@@ -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 <string>

#include <xlnt/xlnt_config.hpp>
#include <xlnt/styles/font.hpp>
#include <xlnt/utils/optional.hpp>

namespace xlnt {

using rich_text_run = std::pair<std::string, optional<font>>;

} // namespace xlnt
90 changes: 90 additions & 0 deletions source/cell/rich_text.cpp
Original file line number Diff line number Diff line change
@@ -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 <numeric>

#include <xlnt/cell/rich_text.hpp>
#include <xlnt/cell/rich_text_run.hpp>

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_run> 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
28 changes: 28 additions & 0 deletions source/cell/rich_text_run.cpp
Original file line number Diff line number Diff line change
@@ -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 <xlnt/cell/rich_text_run.hpp>

namespace xlnt {

} // namespace xlnt

0 comments on commit 6c32563

Please sign in to comment.