border.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/border.hpp>
  25. #include <xlnt/utils/exceptions.hpp>
  26. #include <detail/default_case.hpp>
  27. namespace xlnt {
  28. optional<xlnt::color> border::border_property::color() const
  29. {
  30. return color_;
  31. }
  32. border::border_property &border::border_property::color(const xlnt::color &c)
  33. {
  34. color_ = c;
  35. return *this;
  36. }
  37. optional<border_style> border::border_property::style() const
  38. {
  39. return style_;
  40. }
  41. border::border_property &border::border_property::style(border_style s)
  42. {
  43. style_ = s;
  44. return *this;
  45. }
  46. bool border::border_property::operator==(const border::border_property &right) const
  47. {
  48. auto &left = *this;
  49. if (left.style().is_set() != right.style().is_set())
  50. {
  51. return false;
  52. }
  53. if (left.style().is_set())
  54. {
  55. if (left.style().get() != right.style().get())
  56. {
  57. return false;
  58. }
  59. }
  60. if (left.color().is_set() != right.color().is_set())
  61. {
  62. return false;
  63. }
  64. if (left.color().is_set())
  65. {
  66. if (left.color().get() != right.color().get())
  67. {
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. bool border::border_property::operator!=(const border::border_property &right) const
  74. {
  75. return !(*this == right);
  76. }
  77. border::border()
  78. {
  79. }
  80. const std::vector<xlnt::border_side> &border::all_sides()
  81. {
  82. static auto sides = std::vector<xlnt::border_side>{xlnt::border_side::start, xlnt::border_side::end,
  83. xlnt::border_side::top, xlnt::border_side::bottom, xlnt::border_side::diagonal, xlnt::border_side::vertical,
  84. xlnt::border_side::horizontal};
  85. return sides;
  86. }
  87. optional<border::border_property> border::side(border_side s) const
  88. {
  89. switch (s)
  90. {
  91. case border_side::bottom:
  92. return bottom_;
  93. case border_side::top:
  94. return top_;
  95. case border_side::start:
  96. return start_;
  97. case border_side::end:
  98. return end_;
  99. case border_side::vertical:
  100. return vertical_;
  101. case border_side::horizontal:
  102. return horizontal_;
  103. case border_side::diagonal:
  104. return diagonal_;
  105. }
  106. default_case(start_);
  107. }
  108. border &border::side(border_side s, const border_property &prop)
  109. {
  110. switch (s)
  111. {
  112. case border_side::bottom:
  113. bottom_ = prop;
  114. break;
  115. case border_side::top:
  116. top_ = prop;
  117. break;
  118. case border_side::start:
  119. start_ = prop;
  120. break;
  121. case border_side::end:
  122. end_ = prop;
  123. break;
  124. case border_side::vertical:
  125. vertical_ = prop;
  126. break;
  127. case border_side::horizontal:
  128. horizontal_ = prop;
  129. break;
  130. case border_side::diagonal:
  131. diagonal_ = prop;
  132. break;
  133. }
  134. return *this;
  135. }
  136. border &border::diagonal(diagonal_direction direction)
  137. {
  138. diagonal_direction_ = direction;
  139. return *this;
  140. }
  141. optional<diagonal_direction> border::diagonal() const
  142. {
  143. return diagonal_direction_;
  144. }
  145. bool border::operator==(const border &right) const
  146. {
  147. auto &left = *this;
  148. for (auto side : border::all_sides())
  149. {
  150. if (left.side(side).is_set() != right.side(side).is_set())
  151. {
  152. return false;
  153. }
  154. if (left.side(side).is_set())
  155. {
  156. if (left.side(side).get() != right.side(side).get())
  157. {
  158. return false;
  159. }
  160. }
  161. }
  162. return true;
  163. }
  164. bool border::operator!=(const border &right) const
  165. {
  166. return !(*this == right);
  167. }
  168. } // namespace xlnt