PropertyParserDecorator.cpp 6.0 KB

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