ElementStyle.cpp 28 KB

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