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 #pragma once
26 
27 #include <cstddef>
28 #include <functional>
29 #include <unordered_map>
30 #include <vector>
31 
32 #include <xlnt/xlnt_config.hpp>
33 #include <xlnt/styles/color.hpp>
34 #include <xlnt/utils/optional.hpp>
35 
36 namespace xlnt {
37 
38 /// <summary>
39 /// Enumerates the sides of a cell to which a border style can be applied.
40 /// </summary>
41 enum class XLNT_API border_side
42 {
43     start,
44     end,
45     top,
46     bottom,
47     diagonal,
48     vertical,
49     horizontal
50 };
51 
52 /// <summary>
53 /// Enumerates the pattern of the border lines on a particular side.
54 /// </summary>
55 enum class XLNT_API border_style
56 {
57     none,
58     dashdot,
59     dashdotdot,
60     dashed,
61     dotted,
62     double_,
63     hair,
64     medium,
65     mediumdashdot,
66     mediumdashdotdot,
67     mediumdashed,
68     slantdashdot,
69     thick,
70     thin
71 };
72 
73 /// <summary>
74 /// Cells can have borders that go from the top-left to bottom-right
75 /// or from the top-right to bottom-left, or both, or neither.
76 /// Used by style->border.
77 /// </summary>
78 enum class XLNT_API diagonal_direction
79 {
80     neither,
81     up,
82     down,
83     both
84 };
85 
86 } // namespace xlnt
87 
88 namespace xlnt {
89 
90 /// <summary>
91 /// Describes the border style of a particular cell.
92 /// </summary>
93 class XLNT_API border
94 {
95 public:
96     /// <summary>
97     /// Each side of a cell can have a border_property applied to it to change
98     /// how it is displayed.
99     /// </summary>
100     class XLNT_API border_property
101     {
102     public:
103         /// <summary>
104         /// Returns the color of the side.
105         /// </summary>
106         optional<class color> color() const;
107 
108         /// <summary>
109         /// Sets the color of the side and returns a reference to the side properties.
110         /// </summary>
111         border_property &color(const xlnt::color &c);
112 
113         /// <summary>
114         /// Returns the style of the side.
115         /// </summary>
116         optional<border_style> style() const;
117 
118         /// <summary>
119         /// Sets the style of the side and returns a reference to the side properties.
120         /// </summary>
121         border_property &style(border_style style);
122 
123         /// <summary>
124         /// Returns true if left is exactly equal to right.
125         /// </summary>
126         bool operator==(const border_property &right) const;
127 
128         /// <summary>
129         /// Returns true if left is not exactly equal to right.
130         /// </summary>
131         bool operator!=(const border_property &right) const;
132 
133     private:
134         /// <summary>
135         /// The color of the side
136         /// </summary>
137         optional<class color> color_;
138 
139         /// <summary>
140         /// The style of the side
141         /// </summary>
142         optional<border_style> style_;
143     };
144 
145     /// <summary>
146     /// Returns a vector containing all of the border sides to be used for iteration.
147     /// </summary>
148     static const std::vector<border_side> &all_sides();
149 
150     /// <summary>
151     /// Constructs a default border.
152     /// </summary>
153     border();
154 
155     /// <summary>
156     /// Returns the border properties of the given side.
157     /// </summary>
158     optional<border_property> side(border_side s) const;
159 
160     /// <summary>
161     /// Sets the border properties of the side s to prop.
162     /// </summary>
163     border &side(border_side s, const border_property &prop);
164 
165     /// <summary>
166     /// Returns the diagonal direction of this border.
167     /// </summary>
168     optional<diagonal_direction> diagonal() const;
169 
170     /// <summary>
171     /// Sets the diagonal direction of this border to dir.
172     /// </summary>
173     border &diagonal(diagonal_direction dir);
174 
175     /// <summary>
176     /// Returns true if left is exactly equal to right.
177     /// </summary>
178     bool operator==(const border &right) const;
179 
180     /// <summary>
181     /// Returns true if left is not exactly equal to right.
182     /// </summary>
183     bool operator!=(const border &right) const;
184 
185 private:
186     /// <summary>
187     /// Start side (i.e. left) border properties
188     /// </summary>
189     optional<border_property> start_;
190 
191     /// <summary>
192     /// End side (i.e. right) border properties
193     /// </summary>
194     optional<border_property> end_;
195 
196     /// <summary>
197     /// Top side border properties
198     /// </summary>
199     optional<border_property> top_;
200 
201     /// <summary>
202     /// Bottom side border properties
203     /// </summary>
204     optional<border_property> bottom_;
205 
206     /// <summary>
207     /// Vertical border properties
208     /// </summary>
209     optional<border_property> vertical_;
210 
211     /// <summary>
212     /// Horizontal border properties
213     /// </summary>
214     optional<border_property> horizontal_;
215 
216     /// <summary>
217     /// Diagonal border properties
218     /// </summary>
219     optional<border_property> diagonal_;
220 
221     /// <summary>
222     /// Direction of diagonal border properties to be applied
223     /// </summary>
224     optional<diagonal_direction> diagonal_direction_;
225 };
226 
227 } // namespace xlnt
228