ElementStyle.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. ROCKET_ASSERT_NONRECURSIVE;
  496. if (dirty_properties.Empty())
  497. return DirtyPropertyList();
  498. // Generally, this is how it works (for now, we can probably be smarter about this):
  499. // 1. Assign default values (clears any newly dirtied properties)
  500. // 2. Inherit inheritable values from parent
  501. // 3. Assign any local properties (from inline style or stylesheet)
  502. const float font_size_before = values.font_size;
  503. const float line_height_before = values.line_height.value;
  504. // The next flag is just a small optimization, if the element was just created we don't need to copy all the default values.
  505. if (!values_are_default_initialized)
  506. {
  507. values = DefaultComputedValues;
  508. }
  509. // Always do font-size first if dirty, because of em-relative values
  510. {
  511. if (auto p = GetLocalProperty(PropertyId::FontSize))
  512. values.font_size = ComputeFontsize(*p, values, parent_values, document_values, dp_ratio);
  513. else if (parent_values)
  514. values.font_size = parent_values->font_size;
  515. if (font_size_before != values.font_size)
  516. DirtyEmProperties(dirty_properties, element);
  517. }
  518. const float font_size = values.font_size;
  519. const float document_font_size = (document_values ? document_values->font_size : DefaultComputedValues.font_size);
  520. // Since vertical-align depends on line-height we compute this before iteration
  521. {
  522. if (auto p = GetLocalProperty(PropertyId::LineHeight))
  523. {
  524. values.line_height = ComputeLineHeight(p, font_size, document_font_size, dp_ratio);
  525. }
  526. else if (parent_values)
  527. {
  528. // 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.
  529. // See CSS specs for details. Percent is already converted to number.
  530. if (parent_values->line_height.inherit_type == Style::LineHeight::Number)
  531. values.line_height = Style::LineHeight(font_size * parent_values->line_height.inherit_value, Style::LineHeight::Number, parent_values->line_height.inherit_value);
  532. else
  533. values.line_height = parent_values->line_height;
  534. }
  535. if(line_height_before != values.line_height.value)
  536. dirty_properties.Insert(PropertyId::VerticalAlign);
  537. }
  538. if (parent_values)
  539. {
  540. // Inherited properties are copied here, but may be overwritten below by locally defined properties
  541. // Line-height and font-size are computed above
  542. values.clip = parent_values->clip;
  543. values.color = parent_values->color;
  544. values.opacity = parent_values->opacity;
  545. values.font_family = parent_values->font_family;
  546. values.font_charset = parent_values->font_charset;
  547. values.font_style = parent_values->font_style;
  548. values.font_weight = parent_values->font_weight;
  549. values.text_align = parent_values->text_align;
  550. values.text_decoration = parent_values->text_decoration;
  551. values.text_transform = parent_values->text_transform;
  552. values.white_space = parent_values->white_space;
  553. values.cursor = parent_values->cursor;
  554. values.focus = parent_values->focus;
  555. values.pointer_events = parent_values->pointer_events;
  556. }
  557. for (auto it = Iterate(); !it.AtEnd(); ++it)
  558. {
  559. auto name_property_pair = *it;
  560. const PropertyId id = name_property_pair.first;
  561. const Property* p = &name_property_pair.second;
  562. using namespace Style;
  563. switch (id)
  564. {
  565. case PropertyId::MarginTop:
  566. values.margin_top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  567. break;
  568. case PropertyId::MarginRight:
  569. values.margin_right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  570. break;
  571. case PropertyId::MarginBottom:
  572. values.margin_bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  573. break;
  574. case PropertyId::MarginLeft:
  575. values.margin_left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  576. break;
  577. case PropertyId::PaddingTop:
  578. values.padding_top = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  579. break;
  580. case PropertyId::PaddingRight:
  581. values.padding_right = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  582. break;
  583. case PropertyId::PaddingBottom:
  584. values.padding_bottom = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  585. break;
  586. case PropertyId::PaddingLeft:
  587. values.padding_left = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  588. break;
  589. case PropertyId::BorderTopWidth:
  590. values.border_top_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  591. break;
  592. case PropertyId::BorderRightWidth:
  593. values.border_right_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  594. break;
  595. case PropertyId::BorderBottomWidth:
  596. values.border_bottom_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  597. break;
  598. case PropertyId::BorderLeftWidth:
  599. values.border_left_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  600. break;
  601. case PropertyId::BorderTopColor:
  602. values.border_top_color = p->Get<Colourb>();
  603. break;
  604. case PropertyId::BorderRightColor:
  605. values.border_right_color = p->Get<Colourb>();
  606. break;
  607. case PropertyId::BorderBottomColor:
  608. values.border_bottom_color = p->Get<Colourb>();
  609. break;
  610. case PropertyId::BorderLeftColor:
  611. values.border_left_color = p->Get<Colourb>();
  612. break;
  613. case PropertyId::Display:
  614. values.display = (Display)p->Get<int>();
  615. break;
  616. case PropertyId::Position:
  617. values.position = (Position)p->Get<int>();
  618. break;
  619. case PropertyId::Top:
  620. values.top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  621. break;
  622. case PropertyId::Right:
  623. values.right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  624. break;
  625. case PropertyId::Bottom:
  626. values.bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  627. break;
  628. case PropertyId::Left:
  629. values.left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  630. break;
  631. case PropertyId::Float:
  632. values.float_ = (Float)p->Get<int>();
  633. break;
  634. case PropertyId::Clear:
  635. values.clear = (Clear)p->Get<int>();
  636. break;
  637. case PropertyId::ZIndex:
  638. values.z_index = (p->unit == Property::KEYWORD ? ZIndex(ZIndex::Auto) : ZIndex(ZIndex::Number, p->Get<float>()));
  639. break;
  640. case PropertyId::Width:
  641. values.width = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  642. break;
  643. case PropertyId::MinWidth:
  644. values.min_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  645. break;
  646. case PropertyId::MaxWidth:
  647. values.max_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  648. break;
  649. case PropertyId::Height:
  650. values.height = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  651. break;
  652. case PropertyId::MinHeight:
  653. values.min_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  654. break;
  655. case PropertyId::MaxHeight:
  656. values.max_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  657. break;
  658. case PropertyId::LineHeight:
  659. // (Line-height computed above)
  660. break;
  661. case PropertyId::VerticalAlign:
  662. values.vertical_align = ComputeVerticalAlign(p, values.line_height.value, font_size, document_font_size, dp_ratio);
  663. break;
  664. case PropertyId::OverflowX:
  665. values.overflow_x = (Overflow)p->Get< int >();
  666. break;
  667. case PropertyId::OverflowY:
  668. values.overflow_y = (Overflow)p->Get< int >();
  669. break;
  670. case PropertyId::Clip:
  671. values.clip = ComputeClip(p);
  672. break;
  673. case PropertyId::Visibility:
  674. values.visibility = (Visibility)p->Get< int >();
  675. break;
  676. case PropertyId::BackgroundColor:
  677. values.background_color = p->Get<Colourb>();
  678. break;
  679. case PropertyId::Color:
  680. values.color = p->Get<Colourb>();
  681. break;
  682. case PropertyId::ImageColor:
  683. values.image_color = p->Get<Colourb>();
  684. break;
  685. case PropertyId::Opacity:
  686. values.opacity = p->Get<float>();
  687. break;
  688. case PropertyId::FontFamily:
  689. values.font_family = ToLower(p->Get<String>());
  690. break;
  691. case PropertyId::FontCharset:
  692. values.font_charset = p->Get<String>();
  693. break;
  694. case PropertyId::FontStyle:
  695. values.font_style = (FontStyle)p->Get< int >();
  696. break;
  697. case PropertyId::FontWeight:
  698. values.font_weight = (FontWeight)p->Get< int >();
  699. break;
  700. case PropertyId::FontSize:
  701. // (font-size computed above)
  702. break;
  703. case PropertyId::TextAlign:
  704. values.text_align = (TextAlign)p->Get< int >();
  705. break;
  706. case PropertyId::TextDecoration:
  707. values.text_decoration = (TextDecoration)p->Get< int >();
  708. break;
  709. case PropertyId::TextTransform:
  710. values.text_transform = (TextTransform)p->Get< int >();
  711. break;
  712. case PropertyId::WhiteSpace:
  713. values.white_space = (WhiteSpace)p->Get< int >();
  714. break;
  715. case PropertyId::Cursor:
  716. values.cursor = p->Get< String >();
  717. break;
  718. case PropertyId::Drag:
  719. values.drag = (Drag)p->Get< int >();
  720. break;
  721. case PropertyId::TabIndex:
  722. values.tab_index = (TabIndex)p->Get< int >();
  723. break;
  724. case PropertyId::Focus:
  725. values.focus = (Focus)p->Get<int>();
  726. break;
  727. case PropertyId::ScrollbarMargin:
  728. values.scrollbar_margin = ComputeLength(p, font_size, document_font_size, dp_ratio);
  729. break;
  730. case PropertyId::PointerEvents:
  731. values.pointer_events = (PointerEvents)p->Get<int>();
  732. break;
  733. case PropertyId::Perspective:
  734. values.perspective = ComputeLength(p, font_size, document_font_size, dp_ratio);
  735. break;
  736. case PropertyId::PerspectiveOriginX:
  737. values.perspective_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  738. break;
  739. case PropertyId::PerspectiveOriginY:
  740. values.perspective_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  741. break;
  742. case PropertyId::Transform:
  743. values.transform = p->Get<TransformRef>();
  744. break;
  745. case PropertyId::TransformOriginX:
  746. values.transform_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  747. break;
  748. case PropertyId::TransformOriginY:
  749. values.transform_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  750. break;
  751. case PropertyId::TransformOriginZ:
  752. values.transform_origin_z = ComputeLength(p, font_size, document_font_size, dp_ratio);
  753. break;
  754. case PropertyId::Transition:
  755. values.transition = p->Get<TransitionList>();
  756. break;
  757. case PropertyId::Animation:
  758. values.animation = p->Get<AnimationList>();
  759. break;
  760. case PropertyId::Decorator:
  761. values.decorator.clear();
  762. if (p->unit == Property::DECORATOR)
  763. {
  764. values.decorator = p->Get<DecoratorList>();
  765. }
  766. else if (p->unit == Property::STRING)
  767. {
  768. // Usually the decorator is converted from string after the style sheet is set on the ElementDocument. However, if the
  769. // user sets a decorator on the element's style, we may still get a string here which must be parsed and instanced.
  770. if(const StyleSheet* style_sheet = GetStyleSheet())
  771. {
  772. String value = p->Get<String>();
  773. values.decorator = style_sheet->InstanceDecoratorsFromString(value, p->source, p->source_line_number);
  774. }
  775. }
  776. break;
  777. case PropertyId::FontEffect:
  778. break;
  779. }
  780. }
  781. // Next, pass inheritable dirty properties onto our children
  782. // Static to avoid repeated allocations, requires non-recursion.
  783. static PropertyNameList dirty_inherited;
  784. dirty_inherited.clear();
  785. bool all_inherited_dirty = false;
  786. if (dirty_properties.IsAllDirty())
  787. {
  788. all_inherited_dirty = true;
  789. }
  790. else
  791. {
  792. // Find all dirtied properties which are also inherited
  793. const auto& inherited_properties = StyleSheetSpecification::GetRegisteredInheritedProperties();
  794. for (PropertyId id : inherited_properties)
  795. dirty_properties.Insert(id);
  796. }
  797. if (all_inherited_dirty || dirty_inherited.size() > 0)
  798. {
  799. // Dirty inherited properties in our children
  800. const auto& dirty_inherited_ref = (all_inherited_dirty ? StyleSheetSpecification::GetRegisteredInheritedProperties() : dirty_inherited);
  801. for (int i = 0; i < element->GetNumChildren(true); i++)
  802. {
  803. auto child = element->GetChild(i);
  804. child->GetStyle()->dirty_properties.Insert(dirty_inherited_ref);
  805. }
  806. }
  807. DirtyPropertyList result(std::move(dirty_properties));
  808. dirty_properties.Clear();
  809. return result;
  810. }
  811. }
  812. }