PropertyDefinition.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  29. #include "../../Include/Rocket/Core/Log.h"
  30. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  31. namespace Rocket {
  32. namespace Core {
  33. PropertyDefinition::PropertyDefinition(const String& _default_value, bool _inherited, bool _forces_layout) : default_value(_default_value, Property::UNKNOWN)
  34. {
  35. inherited = _inherited;
  36. forces_layout = _forces_layout;
  37. }
  38. PropertyDefinition::~PropertyDefinition()
  39. {
  40. }
  41. // Registers a parser to parse values for this definition.
  42. PropertyDefinition& PropertyDefinition::AddParser(const String& parser_name, const String& parser_parameters)
  43. {
  44. ParserState new_parser;
  45. // Fetch the parser.
  46. new_parser.parser = StyleSheetSpecification::GetParser(parser_name);
  47. if (new_parser.parser == NULL)
  48. {
  49. Log::Message(Log::LT_ERROR, "Property was registered with invalid parser '%s'.", parser_name.CString());
  50. return *this;
  51. }
  52. // Split the parameter list, and set up the map.
  53. if (!parser_parameters.Empty())
  54. {
  55. StringList parameter_list;
  56. StringUtilities::ExpandString(parameter_list, parser_parameters);
  57. for (size_t i = 0; i < parameter_list.size(); i++)
  58. new_parser.parameters[parameter_list[i]] = (int) i;
  59. }
  60. parsers.push_back(new_parser);
  61. // If the default value has not been parsed successfully yet, run it through the new parser.
  62. if (default_value.unit == Property::UNKNOWN)
  63. {
  64. String unparsed_value = default_value.value.Get< String >();
  65. if (!new_parser.parser->ParseValue(default_value, unparsed_value, new_parser.parameters))
  66. {
  67. default_value.value.Set(unparsed_value);
  68. default_value.unit = Property::UNKNOWN;
  69. }
  70. }
  71. return *this;
  72. }
  73. // Called when parsing a RCSS declaration.
  74. bool PropertyDefinition::ParseValue(Property& property, const String& value) const
  75. {
  76. for (size_t i = 0; i < parsers.size(); i++)
  77. {
  78. if (parsers[i].parser->ParseValue(property, value, parsers[i].parameters))
  79. {
  80. property.definition = this;
  81. property.parser_index = (int) i;
  82. return true;
  83. }
  84. }
  85. property.unit = Property::UNKNOWN;
  86. return false;
  87. }
  88. // Called to convert a parsed property back into a value.
  89. bool PropertyDefinition::GetValue(String& value, const Property& property) const
  90. {
  91. value = property.value.Get< String >();
  92. switch (property.unit)
  93. {
  94. case Property::KEYWORD:
  95. {
  96. if (property.parser_index < 0 || property.parser_index >= (int) parsers.size())
  97. return false;
  98. int keyword = property.value.Get< int >();
  99. for (ParameterMap::const_iterator i = parsers[property.parser_index].parameters.begin(); i != parsers[property.parser_index].parameters.end(); ++i)
  100. {
  101. if ((*i).second == keyword)
  102. {
  103. value = (*i).first;
  104. break;
  105. }
  106. }
  107. return false;
  108. }
  109. break;
  110. case Property::COLOUR:
  111. {
  112. Colourb colour = property.value.Get< Colourb >();
  113. value.FormatString(32, "rgb(%d,%d,%d,%d)", colour.red, colour.green, colour.blue, colour.alpha);
  114. }
  115. break;
  116. case Property::PX: value.Append("px"); break;
  117. case Property::EM: value.Append("em"); break;
  118. case Property::REM: value.Append("rem"); break;
  119. case Property::PERCENT: value.Append("%"); break;
  120. case Property::INCH: value.Append("in"); break;
  121. case Property::CM: value.Append("cm"); break;
  122. case Property::MM: value.Append("mm"); break;
  123. case Property::PT: value.Append("pt"); break;
  124. case Property::PC: value.Append("pc"); break;
  125. default: break;
  126. }
  127. return true;
  128. }
  129. // Returns true if this property is inherited from a parent to child elements.
  130. bool PropertyDefinition::IsInherited() const
  131. {
  132. return inherited;
  133. }
  134. // Returns true if this property forces a re-layout when changed.
  135. bool PropertyDefinition::IsLayoutForced() const
  136. {
  137. return forces_layout;
  138. }
  139. // Returns the default for this property.
  140. const Property* PropertyDefinition::GetDefaultValue() const
  141. {
  142. return &default_value;
  143. }
  144. }
  145. }