ElementStyle.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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(const String& name, 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(name);
  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(name, pseudo_classes);
  79. return NULL;
  80. }
  81. // Returns one of this element's properties.
  82. const Property* ElementStyle::GetProperty(const String& name, Element* element, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  83. {
  84. const Property* local_property = GetLocalProperty(name, 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(name);
  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(name);
  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(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.name, element, local_properties, old_definition, pseudo_classes_before);
  122. const Property* target_value = GetProperty(transition.name, 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.name = *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.name);
  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 = NULL;
  160. const StyleSheet* style_sheet = GetStyleSheet();
  161. if (style_sheet != NULL)
  162. {
  163. new_definition = style_sheet->GetElementDefinition(element);
  164. }
  165. // Switch the property definitions if the definition has changed.
  166. if (new_definition != definition || new_definition == NULL)
  167. {
  168. PropertyNameList properties;
  169. if (definition != NULL)
  170. definition->GetDefinedProperties(properties, pseudo_classes);
  171. if (new_definition != NULL)
  172. new_definition->GetDefinedProperties(properties, pseudo_classes);
  173. TransitionPropertyChanges(element, properties, local_properties, definition, new_definition, pseudo_classes, pseudo_classes);
  174. if (definition != NULL)
  175. definition->RemoveReference();
  176. definition = new_definition;
  177. // @performance: It may be faster to dirty all properties
  178. //dirty_properties.SetAllDirty();
  179. DirtyProperties(properties);
  180. element->GetElementDecoration()->DirtyDecorators(true);
  181. }
  182. else if (new_definition != NULL)
  183. {
  184. new_definition->RemoveReference();
  185. }
  186. }
  187. }
  188. // Sets or removes a pseudo-class on the element.
  189. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  190. {
  191. size_t num_pseudo_classes = pseudo_classes.size();
  192. auto pseudo_classes_before = pseudo_classes;
  193. if (activate)
  194. pseudo_classes.push_back(pseudo_class);
  195. else
  196. {
  197. // In case of duplicates, we do a loop here. We could do a sort and unique instead,
  198. // but that might even be slower for a small list with few duplicates, which
  199. // is probably the most common case.
  200. auto it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  201. while(it != pseudo_classes.end())
  202. {
  203. pseudo_classes.erase(it);
  204. it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  205. }
  206. }
  207. if (pseudo_classes.size() != num_pseudo_classes)
  208. {
  209. element->GetElementDecoration()->DirtyDecorators(false);
  210. if (definition != NULL)
  211. {
  212. PropertyNameList properties;
  213. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  214. TransitionPropertyChanges(element, properties, local_properties, definition, definition, pseudo_classes_before, pseudo_classes);
  215. DirtyProperties(properties);
  216. switch (definition->GetPseudoClassVolatility(pseudo_class))
  217. {
  218. case ElementDefinition::FONT_VOLATILE:
  219. element->DirtyFont();
  220. break;
  221. case ElementDefinition::STRUCTURE_VOLATILE:
  222. DirtyChildDefinitions();
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. // Checks if a specific pseudo-class has been set on the element.
  231. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  232. {
  233. return (std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class) != pseudo_classes.end());
  234. }
  235. const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
  236. {
  237. return pseudo_classes;
  238. }
  239. // Sets or removes a class on the element.
  240. void ElementStyle::SetClass(const String& class_name, bool activate)
  241. {
  242. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  243. if (activate)
  244. {
  245. if (class_location == classes.end())
  246. {
  247. classes.push_back(class_name);
  248. DirtyDefinition();
  249. }
  250. }
  251. else
  252. {
  253. if (class_location != classes.end())
  254. {
  255. classes.erase(class_location);
  256. DirtyDefinition();
  257. }
  258. }
  259. }
  260. // Checks if a class is set on the element.
  261. bool ElementStyle::IsClassSet(const String& class_name) const
  262. {
  263. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  264. }
  265. // Specifies the entire list of classes for this element. This will replace any others specified.
  266. void ElementStyle::SetClassNames(const String& class_names)
  267. {
  268. classes.clear();
  269. StringUtilities::ExpandString(classes, class_names, ' ');
  270. DirtyDefinition();
  271. }
  272. // Returns the list of classes specified for this element.
  273. String ElementStyle::GetClassNames() const
  274. {
  275. String class_names;
  276. for (size_t i = 0; i < classes.size(); i++)
  277. {
  278. if (i != 0)
  279. {
  280. class_names += " ";
  281. }
  282. class_names += classes[i];
  283. }
  284. return class_names;
  285. }
  286. // Sets a local property override on the element.
  287. bool ElementStyle::SetProperty(const String& name, const String& value)
  288. {
  289. if (local_properties == NULL)
  290. local_properties = new PropertyDictionary();
  291. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  292. {
  293. DirtyProperty(name);
  294. return true;
  295. }
  296. else
  297. {
  298. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.c_str(), value.c_str());
  299. return false;
  300. }
  301. }
  302. // Sets a local property override on the element to a pre-parsed value.
  303. bool ElementStyle::SetProperty(const String& name, const Property& property)
  304. {
  305. Property new_property = property;
  306. new_property.definition = StyleSheetSpecification::GetProperty(name);
  307. if (new_property.definition == NULL)
  308. return false;
  309. if (local_properties == NULL)
  310. local_properties = new PropertyDictionary();
  311. local_properties->SetProperty(name, new_property);
  312. DirtyProperty(name);
  313. return true;
  314. }
  315. // Removes a local property override on the element.
  316. void ElementStyle::RemoveProperty(const String& name)
  317. {
  318. if (local_properties == NULL)
  319. return;
  320. if (local_properties->GetProperty(name) != NULL)
  321. {
  322. local_properties->RemoveProperty(name);
  323. DirtyProperty(name);
  324. }
  325. }
  326. // Returns one of this element's properties.
  327. const Property* ElementStyle::GetProperty(const String& name)
  328. {
  329. return GetProperty(name, element, local_properties, definition, pseudo_classes);
  330. }
  331. // Returns one of this element's properties.
  332. const Property* ElementStyle::GetLocalProperty(const String& name)
  333. {
  334. return GetLocalProperty(name, local_properties, definition, pseudo_classes);
  335. }
  336. const PropertyMap * ElementStyle::GetLocalProperties() const
  337. {
  338. if (local_properties)
  339. return &local_properties->GetProperties();
  340. return NULL;
  341. }
  342. float ElementStyle::ResolveNumberLengthPercentage(const Property * property, RelativeTarget relative_target)
  343. {
  344. // There is an exception on font-size properties, as 'em' units here refer to parent font size instead
  345. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && relative_target == RelativeTarget::ParentFontSize))
  346. {
  347. float result = ComputeLength(property, element->GetComputedValues().font_size, element->GetOwnerDocument()->GetComputedValues().font_size, ElementUtilities::GetDensityIndependentPixelRatio(element));
  348. return result;
  349. }
  350. float base_value = 0.0f;
  351. switch (relative_target)
  352. {
  353. case RelativeTarget::None:
  354. base_value = 1.0f;
  355. break;
  356. case RelativeTarget::ContainingBlockWidth:
  357. base_value = element->GetContainingBlock().x;
  358. break;
  359. case RelativeTarget::ContainingBlockHeight:
  360. base_value = element->GetContainingBlock().y;
  361. break;
  362. case RelativeTarget::FontSize:
  363. base_value = element->GetComputedValues().font_size;
  364. break;
  365. case RelativeTarget::ParentFontSize:
  366. base_value = element->GetParentNode()->GetComputedValues().font_size;
  367. break;
  368. case RelativeTarget::LineHeight:
  369. base_value = element->GetLineHeight();
  370. break;
  371. default:
  372. break;
  373. }
  374. float scale_value = 0.0f;
  375. switch (property->unit)
  376. {
  377. case Property::EM:
  378. case Property::NUMBER:
  379. scale_value = property->value.Get< float >();
  380. break;
  381. case Property::PERCENT:
  382. scale_value = property->value.Get< float >() * 0.01f;
  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. // Iterates over the properties defined on the element.
  404. bool ElementStyle::IterateProperties(int& index, String& name, const Property*& property, const PseudoClassList** property_pseudo_classes) const
  405. {
  406. // First check for locally defined properties.
  407. if (local_properties != NULL)
  408. {
  409. if (index < local_properties->GetNumProperties())
  410. {
  411. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  412. for (int count = 0; count < index; ++count)
  413. ++i;
  414. name = (*i).first;
  415. property = &((*i).second);
  416. if (property_pseudo_classes)
  417. * property_pseudo_classes = nullptr;
  418. ++index;
  419. return true;
  420. }
  421. }
  422. if (definition != NULL)
  423. {
  424. int index_offset = 0;
  425. if (local_properties != NULL)
  426. index_offset = local_properties->GetNumProperties();
  427. // Offset the index to be relative to the definition before we start indexing. When we do get a property back,
  428. // check that it hasn't been overridden by the element's local properties; if so, continue on to the next one.
  429. index -= index_offset;
  430. while (definition->IterateProperties(index, pseudo_classes, name, property, property_pseudo_classes))
  431. {
  432. if (local_properties == NULL ||
  433. local_properties->GetProperty(name) == NULL)
  434. {
  435. index += index_offset;
  436. return true;
  437. }
  438. }
  439. return false;
  440. }
  441. return false;
  442. }
  443. // Returns the active style sheet for this element. This may be NULL.
  444. StyleSheet* ElementStyle::GetStyleSheet() const
  445. {
  446. ElementDocument* document = element->GetOwnerDocument();
  447. if (document != NULL)
  448. return document->GetStyleSheet();
  449. return NULL;
  450. }
  451. void ElementStyle::DirtyDefinition()
  452. {
  453. definition_dirty = true;
  454. DirtyChildDefinitions();
  455. }
  456. void ElementStyle::DirtyChildDefinitions()
  457. {
  458. for (int i = 0; i < element->GetNumChildren(true); i++)
  459. element->GetChild(i)->GetStyle()->DirtyDefinition();
  460. }
  461. // Dirties em-relative properties.
  462. void ElementStyle::DirtyEmProperties()
  463. {
  464. dirty_properties.SetEmRelativeDirty();
  465. }
  466. // Dirties rem properties.
  467. void ElementStyle::DirtyRemProperties()
  468. {
  469. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  470. PropertyNameList rem_properties;
  471. // Dirty all the properties of this element that use the rem unit.
  472. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  473. {
  474. if (element->GetProperty(*list_iterator)->unit == Property::REM)
  475. rem_properties.insert(*list_iterator);
  476. }
  477. if (!rem_properties.empty())
  478. DirtyProperties(rem_properties);
  479. // Now dirty all of our descendant's properties that use the rem unit.
  480. int num_children = element->GetNumChildren(true);
  481. for (int i = 0; i < num_children; ++i)
  482. element->GetChild(i)->GetStyle()->DirtyRemProperties();
  483. }
  484. void ElementStyle::DirtyDpProperties()
  485. {
  486. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  487. PropertyNameList dp_properties;
  488. // Dirty all the properties of this element that use the dp unit.
  489. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  490. {
  491. if (element->GetProperty(*list_iterator)->unit == Property::DP)
  492. dp_properties.insert(*list_iterator);
  493. }
  494. if (!dp_properties.empty())
  495. DirtyProperties(dp_properties);
  496. // Now dirty all of our descendant's properties that use the dp unit.
  497. int num_children = element->GetNumChildren(true);
  498. for (int i = 0; i < num_children; ++i)
  499. element->GetChild(i)->GetStyle()->DirtyDpProperties();
  500. }
  501. // Sets a single property as dirty.
  502. void ElementStyle::DirtyProperty(const String& property)
  503. {
  504. dirty_properties.SetDirty(property);
  505. }
  506. // Sets a list of properties as dirty.
  507. void ElementStyle::DirtyProperties(const PropertyNameList& properties)
  508. {
  509. dirty_properties.SetDirty(properties);
  510. }
  511. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  512. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  513. {
  514. dirty_properties.SetDirty(properties);
  515. return;
  516. }
  517. DirtyPropertyList ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio)
  518. {
  519. ROCKET_ASSERT_NONRECURSIVE;
  520. if (dirty_properties.Empty())
  521. return DirtyPropertyList();
  522. // Generally, this is how it works (for now, we can probably be smarter about this):
  523. // 1. Assign default values (clears any newly dirtied properties)
  524. // 2. Inherit inheritable values from parent
  525. // 3. Assign any local properties (from inline style or stylesheet)
  526. const float font_size_before = values.font_size;
  527. const float line_height_before = values.line_height.value;
  528. // The next flag is just a small optimization, if the element was just created we don't need to copy all the default values.
  529. if (!values_are_default_initialized)
  530. {
  531. values = DefaultComputedValues;
  532. }
  533. // Always do font-size first if dirty, because of em-relative values
  534. {
  535. if (auto p = GetLocalProperty(FONT_SIZE))
  536. values.font_size = ComputeFontsize(*p, values, parent_values, document_values, dp_ratio);
  537. else if (parent_values)
  538. values.font_size = parent_values->font_size;
  539. if(font_size_before != values.font_size)
  540. dirty_properties.SetEmRelativeDirty();
  541. }
  542. const float font_size = values.font_size;
  543. const float document_font_size = (document_values ? document_values->font_size : DefaultComputedValues.font_size);
  544. // Since vertical-align depends on line-height we compute this before iteration
  545. {
  546. if (auto p = GetLocalProperty(LINE_HEIGHT))
  547. {
  548. values.line_height = ComputeLineHeight(p, font_size, document_font_size, dp_ratio);
  549. }
  550. else if (parent_values)
  551. {
  552. // 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.
  553. // See CSS specs for details. Percent is already converted to number.
  554. if (parent_values->line_height.inherit_type == Style::LineHeight::Number)
  555. values.line_height = Style::LineHeight(font_size * parent_values->line_height.inherit_value, Style::LineHeight::Number, parent_values->line_height.inherit_value);
  556. else
  557. values.line_height = parent_values->line_height;
  558. }
  559. if(line_height_before != values.line_height.value)
  560. dirty_properties.SetDirty(VERTICAL_ALIGN);
  561. }
  562. if (parent_values)
  563. {
  564. // Inherited properties are copied here, but may be overwritten below by locally defined properties
  565. // Line-height and font-size are computed above
  566. values.clip = parent_values->clip;
  567. values.color = parent_values->color;
  568. values.opacity = parent_values->opacity;
  569. values.font_family = parent_values->font_family;
  570. values.font_charset = parent_values->font_charset;
  571. values.font_style = parent_values->font_style;
  572. values.font_weight = parent_values->font_weight;
  573. values.text_align = parent_values->text_align;
  574. values.text_decoration = parent_values->text_decoration;
  575. values.text_transform = parent_values->text_transform;
  576. values.white_space = parent_values->white_space;
  577. values.cursor = parent_values->cursor;
  578. values.focus = parent_values->focus;
  579. values.pointer_events = parent_values->pointer_events;
  580. }
  581. int index = 0;
  582. String name;
  583. const Property* p = nullptr;
  584. while (IterateProperties(index, name, p))
  585. {
  586. using namespace Style;
  587. // @performance: Can use a switch-case with constexpr hashing function
  588. // Or even better: a PropertyId enum, but the problem is custom properties such as decorators. We may want to redo the decleration of these perhaps...
  589. // @performance: Compare to the list of actually changed properties, skip if not inside it
  590. if (name == MARGIN_TOP)
  591. values.margin_top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  592. else if (name == MARGIN_RIGHT)
  593. values.margin_right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  594. else if (name == MARGIN_BOTTOM)
  595. values.margin_bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  596. else if (name == MARGIN_LEFT)
  597. values.margin_left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  598. else if (name == PADDING_TOP)
  599. values.padding_top = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  600. else if (name == PADDING_RIGHT)
  601. values.padding_right = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  602. else if (name == PADDING_BOTTOM)
  603. values.padding_bottom = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  604. else if (name == PADDING_LEFT)
  605. values.padding_left = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  606. else if (name == BORDER_TOP_WIDTH)
  607. values.border_top_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  608. else if (name == BORDER_RIGHT_WIDTH)
  609. values.border_right_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  610. else if (name == BORDER_BOTTOM_WIDTH)
  611. values.border_bottom_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  612. else if (name == BORDER_LEFT_WIDTH)
  613. values.border_left_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  614. else if (name == BORDER_TOP_COLOR)
  615. values.border_top_color = p->Get<Colourb>();
  616. else if (name == BORDER_RIGHT_COLOR)
  617. values.border_right_color = p->Get<Colourb>();
  618. else if (name == BORDER_BOTTOM_COLOR)
  619. values.border_bottom_color = p->Get<Colourb>();
  620. else if (name == BORDER_LEFT_COLOR)
  621. values.border_left_color = p->Get<Colourb>();
  622. else if (name == DISPLAY)
  623. values.display = (Display)p->Get<int>();
  624. else if (name == POSITION)
  625. values.position = (Position)p->Get<int>();
  626. else if (name == TOP)
  627. values.top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  628. else if (name == RIGHT)
  629. values.right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  630. else if (name == BOTTOM)
  631. values.bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  632. else if (name == LEFT)
  633. values.left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  634. else if (name == FLOAT)
  635. values.float_ = (Float)p->Get<int>();
  636. else if (name == CLEAR)
  637. values.clear = (Clear)p->Get<int>();
  638. else if (name == Z_INDEX)
  639. values.z_index = (p->unit == Property::KEYWORD ? ZIndex(ZIndex::Auto) : ZIndex(ZIndex::Number, p->Get<float>()));
  640. else if (name == WIDTH)
  641. values.width = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  642. else if (name == MIN_WIDTH)
  643. values.min_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  644. else if (name == MAX_WIDTH)
  645. values.max_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  646. else if (name == HEIGHT)
  647. values.height = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  648. else if (name == MIN_HEIGHT)
  649. values.min_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  650. else if (name == MAX_HEIGHT)
  651. values.max_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  652. // (Line-height computed above)
  653. else if (name == VERTICAL_ALIGN)
  654. values.vertical_align = ComputeVerticalAlign(p, values.line_height.value, font_size, document_font_size, dp_ratio);
  655. else if (name == OVERFLOW_X)
  656. values.overflow_x = (Overflow)p->Get< int >();
  657. else if (name == OVERFLOW_Y)
  658. values.overflow_y = (Overflow)p->Get< int >();
  659. else if (name == CLIP)
  660. values.clip = ComputeClip(p);
  661. else if (name == VISIBILITY)
  662. values.visibility = (Visibility)p->Get< int >();
  663. else if (name == BACKGROUND_COLOR)
  664. values.background_color = p->Get<Colourb>();
  665. else if (name == COLOR)
  666. values.color = p->Get<Colourb>();
  667. else if (name == IMAGE_COLOR)
  668. values.image_color = p->Get<Colourb>();
  669. else if (name == OPACITY)
  670. values.opacity = p->Get<float>();
  671. else if (name == FONT_FAMILY)
  672. values.font_family = ToLower(p->Get<String>());
  673. else if (name == FONT_CHARSET)
  674. values.font_charset = p->Get<String>();
  675. else if (name == FONT_STYLE)
  676. values.font_style = (FontStyle)p->Get< int >();
  677. else if (name == FONT_WEIGHT)
  678. values.font_weight = (FontWeight)p->Get< int >();
  679. // (font-size computed above)
  680. else if (name == TEXT_ALIGN)
  681. values.text_align = (TextAlign)p->Get< int >();
  682. else if (name == TEXT_DECORATION)
  683. values.text_decoration = (TextDecoration)p->Get< int >();
  684. else if (name == TEXT_TRANSFORM)
  685. values.text_transform = (TextTransform)p->Get< int >();
  686. else if (name == WHITE_SPACE)
  687. values.white_space = (WhiteSpace)p->Get< int >();
  688. else if (name == CURSOR)
  689. values.cursor = p->Get< String >();
  690. else if (name == DRAG)
  691. values.drag = (Drag)p->Get< int >();
  692. else if (name == TAB_INDEX)
  693. values.tab_index = (TabIndex)p->Get< int >();
  694. else if (name == FOCUS)
  695. values.focus = (Focus)p->Get<int>();
  696. else if (name == SCROLLBAR_MARGIN)
  697. values.scrollbar_margin = ComputeLength(p, font_size, document_font_size, dp_ratio);
  698. else if (name == POINTER_EVENTS)
  699. values.pointer_events = (PointerEvents)p->Get<int>();
  700. else if (name == PERSPECTIVE)
  701. values.perspective = ComputeLength(p, font_size, document_font_size, dp_ratio);
  702. else if (name == PERSPECTIVE_ORIGIN_X)
  703. values.perspective_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  704. else if (name == PERSPECTIVE_ORIGIN_Y)
  705. values.perspective_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  706. else if (name == TRANSFORM)
  707. values.transform = p->Get<TransformRef>();
  708. else if(name == TRANSFORM_ORIGIN_X)
  709. values.transform_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  710. else if (name == TRANSFORM_ORIGIN_Y)
  711. values.transform_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  712. else if (name == TRANSFORM_ORIGIN_Z)
  713. values.transform_origin_z = ComputeLength(p, font_size, document_font_size, dp_ratio);
  714. else if (name == TRANSITION)
  715. values.transition = p->Get<TransitionList>();
  716. else if (name == ANIMATION)
  717. values.animation = p->Get<AnimationList>();
  718. }
  719. if (dirty_properties.AnyInheritedDirty())
  720. {
  721. for (int i = 0; i < element->GetNumChildren(true); i++)
  722. {
  723. auto child = element->GetChild(i);
  724. child->GetStyle()->dirty_properties.SetDirty(StyleSheetSpecification::GetRegisteredInheritedProperties());
  725. }
  726. }
  727. DirtyPropertyList result(std::move(dirty_properties));
  728. dirty_properties.Clear();
  729. return result;
  730. }
  731. }
  732. }