PropertyDefinition.cpp 5.6 KB

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