ElementDefinition.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifndef RMLUICOREELEMENTDEFINITION_H
  29. #define RMLUICOREELEMENTDEFINITION_H
  30. #include "../../Include/RmlUi/Core/Dictionary.h"
  31. #include "../../Include/RmlUi/Core/ReferenceCountable.h"
  32. #include <map>
  33. #include <set>
  34. #include "../../Include/RmlUi/Core/FontEffect.h"
  35. #include "StyleSheetNode.h"
  36. namespace Rml {
  37. namespace Core {
  38. class Decorator;
  39. class FontEffect;
  40. // Defines for the optimised version of the pseudo-class properties (note the difference from the
  41. // PseudoClassPropertyMap defined in StyleSheetNode.h ... bit clumsy). Here the properties are stored as a list
  42. // of definitions against each property name in specificity-order, along with the pseudo-class requirements for each
  43. // one. This makes it much more straight-forward to query at run-time.
  44. typedef std::pair< StringList, Property > PseudoClassProperty;
  45. typedef std::vector< PseudoClassProperty > PseudoClassPropertyList;
  46. typedef std::unordered_map< String, PseudoClassPropertyList > PseudoClassPropertyDictionary;
  47. typedef std::unordered_map< String, Decorator* > DecoratorMap;
  48. typedef std::map< StringList, DecoratorMap > PseudoClassDecoratorMap;
  49. /**
  50. @author Peter Curry
  51. */
  52. class ElementDefinition : public ReferenceCountable
  53. {
  54. public:
  55. enum PseudoClassVolatility
  56. {
  57. STABLE, // pseudo-class has no volatility
  58. FONT_VOLATILE, // pseudo-class may impact on font effects
  59. STRUCTURE_VOLATILE // pseudo-class may impact on definitions of child elements
  60. };
  61. ElementDefinition();
  62. virtual ~ElementDefinition();
  63. /// Initialises the element definition from a list of style sheet nodes.
  64. void Initialise(const std::vector< const StyleSheetNode* >& style_sheet_nodes, const PseudoClassList& volatile_pseudo_classes, bool structurally_volatile);
  65. /// Returns a specific property from the element definition's base properties.
  66. /// @param[in] name The name of the property to return.
  67. /// @param[in] pseudo_classes The pseudo-classes currently active on the calling element.
  68. /// @return The property defined against the give name, or NULL if no such property was found.
  69. const Property* GetProperty(const String& name, const PseudoClassList& pseudo_classes) const;
  70. /// Returns the list of properties this element definition defines for an element with the given set of
  71. /// pseudo-classes.
  72. /// @param[out] property_names The list to store the defined properties in.
  73. /// @param[in] pseudo_classes The pseudo-classes defined on the querying element.
  74. void GetDefinedProperties(PropertyNameList& property_names, const PseudoClassList& pseudo_classes) const;
  75. /// Returns the list of properties this element definition has explicit definitions for involving the given
  76. /// pseudo-class.
  77. /// @param[out] property_names The list of store the newly defined / undefined properties in.
  78. /// @param[in] pseudo_classes The list of pseudo-classes currently set on the element (post-change).
  79. /// @param[in] pseudo_class The pseudo-class that was just activated or deactivated.
  80. void GetDefinedProperties(PropertyNameList& property_names, const PseudoClassList& pseudo_classes, const String& pseudo_class) const;
  81. /// Iterates over the properties in the definition.
  82. /// @param[inout] index Index of the property to fetch. This is incremented to the next valid index after the fetch.
  83. /// @param[in] pseudo_classes The pseudo-classes defined on the querying element.
  84. /// @param[out] property_pseudo_classes The pseudo-classes the property is defined by.
  85. /// @param[out] property_name The name of the property at the specified index.
  86. /// @param[out] property The property at the specified index.
  87. /// @return True if a property was successfully fetched.
  88. bool IterateProperties(int& index, const PseudoClassList& pseudo_classes, PseudoClassList& property_pseudo_classes, String& property_name, const Property*& property) const;
  89. /// Returns the list of the element definition's instanced decorators in the default state.
  90. /// @return The list of instanced decorators.
  91. const DecoratorMap& GetDecorators() const;
  92. /// Returns the map of pseudo-class names to overriding instanced decorators.
  93. /// @return The map of the overriding decorators for each pseudo-class.
  94. const PseudoClassDecoratorMap& GetPseudoClassDecorators() const;
  95. /// Appends this definition's font effects (appropriately for the given pseudo classes) into a
  96. /// provided map of effects.
  97. /// @param[out] font_effects The outgoing map of font effects.
  98. /// @param[in] pseudo_classes Pseudo-classes active on the querying element.
  99. void GetFontEffects(FontEffectMap& font_effects, const PseudoClassList& pseudo_classes) const;
  100. /// Returns the volatility of a pseudo-class.
  101. /// @param[in] pseudo_class The name of the pseudo-class to check for volatility.
  102. /// @return The volatility of the pseudo-class.
  103. PseudoClassVolatility GetPseudoClassVolatility(const String& pseudo_class) const;
  104. /// Returns true if this definition is built from nodes using structural selectors, and therefore is reliant on
  105. /// siblings remaining stable.
  106. /// @return True if this definition is structurally volatile.
  107. bool IsStructurallyVolatile() const;
  108. protected:
  109. /// Destroys the definition.
  110. void OnReferenceDeactivate();
  111. private:
  112. typedef std::pair< String, PropertyDictionary > PropertyGroup;
  113. typedef std::map< String, PropertyGroup > PropertyGroupMap;
  114. typedef std::vector< std::pair< StringList, int > > PseudoClassFontEffectIndex;
  115. typedef std::map< String, PseudoClassFontEffectIndex > FontEffectIndex;
  116. typedef std::map< String, PseudoClassVolatility > PseudoClassVolatilityMap;
  117. // Finds all propery declarations for a group.
  118. void BuildPropertyGroup(PropertyGroupMap& groups, const String& group_type, const PropertyDictionary& element_properties, const PropertyGroupMap* default_properties = NULL);
  119. // Updates a property dictionary of all properties for a single group.
  120. int BuildPropertyGroupDictionary(PropertyDictionary& group_properties, const String& group_type, const String& group_name, const PropertyDictionary& element_properties);
  121. // Builds decorator definitions from the parsed properties and instances decorators as
  122. // appropriate.
  123. void InstanceDecorators(const PseudoClassPropertyMap& merged_pseudo_class_properties);
  124. // Attempts to instance a decorator.
  125. bool InstanceDecorator(const String& name, const String& type, const PropertyDictionary& properties, const StringList& pseudo_class = StringList());
  126. // Builds font effect definitions from the parsed properties and instances font effects as
  127. // appropriate.
  128. void InstanceFontEffects(const PseudoClassPropertyMap& merged_pseudo_class_properties);
  129. // Attempts to instance a font effect.
  130. bool InstanceFontEffect(const String& name, const String& type, const PropertyDictionary& properties, const StringList& pseudo_class = StringList());
  131. // Returns true if the pseudo-class requirement of a rule is met by a list of an element's pseudo-classes.
  132. bool IsPseudoClassRuleApplicable(const StringList& rule_pseudo_classes, const PseudoClassList& element_pseudo_classes) const;
  133. // The attributes for the default state of the element, with no pseudo-classes.
  134. PropertyDictionary properties;
  135. // The overridden attributes for the element's pseudo-classes.
  136. PseudoClassPropertyDictionary pseudo_class_properties;
  137. // The instanced decorators for this element definition.
  138. DecoratorMap decorators;
  139. // The overridden decorators for the element's pseudo-classes.
  140. PseudoClassDecoratorMap pseudo_class_decorators;
  141. // The list of every decorator used by this element in every class.
  142. FontEffectList font_effects;
  143. // For each unique decorator name, this stores (in order of specificity) the name of the
  144. // pseudo-class that has a definition for it, and the index into the list of decorators.
  145. FontEffectIndex font_effect_index;
  146. // The list of volatile pseudo-classes in this definition, and how volatile they are.
  147. PseudoClassVolatilityMap pseudo_class_volatility;
  148. // True if this definition has the potential to change as sibling elements are added or removed.
  149. bool structurally_volatile;
  150. };
  151. }
  152. }
  153. #endif