PropertyParserFontEffect.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "PropertyParserFontEffect.h"
  2. #include "../../Include/RmlUi/Core/Factory.h"
  3. #include "../../Include/RmlUi/Core/FontEffect.h"
  4. #include "../../Include/RmlUi/Core/FontEffectInstancer.h"
  5. #include "../../Include/RmlUi/Core/Profiling.h"
  6. #include "../../Include/RmlUi/Core/PropertySpecification.h"
  7. #include "../../Include/RmlUi/Core/Utilities.h"
  8. #include <algorithm>
  9. namespace Rml {
  10. PropertyParserFontEffect::PropertyParserFontEffect() {}
  11. PropertyParserFontEffect::~PropertyParserFontEffect() {}
  12. bool PropertyParserFontEffect::ParseValue(Property& property, const String& font_effect_string_value, const ParameterMap& /*parameters*/) const
  13. {
  14. // Font-effects are declared as
  15. // font-effect: <font-effect-value>[, <font-effect-value> ...];
  16. // Where <font-effect-value> is declared with inline properties, e.g.
  17. // font-effect: outline( 1px black ), ...;
  18. if (font_effect_string_value.empty() || font_effect_string_value == "none")
  19. {
  20. property.value = Variant();
  21. property.unit = Unit::UNKNOWN;
  22. return true;
  23. }
  24. RMLUI_ZoneScoped;
  25. FontEffects font_effects;
  26. // Make sure we don't split inside the parenthesis since they may appear in decorator shorthands.
  27. StringList font_effect_string_list;
  28. StringUtilities::ExpandString(font_effect_string_list, font_effect_string_value, ',', '(', ')');
  29. font_effects.value = font_effect_string_value;
  30. font_effects.list.reserve(font_effect_string_list.size());
  31. // Get or instance each decorator in the comma-separated string list
  32. for (const String& font_effect_string : font_effect_string_list)
  33. {
  34. const size_t shorthand_open = font_effect_string.find('(');
  35. const size_t shorthand_close = font_effect_string.rfind(')');
  36. const bool invalid_parenthesis = (shorthand_open == String::npos || shorthand_close == String::npos || shorthand_open >= shorthand_close);
  37. if (invalid_parenthesis)
  38. {
  39. // We found no parenthesis, font-effects can only be declared anonymously for now.
  40. Log::Message(Log::LT_WARNING, "Invalid syntax for font-effect '%s'.", font_effect_string.c_str());
  41. return false;
  42. }
  43. else
  44. {
  45. // Since we have parentheses it must be an anonymous decorator with inline properties
  46. const String type = StringUtilities::StripWhitespace(font_effect_string.substr(0, shorthand_open));
  47. // Check for valid font-effect type
  48. FontEffectInstancer* instancer = Factory::GetFontEffectInstancer(type);
  49. if (!instancer)
  50. {
  51. Log::Message(Log::LT_WARNING, "Font-effect type '%s' not found.", type.c_str());
  52. return false;
  53. }
  54. const String shorthand = font_effect_string.substr(shorthand_open + 1, shorthand_close - shorthand_open - 1);
  55. const PropertySpecification& specification = instancer->GetPropertySpecification();
  56. // Parse the shorthand properties given by the 'font-effect' shorthand property
  57. PropertyDictionary properties;
  58. if (!specification.ParsePropertyDeclaration(properties, "font-effect", shorthand))
  59. {
  60. // Empty values are allowed in font-effects, if the value is not empty we must have encountered a parser error.
  61. if (!StringUtilities::StripWhitespace(shorthand).empty())
  62. {
  63. Log::Message(Log::LT_WARNING, "Could not parse font-effect value '%s'.", font_effect_string.c_str());
  64. return false;
  65. }
  66. }
  67. // Set unspecified values to their defaults
  68. specification.SetPropertyDefaults(properties);
  69. RMLUI_ZoneScopedN("InstanceFontEffect");
  70. SharedPtr<FontEffect> font_effect = instancer->InstanceFontEffect(type, properties);
  71. if (font_effect)
  72. {
  73. // Create a unique hash value for the given type and values
  74. size_t fingerprint = Hash<String>{}(type);
  75. for (const auto& id_value : properties.GetProperties())
  76. Utilities::HashCombine(fingerprint, id_value.second.Get<String>());
  77. font_effect->SetFingerprint(fingerprint);
  78. font_effects.list.emplace_back(std::move(font_effect));
  79. }
  80. else
  81. {
  82. Log::Message(Log::LT_WARNING, "Font-effect '%s' could not be instanced.", font_effect_string.c_str());
  83. return false;
  84. }
  85. }
  86. }
  87. if (font_effects.list.empty())
  88. return false;
  89. // Partition the list such that the back layer effects appear before the front layer effects
  90. std::stable_partition(font_effects.list.begin(), font_effects.list.end(),
  91. [](const SharedPtr<const FontEffect>& effect) { return effect->GetLayer() == FontEffect::Layer::Back; });
  92. property.value = Variant(MakeShared<FontEffects>(std::move(font_effects)));
  93. property.unit = Unit::FONTEFFECT;
  94. return true;
  95. }
  96. } // namespace Rml