PropertyParserBoxShadow.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "PropertyParserBoxShadow.h"
  29. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  30. #include "../../Include/RmlUi/Core/StringUtilities.h"
  31. namespace Rml {
  32. PropertyParserBoxShadow::PropertyParserBoxShadow(PropertyParser* parser_color, PropertyParser* parser_length) :
  33. parser_color(parser_color), parser_length(parser_length)
  34. {
  35. RMLUI_ASSERT(parser_color && parser_length);
  36. }
  37. bool PropertyParserBoxShadow::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  38. {
  39. if (value.empty() || value == "none")
  40. {
  41. property.value = Variant();
  42. property.unit = Unit::UNKNOWN;
  43. return true;
  44. }
  45. StringList shadows_string_list;
  46. {
  47. auto lowercase_value = StringUtilities::ToLower(value);
  48. StringUtilities::ExpandString(shadows_string_list, lowercase_value, ',', '(', ')');
  49. }
  50. if (shadows_string_list.empty())
  51. return false;
  52. const ParameterMap empty_parameter_map;
  53. BoxShadowList shadow_list;
  54. shadow_list.reserve(shadows_string_list.size());
  55. for (const String& shadow_str : shadows_string_list)
  56. {
  57. StringList arguments;
  58. StringUtilities::ExpandString(arguments, shadow_str, ' ', '(', ')');
  59. if (arguments.empty())
  60. return false;
  61. shadow_list.push_back({});
  62. BoxShadow& shadow = shadow_list.back();
  63. int length_argument_index = 0;
  64. for (const String& argument : arguments)
  65. {
  66. if (argument.empty())
  67. continue;
  68. Property prop;
  69. if (parser_length->ParseValue(prop, argument, empty_parameter_map))
  70. {
  71. switch (length_argument_index)
  72. {
  73. case 0: shadow.offset_x = prop.GetNumericValue(); break;
  74. case 1: shadow.offset_y = prop.GetNumericValue(); break;
  75. case 2: shadow.blur_radius = prop.GetNumericValue(); break;
  76. case 3: shadow.spread_distance = prop.GetNumericValue(); break;
  77. default: return false;
  78. }
  79. length_argument_index += 1;
  80. }
  81. else if (argument == "inset")
  82. {
  83. shadow.inset = true;
  84. }
  85. else if (parser_color->ParseValue(prop, argument, empty_parameter_map))
  86. {
  87. shadow.color = prop.Get<Colourb>().ToPremultiplied();
  88. }
  89. else
  90. {
  91. return false;
  92. }
  93. }
  94. if (length_argument_index < 2)
  95. return false;
  96. }
  97. property.unit = Unit::BOXSHADOWLIST;
  98. property.value = Variant(std::move(shadow_list));
  99. return true;
  100. }
  101. } // namespace Rml