ElementStyle.cpp 30 KB

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