PropertyParserDecorator.cpp 4.9 KB

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