ElementUtilities.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 <Rocket/Core/ElementUtilities.h>
  29. #include <queue>
  30. #include "FontFaceHandle.h"
  31. #include "LayoutEngine.h"
  32. #include <Rocket/Core.h>
  33. namespace Rocket {
  34. namespace Core {
  35. // Builds and sets the box for an element.
  36. static void SetBox(Element* element);
  37. // Positions an element relative to an offset parent.
  38. static void SetElementOffset(Element* element, const Vector2f& offset);
  39. Element* ElementUtilities::GetElementById(Element* root_element, const String& id)
  40. {
  41. // Breadth first search on elements for the corresponding id
  42. typedef std::queue<Element*> SearchQueue;
  43. SearchQueue search_queue;
  44. search_queue.push(root_element);
  45. while (!search_queue.empty())
  46. {
  47. Element* element = search_queue.front();
  48. search_queue.pop();
  49. if (element->GetId() == id)
  50. {
  51. return element;
  52. }
  53. // Add all children to search
  54. for (int i = 0; i < element->GetNumChildren(); i++)
  55. search_queue.push(element->GetChild(i));
  56. }
  57. return NULL;
  58. }
  59. void ElementUtilities::GetElementsByTagName(ElementList& elements, Element* root_element, const String& tag)
  60. {
  61. // Breadth first search on elements for the corresponding id
  62. typedef std::queue< Element* > SearchQueue;
  63. SearchQueue search_queue;
  64. for (int i = 0; i < root_element->GetNumChildren(); ++i)
  65. search_queue.push(root_element->GetChild(i));
  66. while (!search_queue.empty())
  67. {
  68. Element* element = search_queue.front();
  69. search_queue.pop();
  70. if (element->GetTagName() == tag)
  71. elements.push_back(element);
  72. // Add all children to search.
  73. for (int i = 0; i < element->GetNumChildren(); i++)
  74. search_queue.push(element->GetChild(i));
  75. }
  76. }
  77. // Returns the element's font face.
  78. FontFaceHandle* ElementUtilities::GetFontFaceHandle(Element* element)
  79. {
  80. // Fetch the new font face.
  81. String font_family = element->GetProperty(FONT_FAMILY)->value.Get< String >();
  82. String font_charset = element->GetProperty(FONT_CHARSET)->value.Get< String >();
  83. Font::Style font_style = (Font::Style) element->GetProperty(FONT_STYLE)->value.Get< int >();
  84. Font::Weight font_weight = (Font::Weight) element->GetProperty(FONT_WEIGHT)->value.Get< int >();
  85. int font_size = Math::RealToInteger(element->ResolveProperty(FONT_SIZE, 0));
  86. FontFaceHandle* font = FontDatabase::GetFontFaceHandle(font_family, font_charset, font_style, font_weight, font_size);
  87. return font;
  88. }
  89. // Returns an element's font size, if it has a font defined.
  90. int ElementUtilities::GetFontSize(Element* element)
  91. {
  92. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  93. if (font_face_handle == NULL)
  94. return 0;
  95. return font_face_handle->GetSize();
  96. }
  97. // Returns an element's line height, if it has a font defined.
  98. int ElementUtilities::GetLineHeight(Element* element)
  99. {
  100. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  101. if (font_face_handle == NULL)
  102. return 0;
  103. int line_height = font_face_handle->GetLineHeight();
  104. const Property* line_height_property = element->GetProperty(LINE_HEIGHT);
  105. // If the property is a straight number or an em measurement, then it scales the line height.
  106. if (line_height_property->unit == Property::NUMBER ||
  107. line_height_property->unit == Property::EM)
  108. return Math::Round(line_height_property->value.Get< float >() * line_height);
  109. // If the property is a percentage, then it scales the line height.
  110. else if (line_height_property->unit == Property::PERCENT)
  111. return Math::Round(line_height_property->value.Get< float >() * line_height * 0.01f);
  112. // Otherwise, we're a px measurement.
  113. else if (line_height_property->unit == Property::PX)
  114. return Math::Round(line_height_property->value.Get< float >());
  115. return 0;
  116. }
  117. // Returns the width of a string rendered within the context of the given element.
  118. int ElementUtilities::GetStringWidth(Element* element, const WString& string)
  119. {
  120. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  121. if (font_face_handle == NULL)
  122. return 0;
  123. return font_face_handle->GetStringWidth(string);
  124. }
  125. void ElementUtilities::BindEventAttributes(Element* element)
  126. {
  127. int index = 0;
  128. String name;
  129. String value;
  130. // Check for and instance the on* events
  131. while(element->IterateAttributes(index, name, value))
  132. {
  133. if (name.Substring(0, 2) == "on")
  134. {
  135. EventListener* listener = Factory::InstanceEventListener(value, element);
  136. if (listener)
  137. element->AddEventListener(&name[2], listener, false);
  138. }
  139. }
  140. }
  141. // Generates the clipping region for an element.
  142. bool ElementUtilities::GetClippingRegion(Vector2i& clip_origin, Vector2i& clip_dimensions, Element* element)
  143. {
  144. clip_origin = Vector2i(-1, -1);
  145. clip_dimensions = Vector2i(-1, -1);
  146. int num_ignored_clips = element->GetClippingIgnoreDepth();
  147. if (num_ignored_clips < 0)
  148. return false;
  149. // Search through the element's ancestors, finding all elements that clip their overflow and have overflow to clip.
  150. // For each that we find, we combine their clipping region with the existing clipping region, and so build up a
  151. // complete clipping region for the element.
  152. Element* clipping_element = element->GetParentNode();
  153. while (clipping_element != NULL)
  154. {
  155. // Merge the existing clip region with the current clip region if we aren't ignoring clip regions.
  156. if (num_ignored_clips == 0 && clipping_element->IsClippingEnabled())
  157. {
  158. // Ignore nodes that don't clip.
  159. if (clipping_element->GetClientWidth() < clipping_element->GetScrollWidth()
  160. || clipping_element->GetClientHeight() < clipping_element->GetScrollHeight())
  161. {
  162. Vector2f element_origin_f = clipping_element->GetAbsoluteOffset(Box::CONTENT);
  163. Vector2f element_dimensions_f = clipping_element->GetBox().GetSize(Box::CONTENT);
  164. Vector2i element_origin(Math::RealToInteger(element_origin_f.x), Math::RealToInteger(element_origin_f.y));
  165. Vector2i element_dimensions(Math::RealToInteger(element_dimensions_f.x), Math::RealToInteger(element_dimensions_f.y));
  166. if (clip_origin == Vector2i(-1, -1) && clip_dimensions == Vector2i(-1, -1))
  167. {
  168. clip_origin = element_origin;
  169. clip_dimensions = element_dimensions;
  170. }
  171. else
  172. {
  173. Vector2i top_left(Math::Max(clip_origin.x, element_origin.x),
  174. Math::Max(clip_origin.y, element_origin.y));
  175. Vector2i bottom_right(Math::Min(clip_origin.x + clip_dimensions.x, element_origin.x + element_dimensions.x),
  176. Math::Min(clip_origin.y + clip_dimensions.y, element_origin.y + element_dimensions.y));
  177. clip_origin = top_left;
  178. clip_dimensions.x = Math::Max(0, bottom_right.x - top_left.x);
  179. clip_dimensions.y = Math::Max(0, bottom_right.y - top_left.y);
  180. }
  181. }
  182. }
  183. // If this region is meant to clip and we're skipping regions, update the counter.
  184. if (num_ignored_clips > 0)
  185. {
  186. if (clipping_element->IsClippingEnabled())
  187. num_ignored_clips--;
  188. }
  189. // Determine how many clip regions this ancestor ignores, and inherit the value. If this region ignores all
  190. // clipping regions, then we do too.
  191. int clipping_element_ignore_clips = clipping_element->GetClippingIgnoreDepth();
  192. if (clipping_element_ignore_clips < 0)
  193. break;
  194. num_ignored_clips = Math::Max(num_ignored_clips, clipping_element_ignore_clips);
  195. // Climb the tree to this region's parent.
  196. clipping_element = clipping_element->GetParentNode();
  197. }
  198. return clip_dimensions.x >= 0 && clip_dimensions.y >= 0;
  199. }
  200. // Sets the clipping region from an element and its ancestors.
  201. bool ElementUtilities::SetClippingRegion(Element* element, Context* context)
  202. {
  203. Rocket::Core::RenderInterface* render_interface = NULL;
  204. if (element)
  205. {
  206. render_interface = element->GetRenderInterface();
  207. if (!context)
  208. context = element->GetContext();
  209. }
  210. else if (context)
  211. {
  212. render_interface = context->GetRenderInterface();
  213. if (!render_interface)
  214. render_interface = GetRenderInterface();
  215. }
  216. if (!render_interface || !context)
  217. return false;
  218. Vector2i clip_origin, clip_dimensions;
  219. bool clip = element && GetClippingRegion(clip_origin, clip_dimensions, element);
  220. Vector2i current_origin;
  221. Vector2i current_dimensions;
  222. bool current_clip = context->GetActiveClipRegion(current_origin, current_dimensions);
  223. if (current_clip != clip || (clip && (clip_origin != current_origin || clip_dimensions != current_dimensions)))
  224. {
  225. context->SetActiveClipRegion(clip_origin, clip_dimensions);
  226. ApplyActiveClipRegion(context, render_interface);
  227. }
  228. return true;
  229. }
  230. void ElementUtilities::ApplyActiveClipRegion(Context* context, RenderInterface* render_interface)
  231. {
  232. if (render_interface == NULL)
  233. return;
  234. Vector2i origin;
  235. Vector2i dimensions;
  236. bool clip_enabled = context->GetActiveClipRegion(origin, dimensions);
  237. render_interface->EnableScissorRegion(clip_enabled);
  238. if (clip_enabled)
  239. {
  240. render_interface->SetScissorRegion(origin.x, origin.y, dimensions.x, dimensions.y);
  241. }
  242. }
  243. // Formats the contents of an element.
  244. bool ElementUtilities::FormatElement(Element* element, const Vector2f& containing_block)
  245. {
  246. LayoutEngine layout_engine;
  247. return layout_engine.FormatElement(element, containing_block);
  248. }
  249. // Generates the box for an element.
  250. void ElementUtilities::BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element)
  251. {
  252. LayoutEngine::BuildBox(box, containing_block, element, inline_element);
  253. }
  254. // Sizes and positions an element within its parent.
  255. bool ElementUtilities::PositionElement(Element* element, const Vector2f& offset)
  256. {
  257. Element* parent = element->GetParentNode();
  258. if (parent == NULL)
  259. return false;
  260. SetBox(element);
  261. SetElementOffset(element, offset);
  262. return true;
  263. }
  264. // Sizes an element, and positions it within its parent offset from the borders of its content area.
  265. bool ElementUtilities::PositionElement(Element* element, const Vector2f& offset, PositionAnchor anchor)
  266. {
  267. Element* parent = element->GetParentNode();
  268. if (parent == NULL)
  269. return false;
  270. SetBox(element);
  271. Vector2f containing_block = element->GetParentNode()->GetBox().GetSize(Box::CONTENT);
  272. Vector2f element_block = element->GetBox().GetSize(Box::MARGIN);
  273. Vector2f resolved_offset = offset;
  274. if (anchor & RIGHT)
  275. resolved_offset.x = containing_block.x - (element_block.x + offset.x);
  276. if (anchor & BOTTOM)
  277. resolved_offset.y = containing_block.y - (element_block.y + offset.y);
  278. SetElementOffset(element, resolved_offset);
  279. return true;
  280. }
  281. /*
  282. // Returns true if the element is visible within the current clipping region (if any), false if not.
  283. static bool IsElementVisible(const Element* ROCKET_UNUSED(element))
  284. {
  285. // Fix this when text elements have their sizes correctly set!
  286. return true;
  287. if (clip_root == NULL)
  288. return true;
  289. Vector2f element_position = element->GetAbsoluteOffset(Box::BORDER);
  290. for (int i = 0; i < element->GetNumBoxes(); ++i)
  291. {
  292. Vector2f box_position = element_position + element->GetBox(i).GetPosition(Box::MARGIN);
  293. Vector2f box_size = element->GetBox(i).GetSize(Box::MARGIN);
  294. // If both the left and right edges of this box are to the left of the clipping region,
  295. // then this box can't intersect the clipping region.
  296. if (box_position.x < clipping_region.top_left.x &&
  297. box_position.x + box_size.x < clipping_region.top_left.x)
  298. continue;
  299. // If both the left and right edges of this box are to the right of the clipping region,
  300. // then this box can't intersect the clipping region.
  301. if (box_position.x > clipping_region.bottom_right.x &&
  302. box_position.x + box_size.x > clipping_region.bottom_right.x)
  303. continue;
  304. // If both the top and bottom edges of this box are to the top of the clipping region,
  305. // then this box can't intersect the clipping region.
  306. if (box_position.y < clipping_region.top_left.y &&
  307. box_position.y + box_size.y < clipping_region.top_left.y)
  308. continue;
  309. // If both the top and bottom edges of this box are to the bottom of the clipping region,
  310. // then this box can't intersect the clipping region.
  311. if (box_position.y > clipping_region.bottom_right.y &&
  312. box_position.y + box_size.y > clipping_region.bottom_right.y)
  313. continue;
  314. // We intersect!
  315. return true;
  316. }
  317. return false;
  318. }
  319. */
  320. // Builds and sets the box for an element.
  321. static void SetBox(Element* element)
  322. {
  323. Element* parent = element->GetParentNode();
  324. ROCKET_ASSERT(parent != NULL);
  325. Vector2f containing_block = parent->GetBox().GetSize();
  326. containing_block.x -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  327. containing_block.y -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  328. Box box;
  329. LayoutEngine::BuildBox(box, containing_block, element);
  330. if (element->GetLocalProperty(HEIGHT) == NULL)
  331. box.SetContent(Vector2f(box.GetSize().x, containing_block.y));
  332. element->SetBox(box);
  333. }
  334. // Positions an element relative to an offset parent.
  335. static void SetElementOffset(Element* element, const Vector2f& offset)
  336. {
  337. Vector2f relative_offset = element->GetParentNode()->GetBox().GetPosition(Box::CONTENT);
  338. relative_offset += offset;
  339. relative_offset.x += element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  340. relative_offset.y += element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  341. element->SetOffset(relative_offset, element->GetParentNode());
  342. }
  343. }
  344. }