phonetic_pr.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <xlnt/worksheet/phonetic_pr.hpp>
  24. #include <array>
  25. namespace {
  26. // Order of elements defined by phonetic_pr::Type enum
  27. const std::array<std::string, 4> &Types()
  28. {
  29. static const std::array<std::string, 4> types{
  30. std::string("fullwidthKatakana"),
  31. std::string("halfwidthKatakana"),
  32. std::string("Hiragana"),
  33. std::string("noConversion")};
  34. return types;
  35. }
  36. // Order of elements defined by phonetic_pr::alignment enum
  37. const std::array<std::string, 4> &Alignments()
  38. {
  39. static const std::array<std::string, 4> alignments{
  40. std::string("Center"),
  41. std::string("Distributed"),
  42. std::string("Left"),
  43. std::string("NoControl")};
  44. return alignments;
  45. }
  46. } // namespace
  47. namespace xlnt {
  48. /// <summary>
  49. /// out of line initialiser for static const member
  50. /// </summary>
  51. std::string phonetic_pr::Serialised_ID()
  52. {
  53. return "phoneticPr";
  54. }
  55. phonetic_pr::phonetic_pr(font_id_t font)
  56. : font_id_(font)
  57. {
  58. }
  59. void phonetic_pr::serialise(std::ostream &output_stream) const
  60. {
  61. output_stream << '<' << Serialised_ID() << R"( fontID=")" << std::to_string(font_id_) << '"';
  62. if (has_type())
  63. {
  64. output_stream << R"( type=")" << type_as_string(type_.get()) << '"';
  65. }
  66. if (has_alignment())
  67. {
  68. output_stream << R"( alignment=")" << alignment_as_string(alignment_.get()) << '"';
  69. }
  70. output_stream << "/>";
  71. }
  72. phonetic_pr::font_id_t phonetic_pr::font_id() const
  73. {
  74. return font_id_;
  75. }
  76. void phonetic_pr::font_id(font_id_t font)
  77. {
  78. font_id_ = font;
  79. }
  80. bool phonetic_pr::has_type() const
  81. {
  82. return type_.is_set();
  83. }
  84. phonetic_pr::phonetic_type phonetic_pr::type() const
  85. {
  86. return type_.get();
  87. }
  88. void phonetic_pr::type(phonetic_type type)
  89. {
  90. type_.set(type);
  91. }
  92. bool phonetic_pr::has_alignment() const
  93. {
  94. return alignment_.is_set();
  95. }
  96. phonetic_pr::align phonetic_pr::alignment() const
  97. {
  98. return alignment_.get();
  99. }
  100. void phonetic_pr::alignment(align align)
  101. {
  102. alignment_.set(align);
  103. }
  104. // serialisation
  105. const std::string &phonetic_pr::type_as_string(phonetic_pr::phonetic_type type)
  106. {
  107. return Types()[static_cast<std::size_t>(type)];
  108. }
  109. phonetic_pr::phonetic_type phonetic_pr::type_from_string(const std::string &str)
  110. {
  111. for (std::size_t i = 0; i < Types().size(); ++i)
  112. {
  113. if (str == Types()[i])
  114. {
  115. return static_cast<phonetic_type>(i);
  116. }
  117. }
  118. return phonetic_type::no_conversion;
  119. }
  120. const std::string &phonetic_pr::alignment_as_string(align type)
  121. {
  122. return Alignments()[static_cast<std::size_t>(type)];
  123. }
  124. phonetic_pr::align phonetic_pr::alignment_from_string(const std::string &str)
  125. {
  126. for (std::size_t i = 0; i < Alignments().size(); ++i)
  127. {
  128. if (str == Alignments()[i])
  129. {
  130. return static_cast<align>(i);
  131. }
  132. }
  133. return align::no_control;
  134. }
  135. bool phonetic_pr::operator==(const phonetic_pr &rhs) const
  136. {
  137. return font_id_ == rhs.font_id_
  138. && type_ == rhs.type_
  139. && alignment_ == rhs.alignment_;
  140. }
  141. } // namespace xlnt