ElementStyleCache.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. namespace Rocket {
  31. namespace Core {
  32. ElementStyleCache::ElementStyleCache(ElementStyle *style) : style(style),
  33. border_top_width(NULL), border_bottom_width(NULL), border_left_width(NULL), border_right_width(NULL),
  34. margin_top(NULL), margin_bottom(NULL), margin_left(NULL), margin_right(NULL),
  35. padding_top(NULL), padding_bottom(NULL), padding_left(NULL), padding_right(NULL),
  36. width(NULL), height(NULL),
  37. local_width(NULL), local_height(NULL), have_local_width(false), have_local_height(false),
  38. overflow_x(NULL), overflow_y(NULL),
  39. position(-1), float_(-1), display(-1), whitespace(-1),
  40. line_height(NULL), text_align(-1), text_transform(-1), vertical_align(NULL)
  41. {
  42. }
  43. void ElementStyleCache::Clear()
  44. {
  45. ClearBorder();
  46. ClearMargin();
  47. ClearPadding();
  48. ClearDimensions();
  49. ClearOverflow();
  50. ClearPosition();
  51. ClearFloat();
  52. ClearDisplay();
  53. ClearWhitespace();
  54. }
  55. void ElementStyleCache::ClearInherited()
  56. {
  57. ClearLineHeight();
  58. ClearTextAlign();
  59. ClearTextTransform();
  60. ClearVerticalAlign();
  61. }
  62. void ElementStyleCache::ClearBorder()
  63. {
  64. border_top_width = border_bottom_width = border_left_width = border_right_width = NULL;
  65. }
  66. void ElementStyleCache::ClearMargin()
  67. {
  68. margin_top = margin_bottom = margin_left = margin_right = NULL;
  69. }
  70. void ElementStyleCache::ClearPadding()
  71. {
  72. padding_top = padding_bottom = padding_left = padding_right = NULL;
  73. }
  74. void ElementStyleCache::ClearDimensions()
  75. {
  76. width = height = NULL;
  77. have_local_width = have_local_height = false;
  78. }
  79. void ElementStyleCache::ClearOverflow()
  80. {
  81. overflow_x = overflow_y = -1;
  82. }
  83. void ElementStyleCache::ClearPosition()
  84. {
  85. position = -1;
  86. }
  87. void ElementStyleCache::ClearFloat()
  88. {
  89. float_ = -1;
  90. }
  91. void ElementStyleCache::ClearDisplay()
  92. {
  93. display = -1;
  94. }
  95. void ElementStyleCache::ClearWhitespace()
  96. {
  97. whitespace = -1;
  98. }
  99. void ElementStyleCache::ClearLineHeight()
  100. {
  101. line_height = NULL;
  102. }
  103. void ElementStyleCache::ClearTextAlign()
  104. {
  105. text_align = -1;
  106. }
  107. void ElementStyleCache::ClearTextTransform()
  108. {
  109. text_transform = -1;
  110. }
  111. void ElementStyleCache::ClearVerticalAlign()
  112. {
  113. vertical_align = NULL;
  114. }
  115. void ElementStyleCache::GetBorderWidthProperties(const Property **o_border_top_width, const Property **o_border_bottom_width, const Property **o_border_left_width, const Property **o_border_right_width)
  116. {
  117. if (o_border_top_width)
  118. {
  119. if (!border_top_width)
  120. border_top_width = style->GetProperty(BORDER_TOP_WIDTH);
  121. *o_border_top_width = border_top_width;
  122. }
  123. if (o_border_bottom_width)
  124. {
  125. if (!border_bottom_width)
  126. border_bottom_width = style->GetProperty(BORDER_BOTTOM_WIDTH);
  127. *o_border_bottom_width = border_bottom_width;
  128. }
  129. if (o_border_left_width)
  130. {
  131. if (!border_left_width)
  132. border_left_width = style->GetProperty(BORDER_LEFT_WIDTH);
  133. *o_border_left_width = border_left_width;
  134. }
  135. if (o_border_right_width)
  136. {
  137. if (!border_right_width)
  138. border_right_width = style->GetProperty(BORDER_RIGHT_WIDTH);
  139. *o_border_right_width = border_right_width;
  140. }
  141. }
  142. void ElementStyleCache::GetMarginProperties(const Property **o_margin_top, const Property **o_margin_bottom, const Property **o_margin_left, const Property **o_margin_right)
  143. {
  144. if (o_margin_top)
  145. {
  146. if (!margin_top)
  147. margin_top = style->GetProperty(MARGIN_TOP);
  148. *o_margin_top = margin_top;
  149. }
  150. if (o_margin_bottom)
  151. {
  152. if (!margin_bottom)
  153. margin_bottom = style->GetProperty(MARGIN_BOTTOM);
  154. *o_margin_bottom = margin_bottom;
  155. }
  156. if (o_margin_left)
  157. {
  158. if (!margin_left)
  159. margin_left = style->GetProperty(MARGIN_LEFT);
  160. *o_margin_left = margin_left;
  161. }
  162. if (o_margin_right)
  163. {
  164. if (!margin_right)
  165. margin_right = style->GetProperty(MARGIN_RIGHT);
  166. *o_margin_right = margin_right;
  167. }
  168. }
  169. void ElementStyleCache::GetPaddingProperties(const Property **o_padding_top, const Property **o_padding_bottom, const Property **o_padding_left, const Property **o_padding_right)
  170. {
  171. if (o_padding_top)
  172. {
  173. if (!padding_top)
  174. padding_top = style->GetProperty(PADDING_TOP);
  175. *o_padding_top = padding_top;
  176. }
  177. if (o_padding_bottom)
  178. {
  179. if (!padding_bottom)
  180. padding_bottom = style->GetProperty(PADDING_BOTTOM);
  181. *o_padding_bottom = padding_bottom;
  182. }
  183. if (o_padding_left)
  184. {
  185. if (!padding_left)
  186. padding_left = style->GetProperty(PADDING_LEFT);
  187. *o_padding_left = padding_left;
  188. }
  189. if (o_padding_right)
  190. {
  191. if (!padding_right)
  192. padding_right = style->GetProperty(PADDING_RIGHT);
  193. *o_padding_right = padding_right;
  194. }
  195. }
  196. void ElementStyleCache::GetDimensionProperties(const Property **o_width, const Property **o_height)
  197. {
  198. if (o_width)
  199. {
  200. if (!width)
  201. width = style->GetProperty(WIDTH);
  202. *o_width = width;
  203. }
  204. if (o_height)
  205. {
  206. if (!height)
  207. height = style->GetProperty(HEIGHT);
  208. *o_height = height;
  209. }
  210. }
  211. void ElementStyleCache::GetLocalDimensionProperties(const Property **o_width, const Property **o_height)
  212. {
  213. if (o_width)
  214. {
  215. if (!have_local_width)
  216. {
  217. have_local_width = true;
  218. local_width = style->GetLocalProperty(WIDTH);
  219. }
  220. *o_width = local_width;
  221. }
  222. if (o_height)
  223. {
  224. if (!have_local_height)
  225. {
  226. have_local_height = true;
  227. local_height = style->GetLocalProperty(HEIGHT);
  228. }
  229. *o_height = local_height;
  230. }
  231. }
  232. void ElementStyleCache::GetOverflow(int *o_overflow_x, int *o_overflow_y)
  233. {
  234. if (o_overflow_x)
  235. {
  236. if (overflow_x < 0)
  237. overflow_x = style->GetProperty(OVERFLOW_X)->Get< int >();
  238. *o_overflow_x = overflow_x;
  239. }
  240. if (o_overflow_y)
  241. {
  242. if (overflow_y < 0)
  243. overflow_y = style->GetProperty(OVERFLOW_Y)->Get< int >();
  244. *o_overflow_y = overflow_y;
  245. }
  246. }
  247. int ElementStyleCache::GetPosition()
  248. {
  249. if (position < 0)
  250. position = style->GetProperty(POSITION)->Get< int >();
  251. return position;
  252. }
  253. int ElementStyleCache::GetFloat()
  254. {
  255. if (float_ < 0)
  256. float_ = style->GetProperty(FLOAT)->Get< int >();
  257. return float_;
  258. }
  259. int ElementStyleCache::GetDisplay()
  260. {
  261. if (display < 0)
  262. display = style->GetProperty(DISPLAY)->Get< int >();
  263. return display;
  264. }
  265. int ElementStyleCache::GetWhitespace()
  266. {
  267. if (whitespace < 0)
  268. whitespace = style->GetProperty(WHITE_SPACE)->Get< int >();
  269. return whitespace;
  270. }
  271. const Property *ElementStyleCache::GetLineHeightProperty()
  272. {
  273. if (!line_height)
  274. line_height = style->GetProperty(LINE_HEIGHT);
  275. return line_height;
  276. }
  277. int ElementStyleCache::GetTextAlign()
  278. {
  279. if (text_align < 0)
  280. text_align = style->GetProperty(TEXT_ALIGN)->Get< int >();
  281. return text_align;
  282. }
  283. int ElementStyleCache::GetTextTransform()
  284. {
  285. if (text_transform < 0)
  286. text_transform = style->GetProperty(TEXT_TRANSFORM)->Get< int >();
  287. return text_transform;
  288. }
  289. const Property *ElementStyleCache::GetVerticalAlignProperty()
  290. {
  291. if (!vertical_align)
  292. vertical_align = style->GetProperty(VERTICAL_ALIGN);
  293. return vertical_align;
  294. }
  295. }
  296. }