ElementUtilities.cpp 15 KB

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