ElementUtilities.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.h"
  30. #include "../../Include/RmlUi/Core/TransformState.h"
  31. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  32. #include <queue>
  33. #include <limits>
  34. #include "LayoutEngine.h"
  35. #include "ElementStyle.h"
  36. namespace Rml {
  37. namespace Core {
  38. // Builds and sets the box for an element.
  39. static void SetBox(Element* element)
  40. {
  41. Element* parent = element->GetParentNode();
  42. RMLUI_ASSERT(parent != nullptr);
  43. Vector2f containing_block = parent->GetBox().GetSize();
  44. containing_block.x -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  45. containing_block.y -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  46. Box box;
  47. LayoutEngine::BuildBox(box, containing_block, element);
  48. if (element->GetComputedValues().height.type != Style::Height::Auto)
  49. box.SetContent(Vector2f(box.GetSize().x, containing_block.y));
  50. element->SetBox(box);
  51. }
  52. // Positions an element relative to an offset parent.
  53. static void SetElementOffset(Element* element, const Vector2f& offset)
  54. {
  55. Vector2f relative_offset = element->GetParentNode()->GetBox().GetPosition(Box::CONTENT);
  56. relative_offset += offset;
  57. relative_offset.x += element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  58. relative_offset.y += element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  59. element->SetOffset(relative_offset, element->GetParentNode());
  60. }
  61. Element* ElementUtilities::GetElementById(Element* root_element, const String& id)
  62. {
  63. // Breadth first search on elements for the corresponding id
  64. typedef std::queue<Element*> SearchQueue;
  65. SearchQueue search_queue;
  66. search_queue.push(root_element);
  67. while (!search_queue.empty())
  68. {
  69. Element* element = search_queue.front();
  70. search_queue.pop();
  71. if (element->GetId() == id)
  72. {
  73. return element;
  74. }
  75. // Add all children to search
  76. for (int i = 0; i < element->GetNumChildren(); i++)
  77. search_queue.push(element->GetChild(i));
  78. }
  79. return nullptr;
  80. }
  81. void ElementUtilities::GetElementsByTagName(ElementList& elements, Element* root_element, const String& tag)
  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->GetTagName() == tag)
  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. void ElementUtilities::GetElementsByClassName(ElementList& elements, Element* root_element, const String& class_name)
  100. {
  101. // Breadth first search on elements for the corresponding id
  102. typedef std::queue< Element* > SearchQueue;
  103. SearchQueue search_queue;
  104. for (int i = 0; i < root_element->GetNumChildren(); ++i)
  105. search_queue.push(root_element->GetChild(i));
  106. while (!search_queue.empty())
  107. {
  108. Element* element = search_queue.front();
  109. search_queue.pop();
  110. if (element->IsClassSet(class_name))
  111. elements.push_back(element);
  112. // Add all children to search.
  113. for (int i = 0; i < element->GetNumChildren(); i++)
  114. search_queue.push(element->GetChild(i));
  115. }
  116. }
  117. float ElementUtilities::GetDensityIndependentPixelRatio(Element * element)
  118. {
  119. Context* context = element->GetContext();
  120. if (context == nullptr)
  121. return 1.0f;
  122. return context->GetDensityIndependentPixelRatio();
  123. }
  124. // Returns the width of a string rendered within the context of the given element.
  125. int ElementUtilities::GetStringWidth(Element* element, const String& string)
  126. {
  127. FontFaceHandle font_face_handle = element->GetFontFaceHandle();
  128. if (font_face_handle == 0)
  129. return 0;
  130. return GetFontEngineInterface()->GetStringWidth(font_face_handle, string);
  131. }
  132. void ElementUtilities::BindEventAttributes(Element* element)
  133. {
  134. // Check for and instance the on* events
  135. for (const auto& pair: element->GetAttributes())
  136. {
  137. if (pair.first.size() > 2 && pair.first[0] == 'o' && pair.first[1] == 'n')
  138. {
  139. EventListener* listener = Factory::InstanceEventListener(pair.second.Get<String>(), element);
  140. if (listener)
  141. element->AddEventListener(pair.first.substr(2), listener, false);
  142. }
  143. }
  144. }
  145. // Generates the clipping region for an element.
  146. bool ElementUtilities::GetClippingRegion(Vector2i& clip_origin, Vector2i& clip_dimensions, Element* element)
  147. {
  148. clip_origin = Vector2i(-1, -1);
  149. clip_dimensions = Vector2i(-1, -1);
  150. int num_ignored_clips = element->GetClippingIgnoreDepth();
  151. if (num_ignored_clips < 0)
  152. return false;
  153. // Search through the element's ancestors, finding all elements that clip their overflow and have overflow to clip.
  154. // For each that we find, we combine their clipping region with the existing clipping region, and so build up a
  155. // complete clipping region for the element.
  156. Element* clipping_element = element->GetParentNode();
  157. while (clipping_element != nullptr)
  158. {
  159. // Merge the existing clip region with the current clip region if we aren't ignoring clip regions.
  160. if (num_ignored_clips == 0 && clipping_element->IsClippingEnabled())
  161. {
  162. // Ignore nodes that don't clip.
  163. if (clipping_element->GetClientWidth() < clipping_element->GetScrollWidth()
  164. || clipping_element->GetClientHeight() < clipping_element->GetScrollHeight())
  165. {
  166. Vector2f element_origin_f = clipping_element->GetAbsoluteOffset(Box::CONTENT);
  167. Vector2f element_dimensions_f = clipping_element->GetBox().GetSize(Box::CONTENT);
  168. Vector2i element_origin(Math::RealToInteger(element_origin_f.x), Math::RealToInteger(element_origin_f.y));
  169. Vector2i element_dimensions(Math::RealToInteger(element_dimensions_f.x), Math::RealToInteger(element_dimensions_f.y));
  170. if (clip_origin == Vector2i(-1, -1) && clip_dimensions == Vector2i(-1, -1))
  171. {
  172. clip_origin = element_origin;
  173. clip_dimensions = element_dimensions;
  174. }
  175. else
  176. {
  177. Vector2i top_left(Math::Max(clip_origin.x, element_origin.x),
  178. Math::Max(clip_origin.y, element_origin.y));
  179. Vector2i bottom_right(Math::Min(clip_origin.x + clip_dimensions.x, element_origin.x + element_dimensions.x),
  180. Math::Min(clip_origin.y + clip_dimensions.y, element_origin.y + element_dimensions.y));
  181. clip_origin = top_left;
  182. clip_dimensions.x = Math::Max(0, bottom_right.x - top_left.x);
  183. clip_dimensions.y = Math::Max(0, bottom_right.y - top_left.y);
  184. }
  185. }
  186. }
  187. // If this region is meant to clip and we're skipping regions, update the counter.
  188. if (num_ignored_clips > 0)
  189. {
  190. if (clipping_element->IsClippingEnabled())
  191. num_ignored_clips--;
  192. }
  193. // Determine how many clip regions this ancestor ignores, and inherit the value. If this region ignores all
  194. // clipping regions, then we do too.
  195. int clipping_element_ignore_clips = clipping_element->GetClippingIgnoreDepth();
  196. if (clipping_element_ignore_clips < 0)
  197. break;
  198. num_ignored_clips = Math::Max(num_ignored_clips, clipping_element_ignore_clips);
  199. // Climb the tree to this region's parent.
  200. clipping_element = clipping_element->GetParentNode();
  201. }
  202. return clip_dimensions.x >= 0 && clip_dimensions.y >= 0;
  203. }
  204. // Sets the clipping region from an element and its ancestors.
  205. bool ElementUtilities::SetClippingRegion(Element* element, Context* context)
  206. {
  207. Rml::Core::RenderInterface* render_interface = nullptr;
  208. if (element)
  209. {
  210. render_interface = element->GetRenderInterface();
  211. if (!context)
  212. context = element->GetContext();
  213. }
  214. else if (context)
  215. {
  216. render_interface = context->GetRenderInterface();
  217. if (!render_interface)
  218. render_interface = GetRenderInterface();
  219. }
  220. if (!render_interface || !context)
  221. return false;
  222. Vector2i clip_origin = { -1, -1 };
  223. Vector2i clip_dimensions = { -1, -1 };
  224. bool clip = element && GetClippingRegion(clip_origin, clip_dimensions, element);
  225. Vector2i current_origin = { -1, -1 };
  226. Vector2i current_dimensions = { -1, -1 };
  227. bool current_clip = context->GetActiveClipRegion(current_origin, current_dimensions);
  228. if (current_clip != clip || (clip && (clip_origin != current_origin || clip_dimensions != current_dimensions)))
  229. {
  230. context->SetActiveClipRegion(clip_origin, clip_dimensions);
  231. ApplyActiveClipRegion(context, render_interface);
  232. }
  233. return true;
  234. }
  235. void ElementUtilities::ApplyActiveClipRegion(Context* context, RenderInterface* render_interface)
  236. {
  237. if (render_interface == nullptr)
  238. return;
  239. Vector2i origin;
  240. Vector2i dimensions;
  241. bool clip_enabled = context->GetActiveClipRegion(origin, dimensions);
  242. render_interface->EnableScissorRegion(clip_enabled);
  243. if (clip_enabled)
  244. {
  245. render_interface->SetScissorRegion(origin.x, origin.y, dimensions.x, dimensions.y);
  246. }
  247. }
  248. // Formats the contents of an element.
  249. bool ElementUtilities::FormatElement(Element* element, const Vector2f& containing_block)
  250. {
  251. LayoutEngine layout_engine;
  252. return layout_engine.FormatElement(element, containing_block);
  253. }
  254. // Generates the box for an element.
  255. void ElementUtilities::BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element)
  256. {
  257. LayoutEngine::BuildBox(box, containing_block, element, inline_element);
  258. }
  259. // Sizes an element, and positions it within its parent offset from the borders of its content area.
  260. bool ElementUtilities::PositionElement(Element* element, const Vector2f& offset, PositionAnchor anchor)
  261. {
  262. Element* parent = element->GetParentNode();
  263. if (parent == nullptr)
  264. return false;
  265. SetBox(element);
  266. Vector2f containing_block = element->GetParentNode()->GetBox().GetSize(Box::CONTENT);
  267. Vector2f element_block = element->GetBox().GetSize(Box::MARGIN);
  268. Vector2f resolved_offset = offset;
  269. if (anchor & RIGHT)
  270. resolved_offset.x = containing_block.x - (element_block.x + offset.x);
  271. if (anchor & BOTTOM)
  272. resolved_offset.y = containing_block.y - (element_block.y + offset.y);
  273. SetElementOffset(element, resolved_offset);
  274. return true;
  275. }
  276. bool ElementUtilities::ApplyTransform(Element &element)
  277. {
  278. RenderInterface *render_interface = element.GetRenderInterface();
  279. if (!render_interface)
  280. return false;
  281. static SmallUnorderedMap<RenderInterface*, const Matrix4f*> previous_matrix;
  282. const Matrix4f*& old_transform = previous_matrix.emplace(render_interface, nullptr).first->second;
  283. const Matrix4f* new_transform = nullptr;
  284. if (auto state = element.GetTransformState())
  285. new_transform = state->GetTransform();
  286. // Only changed transforms are submitted.
  287. if (old_transform != new_transform)
  288. {
  289. render_interface->SetTransform(new_transform);
  290. old_transform = new_transform;
  291. }
  292. return true;
  293. }
  294. }
  295. }