StyleSheet.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 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/DecoratorInstancer.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 "../../Include/RmlUi/Core/Utilities.h"
  35. #include "ElementDefinition.h"
  36. #include "StyleSheetNode.h"
  37. #include <algorithm>
  38. namespace Rml {
  39. // Sorts style nodes based on specificity.
  40. inline static bool StyleSheetNodeSort(const StyleSheetNode* lhs, const StyleSheetNode* rhs)
  41. {
  42. return lhs->GetSpecificity() < rhs->GetSpecificity();
  43. }
  44. StyleSheet::StyleSheet()
  45. {
  46. root = MakeUnique<StyleSheetNode>();
  47. specificity_offset = 0;
  48. }
  49. StyleSheet::~StyleSheet()
  50. {
  51. }
  52. /// Combines this style sheet with another one, producing a new sheet
  53. UniquePtr<StyleSheet> StyleSheet::CombineStyleSheet(const StyleSheet& other_sheet) const
  54. {
  55. RMLUI_ZoneScoped;
  56. UniquePtr<StyleSheet> new_sheet = UniquePtr<StyleSheet>(new StyleSheet());
  57. new_sheet->root = root->DeepCopy();
  58. new_sheet->specificity_offset = specificity_offset;
  59. new_sheet->keyframes = keyframes;
  60. new_sheet->decorator_map = decorator_map;
  61. new_sheet->spritesheet_list = spritesheet_list;
  62. new_sheet->MergeStyleSheet(other_sheet);
  63. return new_sheet;
  64. }
  65. void StyleSheet::MergeStyleSheet(const StyleSheet& other_sheet)
  66. {
  67. RMLUI_ZoneScoped;
  68. root->MergeHierarchy(other_sheet.root.get(), specificity_offset);
  69. specificity_offset += other_sheet.specificity_offset;
  70. // Any matching @keyframe names are overridden as per CSS rules
  71. keyframes.reserve(keyframes.size() + other_sheet.keyframes.size());
  72. for (auto& other_keyframes : other_sheet.keyframes)
  73. {
  74. keyframes[other_keyframes.first] = other_keyframes.second;
  75. }
  76. // Copy over the decorators, and replace any matching decorator names from other_sheet
  77. decorator_map.reserve(decorator_map.size() + other_sheet.decorator_map.size());
  78. for (auto& other_decorator : other_sheet.decorator_map)
  79. {
  80. decorator_map[other_decorator.first] = other_decorator.second;
  81. }
  82. spritesheet_list.Reserve(
  83. spritesheet_list.NumSpriteSheets() + other_sheet.spritesheet_list.NumSpriteSheets(),
  84. spritesheet_list.NumSprites() + other_sheet.spritesheet_list.NumSprites()
  85. );
  86. spritesheet_list.Merge(other_sheet.spritesheet_list);
  87. }
  88. // Builds the node index for a combined style sheet.
  89. void StyleSheet::BuildNodeIndex()
  90. {
  91. RMLUI_ZoneScoped;
  92. styled_node_index.clear();
  93. root->BuildIndex(styled_node_index);
  94. root->SetStructurallyVolatileRecursive(false);
  95. }
  96. // Returns the Keyframes of the given name, or null if it does not exist.
  97. const Keyframes * StyleSheet::GetKeyframes(const String & name) const
  98. {
  99. auto it = keyframes.find(name);
  100. if (it != keyframes.end())
  101. return &(it->second);
  102. return nullptr;
  103. }
  104. const Vector<SharedPtr<const Decorator>>& StyleSheet::InstanceDecorators(const DecoratorDeclarationList& declaration_list, const PropertySource* source) const
  105. {
  106. // Generate the cache key. Relative paths of textures may be affected by the source path, and ultimately
  107. // which texture should be displayed. Thus, we need to include this path in the cache key.
  108. String key;
  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. Vector<SharedPtr<const Decorator>>& decorators = decorator_cache[key];
  118. for (const DecoratorDeclaration& declaration : declaration_list.list)
  119. {
  120. if (declaration.instancer)
  121. {
  122. RMLUI_ZoneScopedN("InstanceDecorator");
  123. if (SharedPtr<Decorator> decorator = declaration.instancer->InstanceDecorator(declaration.type, declaration.properties, DecoratorInstancerInterface(*this, source)))
  124. decorators.push_back(std::move(decorator));
  125. else
  126. Log::Message(Log::LT_WARNING, "Decorator '%s' in '%s' could not be instanced, declared at %s:%d", declaration.type.c_str(), declaration_list.value.c_str(), source ? source->path.c_str() : "", source ? source->line_number : -1);
  127. }
  128. else
  129. {
  130. // If we have no instancer, this means the type is the name of an @decorator rule.
  131. SharedPtr<Decorator> decorator;
  132. auto it_map = decorator_map.find(declaration.type);
  133. if (it_map != decorator_map.end())
  134. decorator = it_map->second.decorator;
  135. if (decorator)
  136. decorators.push_back(std::move(decorator));
  137. else
  138. Log::Message(Log::LT_WARNING, "Decorator name '%s' could not be found in any @decorator rule, declared at %s:%d", declaration.type.c_str(), source ? source->path.c_str() : "", source ? source->line_number : -1);
  139. }
  140. }
  141. return decorators;
  142. }
  143. const Sprite* StyleSheet::GetSprite(const String& name) const
  144. {
  145. return spritesheet_list.GetSprite(name);
  146. }
  147. size_t StyleSheet::NodeHash(const String& tag, const String& id)
  148. {
  149. size_t seed = 0;
  150. if (!tag.empty())
  151. seed = Hash<String>()(tag);
  152. if(!id.empty())
  153. Utilities::HashCombine(seed, id);
  154. return seed;
  155. }
  156. // Returns the compiled element definition for a given element hierarchy.
  157. SharedPtr<const ElementDefinition> StyleSheet::GetElementDefinition(const Element* element) const
  158. {
  159. RMLUI_ASSERT_NONRECURSIVE;
  160. // See if there are any styles defined for this element.
  161. // Using static to avoid allocations. Make sure we don't call this function recursively.
  162. static Vector< const StyleSheetNode* > applicable_nodes;
  163. applicable_nodes.clear();
  164. const String& tag = element->GetTagName();
  165. const String& id = element->GetId();
  166. // The styled_node_index is hashed with the tag and id of the RCSS rule. However, we must also check
  167. // the rules which don't have them defined, because they apply regardless of tag and id.
  168. Array<size_t, 4> node_hash;
  169. int num_hashes = 2;
  170. node_hash[0] = 0;
  171. node_hash[1] = NodeHash(tag, String());
  172. // If we don't have an id, we can safely skip nodes that define an id. Otherwise, we also check the id nodes.
  173. if (!id.empty())
  174. {
  175. num_hashes = 4;
  176. node_hash[2] = NodeHash(String(), id);
  177. node_hash[3] = NodeHash(tag, id);
  178. }
  179. // The hashes are keys into a set of applicable nodes (given tag and id).
  180. for (int i = 0; i < num_hashes; i++)
  181. {
  182. auto it_nodes = styled_node_index.find(node_hash[i]);
  183. if (it_nodes != styled_node_index.end())
  184. {
  185. const NodeList& nodes = it_nodes->second;
  186. // Now see if we satisfy all of the requirements not yet tested: classes, pseudo classes, structural selectors,
  187. // and the full requirements of parent nodes. What this involves is traversing the style nodes backwards,
  188. // trying to match nodes in the element's hierarchy to nodes in the style hierarchy.
  189. for (const StyleSheetNode* node : nodes)
  190. {
  191. if (node->IsApplicable(element, true))
  192. {
  193. applicable_nodes.push_back(node);
  194. }
  195. }
  196. }
  197. }
  198. std::sort(applicable_nodes.begin(), applicable_nodes.end(), StyleSheetNodeSort);
  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. // Check if this puppy has already been cached in the node index.
  203. size_t seed = 0;
  204. for (const StyleSheetNode* node : applicable_nodes)
  205. Utilities::HashCombine(seed, node);
  206. auto cache_iterator = node_cache.find(seed);
  207. if (cache_iterator != node_cache.end())
  208. {
  209. SharedPtr<const ElementDefinition>& definition = (*cache_iterator).second;
  210. return definition;
  211. }
  212. // Create the new definition and add it to our cache.
  213. auto new_definition = MakeShared<const ElementDefinition>(applicable_nodes);
  214. node_cache[seed] = new_definition;
  215. return new_definition;
  216. }
  217. } // namespace Rml