ElementStyle.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. default:
  370. break;
  371. }
  372. float scale_value = 0.0f;
  373. switch (property->unit)
  374. {
  375. case Property::EM:
  376. case Property::NUMBER:
  377. scale_value = property->value.Get< float >();
  378. break;
  379. case Property::PERCENT:
  380. scale_value = property->value.Get< float >() * 0.01f;
  381. break;
  382. default:
  383. break;
  384. }
  385. return base_value * scale_value;
  386. }
  387. void ElementStyle::DirtyInheritedProperties()
  388. {
  389. dirty_properties |= StyleSheetSpecification::GetRegisteredInheritedProperties();
  390. }
  391. void ElementStyle::DirtyPropertiesWithUnits(Property::Unit units)
  392. {
  393. // Dirty all the properties of this element that use the unit(s).
  394. for (auto it = Iterate(); !it.AtEnd(); ++it)
  395. {
  396. auto name_property_pair = *it;
  397. PropertyId id = name_property_pair.first;
  398. const Property& property = name_property_pair.second;
  399. if (property.unit & units)
  400. DirtyProperty(id);
  401. }
  402. }
  403. void ElementStyle::DirtyPropertiesWithUnitsRecursive(Property::Unit units)
  404. {
  405. DirtyPropertiesWithUnits(units);
  406. // Now dirty all of our descendant's properties that use the unit(s).
  407. int num_children = element->GetNumChildren(true);
  408. for (int i = 0; i < num_children; ++i)
  409. element->GetChild(i)->GetStyle()->DirtyPropertiesWithUnitsRecursive(units);
  410. }
  411. bool ElementStyle::AnyPropertiesDirty() const
  412. {
  413. return !dirty_properties.Empty();
  414. }
  415. PropertiesIterator ElementStyle::Iterate() const {
  416. // Note: Value initialized iterators are only guaranteed to compare equal in C++14, and only for iterators satisfying the ForwardIterator requirements.
  417. #ifdef _MSC_VER
  418. // Null forward iterator supported since VS 2015
  419. static_assert(_MSC_VER >= 1900, "Visual Studio 2015 or higher required, see comment.");
  420. #else
  421. static_assert(__cplusplus >= 201402L, "C++14 or higher required, see comment.");
  422. #endif
  423. const PropertyMap& property_map = inline_properties.GetProperties();
  424. auto it_style_begin = property_map.begin();
  425. auto it_style_end = property_map.end();
  426. PropertyMap::const_iterator it_definition{}, it_definition_end{};
  427. if (definition)
  428. {
  429. const PropertyMap& definition_properties = definition->GetProperties().GetProperties();
  430. it_definition = definition_properties.begin();
  431. it_definition_end = definition_properties.end();
  432. }
  433. return PropertiesIterator(it_style_begin, it_style_end, it_definition, it_definition_end);
  434. }
  435. // Sets a single property as dirty.
  436. void ElementStyle::DirtyProperty(PropertyId id)
  437. {
  438. dirty_properties.Insert(id);
  439. }
  440. // Sets a list of properties as dirty.
  441. void ElementStyle::DirtyProperties(const PropertyIdSet& properties)
  442. {
  443. dirty_properties |= properties;
  444. }
  445. 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)
  446. {
  447. if (dirty_properties.Empty())
  448. return PropertyIdSet();
  449. RMLUI_ZoneScopedC(0xFF7F50);
  450. // Generally, this is how it works:
  451. // 1. Assign default values (clears any removed properties)
  452. // 2. Inherit inheritable values from parent
  453. // 3. Assign any local properties (from inline style or stylesheet)
  454. // 4. Dirty properties in children that are inherited
  455. const float font_size_before = values.font_size();
  456. const Style::LineHeight line_height_before = values.line_height();
  457. // The next flag is just a small optimization, if the element was just created we don't need to copy all the default values.
  458. if (!values_are_default_initialized)
  459. {
  460. // This needs to be done in case some properties were removed and thus not in our local style anymore.
  461. // If we skipped this, the old dirty value would be unmodified, instead, now it is set to its default value.
  462. // Strictly speaking, we only really need to do this for the dirty, non-inherited values. However, in most
  463. // cases it seems simply assigning all non-inherited values is faster than iterating the dirty properties.
  464. values.CopyNonInherited(DefaultComputedValues);
  465. }
  466. if (parent_values)
  467. values.CopyInherited(*parent_values);
  468. else if (!values_are_default_initialized)
  469. values.CopyInherited(DefaultComputedValues);
  470. bool dirty_em_properties = false;
  471. // Always do font-size first if dirty, because of em-relative values
  472. if(dirty_properties.Contains(PropertyId::FontSize))
  473. {
  474. if (auto p = GetLocalProperty(PropertyId::FontSize))
  475. values.font_size(ComputeFontsize(*p, values, parent_values, document_values, dp_ratio, vp_dimensions));
  476. else if (parent_values)
  477. values.font_size(parent_values->font_size());
  478. if (font_size_before != values.font_size())
  479. {
  480. dirty_em_properties = true;
  481. dirty_properties.Insert(PropertyId::LineHeight);
  482. }
  483. }
  484. else
  485. {
  486. values.font_size(font_size_before);
  487. }
  488. const float font_size = values.font_size();
  489. const float document_font_size = (document_values ? document_values->font_size() : DefaultComputedValues.font_size());
  490. // Since vertical-align depends on line-height we compute this before iteration
  491. if (dirty_properties.Contains(PropertyId::LineHeight))
  492. {
  493. if (auto p = GetLocalProperty(PropertyId::LineHeight))
  494. {
  495. values.line_height(ComputeLineHeight(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  496. }
  497. else if (parent_values)
  498. {
  499. // Line height has a special inheritance case for numbers/percent: they inherit them directly instead of computed length, but for lengths,
  500. // they inherit the length. See CSS specs for details. Percent is already converted to number.
  501. if (parent_values->line_height().inherit_type == Style::LineHeight::Number)
  502. values.line_height(Style::LineHeight(font_size * parent_values->line_height().inherit_value, Style::LineHeight::Number,
  503. parent_values->line_height().inherit_value));
  504. else
  505. values.line_height(parent_values->line_height());
  506. }
  507. if (line_height_before.value != values.line_height().value || line_height_before.inherit_value != values.line_height().inherit_value)
  508. dirty_properties.Insert(PropertyId::VerticalAlign);
  509. }
  510. else
  511. {
  512. values.line_height(line_height_before);
  513. }
  514. bool dirty_font_face_handle = false;
  515. for (auto it = Iterate(); !it.AtEnd(); ++it)
  516. {
  517. auto name_property_pair = *it;
  518. const PropertyId id = name_property_pair.first;
  519. const Property* p = &name_property_pair.second;
  520. if (dirty_em_properties && p->unit == Property::EM)
  521. dirty_properties.Insert(id);
  522. using namespace Style;
  523. switch (id)
  524. {
  525. case PropertyId::MarginTop:
  526. values.margin_top(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  527. break;
  528. case PropertyId::MarginRight:
  529. values.margin_right(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  530. break;
  531. case PropertyId::MarginBottom:
  532. values.margin_bottom(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  533. break;
  534. case PropertyId::MarginLeft:
  535. values.margin_left(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  536. break;
  537. case PropertyId::PaddingTop:
  538. values.padding_top(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  539. break;
  540. case PropertyId::PaddingRight:
  541. values.padding_right(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  542. break;
  543. case PropertyId::PaddingBottom:
  544. values.padding_bottom(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  545. break;
  546. case PropertyId::PaddingLeft:
  547. values.padding_left(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  548. break;
  549. case PropertyId::BorderTopWidth:
  550. values.border_top_width(ComputeBorderWidth(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions)));
  551. break;
  552. case PropertyId::BorderRightWidth:
  553. values.border_right_width(ComputeBorderWidth(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions)));
  554. break;
  555. case PropertyId::BorderBottomWidth:
  556. values.border_bottom_width(ComputeBorderWidth(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions)));
  557. break;
  558. case PropertyId::BorderLeftWidth:
  559. values.border_left_width(ComputeBorderWidth(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions)));
  560. break;
  561. case PropertyId::BorderTopColor:
  562. values.border_top_color(p->Get<Colourb>());
  563. break;
  564. case PropertyId::BorderRightColor:
  565. values.border_right_color(p->Get<Colourb>());
  566. break;
  567. case PropertyId::BorderBottomColor:
  568. values.border_bottom_color(p->Get<Colourb>());
  569. break;
  570. case PropertyId::BorderLeftColor:
  571. values.border_left_color(p->Get<Colourb>());
  572. break;
  573. case PropertyId::BorderTopLeftRadius:
  574. values.border_top_left_radius(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  575. break;
  576. case PropertyId::BorderTopRightRadius:
  577. values.border_top_right_radius(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  578. break;
  579. case PropertyId::BorderBottomRightRadius:
  580. values.border_bottom_right_radius(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  581. break;
  582. case PropertyId::BorderBottomLeftRadius:
  583. values.border_bottom_left_radius(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  584. break;
  585. case PropertyId::Display:
  586. values.display((Display)p->Get<int>());
  587. break;
  588. case PropertyId::Position:
  589. values.position((Position)p->Get<int>());
  590. break;
  591. case PropertyId::Top:
  592. values.top(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  593. break;
  594. case PropertyId::Right:
  595. values.right(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  596. break;
  597. case PropertyId::Bottom:
  598. values.bottom(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  599. break;
  600. case PropertyId::Left:
  601. values.left(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  602. break;
  603. case PropertyId::Float:
  604. values.float_((Float)p->Get<int>());
  605. break;
  606. case PropertyId::Clear:
  607. values.clear((Clear)p->Get<int>());
  608. break;
  609. case PropertyId::BoxSizing:
  610. values.box_sizing((BoxSizing)p->Get<int>());
  611. break;
  612. case PropertyId::ZIndex:
  613. values.z_index((p->unit == Property::KEYWORD ? ZIndex(ZIndex::Auto) : ZIndex(ZIndex::Number, p->Get<float>())));
  614. break;
  615. case PropertyId::Width:
  616. values.width(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  617. break;
  618. case PropertyId::MinWidth:
  619. values.min_width(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  620. break;
  621. case PropertyId::MaxWidth:
  622. values.max_width(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  623. break;
  624. case PropertyId::Height:
  625. values.height(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  626. break;
  627. case PropertyId::MinHeight:
  628. values.min_height(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  629. break;
  630. case PropertyId::MaxHeight:
  631. values.max_height(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  632. break;
  633. case PropertyId::LineHeight:
  634. // (Line-height computed above)
  635. break;
  636. case PropertyId::VerticalAlign:
  637. values.vertical_align(ComputeVerticalAlign(p, values.line_height().value, font_size, document_font_size, dp_ratio, vp_dimensions));
  638. break;
  639. case PropertyId::OverflowX:
  640. values.overflow_x((Overflow)p->Get< int >());
  641. break;
  642. case PropertyId::OverflowY:
  643. values.overflow_y((Overflow)p->Get< int >());
  644. break;
  645. case PropertyId::Clip:
  646. values.clip(ComputeClip(p));
  647. break;
  648. case PropertyId::Visibility:
  649. values.visibility((Visibility)p->Get< int >());
  650. break;
  651. case PropertyId::BackgroundColor:
  652. values.background_color(p->Get<Colourb>());
  653. break;
  654. case PropertyId::Color:
  655. values.color(p->Get<Colourb>());
  656. break;
  657. case PropertyId::ImageColor:
  658. values.image_color(p->Get<Colourb>());
  659. break;
  660. case PropertyId::Opacity:
  661. values.opacity(p->Get<float>());
  662. break;
  663. case PropertyId::FontFamily:
  664. // Fetched from element's properties.
  665. dirty_font_face_handle = true;
  666. break;
  667. case PropertyId::FontStyle:
  668. values.font_style((FontStyle)p->Get< int >());
  669. dirty_font_face_handle = true;
  670. break;
  671. case PropertyId::FontWeight:
  672. values.font_weight((FontWeight)p->Get< int >());
  673. dirty_font_face_handle = true;
  674. break;
  675. case PropertyId::FontSize:
  676. // (font-size computed above)
  677. dirty_font_face_handle = true;
  678. break;
  679. case PropertyId::TextAlign:
  680. values.text_align((TextAlign)p->Get<int>());
  681. break;
  682. case PropertyId::TextDecoration:
  683. values.text_decoration((TextDecoration)p->Get<int>());
  684. break;
  685. case PropertyId::TextTransform:
  686. values.text_transform((TextTransform)p->Get<int>());
  687. break;
  688. case PropertyId::WhiteSpace:
  689. values.white_space((WhiteSpace)p->Get<int>());
  690. break;
  691. case PropertyId::WordBreak:
  692. values.word_break((WordBreak)p->Get<int>());
  693. break;
  694. case PropertyId::RowGap:
  695. values.row_gap(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  696. break;
  697. case PropertyId::ColumnGap:
  698. values.column_gap(ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  699. break;
  700. case PropertyId::Drag:
  701. values.drag((Drag)p->Get< int >());
  702. break;
  703. case PropertyId::TabIndex:
  704. values.tab_index((TabIndex)p->Get< int >());
  705. break;
  706. case PropertyId::Focus:
  707. values.focus((Focus)p->Get<int>());
  708. break;
  709. case PropertyId::ScrollbarMargin:
  710. values.scrollbar_margin(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  711. break;
  712. case PropertyId::PointerEvents:
  713. values.pointer_events((PointerEvents)p->Get<int>());
  714. break;
  715. case PropertyId::Perspective:
  716. values.perspective(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  717. break;
  718. case PropertyId::PerspectiveOriginX:
  719. values.perspective_origin_x(ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  720. break;
  721. case PropertyId::PerspectiveOriginY:
  722. values.perspective_origin_y(ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  723. break;
  724. case PropertyId::TransformOriginX:
  725. values.transform_origin_x(ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  726. break;
  727. case PropertyId::TransformOriginY:
  728. values.transform_origin_y(ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  729. break;
  730. case PropertyId::TransformOriginZ:
  731. values.transform_origin_z(ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  732. break;
  733. case PropertyId::Decorator:
  734. values.has_decorator(p->unit == Property::DECORATOR);
  735. break;
  736. case PropertyId::FontEffect:
  737. values.has_font_effect((p->unit == Property::FONTEFFECT));
  738. break;
  739. case PropertyId::FlexBasis:
  740. values.flex_basis(ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions));
  741. break;
  742. // Fetched from element's properties.
  743. case PropertyId::Cursor:
  744. case PropertyId::Transform:
  745. case PropertyId::Transition:
  746. case PropertyId::Animation:
  747. case PropertyId::AlignContent:
  748. case PropertyId::AlignItems:
  749. case PropertyId::AlignSelf:
  750. case PropertyId::FlexDirection:
  751. case PropertyId::FlexGrow:
  752. case PropertyId::FlexShrink:
  753. case PropertyId::FlexWrap:
  754. case PropertyId::JustifyContent:
  755. break;
  756. // Unhandled properties. Must be manually retrieved with 'GetProperty()'.
  757. case PropertyId::FillImage:
  758. case PropertyId::CaretColor:
  759. break;
  760. // Invalid properties
  761. case PropertyId::Invalid:
  762. case PropertyId::NumDefinedIds:
  763. case PropertyId::MaxNumIds:
  764. break;
  765. }
  766. }
  767. // The font-face handle is nulled when local font properties are set. In that case we need to retrieve a new handle.
  768. if (dirty_font_face_handle)
  769. {
  770. RMLUI_ZoneScopedN("FontFaceHandle");
  771. values.font_face_handle(
  772. GetFontEngineInterface()->GetFontFaceHandle(values.font_family(), values.font_style(), values.font_weight(), (int)values.font_size()));
  773. }
  774. // Next, pass inheritable dirty properties onto our children
  775. PropertyIdSet dirty_inherited_properties = (dirty_properties & StyleSheetSpecification::GetRegisteredInheritedProperties());
  776. if (!dirty_inherited_properties.Empty())
  777. {
  778. for (int i = 0; i < element->GetNumChildren(true); i++)
  779. {
  780. auto child = element->GetChild(i);
  781. child->GetStyle()->dirty_properties |= dirty_inherited_properties;
  782. }
  783. }
  784. PropertyIdSet result(std::move(dirty_properties));
  785. dirty_properties.Clear();
  786. return result;
  787. }
  788. } // namespace Rml