ElementStyle.cpp 27 KB

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