ElementUtilities.cpp 17 KB

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