PropertyParserDecorator.cpp 5.9 KB

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