font.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <cmath>
  25. #include <xlnt/styles/font.hpp>
  26. namespace {
  27. const std::string &Default_Name()
  28. {
  29. static const std::string Default("Calibri");
  30. return Default;
  31. }
  32. constexpr double Default_Size = 12.0;
  33. } // namespace
  34. namespace xlnt {
  35. font::font()
  36. {
  37. }
  38. font &font::bold(bool bold)
  39. {
  40. bold_ = bold;
  41. return *this;
  42. }
  43. bool font::bold() const
  44. {
  45. return bold_;
  46. }
  47. font &font::superscript(bool value)
  48. {
  49. superscript_ = value;
  50. return *this;
  51. }
  52. bool font::superscript() const
  53. {
  54. return superscript_;
  55. }
  56. font &font::subscript(bool value)
  57. {
  58. subscript_ = value;
  59. return *this;
  60. }
  61. bool font::subscript() const
  62. {
  63. return subscript_;
  64. }
  65. font &font::italic(bool italic)
  66. {
  67. italic_ = italic;
  68. return *this;
  69. }
  70. bool font::italic() const
  71. {
  72. return italic_;
  73. }
  74. font &font::strikethrough(bool strikethrough)
  75. {
  76. strikethrough_ = strikethrough;
  77. return *this;
  78. }
  79. bool font::strikethrough() const
  80. {
  81. return strikethrough_;
  82. }
  83. font &font::outline(bool outline)
  84. {
  85. outline_ = outline;
  86. return *this;
  87. }
  88. bool font::outline() const
  89. {
  90. return outline_;
  91. }
  92. font &font::shadow(bool shadow)
  93. {
  94. shadow_ = shadow;
  95. return *this;
  96. }
  97. bool font::shadow() const
  98. {
  99. return shadow_;
  100. }
  101. font &font::underline(underline_style new_underline)
  102. {
  103. underline_ = new_underline;
  104. return *this;
  105. }
  106. bool font::underlined() const
  107. {
  108. return underline_ != underline_style::none;
  109. }
  110. font::underline_style font::underline() const
  111. {
  112. return underline_;
  113. }
  114. bool font::has_size() const
  115. {
  116. return size_.is_set();
  117. }
  118. font &font::size(double size)
  119. {
  120. size_ = size;
  121. return *this;
  122. }
  123. double font::size() const
  124. {
  125. if (size_.is_set())
  126. {
  127. return size_.get();
  128. }
  129. return Default_Size;
  130. }
  131. bool font::has_name() const
  132. {
  133. return name_.is_set();
  134. }
  135. font &font::name(const std::string &name)
  136. {
  137. name_ = name;
  138. return *this;
  139. }
  140. const std::string &font::name() const
  141. {
  142. if (name_.is_set())
  143. {
  144. return name_.get();
  145. }
  146. return Default_Name();
  147. }
  148. bool font::has_color() const
  149. {
  150. return color_.is_set();
  151. }
  152. font &font::color(const xlnt::color &c)
  153. {
  154. color_ = c;
  155. return *this;
  156. }
  157. bool font::has_family() const
  158. {
  159. return family_.is_set();
  160. }
  161. font &font::family(std::size_t family)
  162. {
  163. family_ = family;
  164. return *this;
  165. }
  166. bool font::has_charset() const
  167. {
  168. return charset_.is_set();
  169. }
  170. font &font::charset(std::size_t charset)
  171. {
  172. charset_ = charset;
  173. return *this;
  174. }
  175. std::size_t font::charset() const
  176. {
  177. return charset_.get();
  178. }
  179. bool font::has_scheme() const
  180. {
  181. return scheme_.is_set();
  182. }
  183. font &font::scheme(const std::string &scheme)
  184. {
  185. scheme_ = scheme;
  186. return *this;
  187. }
  188. color font::color() const
  189. {
  190. return color_.get();
  191. }
  192. std::size_t font::family() const
  193. {
  194. return family_.get();
  195. }
  196. const std::string &font::scheme() const
  197. {
  198. return scheme_.get();
  199. }
  200. bool font::operator==(const font &other) const
  201. {
  202. // name
  203. if (has_name() != other.has_name()) return false;
  204. if (has_name() && name() != other.name()) return false;
  205. // size
  206. if (has_size() != other.has_size()) return false;
  207. if (has_size() && std::fabs(size() - other.size()) != 0.0) return false;
  208. // family
  209. if (has_family() != other.has_family()) return false;
  210. if (has_family() && family() != other.family()) return false;
  211. // scheme
  212. if (has_scheme() != other.has_scheme()) return false;
  213. if (has_scheme() && scheme() != other.scheme()) return false;
  214. // color
  215. if (has_color() != other.has_color()) return false;
  216. if (has_color() && color() != other.color()) return false;
  217. // charset
  218. if (has_charset() != other.has_charset()) return false;
  219. if (has_charset() && charset() != other.charset()) return false;
  220. // modifiers
  221. if (bold() != other.bold()) return false;
  222. if (italic() != other.italic()) return false;
  223. if (strikethrough() != other.strikethrough()) return false;
  224. if (superscript() != other.superscript()) return false;
  225. if (subscript() != other.subscript()) return false;
  226. if (underline() != other.underline()) return false;
  227. if (shadow() != other.shadow()) return false;
  228. return true;
  229. }
  230. } // namespace xlnt