StyleSheet.cpp 8.6 KB

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