PropertyParserBoxShadow.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "PropertyParserBoxShadow.h"
  2. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  3. #include "../../Include/RmlUi/Core/StringUtilities.h"
  4. namespace Rml {
  5. PropertyParserBoxShadow::PropertyParserBoxShadow(PropertyParser* parser_color, PropertyParser* parser_length) :
  6. parser_color(parser_color), parser_length(parser_length)
  7. {
  8. RMLUI_ASSERT(parser_color && parser_length);
  9. }
  10. bool PropertyParserBoxShadow::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  11. {
  12. if (value.empty() || value == "none")
  13. {
  14. property.value = Variant();
  15. property.unit = Unit::UNKNOWN;
  16. return true;
  17. }
  18. StringList shadows_string_list;
  19. {
  20. auto lowercase_value = StringUtilities::ToLower(value);
  21. StringUtilities::ExpandString(shadows_string_list, lowercase_value, ',', '(', ')');
  22. }
  23. if (shadows_string_list.empty())
  24. return false;
  25. const ParameterMap empty_parameter_map;
  26. BoxShadowList shadow_list;
  27. shadow_list.reserve(shadows_string_list.size());
  28. for (const String& shadow_str : shadows_string_list)
  29. {
  30. StringList arguments;
  31. StringUtilities::ExpandString(arguments, shadow_str, ' ', '(', ')');
  32. if (arguments.empty())
  33. return false;
  34. shadow_list.push_back({});
  35. BoxShadow& shadow = shadow_list.back();
  36. int length_argument_index = 0;
  37. for (const String& argument : arguments)
  38. {
  39. if (argument.empty())
  40. continue;
  41. Property prop;
  42. if (parser_length->ParseValue(prop, argument, empty_parameter_map))
  43. {
  44. switch (length_argument_index)
  45. {
  46. case 0: shadow.offset_x = prop.GetNumericValue(); break;
  47. case 1: shadow.offset_y = prop.GetNumericValue(); break;
  48. case 2: shadow.blur_radius = prop.GetNumericValue(); break;
  49. case 3: shadow.spread_distance = prop.GetNumericValue(); break;
  50. default: return false;
  51. }
  52. length_argument_index += 1;
  53. }
  54. else if (argument == "inset")
  55. {
  56. shadow.inset = true;
  57. }
  58. else if (parser_color->ParseValue(prop, argument, empty_parameter_map))
  59. {
  60. shadow.color = prop.Get<Colourb>().ToPremultiplied();
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. }
  67. if (length_argument_index < 2)
  68. return false;
  69. }
  70. property.unit = Unit::BOXSHADOWLIST;
  71. property.value = Variant(std::move(shadow_list));
  72. return true;
  73. }
  74. } // namespace Rml