PropertyParserDecorator.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "PropertyParserDecorator.h"
  29. #include "../../Include/RmlUi/Core/Decorator.h"
  30. #include "../../Include/RmlUi/Core/Factory.h"
  31. #include "../../Include/RmlUi/Core/Profiling.h"
  32. #include "../../Include/RmlUi/Core/PropertySpecification.h"
  33. #include "../../Include/RmlUi/Core/StyleSheetTypes.h"
  34. namespace Rml {
  35. struct PropertyParserDecoratorData {
  36. const SmallUnorderedMap<String, BoxArea> area_keywords = {
  37. {"border-box", BoxArea::Border},
  38. {"padding-box", BoxArea::Padding},
  39. {"content-box", BoxArea::Content},
  40. };
  41. };
  42. ControlledLifetimeResource<PropertyParserDecoratorData> PropertyParserDecorator::parser_data;
  43. void PropertyParserDecorator::Initialize()
  44. {
  45. parser_data.Initialize();
  46. }
  47. void PropertyParserDecorator::Shutdown()
  48. {
  49. parser_data.Shutdown();
  50. }
  51. PropertyParserDecorator::PropertyParserDecorator() {}
  52. PropertyParserDecorator::~PropertyParserDecorator() {}
  53. bool PropertyParserDecorator::ParseValue(Property& property, const String& decorator_string_value, const ParameterMap& /*parameters*/) const
  54. {
  55. // Decorators are declared as
  56. // decorator: <decorator-value>[, <decorator-value> ...];
  57. // Where <decorator-value> is either a @decorator name:
  58. // decorator: invader-theme-background <paint-area>?, ...;
  59. // or is an anonymous decorator with inline properties
  60. // decorator: tiled-box( <shorthand properties> ) <paint-area>?, ...;
  61. // where <paint-area> is one of
  62. // border-box, padding-box, content-box
  63. if (decorator_string_value.empty() || decorator_string_value == "none")
  64. {
  65. property.value = Variant(DecoratorsPtr());
  66. property.unit = Unit::DECORATOR;
  67. return true;
  68. }
  69. RMLUI_ZoneScoped;
  70. // Make sure we don't split inside the parenthesis since they may appear in decorator shorthands.
  71. StringList decorator_string_list;
  72. StringUtilities::ExpandString(decorator_string_list, decorator_string_value, ',', '(', ')');
  73. DecoratorDeclarationList decorators;
  74. decorators.value = decorator_string_value;
  75. decorators.list.reserve(decorator_string_list.size());
  76. // Get or instance each decorator in the comma-separated string list
  77. for (const String& decorator_string : decorator_string_list)
  78. {
  79. const size_t shorthand_open = decorator_string.find('(');
  80. const size_t shorthand_close = decorator_string.rfind(')');
  81. const bool invalid_parenthesis = (shorthand_open == String::npos || shorthand_close == String::npos || shorthand_open >= shorthand_close);
  82. const size_t keywords_begin = (invalid_parenthesis ? decorator_string.find(' ') : shorthand_close + 1);
  83. // Look-up keywords for customized paint area.
  84. BoxArea paint_area = BoxArea::Auto;
  85. {
  86. StringList keywords;
  87. if (keywords_begin < decorator_string.size())
  88. StringUtilities::ExpandString(keywords, decorator_string.substr(keywords_begin), ' ');
  89. for (const String& keyword : keywords)
  90. {
  91. if (keyword.empty())
  92. continue;
  93. auto it = parser_data->area_keywords.find(StringUtilities::ToLower(keyword));
  94. if (it == parser_data->area_keywords.end())
  95. return false; // Bail out if we have an invalid keyword.
  96. paint_area = it->second;
  97. }
  98. }
  99. if (invalid_parenthesis)
  100. {
  101. // We found no parenthesis, that means the value must be a name of a @decorator rule.
  102. decorators.list.emplace_back(DecoratorDeclaration{decorator_string.substr(0, keywords_begin), nullptr, {}, paint_area});
  103. }
  104. else
  105. {
  106. // Since we have parentheses it must be an anonymous decorator with inline properties
  107. const String type = StringUtilities::StripWhitespace(decorator_string.substr(0, shorthand_open));
  108. // Check for valid decorator type
  109. DecoratorInstancer* instancer = Factory::GetDecoratorInstancer(type);
  110. if (!instancer)
  111. {
  112. Log::Message(Log::LT_WARNING, "Decorator type '%s' not found.", type.c_str());
  113. return false;
  114. }
  115. const String shorthand = decorator_string.substr(shorthand_open + 1, shorthand_close - shorthand_open - 1);
  116. const PropertySpecification& specification = instancer->GetPropertySpecification();
  117. // Parse the shorthand properties given by the 'decorator' shorthand property
  118. PropertyDictionary properties;
  119. if (!specification.ParsePropertyDeclaration(properties, "decorator", shorthand))
  120. {
  121. // Empty values are allowed in decorators, if the value is not empty we must have encountered a parser error.
  122. if (!StringUtilities::StripWhitespace(shorthand).empty())
  123. return false;
  124. }
  125. // Set unspecified values to their defaults
  126. specification.SetPropertyDefaults(properties);
  127. decorators.list.emplace_back(DecoratorDeclaration{type, instancer, std::move(properties), paint_area});
  128. }
  129. }
  130. if (decorators.list.empty())
  131. return false;
  132. property.value = Variant(MakeShared<DecoratorDeclarationList>(std::move(decorators)));
  133. property.unit = Unit::DECORATOR;
  134. return true;
  135. }
  136. String PropertyParserDecorator::ConvertAreaToString(BoxArea area)
  137. {
  138. for (const auto& it : parser_data->area_keywords)
  139. {
  140. if (it.second == area)
  141. return it.first;
  142. }
  143. return String();
  144. }
  145. } // namespace Rml