ElementUtilities.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 "../../Include/Rocket/Core/ElementUtilities.h"
  29. #include <queue>
  30. #include <limits>
  31. #include "FontFaceHandle.h"
  32. #include "LayoutEngine.h"
  33. #include "../../Include/Rocket/Core.h"
  34. #include "../../Include/Rocket/Core/TransformPrimitive.h"
  35. #include "ElementStyle.h"
  36. namespace Rocket {
  37. namespace Core {
  38. // Builds and sets the box for an element.
  39. static void SetBox(Element* element);
  40. // Positions an element relative to an offset parent.
  41. static void SetElementOffset(Element* element, const Vector2f& offset);
  42. Element* ElementUtilities::GetElementById(Element* root_element, const String& id)
  43. {
  44. // Breadth first search on elements for the corresponding id
  45. typedef std::queue<Element*> SearchQueue;
  46. SearchQueue search_queue;
  47. search_queue.push(root_element);
  48. while (!search_queue.empty())
  49. {
  50. Element* element = search_queue.front();
  51. search_queue.pop();
  52. if (element->GetId() == id)
  53. {
  54. return element;
  55. }
  56. // Add all children to search
  57. for (int i = 0; i < element->GetNumChildren(); i++)
  58. search_queue.push(element->GetChild(i));
  59. }
  60. return NULL;
  61. }
  62. void ElementUtilities::GetElementsByTagName(ElementList& elements, Element* root_element, const String& tag)
  63. {
  64. // Breadth first search on elements for the corresponding id
  65. typedef std::queue< Element* > SearchQueue;
  66. SearchQueue search_queue;
  67. for (int i = 0; i < root_element->GetNumChildren(); ++i)
  68. search_queue.push(root_element->GetChild(i));
  69. while (!search_queue.empty())
  70. {
  71. Element* element = search_queue.front();
  72. search_queue.pop();
  73. if (element->GetTagName() == tag)
  74. elements.push_back(element);
  75. // Add all children to search.
  76. for (int i = 0; i < element->GetNumChildren(); i++)
  77. search_queue.push(element->GetChild(i));
  78. }
  79. }
  80. void ElementUtilities::GetElementsByClassName(ElementList& elements, Element* root_element, const String& class_name)
  81. {
  82. // Breadth first search on elements for the corresponding id
  83. typedef std::queue< Element* > SearchQueue;
  84. SearchQueue search_queue;
  85. for (int i = 0; i < root_element->GetNumChildren(); ++i)
  86. search_queue.push(root_element->GetChild(i));
  87. while (!search_queue.empty())
  88. {
  89. Element* element = search_queue.front();
  90. search_queue.pop();
  91. if (element->IsClassSet(class_name))
  92. elements.push_back(element);
  93. // Add all children to search.
  94. for (int i = 0; i < element->GetNumChildren(); i++)
  95. search_queue.push(element->GetChild(i));
  96. }
  97. }
  98. // Returns the element's font face.
  99. FontFaceHandle* ElementUtilities::GetFontFaceHandle(const Style::ComputedValues& computed_values)
  100. {
  101. static const String default_charset = "U+0020-007E";
  102. const String& charset = (computed_values.font_charset.empty() ? default_charset : computed_values.font_charset);
  103. int font_size = (int)computed_values.font_size;
  104. // TODO Synchronize enums
  105. FontFaceHandle* font = FontDatabase::GetFontFaceHandle(computed_values.font_family, charset, (Font::Style)computed_values.font_style, (Font::Weight)computed_values.font_weight, font_size);
  106. return font;
  107. }
  108. float ElementUtilities::GetDensityIndependentPixelRatio(Element * element)
  109. {
  110. Context* context = element->GetContext();
  111. if (context == NULL)
  112. return 1.0f;
  113. return context->GetDensityIndependentPixelRatio();
  114. }
  115. // Returns the width of a string rendered within the context of the given element.
  116. int ElementUtilities::GetStringWidth(Element* element, const WString& string)
  117. {
  118. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  119. if (font_face_handle == NULL)
  120. return 0;
  121. return font_face_handle->GetStringWidth(string);
  122. }
  123. void ElementUtilities::BindEventAttributes(Element* element)
  124. {
  125. // Check for and instance the on* events
  126. for (const auto& pair: element->GetAttributes())
  127. {
  128. if (pair.first.size() > 2 && pair.first[0] == 'o' && pair.first[1] == 'n')
  129. {
  130. EventListener* listener = Factory::InstanceEventListener(pair.second.Get<String>(), element);
  131. if (listener)
  132. element->AddEventListener(pair.first.substr(2), listener, false);
  133. }
  134. }
  135. }
  136. // Generates the clipping region for an element.
  137. bool ElementUtilities::GetClippingRegion(Vector2i& clip_origin, Vector2i& clip_dimensions, Element* element)
  138. {
  139. clip_origin = Vector2i(-1, -1);
  140. clip_dimensions = Vector2i(-1, -1);
  141. int num_ignored_clips = element->GetClippingIgnoreDepth();
  142. if (num_ignored_clips < 0)
  143. return false;
  144. // Search through the element's ancestors, finding all elements that clip their overflow and have overflow to clip.
  145. // For each that we find, we combine their clipping region with the existing clipping region, and so build up a
  146. // complete clipping region for the element.
  147. Element* clipping_element = element->GetParentNode();
  148. while (clipping_element != NULL)
  149. {
  150. // Merge the existing clip region with the current clip region if we aren't ignoring clip regions.
  151. if (num_ignored_clips == 0 && clipping_element->IsClippingEnabled())
  152. {
  153. // Ignore nodes that don't clip.
  154. if (clipping_element->GetClientWidth() < clipping_element->GetScrollWidth()
  155. || clipping_element->GetClientHeight() < clipping_element->GetScrollHeight())
  156. {
  157. Vector2f element_origin_f = clipping_element->GetAbsoluteOffset(Box::CONTENT);
  158. Vector2f element_dimensions_f = clipping_element->GetBox().GetSize(Box::CONTENT);
  159. Vector2i element_origin(Math::RealToInteger(element_origin_f.x), Math::RealToInteger(element_origin_f.y));
  160. Vector2i element_dimensions(Math::RealToInteger(element_dimensions_f.x), Math::RealToInteger(element_dimensions_f.y));
  161. if (clip_origin == Vector2i(-1, -1) && clip_dimensions == Vector2i(-1, -1))
  162. {
  163. clip_origin = element_origin;
  164. clip_dimensions = element_dimensions;
  165. }
  166. else
  167. {
  168. Vector2i top_left(Math::Max(clip_origin.x, element_origin.x),
  169. Math::Max(clip_origin.y, element_origin.y));
  170. Vector2i bottom_right(Math::Min(clip_origin.x + clip_dimensions.x, element_origin.x + element_dimensions.x),
  171. Math::Min(clip_origin.y + clip_dimensions.y, element_origin.y + element_dimensions.y));
  172. clip_origin = top_left;
  173. clip_dimensions.x = Math::Max(0, bottom_right.x - top_left.x);
  174. clip_dimensions.y = Math::Max(0, bottom_right.y - top_left.y);
  175. }
  176. }
  177. }
  178. // If this region is meant to clip and we're skipping regions, update the counter.
  179. if (num_ignored_clips > 0)
  180. {
  181. if (clipping_element->IsClippingEnabled())
  182. num_ignored_clips--;
  183. }
  184. // Determine how many clip regions this ancestor ignores, and inherit the value. If this region ignores all
  185. // clipping regions, then we do too.
  186. int clipping_element_ignore_clips = clipping_element->GetClippingIgnoreDepth();
  187. if (clipping_element_ignore_clips < 0)
  188. break;
  189. num_ignored_clips = Math::Max(num_ignored_clips, clipping_element_ignore_clips);
  190. // Climb the tree to this region's parent.
  191. clipping_element = clipping_element->GetParentNode();
  192. }
  193. return clip_dimensions.x >= 0 && clip_dimensions.y >= 0;
  194. }
  195. // Sets the clipping region from an element and its ancestors.
  196. bool ElementUtilities::SetClippingRegion(Element* element, Context* context)
  197. {
  198. Rocket::Core::RenderInterface* render_interface = NULL;
  199. if (element)
  200. {
  201. render_interface = element->GetRenderInterface();
  202. if (!context)
  203. context = element->GetContext();
  204. }
  205. else if (context)
  206. {
  207. render_interface = context->GetRenderInterface();
  208. if (!render_interface)
  209. render_interface = GetRenderInterface();
  210. }
  211. if (!render_interface || !context)
  212. return false;
  213. Vector2i clip_origin, clip_dimensions;
  214. bool clip = element && GetClippingRegion(clip_origin, clip_dimensions, element);
  215. Vector2i current_origin;
  216. Vector2i current_dimensions;
  217. bool current_clip = context->GetActiveClipRegion(current_origin, current_dimensions);
  218. if (current_clip != clip || (clip && (clip_origin != current_origin || clip_dimensions != current_dimensions)))
  219. {
  220. context->SetActiveClipRegion(clip_origin, clip_dimensions);
  221. ApplyActiveClipRegion(context, render_interface);
  222. }
  223. return true;
  224. }
  225. void ElementUtilities::ApplyActiveClipRegion(Context* context, RenderInterface* render_interface)
  226. {
  227. if (render_interface == NULL)
  228. return;
  229. Vector2i origin;
  230. Vector2i dimensions;
  231. bool clip_enabled = context->GetActiveClipRegion(origin, dimensions);
  232. render_interface->EnableScissorRegion(clip_enabled);
  233. if (clip_enabled)
  234. {
  235. render_interface->SetScissorRegion(origin.x, origin.y, dimensions.x, dimensions.y);
  236. }
  237. }
  238. // Formats the contents of an element.
  239. bool ElementUtilities::FormatElement(Element* element, const Vector2f& containing_block)
  240. {
  241. LayoutEngine layout_engine;
  242. return layout_engine.FormatElement(element, containing_block);
  243. }
  244. // Generates the box for an element.
  245. void ElementUtilities::BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element)
  246. {
  247. LayoutEngine::BuildBox(box, containing_block, element, inline_element);
  248. }
  249. // Sizes an element, and positions it within its parent offset from the borders of its content area.
  250. bool ElementUtilities::PositionElement(Element* element, const Vector2f& offset, PositionAnchor anchor)
  251. {
  252. Element* parent = element->GetParentNode();
  253. if (parent == NULL)
  254. return false;
  255. SetBox(element);
  256. Vector2f containing_block = element->GetParentNode()->GetBox().GetSize(Box::CONTENT);
  257. Vector2f element_block = element->GetBox().GetSize(Box::MARGIN);
  258. Vector2f resolved_offset = offset;
  259. if (anchor & RIGHT)
  260. resolved_offset.x = containing_block.x - (element_block.x + offset.x);
  261. if (anchor & BOTTOM)
  262. resolved_offset.y = containing_block.y - (element_block.y + offset.y);
  263. SetElementOffset(element, resolved_offset);
  264. return true;
  265. }
  266. // Builds and sets the box for an element.
  267. static void SetBox(Element* element)
  268. {
  269. Element* parent = element->GetParentNode();
  270. ROCKET_ASSERT(parent != NULL);
  271. Vector2f containing_block = parent->GetBox().GetSize();
  272. containing_block.x -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  273. containing_block.y -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  274. Box box;
  275. LayoutEngine::BuildBox(box, containing_block, element);
  276. if (element->GetComputedValues().height.type != Style::Height::Auto)
  277. box.SetContent(Vector2f(box.GetSize().x, containing_block.y));
  278. element->SetBox(box);
  279. }
  280. // Positions an element relative to an offset parent.
  281. static void SetElementOffset(Element* element, const Vector2f& offset)
  282. {
  283. Vector2f relative_offset = element->GetParentNode()->GetBox().GetPosition(Box::CONTENT);
  284. relative_offset += offset;
  285. relative_offset.x += element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  286. relative_offset.y += element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  287. element->SetOffset(relative_offset, element->GetParentNode());
  288. }
  289. // Applies an element's `perspective' and `transform' properties.
  290. bool ElementUtilities::ApplyTransform(Element &element, bool apply)
  291. {
  292. Context *context = element.GetContext();
  293. if (!context)
  294. {
  295. return false;
  296. }
  297. RenderInterface *render_interface = element.GetRenderInterface();
  298. if (!render_interface)
  299. {
  300. return false;
  301. }
  302. const TransformState *local_perspective, *perspective, *transform;
  303. element.GetEffectiveTransformState(&local_perspective, &perspective, &transform);
  304. bool have_perspective = false;
  305. float perspective_distance = 0.0f;
  306. Matrix4f the_projection;
  307. if (local_perspective)
  308. {
  309. TransformState::LocalPerspective the_local_perspective;
  310. local_perspective->GetLocalPerspective(&the_local_perspective);
  311. have_perspective = true;
  312. perspective_distance = the_local_perspective.distance;
  313. the_projection = the_local_perspective.GetProjection();
  314. }
  315. else if (perspective)
  316. {
  317. TransformState::Perspective the_perspective;
  318. perspective->GetPerspective(&the_perspective);
  319. have_perspective = true;
  320. perspective_distance = the_perspective.distance;
  321. the_projection = the_perspective.GetProjection();
  322. }
  323. bool have_transform = false;
  324. Matrix4f the_transform;
  325. if (transform)
  326. {
  327. transform->GetRecursiveTransform(&the_transform);
  328. have_transform = true;
  329. }
  330. if (have_perspective && perspective_distance >= 0)
  331. {
  332. // If we are to apply a custom projection, then we need to cancel the global one first.
  333. Matrix4f global_pv_inv;
  334. bool have_global_pv_inv = context->GetViewState().GetProjectionViewInv(global_pv_inv);
  335. if (have_global_pv_inv && have_transform)
  336. {
  337. if (apply)
  338. {
  339. render_interface->PushTransform(global_pv_inv * the_projection * the_transform);
  340. }
  341. else
  342. {
  343. render_interface->PopTransform(global_pv_inv * the_projection * the_transform);
  344. }
  345. return true;
  346. }
  347. else if (have_global_pv_inv)
  348. {
  349. if (apply)
  350. {
  351. render_interface->PushTransform(global_pv_inv * the_projection);
  352. }
  353. else
  354. {
  355. render_interface->PopTransform(global_pv_inv * the_projection);
  356. }
  357. return true;
  358. }
  359. else if (have_transform)
  360. {
  361. // The context has not received Process(Projection|View)Change() calls.
  362. // Assume we don't really need to cancel.
  363. if (apply)
  364. {
  365. render_interface->PushTransform(the_transform);
  366. }
  367. else
  368. {
  369. render_interface->PopTransform(the_transform);
  370. }
  371. return true;
  372. }
  373. else
  374. {
  375. return false;
  376. }
  377. }
  378. else
  379. {
  380. if (have_transform)
  381. {
  382. if (apply)
  383. {
  384. render_interface->PushTransform(the_transform);
  385. }
  386. else
  387. {
  388. render_interface->PopTransform(the_transform);
  389. }
  390. return true;
  391. }
  392. else
  393. {
  394. return false;
  395. }
  396. }
  397. }
  398. // Unapplies an element's `perspective' and `transform' properties.
  399. bool ElementUtilities::UnapplyTransform(Element &element)
  400. {
  401. return ApplyTransform(element, false);
  402. }
  403. }
  404. }