ElementUtilities.cpp 12 KB

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