ElementStyle.cpp 31 KB

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