ElementStyle.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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 "ElementStyleCache.h"
  30. #include <algorithm>
  31. #include "../../Include/Rocket/Core/ElementDocument.h"
  32. #include "../../Include/Rocket/Core/ElementUtilities.h"
  33. #include "../../Include/Rocket/Core/Log.h"
  34. #include "../../Include/Rocket/Core/Math.h"
  35. #include "../../Include/Rocket/Core/Property.h"
  36. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  37. #include "../../Include/Rocket/Core/PropertyDictionary.h"
  38. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  39. #include "../../Include/Rocket/Core/TransformPrimitive.h"
  40. #include "ElementBackground.h"
  41. #include "ElementBorder.h"
  42. #include "ElementDecoration.h"
  43. #include "ElementDefinition.h"
  44. #include "FontFaceHandle.h"
  45. namespace Rocket {
  46. namespace Core {
  47. static const Style::ComputedValues DefaultComputedValues;
  48. ElementStyle::ElementStyle(Element* _element)
  49. {
  50. local_properties = NULL;
  51. em_properties = NULL;
  52. definition = NULL;
  53. element = _element;
  54. cache = new ElementStyleCache(this);
  55. definition_dirty = true;
  56. }
  57. ElementStyle::~ElementStyle()
  58. {
  59. if (local_properties != NULL)
  60. delete local_properties;
  61. if (em_properties != NULL)
  62. delete em_properties;
  63. if (definition != NULL)
  64. definition->RemoveReference();
  65. delete cache;
  66. }
  67. // Returns the element's definition, updating if necessary.
  68. const ElementDefinition* ElementStyle::GetDefinition()
  69. {
  70. return definition;
  71. }
  72. // Returns one of this element's properties.
  73. const Property* ElementStyle::GetLocalProperty(const String& name, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  74. {
  75. // Check for overriding local properties.
  76. if (local_properties != NULL)
  77. {
  78. const Property* property = local_properties->GetProperty(name);
  79. if (property != NULL)
  80. return property;
  81. }
  82. // Check for a property defined in an RCSS rule.
  83. if (definition != NULL)
  84. return definition->GetProperty(name, pseudo_classes);
  85. return NULL;
  86. }
  87. // Returns one of this element's properties.
  88. const Property* ElementStyle::GetProperty(const String& name, Element* element, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  89. {
  90. const Property* local_property = GetLocalProperty(name, local_properties, definition, pseudo_classes);
  91. if (local_property != NULL)
  92. return local_property;
  93. // Fetch the property specification.
  94. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  95. if (property == NULL)
  96. return NULL;
  97. // If we can inherit this property, return our parent's property.
  98. if (property->IsInherited())
  99. {
  100. Element* parent = element->GetParentNode();
  101. while (parent != NULL)
  102. {
  103. const Property* parent_property = parent->GetStyle()->GetLocalProperty(name);
  104. if (parent_property)
  105. return parent_property;
  106. parent = parent->GetParentNode();
  107. }
  108. }
  109. // No property available! Return the default value.
  110. return property->GetDefaultValue();
  111. }
  112. // Apply transition to relevant properties if a transition is defined on element.
  113. // Properties that are part of a transition are removed from the properties list.
  114. void ElementStyle::TransitionPropertyChanges(Element* element, PropertyNameList& properties, PropertyDictionary* local_properties, ElementDefinition* old_definition, ElementDefinition* new_definition,
  115. const PseudoClassList& pseudo_classes_before, const PseudoClassList& pseudo_classes_after)
  116. {
  117. ROCKET_ASSERT(element);
  118. if (!old_definition || !new_definition || properties.empty())
  119. return;
  120. if (const Property* transition_property = GetLocalProperty(TRANSITION, local_properties, new_definition, pseudo_classes_after))
  121. {
  122. auto transition_list = transition_property->Get<TransitionList>();
  123. if (!transition_list.none)
  124. {
  125. auto add_transition = [&](const Transition& transition) {
  126. bool transition_added = false;
  127. const Property* start_value = GetProperty(transition.name, element, local_properties, old_definition, pseudo_classes_before);
  128. const Property* target_value = GetProperty(transition.name, element, nullptr, new_definition, pseudo_classes_after);
  129. if (start_value && target_value && (*start_value != *target_value))
  130. transition_added = element->StartTransition(transition, *start_value, *target_value);
  131. return transition_added;
  132. };
  133. if (transition_list.all)
  134. {
  135. Transition transition = transition_list.transitions[0];
  136. for (auto it = properties.begin(); it != properties.end(); )
  137. {
  138. transition.name = *it;
  139. if (add_transition(transition))
  140. it = properties.erase(it);
  141. else
  142. ++it;
  143. }
  144. }
  145. else
  146. {
  147. for (auto& transition : transition_list.transitions)
  148. {
  149. if (auto it = properties.find(transition.name); it != properties.end())
  150. {
  151. if (add_transition(transition))
  152. properties.erase(it);
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. void ElementStyle::UpdateDefinition()
  160. {
  161. if (definition_dirty)
  162. {
  163. definition_dirty = false;
  164. ElementDefinition* new_definition = NULL;
  165. const StyleSheet* style_sheet = GetStyleSheet();
  166. if (style_sheet != NULL)
  167. {
  168. new_definition = style_sheet->GetElementDefinition(element);
  169. }
  170. // Switch the property definitions if the definition has changed.
  171. if (new_definition != definition || new_definition == NULL)
  172. {
  173. PropertyNameList properties;
  174. if (definition != NULL)
  175. definition->GetDefinedProperties(properties, pseudo_classes);
  176. if (new_definition != NULL)
  177. new_definition->GetDefinedProperties(properties, pseudo_classes);
  178. TransitionPropertyChanges(element, properties, local_properties, definition, new_definition, pseudo_classes, pseudo_classes);
  179. if (definition != NULL)
  180. definition->RemoveReference();
  181. definition = new_definition;
  182. DirtyProperties(properties);
  183. element->GetElementDecoration()->DirtyDecorators(true);
  184. }
  185. else if (new_definition != NULL)
  186. {
  187. new_definition->RemoveReference();
  188. }
  189. }
  190. }
  191. // Sets or removes a pseudo-class on the element.
  192. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  193. {
  194. size_t num_pseudo_classes = pseudo_classes.size();
  195. auto pseudo_classes_before = pseudo_classes;
  196. if (activate)
  197. pseudo_classes.push_back(pseudo_class);
  198. else
  199. {
  200. // In case of duplicates, we do a loop here. We could do a sort and unique instead,
  201. // but that might even be slower for a small list with few duplicates, which
  202. // is probably the most common case.
  203. auto it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  204. while(it != pseudo_classes.end())
  205. {
  206. pseudo_classes.erase(it);
  207. it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  208. }
  209. }
  210. if (pseudo_classes.size() != num_pseudo_classes)
  211. {
  212. element->GetElementDecoration()->DirtyDecorators(false);
  213. if (definition != NULL)
  214. {
  215. PropertyNameList properties;
  216. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  217. TransitionPropertyChanges(element, properties, local_properties, definition, definition, pseudo_classes_before, pseudo_classes);
  218. DirtyProperties(properties);
  219. switch (definition->GetPseudoClassVolatility(pseudo_class))
  220. {
  221. case ElementDefinition::FONT_VOLATILE:
  222. element->DirtyFont();
  223. break;
  224. case ElementDefinition::STRUCTURE_VOLATILE:
  225. DirtyChildDefinitions();
  226. break;
  227. default:
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. // Checks if a specific pseudo-class has been set on the element.
  234. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  235. {
  236. return (std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class) != pseudo_classes.end());
  237. }
  238. const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
  239. {
  240. return pseudo_classes;
  241. }
  242. // Sets or removes a class on the element.
  243. void ElementStyle::SetClass(const String& class_name, bool activate)
  244. {
  245. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  246. if (activate)
  247. {
  248. if (class_location == classes.end())
  249. {
  250. classes.push_back(class_name);
  251. DirtyDefinition();
  252. }
  253. }
  254. else
  255. {
  256. if (class_location != classes.end())
  257. {
  258. classes.erase(class_location);
  259. DirtyDefinition();
  260. }
  261. }
  262. }
  263. // Checks if a class is set on the element.
  264. bool ElementStyle::IsClassSet(const String& class_name) const
  265. {
  266. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  267. }
  268. // Specifies the entire list of classes for this element. This will replace any others specified.
  269. void ElementStyle::SetClassNames(const String& class_names)
  270. {
  271. classes.clear();
  272. StringUtilities::ExpandString(classes, class_names, ' ');
  273. DirtyDefinition();
  274. }
  275. // Returns the list of classes specified for this element.
  276. String ElementStyle::GetClassNames() const
  277. {
  278. String class_names;
  279. for (size_t i = 0; i < classes.size(); i++)
  280. {
  281. if (i != 0)
  282. {
  283. class_names += " ";
  284. }
  285. class_names += classes[i];
  286. }
  287. return class_names;
  288. }
  289. // Sets a local property override on the element.
  290. bool ElementStyle::SetProperty(const String& name, const String& value)
  291. {
  292. if (local_properties == NULL)
  293. local_properties = new PropertyDictionary();
  294. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  295. {
  296. DirtyProperty(name);
  297. return true;
  298. }
  299. else
  300. {
  301. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.c_str(), value.c_str());
  302. return false;
  303. }
  304. }
  305. // Sets a local property override on the element to a pre-parsed value.
  306. bool ElementStyle::SetProperty(const String& name, const Property& property)
  307. {
  308. Property new_property = property;
  309. new_property.definition = StyleSheetSpecification::GetProperty(name);
  310. if (new_property.definition == NULL)
  311. return false;
  312. if (local_properties == NULL)
  313. local_properties = new PropertyDictionary();
  314. local_properties->SetProperty(name, new_property);
  315. DirtyProperty(name);
  316. return true;
  317. }
  318. // Removes a local property override on the element.
  319. void ElementStyle::RemoveProperty(const String& name)
  320. {
  321. if (local_properties == NULL)
  322. return;
  323. if (local_properties->GetProperty(name) != NULL)
  324. {
  325. local_properties->RemoveProperty(name);
  326. DirtyProperty(name);
  327. }
  328. }
  329. // Returns one of this element's properties.
  330. const Property* ElementStyle::GetProperty(const String& name)
  331. {
  332. return GetProperty(name, element, local_properties, definition, pseudo_classes);
  333. }
  334. // Returns one of this element's properties.
  335. const Property* ElementStyle::GetLocalProperty(const String& name)
  336. {
  337. return GetLocalProperty(name, local_properties, definition, pseudo_classes);
  338. }
  339. const PropertyMap * ElementStyle::GetLocalProperties() const
  340. {
  341. if (local_properties)
  342. return &local_properties->GetProperties();
  343. return NULL;
  344. }
  345. float ElementStyle::ResolveLength(const Property * property)
  346. {
  347. if (!property)
  348. {
  349. ROCKET_ERROR;
  350. return 0.0f;
  351. }
  352. if (!(property->unit & Property::LENGTH))
  353. {
  354. ROCKET_ERRORMSG("Trying to resolve length on a non-length property.");
  355. return 0.0f;
  356. }
  357. switch (property->unit)
  358. {
  359. case Property::NUMBER:
  360. case Property::PX:
  361. return property->value.Get< float >();
  362. case Property::EM:
  363. return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
  364. case Property::REM:
  365. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  366. case Property::DP:
  367. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  368. }
  369. // Values based on pixels-per-inch.
  370. if (property->unit & Property::PPI_UNIT)
  371. {
  372. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  373. switch (property->unit)
  374. {
  375. case Property::INCH: // inch
  376. return inch;
  377. case Property::CM: // centimeter
  378. return inch * (1.0f / 2.54f);
  379. case Property::MM: // millimeter
  380. return inch * (1.0f / 25.4f);
  381. case Property::PT: // point
  382. return inch * (1.0f / 72.0f);
  383. case Property::PC: // pica
  384. return inch * (1.0f / 6.0f);
  385. }
  386. }
  387. // We're not a numeric property; return 0.
  388. return 0.0f;
  389. }
  390. float ElementStyle::ResolveAngle(const Property * property)
  391. {
  392. switch (property->unit)
  393. {
  394. case Property::NUMBER:
  395. case Property::DEG:
  396. return Math::DegreesToRadians(property->value.Get< float >());
  397. case Property::RAD:
  398. return property->value.Get< float >();
  399. case Property::PERCENT:
  400. return property->value.Get< float >() * 0.01f * 2.0f * Math::ROCKET_PI;
  401. }
  402. ROCKET_ERRORMSG("Trying to resolve angle on a non-angle property.");
  403. return 0.0f;
  404. }
  405. float ElementStyle::ResolveNumericProperty(const String& property_name, const Property * property)
  406. {
  407. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && property_name == FONT_SIZE))
  408. {
  409. return ResolveLength(property);
  410. }
  411. auto definition = property->definition;
  412. if (!definition) definition = StyleSheetSpecification::GetProperty(property_name);
  413. if (!definition) return 0.0f;
  414. auto relative_target = definition->GetRelativeTarget();
  415. return ResolveNumericProperty(property, relative_target);
  416. }
  417. float ElementStyle::ResolveNumericProperty(const Property * property, RelativeTarget relative_target)
  418. {
  419. // There is an exception on font-size properties, as 'em' units here refer to parent font size instead
  420. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && relative_target == RelativeTarget::ParentFontSize))
  421. {
  422. return ResolveLength(property);
  423. }
  424. float base_value = 0.0f;
  425. switch (relative_target)
  426. {
  427. case RelativeTarget::None:
  428. base_value = 1.0f;
  429. break;
  430. case RelativeTarget::ContainingBlockWidth:
  431. base_value = element->GetContainingBlock().x;
  432. break;
  433. case RelativeTarget::ContainingBlockHeight:
  434. base_value = element->GetContainingBlock().y;
  435. break;
  436. case RelativeTarget::FontSize:
  437. base_value = (float)ElementUtilities::GetFontSize(element);
  438. break;
  439. case RelativeTarget::ParentFontSize:
  440. base_value = (float)ElementUtilities::GetFontSize(element->GetParentNode());
  441. break;
  442. case RelativeTarget::LineHeight:
  443. base_value = (float)ElementUtilities::GetLineHeight(element);
  444. break;
  445. default:
  446. break;
  447. }
  448. float scale_value = 0.0f;
  449. switch (property->unit)
  450. {
  451. case Property::EM:
  452. case Property::NUMBER:
  453. scale_value = property->value.Get< float >();
  454. break;
  455. case Property::PERCENT:
  456. scale_value = property->value.Get< float >() * 0.01f;
  457. break;
  458. }
  459. return base_value * scale_value;
  460. }
  461. // Resolves one of this element's properties.
  462. float ElementStyle::ResolveProperty(const Property* property, float base_value)
  463. {
  464. if (!property)
  465. {
  466. ROCKET_ERROR;
  467. return 0.0f;
  468. }
  469. switch (property->unit)
  470. {
  471. case Property::NUMBER:
  472. case Property::PX:
  473. case Property::RAD:
  474. return property->value.Get< float >();
  475. case Property::PERCENT:
  476. return base_value * property->value.Get< float >() * 0.01f;
  477. case Property::EM:
  478. return property->value.Get< float >() * (float)ElementUtilities::GetFontSize(element);
  479. case Property::REM:
  480. return property->value.Get< float >() * (float)ElementUtilities::GetFontSize(element->GetOwnerDocument());
  481. case Property::DP:
  482. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  483. case Property::DEG:
  484. return Math::DegreesToRadians(property->value.Get< float >());
  485. }
  486. // Values based on pixels-per-inch.
  487. if (property->unit & Property::PPI_UNIT)
  488. {
  489. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  490. switch (property->unit)
  491. {
  492. case Property::INCH: // inch
  493. return inch;
  494. case Property::CM: // centimeter
  495. return inch * (1.0f / 2.54f);
  496. case Property::MM: // millimeter
  497. return inch * (1.0f / 25.4f);
  498. case Property::PT: // point
  499. return inch * (1.0f / 72.0f);
  500. case Property::PC: // pica
  501. return inch * (1.0f / 6.0f);
  502. }
  503. }
  504. // We're not a numeric property; return 0.
  505. return 0.0f;
  506. }
  507. // Resolves one of this element's properties.
  508. float ElementStyle::ResolveProperty(const String& name, float base_value)
  509. {
  510. const Property* property = GetProperty(name);
  511. if (!property)
  512. {
  513. ROCKET_ERROR;
  514. return 0.0f;
  515. }
  516. // The calculated value of the font-size property is inherited, so we need to check if this
  517. // is an inherited property. If so, then we return our parent's font size instead.
  518. if (name == FONT_SIZE && property->unit & Property::RELATIVE_UNIT)
  519. {
  520. // If the rem unit is used, the font-size is inherited directly from the document,
  521. // otherwise we use the parent's font size.
  522. if (property->unit & Property::REM)
  523. {
  524. Rocket::Core::ElementDocument* owner_document = element->GetOwnerDocument();
  525. if (owner_document == NULL)
  526. return 0;
  527. base_value = owner_document->ResolveProperty(FONT_SIZE, 0);
  528. }
  529. else
  530. {
  531. Rocket::Core::Element* parent = element->GetParentNode();
  532. if (parent == NULL)
  533. return 0;
  534. if (GetLocalProperty(FONT_SIZE) == NULL)
  535. return parent->ResolveProperty(FONT_SIZE, 0);
  536. // The base value for font size is always the height of *this* element's parent's font.
  537. base_value = parent->ResolveProperty(FONT_SIZE, 0);
  538. }
  539. switch (property->unit)
  540. {
  541. case Property::PERCENT:
  542. return base_value * property->value.Get< float >() * 0.01f;
  543. case Property::EM:
  544. return property->value.Get< float >() * base_value;
  545. case Property::REM:
  546. // If an rem-relative font size is specified, it is expressed relative to the document's
  547. // font height.
  548. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  549. }
  550. }
  551. return ResolveProperty(property, base_value);
  552. }
  553. // Iterates over the properties defined on the element.
  554. bool ElementStyle::IterateProperties(int& index, String& name, const Property*& property, const PseudoClassList** property_pseudo_classes)
  555. {
  556. // First check for locally defined properties.
  557. if (local_properties != NULL)
  558. {
  559. if (index < local_properties->GetNumProperties())
  560. {
  561. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  562. for (int count = 0; count < index; ++count)
  563. ++i;
  564. name = (*i).first;
  565. property = &((*i).second);
  566. if (property_pseudo_classes)
  567. * property_pseudo_classes = nullptr;
  568. ++index;
  569. return true;
  570. }
  571. }
  572. const ElementDefinition* definition = GetDefinition();
  573. if (definition != NULL)
  574. {
  575. int index_offset = 0;
  576. if (local_properties != NULL)
  577. index_offset = local_properties->GetNumProperties();
  578. // Offset the index to be relative to the definition before we start indexing. When we do get a property back,
  579. // check that it hasn't been overridden by the element's local properties; if so, continue on to the next one.
  580. index -= index_offset;
  581. while (definition->IterateProperties(index, pseudo_classes, name, property, property_pseudo_classes))
  582. {
  583. if (local_properties == NULL ||
  584. local_properties->GetProperty(name) == NULL)
  585. {
  586. index += index_offset;
  587. return true;
  588. }
  589. }
  590. return false;
  591. }
  592. return false;
  593. }
  594. // Returns the active style sheet for this element. This may be NULL.
  595. StyleSheet* ElementStyle::GetStyleSheet() const
  596. {
  597. ElementDocument* document = element->GetOwnerDocument();
  598. if (document != NULL)
  599. return document->GetStyleSheet();
  600. return NULL;
  601. }
  602. void ElementStyle::DirtyDefinition()
  603. {
  604. definition_dirty = true;
  605. DirtyChildDefinitions();
  606. }
  607. void ElementStyle::DirtyChildDefinitions()
  608. {
  609. for (int i = 0; i < element->GetNumChildren(true); i++)
  610. element->GetChild(i)->GetStyle()->DirtyDefinition();
  611. }
  612. // Dirties every property.
  613. void ElementStyle::DirtyProperties()
  614. {
  615. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  616. DirtyProperties(properties);
  617. }
  618. // Dirties em-relative properties.
  619. void ElementStyle::DirtyEmProperties()
  620. {
  621. if (!em_properties)
  622. {
  623. // Check if any of these are currently em-relative. If so, dirty them.
  624. em_properties = new PropertyNameList;
  625. for (auto& property : StyleSheetSpecification::GetRegisteredProperties())
  626. {
  627. // Skip font-size; this is relative to our parent's em, not ours.
  628. if (property == FONT_SIZE)
  629. continue;
  630. // Get this property from this element. If this is em-relative, then add it to the list to
  631. // dirty.
  632. if (element->GetProperty(property)->unit == Property::EM)
  633. em_properties->insert(property);
  634. }
  635. }
  636. if (!em_properties->empty())
  637. DirtyProperties(*em_properties, false);
  638. // Now dirty all of our descendant's font-size properties that are relative to ems.
  639. int num_children = element->GetNumChildren(true);
  640. for (int i = 0; i < num_children; ++i)
  641. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  642. }
  643. // Dirties font-size on child elements if appropriate.
  644. void ElementStyle::DirtyInheritedEmProperties()
  645. {
  646. const Property* font_size = element->GetLocalProperty(FONT_SIZE);
  647. if (font_size == NULL)
  648. {
  649. int num_children = element->GetNumChildren(true);
  650. for (int i = 0; i < num_children; ++i)
  651. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  652. }
  653. else
  654. {
  655. if (font_size->unit & Property::RELATIVE_UNIT)
  656. DirtyProperty(FONT_SIZE);
  657. }
  658. }
  659. // Dirties rem properties.
  660. void ElementStyle::DirtyRemProperties()
  661. {
  662. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  663. PropertyNameList rem_properties;
  664. // Dirty all the properties of this element that use the rem unit.
  665. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  666. {
  667. if (element->GetProperty(*list_iterator)->unit == Property::REM)
  668. rem_properties.insert(*list_iterator);
  669. }
  670. if (!rem_properties.empty())
  671. DirtyProperties(rem_properties, false);
  672. // Now dirty all of our descendant's properties that use the rem unit.
  673. int num_children = element->GetNumChildren(true);
  674. for (int i = 0; i < num_children; ++i)
  675. element->GetChild(i)->GetStyle()->DirtyRemProperties();
  676. }
  677. void ElementStyle::DirtyDpProperties()
  678. {
  679. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  680. PropertyNameList dp_properties;
  681. // Dirty all the properties of this element that use the dp unit.
  682. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  683. {
  684. if (element->GetProperty(*list_iterator)->unit == Property::DP)
  685. dp_properties.insert(*list_iterator);
  686. }
  687. if (!dp_properties.empty())
  688. DirtyProperties(dp_properties, false);
  689. // Now dirty all of our descendant's properties that use the dp unit.
  690. int num_children = element->GetNumChildren(true);
  691. for (int i = 0; i < num_children; ++i)
  692. element->GetChild(i)->GetStyle()->DirtyDpProperties();
  693. }
  694. // Sets a single property as dirty.
  695. void ElementStyle::DirtyProperty(const String& property)
  696. {
  697. PropertyNameList properties;
  698. properties.insert(String(property));
  699. DirtyProperties(properties);
  700. }
  701. // Sets a list of properties as dirty.
  702. void ElementStyle::DirtyProperties(const PropertyNameList& properties, bool clear_em_properties)
  703. {
  704. if (properties.empty())
  705. return;
  706. bool all_inherited_dirty =
  707. &properties == &StyleSheetSpecification::GetRegisteredProperties() ||
  708. StyleSheetSpecification::GetRegisteredProperties() == properties ||
  709. StyleSheetSpecification::GetRegisteredInheritedProperties() == properties;
  710. if (all_inherited_dirty)
  711. {
  712. const PropertyNameList &all_inherited_properties = StyleSheetSpecification::GetRegisteredInheritedProperties();
  713. for (int i = 0; i < element->GetNumChildren(true); i++)
  714. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(all_inherited_properties);
  715. }
  716. else
  717. {
  718. PropertyNameList inherited_properties;
  719. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  720. {
  721. // If this property is an inherited property, then push it into the list to be passed onto our children.
  722. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(*i);
  723. if (property != NULL &&
  724. property->IsInherited())
  725. inherited_properties.insert(*i);
  726. }
  727. // Pass the list of those properties that are inherited onto our children.
  728. if (!inherited_properties.empty())
  729. {
  730. for (int i = 0; i < element->GetNumChildren(true); i++)
  731. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  732. }
  733. }
  734. // Clear all cached properties.
  735. cache->Clear();
  736. cache->ClearInherited();
  737. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  738. if (clear_em_properties && em_properties != NULL)
  739. {
  740. delete em_properties;
  741. em_properties = NULL;
  742. }
  743. // And send the event.
  744. element->DirtyProperties(properties);
  745. }
  746. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  747. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  748. {
  749. bool clear_em_properties = em_properties != NULL;
  750. PropertyNameList inherited_properties;
  751. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  752. {
  753. const Property *property = GetLocalProperty((*i));
  754. if (property == NULL)
  755. {
  756. inherited_properties.insert(*i);
  757. if (!clear_em_properties && em_properties != NULL && em_properties->find((*i)) != em_properties->end()) {
  758. clear_em_properties = true;
  759. }
  760. }
  761. }
  762. if (inherited_properties.empty())
  763. return;
  764. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  765. if (clear_em_properties && em_properties != NULL)
  766. {
  767. delete em_properties;
  768. em_properties = NULL;
  769. }
  770. // Clear cached inherited properties.
  771. cache->ClearInherited();
  772. // Pass the list of those properties that this element doesn't override onto our children.
  773. for (int i = 0; i < element->GetNumChildren(true); i++)
  774. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  775. element->DirtyProperties(properties);
  776. }
  777. void ElementStyle::GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right )
  778. {
  779. cache->GetOffsetProperties(top, bottom, left, right);
  780. }
  781. void ElementStyle::GetBorderWidthProperties(const Property **border_top_width, const Property **border_bottom_width, const Property **border_left_width, const Property **bottom_right_width)
  782. {
  783. cache->GetBorderWidthProperties(border_top_width, border_bottom_width, border_left_width, bottom_right_width);
  784. }
  785. void ElementStyle::GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right)
  786. {
  787. cache->GetMarginProperties(margin_top, margin_bottom, margin_left, margin_right);
  788. }
  789. void ElementStyle::GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right)
  790. {
  791. cache->GetPaddingProperties(padding_top, padding_bottom, padding_left, padding_right);
  792. }
  793. void ElementStyle::GetDimensionProperties(const Property **width, const Property **height)
  794. {
  795. cache->GetDimensionProperties(width, height);
  796. }
  797. void ElementStyle::GetLocalDimensionProperties(const Property **width, const Property **height)
  798. {
  799. cache->GetLocalDimensionProperties(width, height);
  800. }
  801. void ElementStyle::GetOverflow(int *overflow_x, int *overflow_y)
  802. {
  803. cache->GetOverflow(overflow_x, overflow_y);
  804. }
  805. int ElementStyle::GetPosition()
  806. {
  807. return cache->GetPosition();
  808. }
  809. int ElementStyle::GetFloat()
  810. {
  811. return cache->GetFloat();
  812. }
  813. int ElementStyle::GetDisplay()
  814. {
  815. return cache->GetDisplay();
  816. }
  817. int ElementStyle::GetWhitespace()
  818. {
  819. return cache->GetWhitespace();
  820. }
  821. int ElementStyle::GetPointerEvents()
  822. {
  823. return cache->GetPointerEvents();
  824. }
  825. const Property *ElementStyle::GetLineHeightProperty()
  826. {
  827. return cache->GetLineHeightProperty();
  828. }
  829. int ElementStyle::GetTextAlign()
  830. {
  831. return cache->GetTextAlign();
  832. }
  833. int ElementStyle::GetTextTransform()
  834. {
  835. return cache->GetTextTransform();
  836. }
  837. const Property *ElementStyle::GetVerticalAlignProperty()
  838. {
  839. return cache->GetVerticalAlignProperty();
  840. }
  841. // Returns 'perspective' property value from element's style or local cache.
  842. const Property *ElementStyle::GetPerspective()
  843. {
  844. return element->GetProperty(PERSPECTIVE);
  845. }
  846. // Returns 'perspective-origin-x' property value from element's style or local cache.
  847. const Property *ElementStyle::GetPerspectiveOriginX()
  848. {
  849. return element->GetProperty(PERSPECTIVE_ORIGIN_X);
  850. }
  851. // Returns 'perspective-origin-y' property value from element's style or local cache.
  852. const Property *ElementStyle::GetPerspectiveOriginY()
  853. {
  854. return element->GetProperty(PERSPECTIVE_ORIGIN_Y);
  855. }
  856. // Returns 'transform' property value from element's style or local cache.
  857. const Property *ElementStyle::GetTransform()
  858. {
  859. return element->GetProperty(TRANSFORM);
  860. }
  861. // Returns 'transform-origin-x' property value from element's style or local cache.
  862. const Property *ElementStyle::GetTransformOriginX()
  863. {
  864. return element->GetProperty(TRANSFORM_ORIGIN_X);
  865. }
  866. // Returns 'transform-origin-y' property value from element's style or local cache.
  867. const Property *ElementStyle::GetTransformOriginY()
  868. {
  869. return element->GetProperty(TRANSFORM_ORIGIN_Y);
  870. }
  871. // Returns 'transform-origin-z' property value from element's style or local cache.
  872. const Property *ElementStyle::GetTransformOriginZ()
  873. {
  874. return element->GetProperty(TRANSFORM_ORIGIN_Z);
  875. }
  876. static float ComputeLength(const Property* property, float font_size, float document_font_size, float dp_ratio, float pixels_per_inch)
  877. {
  878. // Note that percentages are not lengths! They have to be resolved elsewhere.
  879. if (!property)
  880. {
  881. ROCKET_ERROR;
  882. return 0.0f;
  883. }
  884. switch (property->unit)
  885. {
  886. case Property::NUMBER:
  887. case Property::PX:
  888. case Property::RAD:
  889. return property->value.Get< float >();
  890. case Property::EM:
  891. return property->value.Get< float >() * font_size;
  892. case Property::REM:
  893. return property->value.Get< float >() * document_font_size;
  894. case Property::DP:
  895. return property->value.Get< float >() * dp_ratio;
  896. case Property::DEG:
  897. return Math::DegreesToRadians(property->value.Get< float >());
  898. default:
  899. break;
  900. }
  901. // Values based on pixels-per-inch.
  902. if (property->unit & Property::PPI_UNIT)
  903. {
  904. float inch = property->value.Get< float >() * pixels_per_inch;
  905. switch (property->unit)
  906. {
  907. case Property::INCH: // inch
  908. return inch;
  909. case Property::CM: // centimeter
  910. return inch * (1.0f / 2.54f);
  911. case Property::MM: // millimeter
  912. return inch * (1.0f / 25.4f);
  913. case Property::PT: // point
  914. return inch * (1.0f / 72.0f);
  915. case Property::PC: // pica
  916. return inch * (1.0f / 6.0f);
  917. default:
  918. break;
  919. }
  920. }
  921. // We're not a numeric property; return 0.
  922. return 0.0f;
  923. }
  924. static float ComputeAbsoluteLength(const Property& property, float dp_ratio, float pixels_per_inch)
  925. {
  926. ROCKET_ASSERT(property.unit & Property::ABSOLUTE_LENGTH);
  927. switch (property.unit)
  928. {
  929. case Property::PX:
  930. return property.value.Get< float >();
  931. case Property::DP:
  932. return property.value.Get< float >()* dp_ratio;
  933. default:
  934. // Values based on pixels-per-inch.
  935. if (property.unit & Property::PPI_UNIT)
  936. {
  937. float inch = property.value.Get< float >() * pixels_per_inch;
  938. switch (property.unit)
  939. {
  940. case Property::INCH: // inch
  941. return inch;
  942. case Property::CM: // centimeter
  943. return inch * (1.0f / 2.54f);
  944. case Property::MM: // millimeter
  945. return inch * (1.0f / 25.4f);
  946. case Property::PT: // point
  947. return inch * (1.0f / 72.0f);
  948. case Property::PC: // pica
  949. return inch * (1.0f / 6.0f);
  950. }
  951. }
  952. }
  953. ROCKET_ERROR;
  954. return 0.0f;
  955. }
  956. // Resolves one of this element's properties.
  957. static float ComputeFontsize(const Property& property, const Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio, float pixels_per_inch)
  958. {
  959. // The calculated value of the font-size property is inherited, so we need to check if this
  960. // is an inherited property. If so, then we return our parent's font size instead.
  961. if (property.unit & Property::RELATIVE_UNIT)
  962. {
  963. float multiplier = 1.0f;
  964. switch (property.unit)
  965. {
  966. case Property::PERCENT:
  967. multiplier = 0.01f;
  968. [[fallthrough]];
  969. case Property::EM:
  970. if (!parent_values)
  971. return 0;
  972. return property.value.Get< float >() * multiplier * parent_values->font_size;
  973. case Property::REM:
  974. if (!document_values)
  975. return 0;
  976. // If the current element is a document, the rem unit is relative to the default size
  977. if(&values == document_values)
  978. return property.value.Get< float >() * DefaultComputedValues.font_size;
  979. // Otherwise it is relative to the document font size
  980. return property.value.Get< float >() * document_values->font_size;
  981. default:
  982. ROCKET_ERRORMSG("A relative unit must be percentage, em or rem.");
  983. }
  984. }
  985. return ComputeAbsoluteLength(property, dp_ratio, pixels_per_inch);
  986. }
  987. static inline Style::Clip ComputeClip(const Property* property)
  988. {
  989. int value = property->Get<int>();
  990. if (property->unit == Property::KEYWORD)
  991. return (value == CLIP_NONE ? Style::Clip::None : Style::Clip::Auto);
  992. else if (property->unit == Property::NUMBER)
  993. return (Style::Clip)value;
  994. ROCKET_ERRORMSG("Invalid clip type");
  995. return Style::Clip::Auto;
  996. }
  997. static inline Style::LineHeight ComputeLineHeight(const Property* property, float font_size, float document_font_size, float dp_ratio, float pixels_per_inch)
  998. {
  999. if (property->unit & Property::LENGTH)
  1000. {
  1001. float value = ComputeLength(property, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1002. return Style::LineHeight(value, Style::LineHeight::Length, value);
  1003. }
  1004. float scale_factor = 1.0f;
  1005. switch (property->unit)
  1006. {
  1007. case Property::NUMBER:
  1008. scale_factor = property->value.Get< float >();
  1009. break;
  1010. case Property::PERCENT:
  1011. scale_factor = property->value.Get< float >() * 0.01f;
  1012. break;
  1013. default:
  1014. ROCKET_ERRORMSG("Invalid unit for line-height");
  1015. }
  1016. float value = font_size * scale_factor;
  1017. return Style::LineHeight(value, Style::LineHeight::Number, scale_factor);
  1018. }
  1019. static inline Style::VerticalAlign ComputeVerticalAlign(const Property* property, float line_height, float font_size, float document_font_size, float dp_ratio, float pixels_per_inch)
  1020. {
  1021. if (property->unit & Property::LENGTH)
  1022. {
  1023. float value = ComputeLength(property, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1024. return Style::VerticalAlign(value);
  1025. }
  1026. else if (property->unit & Property::PERCENT)
  1027. {
  1028. return Style::VerticalAlign(property->Get<float>() * line_height);
  1029. }
  1030. ROCKET_ASSERT(property->unit & Property::KEYWORD);
  1031. return Style::VerticalAlign((Style::VerticalAlign::Type)property->Get<int>());
  1032. }
  1033. static inline LengthPercentage ComputeLengthPercentage(const Property* property, float font_size, float document_font_size, float dp_ratio, float pixels_per_inch)
  1034. {
  1035. if (property->unit & Property::PERCENT)
  1036. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  1037. return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, pixels_per_inch));
  1038. }
  1039. static inline LengthPercentageAuto ComputeLengthPercentageAuto(const Property* property, float font_size, float document_font_size, float dp_ratio, float pixels_per_inch)
  1040. {
  1041. // Assuming here that 'auto' is the only possible keyword
  1042. if (property->unit & Property::PERCENT)
  1043. return LengthPercentageAuto(LengthPercentageAuto::Percentage, property->Get<float>());
  1044. else if (property->unit & Property::KEYWORD)
  1045. return LengthPercentageAuto(LengthPercentageAuto::Auto);
  1046. return LengthPercentageAuto(LengthPercentageAuto::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, pixels_per_inch));
  1047. }
  1048. static inline LengthPercentage ComputeOrigin(const Property* property, float font_size, float document_font_size, float dp_ratio, float pixels_per_inch)
  1049. {
  1050. using namespace Style;
  1051. static_assert((int)OriginX::Left == (int)OriginY::Top && (int)OriginX::Center == (int)OriginY::Center && (int)OriginX::Right == (int)OriginY::Bottom, "");
  1052. if (property->unit & Property::KEYWORD)
  1053. {
  1054. float percent = 0.0f;
  1055. OriginX origin = (OriginX)property->Get<int>();
  1056. switch (origin)
  1057. {
  1058. case OriginX::Left: percent = 0.0f; break;
  1059. case OriginX::Center: percent = 50.0f; break;
  1060. case OriginX::Right: percent = 100.f; break;
  1061. }
  1062. return LengthPercentage(LengthPercentage::Percentage, percent);
  1063. }
  1064. else if (property->unit & Property::PERCENT)
  1065. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  1066. return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, pixels_per_inch));
  1067. }
  1068. // Must be called in correct order, from document root to children elements.
  1069. void ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio, float pixels_per_inch)
  1070. {
  1071. // Always do font-size first if dirty, because of em-relative values
  1072. if (auto p = GetLocalProperty(FONT_SIZE))
  1073. values.font_size = ComputeFontsize(*p, values, parent_values, document_values, dp_ratio, pixels_per_inch);
  1074. else if (parent_values)
  1075. values.font_size = parent_values->font_size;
  1076. const float font_size = values.font_size;
  1077. const float document_font_size = (document_values ? document_values->font_size : DefaultComputedValues.font_size);
  1078. // Since vertical-align depends on line-height we compute this before iteration
  1079. if (auto p = GetLocalProperty(LINE_HEIGHT))
  1080. {
  1081. values.line_height = ComputeLineHeight(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1082. }
  1083. else if (parent_values)
  1084. {
  1085. // 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.
  1086. // See CSS specs for details. Percent is already converted to number.
  1087. if (parent_values->line_height.inherit_type == Style::LineHeight::Number)
  1088. values.line_height = Style::LineHeight(font_size * parent_values->line_height.inherit_value, Style::LineHeight::Number, parent_values->line_height.inherit_value);
  1089. else
  1090. values.line_height = parent_values->line_height;
  1091. }
  1092. if (parent_values)
  1093. {
  1094. // Inherited properties are copied here, but may be overwritten below by locally defined properties
  1095. // Line-height and font-size are computed above
  1096. values.clip = parent_values->clip;
  1097. values.color = parent_values->color;
  1098. values.opacity = parent_values->opacity;
  1099. values.font_family = parent_values->font_family;
  1100. values.font_charset = parent_values->font_charset;
  1101. values.font_style = parent_values->font_style;
  1102. values.font_weight = parent_values->font_weight;
  1103. values.text_align = parent_values->text_align;
  1104. values.text_decoration = parent_values->text_decoration;
  1105. values.text_transform = parent_values->text_transform;
  1106. values.white_space = parent_values->white_space;
  1107. values.cursor = parent_values->cursor;
  1108. values.focus = parent_values->focus;
  1109. values.pointer_events = parent_values->pointer_events;
  1110. }
  1111. int index = 0;
  1112. String name;
  1113. const Property* p = nullptr;
  1114. while (IterateProperties(index, name, p))
  1115. {
  1116. using namespace Style;
  1117. // @performance: Can use a switch-case with constexpr hashing function
  1118. // 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...
  1119. // @performance: Compare to the list of actually changed properties, skip if not inside it
  1120. if (name == MARGIN_TOP)
  1121. values.margin_top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1122. else if (name == MARGIN_RIGHT)
  1123. values.margin_right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1124. else if (name == MARGIN_BOTTOM)
  1125. values.margin_bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1126. else if (name == MARGIN_LEFT)
  1127. values.margin_top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1128. else if (name == PADDING_TOP)
  1129. values.padding_top = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1130. else if (name == PADDING_RIGHT)
  1131. values.padding_right = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1132. else if (name == PADDING_BOTTOM)
  1133. values.padding_bottom = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1134. else if (name == PADDING_LEFT)
  1135. values.padding_top = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1136. else if (name == BORDER_TOP_WIDTH)
  1137. values.border_top_width = ComputeLength(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1138. else if (name == BORDER_RIGHT_WIDTH)
  1139. values.border_right_width = ComputeLength(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1140. else if (name == BORDER_BOTTOM_WIDTH)
  1141. values.border_bottom_width = ComputeLength(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1142. else if (name == BORDER_LEFT_WIDTH)
  1143. values.border_top_width = ComputeLength(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1144. else if (name == BORDER_TOP_COLOR)
  1145. values.border_top_color = p->Get<Colourb>();
  1146. else if (name == BORDER_RIGHT_COLOR)
  1147. values.border_right_color = p->Get<Colourb>();
  1148. else if (name == BORDER_BOTTOM_COLOR)
  1149. values.border_bottom_color = p->Get<Colourb>();
  1150. else if (name == BORDER_LEFT_COLOR)
  1151. values.border_left_color = p->Get<Colourb>();
  1152. else if (name == DISPLAY)
  1153. values.display = (Display)p->Get<int>();
  1154. else if (name == POSITION)
  1155. values.position = (Position)p->Get<int>();
  1156. else if (name == TOP)
  1157. values.top = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1158. else if (name == RIGHT)
  1159. values.right = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1160. else if (name == BOTTOM)
  1161. values.bottom = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1162. else if (name == LEFT)
  1163. values.left = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1164. else if (name == FLOAT)
  1165. values.float_ = (Float)p->Get<int>();
  1166. else if (name == CLEAR)
  1167. values.clear = (Clear)p->Get<int>();
  1168. else if (name == Z_INDEX)
  1169. values.z_index = (p->unit == Property::KEYWORD ? NumberAuto(NumberAuto::Auto) : NumberAuto(NumberAuto::Number, p->Get<float>()));
  1170. else if (name == WIDTH)
  1171. values.width = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1172. else if (name == MIN_WIDTH)
  1173. values.min_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1174. else if (name == MAX_WIDTH)
  1175. values.max_width = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1176. else if (name == HEIGHT)
  1177. values.height = ComputeLengthPercentageAuto(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1178. else if (name == MIN_HEIGHT)
  1179. values.min_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1180. else if (name == MAX_HEIGHT)
  1181. values.max_height = ComputeLengthPercentage(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1182. // (Line-height computed above)
  1183. else if (name == VERTICAL_ALIGN)
  1184. values.vertical_align = ComputeVerticalAlign(p, values.line_height.value, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1185. else if (name == OVERFLOW_X)
  1186. values.overflow_x = (Overflow)p->Get< int >();
  1187. else if (name == OVERFLOW_Y)
  1188. values.overflow_y = (Overflow)p->Get< int >();
  1189. else if (name == CLIP)
  1190. values.clip = ComputeClip(p);
  1191. else if (name == VISIBILITY)
  1192. values.visibility = (Visibility)p->Get< int >();
  1193. else if (name == BACKGROUND_COLOR)
  1194. values.background_color = p->Get<Colourb>();
  1195. else if (name == COLOR)
  1196. values.color = p->Get<Colourb>();
  1197. else if (name == IMAGE_COLOR)
  1198. values.image_color = p->Get<Colourb>();
  1199. else if (name == OPACITY)
  1200. values.opacity = p->Get<float>();
  1201. else if (name == FONT_FAMILY)
  1202. values.font_family = p->Get<String>();
  1203. else if (name == FONT_CHARSET)
  1204. values.font_charset = p->Get<String>();
  1205. else if (name == FONT_STYLE)
  1206. values.font_style = (FontStyle)p->Get< int >();
  1207. else if (name == FONT_WEIGHT)
  1208. values.font_weight = (FontWeight)p->Get< int >();
  1209. // (font-size computed above)
  1210. else if (name == TEXT_ALIGN)
  1211. values.text_align = (TextAlign)p->Get< int >();
  1212. else if (name == TEXT_DECORATION)
  1213. values.text_decoration = (TextDecoration)p->Get< int >();
  1214. else if (name == TEXT_TRANSFORM)
  1215. values.text_transform = (TextTransform)p->Get< int >();
  1216. else if (name == WHITE_SPACE)
  1217. values.white_space = (WhiteSpace)p->Get< int >();
  1218. else if (name == CURSOR)
  1219. values.cursor = p->Get< String >();
  1220. else if (name == DRAG)
  1221. values.drag = (Drag)p->Get< int >();
  1222. else if (name == TAB_INDEX)
  1223. values.tab_index = (TabIndex)p->Get< int >();
  1224. else if (name == FOCUS)
  1225. values.focus = (Focus)p->Get<int>();
  1226. else if (name == SCROLLBAR_MARGIN)
  1227. values.scrollbar_margin = ComputeLength(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1228. else if (name == POINTER_EVENTS)
  1229. values.pointer_events = (PointerEvents)p->Get<int>();
  1230. else if (name == PERSPECTIVE)
  1231. values.perspective = ComputeLength(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1232. else if (name == PERSPECTIVE_ORIGIN_X)
  1233. values.perspective_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1234. else if (name == PERSPECTIVE_ORIGIN_Y)
  1235. values.perspective_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1236. else if (name == TRANSFORM)
  1237. values.transform = p->Get<TransformRef>();
  1238. else if(name == TRANSFORM_ORIGIN_X)
  1239. values.transform_origin_x = ComputeOrigin(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1240. else if (name == TRANSFORM_ORIGIN_Y)
  1241. values.transform_origin_y = ComputeOrigin(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1242. else if (name == TRANSFORM_ORIGIN_Z)
  1243. values.transform_origin_z = ComputeLength(p, font_size, document_font_size, dp_ratio, pixels_per_inch);
  1244. else if (name == TRANSITION)
  1245. values.transition = p->Get<TransitionList>();
  1246. else if (name == ANIMATION)
  1247. values.animation = p->Get<AnimationList>();
  1248. }
  1249. }
  1250. }
  1251. }