ElementUtilities.cpp 14 KB

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