ElementStyle.cpp 29 KB

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