style.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <xlnt/styles/alignment.hpp>
  25. #include <xlnt/styles/border.hpp>
  26. #include <xlnt/styles/fill.hpp>
  27. #include <xlnt/styles/font.hpp>
  28. #include <xlnt/styles/number_format.hpp>
  29. #include <xlnt/styles/protection.hpp>
  30. #include <xlnt/styles/style.hpp>
  31. #include <detail/implementations/style_impl.hpp>
  32. #include <detail/implementations/stylesheet.hpp>
  33. namespace {
  34. std::vector<xlnt::number_format>::iterator find_number_format(
  35. std::vector<xlnt::number_format> &number_formats, std::size_t id)
  36. {
  37. return std::find_if(number_formats.begin(), number_formats.end(),
  38. [=](const xlnt::number_format &nf) { return nf.id() == id; });
  39. }
  40. } // namespace
  41. namespace xlnt {
  42. style::style(detail::style_impl *d)
  43. : d_(d)
  44. {
  45. }
  46. bool style::hidden() const
  47. {
  48. return d_->hidden_style;
  49. }
  50. style style::hidden(bool value)
  51. {
  52. d_->hidden_style = value;
  53. return style(d_);
  54. }
  55. std::size_t style::builtin_id() const
  56. {
  57. return d_->builtin_id.get();
  58. }
  59. bool style::builtin() const
  60. {
  61. return d_->builtin_id.is_set();
  62. }
  63. std::string style::name() const
  64. {
  65. return d_->name;
  66. }
  67. style style::name(const std::string &name)
  68. {
  69. d_->name = name;
  70. return *this;
  71. }
  72. bool style::custom_builtin() const
  73. {
  74. return d_->builtin_id.is_set() && d_->custom_builtin;
  75. }
  76. bool style::operator==(const style &other) const
  77. {
  78. return name() == other.name();
  79. }
  80. bool style::operator!=(const style &other) const
  81. {
  82. return !operator==(other);
  83. }
  84. xlnt::alignment style::alignment() const
  85. {
  86. return d_->parent->alignments.at(d_->alignment_id.get());
  87. }
  88. style style::alignment(const xlnt::alignment &new_alignment, optional<bool> applied)
  89. {
  90. d_->alignment_id = d_->parent->find_or_add(d_->parent->alignments, new_alignment);
  91. d_->alignment_applied = applied;
  92. return *this;
  93. }
  94. xlnt::border style::border() const
  95. {
  96. return d_->parent->borders.at(d_->border_id.get());
  97. }
  98. style style::border(const xlnt::border &new_border, optional<bool> applied)
  99. {
  100. d_->border_id = d_->parent->find_or_add(d_->parent->borders, new_border);
  101. d_->border_applied = applied;
  102. return *this;
  103. }
  104. xlnt::fill style::fill() const
  105. {
  106. return d_->parent->fills.at(d_->fill_id.get());
  107. }
  108. style style::fill(const xlnt::fill &new_fill, optional<bool> applied)
  109. {
  110. d_->fill_id = d_->parent->find_or_add(d_->parent->fills, new_fill);
  111. d_->fill_applied = applied;
  112. return *this;
  113. }
  114. xlnt::font style::font() const
  115. {
  116. return d_->parent->fonts.at(d_->font_id.get());
  117. }
  118. style style::font(const xlnt::font &new_font, optional<bool> applied)
  119. {
  120. d_->font_id = d_->parent->find_or_add(d_->parent->fonts, new_font);
  121. d_->font_applied = applied;
  122. return *this;
  123. }
  124. xlnt::number_format style::number_format() const
  125. {
  126. auto match = find_number_format(d_->parent->number_formats,
  127. d_->number_format_id.get());
  128. if (match == d_->parent->number_formats.end())
  129. {
  130. throw invalid_attribute();
  131. }
  132. return *match;
  133. }
  134. style style::number_format(const xlnt::number_format &new_number_format, optional<bool> applied)
  135. {
  136. auto copy = new_number_format;
  137. if (!copy.has_id())
  138. {
  139. copy.id(d_->parent->next_custom_number_format_id());
  140. d_->parent->number_formats.push_back(copy);
  141. }
  142. else if (find_number_format(d_->parent->number_formats, copy.id())
  143. == d_->parent->number_formats.end())
  144. {
  145. d_->parent->number_formats.push_back(copy);
  146. }
  147. d_->number_format_id = copy.id();
  148. d_->number_format_applied = applied;
  149. return *this;
  150. }
  151. xlnt::protection style::protection() const
  152. {
  153. return d_->parent->protections.at(d_->protection_id.get());
  154. }
  155. style style::protection(const xlnt::protection &new_protection, optional<bool> applied)
  156. {
  157. d_->protection_id = d_->parent->find_or_add(d_->parent->protections, new_protection);
  158. d_->protection_applied = applied;
  159. return *this;
  160. }
  161. bool style::alignment_applied() const
  162. {
  163. return d_->alignment_applied.is_set()
  164. ? d_->alignment_applied.get()
  165. : d_->alignment_id.is_set();
  166. }
  167. bool style::border_applied() const
  168. {
  169. return d_->border_applied.is_set()
  170. ? d_->border_applied.get()
  171. : d_->border_id.is_set();
  172. }
  173. bool style::fill_applied() const
  174. {
  175. return d_->fill_applied.is_set()
  176. ? d_->fill_applied.get()
  177. : d_->fill_id.is_set();
  178. }
  179. bool style::font_applied() const
  180. {
  181. return d_->font_applied.is_set()
  182. ? d_->font_applied.get()
  183. : d_->font_id.is_set();
  184. }
  185. bool style::number_format_applied() const
  186. {
  187. return d_->number_format_applied.is_set()
  188. ? d_->number_format_applied.get()
  189. : d_->number_format_id.is_set();
  190. }
  191. bool style::protection_applied() const
  192. {
  193. return d_->protection_applied.is_set()
  194. ? d_->protection_applied.get()
  195. : d_->protection_id.is_set();
  196. }
  197. bool style::pivot_button() const
  198. {
  199. return d_->pivot_button_;
  200. }
  201. void style::pivot_button(bool show)
  202. {
  203. d_->pivot_button_ = show;
  204. }
  205. bool style::quote_prefix() const
  206. {
  207. return d_->quote_prefix_;
  208. }
  209. void style::quote_prefix(bool quote)
  210. {
  211. d_->quote_prefix_ = quote;
  212. }
  213. } // namespace xlnt