ElementStyle.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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)
  48. {
  49. local_properties = NULL;
  50. em_properties = NULL;
  51. definition = NULL;
  52. element = _element;
  53. definition_dirty = true;
  54. }
  55. ElementStyle::~ElementStyle()
  56. {
  57. if (local_properties != NULL)
  58. delete local_properties;
  59. if (em_properties != NULL)
  60. delete em_properties;
  61. if (definition != NULL)
  62. definition->RemoveReference();
  63. }
  64. // Returns the element's definition, updating if necessary.
  65. const ElementDefinition* ElementStyle::GetDefinition()
  66. {
  67. return definition;
  68. }
  69. // Returns one of this element's properties.
  70. const Property* ElementStyle::GetLocalProperty(const String& name, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  71. {
  72. // Check for overriding local properties.
  73. if (local_properties != NULL)
  74. {
  75. const Property* property = local_properties->GetProperty(name);
  76. if (property != NULL)
  77. return property;
  78. }
  79. // Check for a property defined in an RCSS rule.
  80. if (definition != NULL)
  81. return definition->GetProperty(name, pseudo_classes);
  82. return NULL;
  83. }
  84. // Returns one of this element's properties.
  85. const Property* ElementStyle::GetProperty(const String& name, Element* element, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  86. {
  87. const Property* local_property = GetLocalProperty(name, local_properties, definition, pseudo_classes);
  88. if (local_property != NULL)
  89. return local_property;
  90. // Fetch the property specification.
  91. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  92. if (property == NULL)
  93. return NULL;
  94. // If we can inherit this property, return our parent's property.
  95. if (property->IsInherited())
  96. {
  97. Element* parent = element->GetParentNode();
  98. while (parent != NULL)
  99. {
  100. const Property* parent_property = parent->GetStyle()->GetLocalProperty(name);
  101. if (parent_property)
  102. return parent_property;
  103. parent = parent->GetParentNode();
  104. }
  105. }
  106. // No property available! Return the default value.
  107. return property->GetDefaultValue();
  108. }
  109. // Apply transition to relevant properties if a transition is defined on element.
  110. // Properties that are part of a transition are removed from the properties list.
  111. void ElementStyle::TransitionPropertyChanges(Element* element, PropertyNameList& properties, PropertyDictionary* local_properties, ElementDefinition* old_definition, ElementDefinition* new_definition,
  112. const PseudoClassList& pseudo_classes_before, const PseudoClassList& pseudo_classes_after)
  113. {
  114. ROCKET_ASSERT(element);
  115. if (!old_definition || !new_definition || properties.empty())
  116. return;
  117. if (const Property* transition_property = GetLocalProperty(TRANSITION, local_properties, new_definition, pseudo_classes_after))
  118. {
  119. auto transition_list = transition_property->Get<TransitionList>();
  120. if (!transition_list.none)
  121. {
  122. auto add_transition = [&](const Transition& transition) {
  123. bool transition_added = false;
  124. const Property* start_value = GetProperty(transition.name, element, local_properties, old_definition, pseudo_classes_before);
  125. const Property* target_value = GetProperty(transition.name, element, nullptr, new_definition, pseudo_classes_after);
  126. if (start_value && target_value && (*start_value != *target_value))
  127. transition_added = element->StartTransition(transition, *start_value, *target_value);
  128. return transition_added;
  129. };
  130. if (transition_list.all)
  131. {
  132. Transition transition = transition_list.transitions[0];
  133. for (auto it = properties.begin(); it != properties.end(); )
  134. {
  135. transition.name = *it;
  136. if (add_transition(transition))
  137. it = properties.erase(it);
  138. else
  139. ++it;
  140. }
  141. }
  142. else
  143. {
  144. for (auto& transition : transition_list.transitions)
  145. {
  146. auto it = properties.find(transition.name);
  147. if (it != properties.end())
  148. {
  149. if (add_transition(transition))
  150. properties.erase(it);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. void ElementStyle::UpdateDefinition()
  158. {
  159. if (definition_dirty)
  160. {
  161. definition_dirty = false;
  162. ElementDefinition* new_definition = NULL;
  163. const StyleSheet* style_sheet = GetStyleSheet();
  164. if (style_sheet != NULL)
  165. {
  166. new_definition = style_sheet->GetElementDefinition(element);
  167. }
  168. // Switch the property definitions if the definition has changed.
  169. if (new_definition != definition || new_definition == NULL)
  170. {
  171. PropertyNameList properties;
  172. if (definition != NULL)
  173. definition->GetDefinedProperties(properties, pseudo_classes);
  174. if (new_definition != NULL)
  175. new_definition->GetDefinedProperties(properties, pseudo_classes);
  176. TransitionPropertyChanges(element, properties, local_properties, definition, new_definition, pseudo_classes, pseudo_classes);
  177. if (definition != NULL)
  178. definition->RemoveReference();
  179. definition = new_definition;
  180. DirtyProperties(properties);
  181. element->GetElementDecoration()->DirtyDecorators(true);
  182. }
  183. else if (new_definition != NULL)
  184. {
  185. new_definition->RemoveReference();
  186. }
  187. }
  188. }
  189. // Sets or removes a pseudo-class on the element.
  190. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  191. {
  192. size_t num_pseudo_classes = pseudo_classes.size();
  193. auto pseudo_classes_before = pseudo_classes;
  194. if (activate)
  195. pseudo_classes.push_back(pseudo_class);
  196. else
  197. {
  198. // In case of duplicates, we do a loop here. We could do a sort and unique instead,
  199. // but that might even be slower for a small list with few duplicates, which
  200. // is probably the most common case.
  201. auto it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  202. while(it != pseudo_classes.end())
  203. {
  204. pseudo_classes.erase(it);
  205. it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  206. }
  207. }
  208. if (pseudo_classes.size() != num_pseudo_classes)
  209. {
  210. element->GetElementDecoration()->DirtyDecorators(false);
  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.
  288. bool ElementStyle::SetProperty(const String& name, const String& value)
  289. {
  290. if (local_properties == NULL)
  291. local_properties = new PropertyDictionary();
  292. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  293. {
  294. DirtyProperty(name);
  295. return true;
  296. }
  297. else
  298. {
  299. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.c_str(), value.c_str());
  300. return false;
  301. }
  302. }
  303. // Sets a local property override on the element to a pre-parsed value.
  304. bool ElementStyle::SetProperty(const String& name, const Property& property)
  305. {
  306. Property new_property = property;
  307. new_property.definition = StyleSheetSpecification::GetProperty(name);
  308. if (new_property.definition == NULL)
  309. return false;
  310. if (local_properties == NULL)
  311. local_properties = new PropertyDictionary();
  312. local_properties->SetProperty(name, new_property);
  313. DirtyProperty(name);
  314. return true;
  315. }
  316. // Removes a local property override on the element.
  317. void ElementStyle::RemoveProperty(const String& name)
  318. {
  319. if (local_properties == NULL)
  320. return;
  321. if (local_properties->GetProperty(name) != NULL)
  322. {
  323. local_properties->RemoveProperty(name);
  324. DirtyProperty(name);
  325. }
  326. }
  327. // Returns one of this element's properties.
  328. const Property* ElementStyle::GetProperty(const String& name)
  329. {
  330. return GetProperty(name, element, local_properties, definition, pseudo_classes);
  331. }
  332. // Returns one of this element's properties.
  333. const Property* ElementStyle::GetLocalProperty(const String& name)
  334. {
  335. return GetLocalProperty(name, local_properties, definition, pseudo_classes);
  336. }
  337. const PropertyMap * ElementStyle::GetLocalProperties() const
  338. {
  339. if (local_properties)
  340. return &local_properties->GetProperties();
  341. return NULL;
  342. }
  343. float ElementStyle::ResolveNumberLengthPercentage(const Property * property, RelativeTarget relative_target)
  344. {
  345. // There is an exception on font-size properties, as 'em' units here refer to parent font size instead
  346. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && relative_target == RelativeTarget::ParentFontSize))
  347. {
  348. float result = ComputeLength(property, element->GetComputedValues().font_size, element->GetOwnerDocument()->GetComputedValues().font_size, ElementUtilities::GetDensityIndependentPixelRatio(element));
  349. return result;
  350. }
  351. float base_value = 0.0f;
  352. switch (relative_target)
  353. {
  354. case RelativeTarget::None:
  355. base_value = 1.0f;
  356. break;
  357. case RelativeTarget::ContainingBlockWidth:
  358. base_value = element->GetContainingBlock().x;
  359. break;
  360. case RelativeTarget::ContainingBlockHeight:
  361. base_value = element->GetContainingBlock().y;
  362. break;
  363. case RelativeTarget::FontSize:
  364. base_value = element->GetComputedValues().font_size;
  365. break;
  366. case RelativeTarget::ParentFontSize:
  367. base_value = element->GetParentNode()->GetComputedValues().font_size;
  368. break;
  369. case RelativeTarget::LineHeight:
  370. base_value = element->GetLineHeight();
  371. break;
  372. default:
  373. break;
  374. }
  375. float scale_value = 0.0f;
  376. switch (property->unit)
  377. {
  378. case Property::EM:
  379. case Property::NUMBER:
  380. scale_value = property->value.Get< float >();
  381. break;
  382. case Property::PERCENT:
  383. scale_value = property->value.Get< float >() * 0.01f;
  384. break;
  385. }
  386. return base_value * scale_value;
  387. }
  388. // Resolves one of this element's properties.
  389. float ElementStyle::ResolveLengthPercentage(const Property* property, float base_value)
  390. {
  391. if (!property)
  392. {
  393. ROCKET_ERROR;
  394. return 0.0f;
  395. }
  396. ROCKET_ASSERT(property->unit & Property::LENGTH_PERCENT);
  397. const float font_size = element->GetComputedValues().font_size;
  398. const float doc_font_size = element->GetOwnerDocument()->GetComputedValues().font_size;
  399. const float dp_ratio = ElementUtilities::GetDensityIndependentPixelRatio(element);
  400. Style::LengthPercentage computed = ComputeLengthPercentage(property, font_size, doc_font_size, dp_ratio);
  401. float result = ResolveValue(computed, base_value);
  402. return result;
  403. }
  404. // Iterates over the properties defined on the element.
  405. bool ElementStyle::IterateProperties(int& index, String& name, const Property*& property, const PseudoClassList** property_pseudo_classes)
  406. {
  407. // First check for locally defined properties.
  408. if (local_properties != NULL)
  409. {
  410. if (index < local_properties->GetNumProperties())
  411. {
  412. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  413. for (int count = 0; count < index; ++count)
  414. ++i;
  415. name = (*i).first;
  416. property = &((*i).second);
  417. if (property_pseudo_classes)
  418. * property_pseudo_classes = nullptr;
  419. ++index;
  420. return true;
  421. }
  422. }
  423. const ElementDefinition* definition = GetDefinition();
  424. if (definition != NULL)
  425. {
  426. int index_offset = 0;
  427. if (local_properties != NULL)
  428. index_offset = local_properties->GetNumProperties();
  429. // Offset the index to be relative to the definition before we start indexing. When we do get a property back,
  430. // check that it hasn't been overridden by the element's local properties; if so, continue on to the next one.
  431. index -= index_offset;
  432. while (definition->IterateProperties(index, pseudo_classes, name, property, property_pseudo_classes))
  433. {
  434. if (local_properties == NULL ||
  435. local_properties->GetProperty(name) == NULL)
  436. {
  437. index += index_offset;
  438. return true;
  439. }
  440. }
  441. return false;
  442. }
  443. return false;
  444. }
  445. // Returns the active style sheet for this element. This may be NULL.
  446. StyleSheet* ElementStyle::GetStyleSheet() const
  447. {
  448. ElementDocument* document = element->GetOwnerDocument();
  449. if (document != NULL)
  450. return document->GetStyleSheet();
  451. return NULL;
  452. }
  453. void ElementStyle::DirtyDefinition()
  454. {
  455. definition_dirty = true;
  456. DirtyChildDefinitions();
  457. }
  458. void ElementStyle::DirtyChildDefinitions()
  459. {
  460. for (int i = 0; i < element->GetNumChildren(true); i++)
  461. element->GetChild(i)->GetStyle()->DirtyDefinition();
  462. }
  463. // Dirties every property.
  464. void ElementStyle::DirtyProperties()
  465. {
  466. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  467. DirtyProperties(properties);
  468. }
  469. // Dirties em-relative properties.
  470. void ElementStyle::DirtyEmProperties()
  471. {
  472. if (!em_properties)
  473. {
  474. // Check if any of these are currently em-relative. If so, dirty them.
  475. em_properties = new PropertyNameList;
  476. for (auto& property : StyleSheetSpecification::GetRegisteredProperties())
  477. {
  478. // Skip font-size; this is relative to our parent's em, not ours.
  479. if (property == FONT_SIZE)
  480. continue;
  481. // Get this property from this element. If this is em-relative, then add it to the list to
  482. // dirty.
  483. if (element->GetProperty(property)->unit == Property::EM)
  484. em_properties->insert(property);
  485. }
  486. }
  487. if (!em_properties->empty())
  488. DirtyProperties(*em_properties, false);
  489. // Now dirty all of our descendant's font-size properties that are relative to ems.
  490. int num_children = element->GetNumChildren(true);
  491. for (int i = 0; i < num_children; ++i)
  492. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  493. }
  494. // Dirties font-size on child elements if appropriate.
  495. void ElementStyle::DirtyInheritedEmProperties()
  496. {
  497. const Property* font_size = element->GetLocalProperty(FONT_SIZE);
  498. if (font_size == NULL)
  499. {
  500. int num_children = element->GetNumChildren(true);
  501. for (int i = 0; i < num_children; ++i)
  502. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  503. }
  504. else
  505. {
  506. if (font_size->unit & Property::RELATIVE_UNIT)
  507. DirtyProperty(FONT_SIZE);
  508. }
  509. }
  510. // Dirties rem properties.
  511. void ElementStyle::DirtyRemProperties()
  512. {
  513. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  514. PropertyNameList rem_properties;
  515. // Dirty all the properties of this element that use the rem unit.
  516. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  517. {
  518. if (element->GetProperty(*list_iterator)->unit == Property::REM)
  519. rem_properties.insert(*list_iterator);
  520. }
  521. if (!rem_properties.empty())
  522. DirtyProperties(rem_properties, false);
  523. // Now dirty all of our descendant's properties that use the rem unit.
  524. int num_children = element->GetNumChildren(true);
  525. for (int i = 0; i < num_children; ++i)
  526. element->GetChild(i)->GetStyle()->DirtyRemProperties();
  527. }
  528. void ElementStyle::DirtyDpProperties()
  529. {
  530. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  531. PropertyNameList dp_properties;
  532. // Dirty all the properties of this element that use the dp unit.
  533. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  534. {
  535. if (element->GetProperty(*list_iterator)->unit == Property::DP)
  536. dp_properties.insert(*list_iterator);
  537. }
  538. if (!dp_properties.empty())
  539. DirtyProperties(dp_properties, false);
  540. // Now dirty all of our descendant's properties that use the dp unit.
  541. int num_children = element->GetNumChildren(true);
  542. for (int i = 0; i < num_children; ++i)
  543. element->GetChild(i)->GetStyle()->DirtyDpProperties();
  544. }
  545. // Sets a single property as dirty.
  546. void ElementStyle::DirtyProperty(const String& property)
  547. {
  548. PropertyNameList properties;
  549. properties.insert(String(property));
  550. DirtyProperties(properties);
  551. }
  552. // Sets a list of properties as dirty.
  553. void ElementStyle::DirtyProperties(const PropertyNameList& properties, bool clear_em_properties)
  554. {
  555. if (properties.empty())
  556. return;
  557. bool all_inherited_dirty =
  558. &properties == &StyleSheetSpecification::GetRegisteredProperties() ||
  559. StyleSheetSpecification::GetRegisteredProperties() == properties ||
  560. StyleSheetSpecification::GetRegisteredInheritedProperties() == properties;
  561. if (all_inherited_dirty)
  562. {
  563. const PropertyNameList &all_inherited_properties = StyleSheetSpecification::GetRegisteredInheritedProperties();
  564. for (int i = 0; i < element->GetNumChildren(true); i++)
  565. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(all_inherited_properties);
  566. }
  567. else
  568. {
  569. PropertyNameList inherited_properties;
  570. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  571. {
  572. // If this property is an inherited property, then push it into the list to be passed onto our children.
  573. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(*i);
  574. if (property != NULL &&
  575. property->IsInherited())
  576. inherited_properties.insert(*i);
  577. }
  578. // Pass the list of those properties that are inherited onto our children.
  579. if (!inherited_properties.empty())
  580. {
  581. for (int i = 0; i < element->GetNumChildren(true); i++)
  582. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  583. }
  584. }
  585. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  586. if (clear_em_properties && em_properties != NULL)
  587. {
  588. delete em_properties;
  589. em_properties = NULL;
  590. }
  591. // And send the event.
  592. element->DirtyProperties(properties);
  593. }
  594. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  595. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  596. {
  597. bool clear_em_properties = em_properties != NULL;
  598. PropertyNameList inherited_properties;
  599. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  600. {
  601. const Property *property = GetLocalProperty((*i));
  602. if (property == NULL)
  603. {
  604. inherited_properties.insert(*i);
  605. if (!clear_em_properties && em_properties != NULL && em_properties->find((*i)) != em_properties->end()) {
  606. clear_em_properties = true;
  607. }
  608. }
  609. }
  610. if (inherited_properties.empty())
  611. return;
  612. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  613. if (clear_em_properties && em_properties != NULL)
  614. {
  615. delete em_properties;
  616. em_properties = NULL;
  617. }
  618. // Pass the list of those properties that this element doesn't override onto our children.
  619. for (int i = 0; i < element->GetNumChildren(true); i++)
  620. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  621. element->DirtyProperties(properties);
  622. }
  623. void ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio)
  624. {
  625. // Generally, this is how it works (for now, we can probably be smarter about this):
  626. // 1. Assign default values (clears any newly dirtied properties)
  627. // 2. Inherit inheritable values from parent
  628. // 3. Assign any local properties (from inline style or stylesheet)
  629. // The next flag is just a small optimization, if the element was just created we don't need to copy all the default values.
  630. if (!values_are_default_initialized)
  631. {
  632. values = DefaultComputedValues;
  633. }
  634. // Always do font-size first if dirty, because of em-relative values
  635. if (auto p = GetLocalProperty(FONT_SIZE))
  636. values.font_size = ComputeFontsize(*p, values, parent_values, document_values, dp_ratio);
  637. else if (parent_values)
  638. values.font_size = parent_values->font_size;
  639. const float font_size = values.font_size;
  640. const float document_font_size = (document_values ? document_values->font_size : DefaultComputedValues.font_size);
  641. // Since vertical-align depends on line-height we compute this before iteration
  642. if (auto p = GetLocalProperty(LINE_HEIGHT))
  643. {
  644. values.line_height = ComputeLineHeight(p, font_size, document_font_size, dp_ratio);
  645. }
  646. else if (parent_values)
  647. {
  648. // 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.
  649. // See CSS specs for details. Percent is already converted to number.
  650. if (parent_values->line_height.inherit_type == Style::LineHeight::Number)
  651. values.line_height = Style::LineHeight(font_size * parent_values->line_height.inherit_value, Style::LineHeight::Number, parent_values->line_height.inherit_value);
  652. else
  653. values.line_height = parent_values->line_height;
  654. }
  655. if (parent_values)
  656. {
  657. // Inherited properties are copied here, but may be overwritten below by locally defined properties
  658. // Line-height and font-size are computed above
  659. values.clip = parent_values->clip;
  660. values.color = parent_values->color;
  661. values.opacity = parent_values->opacity;
  662. values.font_family = parent_values->font_family;
  663. values.font_charset = parent_values->font_charset;
  664. values.font_style = parent_values->font_style;
  665. values.font_weight = parent_values->font_weight;
  666. values.text_align = parent_values->text_align;
  667. values.text_decoration = parent_values->text_decoration;
  668. values.text_transform = parent_values->text_transform;
  669. values.white_space = parent_values->white_space;
  670. values.cursor = parent_values->cursor;
  671. values.focus = parent_values->focus;
  672. values.pointer_events = parent_values->pointer_events;
  673. }
  674. int index = 0;
  675. String name;
  676. const Property* p = nullptr;
  677. while (IterateProperties(index, name, p))
  678. {
  679. using namespace Style;
  680. // @performance: Can use a switch-case with constexpr hashing function
  681. // 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...
  682. // @performance: Compare to the list of actually changed properties, skip if not inside it
  683. if (name == MARGIN_TOP)
  684. values.margin_top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  685. else if (name == MARGIN_RIGHT)
  686. values.margin_right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  687. else if (name == MARGIN_BOTTOM)
  688. values.margin_bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  689. else if (name == MARGIN_LEFT)
  690. values.margin_left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  691. else if (name == PADDING_TOP)
  692. values.padding_top = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  693. else if (name == PADDING_RIGHT)
  694. values.padding_right = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  695. else if (name == PADDING_BOTTOM)
  696. values.padding_bottom = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  697. else if (name == PADDING_LEFT)
  698. values.padding_left = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  699. else if (name == BORDER_TOP_WIDTH)
  700. values.border_top_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  701. else if (name == BORDER_RIGHT_WIDTH)
  702. values.border_right_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  703. else if (name == BORDER_BOTTOM_WIDTH)
  704. values.border_bottom_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  705. else if (name == BORDER_LEFT_WIDTH)
  706. values.border_left_width = ComputeLength(p, font_size, document_font_size, dp_ratio);
  707. else if (name == BORDER_TOP_COLOR)
  708. values.border_top_color = p->Get<Colourb>();
  709. else if (name == BORDER_RIGHT_COLOR)
  710. values.border_right_color = p->Get<Colourb>();
  711. else if (name == BORDER_BOTTOM_COLOR)
  712. values.border_bottom_color = p->Get<Colourb>();
  713. else if (name == BORDER_LEFT_COLOR)
  714. values.border_left_color = p->Get<Colourb>();
  715. else if (name == DISPLAY)
  716. values.display = (Display)p->Get<int>();
  717. else if (name == POSITION)
  718. values.position = (Position)p->Get<int>();
  719. else if (name == TOP)
  720. values.top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  721. else if (name == RIGHT)
  722. values.right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  723. else if (name == BOTTOM)
  724. values.bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  725. else if (name == LEFT)
  726. values.left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  727. else if (name == FLOAT)
  728. values.float_ = (Float)p->Get<int>();
  729. else if (name == CLEAR)
  730. values.clear = (Clear)p->Get<int>();
  731. else if (name == Z_INDEX)
  732. values.z_index = (p->unit == Property::KEYWORD ? ZIndex(ZIndex::Auto) : ZIndex(ZIndex::Number, p->Get<float>()));
  733. else if (name == WIDTH)
  734. values.width = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  735. else if (name == MIN_WIDTH)
  736. values.min_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  737. else if (name == MAX_WIDTH)
  738. values.max_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  739. else if (name == HEIGHT)
  740. values.height = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio);
  741. else if (name == MIN_HEIGHT)
  742. values.min_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  743. else if (name == MAX_HEIGHT)
  744. values.max_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio);
  745. // (Line-height computed above)
  746. else if (name == VERTICAL_ALIGN)
  747. values.vertical_align = ComputeVerticalAlign(p, values.line_height.value, font_size, document_font_size, dp_ratio);
  748. else if (name == OVERFLOW_X)
  749. values.overflow_x = (Overflow)p->Get< int >();
  750. else if (name == OVERFLOW_Y)
  751. values.overflow_y = (Overflow)p->Get< int >();
  752. else if (name == CLIP)
  753. values.clip = ComputeClip(p);
  754. else if (name == VISIBILITY)
  755. values.visibility = (Visibility)p->Get< int >();
  756. else if (name == BACKGROUND_COLOR)
  757. values.background_color = p->Get<Colourb>();
  758. else if (name == COLOR)
  759. values.color = p->Get<Colourb>();
  760. else if (name == IMAGE_COLOR)
  761. values.image_color = p->Get<Colourb>();
  762. else if (name == OPACITY)
  763. values.opacity = p->Get<float>();
  764. else if (name == FONT_FAMILY)
  765. values.font_family = ToLower(p->Get<String>());
  766. else if (name == FONT_CHARSET)
  767. values.font_charset = p->Get<String>();
  768. else if (name == FONT_STYLE)
  769. values.font_style = (FontStyle)p->Get< int >();
  770. else if (name == FONT_WEIGHT)
  771. values.font_weight = (FontWeight)p->Get< int >();
  772. // (font-size computed above)
  773. else if (name == TEXT_ALIGN)
  774. values.text_align = (TextAlign)p->Get< int >();
  775. else if (name == TEXT_DECORATION)
  776. values.text_decoration = (TextDecoration)p->Get< int >();
  777. else if (name == TEXT_TRANSFORM)
  778. values.text_transform = (TextTransform)p->Get< int >();
  779. else if (name == WHITE_SPACE)
  780. values.white_space = (WhiteSpace)p->Get< int >();
  781. else if (name == CURSOR)
  782. values.cursor = p->Get< String >();
  783. else if (name == DRAG)
  784. values.drag = (Drag)p->Get< int >();
  785. else if (name == TAB_INDEX)
  786. values.tab_index = (TabIndex)p->Get< int >();
  787. else if (name == FOCUS)
  788. values.focus = (Focus)p->Get<int>();
  789. else if (name == SCROLLBAR_MARGIN)
  790. values.scrollbar_margin = ComputeLength(p, font_size, document_font_size, dp_ratio);
  791. else if (name == POINTER_EVENTS)
  792. values.pointer_events = (PointerEvents)p->Get<int>();
  793. else if (name == PERSPECTIVE)
  794. values.perspective = ComputeLength(p, font_size, document_font_size, dp_ratio);
  795. else if (name == PERSPECTIVE_ORIGIN_X)
  796. values.perspective_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  797. else if (name == PERSPECTIVE_ORIGIN_Y)
  798. values.perspective_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  799. else if (name == TRANSFORM)
  800. values.transform = p->Get<TransformRef>();
  801. else if(name == TRANSFORM_ORIGIN_X)
  802. values.transform_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  803. else if (name == TRANSFORM_ORIGIN_Y)
  804. values.transform_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio);
  805. else if (name == TRANSFORM_ORIGIN_Z)
  806. values.transform_origin_z = ComputeLength(p, font_size, document_font_size, dp_ratio);
  807. else if (name == TRANSITION)
  808. values.transition = p->Get<TransitionList>();
  809. else if (name == ANIMATION)
  810. values.animation = p->Get<AnimationList>();
  811. }
  812. }
  813. }
  814. }