1 // Copyright (c) 2014-2020 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, WRISING 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 
25 #include <xlnt/worksheet/header_footer.hpp>
26 
27 namespace xlnt {
28 
has_header() const29 bool header_footer::has_header() const
30 {
31     return !odd_headers_.empty() || !even_headers_.empty() || has_first_page_header();
32 }
33 
has_footer() const34 bool header_footer::has_footer() const
35 {
36     return !odd_headers_.empty() || !even_headers_.empty() || has_first_page_footer();
37 }
38 
align_with_margins() const39 bool header_footer::align_with_margins() const
40 {
41     return align_with_margins_;
42 }
43 
align_with_margins(bool align)44 header_footer &header_footer::align_with_margins(bool align)
45 {
46     align_with_margins_ = align;
47     return *this;
48 }
49 
different_odd_even() const50 bool header_footer::different_odd_even() const
51 {
52     return different_odd_even_;
53 }
54 
different_first() const55 bool header_footer::different_first() const
56 {
57     return !first_headers_.empty() || !first_footers_.empty();
58 }
59 
scale_with_doc() const60 bool header_footer::scale_with_doc() const
61 {
62     return scale_with_doc_;
63 }
64 
scale_with_doc(bool scale)65 header_footer &header_footer::scale_with_doc(bool scale)
66 {
67     scale_with_doc_ = scale;
68     return *this;
69 }
70 
71 // Normal Header
72 
has_header(location where) const73 bool header_footer::has_header(location where) const
74 {
75     return odd_headers_.count(where) > 0 || has_first_page_header(where);
76 }
77 
clear_header()78 void header_footer::clear_header()
79 {
80     odd_headers_.clear();
81     even_headers_.clear();
82     first_headers_.clear();
83 }
84 
clear_header(location where)85 void header_footer::clear_header(location where)
86 {
87     odd_headers_.erase(where);
88     even_headers_.erase(where);
89     first_headers_.erase(where);
90 }
91 
header(location where,const std::string & text)92 header_footer &header_footer::header(location where, const std::string &text)
93 {
94     return header(where, rich_text(text));
95 }
96 
header(location where,const rich_text & text)97 header_footer &header_footer::header(location where, const rich_text &text)
98 {
99     odd_headers_[where] = text;
100     return *this;
101 }
102 
header(location where) const103 rich_text header_footer::header(location where) const
104 {
105     return odd_headers_.at(where);
106 }
107 
108 // First Page Header
109 
has_first_page_header() const110 bool header_footer::has_first_page_header() const
111 {
112     return !first_headers_.empty();
113 }
114 
has_first_page_header(location where) const115 bool header_footer::has_first_page_header(location where) const
116 {
117     return first_headers_.count(where) > 0;
118 }
119 
clear_first_page_header()120 void header_footer::clear_first_page_header()
121 {
122     first_headers_.clear();
123 }
124 
clear_first_page_header(location where)125 void header_footer::clear_first_page_header(location where)
126 {
127     first_headers_.erase(where);
128 }
129 
first_page_header(location where,const rich_text & text)130 header_footer &header_footer::first_page_header(location where, const rich_text &text)
131 {
132     first_headers_[where] = text;
133     return *this;
134 }
135 
first_page_header(location where) const136 rich_text header_footer::first_page_header(location where) const
137 {
138     return first_headers_.at(where);
139 }
140 
141 // Odd/Even Header
142 
has_odd_even_header() const143 bool header_footer::has_odd_even_header() const
144 {
145     return different_odd_even() && !odd_headers_.empty();
146 }
147 
has_odd_even_header(location where) const148 bool header_footer::has_odd_even_header(location where) const
149 {
150     return different_odd_even() && odd_headers_.count(where) > 0;
151 }
152 
clear_odd_even_header()153 void header_footer::clear_odd_even_header()
154 {
155     odd_headers_.clear();
156     even_headers_.clear();
157     different_odd_even_ = false;
158 }
159 
clear_odd_even_header(location where)160 void header_footer::clear_odd_even_header(location where)
161 {
162     odd_headers_.erase(where);
163     even_headers_.erase(where);
164 }
165 
odd_even_header(location where,const rich_text & odd,const rich_text & even)166 header_footer &header_footer::odd_even_header(location where, const rich_text &odd, const rich_text &even)
167 {
168     odd_headers_[where] = odd;
169     even_headers_[where] = even;
170     different_odd_even_ = true;
171 
172     return *this;
173 }
174 
odd_header(location where) const175 rich_text header_footer::odd_header(location where) const
176 {
177     return odd_headers_.at(where);
178 }
179 
even_header(location where) const180 rich_text header_footer::even_header(location where) const
181 {
182     return even_headers_.at(where);
183 }
184 
185 // Normal Footer
186 
has_footer(location where) const187 bool header_footer::has_footer(location where) const
188 {
189     return odd_footers_.count(where) > 0;
190 }
191 
clear_footer()192 void header_footer::clear_footer()
193 {
194     odd_footers_.clear();
195     even_footers_.clear();
196     first_footers_.clear();
197 }
198 
clear_footer(location where)199 void header_footer::clear_footer(location where)
200 {
201     odd_footers_.erase(where);
202     even_footers_.erase(where);
203     first_footers_.erase(where);
204 }
205 
footer(location where,const std::string & text)206 header_footer &header_footer::footer(location where, const std::string &text)
207 {
208     return footer(where, rich_text(text));
209 }
210 
footer(location where,const rich_text & text)211 header_footer &header_footer::footer(location where, const rich_text &text)
212 {
213     odd_footers_[where] = text;
214     return *this;
215 }
216 
footer(location where) const217 rich_text header_footer::footer(location where) const
218 {
219     return odd_footers_.at(where);
220 }
221 
222 // First Page footer
223 
has_first_page_footer() const224 bool header_footer::has_first_page_footer() const
225 {
226     return different_first() && !first_footers_.empty();
227 }
228 
has_first_page_footer(location where) const229 bool header_footer::has_first_page_footer(location where) const
230 {
231     return different_first() && first_footers_.count(where) > 0;
232 }
233 
clear_first_page_footer()234 void header_footer::clear_first_page_footer()
235 {
236     first_footers_.clear();
237 }
238 
clear_first_page_footer(location where)239 void header_footer::clear_first_page_footer(location where)
240 {
241     first_footers_.erase(where);
242 }
243 
first_page_footer(location where,const rich_text & text)244 header_footer &header_footer::first_page_footer(location where, const rich_text &text)
245 {
246     first_footers_[where] = text;
247     return *this;
248 }
249 
first_page_footer(location where) const250 rich_text header_footer::first_page_footer(location where) const
251 {
252     return first_footers_.at(where);
253 }
254 
255 // Odd/Even Footer
256 
has_odd_even_footer() const257 bool header_footer::has_odd_even_footer() const
258 {
259     return different_odd_even() && !even_footers_.empty();
260 }
261 
has_odd_even_footer(location where) const262 bool header_footer::has_odd_even_footer(location where) const
263 {
264     return different_odd_even() && even_footers_.count(where) > 0;
265 }
266 
clear_odd_even_footer()267 void header_footer::clear_odd_even_footer()
268 {
269     odd_footers_.clear();
270     even_footers_.clear();
271 }
272 
clear_odd_even_footer(location where)273 void header_footer::clear_odd_even_footer(location where)
274 {
275     odd_footers_.erase(where);
276     even_footers_.erase(where);
277 }
278 
odd_even_footer(location where,const rich_text & odd,const rich_text & even)279 header_footer &header_footer::odd_even_footer(location where, const rich_text &odd, const rich_text &even)
280 {
281     odd_footers_[where] = odd;
282     even_footers_[where] = even;
283     different_odd_even_ = true;
284 
285     return *this;
286 }
287 
odd_footer(location where) const288 rich_text header_footer::odd_footer(location where) const
289 {
290     return odd_footers_.at(where);
291 }
292 
even_footer(location where) const293 rich_text header_footer::even_footer(location where) const
294 {
295     return even_footers_.at(where);
296 }
297 
operator ==(const header_footer & rhs) const298 bool header_footer::operator==(const header_footer &rhs) const
299 {
300     return align_with_margins_ == rhs.align_with_margins_
301         && different_odd_even_ == rhs.different_odd_even_
302         && scale_with_doc_ == rhs.scale_with_doc_
303         && odd_headers_ == rhs.odd_headers_
304         && even_headers_ == rhs.even_headers_
305         && first_headers_ == rhs.first_headers_
306         && odd_footers_ == rhs.odd_footers_
307         && even_footers_ == rhs.even_footers_
308         && first_footers_ == rhs.first_footers_;
309 }
310 
311 } // namespace xlnt
312