header_footer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/worksheet/header_footer.hpp>
  25. namespace xlnt {
  26. bool header_footer::has_header() const
  27. {
  28. return !odd_headers_.empty() || !even_headers_.empty() || has_first_page_header();
  29. }
  30. bool header_footer::has_footer() const
  31. {
  32. return !odd_headers_.empty() || !even_headers_.empty() || has_first_page_footer();
  33. }
  34. bool header_footer::align_with_margins() const
  35. {
  36. return align_with_margins_;
  37. }
  38. header_footer &header_footer::align_with_margins(bool align)
  39. {
  40. align_with_margins_ = align;
  41. return *this;
  42. }
  43. bool header_footer::different_odd_even() const
  44. {
  45. return different_odd_even_;
  46. }
  47. bool header_footer::different_first() const
  48. {
  49. return !first_headers_.empty() || !first_footers_.empty();
  50. }
  51. bool header_footer::scale_with_doc() const
  52. {
  53. return scale_with_doc_;
  54. }
  55. header_footer &header_footer::scale_with_doc(bool scale)
  56. {
  57. scale_with_doc_ = scale;
  58. return *this;
  59. }
  60. // Normal Header
  61. bool header_footer::has_header(location where) const
  62. {
  63. return odd_headers_.count(where) > 0 || has_first_page_header(where);
  64. }
  65. void header_footer::clear_header()
  66. {
  67. odd_headers_.clear();
  68. even_headers_.clear();
  69. first_headers_.clear();
  70. }
  71. void header_footer::clear_header(location where)
  72. {
  73. odd_headers_.erase(where);
  74. even_headers_.erase(where);
  75. first_headers_.erase(where);
  76. }
  77. header_footer &header_footer::header(location where, const std::string &text)
  78. {
  79. return header(where, rich_text(text));
  80. }
  81. header_footer &header_footer::header(location where, const rich_text &text)
  82. {
  83. odd_headers_[where] = text;
  84. return *this;
  85. }
  86. rich_text header_footer::header(location where) const
  87. {
  88. return odd_headers_.at(where);
  89. }
  90. // First Page Header
  91. bool header_footer::has_first_page_header() const
  92. {
  93. return !first_headers_.empty();
  94. }
  95. bool header_footer::has_first_page_header(location where) const
  96. {
  97. return first_headers_.count(where) > 0;
  98. }
  99. void header_footer::clear_first_page_header()
  100. {
  101. first_headers_.clear();
  102. }
  103. void header_footer::clear_first_page_header(location where)
  104. {
  105. first_headers_.erase(where);
  106. }
  107. header_footer &header_footer::first_page_header(location where, const rich_text &text)
  108. {
  109. first_headers_[where] = text;
  110. return *this;
  111. }
  112. rich_text header_footer::first_page_header(location where) const
  113. {
  114. return first_headers_.at(where);
  115. }
  116. // Odd/Even Header
  117. bool header_footer::has_odd_even_header() const
  118. {
  119. return different_odd_even() && !odd_headers_.empty();
  120. }
  121. bool header_footer::has_odd_even_header(location where) const
  122. {
  123. return different_odd_even() && odd_headers_.count(where) > 0;
  124. }
  125. void header_footer::clear_odd_even_header()
  126. {
  127. odd_headers_.clear();
  128. even_headers_.clear();
  129. different_odd_even_ = false;
  130. }
  131. void header_footer::clear_odd_even_header(location where)
  132. {
  133. odd_headers_.erase(where);
  134. even_headers_.erase(where);
  135. }
  136. header_footer &header_footer::odd_even_header(location where, const rich_text &odd, const rich_text &even)
  137. {
  138. odd_headers_[where] = odd;
  139. even_headers_[where] = even;
  140. different_odd_even_ = true;
  141. return *this;
  142. }
  143. rich_text header_footer::odd_header(location where) const
  144. {
  145. return odd_headers_.at(where);
  146. }
  147. rich_text header_footer::even_header(location where) const
  148. {
  149. return even_headers_.at(where);
  150. }
  151. // Normal Footer
  152. bool header_footer::has_footer(location where) const
  153. {
  154. return odd_footers_.count(where) > 0;
  155. }
  156. void header_footer::clear_footer()
  157. {
  158. odd_footers_.clear();
  159. even_footers_.clear();
  160. first_footers_.clear();
  161. }
  162. void header_footer::clear_footer(location where)
  163. {
  164. odd_footers_.erase(where);
  165. even_footers_.erase(where);
  166. first_footers_.erase(where);
  167. }
  168. header_footer &header_footer::footer(location where, const std::string &text)
  169. {
  170. return footer(where, rich_text(text));
  171. }
  172. header_footer &header_footer::footer(location where, const rich_text &text)
  173. {
  174. odd_footers_[where] = text;
  175. return *this;
  176. }
  177. rich_text header_footer::footer(location where) const
  178. {
  179. return odd_footers_.at(where);
  180. }
  181. // First Page footer
  182. bool header_footer::has_first_page_footer() const
  183. {
  184. return different_first() && !first_footers_.empty();
  185. }
  186. bool header_footer::has_first_page_footer(location where) const
  187. {
  188. return different_first() && first_footers_.count(where) > 0;
  189. }
  190. void header_footer::clear_first_page_footer()
  191. {
  192. first_footers_.clear();
  193. }
  194. void header_footer::clear_first_page_footer(location where)
  195. {
  196. first_footers_.erase(where);
  197. }
  198. header_footer &header_footer::first_page_footer(location where, const rich_text &text)
  199. {
  200. first_footers_[where] = text;
  201. return *this;
  202. }
  203. rich_text header_footer::first_page_footer(location where) const
  204. {
  205. return first_footers_.at(where);
  206. }
  207. // Odd/Even Footer
  208. bool header_footer::has_odd_even_footer() const
  209. {
  210. return different_odd_even() && !even_footers_.empty();
  211. }
  212. bool header_footer::has_odd_even_footer(location where) const
  213. {
  214. return different_odd_even() && even_footers_.count(where) > 0;
  215. }
  216. void header_footer::clear_odd_even_footer()
  217. {
  218. odd_footers_.clear();
  219. even_footers_.clear();
  220. }
  221. void header_footer::clear_odd_even_footer(location where)
  222. {
  223. odd_footers_.erase(where);
  224. even_footers_.erase(where);
  225. }
  226. header_footer &header_footer::odd_even_footer(location where, const rich_text &odd, const rich_text &even)
  227. {
  228. odd_footers_[where] = odd;
  229. even_footers_[where] = even;
  230. different_odd_even_ = true;
  231. return *this;
  232. }
  233. rich_text header_footer::odd_footer(location where) const
  234. {
  235. return odd_footers_.at(where);
  236. }
  237. rich_text header_footer::even_footer(location where) const
  238. {
  239. return even_footers_.at(where);
  240. }
  241. bool header_footer::operator==(const header_footer &rhs) const
  242. {
  243. return align_with_margins_ == rhs.align_with_margins_
  244. && different_odd_even_ == rhs.different_odd_even_
  245. && scale_with_doc_ == rhs.scale_with_doc_
  246. && odd_headers_ == rhs.odd_headers_
  247. && even_headers_ == rhs.even_headers_
  248. && first_headers_ == rhs.first_headers_
  249. && odd_footers_ == rhs.odd_footers_
  250. && even_footers_ == rhs.even_footers_
  251. && first_footers_ == rhs.first_footers_;
  252. }
  253. } // namespace xlnt