ElementStyle.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. ElementStyle::ElementStyle(Element* _element)
  48. {
  49. local_properties = NULL;
  50. em_properties = NULL;
  51. definition = NULL;
  52. element = _element;
  53. cache = new ElementStyleCache(this);
  54. definition_dirty = true;
  55. child_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. static PropCounter prop_counter;
  68. PropCounter &ElementStyle::GetPropCounter()
  69. {
  70. return prop_counter;
  71. }
  72. // Returns the element's definition, updating if necessary.
  73. const ElementDefinition* ElementStyle::GetDefinition()
  74. {
  75. if (definition_dirty)
  76. {
  77. UpdateDefinition();
  78. }
  79. return definition;
  80. }
  81. // Returns one of this element's properties.
  82. static const Property* GetLocalProperty(const String& name, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  83. {
  84. // Check for overriding local properties.
  85. if (local_properties != NULL)
  86. {
  87. const Property* property = local_properties->GetProperty(name);
  88. if (property != NULL)
  89. return property;
  90. }
  91. // Check for a property defined in an RCSS rule.
  92. if (definition != NULL)
  93. return definition->GetProperty(name, pseudo_classes);
  94. return NULL;
  95. }
  96. // Returns one of this element's properties.
  97. static const Property* GetProperty(const String& name, Element* element, PropertyDictionary* local_properties, ElementDefinition* definition, const PseudoClassList& pseudo_classes)
  98. {
  99. if (prop_counter.find(name) == prop_counter.end())
  100. prop_counter[name] = 0;
  101. prop_counter[name] = prop_counter[name] + 1;
  102. const Property* local_property = GetLocalProperty(name, local_properties, definition, pseudo_classes);
  103. if (local_property != NULL)
  104. return local_property;
  105. // Fetch the property specification.
  106. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  107. if (property == NULL)
  108. return NULL;
  109. // If we can inherit this property, return our parent's property.
  110. if (property->IsInherited())
  111. {
  112. Element* parent = element->GetParentNode();
  113. while (parent != NULL)
  114. {
  115. const Property* parent_property = parent->GetStyle()->GetLocalProperty(name);
  116. if (parent_property)
  117. return parent_property;
  118. parent = parent->GetParentNode();
  119. }
  120. }
  121. // No property available! Return the default value.
  122. return property->GetDefaultValue();
  123. }
  124. // For the properties which are defined in a transition on the given element, apply transition to all properties changed due to a class change.
  125. // Call this before new_definition has been assigned to the element and its style.
  126. // Properties that are part of a transition are removed from the properties list.
  127. static void TransitionUpdatedClass(ElementStyle* style, Element* element, PropertyNameList& properties, ElementDefinition* old_definition, ElementDefinition* new_definition, PropertyDictionary* local_properties, const PseudoClassList& pseudo_classes)
  128. {
  129. if (!element || !old_definition || !new_definition || properties.empty())
  130. return;
  131. if (const Property* transition_property = GetLocalProperty(TRANSITION, local_properties, new_definition, pseudo_classes))
  132. {
  133. auto transition_list = transition_property->Get<TransitionList>();
  134. if (!transition_list.none && new_definition)
  135. {
  136. for (auto it = properties.begin(); it != properties.end();)
  137. {
  138. auto& property = *it;
  139. Transition* transition = nullptr;
  140. bool transition_added = false;
  141. if (transition_list.all)
  142. {
  143. transition = &transition_list.transitions[0];
  144. }
  145. else
  146. {
  147. // TODO: Swap the loops because a lookup in properties is constant time
  148. for (auto& transition_candidate : transition_list.transitions) {
  149. if (transition_candidate.name == property) {
  150. transition = &transition_candidate;
  151. break;
  152. }
  153. }
  154. }
  155. if (transition)
  156. {
  157. const Property* start_value = GetProperty(property, element, local_properties, old_definition, pseudo_classes);
  158. const Property* target_value = GetProperty(property, element, local_properties, new_definition, pseudo_classes);
  159. // TODO: See if the start and target_value are equal, if so, skip.
  160. if (start_value && target_value)
  161. transition_added = element->StartTransition(*transition, *start_value, *target_value);
  162. }
  163. if (transition_added)
  164. it = properties.erase(it);
  165. else
  166. ++it;
  167. }
  168. }
  169. }
  170. }
  171. void ElementStyle::UpdateDefinition()
  172. {
  173. if (definition_dirty)
  174. {
  175. definition_dirty = false;
  176. ElementDefinition* new_definition = NULL;
  177. const StyleSheet* style_sheet = GetStyleSheet();
  178. if (style_sheet != NULL)
  179. {
  180. new_definition = style_sheet->GetElementDefinition(element);
  181. }
  182. // Switch the property definitions if the definition has changed.
  183. if (new_definition != definition || new_definition == NULL)
  184. {
  185. PropertyNameList properties;
  186. if (definition != NULL)
  187. definition->GetDefinedProperties(properties, pseudo_classes);
  188. if (new_definition != NULL)
  189. new_definition->GetDefinedProperties(properties, pseudo_classes);
  190. TransitionUpdatedClass(this, element, properties, definition, new_definition, local_properties, pseudo_classes);
  191. if (definition != NULL)
  192. definition->RemoveReference();
  193. definition = new_definition;
  194. DirtyProperties(properties);
  195. element->GetElementDecoration()->ReloadDecorators();
  196. }
  197. else if (new_definition != NULL)
  198. {
  199. new_definition->RemoveReference();
  200. }
  201. }
  202. if (child_definition_dirty)
  203. {
  204. for (int i = 0; i < element->GetNumChildren(true); i++)
  205. {
  206. element->GetChild(i)->GetStyle()->UpdateDefinition();
  207. }
  208. child_definition_dirty = false;
  209. }
  210. }
  211. // Sets or removes a pseudo-class on the element.
  212. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  213. {
  214. size_t num_pseudo_classes = pseudo_classes.size();
  215. auto pseudo_classes_before = pseudo_classes;
  216. if (activate)
  217. pseudo_classes.insert(pseudo_class);
  218. else
  219. pseudo_classes.erase(pseudo_class);
  220. // Apply transition if defined on this element
  221. // Note: We had to set the pseudo classes first so that any transition being overriden in the pseudo class is captured
  222. if (const Property* property = GetLocalProperty(TRANSITION))
  223. {
  224. ROCKET_ASSERT(property->unit == Property::TRANSITION);
  225. auto transition_list = property->Get<TransitionList>();
  226. const ElementDefinition* definition = element->GetDefinition();
  227. if (!transition_list.none && definition != NULL)
  228. {
  229. PropertyNameList properties;
  230. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  231. for (auto& property : properties)
  232. {
  233. Transition* transition = nullptr;
  234. if (transition_list.all)
  235. {
  236. transition = &transition_list.transitions[0];
  237. }
  238. else
  239. {
  240. for (auto& transition_candidate : transition_list.transitions) {
  241. if (transition_candidate.name == property) {
  242. transition = &transition_candidate;
  243. break;
  244. }
  245. }
  246. }
  247. if (transition)
  248. {
  249. // Get start value from local properties first, else, we fetch the property given the previous pseudo classes
  250. const Property* start_value = nullptr;
  251. if (local_properties)
  252. start_value = local_properties->GetProperty(property);
  253. if(!start_value)
  254. start_value = definition->GetProperty(property, pseudo_classes_before);
  255. auto target_value = definition->GetProperty(property, pseudo_classes);
  256. if (start_value && target_value)
  257. element->StartTransition(*transition, *start_value, *target_value);
  258. }
  259. }
  260. }
  261. }
  262. if (pseudo_classes.size() != num_pseudo_classes)
  263. {
  264. element->GetElementDecoration()->DirtyDecorators();
  265. const ElementDefinition* definition = element->GetDefinition();
  266. if (definition != NULL)
  267. {
  268. PropertyNameList properties;
  269. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  270. DirtyProperties(properties);
  271. switch (definition->GetPseudoClassVolatility(pseudo_class))
  272. {
  273. case ElementDefinition::FONT_VOLATILE:
  274. element->DirtyFont();
  275. break;
  276. case ElementDefinition::STRUCTURE_VOLATILE:
  277. DirtyChildDefinitions();
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. }
  284. }
  285. // Checks if a specific pseudo-class has been set on the element.
  286. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  287. {
  288. return (pseudo_classes.find(pseudo_class) != pseudo_classes.end());
  289. }
  290. const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
  291. {
  292. return pseudo_classes;
  293. }
  294. // Sets or removes a class on the element.
  295. void ElementStyle::SetClass(const String& class_name, bool activate)
  296. {
  297. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  298. if (activate)
  299. {
  300. if (class_location == classes.end())
  301. {
  302. classes.push_back(class_name);
  303. DirtyDefinition();
  304. }
  305. }
  306. else
  307. {
  308. if (class_location != classes.end())
  309. {
  310. classes.erase(class_location);
  311. DirtyDefinition();
  312. }
  313. }
  314. }
  315. // Checks if a class is set on the element.
  316. bool ElementStyle::IsClassSet(const String& class_name) const
  317. {
  318. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  319. }
  320. // Specifies the entire list of classes for this element. This will replace any others specified.
  321. void ElementStyle::SetClassNames(const String& class_names)
  322. {
  323. classes.clear();
  324. StringUtilities::ExpandString(classes, class_names, ' ');
  325. DirtyDefinition();
  326. }
  327. // Returns the list of classes specified for this element.
  328. String ElementStyle::GetClassNames() const
  329. {
  330. String class_names;
  331. for (size_t i = 0; i < classes.size(); i++)
  332. {
  333. if (i != 0)
  334. {
  335. class_names.Append(" ");
  336. }
  337. class_names.Append(classes[i]);
  338. }
  339. return class_names;
  340. }
  341. // Sets a local property override on the element.
  342. bool ElementStyle::SetProperty(const String& name, const String& value)
  343. {
  344. if (local_properties == NULL)
  345. local_properties = new PropertyDictionary();
  346. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  347. {
  348. DirtyProperty(name);
  349. return true;
  350. }
  351. else
  352. {
  353. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.CString(), value.CString());
  354. return false;
  355. }
  356. }
  357. // Sets a local property override on the element to a pre-parsed value.
  358. bool ElementStyle::SetProperty(const String& name, const Property& property)
  359. {
  360. Property new_property = property;
  361. new_property.definition = StyleSheetSpecification::GetProperty(name);
  362. if (new_property.definition == NULL)
  363. return false;
  364. if (local_properties == NULL)
  365. local_properties = new PropertyDictionary();
  366. local_properties->SetProperty(name, new_property);
  367. DirtyProperty(name);
  368. return true;
  369. }
  370. // Removes a local property override on the element.
  371. void ElementStyle::RemoveProperty(const String& name)
  372. {
  373. if (local_properties == NULL)
  374. return;
  375. if (local_properties->GetProperty(name) != NULL)
  376. {
  377. local_properties->RemoveProperty(name);
  378. DirtyProperty(name);
  379. }
  380. }
  381. // Returns one of this element's properties.
  382. const Property* ElementStyle::GetProperty(const String& name)
  383. {
  384. if (prop_counter.find(name) == prop_counter.end())
  385. prop_counter[name] = 0;
  386. prop_counter[name] = prop_counter[name] + 1;
  387. const Property* local_property = GetLocalProperty(name);
  388. if (local_property != NULL)
  389. return local_property;
  390. // Fetch the property specification.
  391. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  392. if (property == NULL)
  393. return NULL;
  394. // If we can inherit this property, return our parent's property.
  395. if (property->IsInherited())
  396. {
  397. Element* parent = element->GetParentNode();
  398. while (parent != NULL)
  399. {
  400. const Property* parent_property = parent->style->GetLocalProperty(name);
  401. if (parent_property)
  402. return parent_property;
  403. parent = parent->GetParentNode();
  404. }
  405. }
  406. // No property available! Return the default value.
  407. return property->GetDefaultValue();
  408. }
  409. // Returns one of this element's properties.
  410. const Property* ElementStyle::GetLocalProperty(const String& name)
  411. {
  412. // Check for overriding local properties.
  413. if (local_properties != NULL)
  414. {
  415. const Property* property = local_properties->GetProperty(name);
  416. if (property != NULL)
  417. return property;
  418. }
  419. // Check for a property defined in an RCSS rule.
  420. if (definition != NULL)
  421. return definition->GetProperty(name, pseudo_classes);
  422. return NULL;
  423. }
  424. float ElementStyle::ResolveLength(const Property * property)
  425. {
  426. if (!property)
  427. {
  428. ROCKET_ERROR;
  429. return 0.0f;
  430. }
  431. if (!(property->unit & Property::LENGTH))
  432. {
  433. ROCKET_ERRORMSG("Trying to resolve length on a non-length property.");
  434. return 0.0f;
  435. }
  436. switch (property->unit)
  437. {
  438. case Property::NUMBER:
  439. case Property::PX:
  440. return property->value.Get< float >();
  441. case Property::EM:
  442. return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
  443. case Property::REM:
  444. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  445. case Property::DP:
  446. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  447. }
  448. // Values based on pixels-per-inch.
  449. if (property->unit & Property::PPI_UNIT)
  450. {
  451. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  452. switch (property->unit)
  453. {
  454. case Property::INCH: // inch
  455. return inch;
  456. case Property::CM: // centimeter
  457. return inch * (1.0f / 2.54f);
  458. case Property::MM: // millimeter
  459. return inch * (1.0f / 25.4f);
  460. case Property::PT: // point
  461. return inch * (1.0f / 72.0f);
  462. case Property::PC: // pica
  463. return inch * (1.0f / 6.0f);
  464. }
  465. }
  466. // We're not a numeric property; return 0.
  467. return 0.0f;
  468. }
  469. float ElementStyle::ResolveAngle(const Property * property)
  470. {
  471. switch (property->unit)
  472. {
  473. case Property::NUMBER:
  474. case Property::DEG:
  475. return Math::DegreesToRadians(property->value.Get< float >());
  476. case Property::RAD:
  477. return property->value.Get< float >();
  478. case Property::PERCENT:
  479. return property->value.Get< float >() * 0.01f * 2.0f * Math::ROCKET_PI;
  480. }
  481. ROCKET_ERRORMSG("Trying to resolve angle on a non-angle property.");
  482. return 0.0f;
  483. }
  484. float ElementStyle::ResolveNumericProperty(const String& property_name, const Property * property)
  485. {
  486. if (property->unit & Property::LENGTH)
  487. {
  488. return ResolveLength(property);
  489. }
  490. auto definition = property->definition;
  491. if (!definition) definition = StyleSheetSpecification::GetProperty(property_name);
  492. if (!definition) return 0.0f;
  493. auto relative_target = definition->GetRelativeTarget();
  494. return ResolveNumericProperty(property, relative_target);
  495. }
  496. float ElementStyle::ResolveNumericProperty(const Property * property, RelativeTarget relative_target)
  497. {
  498. if (property->unit & Property::LENGTH)
  499. {
  500. return ResolveLength(property);
  501. }
  502. float base_value = 0.0f;
  503. switch (relative_target)
  504. {
  505. case RelativeTarget::None:
  506. base_value = 1.0f;
  507. break;
  508. case RelativeTarget::ContainingBlockWidth:
  509. base_value = element->GetContainingBlock().x;
  510. break;
  511. case RelativeTarget::ContainingBlockHeight:
  512. base_value = element->GetContainingBlock().y;
  513. break;
  514. case RelativeTarget::FontSize:
  515. base_value = (float)ElementUtilities::GetFontSize(element);
  516. break;
  517. case RelativeTarget::ParentFontSize:
  518. base_value = (float)ElementUtilities::GetFontSize(element->GetParentNode());
  519. break;
  520. case RelativeTarget::LineHeight:
  521. base_value = (float)ElementUtilities::GetLineHeight(element);
  522. break;
  523. default:
  524. break;
  525. }
  526. float scale_value = 0.0f;
  527. switch (property->unit)
  528. {
  529. case Property::NUMBER:
  530. scale_value = property->value.Get< float >();
  531. case Property::PERCENT:
  532. scale_value = property->value.Get< float >() * 0.01f;
  533. }
  534. return base_value * scale_value;
  535. }
  536. // Resolves one of this element's properties.
  537. float ElementStyle::ResolveProperty(const Property* property, float base_value)
  538. {
  539. if (!property)
  540. {
  541. ROCKET_ERROR;
  542. return 0.0f;
  543. }
  544. switch (property->unit)
  545. {
  546. case Property::NUMBER:
  547. case Property::PX:
  548. case Property::RAD:
  549. return property->value.Get< float >();
  550. case Property::PERCENT:
  551. return base_value * property->value.Get< float >() * 0.01f;
  552. case Property::EM:
  553. return property->value.Get< float >() * (float)ElementUtilities::GetFontSize(element);
  554. case Property::REM:
  555. return property->value.Get< float >() * (float)ElementUtilities::GetFontSize(element->GetOwnerDocument());
  556. case Property::DP:
  557. return property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element);
  558. case Property::DEG:
  559. return Math::DegreesToRadians(property->value.Get< float >());
  560. }
  561. // Values based on pixels-per-inch.
  562. if (property->unit & Property::PPI_UNIT)
  563. {
  564. float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();
  565. switch (property->unit)
  566. {
  567. case Property::INCH: // inch
  568. return inch;
  569. case Property::CM: // centimeter
  570. return inch * (1.0f / 2.54f);
  571. case Property::MM: // millimeter
  572. return inch * (1.0f / 25.4f);
  573. case Property::PT: // point
  574. return inch * (1.0f / 72.0f);
  575. case Property::PC: // pica
  576. return inch * (1.0f / 6.0f);
  577. }
  578. }
  579. // We're not a numeric property; return 0.
  580. return 0.0f;
  581. }
  582. // Resolves one of this element's properties.
  583. float ElementStyle::ResolveProperty(const String& name, float base_value)
  584. {
  585. const Property* property = GetProperty(name);
  586. if (!property)
  587. {
  588. ROCKET_ERROR;
  589. return 0.0f;
  590. }
  591. // The calculated value of the font-size property is inherited, so we need to check if this
  592. // is an inherited property. If so, then we return our parent's font size instead.
  593. if (name == FONT_SIZE && property->unit & Property::RELATIVE_UNIT)
  594. {
  595. // If the rem unit is used, the font-size is inherited directly from the document,
  596. // otherwise we use the parent's font size.
  597. if (property->unit & Property::REM)
  598. {
  599. Rocket::Core::ElementDocument* owner_document = element->GetOwnerDocument();
  600. if (owner_document == NULL)
  601. return 0;
  602. base_value = element->GetOwnerDocument()->ResolveProperty(FONT_SIZE, 0);
  603. }
  604. else
  605. {
  606. Rocket::Core::Element* parent = element->GetParentNode();
  607. if (parent == NULL)
  608. return 0;
  609. if (GetLocalProperty(FONT_SIZE) == NULL)
  610. return parent->ResolveProperty(FONT_SIZE, 0);
  611. // The base value for font size is always the height of *this* element's parent's font.
  612. base_value = parent->ResolveProperty(FONT_SIZE, 0);
  613. }
  614. switch (property->unit)
  615. {
  616. case Property::PERCENT:
  617. return base_value * property->value.Get< float >() * 0.01f;
  618. case Property::EM:
  619. return property->value.Get< float >() * base_value;
  620. case Property::REM:
  621. // If an rem-relative font size is specified, it is expressed relative to the document's
  622. // font height.
  623. return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
  624. }
  625. }
  626. return ResolveProperty(property, base_value);
  627. }
  628. // Iterates over the properties defined on the element.
  629. bool ElementStyle::IterateProperties(int& index, PseudoClassList& property_pseudo_classes, String& name, const Property*& property)
  630. {
  631. // First check for locally defined properties.
  632. if (local_properties != NULL)
  633. {
  634. if (index < local_properties->GetNumProperties())
  635. {
  636. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  637. for (int count = 0; count < index; ++count)
  638. ++i;
  639. name = (*i).first;
  640. property = &((*i).second);
  641. property_pseudo_classes.clear();
  642. ++index;
  643. return true;
  644. }
  645. }
  646. const ElementDefinition* definition = GetDefinition();
  647. if (definition != NULL)
  648. {
  649. int index_offset = 0;
  650. if (local_properties != NULL)
  651. index_offset = local_properties->GetNumProperties();
  652. // Offset the index to be relative to the definition before we start indexing. When we do get a property back,
  653. // check that it hasn't been overridden by the element's local properties; if so, continue on to the next one.
  654. index -= index_offset;
  655. while (definition->IterateProperties(index, pseudo_classes, property_pseudo_classes, name, property))
  656. {
  657. if (local_properties == NULL ||
  658. local_properties->GetProperty(name) == NULL)
  659. {
  660. index += index_offset;
  661. return true;
  662. }
  663. }
  664. return false;
  665. }
  666. return false;
  667. }
  668. // Returns the active style sheet for this element. This may be NULL.
  669. StyleSheet* ElementStyle::GetStyleSheet() const
  670. {
  671. ElementDocument* document = element->GetOwnerDocument();
  672. if (document != NULL)
  673. return document->GetStyleSheet();
  674. return NULL;
  675. }
  676. void ElementStyle::DirtyDefinition()
  677. {
  678. definition_dirty = true;
  679. DirtyChildDefinitions();
  680. // Dirty the child definition update the element tree
  681. Element* parent = element->GetParentNode();
  682. while (parent)
  683. {
  684. parent->GetStyle()->child_definition_dirty = true;
  685. parent = parent->GetParentNode();
  686. }
  687. }
  688. void ElementStyle::DirtyChildDefinitions()
  689. {
  690. for (int i = 0; i < element->GetNumChildren(true); i++)
  691. element->GetChild(i)->GetStyle()->DirtyDefinition();
  692. }
  693. // Dirties every property.
  694. void ElementStyle::DirtyProperties()
  695. {
  696. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  697. DirtyProperties(properties);
  698. }
  699. // Dirties em-relative properties.
  700. void ElementStyle::DirtyEmProperties()
  701. {
  702. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  703. if (!em_properties)
  704. {
  705. // Check if any of these are currently em-relative. If so, dirty them.
  706. em_properties = new PropertyNameList;
  707. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  708. {
  709. // Skip font-size; this is relative to our parent's em, not ours.
  710. if (*list_iterator == FONT_SIZE)
  711. continue;
  712. // Get this property from this element. If this is em-relative, then add it to the list to
  713. // dirty.
  714. if (element->GetProperty(*list_iterator)->unit == Property::EM)
  715. em_properties->insert(*list_iterator);
  716. }
  717. }
  718. if (!em_properties->empty())
  719. DirtyProperties(*em_properties, false);
  720. // Now dirty all of our descendant's font-size properties that are relative to ems.
  721. int num_children = element->GetNumChildren(true);
  722. for (int i = 0; i < num_children; ++i)
  723. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  724. }
  725. // Dirties font-size on child elements if appropriate.
  726. void ElementStyle::DirtyInheritedEmProperties()
  727. {
  728. const Property* font_size = element->GetLocalProperty(FONT_SIZE);
  729. if (font_size == NULL)
  730. {
  731. int num_children = element->GetNumChildren(true);
  732. for (int i = 0; i < num_children; ++i)
  733. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  734. }
  735. else
  736. {
  737. if (font_size->unit & Property::RELATIVE_UNIT)
  738. DirtyProperty(FONT_SIZE);
  739. }
  740. }
  741. // Dirties rem properties.
  742. void ElementStyle::DirtyRemProperties()
  743. {
  744. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  745. PropertyNameList rem_properties;
  746. // Dirty all the properties of this element that use the rem unit.
  747. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  748. {
  749. if (element->GetProperty(*list_iterator)->unit == Property::REM)
  750. rem_properties.insert(*list_iterator);
  751. }
  752. if (!rem_properties.empty())
  753. DirtyProperties(rem_properties, false);
  754. // Now dirty all of our descendant's properties that use the rem unit.
  755. int num_children = element->GetNumChildren(true);
  756. for (int i = 0; i < num_children; ++i)
  757. element->GetChild(i)->GetStyle()->DirtyRemProperties();
  758. }
  759. void ElementStyle::DirtyDpProperties()
  760. {
  761. const PropertyNameList &properties = StyleSheetSpecification::GetRegisteredProperties();
  762. PropertyNameList dp_properties;
  763. // Dirty all the properties of this element that use the dp unit.
  764. for (PropertyNameList::const_iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  765. {
  766. if (element->GetProperty(*list_iterator)->unit == Property::DP)
  767. dp_properties.insert(*list_iterator);
  768. }
  769. if (!dp_properties.empty())
  770. DirtyProperties(dp_properties, false);
  771. // Now dirty all of our descendant's properties that use the dp unit.
  772. int num_children = element->GetNumChildren(true);
  773. for (int i = 0; i < num_children; ++i)
  774. element->GetChild(i)->GetStyle()->DirtyDpProperties();
  775. }
  776. // Sets a single property as dirty.
  777. void ElementStyle::DirtyProperty(const String& property)
  778. {
  779. PropertyNameList properties;
  780. properties.insert(String(property));
  781. DirtyProperties(properties);
  782. }
  783. // Sets a list of properties as dirty.
  784. void ElementStyle::DirtyProperties(const PropertyNameList& properties, bool clear_em_properties)
  785. {
  786. if (properties.empty())
  787. return;
  788. bool all_inherited_dirty =
  789. StyleSheetSpecification::GetRegisteredProperties() == properties ||
  790. StyleSheetSpecification::GetRegisteredInheritedProperties() == properties;
  791. if (all_inherited_dirty)
  792. {
  793. const PropertyNameList &all_inherited_properties = StyleSheetSpecification::GetRegisteredInheritedProperties();
  794. for (int i = 0; i < element->GetNumChildren(true); i++)
  795. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(all_inherited_properties);
  796. }
  797. else
  798. {
  799. PropertyNameList inherited_properties;
  800. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  801. {
  802. // If this property is an inherited property, then push it into the list to be passed onto our children.
  803. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(*i);
  804. if (property != NULL &&
  805. property->IsInherited())
  806. inherited_properties.insert(*i);
  807. }
  808. // Pass the list of those properties that are inherited onto our children.
  809. if (!inherited_properties.empty())
  810. {
  811. for (int i = 0; i < element->GetNumChildren(true); i++)
  812. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  813. }
  814. }
  815. // Clear all cached properties.
  816. cache->Clear();
  817. cache->ClearInherited();
  818. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  819. if (clear_em_properties && em_properties != NULL)
  820. {
  821. delete em_properties;
  822. em_properties = NULL;
  823. }
  824. // And send the event.
  825. element->OnPropertyChange(properties);
  826. }
  827. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  828. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  829. {
  830. bool clear_em_properties = em_properties != NULL;
  831. PropertyNameList inherited_properties;
  832. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  833. {
  834. const Property *property = GetLocalProperty((*i));
  835. if (property == NULL)
  836. {
  837. inherited_properties.insert(*i);
  838. if (!clear_em_properties && em_properties != NULL && em_properties->find((*i)) != em_properties->end()) {
  839. clear_em_properties = true;
  840. }
  841. }
  842. }
  843. if (inherited_properties.empty())
  844. return;
  845. // clear the list of EM-properties, we will refill it in DirtyEmProperties
  846. if (clear_em_properties && em_properties != NULL)
  847. {
  848. delete em_properties;
  849. em_properties = NULL;
  850. }
  851. // Clear cached inherited properties.
  852. cache->ClearInherited();
  853. // Pass the list of those properties that this element doesn't override onto our children.
  854. for (int i = 0; i < element->GetNumChildren(true); i++)
  855. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  856. element->OnPropertyChange(properties);
  857. }
  858. void ElementStyle::GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right )
  859. {
  860. cache->GetOffsetProperties(top, bottom, left, right);
  861. }
  862. void ElementStyle::GetBorderWidthProperties(const Property **border_top_width, const Property **border_bottom_width, const Property **border_left_width, const Property **bottom_right_width)
  863. {
  864. cache->GetBorderWidthProperties(border_top_width, border_bottom_width, border_left_width, bottom_right_width);
  865. }
  866. void ElementStyle::GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right)
  867. {
  868. cache->GetMarginProperties(margin_top, margin_bottom, margin_left, margin_right);
  869. }
  870. void ElementStyle::GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right)
  871. {
  872. cache->GetPaddingProperties(padding_top, padding_bottom, padding_left, padding_right);
  873. }
  874. void ElementStyle::GetDimensionProperties(const Property **width, const Property **height)
  875. {
  876. cache->GetDimensionProperties(width, height);
  877. }
  878. void ElementStyle::GetLocalDimensionProperties(const Property **width, const Property **height)
  879. {
  880. cache->GetLocalDimensionProperties(width, height);
  881. }
  882. void ElementStyle::GetOverflow(int *overflow_x, int *overflow_y)
  883. {
  884. cache->GetOverflow(overflow_x, overflow_y);
  885. }
  886. int ElementStyle::GetPosition()
  887. {
  888. return cache->GetPosition();
  889. }
  890. int ElementStyle::GetFloat()
  891. {
  892. return cache->GetFloat();
  893. }
  894. int ElementStyle::GetDisplay()
  895. {
  896. return cache->GetDisplay();
  897. }
  898. int ElementStyle::GetWhitespace()
  899. {
  900. return cache->GetWhitespace();
  901. }
  902. int ElementStyle::GetPointerEvents()
  903. {
  904. return cache->GetPointerEvents();
  905. }
  906. const Property *ElementStyle::GetLineHeightProperty()
  907. {
  908. return cache->GetLineHeightProperty();
  909. }
  910. int ElementStyle::GetTextAlign()
  911. {
  912. return cache->GetTextAlign();
  913. }
  914. int ElementStyle::GetTextTransform()
  915. {
  916. return cache->GetTextTransform();
  917. }
  918. const Property *ElementStyle::GetVerticalAlignProperty()
  919. {
  920. return cache->GetVerticalAlignProperty();
  921. }
  922. // Returns 'perspective' property value from element's style or local cache.
  923. const Property *ElementStyle::GetPerspective()
  924. {
  925. return element->GetProperty(PERSPECTIVE);
  926. }
  927. // Returns 'perspective-origin-x' property value from element's style or local cache.
  928. const Property *ElementStyle::GetPerspectiveOriginX()
  929. {
  930. return element->GetProperty(PERSPECTIVE_ORIGIN_X);
  931. }
  932. // Returns 'perspective-origin-y' property value from element's style or local cache.
  933. const Property *ElementStyle::GetPerspectiveOriginY()
  934. {
  935. return element->GetProperty(PERSPECTIVE_ORIGIN_Y);
  936. }
  937. // Returns 'transform' property value from element's style or local cache.
  938. const Property *ElementStyle::GetTransform()
  939. {
  940. return element->GetProperty(TRANSFORM);
  941. }
  942. // Returns 'transform-origin-x' property value from element's style or local cache.
  943. const Property *ElementStyle::GetTransformOriginX()
  944. {
  945. return element->GetProperty(TRANSFORM_ORIGIN_X);
  946. }
  947. // Returns 'transform-origin-y' property value from element's style or local cache.
  948. const Property *ElementStyle::GetTransformOriginY()
  949. {
  950. return element->GetProperty(TRANSFORM_ORIGIN_Y);
  951. }
  952. // Returns 'transform-origin-z' property value from element's style or local cache.
  953. const Property *ElementStyle::GetTransformOriginZ()
  954. {
  955. return element->GetProperty(TRANSFORM_ORIGIN_Z);
  956. }
  957. }
  958. }