ElementStyle.cpp 30 KB

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