PropertyDefinition.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  29. #include "../../Include/RmlUi/Core/Log.h"
  30. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  31. namespace Rml {
  32. PropertyDefinition::PropertyDefinition(PropertyId id, const String& _default_value, bool _inherited, bool _forces_layout) :
  33. id(id), default_value(_default_value, Unit::UNKNOWN), relative_target(RelativeTarget::None)
  34. {
  35. inherited = _inherited;
  36. forces_layout = _forces_layout;
  37. default_value.definition = this;
  38. }
  39. PropertyDefinition::~PropertyDefinition() {}
  40. PropertyDefinition& PropertyDefinition::AddParser(const String& parser_name, const String& parser_parameters)
  41. {
  42. ParserState new_parser;
  43. // Fetch the parser.
  44. new_parser.parser = StyleSheetSpecification::GetParser(parser_name);
  45. if (new_parser.parser == nullptr)
  46. {
  47. Log::Message(Log::LT_ERROR, "Property was registered with invalid parser '%s'.", parser_name.c_str());
  48. return *this;
  49. }
  50. // Split the parameter list, and set up the map.
  51. if (!parser_parameters.empty())
  52. {
  53. StringList parameter_list;
  54. StringUtilities::ExpandString(parameter_list, parser_parameters);
  55. int parameter_value = 0;
  56. for (const String& parameter : parameter_list)
  57. {
  58. // Look for an optional parameter value such as in "normal=400".
  59. const size_t i_equal = parameter.find('=');
  60. if (i_equal != String::npos)
  61. {
  62. if (!TypeConverter<String, int>::Convert(parameter.substr(i_equal + 1), parameter_value))
  63. {
  64. Log::Message(Log::LT_ERROR, "Parser was added with invalid parameter '%s'.", parameter.c_str());
  65. return *this;
  66. }
  67. }
  68. new_parser.parameters[parameter.substr(0, i_equal)] = parameter_value;
  69. parameter_value += 1;
  70. }
  71. }
  72. const int parser_index = (int)parsers.size();
  73. parsers.push_back(new_parser);
  74. // If the default value has not been parsed successfully yet, run it through the new parser.
  75. if (default_value.unit == Unit::UNKNOWN)
  76. {
  77. String unparsed_value = default_value.value.Get<String>();
  78. if (new_parser.parser->ParseValue(default_value, unparsed_value, new_parser.parameters))
  79. {
  80. default_value.parser_index = parser_index;
  81. }
  82. else
  83. {
  84. default_value.value = unparsed_value;
  85. default_value.unit = Unit::UNKNOWN;
  86. }
  87. }
  88. return *this;
  89. }
  90. bool PropertyDefinition::ParseValue(Property& property, const String& value) const
  91. {
  92. for (size_t i = 0; i < parsers.size(); i++)
  93. {
  94. if (parsers[i].parser->ParseValue(property, value, parsers[i].parameters))
  95. {
  96. property.definition = this;
  97. property.parser_index = (int)i;
  98. return true;
  99. }
  100. }
  101. property.unit = Unit::UNKNOWN;
  102. return false;
  103. }
  104. bool PropertyDefinition::GetValue(String& value, const Property& property) const
  105. {
  106. value = property.value.Get<String>();
  107. switch (property.unit)
  108. {
  109. case Unit::KEYWORD:
  110. {
  111. int parser_index = property.parser_index;
  112. if (parser_index < 0 || parser_index >= (int)parsers.size())
  113. {
  114. // Look for the keyword parser in the property's list of parsers
  115. const auto* keyword_parser = StyleSheetSpecification::GetParser("keyword");
  116. for (int i = 0; i < (int)parsers.size(); i++)
  117. {
  118. if (parsers[i].parser == keyword_parser)
  119. {
  120. parser_index = i;
  121. break;
  122. }
  123. }
  124. // If we couldn't find it, exit now
  125. if (parser_index < 0 || parser_index >= (int)parsers.size())
  126. return false;
  127. }
  128. int keyword = property.value.Get<int>();
  129. for (const auto& name_keyword : parsers[parser_index].parameters)
  130. {
  131. if (name_keyword.second == keyword)
  132. {
  133. value = name_keyword.first;
  134. break;
  135. }
  136. }
  137. return false;
  138. }
  139. break;
  140. default: value += ToString(property.unit); break;
  141. }
  142. return true;
  143. }
  144. bool PropertyDefinition::IsInherited() const
  145. {
  146. return inherited;
  147. }
  148. bool PropertyDefinition::IsLayoutForced() const
  149. {
  150. return forces_layout;
  151. }
  152. const Property* PropertyDefinition::GetDefaultValue() const
  153. {
  154. return &default_value;
  155. }
  156. RelativeTarget PropertyDefinition::GetRelativeTarget() const
  157. {
  158. return relative_target;
  159. }
  160. PropertyId PropertyDefinition::GetId() const
  161. {
  162. return id;
  163. }
  164. PropertyDefinition& PropertyDefinition::SetRelativeTarget(RelativeTarget relative_target)
  165. {
  166. this->relative_target = relative_target;
  167. return *this;
  168. }
  169. } // namespace Rml