ElementUtilities.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. void ElementUtilities::GetElementsByClassName(ElementList& elements, Element* root_element, const String& class_name)
  78. {
  79. // Breadth first search on elements for the corresponding id
  80. typedef std::queue< Element* > SearchQueue;
  81. SearchQueue search_queue;
  82. for (int i = 0; i < root_element->GetNumChildren(); ++i)
  83. search_queue.push(root_element->GetChild(i));
  84. while (!search_queue.empty())
  85. {
  86. Element* element = search_queue.front();
  87. search_queue.pop();
  88. if (element->IsClassSet(class_name))
  89. elements.push_back(element);
  90. // Add all children to search.
  91. for (int i = 0; i < element->GetNumChildren(); i++)
  92. search_queue.push(element->GetChild(i));
  93. }
  94. }
  95. // Returns the element's font face.
  96. FontFaceHandle* ElementUtilities::GetFontFaceHandle(Element* element)
  97. {
  98. // Fetch the new font face.
  99. String font_family = element->GetProperty(FONT_FAMILY)->value.Get< String >();
  100. String font_charset = element->GetProperty(FONT_CHARSET)->value.Get< String >();
  101. Font::Style font_style = (Font::Style) element->GetProperty(FONT_STYLE)->value.Get< int >();
  102. Font::Weight font_weight = (Font::Weight) element->GetProperty(FONT_WEIGHT)->value.Get< int >();
  103. int font_size = Math::RealToInteger(element->ResolveProperty(FONT_SIZE, 0));
  104. FontFaceHandle* font = FontDatabase::GetFontFaceHandle(font_family, font_charset, font_style, font_weight, font_size);
  105. return font;
  106. }
  107. // Returns an element's font size, if it has a font defined.
  108. int ElementUtilities::GetFontSize(Element* element)
  109. {
  110. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  111. if (font_face_handle == NULL)
  112. return 0;
  113. return font_face_handle->GetSize();
  114. }
  115. // Returns an element's line height, if it has a font defined.
  116. int ElementUtilities::GetLineHeight(Element* element)
  117. {
  118. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  119. if (font_face_handle == NULL)
  120. return 0;
  121. int line_height = font_face_handle->GetLineHeight();
  122. float inch = element->GetRenderInterface()->GetPixelsPerInch();
  123. const Property* line_height_property = element->GetLineHeightProperty();
  124. switch (line_height_property->unit)
  125. {
  126. case Property::REM:
  127. // If the property is a straight number or an em measurement, then it scales the line height of the parent.
  128. // Ref: Equal to the computed value of ‘font-size’ on the root element.
  129. // Ref URL: http://www.w3.org/TR/css3-values/#rem-unit
  130. {
  131. Rocket::Core::ElementDocument* owner_document = element->GetOwnerDocument();
  132. if (owner_document == NULL)
  133. return 0;
  134. FontFaceHandle* parent_font_face_handle = owner_document->GetFontFaceHandle();
  135. if (parent_font_face_handle == NULL)
  136. return 0;
  137. int parent_line_height = parent_font_face_handle->GetLineHeight();
  138. const Property* parent_line_height_property = owner_document->GetLineHeightProperty();
  139. float calculated_parent_line_height = parent_line_height_property->value.Get< float >() * parent_line_height;
  140. return Math::Round((line_height_property->value.Get< float >() * calculated_parent_line_height));
  141. }
  142. case Property::NUMBER:
  143. case Property::EM:
  144. // If the property is a straight number or an em measurement, then it scales the line height.
  145. return Math::Round(line_height_property->value.Get< float >() * line_height);
  146. case Property::PERCENT:
  147. // If the property is a percentage, then it scales the line height.
  148. return Math::Round(line_height_property->value.Get< float >() * line_height * 0.01f);
  149. case Property::PX:
  150. // A px measurement.
  151. return Math::Round(line_height_property->value.Get< float >());
  152. case Property::INCH:
  153. // Values based on pixels-per-inch.
  154. return Math::Round(line_height_property->value.Get< float >() * inch);
  155. case Property::CM:
  156. return Math::Round(line_height_property->value.Get< float >() * inch * (1.0f / 2.54f));
  157. case Property::MM:
  158. return Math::Round(line_height_property->value.Get< float >() * inch * (1.0f / 25.4f));
  159. case Property::PT:
  160. return Math::Round(line_height_property->value.Get< float >() * inch * (1.0f / 72.0f));
  161. case Property::PC:
  162. return Math::Round(line_height_property->value.Get< float >() * inch * (1.0f / 6.0f));
  163. }
  164. return 0;
  165. }
  166. // Returns the width of a string rendered within the context of the given element.
  167. int ElementUtilities::GetStringWidth(Element* element, const WString& string)
  168. {
  169. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  170. if (font_face_handle == NULL)
  171. return 0;
  172. return font_face_handle->GetStringWidth(string);
  173. }
  174. void ElementUtilities::BindEventAttributes(Element* element)
  175. {
  176. int index = 0;
  177. String name;
  178. String value;
  179. // Check for and instance the on* events
  180. while(element->IterateAttributes(index, name, value))
  181. {
  182. if (name.Substring(0, 2) == "on")
  183. {
  184. EventListener* listener = Factory::InstanceEventListener(value, element);
  185. if (listener)
  186. element->AddEventListener(&name[2], listener, false);
  187. }
  188. }
  189. }
  190. // Generates the clipping region for an element.
  191. bool ElementUtilities::GetClippingRegion(Vector2i& clip_origin, Vector2i& clip_dimensions, Element* element)
  192. {
  193. clip_origin = Vector2i(-1, -1);
  194. clip_dimensions = Vector2i(-1, -1);
  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. {
  211. Vector2f element_origin_f = clipping_element->GetAbsoluteOffset(Box::CONTENT);
  212. Vector2f element_dimensions_f = clipping_element->GetBox().GetSize(Box::CONTENT);
  213. Vector2i element_origin(Math::RealToInteger(element_origin_f.x), Math::RealToInteger(element_origin_f.y));
  214. Vector2i element_dimensions(Math::RealToInteger(element_dimensions_f.x), Math::RealToInteger(element_dimensions_f.y));
  215. if (clip_origin == Vector2i(-1, -1) && clip_dimensions == Vector2i(-1, -1))
  216. {
  217. clip_origin = element_origin;
  218. clip_dimensions = element_dimensions;
  219. }
  220. else
  221. {
  222. Vector2i top_left(Math::Max(clip_origin.x, element_origin.x),
  223. Math::Max(clip_origin.y, element_origin.y));
  224. Vector2i bottom_right(Math::Min(clip_origin.x + clip_dimensions.x, element_origin.x + element_dimensions.x),
  225. Math::Min(clip_origin.y + clip_dimensions.y, element_origin.y + element_dimensions.y));
  226. clip_origin = top_left;
  227. clip_dimensions.x = Math::Max(0, bottom_right.x - top_left.x);
  228. clip_dimensions.y = Math::Max(0, bottom_right.y - top_left.y);
  229. }
  230. }
  231. }
  232. // If this region is meant to clip and we're skipping regions, update the counter.
  233. if (num_ignored_clips > 0)
  234. {
  235. if (clipping_element->IsClippingEnabled())
  236. num_ignored_clips--;
  237. }
  238. // Determine how many clip regions this ancestor ignores, and inherit the value. If this region ignores all
  239. // clipping regions, then we do too.
  240. int clipping_element_ignore_clips = clipping_element->GetClippingIgnoreDepth();
  241. if (clipping_element_ignore_clips < 0)
  242. break;
  243. num_ignored_clips = Math::Max(num_ignored_clips, clipping_element_ignore_clips);
  244. // Climb the tree to this region's parent.
  245. clipping_element = clipping_element->GetParentNode();
  246. }
  247. return clip_dimensions.x >= 0 && clip_dimensions.y >= 0;
  248. }
  249. // Sets the clipping region from an element and its ancestors.
  250. bool ElementUtilities::SetClippingRegion(Element* element, Context* context)
  251. {
  252. Rocket::Core::RenderInterface* render_interface = NULL;
  253. if (element)
  254. {
  255. render_interface = element->GetRenderInterface();
  256. if (!context)
  257. context = element->GetContext();
  258. }
  259. else if (context)
  260. {
  261. render_interface = context->GetRenderInterface();
  262. if (!render_interface)
  263. render_interface = GetRenderInterface();
  264. }
  265. if (!render_interface || !context)
  266. return false;
  267. Vector2i clip_origin, clip_dimensions;
  268. bool clip = element && GetClippingRegion(clip_origin, clip_dimensions, element);
  269. Vector2i current_origin;
  270. Vector2i current_dimensions;
  271. bool current_clip = context->GetActiveClipRegion(current_origin, current_dimensions);
  272. if (current_clip != clip || (clip && (clip_origin != current_origin || clip_dimensions != current_dimensions)))
  273. {
  274. context->SetActiveClipRegion(clip_origin, clip_dimensions);
  275. ApplyActiveClipRegion(context, render_interface);
  276. }
  277. return true;
  278. }
  279. void ElementUtilities::ApplyActiveClipRegion(Context* context, RenderInterface* render_interface)
  280. {
  281. if (render_interface == NULL)
  282. return;
  283. Vector2i origin;
  284. Vector2i dimensions;
  285. bool clip_enabled = context->GetActiveClipRegion(origin, dimensions);
  286. render_interface->EnableScissorRegion(clip_enabled);
  287. if (clip_enabled)
  288. {
  289. render_interface->SetScissorRegion(origin.x, origin.y, dimensions.x, dimensions.y);
  290. }
  291. }
  292. // Formats the contents of an element.
  293. bool ElementUtilities::FormatElement(Element* element, const Vector2f& containing_block)
  294. {
  295. LayoutEngine layout_engine;
  296. return layout_engine.FormatElement(element, containing_block);
  297. }
  298. // Generates the box for an element.
  299. void ElementUtilities::BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element)
  300. {
  301. LayoutEngine::BuildBox(box, containing_block, element, inline_element);
  302. }
  303. // Sizes and positions an element within its parent.
  304. bool ElementUtilities::PositionElement(Element* element, const Vector2f& offset)
  305. {
  306. Element* parent = element->GetParentNode();
  307. if (parent == NULL)
  308. return false;
  309. SetBox(element);
  310. SetElementOffset(element, offset);
  311. return true;
  312. }
  313. // Sizes an element, and positions it within its parent offset from the borders of its content area.
  314. bool ElementUtilities::PositionElement(Element* element, const Vector2f& offset, PositionAnchor anchor)
  315. {
  316. Element* parent = element->GetParentNode();
  317. if (parent == NULL)
  318. return false;
  319. SetBox(element);
  320. Vector2f containing_block = element->GetParentNode()->GetBox().GetSize(Box::CONTENT);
  321. Vector2f element_block = element->GetBox().GetSize(Box::MARGIN);
  322. Vector2f resolved_offset = offset;
  323. if (anchor & RIGHT)
  324. resolved_offset.x = containing_block.x - (element_block.x + offset.x);
  325. if (anchor & BOTTOM)
  326. resolved_offset.y = containing_block.y - (element_block.y + offset.y);
  327. SetElementOffset(element, resolved_offset);
  328. return true;
  329. }
  330. /*
  331. // Returns true if the element is visible within the current clipping region (if any), false if not.
  332. static bool IsElementVisible(const Element* ROCKET_UNUSED(element))
  333. {
  334. // Fix this when text elements have their sizes correctly set!
  335. return true;
  336. if (clip_root == NULL)
  337. return true;
  338. Vector2f element_position = element->GetAbsoluteOffset(Box::BORDER);
  339. for (int i = 0; i < element->GetNumBoxes(); ++i)
  340. {
  341. Vector2f box_position = element_position + element->GetBox(i).GetPosition(Box::MARGIN);
  342. Vector2f box_size = element->GetBox(i).GetSize(Box::MARGIN);
  343. // If both the left and right edges of this box are to the left of the clipping region,
  344. // then this box can't intersect the clipping region.
  345. if (box_position.x < clipping_region.top_left.x &&
  346. box_position.x + box_size.x < clipping_region.top_left.x)
  347. continue;
  348. // If both the left and right edges of this box are to the right of the clipping region,
  349. // then this box can't intersect the clipping region.
  350. if (box_position.x > clipping_region.bottom_right.x &&
  351. box_position.x + box_size.x > clipping_region.bottom_right.x)
  352. continue;
  353. // If both the top and bottom edges of this box are to the top of the clipping region,
  354. // then this box can't intersect the clipping region.
  355. if (box_position.y < clipping_region.top_left.y &&
  356. box_position.y + box_size.y < clipping_region.top_left.y)
  357. continue;
  358. // If both the top and bottom edges of this box are to the bottom of the clipping region,
  359. // then this box can't intersect the clipping region.
  360. if (box_position.y > clipping_region.bottom_right.y &&
  361. box_position.y + box_size.y > clipping_region.bottom_right.y)
  362. continue;
  363. // We intersect!
  364. return true;
  365. }
  366. return false;
  367. }
  368. */
  369. // Builds and sets the box for an element.
  370. static void SetBox(Element* element)
  371. {
  372. Element* parent = element->GetParentNode();
  373. ROCKET_ASSERT(parent != NULL);
  374. Vector2f containing_block = parent->GetBox().GetSize();
  375. containing_block.x -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  376. containing_block.y -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  377. Box box;
  378. LayoutEngine::BuildBox(box, containing_block, element);
  379. const Property *local_height;
  380. element->GetLocalDimensionProperties(NULL, &local_height);
  381. if (local_height == NULL)
  382. box.SetContent(Vector2f(box.GetSize().x, containing_block.y));
  383. element->SetBox(box);
  384. }
  385. // Positions an element relative to an offset parent.
  386. static void SetElementOffset(Element* element, const Vector2f& offset)
  387. {
  388. Vector2f relative_offset = element->GetParentNode()->GetBox().GetPosition(Box::CONTENT);
  389. relative_offset += offset;
  390. relative_offset.x += element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  391. relative_offset.y += element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  392. element->SetOffset(relative_offset, element->GetParentNode());
  393. }
  394. }
  395. }