rich_text.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. // Copyright (c) 2010-2015 openpyxl
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE
  21. //
  22. // @license: http://www.opensource.org/licenses/mit-license.php
  23. // @author: see AUTHORS file
  24. #include <numeric>
  25. #include <xlnt/cell/rich_text.hpp>
  26. #include <xlnt/cell/rich_text_run.hpp>
  27. namespace {
  28. bool has_trailing_whitespace(const std::string &s)
  29. {
  30. return !s.empty() && (s.front() == ' ' || s.back() == ' ');
  31. };
  32. } // namespace
  33. namespace xlnt {
  34. rich_text::rich_text(const std::string &plain_text)
  35. : rich_text(rich_text_run{plain_text, optional<font>(), has_trailing_whitespace(plain_text)})
  36. {
  37. }
  38. rich_text::rich_text(const std::string &plain_text, const class font &text_font)
  39. : rich_text(rich_text_run{plain_text, optional<font>(text_font), has_trailing_whitespace(plain_text)})
  40. {
  41. }
  42. rich_text::rich_text(const rich_text &other)
  43. {
  44. *this = other;
  45. }
  46. rich_text &rich_text::operator=(const rich_text &rhs)
  47. {
  48. clear();
  49. runs_ = rhs.runs_;
  50. phonetic_runs_ = rhs.phonetic_runs_;
  51. phonetic_properties_ = rhs.phonetic_properties_;
  52. return *this;
  53. }
  54. rich_text::rich_text(const rich_text_run &single_run)
  55. {
  56. add_run(single_run);
  57. }
  58. void rich_text::clear()
  59. {
  60. runs_.clear();
  61. phonetic_runs_.clear();
  62. phonetic_properties_.clear();
  63. }
  64. void rich_text::plain_text(const std::string &s, bool preserve_space = false)
  65. {
  66. clear();
  67. add_run(rich_text_run{s, {}, preserve_space});
  68. }
  69. std::string rich_text::plain_text() const
  70. {
  71. if (runs_.size() == 1)
  72. {
  73. return runs_.begin()->first;
  74. }
  75. return std::accumulate(runs_.begin(), runs_.end(), std::string(),
  76. [](const std::string &a, const rich_text_run &run) { return a + run.first; });
  77. }
  78. std::vector<rich_text_run> rich_text::runs() const
  79. {
  80. return runs_;
  81. }
  82. void rich_text::runs(const std::vector<rich_text_run> &new_runs)
  83. {
  84. runs_ = new_runs;
  85. }
  86. void rich_text::add_run(const rich_text_run &t)
  87. {
  88. runs_.push_back(t);
  89. }
  90. std::vector<phonetic_run> rich_text::phonetic_runs() const
  91. {
  92. return phonetic_runs_;
  93. }
  94. void rich_text::phonetic_runs(const std::vector<phonetic_run> &new_phonetic_runs)
  95. {
  96. phonetic_runs_ = new_phonetic_runs;
  97. }
  98. void rich_text::add_phonetic_run(const phonetic_run &r)
  99. {
  100. phonetic_runs_.push_back(r);
  101. }
  102. bool rich_text::has_phonetic_properties() const
  103. {
  104. return phonetic_properties_.is_set();
  105. }
  106. const phonetic_pr &rich_text::phonetic_properties() const
  107. {
  108. return phonetic_properties_.get();
  109. }
  110. void rich_text::phonetic_properties(const phonetic_pr &phonetic_props)
  111. {
  112. phonetic_properties_.set(phonetic_props);
  113. }
  114. bool rich_text::operator==(const rich_text &rhs) const
  115. {
  116. if (runs_.size() != rhs.runs_.size()) return false;
  117. for (std::size_t i = 0; i < runs_.size(); i++)
  118. {
  119. if (runs_[i] != rhs.runs_[i]) return false;
  120. }
  121. if (phonetic_runs_.size() != rhs.phonetic_runs_.size()) return false;
  122. for (std::size_t i = 0; i < phonetic_runs_.size(); i++)
  123. {
  124. if (phonetic_runs_[i] != rhs.phonetic_runs_[i]) return false;
  125. }
  126. if (phonetic_properties_ != rhs.phonetic_properties_) return false;
  127. return true;
  128. }
  129. bool rich_text::operator==(const std::string &rhs) const
  130. {
  131. return *this == rich_text(rhs);
  132. }
  133. bool rich_text::operator!=(const rich_text &rhs) const
  134. {
  135. return !(*this == rhs);
  136. }
  137. bool rich_text::operator!=(const std::string &rhs) const
  138. {
  139. return !(*this == rhs);
  140. }
  141. } // namespace xlnt