ElementStyle.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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. #include "RCSS.h"
  46. namespace Rocket {
  47. namespace Core {
  48. static const Style::ComputedValues DefaultComputedValues;
  49. ElementStyle::ElementStyle(Element* _element)
  50. {
  51. local_properties = NULL;
  52. em_properties = NULL;
  53. definition = NULL;
  54. element = _element;
  55. cache = new ElementStyleCache(this);
  56. definition_dirty = true;
  57. }
  58. ElementStyle::~ElementStyle()
  59. {
  60. if (local_properties != NULL)
  61. delete local_properties;
  62. if (em_properties != NULL)
  63. delete em_properties;
  64. if (definition != NULL)
  65. definition->RemoveReference();
  66. delete cache;
  67. }
  68. // Returns the element's definition, updating if necessary.
  69. const ElementDefinition* ElementStyle::GetDefinition()
  70. {
  71. return definition;
  72. }
  73. // Returns one of this element's properties.
  74. const Property* ElementStyle::GetLocalProperty(const String& name, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  75. {
  76. // Check for overriding local properties.
  77. if (local_properties != NULL)
  78. {
  79. const Property* property = local_properties->GetProperty(name);
  80. if (property != NULL)
  81. return property;
  82. }
  83. // Check for a property defined in an RCSS rule.
  84. if (definition != NULL)
  85. return definition->GetProperty(name, pseudo_classes);
  86. return NULL;
  87. }
  88. // Returns one of this element's properties.
  89. const Property* ElementStyle::GetProperty(const String& name, Element* element, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  90. {
  91. const Property* local_property = GetLocalProperty(name, local_properties, definition, pseudo_classes);
  92. if (local_property != NULL)
  93. return local_property;
  94. // Fetch the property specification.
  95. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  96. if (property == NULL)
  97. return NULL;
  98. // If we can inherit this property, return our parent's property.
  99. if (property->IsInherited())
  100. {
  101. Element* parent = element->GetParentNode();
  102. while (parent != NULL)
  103. {
  104. const Property* parent_property = parent->GetStyle()->GetLocalProperty(name);
  105. if (parent_property)
  106. return parent_property;
  107. parent = parent->GetParentNode();
  108. }
  109. }
  110. // No property available! Return the default value.
  111. return property->GetDefaultValue();
  112. }
  113. // Apply transition to relevant properties if a transition is defined on element.
  114. // Properties that are part of a transition are removed from the properties list.
  115. void ElementStyle::TransitionPropertyChanges(Element* element, PropertyNameList& properties, PropertyDictionary* local_properties, ElementDefinition* old_definition, ElementDefinition* new_definition,
  116. const PseudoClassList& pseudo_classes_before, const PseudoClassList& pseudo_classes_after)
  117. {
  118. ROCKET_ASSERT(element);
  119. if (!old_definition || !new_definition || properties.empty())
  120. return;
  121. if (const Property* transition_property = GetLocalProperty(TRANSITION, local_properties, new_definition, pseudo_classes_after))
  122. {
  123. auto transition_list = transition_property->Get<TransitionList>();
  124. if (!transition_list.none)
  125. {
  126. auto add_transition = [&](const Transition& transition) {
  127. bool transition_added = false;
  128. const Property* start_value = GetProperty(transition.name, element, local_properties, old_definition, pseudo_classes_before);
  129. const Property* target_value = GetProperty(transition.name, element, nullptr, new_definition, pseudo_classes_after);
  130. if (start_value && target_value && (*start_value != *target_value))
  131. transition_added = element->StartTransition(transition, *start_value, *target_value);
  132. return transition_added;
  133. };
  134. if (transition_list.all)
  135. {
  136. Transition transition = transition_list.transitions[0];
  137. for (auto it = properties.begin(); it != properties.end(); )
  138. {
  139. transition.name = *it;
  140. if (add_transition(transition))
  141. it = properties.erase(it);
  142. else
  143. ++it;
  144. }
  145. }
  146. else
  147. {
  148. for (auto& transition : transition_list.transitions)
  149. {
  150. if (auto it = properties.find(transition.name); it != properties.end())
  151. {
  152. if (add_transition(transition))
  153. properties.erase(it);
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. void ElementStyle::UpdateDefinition()
  161. {
  162. if (definition_dirty)
  163. {
  164. definition_dirty = false;
  165. ElementDefinition* new_definition = NULL;
  166. const StyleSheet* style_sheet = GetStyleSheet();
  167. if (style_sheet != NULL)
  168. {
  169. new_definition = style_sheet->GetElementDefinition(element);
  170. }
  171. // Switch the property definitions if the definition has changed.
  172. if (new_definition != definition || new_definition == NULL)
  173. {
  174. PropertyNameList properties;
  175. if (definition != NULL)
  176. definition->GetDefinedProperties(properties, pseudo_classes);
  177. if (new_definition != NULL)
  178. new_definition->GetDefinedProperties(properties, pseudo_classes);
  179. TransitionPropertyChanges(element, properties, local_properties, definition, new_definition, pseudo_classes, pseudo_classes);
  180. if (definition != NULL)
  181. definition->RemoveReference();
  182. definition = new_definition;
  183. DirtyProperties(properties);
  184. element->GetElementDecoration()->DirtyDecorators(true);
  185. }
  186. else if (new_definition != NULL)
  187. {
  188. new_definition->RemoveReference();
  189. }
  190. }
  191. }
  192. // Sets or removes a pseudo-class on the element.
  193. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  194. {
  195. size_t num_pseudo_classes = pseudo_classes.size();
  196. auto pseudo_classes_before = pseudo_classes;
  197. if (activate)
  198. pseudo_classes.push_back(pseudo_class);
  199. else
  200. {
  201. // In case of duplicates, we do a loop here. We could do a sort and unique instead,
  202. // but that might even be slower for a small list with few duplicates, which
  203. // is probably the most common case.
  204. auto it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  205. while(it != pseudo_classes.end())
  206. {
  207. pseudo_classes.erase(it);
  208. it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
  209. }
  210. }
  211. if (pseudo_classes.size() != num_pseudo_classes)
  212. {
  213. element->GetElementDecoration()->DirtyDecorators(false);
  214. if (definition != NULL)
  215. {
  216. PropertyNameList properties;
  217. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  218. TransitionPropertyChanges(element, properties, local_properties, definition, definition, pseudo_classes_before, pseudo_classes);
  219. DirtyProperties(properties);
  220. switch (definition->GetPseudoClassVolatility(pseudo_class))
  221. {
  222. case ElementDefinition::FONT_VOLATILE:
  223. element->DirtyFont();
  224. break;
  225. case ElementDefinition::STRUCTURE_VOLATILE:
  226. DirtyChildDefinitions();
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. }
  233. }
  234. // Checks if a specific pseudo-class has been set on the element.
  235. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  236. {
  237. return (std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class) != pseudo_classes.end());
  238. }
  239. const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
  240. {
  241. return pseudo_classes;
  242. }
  243. // Sets or removes a class on the element.
  244. void ElementStyle::SetClass(const String& class_name, bool activate)
  245. {
  246. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  247. if (activate)
  248. {
  249. if (class_location == classes.end())
  250. {
  251. classes.push_back(class_name);
  252. DirtyDefinition();
  253. }
  254. }
  255. else
  256. {
  257. if (class_location != classes.end())
  258. {
  259. classes.erase(class_location);
  260. DirtyDefinition();
  261. }
  262. }
  263. }
  264. // Checks if a class is set on the element.
  265. bool ElementStyle::IsClassSet(const String& class_name) const
  266. {
  267. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  268. }
  269. // Specifies the entire list of classes for this element. This will replace any others specified.
  270. void ElementStyle::SetClassNames(const String& class_names)
  271. {
  272. classes.clear();
  273. StringUtilities::ExpandString(classes, class_names, ' ');
  274. DirtyDefinition();
  275. }
  276. // Returns the list of classes specified for this element.
  277. String ElementStyle::GetClassNames() const
  278. {
  279. String class_names;
  280. for (size_t i = 0; i < classes.size(); i++)
  281. {
  282. if (i != 0)
  283. {
  284. class_names += " ";
  285. }
  286. class_names += classes[i];
  287. }
  288. return class_names;
  289. }
  290. // Sets a local property override on the element.
  291. bool ElementStyle::SetProperty(const String& name, const String& value)
  292. {
  293. if (local_properties == NULL)
  294. local_properties = new PropertyDictionary();
  295. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  296. {
  297. DirtyProperty(name);
  298. return true;
  299. }
  300. else
  301. {
  302. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.c_str(), value.c_str());
  303. return false;
  304. }
  305. }
  306. // Sets a local property override on the element to a pre-parsed value.
  307. bool ElementStyle::SetProperty(const String& name, const Property& property)
  308. {
  309. Property new_property = property;
  310. new_property.definition = StyleSheetSpecification::GetProperty(name);
  311. if (new_property.definition == NULL)
  312. return false;
  313. if (local_properties == NULL)
  314. local_properties = new PropertyDictionary();
  315. local_properties->SetProperty(name, new_property);
  316. DirtyProperty(name);
  317. return true;
  318. }
  319. // Removes a local property override on the element.
  320. void ElementStyle::RemoveProperty(const String& name)
  321. {
  322. if (local_properties == NULL)
  323. return;
  324. if (local_properties->GetProperty(name) != NULL)
  325. {
  326. local_properties->RemoveProperty(name);
  327. DirtyProperty(name);
  328. }
  329. }
  330. // Returns one of this element's properties.
  331. const Property* ElementStyle::GetProperty(const String& name)
  332. {
  333. return GetProperty(name, element, local_properties, definition, pseudo_classes);
  334. }
  335. // Returns one of this element's properties.
  336. const Property* ElementStyle::GetLocalProperty(const String& name)
  337. {
  338. return GetLocalProperty(name, local_properties, definition, pseudo_classes);
  339. }
  340. const PropertyMap * ElementStyle::GetLocalProperties() const
  341. {
  342. if (local_properties)
  343. return &local_properties->GetProperties();
  344. return NULL;
  345. }
  346. float ElementStyle::ResolveLength(const Property * property)
  347. {
  348. if (!property)
  349. {
  350. ROCKET_ERROR;
  351. return 0.0f;
  352. }
  353. if (!(property->unit & Property::LENGTH))
  354. {
  355. ROCKET_ERRORMSG("Trying to resolve length on a non-length property.");
  356. return 0.0f;
  357. }
  358. switch (property->unit)
  359. {
  360. case Property::NUMBER:
  361. case Property::PX:
  362. return property->value.Get< float >();
  363. case Property::EM:
  364. return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
  365. case Property::REM:
  366. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  367. case Property::DP:
  368. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  369. }
  370. // Values based on pixels-per-inch.
  371. if (property->unit & Property::PPI_UNIT)
  372. {
  373. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  374. switch (property->unit)
  375. {
  376. case Property::INCH: // inch
  377. return inch;
  378. case Property::CM: // centimeter
  379. return inch * (1.0f / 2.54f);
  380. case Property::MM: // millimeter
  381. return inch * (1.0f / 25.4f);
  382. case Property::PT: // point
  383. return inch * (1.0f / 72.0f);
  384. case Property::PC: // pica
  385. return inch * (1.0f / 6.0f);
  386. }
  387. }
  388. // We're not a numeric property; return 0.
  389. return 0.0f;
  390. }
  391. float ElementStyle::ResolveAngle(const Property * property)
  392. {
  393. switch (property->unit)
  394. {
  395. case Property::NUMBER:
  396. case Property::DEG:
  397. return Math::DegreesToRadians(property->value.Get< float >());
  398. case Property::RAD:
  399. return property->value.Get< float >();
  400. case Property::PERCENT:
  401. return property->value.Get< float >() * 0.01f * 2.0f * Math::ROCKET_PI;
  402. }
  403. ROCKET_ERRORMSG("Trying to resolve angle on a non-angle property.");
  404. return 0.0f;
  405. }
  406. float ElementStyle::ResolveNumericProperty(const String& property_name, const Property * property)
  407. {
  408. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && property_name == FONT_SIZE))
  409. {
  410. return ResolveLength(property);
  411. }
  412. auto definition = property->definition;
  413. if (!definition) definition = StyleSheetSpecification::GetProperty(property_name);
  414. if (!definition) return 0.0f;
  415. auto relative_target = definition->GetRelativeTarget();
  416. return ResolveNumericProperty(property, relative_target);
  417. }
  418. float ElementStyle::ResolveNumericProperty(const Property * property, RelativeTarget relative_target)
  419. {
  420. // There is an exception on font-size properties, as 'em' units here refer to parent font size instead
  421. if ((property->unit & Property::LENGTH) && !(property->unit == Property::EM && relative_target == RelativeTarget::ParentFontSize))
  422. {
  423. return ResolveLength(property);
  424. }
  425. float base_value = 0.0f;
  426. switch (relative_target)
  427. {
  428. case RelativeTarget::None:
  429. base_value = 1.0f;
  430. break;
  431. case RelativeTarget::ContainingBlockWidth:
  432. base_value = element->GetContainingBlock().x;
  433. break;
  434. case RelativeTarget::ContainingBlockHeight:
  435. base_value = element->GetContainingBlock().y;
  436. break;
  437. case RelativeTarget::FontSize:
  438. base_value = (float)ElementUtilities::GetFontSize(element);
  439. break;
  440. case RelativeTarget::ParentFontSize:
  441. base_value = (float)ElementUtilities::GetFontSize(element->GetParentNode());
  442. break;
  443. case RelativeTarget::LineHeight:
  444. base_value = (float)ElementUtilities::GetLineHeight(element);
  445. break;
  446. default:
  447. break;
  448. }
  449. float scale_value = 0.0f;
  450. switch (property->unit)
  451. {
  452. case Property::EM:
  453. case Property::NUMBER:
  454. scale_value = property->value.Get< float >();
  455. break;
  456. case Property::PERCENT:
  457. scale_value = property->value.Get< float >() * 0.01f;
  458. break;
  459. }
  460. return base_value * scale_value;
  461. }
  462. // Resolves one of this element's properties.
  463. float ElementStyle::ResolveProperty(const Property* property, float base_value)
  464. {
  465. if (!property)
  466. {
  467. ROCKET_ERROR;
  468. return 0.0f;
  469. }
  470. switch (property->unit)
  471. {
  472. case Property::NUMBER:
  473. case Property::PX:
  474. case Property::RAD:
  475. return property->value.Get< float >();
  476. case Property::PERCENT:
  477. return base_value * property->value.Get< float >() * 0.01f;
  478. case Property::EM:
  479. return property->value.Get< float >() * (float)ElementUtilities::GetFontSize(element);
  480. case Property::REM:
  481. return property->value.Get< float >() * (float)ElementUtilities::GetFontSize(element->GetOwnerDocument());
  482. case Property::DP:
  483. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  484. case Property::DEG:
  485. return Math::DegreesToRadians(property->value.Get< float >());
  486. }
  487. // Values based on pixels-per-inch.
  488. if (property->unit & Property::PPI_UNIT)
  489. {
  490. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  491. switch (property->unit)
  492. {
  493. case Property::INCH: // inch
  494. return inch;
  495. case Property::CM: // centimeter
  496. return inch * (1.0f / 2.54f);
  497. case Property::MM: // millimeter
  498. return inch * (1.0f / 25.4f);
  499. case Property::PT: // point
  500. return inch * (1.0f / 72.0f);
  501. case Property::PC: // pica
  502. return inch * (1.0f / 6.0f);
  503. }
  504. }
  505. // We're not a numeric property; return 0.
  506. return 0.0f;
  507. }
  508. // Resolves one of this element's properties.
  509. float ElementStyle::ResolveProperty(const String& name, float base_value)
  510. {
  511. const Property* property = GetProperty(name);
  512. if (!property)
  513. {
  514. ROCKET_ERROR;
  515. return 0.0f;
  516. }
  517. // The calculated value of the font-size property is inherited, so we need to check if this
  518. // is an inherited property. If so, then we return our parent's font size instead.
  519. if (name == FONT_SIZE && property->unit & Property::RELATIVE_UNIT)
  520. {
  521. // If the rem unit is used, the font-size is inherited directly from the document,
  522. // otherwise we use the parent's font size.
  523. if (property->unit & Property::REM)
  524. {
  525. Rocket::Core::ElementDocument* owner_document = element->GetOwnerDocument();
  526. if (owner_document == NULL)
  527. return 0;
  528. base_value = owner_document->ResolveProperty(FONT_SIZE, 0);
  529. }
  530. else
  531. {
  532. Rocket::Core::Element* parent = element->GetParentNode();
  533. if (parent == NULL)
  534. return 0;
  535. if (GetLocalProperty(FONT_SIZE) == NULL)
  536. return parent->ResolveProperty(FONT_SIZE, 0);
  537. // The base value for font size is always the height of *this* element's parent's font.
  538. base_value = parent->ResolveProperty(FONT_SIZE, 0);
  539. }
  540. switch (property->unit)
  541. {
  542. case Property::PERCENT:
  543. return base_value * property->value.Get< float >() * 0.01f;
  544. case Property::EM:
  545. return property->value.Get< float >() * base_value;
  546. case Property::REM:
  547. // If an rem-relative font size is specified, it is expressed relative to the document's
  548. // font height.
  549. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  550. }
  551. }
  552. return ResolveProperty(property, base_value);
  553. }
  554. // Iterates over the properties defined on the element.
  555. bool ElementStyle::IterateProperties(int& index, PseudoClassList& property_pseudo_classes, String& name, const Property*& property)
  556. {
  557. // First check for locally defined properties.
  558. if (local_properties != NULL)
  559. {
  560. if (index < local_properties->GetNumProperties())
  561. {
  562. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  563. for (int count = 0; count < index; ++count)
  564. ++i;
  565. name = (*i).first;
  566. property = &((*i).second);
  567. property_pseudo_classes.clear();
  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, property_pseudo_classes, name, property))
  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. if (!property)
  879. {
  880. ROCKET_ERROR;
  881. return 0.0f;
  882. }
  883. // TODO
  884. float base_value = 1.0f;
  885. switch (property->unit)
  886. {
  887. case Property::NUMBER:
  888. case Property::PX:
  889. case Property::RAD:
  890. return property->value.Get< float >();
  891. case Property::PERCENT:
  892. return base_value * property->value.Get< float >()* 0.01f;
  893. case Property::EM:
  894. return property->value.Get< float >() * font_size;
  895. case Property::REM:
  896. return property->value.Get< float >() * document_font_size;
  897. case Property::DP:
  898. return property->value.Get< float >() * dp_ratio;
  899. case Property::DEG:
  900. return Math::DegreesToRadians(property->value.Get< float >());
  901. }
  902. // Values based on pixels-per-inch.
  903. if (property->unit & Property::PPI_UNIT)
  904. {
  905. float inch = property->value.Get< float >() * pixels_per_inch;
  906. switch (property->unit)
  907. {
  908. case Property::INCH: // inch
  909. return inch;
  910. case Property::CM: // centimeter
  911. return inch * (1.0f / 2.54f);
  912. case Property::MM: // millimeter
  913. return inch * (1.0f / 25.4f);
  914. case Property::PT: // point
  915. return inch * (1.0f / 72.0f);
  916. case Property::PC: // pica
  917. return inch * (1.0f / 6.0f);
  918. }
  919. }
  920. // We're not a numeric property; return 0.
  921. return 0.0f;
  922. }
  923. static float ComputeAbsoluteLength(const Property& property, float dp_ratio, float pixels_per_inch)
  924. {
  925. ROCKET_ASSERT(property.unit & Property::ABSOLUTE_LENGTH);
  926. switch (property.unit)
  927. {
  928. case Property::PX:
  929. return property.value.Get< float >();
  930. case Property::DP:
  931. return property.value.Get< float >()* dp_ratio;
  932. default:
  933. // Values based on pixels-per-inch.
  934. if (property.unit & Property::PPI_UNIT)
  935. {
  936. float inch = property.value.Get< float >() * pixels_per_inch;
  937. switch (property.unit)
  938. {
  939. case Property::INCH: // inch
  940. return inch;
  941. case Property::CM: // centimeter
  942. return inch * (1.0f / 2.54f);
  943. case Property::MM: // millimeter
  944. return inch * (1.0f / 25.4f);
  945. case Property::PT: // point
  946. return inch * (1.0f / 72.0f);
  947. case Property::PC: // pica
  948. return inch * (1.0f / 6.0f);
  949. }
  950. }
  951. }
  952. ROCKET_ERROR;
  953. return 0.0f;
  954. }
  955. // Resolves one of this element's properties.
  956. 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)
  957. {
  958. // The calculated value of the font-size property is inherited, so we need to check if this
  959. // is an inherited property. If so, then we return our parent's font size instead.
  960. if (property.unit & Property::RELATIVE_UNIT)
  961. {
  962. float multiplier = 1.0f;
  963. switch (property.unit)
  964. {
  965. case Property::PERCENT:
  966. multiplier = 0.01f;
  967. [[fallthrough]];
  968. case Property::EM:
  969. if (!parent_values)
  970. return 0;
  971. return property.value.Get< float >() * multiplier * parent_values->font_size;
  972. case Property::REM:
  973. if (!document_values)
  974. return 0;
  975. // If the current element is a document, the rem unit is relative to the default size
  976. if(&values == document_values)
  977. return property.value.Get< float >() * DefaultComputedValues.font_size;
  978. // Otherwise it is relative to the document font size
  979. return property.value.Get< float >() * document_values->font_size;
  980. default:
  981. ROCKET_ERRORMSG("A relative unit must be percentage, em or rem.");
  982. }
  983. }
  984. return ComputeAbsoluteLength(property, dp_ratio, pixels_per_inch);
  985. }
  986. static inline Style::Clip ComputeClip(const Property* property)
  987. {
  988. int value = property->Get<int>();
  989. if (property->unit == Property::KEYWORD)
  990. return (value == CLIP_NONE ? Style::Clip::None : Style::Clip::Auto);
  991. else if (property->unit == Property::NUMBER)
  992. return (Style::Clip)value;
  993. ROCKET_ERRORMSG("Invalid clip type");
  994. return Style::Clip::Auto;
  995. }
  996. // Must be called in correct order, from document root to children elements.
  997. void ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio, float pixels_per_inch)
  998. {
  999. // TODO: Iterate through dirty values, and compute corresponding properties.
  1000. // Important: Always do font-size first if dirty, because of em-relative values.
  1001. if (auto p = GetLocalProperty(FONT_SIZE))
  1002. values.font_size = ComputeFontsize(*p, values, parent_values, document_values, dp_ratio, pixels_per_inch);
  1003. else if (parent_values)
  1004. values.font_size = parent_values->font_size;
  1005. if (auto p = GetLocalProperty(FONT_FAMILY))
  1006. values.font_family = p->Get<String>();
  1007. else if (parent_values)
  1008. values.font_family = parent_values->font_family;
  1009. if (auto p = GetLocalProperty(FONT_CHARSET))
  1010. values.font_charset = p->Get<String>();
  1011. else if (parent_values)
  1012. values.font_charset = parent_values->font_charset;
  1013. if (auto p = GetLocalProperty(FONT_STYLE))
  1014. values.font_style = (Style::FontStyle)p->Get< int >();
  1015. else if (parent_values)
  1016. values.font_style = parent_values->font_style;
  1017. if (auto p = GetLocalProperty(FONT_WEIGHT))
  1018. values.font_weight = (Style::FontWeight)p->Get< int >();
  1019. else if (parent_values)
  1020. values.font_weight = parent_values->font_weight;
  1021. if (auto p = GetLocalProperty(OVERFLOW_X))
  1022. values.overflow_x = (Style::Overflow)p->Get< int >();
  1023. if (auto p = GetLocalProperty(OVERFLOW_Y))
  1024. values.overflow_y = (Style::Overflow)p->Get< int >();
  1025. if (auto p = GetLocalProperty(CLIP))
  1026. values.clip = ComputeClip(p);
  1027. else if (parent_values)
  1028. values.clip = parent_values->clip;
  1029. if (auto p = GetLocalProperty(OPACITY))
  1030. values.opacity = p->Get<float>();
  1031. else if (parent_values)
  1032. values.opacity = parent_values->opacity;
  1033. if (auto p = GetLocalProperty(BACKGROUND_COLOR))
  1034. values.background_color = p->Get<Colourb>();
  1035. if (auto p = GetLocalProperty(IMAGE_COLOR))
  1036. values.image_color = p->Get<Colourb>();
  1037. }
  1038. }
  1039. }