ElementStyle.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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 "ElementStyle.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Context.h"
  31. #include "../../Include/RmlUi/Core/Core.h"
  32. #include "../../Include/RmlUi/Core/ElementDocument.h"
  33. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  34. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  35. #include "../../Include/RmlUi/Core/Log.h"
  36. #include "../../Include/RmlUi/Core/Math.h"
  37. #include "../../Include/RmlUi/Core/Property.h"
  38. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  39. #include "../../Include/RmlUi/Core/PropertyDictionary.h"
  40. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  41. #include "../../Include/RmlUi/Core/Profiling.h"
  42. #include "../../Include/RmlUi/Core/StyleSheet.h"
  43. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  44. #include "../../Include/RmlUi/Core/TransformPrimitive.h"
  45. #include "ElementDecoration.h"
  46. #include "ElementDefinition.h"
  47. #include "ComputeProperty.h"
  48. #include "PropertiesIterator.h"
  49. #include <algorithm>
  50. namespace Rml {
  51. // Bitwise operations on the PseudoClassState.
  52. inline PseudoClassState operator|(PseudoClassState lhs, PseudoClassState rhs)
  53. {
  54. return PseudoClassState(int(lhs) | int(rhs));
  55. }
  56. inline PseudoClassState operator&(PseudoClassState lhs, PseudoClassState rhs)
  57. {
  58. return PseudoClassState(int(lhs) & int(rhs));
  59. }
  60. ElementStyle::ElementStyle(Element* _element)
  61. {
  62. element = _element;
  63. definition_dirty = true;
  64. }
  65. // Returns one of this element's properties.
  66. const Property* ElementStyle::GetLocalProperty(PropertyId id, const PropertyDictionary& inline_properties, const ElementDefinition* definition)
  67. {
  68. // Check for overriding local properties.
  69. const Property* property = inline_properties.GetProperty(id);
  70. if (property)
  71. return property;
  72. // Check for a property defined in an RCSS rule.
  73. if (definition)
  74. return definition->GetProperty(id);
  75. return nullptr;
  76. }
  77. // Returns one of this element's properties.
  78. const Property* ElementStyle::GetProperty(PropertyId id, const Element* element, const PropertyDictionary& inline_properties, const ElementDefinition* definition)
  79. {
  80. const Property* local_property = GetLocalProperty(id, inline_properties, definition);
  81. if (local_property)
  82. return local_property;
  83. // Fetch the property specification.
  84. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(id);
  85. if (!property)
  86. return nullptr;
  87. // If we can inherit this property, return our parent's property.
  88. if (property->IsInherited())
  89. {
  90. Element* parent = element->GetParentNode();
  91. while (parent)
  92. {
  93. const Property* parent_property = parent->GetStyle()->GetLocalProperty(id);
  94. if (parent_property)
  95. return parent_property;
  96. parent = parent->GetParentNode();
  97. }
  98. }
  99. // No property available! Return the default value.
  100. return property->GetDefaultValue();
  101. }
  102. // Apply transition to relevant properties if a transition is defined on element.
  103. // Properties that are part of a transition are removed from the properties list.
  104. void ElementStyle::TransitionPropertyChanges(Element* element, PropertyIdSet& properties, const PropertyDictionary& inline_properties, const ElementDefinition* old_definition, const ElementDefinition* new_definition)
  105. {
  106. RMLUI_ASSERT(element);
  107. if (!old_definition || !new_definition || properties.Empty())
  108. return;
  109. // We get the local property instead of the computed value here, because we want to intercept property changes even before the computed values are ready.
  110. // Now that we have the concept of computed values, we may want do this operation directly on them instead.
  111. if (const Property* transition_property = GetLocalProperty(PropertyId::Transition, inline_properties, new_definition))
  112. {
  113. if (transition_property->value.GetType() != Variant::TRANSITIONLIST)
  114. return;
  115. const TransitionList& transition_list = transition_property->value.GetReference<TransitionList>();
  116. if (!transition_list.none)
  117. {
  118. static const PropertyDictionary empty_properties;
  119. auto add_transition = [&](const Transition& transition) {
  120. bool transition_added = false;
  121. const Property* start_value = GetProperty(transition.id, element, inline_properties, old_definition);
  122. const Property* target_value = GetProperty(transition.id, element, empty_properties, new_definition);
  123. if (start_value && target_value && (*start_value != *target_value))
  124. transition_added = element->StartTransition(transition, *start_value, *target_value);
  125. return transition_added;
  126. };
  127. if (transition_list.all)
  128. {
  129. Transition transition = transition_list.transitions[0];
  130. for (auto it = properties.begin(); it != properties.end(); )
  131. {
  132. transition.id = *it;
  133. if (add_transition(transition))
  134. it = properties.Erase(it);
  135. else
  136. ++it;
  137. }
  138. }
  139. else
  140. {
  141. for (const Transition& transition : transition_list.transitions)
  142. {
  143. if (properties.Contains(transition.id))
  144. {
  145. if (add_transition(transition))
  146. properties.Erase(transition.id);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. void ElementStyle::UpdateDefinition()
  154. {
  155. if (definition_dirty)
  156. {
  157. RMLUI_ZoneScoped;
  158. definition_dirty = false;
  159. SharedPtr<const ElementDefinition> new_definition;
  160. if (const StyleSheet* style_sheet = element->GetStyleSheet())
  161. {
  162. new_definition = style_sheet->GetElementDefinition(element);
  163. }
  164. // Switch the property definitions if the definition has changed.
  165. if (new_definition != definition)
  166. {
  167. PropertyIdSet changed_properties;
  168. if (definition)
  169. changed_properties = definition->GetPropertyIds();
  170. if (new_definition)
  171. changed_properties |= new_definition->GetPropertyIds();
  172. if (definition && new_definition)
  173. {
  174. // Remove properties that compare equal from the changed list.
  175. const PropertyIdSet properties_in_both_definitions = (definition->GetPropertyIds() & new_definition->GetPropertyIds());
  176. for (PropertyId id : properties_in_both_definitions)
  177. {
  178. const Property* p0 = definition->GetProperty(id);
  179. const Property* p1 = new_definition->GetProperty(id);
  180. if (p0 && p1 && *p0 == *p1)
  181. changed_properties.Erase(id);
  182. }
  183. // Transition changed properties if transition property is set
  184. TransitionPropertyChanges(element, changed_properties, inline_properties, definition.get(), new_definition.get());
  185. }
  186. definition = new_definition;
  187. DirtyProperties(changed_properties);
  188. }
  189. // Even if the definition was not changed, the child definitions may have changed as a result of anything that
  190. // could change the definition of this element, such as a new pseudo class.
  191. DirtyChildDefinitions();
  192. }
  193. }
  194. // Sets or removes a pseudo-class on the element.
  195. bool ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate, bool override_class)
  196. {
  197. bool changed = false;
  198. if (activate)
  199. {
  200. PseudoClassState& state = pseudo_classes[pseudo_class];
  201. changed = (state == PseudoClassState::Clear);
  202. state = (state | (override_class ? PseudoClassState::Override : PseudoClassState::Set));
  203. }
  204. else
  205. {
  206. auto it = pseudo_classes.find(pseudo_class);
  207. if (it != pseudo_classes.end())
  208. {
  209. PseudoClassState& state = it->second;
  210. state = (state & (override_class ? PseudoClassState::Set : PseudoClassState::Override));
  211. if (state == PseudoClassState::Clear)
  212. {
  213. pseudo_classes.erase(it);
  214. changed = true;
  215. }
  216. }
  217. }
  218. if (changed)
  219. DirtyDefinition();
  220. return changed;
  221. }
  222. // Checks if a specific pseudo-class has been set on the element.
  223. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  224. {
  225. return (pseudo_classes.count(pseudo_class) == 1);
  226. }
  227. const PseudoClassMap& ElementStyle::GetActivePseudoClasses() const
  228. {
  229. return pseudo_classes;
  230. }
  231. // Sets or removes a class on the element.
  232. void ElementStyle::SetClass(const String& class_name, bool activate)
  233. {
  234. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  235. if (activate)
  236. {
  237. if (class_location == classes.end())
  238. {
  239. classes.push_back(class_name);
  240. DirtyDefinition();
  241. }
  242. }
  243. else
  244. {
  245. if (class_location != classes.end())
  246. {
  247. classes.erase(class_location);
  248. DirtyDefinition();
  249. }
  250. }
  251. }
  252. // Checks if a class is set on the element.
  253. bool ElementStyle::IsClassSet(const String& class_name) const
  254. {
  255. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  256. }
  257. // Specifies the entire list of classes for this element. This will replace any others specified.
  258. void ElementStyle::SetClassNames(const String& class_names)
  259. {
  260. classes.clear();
  261. StringUtilities::ExpandString(classes, class_names, ' ');
  262. DirtyDefinition();
  263. }
  264. // Returns the list of classes specified for this element.
  265. String ElementStyle::GetClassNames() const
  266. {
  267. String class_names;
  268. for (size_t i = 0; i < classes.size(); i++)
  269. {
  270. if (i != 0)
  271. {
  272. class_names += " ";
  273. }
  274. class_names += classes[i];
  275. }
  276. return class_names;
  277. }
  278. const StringList& ElementStyle::GetClassNameList() const
  279. {
  280. return classes;
  281. }
  282. // Sets a local property override on the element to a pre-parsed value.
  283. bool ElementStyle::SetProperty(PropertyId id, const Property& property)
  284. {
  285. Property new_property = property;
  286. new_property.definition = StyleSheetSpecification::GetProperty(id);
  287. if (!new_property.definition)
  288. return false;
  289. inline_properties.SetProperty(id, new_property);
  290. DirtyProperty(id);
  291. return true;
  292. }
  293. // Removes a local property override on the element.
  294. void ElementStyle::RemoveProperty(PropertyId id)
  295. {
  296. int size_before = inline_properties.GetNumProperties();
  297. inline_properties.RemoveProperty(id);
  298. if(inline_properties.GetNumProperties() != size_before)
  299. DirtyProperty(id);
  300. }
  301. // Returns one of this element's properties.
  302. const Property* ElementStyle::GetProperty(PropertyId id) const
  303. {
  304. return GetProperty(id, element, inline_properties, definition.get());
  305. }
  306. // Returns one of this element's properties.
  307. const Property* ElementStyle::GetLocalProperty(PropertyId id) const
  308. {
  309. return GetLocalProperty(id, inline_properties, definition.get());
  310. }
  311. const PropertyMap& ElementStyle::GetLocalStyleProperties() const
  312. {
  313. return inline_properties.GetProperties();
  314. }
  315. static float ComputeLength(const Property* property, Element* element)
  316. {
  317. const float font_size = element->GetComputedValues().font_size();
  318. float doc_font_size = DefaultComputedValues.font_size();
  319. float dp_ratio = 1.0f;
  320. Vector2f vp_dimensions(1.0f);
  321. if (ElementDocument* document = element->GetOwnerDocument())
  322. {
  323. doc_font_size = document->GetComputedValues().font_size();
  324. if (Context* context = document->GetContext())
  325. {
  326. dp_ratio = context->GetDensityIndependentPixelRatio();
  327. vp_dimensions = Vector2f(context->GetDimensions());
  328. }
  329. }
  330. const float result = ComputeLength(property, font_size, doc_font_size, dp_ratio, vp_dimensions);
  331. return result;
  332. }
  333. float ElementStyle::ResolveNumericProperty(const Property* property, float base_value) const
  334. {
  335. if (!property || !(property->unit & (Property::NUMBER_LENGTH_PERCENT | Property::ANGLE)))
  336. return 0.0f;
  337. if (property->unit & Property::NUMBER)
  338. return property->Get<float>() * base_value;
  339. else if (property->unit & Property::PERCENT)
  340. return property->Get<float>() * base_value * 0.01f;
  341. else if (property->unit & Property::ANGLE)
  342. return ComputeAngle(*property);
  343. const float result = ComputeLength(property, element);
  344. return result;
  345. }
  346. float ElementStyle::ResolveLength(const Property* property, RelativeTarget relative_target) const
  347. {
  348. RMLUI_ASSERT(property);
  349. // There is an exception on font-size properties, as 'em' units here refer to parent font size instead
  350. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && relative_target == RelativeTarget::ParentFontSize))
  351. {
  352. const float result = ComputeLength(property, element);
  353. return result;
  354. }
  355. float base_value = 0.0f;
  356. switch (relative_target)
  357. {
  358. case RelativeTarget::None:
  359. base_value = 1.0f;
  360. break;
  361. case RelativeTarget::ContainingBlockWidth:
  362. base_value = element->GetContainingBlock().x;
  363. break;
  364. case RelativeTarget::ContainingBlockHeight:
  365. base_value = element->GetContainingBlock().y;
  366. break;
  367. case RelativeTarget::FontSize:
  368. base_value = element->GetComputedValues().font_size();
  369. break;
  370. case RelativeTarget::ParentFontSize:
  371. {
  372. auto p = element->GetParentNode();
  373. base_value = (p ? p->GetComputedValues().font_size() : DefaultComputedValues.font_size());
  374. }
  375. break;
  376. case RelativeTarget::LineHeight:
  377. base_value = element->GetLineHeight();
  378. break;
  379. default:
  380. break;
  381. }
  382. float scale_value = 0.0f;
  383. switch (property->unit)
  384. {
  385. case Property::EM:
  386. case Property::NUMBER:
  387. scale_value = property->value.Get< float >();
  388. break;
  389. case Property::PERCENT:
  390. scale_value = property->value.Get< float >() * 0.01f;
  391. break;
  392. default:
  393. break;
  394. }
  395. return base_value * scale_value;
  396. }
  397. void ElementStyle::DirtyDefinition()
  398. {
  399. definition_dirty = true;
  400. }
  401. void ElementStyle::DirtyInheritedProperties()
  402. {
  403. dirty_properties |= StyleSheetSpecification::GetRegisteredInheritedProperties();
  404. }
  405. void ElementStyle::DirtyChildDefinitions()
  406. {
  407. for (int i = 0; i < element->GetNumChildren(true); i++)
  408. element->GetChild(i)->GetStyle()->DirtyDefinition();
  409. }
  410. void ElementStyle::DirtyPropertiesWithUnits(Property::Unit units)
  411. {
  412. // Dirty all the properties of this element that use the unit(s).
  413. for (auto it = Iterate(); !it.AtEnd(); ++it)
  414. {
  415. auto name_property_pair = *it;
  416. PropertyId id = name_property_pair.first;
  417. const Property& property = name_property_pair.second;
  418. if (property.unit & units)
  419. DirtyProperty(id);
  420. }
  421. }
  422. void ElementStyle::DirtyPropertiesWithUnitsRecursive(Property::Unit units)
  423. {
  424. DirtyPropertiesWithUnits(units);
  425. // Now dirty all of our descendant's properties that use the unit(s).
  426. int num_children = element->GetNumChildren(true);
  427. for (int i = 0; i < num_children; ++i)
  428. element->GetChild(i)->GetStyle()->DirtyPropertiesWithUnitsRecursive(units);
  429. }
  430. bool ElementStyle::AnyPropertiesDirty() const
  431. {
  432. return !dirty_properties.Empty();
  433. }
  434. PropertiesIterator ElementStyle::Iterate() const {
  435. // Note: Value initialized iterators are only guaranteed to compare equal in C++14, and only for iterators satisfying the ForwardIterator requirements.
  436. #ifdef _MSC_VER
  437. // Null forward iterator supported since VS 2015
  438. static_assert(_MSC_VER >= 1900, "Visual Studio 2015 or higher required, see comment.");
  439. #else
  440. static_assert(__cplusplus >= 201402L, "C++14 or higher required, see comment.");
  441. #endif
  442. const PropertyMap& property_map = inline_properties.GetProperties();
  443. auto it_style_begin = property_map.begin();
  444. auto it_style_end = property_map.end();
  445. PropertyMap::const_iterator it_definition{}, it_definition_end{};
  446. if (definition)
  447. {
  448. const PropertyMap& definition_properties = definition->GetProperties().GetProperties();
  449. it_definition = definition_properties.begin();
  450. it_definition_end = definition_properties.end();
  451. }
  452. return PropertiesIterator(it_style_begin, it_style_end, it_definition, it_definition_end);
  453. }
  454. // Sets a single property as dirty.
  455. void ElementStyle::DirtyProperty(PropertyId id)
  456. {
  457. dirty_properties.Insert(id);
  458. }
  459. // Sets a list of properties as dirty.
  460. void ElementStyle::DirtyProperties(const PropertyIdSet& properties)
  461. {
  462. dirty_properties |= properties;
  463. }
  464. PropertyIdSet ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio, Vector2f vp_dimensions)
  465. {
  466. if (dirty_properties.Empty())
  467. return PropertyIdSet();
  468. RMLUI_ZoneScopedC(0xFF7F50);
  469. // Generally, this is how it works:
  470. // 1. Assign default values (clears any removed properties)
  471. // 2. Inherit inheritable values from parent
  472. // 3. Assign any local properties (from inline style or stylesheet)
  473. // 4. Dirty properties in children that are inherited
  474. const float font_size_before = values.font_size();
  475. const Style::LineHeight line_height_before = values.line_height();
  476. // The next flag is just a small optimization, if the element was just created we don't need to copy all the default values.
  477. if (!values_are_default_initialized)
  478. {
  479. // This needs to be done in case some properties were removed and thus not in our local style anymore.
  480. // If we skipped this, the old dirty value would be unmodified, instead, now it is set to its default value.
  481. // Strictly speaking, we only really need to do this for the dirty, non-inherited values. However, in most
  482. // cases it seems simply assigning all non-inherited values is faster than iterating the dirty properties.
  483. values.CopyNonInherited(DefaultComputedValues);
  484. }
  485. if (parent_values)
  486. values.CopyInherited(*parent_values);
  487. else if (!values_are_default_initialized)
  488. values.CopyInherited(DefaultComputedValues);
  489. bool dirty_em_properties = false;
  490. // Always do font-size first if dirty, because of em-relative values
  491. if(dirty_properties.Contains(PropertyId::FontSize))
  492. {
  493. if (auto p = GetLocalProperty(PropertyId::FontSize))
  494. values.font_size(ComputeFontsize(*p, values, parent_values, document_values, dp_ratio, vp_dimensions));
  495. else if (parent_values)
  496. values.font_size(parent_values->font_size());
  497. if (font_size_before != values.font_size())
  498. {
  499. dirty_em_properties = true;
  500. dirty_properties.Insert(PropertyId::LineHeight);
  501. }
  502. }
  503. else
  504. {
  505. values.font_size(font_size_before);
  506. }
  507. const float font_size = values.font_size();
  508. const float document_font_size = (document_values ? document_values->font_size() : DefaultComputedValues.font_size());
  509. // Since vertical-align depends on line-height we compute this before iteration
  510. if (dirty_properties.Contains(PropertyId::LineHeight))
  511. {
  512. if (auto p = GetLocalProperty(PropertyId::LineHeight))
  513. {
  514. values.line_height(ComputeLineHeight(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  515. }
  516. else if (parent_values)
  517. {
  518. // Line height has a special inheritance case for numbers/percent: they inherit them directly instead of computed length, but for lengths,
  519. // they inherit the length. See CSS specs for details. Percent is already converted to number.
  520. if (parent_values->line_height().inherit_type == Style::LineHeight::Number)
  521. values.line_height(Style::LineHeight(font_size * parent_values->line_height().inherit_value, Style::LineHeight::Number,
  522. parent_values->line_height().inherit_value));
  523. else
  524. values.line_height(parent_values->line_height());
  525. }
  526. if (line_height_before.value != values.line_height().value || line_height_before.inherit_value != values.line_height().inherit_value)
  527. dirty_properties.Insert(PropertyId::VerticalAlign);
  528. }
  529. else
  530. {
  531. values.line_height(line_height_before);
  532. }
  533. bool dirty_font_face_handle = false;
  534. for (auto it = Iterate(); !it.AtEnd(); ++it)
  535. {
  536. auto name_property_pair = *it;
  537. const PropertyId id = name_property_pair.first;
  538. const Property* p = &name_property_pair.second;
  539. if (dirty_em_properties && p->unit == Property::EM)
  540. dirty_properties.Insert(id);
  541. using namespace Style;
  542. switch (id)
  543. {
  544. case PropertyId::MarginTop:
  545. values.margin_top(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  546. break;
  547. case PropertyId::MarginRight:
  548. values.margin_right(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  549. break;
  550. case PropertyId::MarginBottom:
  551. values.margin_bottom(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  552. break;
  553. case PropertyId::MarginLeft:
  554. values.margin_left(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  555. break;
  556. case PropertyId::PaddingTop:
  557. values.padding_top(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  558. break;
  559. case PropertyId::PaddingRight:
  560. values.padding_right(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  561. break;
  562. case PropertyId::PaddingBottom:
  563. values.padding_bottom(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  564. break;
  565. case PropertyId::PaddingLeft:
  566. values.padding_left(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  567. break;
  568. case PropertyId::BorderTopWidth:
  569. values.border_top_width(ComputeBorderWidth(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions)));
  570. break;
  571. case PropertyId::BorderRightWidth:
  572. values.border_right_width(ComputeBorderWidth(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions)));
  573. break;
  574. case PropertyId::BorderBottomWidth:
  575. values.border_bottom_width(ComputeBorderWidth(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions)));
  576. break;
  577. case PropertyId::BorderLeftWidth:
  578. values.border_left_width(ComputeBorderWidth(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions)));
  579. break;
  580. case PropertyId::BorderTopColor:
  581. values.border_top_color(p->Get<Colourb>());
  582. break;
  583. case PropertyId::BorderRightColor:
  584. values.border_right_color(p->Get<Colourb>());
  585. break;
  586. case PropertyId::BorderBottomColor:
  587. values.border_bottom_color(p->Get<Colourb>());
  588. break;
  589. case PropertyId::BorderLeftColor:
  590. values.border_left_color(p->Get<Colourb>());
  591. break;
  592. case PropertyId::BorderTopLeftRadius:
  593. values.border_top_left_radius(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  594. break;
  595. case PropertyId::BorderTopRightRadius:
  596. values.border_top_right_radius(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  597. break;
  598. case PropertyId::BorderBottomRightRadius:
  599. values.border_bottom_right_radius(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  600. break;
  601. case PropertyId::BorderBottomLeftRadius:
  602. values.border_bottom_left_radius(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  603. break;
  604. case PropertyId::Display:
  605. values.display((Display)p->Get<int>());
  606. break;
  607. case PropertyId::Position:
  608. values.position((Position)p->Get<int>());
  609. break;
  610. case PropertyId::Top:
  611. values.top(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  612. break;
  613. case PropertyId::Right:
  614. values.right(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  615. break;
  616. case PropertyId::Bottom:
  617. values.bottom(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  618. break;
  619. case PropertyId::Left:
  620. values.left(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  621. break;
  622. case PropertyId::Float:
  623. values.float_((Float)p->Get<int>());
  624. break;
  625. case PropertyId::Clear:
  626. values.clear((Clear)p->Get<int>());
  627. break;
  628. case PropertyId::BoxSizing:
  629. values.box_sizing((BoxSizing)p->Get<int>());
  630. break;
  631. case PropertyId::ZIndex:
  632. values.z_index((p->unit == Property::KEYWORD ? ZIndex(ZIndex::Auto) : ZIndex(ZIndex::Number, p->Get<float>())));
  633. break;
  634. case PropertyId::Width:
  635. values.width(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  636. break;
  637. case PropertyId::MinWidth:
  638. values.min_width(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  639. break;
  640. case PropertyId::MaxWidth:
  641. values.max_width(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  642. break;
  643. case PropertyId::Height:
  644. values.height(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  645. break;
  646. case PropertyId::MinHeight:
  647. values.min_height(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  648. break;
  649. case PropertyId::MaxHeight:
  650. values.max_height(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  651. break;
  652. case PropertyId::LineHeight:
  653. // (Line-height computed above)
  654. break;
  655. case PropertyId::VerticalAlign:
  656. values.vertical_align(ComputeVerticalAlign(p, values.line_height().value, font_size, document_font_size, dp_ratio, vp_dimensions));
  657. break;
  658. case PropertyId::OverflowX:
  659. values.overflow_x((Overflow)p->Get< int >());
  660. break;
  661. case PropertyId::OverflowY:
  662. values.overflow_y((Overflow)p->Get< int >());
  663. break;
  664. case PropertyId::Clip:
  665. values.clip(ComputeClip(p));
  666. break;
  667. case PropertyId::Visibility:
  668. values.visibility((Visibility)p->Get< int >());
  669. break;
  670. case PropertyId::BackgroundColor:
  671. values.background_color(p->Get<Colourb>());
  672. break;
  673. case PropertyId::Color:
  674. values.color(p->Get<Colourb>());
  675. break;
  676. case PropertyId::ImageColor:
  677. values.image_color(p->Get<Colourb>());
  678. break;
  679. case PropertyId::Opacity:
  680. values.opacity(p->Get<float>());
  681. break;
  682. case PropertyId::FontFamily:
  683. // Fetched from element's properties.
  684. dirty_font_face_handle = true;
  685. break;
  686. case PropertyId::FontStyle:
  687. values.font_style((FontStyle)p->Get< int >());
  688. dirty_font_face_handle = true;
  689. break;
  690. case PropertyId::FontWeight:
  691. values.font_weight((FontWeight)p->Get< int >());
  692. dirty_font_face_handle = true;
  693. break;
  694. case PropertyId::FontSize:
  695. // (font-size computed above)
  696. dirty_font_face_handle = true;
  697. break;
  698. case PropertyId::TextAlign:
  699. values.text_align((TextAlign)p->Get<int>());
  700. break;
  701. case PropertyId::TextDecoration:
  702. values.text_decoration((TextDecoration)p->Get<int>());
  703. break;
  704. case PropertyId::TextTransform:
  705. values.text_transform((TextTransform)p->Get<int>());
  706. break;
  707. case PropertyId::WhiteSpace:
  708. values.white_space((WhiteSpace)p->Get<int>());
  709. break;
  710. case PropertyId::WordBreak:
  711. values.word_break((WordBreak)p->Get<int>());
  712. break;
  713. case PropertyId::RowGap:
  714. values.row_gap(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  715. break;
  716. case PropertyId::ColumnGap:
  717. values.column_gap(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  718. break;
  719. case PropertyId::Drag:
  720. values.drag((Drag)p->Get< int >());
  721. break;
  722. case PropertyId::TabIndex:
  723. values.tab_index((TabIndex)p->Get< int >());
  724. break;
  725. case PropertyId::Focus:
  726. values.focus((Focus)p->Get<int>());
  727. break;
  728. case PropertyId::ScrollbarMargin:
  729. values.scrollbar_margin(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  730. break;
  731. case PropertyId::PointerEvents:
  732. values.pointer_events((PointerEvents)p->Get<int>());
  733. break;
  734. case PropertyId::Perspective:
  735. values.perspective(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  736. break;
  737. case PropertyId::PerspectiveOriginX:
  738. values.perspective_origin_x(ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  739. break;
  740. case PropertyId::PerspectiveOriginY:
  741. values.perspective_origin_y(ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  742. break;
  743. case PropertyId::TransformOriginX:
  744. values.transform_origin_x(ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  745. break;
  746. case PropertyId::TransformOriginY:
  747. values.transform_origin_y(ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  748. break;
  749. case PropertyId::TransformOriginZ:
  750. values.transform_origin_z(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  751. break;
  752. case PropertyId::Decorator:
  753. values.has_decorator(p->unit == Property::DECORATOR);
  754. break;
  755. case PropertyId::FontEffect:
  756. values.has_font_effect((p->unit == Property::FONTEFFECT));
  757. break;
  758. case PropertyId::FlexBasis:
  759. values.flex_basis(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  760. break;
  761. // Fetched from element's properties.
  762. case PropertyId::Cursor:
  763. case PropertyId::Transform:
  764. case PropertyId::Transition:
  765. case PropertyId::Animation:
  766. case PropertyId::AlignContent:
  767. case PropertyId::AlignItems:
  768. case PropertyId::AlignSelf:
  769. case PropertyId::FlexDirection:
  770. case PropertyId::FlexGrow:
  771. case PropertyId::FlexShrink:
  772. case PropertyId::FlexWrap:
  773. case PropertyId::JustifyContent:
  774. break;
  775. // Unhandled properties. Must be manually retrieved with 'GetProperty()'.
  776. case PropertyId::FillImage:
  777. case PropertyId::CaretColor:
  778. break;
  779. // Invalid properties
  780. case PropertyId::Invalid:
  781. case PropertyId::NumDefinedIds:
  782. case PropertyId::MaxNumIds:
  783. break;
  784. }
  785. }
  786. // The font-face handle is nulled when local font properties are set. In that case we need to retrieve a new handle.
  787. if (dirty_font_face_handle)
  788. {
  789. RMLUI_ZoneScopedN("FontFaceHandle");
  790. values.font_face_handle(
  791. GetFontEngineInterface()->GetFontFaceHandle(values.font_family(), values.font_style(), values.font_weight(), (int)values.font_size()));
  792. }
  793. // Next, pass inheritable dirty properties onto our children
  794. PropertyIdSet dirty_inherited_properties = (dirty_properties & StyleSheetSpecification::GetRegisteredInheritedProperties());
  795. if (!dirty_inherited_properties.Empty())
  796. {
  797. for (int i = 0; i < element->GetNumChildren(true); i++)
  798. {
  799. auto child = element->GetChild(i);
  800. child->GetStyle()->dirty_properties |= dirty_inherited_properties;
  801. }
  802. }
  803. PropertyIdSet result(std::move(dirty_properties));
  804. dirty_properties.Clear();
  805. return result;
  806. }
  807. } // namespace Rml