StyleSheet.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "../../Include/RmlUi/Core/StyleSheet.h"
  29. #include "../../Include/RmlUi/Core/Decorator.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. #include "../../Include/RmlUi/Core/Profiling.h"
  32. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  33. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  34. #include "ElementDefinition.h"
  35. #include "ElementStyle.h"
  36. #include "StyleSheetNode.h"
  37. #include <algorithm>
  38. namespace Rml {
  39. StyleSheet::StyleSheet()
  40. {
  41. root = MakeUnique<StyleSheetNode>();
  42. specificity_offset = 0;
  43. }
  44. StyleSheet::~StyleSheet() {}
  45. UniquePtr<StyleSheet> StyleSheet::CombineStyleSheet(const StyleSheet& other_sheet) const
  46. {
  47. RMLUI_ZoneScoped;
  48. UniquePtr<StyleSheet> new_sheet = UniquePtr<StyleSheet>(new StyleSheet());
  49. new_sheet->root = root->DeepCopy();
  50. new_sheet->specificity_offset = specificity_offset;
  51. new_sheet->keyframes = keyframes;
  52. new_sheet->named_decorator_map = named_decorator_map;
  53. new_sheet->spritesheet_list = spritesheet_list;
  54. new_sheet->MergeStyleSheet(other_sheet);
  55. return new_sheet;
  56. }
  57. void StyleSheet::MergeStyleSheet(const StyleSheet& other_sheet)
  58. {
  59. RMLUI_ZoneScoped;
  60. root->MergeHierarchy(other_sheet.root.get(), specificity_offset);
  61. specificity_offset += other_sheet.specificity_offset;
  62. // Any matching @keyframe names are overridden as per CSS rules
  63. keyframes.reserve(keyframes.size() + other_sheet.keyframes.size());
  64. for (auto& other_keyframes : other_sheet.keyframes)
  65. {
  66. keyframes[other_keyframes.first] = other_keyframes.second;
  67. }
  68. // Copy over the decorators, and replace any matching decorator names from other_sheet
  69. named_decorator_map.reserve(named_decorator_map.size() + other_sheet.named_decorator_map.size());
  70. for (auto& other_decorator : other_sheet.named_decorator_map)
  71. {
  72. named_decorator_map[other_decorator.first] = other_decorator.second;
  73. }
  74. spritesheet_list.Reserve(spritesheet_list.NumSpriteSheets() + other_sheet.spritesheet_list.NumSpriteSheets(),
  75. spritesheet_list.NumSprites() + other_sheet.spritesheet_list.NumSprites());
  76. spritesheet_list.Merge(other_sheet.spritesheet_list);
  77. }
  78. void StyleSheet::BuildNodeIndex()
  79. {
  80. RMLUI_ZoneScoped;
  81. styled_node_index = {};
  82. root->BuildIndex(styled_node_index);
  83. }
  84. const NamedDecorator* StyleSheet::GetNamedDecorator(const String& name) const
  85. {
  86. auto it = named_decorator_map.find(name);
  87. if (it != named_decorator_map.end())
  88. return &(it->second);
  89. return nullptr;
  90. }
  91. const Keyframes* StyleSheet::GetKeyframes(const String& name) const
  92. {
  93. auto it = keyframes.find(name);
  94. if (it != keyframes.end())
  95. return &(it->second);
  96. return nullptr;
  97. }
  98. const DecoratorPtrList& StyleSheet::InstanceDecorators(RenderManager& render_manager, const DecoratorDeclarationList& declaration_list,
  99. const PropertySource* source) const
  100. {
  101. RMLUI_ASSERT_NONRECURSIVE; // Since we may return a reference to the below static variable.
  102. static DecoratorPtrList non_cached_decorator_list;
  103. // Empty declaration values are used for interpolated values which we don't want to cache.
  104. const bool enable_cache = !declaration_list.value.empty();
  105. // Generate the cache key. Relative paths of textures may be affected by the source path, and ultimately
  106. // which texture should be displayed. Thus, we need to include this path in the cache key.
  107. String key;
  108. if (enable_cache)
  109. {
  110. key.reserve(declaration_list.value.size() + 1 + (source ? source->path.size() : 0));
  111. key = declaration_list.value;
  112. key += ';';
  113. if (source)
  114. key += source->path;
  115. auto it_cache = decorator_cache.find(key);
  116. if (it_cache != decorator_cache.end())
  117. return it_cache->second;
  118. }
  119. else
  120. {
  121. non_cached_decorator_list.clear();
  122. }
  123. DecoratorPtrList& decorators = enable_cache ? decorator_cache[key] : non_cached_decorator_list;
  124. decorators.reserve(declaration_list.list.size());
  125. for (const DecoratorDeclaration& declaration : declaration_list.list)
  126. {
  127. SharedPtr<Decorator> decorator;
  128. if (declaration.instancer)
  129. {
  130. RMLUI_ZoneScopedN("InstanceDecorator");
  131. decorator = declaration.instancer->InstanceDecorator(declaration.type, declaration.properties,
  132. DecoratorInstancerInterface(render_manager, *this, source));
  133. if (!decorator)
  134. Log::Message(Log::LT_WARNING, "Decorator '%s' in '%s' could not be instanced, declared at %s:%d", declaration.type.c_str(),
  135. declaration_list.value.c_str(), source ? source->path.c_str() : "", source ? source->line_number : -1);
  136. }
  137. else
  138. {
  139. // If we have no instancer, this means the type is the name of an @decorator rule.
  140. auto it_map = named_decorator_map.find(declaration.type);
  141. if (it_map != named_decorator_map.end())
  142. decorator = it_map->second.instancer->InstanceDecorator(it_map->second.type, it_map->second.properties,
  143. DecoratorInstancerInterface(render_manager, *this, source));
  144. if (!decorator)
  145. Log::Message(Log::LT_WARNING, "Decorator name '%s' could not be found in any @decorator rule, declared at %s:%d",
  146. declaration.type.c_str(), source ? source->path.c_str() : "", source ? source->line_number : -1);
  147. }
  148. if (!decorator)
  149. {
  150. decorators.clear();
  151. break;
  152. }
  153. decorators.push_back(std::move(decorator));
  154. }
  155. return decorators;
  156. }
  157. const Sprite* StyleSheet::GetSprite(const String& name) const
  158. {
  159. return spritesheet_list.GetSprite(name);
  160. }
  161. SharedPtr<const ElementDefinition> StyleSheet::GetElementDefinition(const Element* element) const
  162. {
  163. RMLUI_ASSERT_NONRECURSIVE;
  164. // Using static to avoid allocations. Make sure we don't call this function recursively.
  165. static Vector<const StyleSheetNode*> applicable_nodes;
  166. applicable_nodes.clear();
  167. auto AddApplicableNodes = [element](const StyleSheetIndex::NodeIndex& node_index, const String& key) {
  168. auto it_nodes = node_index.find(Hash<String>()(key));
  169. if (it_nodes != node_index.end())
  170. {
  171. const StyleSheetIndex::NodeList& nodes = it_nodes->second;
  172. for (const StyleSheetNode* node : nodes)
  173. {
  174. // We found a node that has at least one requirement matching the element. Now see if we satisfy the remaining requirements of the
  175. // node, including all ancestor nodes. What this involves is traversing the style nodes backwards, trying to match nodes in the
  176. // element's hierarchy to nodes in the style hierarchy.
  177. if (node->IsApplicable(element, nullptr))
  178. applicable_nodes.push_back(node);
  179. }
  180. }
  181. };
  182. // See if there are any styles defined for this element.
  183. const String& tag = element->GetTagName();
  184. const String& id = element->GetId();
  185. const StringList& class_names = element->GetStyle()->GetClassNameList();
  186. // Text elements are never matched.
  187. if (tag == "#text")
  188. return nullptr;
  189. // First, look up the indexed requirements.
  190. if (!id.empty())
  191. AddApplicableNodes(styled_node_index.ids, id);
  192. for (const String& name : class_names)
  193. AddApplicableNodes(styled_node_index.classes, name);
  194. AddApplicableNodes(styled_node_index.tags, tag);
  195. // Also check all remaining nodes that don't contain any indexed requirements.
  196. for (const StyleSheetNode* node : styled_node_index.other)
  197. {
  198. if (node->IsApplicable(element, nullptr))
  199. applicable_nodes.push_back(node);
  200. }
  201. // If this element definition won't actually store any information, don't bother with it.
  202. if (applicable_nodes.empty())
  203. return nullptr;
  204. // Sort the applicable nodes by specificity first, then by pointer value in case we have duplicate specificities.
  205. std::sort(applicable_nodes.begin(), applicable_nodes.end(), [](const StyleSheetNode* a, const StyleSheetNode* b) {
  206. const int a_specificity = a->GetSpecificity();
  207. const int b_specificity = b->GetSpecificity();
  208. if (a_specificity == b_specificity)
  209. return a < b;
  210. return a_specificity < b_specificity;
  211. });
  212. // Check if this puppy has already been cached in the node index.
  213. SharedPtr<const ElementDefinition>& definition = node_cache[applicable_nodes];
  214. if (!definition)
  215. {
  216. // Otherwise, create a new definition and add it to our cache.
  217. definition = MakeShared<const ElementDefinition>(applicable_nodes);
  218. }
  219. return definition;
  220. }
  221. } // namespace Rml