ElementStyle.cpp 31 KB

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