ElementStyle.cpp 28 KB

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