PropertyParserFontEffect.cpp 5.7 KB

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