ElementStyleCache.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "ElementStyle.h"
  30. #include "ElementStyleCache.h"
  31. namespace Rml {
  32. namespace Core {
  33. ElementStyleCache::ElementStyleCache(ElementStyle *style) : style(style),
  34. top(NULL), bottom(NULL), left(NULL), right(NULL),
  35. border_top_width(NULL), border_bottom_width(NULL), border_left_width(NULL), border_right_width(NULL),
  36. margin_top(NULL), margin_bottom(NULL), margin_left(NULL), margin_right(NULL),
  37. padding_top(NULL), padding_bottom(NULL), padding_left(NULL), padding_right(NULL),
  38. width(NULL), height(NULL),
  39. local_width(NULL), local_height(NULL), have_local_width(false), have_local_height(false),
  40. overflow_x(-1), overflow_y(-1),
  41. position(-1), float_(-1), display(-1), whitespace(-1),
  42. line_height(NULL), text_align(-1), text_transform(-1), vertical_align(NULL),
  43. pointer_events(-1)
  44. {
  45. }
  46. void ElementStyleCache::Clear()
  47. {
  48. ClearOffset();
  49. ClearBorder();
  50. ClearMargin();
  51. ClearPadding();
  52. ClearDimensions();
  53. ClearOverflow();
  54. ClearPosition();
  55. ClearFloat();
  56. ClearDisplay();
  57. ClearWhitespace();
  58. }
  59. void ElementStyleCache::ClearInherited()
  60. {
  61. ClearLineHeight();
  62. ClearTextAlign();
  63. ClearTextTransform();
  64. ClearVerticalAlign();
  65. ClearPointerEvents();
  66. }
  67. void ElementStyleCache::ClearOffset()
  68. {
  69. top = bottom = left = right = NULL;
  70. }
  71. void ElementStyleCache::ClearBorder()
  72. {
  73. border_top_width = border_bottom_width = border_left_width = border_right_width = NULL;
  74. }
  75. void ElementStyleCache::ClearMargin()
  76. {
  77. margin_top = margin_bottom = margin_left = margin_right = NULL;
  78. }
  79. void ElementStyleCache::ClearPadding()
  80. {
  81. padding_top = padding_bottom = padding_left = padding_right = NULL;
  82. }
  83. void ElementStyleCache::ClearDimensions()
  84. {
  85. width = height = NULL;
  86. have_local_width = have_local_height = false;
  87. }
  88. void ElementStyleCache::ClearOverflow()
  89. {
  90. overflow_x = overflow_y = -1;
  91. }
  92. void ElementStyleCache::ClearPosition()
  93. {
  94. position = -1;
  95. }
  96. void ElementStyleCache::ClearFloat()
  97. {
  98. float_ = -1;
  99. }
  100. void ElementStyleCache::ClearDisplay()
  101. {
  102. display = -1;
  103. }
  104. void ElementStyleCache::ClearWhitespace()
  105. {
  106. whitespace = -1;
  107. }
  108. void ElementStyleCache::ClearLineHeight()
  109. {
  110. line_height = NULL;
  111. }
  112. void ElementStyleCache::ClearTextAlign()
  113. {
  114. text_align = -1;
  115. }
  116. void ElementStyleCache::ClearTextTransform()
  117. {
  118. text_transform = -1;
  119. }
  120. void ElementStyleCache::ClearVerticalAlign()
  121. {
  122. vertical_align = NULL;
  123. }
  124. void ElementStyleCache::ClearPointerEvents()
  125. {
  126. pointer_events = -1;
  127. }
  128. void ElementStyleCache::GetOffsetProperties(const Property **o_top, const Property **o_bottom, const Property **o_left, const Property **o_right )
  129. {
  130. if (o_top)
  131. {
  132. if (!top)
  133. top = style->GetProperty(TOP);
  134. *o_top = top;
  135. }
  136. if (o_bottom)
  137. {
  138. if (!bottom)
  139. bottom = style->GetProperty(BOTTOM);
  140. *o_bottom = bottom;
  141. }
  142. if (o_left)
  143. {
  144. if (!left)
  145. left = style->GetProperty(LEFT);
  146. *o_left = left;
  147. }
  148. if (o_right)
  149. {
  150. if (!right)
  151. right = style->GetProperty(RIGHT);
  152. *o_right = right;
  153. }
  154. }
  155. 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)
  156. {
  157. if (o_border_top_width)
  158. {
  159. if (!border_top_width)
  160. border_top_width = style->GetProperty(BORDER_TOP_WIDTH);
  161. *o_border_top_width = border_top_width;
  162. }
  163. if (o_border_bottom_width)
  164. {
  165. if (!border_bottom_width)
  166. border_bottom_width = style->GetProperty(BORDER_BOTTOM_WIDTH);
  167. *o_border_bottom_width = border_bottom_width;
  168. }
  169. if (o_border_left_width)
  170. {
  171. if (!border_left_width)
  172. border_left_width = style->GetProperty(BORDER_LEFT_WIDTH);
  173. *o_border_left_width = border_left_width;
  174. }
  175. if (o_border_right_width)
  176. {
  177. if (!border_right_width)
  178. border_right_width = style->GetProperty(BORDER_RIGHT_WIDTH);
  179. *o_border_right_width = border_right_width;
  180. }
  181. }
  182. void ElementStyleCache::GetMarginProperties(const Property **o_margin_top, const Property **o_margin_bottom, const Property **o_margin_left, const Property **o_margin_right)
  183. {
  184. if (o_margin_top)
  185. {
  186. if (!margin_top)
  187. margin_top = style->GetProperty(MARGIN_TOP);
  188. *o_margin_top = margin_top;
  189. }
  190. if (o_margin_bottom)
  191. {
  192. if (!margin_bottom)
  193. margin_bottom = style->GetProperty(MARGIN_BOTTOM);
  194. *o_margin_bottom = margin_bottom;
  195. }
  196. if (o_margin_left)
  197. {
  198. if (!margin_left)
  199. margin_left = style->GetProperty(MARGIN_LEFT);
  200. *o_margin_left = margin_left;
  201. }
  202. if (o_margin_right)
  203. {
  204. if (!margin_right)
  205. margin_right = style->GetProperty(MARGIN_RIGHT);
  206. *o_margin_right = margin_right;
  207. }
  208. }
  209. void ElementStyleCache::GetPaddingProperties(const Property **o_padding_top, const Property **o_padding_bottom, const Property **o_padding_left, const Property **o_padding_right)
  210. {
  211. if (o_padding_top)
  212. {
  213. if (!padding_top)
  214. padding_top = style->GetProperty(PADDING_TOP);
  215. *o_padding_top = padding_top;
  216. }
  217. if (o_padding_bottom)
  218. {
  219. if (!padding_bottom)
  220. padding_bottom = style->GetProperty(PADDING_BOTTOM);
  221. *o_padding_bottom = padding_bottom;
  222. }
  223. if (o_padding_left)
  224. {
  225. if (!padding_left)
  226. padding_left = style->GetProperty(PADDING_LEFT);
  227. *o_padding_left = padding_left;
  228. }
  229. if (o_padding_right)
  230. {
  231. if (!padding_right)
  232. padding_right = style->GetProperty(PADDING_RIGHT);
  233. *o_padding_right = padding_right;
  234. }
  235. }
  236. void ElementStyleCache::GetDimensionProperties(const Property **o_width, const Property **o_height)
  237. {
  238. if (o_width)
  239. {
  240. if (!width)
  241. width = style->GetProperty(WIDTH);
  242. *o_width = width;
  243. }
  244. if (o_height)
  245. {
  246. if (!height)
  247. height = style->GetProperty(HEIGHT);
  248. *o_height = height;
  249. }
  250. }
  251. void ElementStyleCache::GetLocalDimensionProperties(const Property **o_width, const Property **o_height)
  252. {
  253. if (o_width)
  254. {
  255. if (!have_local_width)
  256. {
  257. have_local_width = true;
  258. local_width = style->GetLocalProperty(WIDTH);
  259. }
  260. *o_width = local_width;
  261. }
  262. if (o_height)
  263. {
  264. if (!have_local_height)
  265. {
  266. have_local_height = true;
  267. local_height = style->GetLocalProperty(HEIGHT);
  268. }
  269. *o_height = local_height;
  270. }
  271. }
  272. void ElementStyleCache::GetOverflow(int *o_overflow_x, int *o_overflow_y)
  273. {
  274. if (o_overflow_x)
  275. {
  276. if (overflow_x < 0)
  277. overflow_x = style->GetProperty(OVERFLOW_X)->Get< int >();
  278. *o_overflow_x = overflow_x;
  279. }
  280. if (o_overflow_y)
  281. {
  282. if (overflow_y < 0)
  283. overflow_y = style->GetProperty(OVERFLOW_Y)->Get< int >();
  284. *o_overflow_y = overflow_y;
  285. }
  286. }
  287. int ElementStyleCache::GetPosition()
  288. {
  289. if (position < 0)
  290. position = style->GetProperty(POSITION)->Get< int >();
  291. return position;
  292. }
  293. int ElementStyleCache::GetFloat()
  294. {
  295. if (float_ < 0)
  296. float_ = style->GetProperty(FLOAT)->Get< int >();
  297. return float_;
  298. }
  299. int ElementStyleCache::GetDisplay()
  300. {
  301. if (display < 0)
  302. display = style->GetProperty(DISPLAY)->Get< int >();
  303. return display;
  304. }
  305. int ElementStyleCache::GetWhitespace()
  306. {
  307. if (whitespace < 0)
  308. whitespace = style->GetProperty(WHITE_SPACE)->Get< int >();
  309. return whitespace;
  310. }
  311. int ElementStyleCache::GetPointerEvents()
  312. {
  313. if (pointer_events < 0)
  314. pointer_events = style->GetProperty(POINTER_EVENTS)->Get< int >();
  315. return pointer_events;
  316. }
  317. const Property *ElementStyleCache::GetLineHeightProperty()
  318. {
  319. if (!line_height)
  320. line_height = style->GetProperty(LINE_HEIGHT);
  321. return line_height;
  322. }
  323. int ElementStyleCache::GetTextAlign()
  324. {
  325. if (text_align < 0)
  326. text_align = style->GetProperty(TEXT_ALIGN)->Get< int >();
  327. return text_align;
  328. }
  329. int ElementStyleCache::GetTextTransform()
  330. {
  331. if (text_transform < 0)
  332. text_transform = style->GetProperty(TEXT_TRANSFORM)->Get< int >();
  333. return text_transform;
  334. }
  335. const Property *ElementStyleCache::GetVerticalAlignProperty()
  336. {
  337. if (!vertical_align)
  338. vertical_align = style->GetProperty(VERTICAL_ALIGN);
  339. return vertical_align;
  340. }
  341. }
  342. }