StyleSheet.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "ElementDefinition.h"
  30. #include "StyleSheetNode.h"
  31. #include "Utilities.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. #include "../../Include/RmlUi/Core/Profiling.h"
  34. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  35. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  36. #include <algorithm>
  37. namespace Rml {
  38. // Sorts style nodes based on specificity.
  39. inline static bool StyleSheetNodeSort(const StyleSheetNode* lhs, const StyleSheetNode* rhs)
  40. {
  41. return lhs->GetSpecificity() < rhs->GetSpecificity();
  42. }
  43. StyleSheet::StyleSheet()
  44. {
  45. root = MakeUnique<StyleSheetNode>();
  46. specificity_offset = 0;
  47. }
  48. StyleSheet::~StyleSheet()
  49. {
  50. }
  51. /// Combines this style sheet with another one, producing a new sheet
  52. UniquePtr<StyleSheet> StyleSheet::CombineStyleSheet(const StyleSheet& other_sheet) const
  53. {
  54. RMLUI_ZoneScoped;
  55. UniquePtr<StyleSheet> new_sheet = UniquePtr<StyleSheet>(new StyleSheet());
  56. new_sheet->root = root->DeepCopy();
  57. new_sheet->specificity_offset = specificity_offset;
  58. new_sheet->keyframes = keyframes;
  59. new_sheet->decorator_map = decorator_map;
  60. new_sheet->spritesheet_list = spritesheet_list;
  61. new_sheet->MergeStyleSheet(other_sheet);
  62. return new_sheet;
  63. }
  64. void StyleSheet::MergeStyleSheet(const StyleSheet& other_sheet)
  65. {
  66. RMLUI_ZoneScoped;
  67. root->MergeHierarchy(other_sheet.root.get(), specificity_offset);
  68. specificity_offset += other_sheet.specificity_offset;
  69. // Any matching @keyframe names are overridden as per CSS rules
  70. keyframes.reserve(keyframes.size() + other_sheet.keyframes.size());
  71. for (auto& other_keyframes : other_sheet.keyframes)
  72. {
  73. keyframes[other_keyframes.first] = other_keyframes.second;
  74. }
  75. // Copy over the decorators, and replace any matching decorator names from other_sheet
  76. decorator_map.reserve(decorator_map.size() + other_sheet.decorator_map.size());
  77. for (auto& other_decorator : other_sheet.decorator_map)
  78. {
  79. decorator_map[other_decorator.first] = other_decorator.second;
  80. }
  81. spritesheet_list.Reserve(
  82. spritesheet_list.NumSpriteSheets() + other_sheet.spritesheet_list.NumSpriteSheets(),
  83. spritesheet_list.NumSprites() + other_sheet.spritesheet_list.NumSprites()
  84. );
  85. spritesheet_list.Merge(other_sheet.spritesheet_list);
  86. }
  87. // Builds the node index for a combined style sheet.
  88. void StyleSheet::BuildNodeIndex()
  89. {
  90. RMLUI_ZoneScoped;
  91. styled_node_index.clear();
  92. root->BuildIndex(styled_node_index);
  93. root->SetStructurallyVolatileRecursive(false);
  94. }
  95. // Returns the Keyframes of the given name, or null if it does not exist.
  96. const Keyframes * StyleSheet::GetKeyframes(const String & name) const
  97. {
  98. auto it = keyframes.find(name);
  99. if (it != keyframes.end())
  100. return &(it->second);
  101. return nullptr;
  102. }
  103. SharedPtr<Decorator> StyleSheet::GetDecorator(const String& name) const
  104. {
  105. auto it = decorator_map.find(name);
  106. if (it == decorator_map.end())
  107. return nullptr;
  108. return it->second.decorator;
  109. }
  110. const Sprite* StyleSheet::GetSprite(const String& name) const
  111. {
  112. return spritesheet_list.GetSprite(name);
  113. }
  114. size_t StyleSheet::NodeHash(const String& tag, const String& id)
  115. {
  116. size_t seed = 0;
  117. if (!tag.empty())
  118. seed = Hash<String>()(tag);
  119. if(!id.empty())
  120. Utilities::HashCombine(seed, id);
  121. return seed;
  122. }
  123. // Returns the compiled element definition for a given element hierarchy.
  124. SharedPtr<ElementDefinition> StyleSheet::GetElementDefinition(const Element* element) const
  125. {
  126. RMLUI_ASSERT_NONRECURSIVE;
  127. // See if there are any styles defined for this element.
  128. // Using static to avoid allocations. Make sure we don't call this function recursively.
  129. static Vector< const StyleSheetNode* > applicable_nodes;
  130. applicable_nodes.clear();
  131. const String& tag = element->GetTagName();
  132. const String& id = element->GetId();
  133. // The styled_node_index is hashed with the tag and id of the RCSS rule. However, we must also check
  134. // the rules which don't have them defined, because they apply regardless of tag and id.
  135. Array<size_t, 4> node_hash;
  136. int num_hashes = 2;
  137. node_hash[0] = 0;
  138. node_hash[1] = NodeHash(tag, String());
  139. // If we don't have an id, we can safely skip nodes that define an id. Otherwise, we also check the id nodes.
  140. if (!id.empty())
  141. {
  142. num_hashes = 4;
  143. node_hash[2] = NodeHash(String(), id);
  144. node_hash[3] = NodeHash(tag, id);
  145. }
  146. // The hashes are keys into a set of applicable nodes (given tag and id).
  147. for (int i = 0; i < num_hashes; i++)
  148. {
  149. auto it_nodes = styled_node_index.find(node_hash[i]);
  150. if (it_nodes != styled_node_index.end())
  151. {
  152. const NodeList& nodes = it_nodes->second;
  153. // Now see if we satisfy all of the requirements not yet tested: classes, pseudo classes, structural selectors,
  154. // and the full requirements of parent nodes. What this involves is traversing the style nodes backwards,
  155. // trying to match nodes in the element's hierarchy to nodes in the style hierarchy.
  156. for (const StyleSheetNode* node : nodes)
  157. {
  158. if (node->IsApplicable(element, true))
  159. {
  160. applicable_nodes.push_back(node);
  161. }
  162. }
  163. }
  164. }
  165. std::sort(applicable_nodes.begin(), applicable_nodes.end(), StyleSheetNodeSort);
  166. // If this element definition won't actually store any information, don't bother with it.
  167. if (applicable_nodes.empty())
  168. return nullptr;
  169. // Check if this puppy has already been cached in the node index.
  170. size_t seed = 0;
  171. for (const StyleSheetNode* node : applicable_nodes)
  172. Utilities::HashCombine(seed, node);
  173. auto cache_iterator = node_cache.find(seed);
  174. if (cache_iterator != node_cache.end())
  175. {
  176. SharedPtr<ElementDefinition>& definition = (*cache_iterator).second;
  177. return definition;
  178. }
  179. // Create the new definition and add it to our cache.
  180. auto new_definition = MakeShared<ElementDefinition>(applicable_nodes);
  181. node_cache[seed] = new_definition;
  182. return new_definition;
  183. }
  184. } // namespace Rml