ElementStyle.cpp 29 KB

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