StyleSheet.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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->decorator_map = 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. decorator_map.reserve(decorator_map.size() + other_sheet.decorator_map.size());
  70. for (auto& other_decorator : other_sheet.decorator_map)
  71. {
  72. 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 = decorator_map.find(name);
  87. if (it != 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(const DecoratorDeclarationList& declaration_list, const PropertySource* source) const
  99. {
  100. RMLUI_ASSERT_NONRECURSIVE; // Since we may return a reference to the below static variable.
  101. static DecoratorPtrList non_cached_decorator_list;
  102. // Empty declaration values are used for interpolated values which we don't want to cache.
  103. const bool enable_cache = !declaration_list.value.empty();
  104. // Generate the cache key. Relative paths of textures may be affected by the source path, and ultimately
  105. // which texture should be displayed. Thus, we need to include this path in the cache key.
  106. String key;
  107. if (enable_cache)
  108. {
  109. key.reserve(declaration_list.value.size() + 1 + (source ? source->path.size() : 0));
  110. key = declaration_list.value;
  111. key += ';';
  112. if (source)
  113. key += source->path;
  114. auto it_cache = decorator_cache.find(key);
  115. if (it_cache != decorator_cache.end())
  116. return it_cache->second;
  117. }
  118. else
  119. {
  120. non_cached_decorator_list.clear();
  121. }
  122. DecoratorPtrList& decorators = enable_cache ? decorator_cache[key] : non_cached_decorator_list;
  123. decorators.reserve(declaration_list.list.size());
  124. for (const DecoratorDeclaration& declaration : declaration_list.list)
  125. {
  126. SharedPtr<Decorator> decorator;
  127. if (declaration.instancer)
  128. {
  129. RMLUI_ZoneScopedN("InstanceDecorator");
  130. decorator =
  131. declaration.instancer->InstanceDecorator(declaration.type, declaration.properties, DecoratorInstancerInterface(*this, source));
  132. if (!decorator)
  133. Log::Message(Log::LT_WARNING, "Decorator '%s' in '%s' could not be instanced, declared at %s:%d", declaration.type.c_str(),
  134. declaration_list.value.c_str(), source ? source->path.c_str() : "", source ? source->line_number : -1);
  135. }
  136. else
  137. {
  138. // If we have no instancer, this means the type is the name of an @decorator rule.
  139. auto it_map = decorator_map.find(declaration.type);
  140. if (it_map != decorator_map.end())
  141. decorator = it_map->second.decorator;
  142. if (!decorator)
  143. Log::Message(Log::LT_WARNING, "Decorator name '%s' could not be found in any @decorator rule, declared at %s:%d",
  144. declaration.type.c_str(), source ? source->path.c_str() : "", source ? source->line_number : -1);
  145. }
  146. if (!decorator)
  147. {
  148. decorators.clear();
  149. break;
  150. }
  151. decorators.push_back(std::move(decorator));
  152. }
  153. return decorators;
  154. }
  155. const Sprite* StyleSheet::GetSprite(const String& name) const
  156. {
  157. return spritesheet_list.GetSprite(name);
  158. }
  159. SharedPtr<const ElementDefinition> StyleSheet::GetElementDefinition(const Element* element) const
  160. {
  161. RMLUI_ASSERT_NONRECURSIVE;
  162. // Using static to avoid allocations. Make sure we don't call this function recursively.
  163. static Vector<const StyleSheetNode*> applicable_nodes;
  164. applicable_nodes.clear();
  165. auto AddApplicableNodes = [element](const StyleSheetIndex::NodeIndex& node_index, const String& key) {
  166. auto it_nodes = node_index.find(Hash<String>()(key));
  167. if (it_nodes != node_index.end())
  168. {
  169. const StyleSheetIndex::NodeList& nodes = it_nodes->second;
  170. for (const StyleSheetNode* node : nodes)
  171. {
  172. // We found a node that has at least one requirement matching the element. Now see if we satisfy the remaining requirements of the
  173. // node, including all ancestor nodes. What this involves is traversing the style nodes backwards, trying to match nodes in the
  174. // element's hierarchy to nodes in the style hierarchy.
  175. if (node->IsApplicable(element))
  176. applicable_nodes.push_back(node);
  177. }
  178. }
  179. };
  180. // See if there are any styles defined for this element.
  181. const String& tag = element->GetTagName();
  182. const String& id = element->GetId();
  183. const StringList& class_names = element->GetStyle()->GetClassNameList();
  184. // Text elements are never matched.
  185. if (tag == "#text")
  186. return nullptr;
  187. // First, look up the indexed requirements.
  188. if (!id.empty())
  189. AddApplicableNodes(styled_node_index.ids, id);
  190. for (const String& name : class_names)
  191. AddApplicableNodes(styled_node_index.classes, name);
  192. AddApplicableNodes(styled_node_index.tags, tag);
  193. // Also check all remaining nodes that don't contain any indexed requirements.
  194. for (const StyleSheetNode* node : styled_node_index.other)
  195. {
  196. if (node->IsApplicable(element))
  197. applicable_nodes.push_back(node);
  198. }
  199. // If this element definition won't actually store any information, don't bother with it.
  200. if (applicable_nodes.empty())
  201. return nullptr;
  202. // Sort the applicable nodes by specificity first, then by pointer value in case we have duplicate specificities.
  203. std::sort(applicable_nodes.begin(), applicable_nodes.end(), [](const StyleSheetNode* a, const StyleSheetNode* b) {
  204. const int a_specificity = a->GetSpecificity();
  205. const int b_specificity = b->GetSpecificity();
  206. if (a_specificity == b_specificity)
  207. return a < b;
  208. return a_specificity < b_specificity;
  209. });
  210. // Check if this puppy has already been cached in the node index.
  211. SharedPtr<const ElementDefinition>& definition = node_cache[applicable_nodes];
  212. if (!definition)
  213. {
  214. // Otherwise, create a new definition and add it to our cache.
  215. definition = MakeShared<const ElementDefinition>(applicable_nodes);
  216. }
  217. return definition;
  218. }
  219. } // namespace Rml