ElementStyle.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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/StyleSheetSpecification.h"
  39. #include "../../Include/RmlUi/Core/TransformPrimitive.h"
  40. #include "ElementBackground.h"
  41. #include "ElementBorder.h"
  42. #include "ElementDecoration.h"
  43. #include "ElementDefinition.h"
  44. #include "FontFaceHandle.h"
  45. #include "ComputeProperty.h"
  46. #include "DirtyPropertyList.h"
  47. #include "PropertiesIterator.h"
  48. namespace Rml {
  49. namespace Core {
  50. ElementStyle::ElementStyle(Element* _element) : dirty_properties(true)
  51. {
  52. definition = nullptr;
  53. element = _element;
  54. definition_dirty = true;
  55. }
  56. const ElementDefinition* ElementStyle::GetDefinition() const
  57. {
  58. return definition.get();
  59. }
  60. // Returns one of this element's properties.
  61. const Property* ElementStyle::GetLocalProperty(PropertyId id, const PropertyDictionary& inline_properties, const ElementDefinition* definition)
  62. {
  63. // Check for overriding local properties.
  64. const Property* property = inline_properties.GetProperty(id);
  65. if (property)
  66. return property;
  67. // Check for a property defined in an RCSS rule.
  68. if (definition)
  69. return definition->GetProperty(id);
  70. return nullptr;
  71. }
  72. // Returns one of this element's properties.
  73. const Property* ElementStyle::GetProperty(PropertyId id, const Element* element, const PropertyDictionary& inline_properties, const ElementDefinition* definition)
  74. {
  75. const Property* local_property = GetLocalProperty(id, inline_properties, definition);
  76. if (local_property)
  77. return local_property;
  78. // Fetch the property specification.
  79. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(id);
  80. if (!property)
  81. return nullptr;
  82. // If we can inherit this property, return our parent's property.
  83. if (property->IsInherited())
  84. {
  85. Element* parent = element->GetParentNode();
  86. while (parent)
  87. {
  88. const Property* parent_property = parent->GetStyle()->GetLocalProperty(id);
  89. if (parent_property)
  90. return parent_property;
  91. parent = parent->GetParentNode();
  92. }
  93. }
  94. // No property available! Return the default value.
  95. return property->GetDefaultValue();
  96. }
  97. // Apply transition to relevant properties if a transition is defined on element.
  98. // Properties that are part of a transition are removed from the properties list.
  99. void ElementStyle::TransitionPropertyChanges(Element* element, PropertyNameList& properties, const PropertyDictionary& inline_properties, const ElementDefinition* old_definition, const ElementDefinition* new_definition)
  100. {
  101. RMLUI_ASSERT(element);
  102. if (!old_definition || !new_definition || properties.empty())
  103. return;
  104. // 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.
  105. // Now that we have the concept of computed values, we may want do this operation directly on them instead.
  106. if (const Property* transition_property = GetLocalProperty(PropertyId::Transition, inline_properties, new_definition))
  107. {
  108. auto transition_list = transition_property->Get<TransitionList>();
  109. if (!transition_list.none)
  110. {
  111. static const PropertyDictionary empty_properties;
  112. auto add_transition = [&](const Transition& transition) {
  113. bool transition_added = false;
  114. const Property* start_value = GetProperty(transition.id, element, inline_properties, old_definition);
  115. const Property* target_value = GetProperty(transition.id, element, empty_properties, new_definition);
  116. if (start_value && target_value && (*start_value != *target_value))
  117. transition_added = element->StartTransition(transition, *start_value, *target_value);
  118. return transition_added;
  119. };
  120. if (transition_list.all)
  121. {
  122. Transition transition = transition_list.transitions[0];
  123. for (auto it = properties.begin(); it != properties.end(); )
  124. {
  125. transition.id = *it;
  126. if (add_transition(transition))
  127. it = properties.erase(it);
  128. else
  129. ++it;
  130. }
  131. }
  132. else
  133. {
  134. for (auto& transition : transition_list.transitions)
  135. {
  136. auto it = properties.find(transition.id);
  137. if (it != properties.end())
  138. {
  139. if (add_transition(transition))
  140. properties.erase(it);
  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.DirtyAll();
  163. definition = new_definition;
  164. }
  165. else if (new_definition != definition)
  166. {
  167. PropertyNameList 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. for (auto it = changed_properties.begin(); it != changed_properties.end();)
  176. {
  177. PropertyId id = *it;
  178. const Property* p0 = definition->GetProperty(id);
  179. const Property* p1 = new_definition->GetProperty(id);
  180. if (p0 && p1 && *p0 == *p1)
  181. it = changed_properties.erase(it);
  182. else
  183. ++it;
  184. }
  185. }
  186. TransitionPropertyChanges(element, changed_properties, inline_properties, definition.get(), new_definition.get());
  187. definition = new_definition;
  188. DirtyProperties(changed_properties);
  189. }
  190. // Even if the definition was not changed, the child definitions may have changed as a result of anything that
  191. // could change the definition of this element, such as a new pseudo class.
  192. DirtyChildDefinitions();
  193. }
  194. }
  195. // Sets or removes a pseudo-class on the element.
  196. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  197. {
  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 (pseudo_classes.count(pseudo_class) == 1);
  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. inline_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 = inline_properties.GetNumProperties();
  279. inline_properties.RemoveProperty(id);
  280. if(inline_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, inline_properties, definition.get());
  287. }
  288. // Returns one of this element's properties.
  289. const Property* ElementStyle::GetLocalProperty(PropertyId id) const
  290. {
  291. return GetLocalProperty(id, inline_properties, definition.get());
  292. }
  293. const PropertyMap& ElementStyle::GetLocalStyleProperties() const
  294. {
  295. return inline_properties.GetProperties();
  296. }
  297. float ElementStyle::ResolveNumberLengthPercentage(const Property * property, RelativeTarget relative_target) const
  298. {
  299. // There is an exception on font-size properties, as 'em' units here refer to parent font size instead
  300. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && relative_target == RelativeTarget::ParentFontSize))
  301. {
  302. float result = ComputeLength(property, element->GetComputedValues().font_size, element->GetOwnerDocument()->GetComputedValues().font_size, ElementUtilities::GetDensityIndependentPixelRatio(element));
  303. return result;
  304. }
  305. float base_value = 0.0f;
  306. switch (relative_target)
  307. {
  308. case RelativeTarget::None:
  309. base_value = 1.0f;
  310. break;
  311. case RelativeTarget::ContainingBlockWidth:
  312. base_value = element->GetContainingBlock().x;
  313. break;
  314. case RelativeTarget::ContainingBlockHeight:
  315. base_value = element->GetContainingBlock().y;
  316. break;
  317. case RelativeTarget::FontSize:
  318. base_value = element->GetComputedValues().font_size;
  319. break;
  320. case RelativeTarget::ParentFontSize:
  321. base_value = element->GetParentNode()->GetComputedValues().font_size;
  322. break;
  323. case RelativeTarget::LineHeight:
  324. base_value = element->GetLineHeight();
  325. break;
  326. default:
  327. break;
  328. }
  329. float scale_value = 0.0f;
  330. switch (property->unit)
  331. {
  332. case Property::EM:
  333. case Property::NUMBER:
  334. scale_value = property->value.Get< float >();
  335. break;
  336. case Property::PERCENT:
  337. scale_value = property->value.Get< float >() * 0.01f;
  338. break;
  339. default:
  340. break;
  341. }
  342. return base_value * scale_value;
  343. }
  344. // Resolves one of this element's properties.
  345. float ElementStyle::ResolveLengthPercentage(const Property* property, float base_value) const
  346. {
  347. if (!property)
  348. {
  349. RMLUI_ERROR;
  350. return 0.0f;
  351. }
  352. RMLUI_ASSERT(property->unit & Property::LENGTH_PERCENT);
  353. const float font_size = element->GetComputedValues().font_size;
  354. const float doc_font_size = element->GetOwnerDocument()->GetComputedValues().font_size;
  355. const float dp_ratio = ElementUtilities::GetDensityIndependentPixelRatio(element);
  356. Style::LengthPercentage computed = ComputeLengthPercentage(property, font_size, doc_font_size, dp_ratio);
  357. float result = ResolveValue(computed, base_value);
  358. return result;
  359. }
  360. void ElementStyle::DirtyDefinition()
  361. {
  362. definition_dirty = true;
  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 PropertyNameList& properties)
  416. {
  417. dirty_properties.Insert(properties);
  418. }
  419. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  420. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  421. {
  422. dirty_properties.Insert(properties);
  423. }
  424. static void DirtyEmProperties(DirtyPropertyList& dirty_properties, Element* element)
  425. {
  426. // Either we can dirty every property, or we can iterate over all properties and see if anyone uses em-units.
  427. // Choose whichever is fastest based on benchmarking.
  428. #if 1
  429. // Dirty every property
  430. dirty_properties.DirtyAll();
  431. #else
  432. if (dirty_properties.AllDirty())
  433. return;
  434. // Check if any of these are currently em-relative. If so, dirty them.
  435. for (auto& property_name : StyleSheetSpecification::GetRegisteredProperties())
  436. {
  437. // Skip font-size; this is relative to our parent's em, not ours.
  438. if (property_name == FONT_SIZE)
  439. continue;
  440. // Get this property from this element. If this is em-relative, then add it to the list to
  441. // dirty.
  442. if (element->GetProperty(property_name)->unit == Property::EM)
  443. dirty_properties.Insert(property_name);
  444. }
  445. #endif
  446. }
  447. DirtyPropertyList ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio)
  448. {
  449. if (dirty_properties.Empty())
  450. return DirtyPropertyList();
  451. // Generally, this is how it works:
  452. // 1. Assign default values (clears any removed properties)
  453. // 2. Inherit inheritable values from parent
  454. // 3. Assign any local properties (from inline style or stylesheet)
  455. const float font_size_before = values.font_size;
  456. const Style::LineHeight line_height_before = values.line_height;
  457. // The next flag is just a small optimization, if the element was just created we don't need to copy all the default values.
  458. if (!values_are_default_initialized)
  459. {
  460. // This needs to be done in case some properties were removed and thus not in our local style anymore.
  461. // If we skipped this, the old dirty value would be unmodified, instead, now it is set to its default value.
  462. // Strictly speaking, we only really need to do this for the dirty values, and only non-inherited. However,
  463. // it seems assigning the whole thing is faster in most cases.
  464. values = DefaultComputedValues;
  465. }
  466. // Always do font-size first if dirty, because of em-relative values
  467. if(dirty_properties.Contains(PropertyId::FontSize))
  468. {
  469. if (auto p = GetLocalProperty(PropertyId::FontSize))
  470. values.font_size = ComputeFontsize(*p, values, parent_values, document_values, dp_ratio);
  471. else if (parent_values)
  472. values.font_size = parent_values->font_size;
  473. if (font_size_before != values.font_size)
  474. DirtyEmProperties(dirty_properties, element);
  475. }
  476. else
  477. {
  478. values.font_size = font_size_before;
  479. }
  480. const float font_size = values.font_size;
  481. const float document_font_size = (document_values ? document_values->font_size : DefaultComputedValues.font_size);
  482. // Since vertical-align depends on line-height we compute this before iteration
  483. if(dirty_properties.Contains(PropertyId::LineHeight))
  484. {
  485. if (auto p = GetLocalProperty(PropertyId::LineHeight))
  486. {
  487. values.line_height = ComputeLineHeight(p, font_size, document_font_size, dp_ratio);
  488. }
  489. else if (parent_values)
  490. {
  491. // 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.
  492. // See CSS specs for details. Percent is already converted to number.
  493. if (parent_values->line_height.inherit_type == Style::LineHeight::Number)
  494. values.line_height = Style::LineHeight(font_size * parent_values->line_height.inherit_value, Style::LineHeight::Number, parent_values->line_height.inherit_value);
  495. else
  496. values.line_height = parent_values->line_height;
  497. }
  498. if(line_height_before.value != values.line_height.value || line_height_before.inherit_value != values.line_height.inherit_value)
  499. dirty_properties.Insert(PropertyId::VerticalAlign);
  500. }
  501. else
  502. {
  503. values.line_height = line_height_before;
  504. }
  505. if (parent_values)
  506. {
  507. // Inherited properties are copied here, but may be overwritten below by locally defined properties
  508. // Line-height and font-size are computed above
  509. values.clip = parent_values->clip;
  510. values.color = parent_values->color;
  511. values.opacity = parent_values->opacity;
  512. values.font_family = parent_values->font_family;
  513. values.font_charset = parent_values->font_charset;
  514. values.font_style = parent_values->font_style;
  515. values.font_weight = parent_values->font_weight;
  516. values.text_align = parent_values->text_align;
  517. values.text_decoration = parent_values->text_decoration;
  518. values.text_transform = parent_values->text_transform;
  519. values.white_space = parent_values->white_space;
  520. values.cursor = parent_values->cursor;
  521. values.focus = parent_values->focus;
  522. values.pointer_events = parent_values->pointer_events;
  523. values.font_effect = parent_values->font_effect;
  524. }
  525. for (auto it = Iterate(); !it.AtEnd(); ++it)
  526. {
  527. auto name_property_pair = *it;
  528. const PropertyId id = name_property_pair.first;
  529. const Property* p = &name_property_pair.second;
  530. using namespace Style;
  531. switch (id)
  532. {
  533. case PropertyId::MarginTop:
  534. values.margin_top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  535. break;
  536. case PropertyId::MarginRight:
  537. values.margin_right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  538. break;
  539. case PropertyId::MarginBottom:
  540. values.margin_bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  541. break;
  542. case PropertyId::MarginLeft:
  543. values.margin_left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  544. break;
  545. case PropertyId::PaddingTop:
  546. values.padding_top = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  547. break;
  548. case PropertyId::PaddingRight:
  549. values.padding_right = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  550. break;
  551. case PropertyId::PaddingBottom:
  552. values.padding_bottom = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  553. break;
  554. case PropertyId::PaddingLeft:
  555. values.padding_left = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  556. break;
  557. case PropertyId::BorderTopWidth:
  558. values.border_top_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  559. break;
  560. case PropertyId::BorderRightWidth:
  561. values.border_right_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  562. break;
  563. case PropertyId::BorderBottomWidth:
  564. values.border_bottom_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  565. break;
  566. case PropertyId::BorderLeftWidth:
  567. values.border_left_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  568. break;
  569. case PropertyId::BorderTopColor:
  570. values.border_top_color = p->Get<Colourb>();
  571. break;
  572. case PropertyId::BorderRightColor:
  573. values.border_right_color = p->Get<Colourb>();
  574. break;
  575. case PropertyId::BorderBottomColor:
  576. values.border_bottom_color = p->Get<Colourb>();
  577. break;
  578. case PropertyId::BorderLeftColor:
  579. values.border_left_color = p->Get<Colourb>();
  580. break;
  581. case PropertyId::Display:
  582. values.display = (Display)p->Get<int>();
  583. break;
  584. case PropertyId::Position:
  585. values.position = (Position)p->Get<int>();
  586. break;
  587. case PropertyId::Top:
  588. values.top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  589. break;
  590. case PropertyId::Right:
  591. values.right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  592. break;
  593. case PropertyId::Bottom:
  594. values.bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  595. break;
  596. case PropertyId::Left:
  597. values.left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  598. break;
  599. case PropertyId::Float:
  600. values.float_ = (Float)p->Get<int>();
  601. break;
  602. case PropertyId::Clear:
  603. values.clear = (Clear)p->Get<int>();
  604. break;
  605. case PropertyId::ZIndex:
  606. values.z_index = (p->unit == Property::KEYWORD ? ZIndex(ZIndex::Auto) : ZIndex(ZIndex::Number, p->Get<float>()));
  607. break;
  608. case PropertyId::Width:
  609. values.width = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  610. break;
  611. case PropertyId::MinWidth:
  612. values.min_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  613. break;
  614. case PropertyId::MaxWidth:
  615. values.max_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  616. break;
  617. case PropertyId::Height:
  618. values.height = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  619. break;
  620. case PropertyId::MinHeight:
  621. values.min_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  622. break;
  623. case PropertyId::MaxHeight:
  624. values.max_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  625. break;
  626. case PropertyId::LineHeight:
  627. // (Line-height computed above)
  628. break;
  629. case PropertyId::VerticalAlign:
  630. values.vertical_align = ComputeVerticalAlign(p, values.line_height.value, font_size, document_font_size, dp_ratio);
  631. break;
  632. case PropertyId::OverflowX:
  633. values.overflow_x = (Overflow)p->Get< int >();
  634. break;
  635. case PropertyId::OverflowY:
  636. values.overflow_y = (Overflow)p->Get< int >();
  637. break;
  638. case PropertyId::Clip:
  639. values.clip = ComputeClip(p);
  640. break;
  641. case PropertyId::Visibility:
  642. values.visibility = (Visibility)p->Get< int >();
  643. break;
  644. case PropertyId::BackgroundColor:
  645. values.background_color = p->Get<Colourb>();
  646. break;
  647. case PropertyId::Color:
  648. values.color = p->Get<Colourb>();
  649. break;
  650. case PropertyId::ImageColor:
  651. values.image_color = p->Get<Colourb>();
  652. break;
  653. case PropertyId::Opacity:
  654. values.opacity = p->Get<float>();
  655. break;
  656. case PropertyId::FontFamily:
  657. values.font_family = ToLower(p->Get<String>());
  658. break;
  659. case PropertyId::FontCharset:
  660. values.font_charset = p->Get<String>();
  661. break;
  662. case PropertyId::FontStyle:
  663. values.font_style = (FontStyle)p->Get< int >();
  664. break;
  665. case PropertyId::FontWeight:
  666. values.font_weight = (FontWeight)p->Get< int >();
  667. break;
  668. case PropertyId::FontSize:
  669. // (font-size computed above)
  670. break;
  671. case PropertyId::TextAlign:
  672. values.text_align = (TextAlign)p->Get< int >();
  673. break;
  674. case PropertyId::TextDecoration:
  675. values.text_decoration = (TextDecoration)p->Get< int >();
  676. break;
  677. case PropertyId::TextTransform:
  678. values.text_transform = (TextTransform)p->Get< int >();
  679. break;
  680. case PropertyId::WhiteSpace:
  681. values.white_space = (WhiteSpace)p->Get< int >();
  682. break;
  683. case PropertyId::Cursor:
  684. values.cursor = p->Get< String >();
  685. break;
  686. case PropertyId::Drag:
  687. values.drag = (Drag)p->Get< int >();
  688. break;
  689. case PropertyId::TabIndex:
  690. values.tab_index = (TabIndex)p->Get< int >();
  691. break;
  692. case PropertyId::Focus:
  693. values.focus = (Focus)p->Get<int>();
  694. break;
  695. case PropertyId::ScrollbarMargin:
  696. values.scrollbar_margin = ComputeLength(p, font_size, document_font_size, dp_ratio);
  697. break;
  698. case PropertyId::PointerEvents:
  699. values.pointer_events = (PointerEvents)p->Get<int>();
  700. break;
  701. case PropertyId::Perspective:
  702. values.perspective = ComputeLength(p, font_size, document_font_size, dp_ratio);
  703. break;
  704. case PropertyId::PerspectiveOriginX:
  705. values.perspective_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  706. break;
  707. case PropertyId::PerspectiveOriginY:
  708. values.perspective_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  709. break;
  710. case PropertyId::Transform:
  711. values.transform = p->Get<TransformPtr>();
  712. break;
  713. case PropertyId::TransformOriginX:
  714. values.transform_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  715. break;
  716. case PropertyId::TransformOriginY:
  717. values.transform_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  718. break;
  719. case PropertyId::TransformOriginZ:
  720. values.transform_origin_z = ComputeLength(p, font_size, document_font_size, dp_ratio);
  721. break;
  722. case PropertyId::Transition:
  723. values.transition = p->Get<TransitionList>();
  724. break;
  725. case PropertyId::Animation:
  726. values.animation = p->Get<AnimationList>();
  727. break;
  728. case PropertyId::Decorator:
  729. values.decorator.clear();
  730. if (p->unit == Property::DECORATOR)
  731. {
  732. values.decorator = p->Get<DecoratorList>();
  733. }
  734. else if (p->unit == Property::STRING)
  735. {
  736. // Usually the decorator is converted from string after the style sheet is set on the ElementDocument. However, if the
  737. // user sets a decorator on the element's style, we may still get a string here which must be parsed and instanced.
  738. if(auto& style_sheet = element->GetStyleSheet())
  739. {
  740. String value = p->Get<String>();
  741. values.decorator = style_sheet->InstanceDecoratorsFromString(value, p->source, p->source_line_number);
  742. }
  743. }
  744. break;
  745. case PropertyId::FontEffect:
  746. values.font_effect.reset();
  747. if (p->unit == Property::FONTEFFECT)
  748. {
  749. values.font_effect = p->Get<FontEffectListPtr>();
  750. }
  751. else if (p->unit == Property::STRING)
  752. {
  753. if (auto & style_sheet = element->GetStyleSheet())
  754. {
  755. String value = p->Get<String>();
  756. values.font_effect = style_sheet->InstanceFontEffectsFromString(value, p->source, p->source_line_number);
  757. }
  758. }
  759. break;
  760. default:
  761. break;
  762. }
  763. }
  764. // Next, pass inheritable dirty properties onto our children
  765. // @performance: We might avoid an allocation here in case of dirty non-inherited custom properties. Instead of the initial copy and &=, introduce & operator.
  766. DirtyPropertyList dirty_inherited_properties = dirty_properties;
  767. dirty_inherited_properties &= StyleSheetSpecification::GetRegisteredInheritedPropertyBitList();
  768. if (!dirty_inherited_properties.Empty())
  769. {
  770. for (int i = 0; i < element->GetNumChildren(true); i++)
  771. {
  772. auto child = element->GetChild(i);
  773. child->GetStyle()->dirty_properties |= dirty_inherited_properties;
  774. }
  775. }
  776. DirtyPropertyList result(std::move(dirty_properties));
  777. dirty_properties.Clear();
  778. return result;
  779. }
  780. }
  781. }