ElementUtilities.cpp 16 KB

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