ElementStyle.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "ElementStyle.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/ElementDocument.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  34. #include "../../Include/RmlUi/Core/Log.h"
  35. #include "../../Include/RmlUi/Core/Math.h"
  36. #include "../../Include/RmlUi/Core/Property.h"
  37. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  38. #include "../../Include/RmlUi/Core/PropertyDictionary.h"
  39. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  40. #include "../../Include/RmlUi/Core/Profiling.h"
  41. #include "../../Include/RmlUi/Core/StyleSheet.h"
  42. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  43. #include "../../Include/RmlUi/Core/TransformPrimitive.h"
  44. #include "ElementDecoration.h"
  45. #include "ElementDefinition.h"
  46. #include "ComputeProperty.h"
  47. #include "PropertiesIterator.h"
  48. #include <algorithm>
  49. namespace Rml {
  50. // Bitwise operations on the PseudoClassState.
  51. inline PseudoClassState operator|(PseudoClassState lhs, PseudoClassState rhs)
  52. {
  53. return PseudoClassState(int(lhs) | int(rhs));
  54. }
  55. inline PseudoClassState operator&(PseudoClassState lhs, PseudoClassState rhs)
  56. {
  57. return PseudoClassState(int(lhs) & int(rhs));
  58. }
  59. ElementStyle::ElementStyle(Element* _element)
  60. {
  61. element = _element;
  62. definition_dirty = true;
  63. }
  64. // Returns one of this element's properties.
  65. const Property* ElementStyle::GetLocalProperty(PropertyId id, const PropertyDictionary& inline_properties, const ElementDefinition* definition)
  66. {
  67. // Check for overriding local properties.
  68. const Property* property = inline_properties.GetProperty(id);
  69. if (property)
  70. return property;
  71. // Check for a property defined in an RCSS rule.
  72. if (definition)
  73. return definition->GetProperty(id);
  74. return nullptr;
  75. }
  76. // Returns one of this element's properties.
  77. const Property* ElementStyle::GetProperty(PropertyId id, const Element* element, const PropertyDictionary& inline_properties, const ElementDefinition* definition)
  78. {
  79. const Property* local_property = GetLocalProperty(id, inline_properties, definition);
  80. if (local_property)
  81. return local_property;
  82. // Fetch the property specification.
  83. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(id);
  84. if (!property)
  85. return nullptr;
  86. // If we can inherit this property, return our parent's property.
  87. if (property->IsInherited())
  88. {
  89. Element* parent = element->GetParentNode();
  90. while (parent)
  91. {
  92. const Property* parent_property = parent->GetStyle()->GetLocalProperty(id);
  93. if (parent_property)
  94. return parent_property;
  95. parent = parent->GetParentNode();
  96. }
  97. }
  98. // No property available! Return the default value.
  99. return property->GetDefaultValue();
  100. }
  101. // Apply transition to relevant properties if a transition is defined on element.
  102. // Properties that are part of a transition are removed from the properties list.
  103. void ElementStyle::TransitionPropertyChanges(Element* element, PropertyIdSet& properties, const PropertyDictionary& inline_properties, const ElementDefinition* old_definition, const ElementDefinition* new_definition)
  104. {
  105. RMLUI_ASSERT(element);
  106. if (!old_definition || !new_definition || properties.Empty())
  107. return;
  108. // We get the local property instead of the computed value here, because we want to intercept property changes even before the computed values are ready.
  109. // Now that we have the concept of computed values, we may want do this operation directly on them instead.
  110. if (const Property* transition_property = GetLocalProperty(PropertyId::Transition, inline_properties, new_definition))
  111. {
  112. if (transition_property->value.GetType() != Variant::TRANSITIONLIST)
  113. return;
  114. const TransitionList& transition_list = transition_property->value.GetReference<TransitionList>();
  115. if (!transition_list.none)
  116. {
  117. static const PropertyDictionary empty_properties;
  118. auto add_transition = [&](const Transition& transition) {
  119. bool transition_added = false;
  120. const Property* start_value = GetProperty(transition.id, element, inline_properties, old_definition);
  121. const Property* target_value = GetProperty(transition.id, element, empty_properties, new_definition);
  122. if (start_value && target_value && (*start_value != *target_value))
  123. transition_added = element->StartTransition(transition, *start_value, *target_value);
  124. return transition_added;
  125. };
  126. if (transition_list.all)
  127. {
  128. Transition transition = transition_list.transitions[0];
  129. for (auto it = properties.begin(); it != properties.end(); )
  130. {
  131. transition.id = *it;
  132. if (add_transition(transition))
  133. it = properties.Erase(it);
  134. else
  135. ++it;
  136. }
  137. }
  138. else
  139. {
  140. for (const Transition& transition : transition_list.transitions)
  141. {
  142. if (properties.Contains(transition.id))
  143. {
  144. if (add_transition(transition))
  145. properties.Erase(transition.id);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. void ElementStyle::UpdateDefinition()
  153. {
  154. if (definition_dirty)
  155. {
  156. RMLUI_ZoneScoped;
  157. definition_dirty = false;
  158. SharedPtr<const ElementDefinition> new_definition;
  159. if (const StyleSheet* style_sheet = element->GetStyleSheet())
  160. {
  161. new_definition = style_sheet->GetElementDefinition(element);
  162. }
  163. // Switch the property definitions if the definition has changed.
  164. if (new_definition != definition)
  165. {
  166. PropertyIdSet changed_properties;
  167. if (definition)
  168. changed_properties = definition->GetPropertyIds();
  169. if (new_definition)
  170. changed_properties |= new_definition->GetPropertyIds();
  171. if (definition && new_definition)
  172. {
  173. // Remove properties that compare equal from the changed list.
  174. const PropertyIdSet properties_in_both_definitions = (definition->GetPropertyIds() & new_definition->GetPropertyIds());
  175. for (PropertyId id : properties_in_both_definitions)
  176. {
  177. const Property* p0 = definition->GetProperty(id);
  178. const Property* p1 = new_definition->GetProperty(id);
  179. if (p0 && p1 && *p0 == *p1)
  180. changed_properties.Erase(id);
  181. }
  182. // Transition changed properties if transition property is set
  183. TransitionPropertyChanges(element, changed_properties, inline_properties, definition.get(), new_definition.get());
  184. }
  185. definition = new_definition;
  186. DirtyProperties(changed_properties);
  187. }
  188. // Even if the definition was not changed, the child definitions may have changed as a result of anything that
  189. // could change the definition of this element, such as a new pseudo class.
  190. DirtyChildDefinitions();
  191. }
  192. }
  193. // Sets or removes a pseudo-class on the element.
  194. bool ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate, bool override_class)
  195. {
  196. bool changed = false;
  197. if (activate)
  198. {
  199. PseudoClassState& state = pseudo_classes[pseudo_class];
  200. changed = (state == PseudoClassState::Clear);
  201. state = (state | (override_class ? PseudoClassState::Override : PseudoClassState::Set));
  202. }
  203. else
  204. {
  205. auto it = pseudo_classes.find(pseudo_class);
  206. if (it != pseudo_classes.end())
  207. {
  208. PseudoClassState& state = it->second;
  209. state = (state & (override_class ? PseudoClassState::Set : PseudoClassState::Override));
  210. if (state == PseudoClassState::Clear)
  211. {
  212. pseudo_classes.erase(it);
  213. changed = true;
  214. }
  215. }
  216. }
  217. if (changed)
  218. DirtyDefinition();
  219. return changed;
  220. }
  221. // Checks if a specific pseudo-class has been set on the element.
  222. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  223. {
  224. return (pseudo_classes.count(pseudo_class) == 1);
  225. }
  226. const PseudoClassMap& ElementStyle::GetActivePseudoClasses() const
  227. {
  228. return pseudo_classes;
  229. }
  230. // Sets or removes a class on the element.
  231. void ElementStyle::SetClass(const String& class_name, bool activate)
  232. {
  233. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  234. if (activate)
  235. {
  236. if (class_location == classes.end())
  237. {
  238. classes.push_back(class_name);
  239. DirtyDefinition();
  240. }
  241. }
  242. else
  243. {
  244. if (class_location != classes.end())
  245. {
  246. classes.erase(class_location);
  247. DirtyDefinition();
  248. }
  249. }
  250. }
  251. // Checks if a class is set on the element.
  252. bool ElementStyle::IsClassSet(const String& class_name) const
  253. {
  254. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  255. }
  256. // Specifies the entire list of classes for this element. This will replace any others specified.
  257. void ElementStyle::SetClassNames(const String& class_names)
  258. {
  259. classes.clear();
  260. StringUtilities::ExpandString(classes, class_names, ' ');
  261. DirtyDefinition();
  262. }
  263. // Returns the list of classes specified for this element.
  264. String ElementStyle::GetClassNames() const
  265. {
  266. String class_names;
  267. for (size_t i = 0; i < classes.size(); i++)
  268. {
  269. if (i != 0)
  270. {
  271. class_names += " ";
  272. }
  273. class_names += classes[i];
  274. }
  275. return class_names;
  276. }
  277. // Sets a local property override on the element to a pre-parsed value.
  278. bool ElementStyle::SetProperty(PropertyId id, const Property& property)
  279. {
  280. Property new_property = property;
  281. new_property.definition = StyleSheetSpecification::GetProperty(id);
  282. if (!new_property.definition)
  283. return false;
  284. inline_properties.SetProperty(id, new_property);
  285. DirtyProperty(id);
  286. return true;
  287. }
  288. // Removes a local property override on the element.
  289. void ElementStyle::RemoveProperty(PropertyId id)
  290. {
  291. int size_before = inline_properties.GetNumProperties();
  292. inline_properties.RemoveProperty(id);
  293. if(inline_properties.GetNumProperties() != size_before)
  294. DirtyProperty(id);
  295. }
  296. // Returns one of this element's properties.
  297. const Property* ElementStyle::GetProperty(PropertyId id) const
  298. {
  299. return GetProperty(id, element, inline_properties, definition.get());
  300. }
  301. // Returns one of this element's properties.
  302. const Property* ElementStyle::GetLocalProperty(PropertyId id) const
  303. {
  304. return GetLocalProperty(id, inline_properties, definition.get());
  305. }
  306. const PropertyMap& ElementStyle::GetLocalStyleProperties() const
  307. {
  308. return inline_properties.GetProperties();
  309. }
  310. static float ComputeLength(const Property* property, Element* element)
  311. {
  312. const float font_size = element->GetComputedValues().font_size;
  313. float doc_font_size = DefaultComputedValues.font_size;
  314. float dp_ratio = 1.0f;
  315. Vector2f vp_dimensions(1.0f);
  316. if (ElementDocument* document = element->GetOwnerDocument())
  317. {
  318. doc_font_size = document->GetComputedValues().font_size;
  319. if (Context* context = document->GetContext())
  320. {
  321. dp_ratio = context->GetDensityIndependentPixelRatio();
  322. vp_dimensions = Vector2f(context->GetDimensions());
  323. }
  324. }
  325. const float result = ComputeLength(property, font_size, doc_font_size, dp_ratio, vp_dimensions);
  326. return result;
  327. }
  328. float ElementStyle::ResolveNumericProperty(const Property* property, float base_value) const
  329. {
  330. if (!property || !(property->unit & (Property::NUMBER_LENGTH_PERCENT | Property::ANGLE)))
  331. return 0.0f;
  332. if (property->unit & Property::NUMBER)
  333. return property->Get<float>() * base_value;
  334. else if (property->unit & Property::PERCENT)
  335. return property->Get<float>() * base_value * 0.01f;
  336. else if (property->unit & Property::ANGLE)
  337. return ComputeAngle(*property);
  338. const float result = ComputeLength(property, element);
  339. return result;
  340. }
  341. float ElementStyle::ResolveLength(const Property* property, RelativeTarget relative_target) const
  342. {
  343. RMLUI_ASSERT(property);
  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. const float result = ComputeLength(property, 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. {
  367. auto p = element->GetParentNode();
  368. base_value = (p ? p->GetComputedValues().font_size : DefaultComputedValues.font_size);
  369. }
  370. break;
  371. case RelativeTarget::LineHeight:
  372. base_value = element->GetLineHeight();
  373. break;
  374. default:
  375. break;
  376. }
  377. float scale_value = 0.0f;
  378. switch (property->unit)
  379. {
  380. case Property::EM:
  381. case Property::NUMBER:
  382. scale_value = property->value.Get< float >();
  383. break;
  384. case Property::PERCENT:
  385. scale_value = property->value.Get< float >() * 0.01f;
  386. break;
  387. default:
  388. break;
  389. }
  390. return base_value * scale_value;
  391. }
  392. void ElementStyle::DirtyDefinition()
  393. {
  394. definition_dirty = true;
  395. }
  396. void ElementStyle::DirtyInheritedProperties()
  397. {
  398. dirty_properties |= StyleSheetSpecification::GetRegisteredInheritedProperties();
  399. }
  400. void ElementStyle::DirtyChildDefinitions()
  401. {
  402. for (int i = 0; i < element->GetNumChildren(true); i++)
  403. element->GetChild(i)->GetStyle()->DirtyDefinition();
  404. }
  405. void ElementStyle::DirtyPropertiesWithUnits(Property::Unit units)
  406. {
  407. // Dirty all the properties of this element that use the unit(s).
  408. for (auto it = Iterate(); !it.AtEnd(); ++it)
  409. {
  410. auto name_property_pair = *it;
  411. PropertyId id = name_property_pair.first;
  412. const Property& property = name_property_pair.second;
  413. if (property.unit & units)
  414. DirtyProperty(id);
  415. }
  416. }
  417. void ElementStyle::DirtyPropertiesWithUnitsRecursive(Property::Unit units)
  418. {
  419. DirtyPropertiesWithUnits(units);
  420. // Now dirty all of our descendant's properties that use the unit(s).
  421. int num_children = element->GetNumChildren(true);
  422. for (int i = 0; i < num_children; ++i)
  423. element->GetChild(i)->GetStyle()->DirtyPropertiesWithUnitsRecursive(units);
  424. }
  425. bool ElementStyle::AnyPropertiesDirty() const
  426. {
  427. return !dirty_properties.Empty();
  428. }
  429. PropertiesIterator ElementStyle::Iterate() const {
  430. // Note: Value initialized iterators are only guaranteed to compare equal in C++14, and only for iterators satisfying the ForwardIterator requirements.
  431. #ifdef _MSC_VER
  432. // Null forward iterator supported since VS 2015
  433. static_assert(_MSC_VER >= 1900, "Visual Studio 2015 or higher required, see comment.");
  434. #else
  435. static_assert(__cplusplus >= 201402L, "C++14 or higher required, see comment.");
  436. #endif
  437. const PropertyMap& property_map = inline_properties.GetProperties();
  438. auto it_style_begin = property_map.begin();
  439. auto it_style_end = property_map.end();
  440. PropertyMap::const_iterator it_definition{}, it_definition_end{};
  441. if (definition)
  442. {
  443. const PropertyMap& definition_properties = definition->GetProperties().GetProperties();
  444. it_definition = definition_properties.begin();
  445. it_definition_end = definition_properties.end();
  446. }
  447. return PropertiesIterator(it_style_begin, it_style_end, it_definition, it_definition_end);
  448. }
  449. // Sets a single property as dirty.
  450. void ElementStyle::DirtyProperty(PropertyId id)
  451. {
  452. dirty_properties.Insert(id);
  453. }
  454. // Sets a list of properties as dirty.
  455. void ElementStyle::DirtyProperties(const PropertyIdSet& properties)
  456. {
  457. dirty_properties |= properties;
  458. }
  459. PropertyIdSet ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio, Vector2f vp_dimensions)
  460. {
  461. if (dirty_properties.Empty())
  462. return PropertyIdSet();
  463. RMLUI_ZoneScopedC(0xFF7F50);
  464. // Generally, this is how it works:
  465. // 1. Assign default values (clears any removed properties)
  466. // 2. Inherit inheritable values from parent
  467. // 3. Assign any local properties (from inline style or stylesheet)
  468. // 4. Dirty properties in children that are inherited
  469. const float font_size_before = values.font_size;
  470. const Style::LineHeight line_height_before = values.line_height;
  471. // The next flag is just a small optimization, if the element was just created we don't need to copy all the default values.
  472. if (!values_are_default_initialized)
  473. {
  474. // This needs to be done in case some properties were removed and thus not in our local style anymore.
  475. // If we skipped this, the old dirty value would be unmodified, instead, now it is set to its default value.
  476. // Strictly speaking, we only really need to do this for the dirty values, and only non-inherited. However,
  477. // it seems assigning the whole thing is faster in most cases.
  478. values = DefaultComputedValues;
  479. }
  480. bool dirty_em_properties = false;
  481. // Always do font-size first if dirty, because of em-relative values
  482. if(dirty_properties.Contains(PropertyId::FontSize))
  483. {
  484. if (auto p = GetLocalProperty(PropertyId::FontSize))
  485. values.font_size = ComputeFontsize(*p, values, parent_values, document_values, dp_ratio, vp_dimensions);
  486. else if (parent_values)
  487. values.font_size = parent_values->font_size;
  488. if (font_size_before != values.font_size)
  489. {
  490. dirty_em_properties = true;
  491. dirty_properties.Insert(PropertyId::LineHeight);
  492. }
  493. }
  494. else
  495. {
  496. values.font_size = font_size_before;
  497. }
  498. const float font_size = values.font_size;
  499. const float document_font_size = (document_values ? document_values->font_size : DefaultComputedValues.font_size);
  500. // Since vertical-align depends on line-height we compute this before iteration
  501. if(dirty_properties.Contains(PropertyId::LineHeight))
  502. {
  503. if (auto p = GetLocalProperty(PropertyId::LineHeight))
  504. {
  505. values.line_height = ComputeLineHeight(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  506. }
  507. else if (parent_values)
  508. {
  509. // 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.
  510. // See CSS specs for details. Percent is already converted to number.
  511. if (parent_values->line_height.inherit_type == Style::LineHeight::Number)
  512. values.line_height = Style::LineHeight(font_size * parent_values->line_height.inherit_value, Style::LineHeight::Number, parent_values->line_height.inherit_value);
  513. else
  514. values.line_height = parent_values->line_height;
  515. }
  516. if(line_height_before.value != values.line_height.value || line_height_before.inherit_value != values.line_height.inherit_value)
  517. dirty_properties.Insert(PropertyId::VerticalAlign);
  518. }
  519. else
  520. {
  521. values.line_height = line_height_before;
  522. }
  523. if (parent_values)
  524. {
  525. // Inherited properties are copied here, but may be overwritten below by locally defined properties
  526. // Line-height and font-size are computed above
  527. values.color = parent_values->color;
  528. values.opacity = parent_values->opacity;
  529. values.font_family = parent_values->font_family;
  530. values.font_style = parent_values->font_style;
  531. values.font_weight = parent_values->font_weight;
  532. values.font_face_handle = parent_values->font_face_handle;
  533. values.text_align = parent_values->text_align;
  534. values.text_decoration = parent_values->text_decoration;
  535. values.text_transform = parent_values->text_transform;
  536. values.white_space = parent_values->white_space;
  537. values.word_break = parent_values->word_break;
  538. values.cursor = parent_values->cursor;
  539. values.focus = parent_values->focus;
  540. values.pointer_events = parent_values->pointer_events;
  541. values.has_font_effect = parent_values->has_font_effect;
  542. }
  543. for (auto it = Iterate(); !it.AtEnd(); ++it)
  544. {
  545. auto name_property_pair = *it;
  546. const PropertyId id = name_property_pair.first;
  547. const Property* p = &name_property_pair.second;
  548. if (dirty_em_properties && p->unit == Property::EM)
  549. dirty_properties.Insert(id);
  550. using namespace Style;
  551. switch (id)
  552. {
  553. case PropertyId::MarginTop:
  554. values.margin_top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  555. break;
  556. case PropertyId::MarginRight:
  557. values.margin_right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  558. break;
  559. case PropertyId::MarginBottom:
  560. values.margin_bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  561. break;
  562. case PropertyId::MarginLeft:
  563. values.margin_left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  564. break;
  565. case PropertyId::PaddingTop:
  566. values.padding_top = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  567. break;
  568. case PropertyId::PaddingRight:
  569. values.padding_right = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  570. break;
  571. case PropertyId::PaddingBottom:
  572. values.padding_bottom = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  573. break;
  574. case PropertyId::PaddingLeft:
  575. values.padding_left = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  576. break;
  577. case PropertyId::BorderTopWidth:
  578. values.border_top_width = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  579. break;
  580. case PropertyId::BorderRightWidth:
  581. values.border_right_width = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  582. break;
  583. case PropertyId::BorderBottomWidth:
  584. values.border_bottom_width = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  585. break;
  586. case PropertyId::BorderLeftWidth:
  587. values.border_left_width = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  588. break;
  589. case PropertyId::BorderTopColor:
  590. values.border_top_color = p->Get<Colourb>();
  591. break;
  592. case PropertyId::BorderRightColor:
  593. values.border_right_color = p->Get<Colourb>();
  594. break;
  595. case PropertyId::BorderBottomColor:
  596. values.border_bottom_color = p->Get<Colourb>();
  597. break;
  598. case PropertyId::BorderLeftColor:
  599. values.border_left_color = p->Get<Colourb>();
  600. break;
  601. case PropertyId::BorderTopLeftRadius:
  602. values.border_top_left_radius = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  603. break;
  604. case PropertyId::BorderTopRightRadius:
  605. values.border_top_right_radius = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  606. break;
  607. case PropertyId::BorderBottomRightRadius:
  608. values.border_bottom_right_radius = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  609. break;
  610. case PropertyId::BorderBottomLeftRadius:
  611. values.border_bottom_left_radius = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  612. break;
  613. case PropertyId::Display:
  614. values.display = (Display)p->Get<int>();
  615. break;
  616. case PropertyId::Position:
  617. values.position = (Position)p->Get<int>();
  618. break;
  619. case PropertyId::Top:
  620. values.top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  621. break;
  622. case PropertyId::Right:
  623. values.right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  624. break;
  625. case PropertyId::Bottom:
  626. values.bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  627. break;
  628. case PropertyId::Left:
  629. values.left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  630. break;
  631. case PropertyId::Float:
  632. values.float_ = (Float)p->Get<int>();
  633. break;
  634. case PropertyId::Clear:
  635. values.clear = (Clear)p->Get<int>();
  636. break;
  637. case PropertyId::BoxSizing:
  638. values.box_sizing = (BoxSizing)p->Get<int>();
  639. break;
  640. case PropertyId::ZIndex:
  641. values.z_index = (p->unit == Property::KEYWORD ? ZIndex(ZIndex::Auto) : ZIndex(ZIndex::Number, p->Get<float>()));
  642. break;
  643. case PropertyId::Width:
  644. values.width = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  645. break;
  646. case PropertyId::MinWidth:
  647. values.min_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  648. break;
  649. case PropertyId::MaxWidth:
  650. values.max_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  651. break;
  652. case PropertyId::Height:
  653. values.height = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  654. break;
  655. case PropertyId::MinHeight:
  656. values.min_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  657. break;
  658. case PropertyId::MaxHeight:
  659. values.max_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  660. break;
  661. case PropertyId::LineHeight:
  662. // (Line-height computed above)
  663. break;
  664. case PropertyId::VerticalAlign:
  665. values.vertical_align = ComputeVerticalAlign(p, values.line_height.value, font_size, document_font_size, dp_ratio, vp_dimensions);
  666. break;
  667. case PropertyId::OverflowX:
  668. values.overflow_x = (Overflow)p->Get< int >();
  669. break;
  670. case PropertyId::OverflowY:
  671. values.overflow_y = (Overflow)p->Get< int >();
  672. break;
  673. case PropertyId::Clip:
  674. values.clip = ComputeClip(p);
  675. break;
  676. case PropertyId::Visibility:
  677. values.visibility = (Visibility)p->Get< int >();
  678. break;
  679. case PropertyId::BackgroundColor:
  680. values.background_color = p->Get<Colourb>();
  681. break;
  682. case PropertyId::Color:
  683. values.color = p->Get<Colourb>();
  684. break;
  685. case PropertyId::ImageColor:
  686. values.image_color = p->Get<Colourb>();
  687. break;
  688. case PropertyId::Opacity:
  689. values.opacity = p->Get<float>();
  690. break;
  691. case PropertyId::FontFamily:
  692. values.font_family = ComputeFontFamily(p->Get<String>());
  693. values.font_face_handle = 0;
  694. break;
  695. case PropertyId::FontStyle:
  696. values.font_style = (FontStyle)p->Get< int >();
  697. values.font_face_handle = 0;
  698. break;
  699. case PropertyId::FontWeight:
  700. values.font_weight = (FontWeight)p->Get< int >();
  701. values.font_face_handle = 0;
  702. break;
  703. case PropertyId::FontSize:
  704. // (font-size computed above)
  705. values.font_face_handle = 0;
  706. break;
  707. case PropertyId::TextAlign:
  708. values.text_align = (TextAlign)p->Get< int >();
  709. break;
  710. case PropertyId::TextDecoration:
  711. values.text_decoration = (TextDecoration)p->Get< int >();
  712. break;
  713. case PropertyId::TextTransform:
  714. values.text_transform = (TextTransform)p->Get< int >();
  715. break;
  716. case PropertyId::WhiteSpace:
  717. values.white_space = (WhiteSpace)p->Get< int >();
  718. break;
  719. case PropertyId::WordBreak:
  720. values.word_break = (WordBreak)p->Get< int >();
  721. break;
  722. case PropertyId::RowGap:
  723. values.row_gap = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  724. break;
  725. case PropertyId::ColumnGap:
  726. values.column_gap = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  727. break;
  728. case PropertyId::Cursor:
  729. values.cursor = p->Get< String >();
  730. break;
  731. case PropertyId::Drag:
  732. values.drag = (Drag)p->Get< int >();
  733. break;
  734. case PropertyId::TabIndex:
  735. values.tab_index = (TabIndex)p->Get< int >();
  736. break;
  737. case PropertyId::Focus:
  738. values.focus = (Focus)p->Get<int>();
  739. break;
  740. case PropertyId::ScrollbarMargin:
  741. values.scrollbar_margin = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  742. break;
  743. case PropertyId::PointerEvents:
  744. values.pointer_events = (PointerEvents)p->Get<int>();
  745. break;
  746. case PropertyId::Perspective:
  747. values.perspective = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  748. break;
  749. case PropertyId::PerspectiveOriginX:
  750. values.perspective_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  751. break;
  752. case PropertyId::PerspectiveOriginY:
  753. values.perspective_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  754. break;
  755. case PropertyId::Transform:
  756. values.transform = p->Get<TransformPtr>();
  757. break;
  758. case PropertyId::TransformOriginX:
  759. values.transform_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  760. break;
  761. case PropertyId::TransformOriginY:
  762. values.transform_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  763. break;
  764. case PropertyId::TransformOriginZ:
  765. values.transform_origin_z = ComputeLength(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  766. break;
  767. case PropertyId::Transition:
  768. values.transition = p->Get<TransitionList>();
  769. break;
  770. case PropertyId::Animation:
  771. values.animation = p->Get<AnimationList>();
  772. break;
  773. case PropertyId::Decorator:
  774. values.has_decorator = (p->unit == Property::DECORATOR);
  775. break;
  776. case PropertyId::FontEffect:
  777. values.has_font_effect = (p->unit == Property::FONTEFFECT);
  778. break;
  779. case PropertyId::AlignContent:
  780. values.align_content = (AlignContent)p->Get<int>();
  781. break;
  782. case PropertyId::AlignItems:
  783. values.align_items = (AlignItems)p->Get<int>();
  784. break;
  785. case PropertyId::AlignSelf:
  786. values.align_self = (AlignSelf)p->Get<int>();
  787. break;
  788. case PropertyId::FlexBasis:
  789. values.flex_basis = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, vp_dimensions);
  790. break;
  791. case PropertyId::FlexDirection:
  792. values.flex_direction = (FlexDirection)p->Get<int>();
  793. break;
  794. case PropertyId::FlexGrow:
  795. values.flex_grow = p->Get<float>();
  796. break;
  797. case PropertyId::FlexShrink:
  798. values.flex_shrink = p->Get<float>();
  799. break;
  800. case PropertyId::FlexWrap:
  801. values.flex_wrap = (FlexWrap)p->Get<int>();
  802. break;
  803. case PropertyId::JustifyContent:
  804. values.justify_content = (JustifyContent)p->Get<int>();
  805. break;
  806. // Unhandled properties. Must be manually retrieved with 'GetProperty()'.
  807. case PropertyId::FillImage:
  808. case PropertyId::CaretColor:
  809. break;
  810. // Invalid properties
  811. case PropertyId::Invalid:
  812. case PropertyId::NumDefinedIds:
  813. case PropertyId::MaxNumIds:
  814. break;
  815. }
  816. }
  817. // The font-face handle is nulled when local font properties are set. In that case we need to retrieve a new handle.
  818. if (!values.font_face_handle)
  819. {
  820. RMLUI_ZoneScopedN("FontFaceHandle");
  821. values.font_face_handle = GetFontEngineInterface()->GetFontFaceHandle(values.font_family, values.font_style, values.font_weight, (int)values.font_size);
  822. }
  823. // Next, pass inheritable dirty properties onto our children
  824. PropertyIdSet dirty_inherited_properties = (dirty_properties & StyleSheetSpecification::GetRegisteredInheritedProperties());
  825. if (!dirty_inherited_properties.Empty())
  826. {
  827. for (int i = 0; i < element->GetNumChildren(true); i++)
  828. {
  829. auto child = element->GetChild(i);
  830. child->GetStyle()->dirty_properties |= dirty_inherited_properties;
  831. }
  832. }
  833. PropertyIdSet result(std::move(dirty_properties));
  834. dirty_properties.Clear();
  835. return result;
  836. }
  837. } // namespace Rml