ElementStyle.cpp 29 KB

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