PropertyDefinition.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 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 "precompiled.h"
  29. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  30. #include "../../Include/RmlUi/Core/Log.h"
  31. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  32. namespace Rml {
  33. namespace Core {
  34. PropertyDefinition::PropertyDefinition(PropertyId id, const String& _default_value, bool _inherited, bool _forces_layout)
  35. : id(id), default_value(_default_value, Property::UNKNOWN), relative_target(RelativeTarget::None)
  36. {
  37. inherited = _inherited;
  38. forces_layout = _forces_layout;
  39. default_value.definition = this;
  40. }
  41. PropertyDefinition::~PropertyDefinition()
  42. {
  43. }
  44. // Registers a parser to parse values for this definition.
  45. PropertyDefinition& PropertyDefinition::AddParser(const String& parser_name, const String& parser_parameters)
  46. {
  47. ParserState new_parser;
  48. // Fetch the parser.
  49. new_parser.parser = StyleSheetSpecification::GetParser(parser_name);
  50. if (new_parser.parser == nullptr)
  51. {
  52. Log::Message(Log::LT_ERROR, "Property was registered with invalid parser '%s'.", parser_name.c_str());
  53. return *this;
  54. }
  55. // Split the parameter list, and set up the map.
  56. if (!parser_parameters.empty())
  57. {
  58. StringList parameter_list;
  59. StringUtilities::ExpandString(parameter_list, parser_parameters);
  60. for (size_t i = 0; i < parameter_list.size(); i++)
  61. new_parser.parameters[parameter_list[i]] = (int) i;
  62. }
  63. const int parser_index = (int)parsers.size();
  64. parsers.push_back(new_parser);
  65. // If the default value has not been parsed successfully yet, run it through the new parser.
  66. if (default_value.unit == Property::UNKNOWN)
  67. {
  68. String unparsed_value = default_value.value.Get< String >();
  69. if (new_parser.parser->ParseValue(default_value, unparsed_value, new_parser.parameters))
  70. {
  71. default_value.parser_index = parser_index;
  72. }
  73. else
  74. {
  75. default_value.value = unparsed_value;
  76. default_value.unit = Property::UNKNOWN;
  77. }
  78. }
  79. return *this;
  80. }
  81. // Called when parsing a RCSS declaration.
  82. bool PropertyDefinition::ParseValue(Property& property, const String& value) const
  83. {
  84. for (size_t i = 0; i < parsers.size(); i++)
  85. {
  86. if (parsers[i].parser->ParseValue(property, value, parsers[i].parameters))
  87. {
  88. property.definition = this;
  89. property.parser_index = (int) i;
  90. return true;
  91. }
  92. }
  93. property.unit = Property::UNKNOWN;
  94. return false;
  95. }
  96. // Called to convert a parsed property back into a value.
  97. bool PropertyDefinition::GetValue(String& value, const Property& property) const
  98. {
  99. value = property.value.Get< String >();
  100. switch (property.unit)
  101. {
  102. case Property::KEYWORD:
  103. {
  104. int parser_index = property.parser_index;
  105. if (parser_index < 0 || parser_index >= (int)parsers.size())
  106. {
  107. // Look for the keyword parser in the property's list of parsers
  108. const auto* keyword_parser = StyleSheetSpecification::GetParser("keyword");
  109. for(int i = 0; i < (int)parsers.size(); i++)
  110. {
  111. if (parsers[i].parser == keyword_parser)
  112. {
  113. parser_index = i;
  114. break;
  115. }
  116. }
  117. // If we couldn't find it, exit now
  118. if (parser_index < 0 || parser_index >= (int)parsers.size())
  119. return false;
  120. }
  121. int keyword = property.value.Get< int >();
  122. for (ParameterMap::const_iterator i = parsers[parser_index].parameters.begin(); i != parsers[parser_index].parameters.end(); ++i)
  123. {
  124. if ((*i).second == keyword)
  125. {
  126. value = (*i).first;
  127. break;
  128. }
  129. }
  130. return false;
  131. }
  132. break;
  133. case Property::COLOUR:
  134. {
  135. Colourb colour = property.value.Get< Colourb >();
  136. value = CreateString(32, "rgba(%d,%d,%d,%d)", colour.red, colour.green, colour.blue, colour.alpha);
  137. }
  138. break;
  139. case Property::PX: value += "px"; break;
  140. case Property::DEG: value += "deg"; break;
  141. case Property::RAD: value += "rad"; break;
  142. case Property::DP: value += "dp"; break;
  143. case Property::EM: value += "em"; break;
  144. case Property::REM: value += "rem"; break;
  145. case Property::PERCENT: value += "%"; break;
  146. case Property::INCH: value += "in"; break;
  147. case Property::CM: value += "cm"; break;
  148. case Property::MM: value += "mm"; break;
  149. case Property::PT: value += "pt"; break;
  150. case Property::PC: value += "pc"; break;
  151. default: break;
  152. }
  153. return true;
  154. }
  155. // Returns true if this property is inherited from a parent to child elements.
  156. bool PropertyDefinition::IsInherited() const
  157. {
  158. return inherited;
  159. }
  160. // Returns true if this property forces a re-layout when changed.
  161. bool PropertyDefinition::IsLayoutForced() const
  162. {
  163. return forces_layout;
  164. }
  165. // Returns the default for this property.
  166. const Property* PropertyDefinition::GetDefaultValue() const
  167. {
  168. return &default_value;
  169. }
  170. /// Returns the default defined for this property.
  171. RelativeTarget PropertyDefinition::GetRelativeTarget() const
  172. {
  173. return relative_target;
  174. }
  175. PropertyId PropertyDefinition::GetId() const
  176. {
  177. return id;
  178. }
  179. /// Set target for units with scaling percentages
  180. PropertyDefinition & PropertyDefinition::SetRelativeTarget(RelativeTarget relative_target)
  181. {
  182. this->relative_target = relative_target;
  183. return *this;
  184. }
  185. }
  186. }