2
0

ElementStyle.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 RMLUICOREELEMENTSTYLE_H
  29. #define RMLUICOREELEMENTSTYLE_H
  30. #include "ElementDefinition.h"
  31. #include "../../Include/RmlUi/Core/Types.h"
  32. namespace Rml {
  33. namespace Core {
  34. class ElementStyleCache;
  35. typedef std::unordered_map<String, int> PropCounter;
  36. /**
  37. Manages an element's style and property information.
  38. @author Lloyd Weehuizen
  39. */
  40. class ElementStyle
  41. {
  42. public:
  43. /// Constructor
  44. /// @param[in] element The element this structure belongs to.
  45. ElementStyle(Element* element);
  46. ~ElementStyle();
  47. /// Returns the element's definition, updating if necessary.
  48. const ElementDefinition* GetDefinition();
  49. /// Update this definition if required
  50. void UpdateDefinition();
  51. /// Sets or removes a pseudo-class on the element.
  52. /// @param[in] pseudo_class The pseudo class to activate or deactivate.
  53. /// @param[in] activate True if the pseudo-class is to be activated, false to be deactivated.
  54. void SetPseudoClass(const String& pseudo_class, bool activate);
  55. /// Checks if a specific pseudo-class has been set on the element.
  56. /// @param[in] pseudo_class The name of the pseudo-class to check for.
  57. /// @return True if the pseudo-class is set on the element, false if not.
  58. bool IsPseudoClassSet(const String& pseudo_class) const;
  59. /// Gets a list of the current active pseudo classes
  60. const PseudoClassList& GetActivePseudoClasses() const;
  61. /// Sets or removes a class on the element.
  62. /// @param[in] class_name The name of the class to add or remove from the class list.
  63. /// @param[in] activate True if the class is to be added, false to be removed.
  64. void SetClass(const String& class_name, bool activate);
  65. /// Checks if a class is set on the element.
  66. /// @param[in] class_name The name of the class to check for.
  67. /// @return True if the class is set on the element, false otherwise.
  68. bool IsClassSet(const String& class_name) const;
  69. /// Specifies the entire list of classes for this element. This will replace any others specified.
  70. /// @param[in] class_names The list of class names to set on the style, separated by spaces.
  71. void SetClassNames(const String& class_names);
  72. /// Return the active class list.
  73. /// @return A string containing all the classes on the element, separated by spaces.
  74. String GetClassNames() const;
  75. /// Sets a local property override on the element.
  76. /// @param[in] name The name of the new property.
  77. /// @param[in] property The new property to set.
  78. bool SetProperty(const String& name, const String& value);
  79. /// Sets a local property override on the element to a pre-parsed value.
  80. /// @param[in] name The name of the new property.
  81. /// @param[in] property The parsed property to set.
  82. bool SetProperty(const String& name, const Property& property);
  83. /// Removes a local property override on the element; its value will revert to that defined in
  84. /// the style sheet.
  85. /// @param[in] name The name of the local property definition to remove.
  86. void RemoveProperty(const String& name);
  87. /// Returns one of this element's properties. If this element is not defined this property, or a parent cannot
  88. /// be found that we can inherit the property from, the default value will be returned.
  89. /// @param[in] name The name of the property to fetch the value for.
  90. /// @return The value of this property for this element, or NULL if no property exists with the given name.
  91. const Property* GetProperty(const String& name);
  92. /// Returns one of this element's properties. If this element is not defined this property, NULL will be
  93. /// returned.
  94. /// @param[in] name The name of the property to fetch the value for.
  95. /// @return The value of this property for this element, or NULL if this property has not been explicitly defined for this element.
  96. const Property* GetLocalProperty(const String& name);
  97. /// Returns the local properties, excluding any properties from local class.
  98. /// @return The local properties for this element, or NULL if no properties defined
  99. const PropertyMap* GetLocalProperties() const;
  100. /// Resolves a length property to pixels. Note: This excludes percentages.
  101. float ResolveLength(const Property* property);
  102. /// Resolves an angle to radians
  103. static float ResolveAngle(const Property* property);
  104. /// Resolves a number-length-percentage property to pixels.
  105. float ResolveNumericProperty(const String& property_name, const Property* property);
  106. /// Resolves the canonical unit (pixels) from 'number-length-percent' property.
  107. /// 'percentage' and 'number' gets multiplied by the size of the specified relative reference.
  108. float ResolveNumericProperty(const Property* property, RelativeTarget relative_target);
  109. /// Resolves one of this element's properties. If the value is a number or px, this is returned. If it's a
  110. /// percentage then it is resolved based on the second argument (the base value).
  111. /// If it's an angle, it is returned as radians.
  112. /// @param[in] property Property to resolve the value for.
  113. /// @param[in] base_value The value that is scaled by the percentage value, if it is a percentage.
  114. /// @return The value of this property for this element.
  115. float ResolveProperty(const Property *property, float base_value);
  116. /// Resolves one of this element's properties. If the value is a number or px, this is returned. If it's a
  117. /// percentage then it is resolved based on the second argument (the base value).
  118. /// If it's an angle, it is returned as radians.
  119. /// @param[in] name The name of the property to resolve the value for.
  120. /// @param[in] base_value The value that is scaled by the percentage value, if it is a percentage.
  121. /// @return The value of this property for this element.
  122. float ResolveProperty(const String& name, float base_value);
  123. /// Iterates over the properties defined on the element.
  124. /// @param[inout] index Index of the property to fetch. This is incremented to the next valid index after the fetch. Indices are not necessarily incremental.
  125. /// @param[out] pseudo_classes The pseudo-classes the property is defined by.
  126. /// @param[out] name The name of the property at the specified index.
  127. /// @param[out] property The property at the specified index.
  128. /// @return True if a property was successfully fetched.
  129. bool IterateProperties(int& index, PseudoClassList& pseudo_classes, String& name, const Property*& property);
  130. /// Returns the active style sheet for this element. This may be NULL.
  131. StyleSheet* GetStyleSheet() const;
  132. /// Mark definition and all children dirty
  133. void DirtyDefinition();
  134. /// Dirty all child definitions
  135. void DirtyChildDefinitions();
  136. // Dirties every property.
  137. void DirtyProperties();
  138. // Dirties em-relative properties.
  139. void DirtyEmProperties();
  140. // Dirties font-size on child elements if appropriate.
  141. void DirtyInheritedEmProperties();
  142. // Dirties rem properties.
  143. void DirtyRemProperties();
  144. // Dirties dp properties.
  145. void DirtyDpProperties();
  146. /// Returns 'top', 'bottom', 'left' and 'right' properties from element's style or local cache.
  147. void GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right );
  148. /// Returns 'border-width' properties from element's style or local cache.
  149. void GetBorderWidthProperties(const Property **border_top_width, const Property **border_bottom_width, const Property **border_left_width, const Property **border_right_width);
  150. /// Returns 'margin' properties from element's style or local cache.
  151. void GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right);
  152. /// Returns 'padding' properties from element's style or local cache.
  153. void GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right);
  154. /// Returns 'width' and 'height' properties from element's style or local cache.
  155. void GetDimensionProperties(const Property **width, const Property **height);
  156. /// Returns local 'width' and 'height' properties from element's style or local cache,
  157. /// ignoring default values.
  158. void GetLocalDimensionProperties(const Property **width, const Property **height);
  159. /// Returns 'overflow' properties' values from element's style or local cache.
  160. void GetOverflow(int *overflow_x, int *overflow_y);
  161. /// Returns 'position' property value from element's style or local cache.
  162. int GetPosition();
  163. /// Returns 'float' property value from element's style or local cache.
  164. int GetFloat();
  165. /// Returns 'display' property value from element's style or local cache.
  166. int GetDisplay();
  167. /// Returns 'white-space' property value from element's style or local cache.
  168. int GetWhitespace();
  169. /// Returns 'pointer-events' property value from element's style or local cache.
  170. int GetPointerEvents();
  171. /// Returns 'line-height' property value from element's style or local cache.
  172. const Property *GetLineHeightProperty();
  173. /// Returns 'text-align' property value from element's style or local cache.
  174. int GetTextAlign();
  175. /// Returns 'text-transform' property value from element's style or local cache.
  176. int GetTextTransform();
  177. /// Returns 'vertical-align' property value from element's style or local cache.
  178. const Property *GetVerticalAlignProperty();
  179. /// Returns 'perspective' property value from element's style or local cache.
  180. const Property *GetPerspective();
  181. /// Returns 'perspective-origin-x' property value from element's style or local cache.
  182. const Property *GetPerspectiveOriginX();
  183. /// Returns 'perspective-origin-y' property value from element's style or local cache.
  184. const Property *GetPerspectiveOriginY();
  185. /// Returns 'transform' property value from element's style or local cache.
  186. const Property *GetTransform();
  187. /// Returns 'transform-origin-x' property value from element's style or local cache.
  188. const Property *GetTransformOriginX();
  189. /// Returns 'transform-origin-y' property value from element's style or local cache.
  190. const Property *GetTransformOriginY();
  191. /// Returns 'transform-origin-z' property value from element's style or local cache.
  192. const Property *GetTransformOriginZ();
  193. static PropCounter &GetPropCounter();
  194. private:
  195. // Sets a single property as dirty.
  196. void DirtyProperty(const String& property);
  197. // Sets a list of properties as dirty.
  198. void DirtyProperties(const PropertyNameList& properties, bool clear_em_properties = true);
  199. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  200. void DirtyInheritedProperties(const PropertyNameList& properties);
  201. static const Property* GetLocalProperty(const String & name, PropertyDictionary * local_properties, ElementDefinition * definition, const PseudoClassList & pseudo_classes);
  202. static const Property* GetProperty(const String & name, Element * element, PropertyDictionary * local_properties, ElementDefinition * definition, const PseudoClassList & pseudo_classes);
  203. static void TransitionPropertyChanges(Element * element, PropertyNameList & properties, PropertyDictionary * local_properties, ElementDefinition * old_definition, ElementDefinition * new_definition,
  204. const PseudoClassList & pseudo_classes_before, const PseudoClassList & pseudo_classes_after);
  205. // Element these properties belong to
  206. Element* element;
  207. // The list of classes applicable to this object.
  208. StringList classes;
  209. // This element's current pseudo-classes.
  210. PseudoClassList pseudo_classes;
  211. // Any properties that have been overridden in this element.
  212. PropertyDictionary* local_properties;
  213. // All properties (including inherited) that are EM-relative.
  214. PropertyNameList* em_properties;
  215. // The definition of this element; if this is NULL one will be fetched from the element's style.
  216. ElementDefinition* definition;
  217. // Set if a new element definition should be fetched from the style.
  218. bool definition_dirty;
  219. // Set if a child element has a dirty style definition
  220. bool child_definition_dirty;
  221. // cached non-inherited properties
  222. ElementStyleCache *cache;
  223. };
  224. }
  225. }
  226. #endif