StyleSheet.cpp 7.8 KB

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