ElementStyle.cpp 29 KB

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