ElementUtilities.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 "../../Include/Rocket/Core/ElementUtilities.h"
  29. #include <queue>
  30. #include <limits>
  31. #include "FontFaceHandle.h"
  32. #include "LayoutEngine.h"
  33. #include "../../Include/Rocket/Core.h"
  34. #include "../../Include/Rocket/Core/TransformPrimitive.h"
  35. namespace Rocket {
  36. namespace Core {
  37. // Builds and sets the box for an element.
  38. static void SetBox(Element* element);
  39. // Positions an element relative to an offset parent.
  40. static void SetElementOffset(Element* element, const Vector2f& offset);
  41. Element* ElementUtilities::GetElementById(Element* root_element, const String& id)
  42. {
  43. // Breadth first search on elements for the corresponding id
  44. typedef std::queue<Element*> SearchQueue;
  45. SearchQueue search_queue;
  46. search_queue.push(root_element);
  47. while (!search_queue.empty())
  48. {
  49. Element* element = search_queue.front();
  50. search_queue.pop();
  51. if (element->GetId() == 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. void ElementUtilities::GetElementsByClassName(ElementList& elements, Element* root_element, const String& class_name)
  80. {
  81. // Breadth first search on elements for the corresponding id
  82. typedef std::queue< Element* > SearchQueue;
  83. SearchQueue search_queue;
  84. for (int i = 0; i < root_element->GetNumChildren(); ++i)
  85. search_queue.push(root_element->GetChild(i));
  86. while (!search_queue.empty())
  87. {
  88. Element* element = search_queue.front();
  89. search_queue.pop();
  90. if (element->IsClassSet(class_name))
  91. elements.push_back(element);
  92. // Add all children to search.
  93. for (int i = 0; i < element->GetNumChildren(); i++)
  94. search_queue.push(element->GetChild(i));
  95. }
  96. }
  97. // Returns the element's font face.
  98. FontFaceHandle* ElementUtilities::GetFontFaceHandle(Element* element)
  99. {
  100. // Fetch the new font face.
  101. String font_family = element->GetProperty(FONT_FAMILY)->value.Get< String >();
  102. String font_charset = element->GetProperty(FONT_CHARSET)->value.Get< String >();
  103. Font::Style font_style = (Font::Style) element->GetProperty(FONT_STYLE)->value.Get< int >();
  104. Font::Weight font_weight = (Font::Weight) element->GetProperty(FONT_WEIGHT)->value.Get< int >();
  105. int font_size = Math::RealToInteger(element->ResolveProperty(FONT_SIZE, 0));
  106. FontFaceHandle* font = FontDatabase::GetFontFaceHandle(font_family, font_charset, font_style, font_weight, font_size);
  107. return font;
  108. }
  109. float ElementUtilities::GetDensityIndependentPixelRatio(Element * element)
  110. {
  111. Context* context = element->GetContext();
  112. if (context == NULL)
  113. return 1.0f;
  114. return context->GetDensityIndependentPixelRatio();
  115. }
  116. // Returns an element's font size, if it has a font defined.
  117. int ElementUtilities::GetFontSize(Element* element)
  118. {
  119. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  120. if (font_face_handle == NULL)
  121. return 0;
  122. return font_face_handle->GetSize();
  123. }
  124. // Returns an element's line height, if it has a font defined.
  125. int ElementUtilities::GetLineHeight(Element* element)
  126. {
  127. const Property* line_height_property = element->GetLineHeightProperty();
  128. Element* font_element = element;
  129. if (line_height_property->unit == Property::REM)
  130. font_element = element->GetOwnerDocument();
  131. FontFaceHandle* font_face_handle = font_element->GetFontFaceHandle();
  132. if (font_face_handle == NULL)
  133. return 0;
  134. int line_height = font_face_handle->GetLineHeight();
  135. float inch = element->GetRenderInterface()->GetPixelsPerInch();
  136. switch (line_height_property->unit)
  137. {
  138. ROCKET_UNUSED_SWITCH_ENUM(Property::UNKNOWN);
  139. ROCKET_UNUSED_SWITCH_ENUM(Property::KEYWORD);
  140. ROCKET_UNUSED_SWITCH_ENUM(Property::STRING);
  141. ROCKET_UNUSED_SWITCH_ENUM(Property::COLOUR);
  142. ROCKET_UNUSED_SWITCH_ENUM(Property::ABSOLUTE_UNIT);
  143. ROCKET_UNUSED_SWITCH_ENUM(Property::PPI_UNIT);
  144. ROCKET_UNUSED_SWITCH_ENUM(Property::RELATIVE_UNIT);
  145. case Property::NUMBER:
  146. case Property::EM:
  147. case Property::REM:
  148. // If the property is a straight number or an em measurement, then it scales the line height.
  149. return Math::RoundToInteger(line_height_property->value.Get< float >() * line_height);
  150. case Property::PERCENT:
  151. // If the property is a percentage, then it scales the line height.
  152. return Math::RoundToInteger(line_height_property->value.Get< float >() * line_height * 0.01f);
  153. case Property::PX:
  154. // A px measurement.
  155. return Math::RoundToInteger(line_height_property->value.Get< float >());
  156. case Property::DP:
  157. // A density-independent pixel measurement.
  158. return Math::RoundToInteger(line_height_property->value.Get< float >() * ElementUtilities::GetDensityIndependentPixelRatio(element));
  159. case Property::INCH:
  160. // Values based on pixels-per-inch.
  161. return Math::RoundToInteger(line_height_property->value.Get< float >() * inch);
  162. case Property::CM:
  163. return Math::RoundToInteger(line_height_property->value.Get< float >() * inch * (1.0f / 2.54f));
  164. case Property::MM:
  165. return Math::RoundToInteger(line_height_property->value.Get< float >() * inch * (1.0f / 25.4f));
  166. case Property::PT:
  167. return Math::RoundToInteger(line_height_property->value.Get< float >() * inch * (1.0f / 72.0f));
  168. case Property::PC:
  169. return Math::RoundToInteger(line_height_property->value.Get< float >() * inch * (1.0f / 6.0f));
  170. }
  171. return 0;
  172. }
  173. // Returns the width of a string rendered within the context of the given element.
  174. int ElementUtilities::GetStringWidth(Element* element, const WString& string)
  175. {
  176. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  177. if (font_face_handle == NULL)
  178. return 0;
  179. return font_face_handle->GetStringWidth(string);
  180. }
  181. void ElementUtilities::BindEventAttributes(Element* element)
  182. {
  183. int index = 0;
  184. String name;
  185. String value;
  186. // Check for and instance the on* events
  187. while(element->IterateAttributes(index, name, value))
  188. {
  189. if (name.Substring(0, 2) == "on")
  190. {
  191. EventListener* listener = Factory::InstanceEventListener(value, element);
  192. if (listener)
  193. element->AddEventListener(&name[2], listener, false);
  194. }
  195. }
  196. }
  197. // Generates the clipping region for an element.
  198. bool ElementUtilities::GetClippingRegion(Vector2i& clip_origin, Vector2i& clip_dimensions, Element* element)
  199. {
  200. clip_origin = Vector2i(-1, -1);
  201. clip_dimensions = Vector2i(-1, -1);
  202. int num_ignored_clips = element->GetClippingIgnoreDepth();
  203. if (num_ignored_clips < 0)
  204. return false;
  205. // Search through the element's ancestors, finding all elements that clip their overflow and have overflow to clip.
  206. // For each that we find, we combine their clipping region with the existing clipping region, and so build up a
  207. // complete clipping region for the element.
  208. Element* clipping_element = element->GetParentNode();
  209. while (clipping_element != NULL)
  210. {
  211. // Merge the existing clip region with the current clip region if we aren't ignoring clip regions.
  212. if (num_ignored_clips == 0 && clipping_element->IsClippingEnabled())
  213. {
  214. // Ignore nodes that don't clip.
  215. if (clipping_element->GetClientWidth() < clipping_element->GetScrollWidth()
  216. || clipping_element->GetClientHeight() < clipping_element->GetScrollHeight())
  217. {
  218. Vector2f element_origin_f = clipping_element->GetAbsoluteOffset(Box::CONTENT);
  219. Vector2f element_dimensions_f = clipping_element->GetBox().GetSize(Box::CONTENT);
  220. Vector2i element_origin(Math::RealToInteger(element_origin_f.x), Math::RealToInteger(element_origin_f.y));
  221. Vector2i element_dimensions(Math::RealToInteger(element_dimensions_f.x), Math::RealToInteger(element_dimensions_f.y));
  222. if (clip_origin == Vector2i(-1, -1) && clip_dimensions == Vector2i(-1, -1))
  223. {
  224. clip_origin = element_origin;
  225. clip_dimensions = element_dimensions;
  226. }
  227. else
  228. {
  229. Vector2i top_left(Math::Max(clip_origin.x, element_origin.x),
  230. Math::Max(clip_origin.y, element_origin.y));
  231. Vector2i bottom_right(Math::Min(clip_origin.x + clip_dimensions.x, element_origin.x + element_dimensions.x),
  232. Math::Min(clip_origin.y + clip_dimensions.y, element_origin.y + element_dimensions.y));
  233. clip_origin = top_left;
  234. clip_dimensions.x = Math::Max(0, bottom_right.x - top_left.x);
  235. clip_dimensions.y = Math::Max(0, bottom_right.y - top_left.y);
  236. }
  237. }
  238. }
  239. // If this region is meant to clip and we're skipping regions, update the counter.
  240. if (num_ignored_clips > 0)
  241. {
  242. if (clipping_element->IsClippingEnabled())
  243. num_ignored_clips--;
  244. }
  245. // Determine how many clip regions this ancestor ignores, and inherit the value. If this region ignores all
  246. // clipping regions, then we do too.
  247. int clipping_element_ignore_clips = clipping_element->GetClippingIgnoreDepth();
  248. if (clipping_element_ignore_clips < 0)
  249. break;
  250. num_ignored_clips = Math::Max(num_ignored_clips, clipping_element_ignore_clips);
  251. // Climb the tree to this region's parent.
  252. clipping_element = clipping_element->GetParentNode();
  253. }
  254. return clip_dimensions.x >= 0 && clip_dimensions.y >= 0;
  255. }
  256. // Sets the clipping region from an element and its ancestors.
  257. bool ElementUtilities::SetClippingRegion(Element* element, Context* context)
  258. {
  259. Rocket::Core::RenderInterface* render_interface = NULL;
  260. if (element)
  261. {
  262. render_interface = element->GetRenderInterface();
  263. if (!context)
  264. context = element->GetContext();
  265. }
  266. else if (context)
  267. {
  268. render_interface = context->GetRenderInterface();
  269. if (!render_interface)
  270. render_interface = GetRenderInterface();
  271. }
  272. if (!render_interface || !context)
  273. return false;
  274. Vector2i clip_origin, clip_dimensions;
  275. bool clip = element && GetClippingRegion(clip_origin, clip_dimensions, element);
  276. Vector2i current_origin;
  277. Vector2i current_dimensions;
  278. bool current_clip = context->GetActiveClipRegion(current_origin, current_dimensions);
  279. if (current_clip != clip || (clip && (clip_origin != current_origin || clip_dimensions != current_dimensions)))
  280. {
  281. context->SetActiveClipRegion(clip_origin, clip_dimensions);
  282. ApplyActiveClipRegion(context, render_interface);
  283. }
  284. return true;
  285. }
  286. void ElementUtilities::ApplyActiveClipRegion(Context* context, RenderInterface* render_interface)
  287. {
  288. if (render_interface == NULL)
  289. return;
  290. Vector2i origin;
  291. Vector2i dimensions;
  292. bool clip_enabled = context->GetActiveClipRegion(origin, dimensions);
  293. render_interface->EnableScissorRegion(clip_enabled);
  294. if (clip_enabled)
  295. {
  296. render_interface->SetScissorRegion(origin.x, origin.y, dimensions.x, dimensions.y);
  297. }
  298. }
  299. // Formats the contents of an element.
  300. bool ElementUtilities::FormatElement(Element* element, const Vector2f& containing_block)
  301. {
  302. LayoutEngine layout_engine;
  303. return layout_engine.FormatElement(element, containing_block);
  304. }
  305. // Generates the box for an element.
  306. void ElementUtilities::BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element)
  307. {
  308. LayoutEngine::BuildBox(box, containing_block, element, inline_element);
  309. }
  310. // Sizes and positions an element within its parent.
  311. bool ElementUtilities::PositionElement(Element* element, const Vector2f& offset)
  312. {
  313. Element* parent = element->GetParentNode();
  314. if (parent == NULL)
  315. return false;
  316. SetBox(element);
  317. SetElementOffset(element, offset);
  318. return true;
  319. }
  320. // Sizes an element, and positions it within its parent offset from the borders of its content area.
  321. bool ElementUtilities::PositionElement(Element* element, const Vector2f& offset, PositionAnchor anchor)
  322. {
  323. Element* parent = element->GetParentNode();
  324. if (parent == NULL)
  325. return false;
  326. SetBox(element);
  327. Vector2f containing_block = element->GetParentNode()->GetBox().GetSize(Box::CONTENT);
  328. Vector2f element_block = element->GetBox().GetSize(Box::MARGIN);
  329. Vector2f resolved_offset = offset;
  330. if (anchor & RIGHT)
  331. resolved_offset.x = containing_block.x - (element_block.x + offset.x);
  332. if (anchor & BOTTOM)
  333. resolved_offset.y = containing_block.y - (element_block.y + offset.y);
  334. SetElementOffset(element, resolved_offset);
  335. return true;
  336. }
  337. // Builds and sets the box for an element.
  338. static void SetBox(Element* element)
  339. {
  340. Element* parent = element->GetParentNode();
  341. ROCKET_ASSERT(parent != NULL);
  342. Vector2f containing_block = parent->GetBox().GetSize();
  343. containing_block.x -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  344. containing_block.y -= parent->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  345. Box box;
  346. LayoutEngine::BuildBox(box, containing_block, element);
  347. const Property *local_height;
  348. element->GetLocalDimensionProperties(NULL, &local_height);
  349. if (local_height == NULL)
  350. box.SetContent(Vector2f(box.GetSize().x, containing_block.y));
  351. element->SetBox(box);
  352. }
  353. // Positions an element relative to an offset parent.
  354. static void SetElementOffset(Element* element, const Vector2f& offset)
  355. {
  356. Vector2f relative_offset = element->GetParentNode()->GetBox().GetPosition(Box::CONTENT);
  357. relative_offset += offset;
  358. relative_offset.x += element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  359. relative_offset.y += element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  360. element->SetOffset(relative_offset, element->GetParentNode());
  361. }
  362. // Applies an element's `perspective' and `transform' properties.
  363. bool ElementUtilities::ApplyTransform(Element &element, bool apply)
  364. {
  365. Context *context = element.GetContext();
  366. if (!context)
  367. {
  368. return false;
  369. }
  370. RenderInterface *render_interface = element.GetRenderInterface();
  371. if (!render_interface)
  372. {
  373. return false;
  374. }
  375. const TransformState *local_perspective, *perspective, *transform;
  376. element.GetEffectiveTransformState(&local_perspective, &perspective, &transform);
  377. bool have_perspective = false;
  378. float perspective_distance;
  379. Matrix4f the_projection;
  380. if (local_perspective)
  381. {
  382. TransformState::LocalPerspective the_local_perspective;
  383. local_perspective->GetLocalPerspective(&the_local_perspective);
  384. have_perspective = true;
  385. perspective_distance = the_local_perspective.distance;
  386. the_projection = the_local_perspective.GetProjection();
  387. }
  388. else if (perspective)
  389. {
  390. TransformState::Perspective the_perspective;
  391. perspective->GetPerspective(&the_perspective);
  392. have_perspective = true;
  393. perspective_distance = the_perspective.distance;
  394. the_projection = the_perspective.GetProjection();
  395. }
  396. bool have_transform = false;
  397. Matrix4f the_transform;
  398. if (transform)
  399. {
  400. transform->GetRecursiveTransform(&the_transform);
  401. have_transform = true;
  402. }
  403. if (have_perspective && perspective_distance >= 0)
  404. {
  405. // If we are to apply a custom projection, then we need to cancel the global one first.
  406. Matrix4f global_pv_inv;
  407. bool have_global_pv_inv = context->GetViewState().GetProjectionViewInv(global_pv_inv);
  408. if (have_global_pv_inv && have_transform)
  409. {
  410. if (apply)
  411. {
  412. render_interface->PushTransform(global_pv_inv * the_projection * the_transform);
  413. }
  414. else
  415. {
  416. render_interface->PopTransform(global_pv_inv * the_projection * the_transform);
  417. }
  418. return true;
  419. }
  420. else if (have_global_pv_inv)
  421. {
  422. if (apply)
  423. {
  424. render_interface->PushTransform(global_pv_inv * the_projection);
  425. }
  426. else
  427. {
  428. render_interface->PopTransform(global_pv_inv * the_projection);
  429. }
  430. return true;
  431. }
  432. else if (have_transform)
  433. {
  434. // The context has not received Process(Projection|View)Change() calls.
  435. // Assume we don't really need to cancel.
  436. if (apply)
  437. {
  438. render_interface->PushTransform(the_transform);
  439. }
  440. else
  441. {
  442. render_interface->PopTransform(the_transform);
  443. }
  444. return true;
  445. }
  446. else
  447. {
  448. return false;
  449. }
  450. }
  451. else
  452. {
  453. if (have_transform)
  454. {
  455. if (apply)
  456. {
  457. render_interface->PushTransform(the_transform);
  458. }
  459. else
  460. {
  461. render_interface->PopTransform(the_transform);
  462. }
  463. return true;
  464. }
  465. else
  466. {
  467. return false;
  468. }
  469. }
  470. }
  471. // Unapplies an element's `perspective' and `transform' properties.
  472. bool ElementUtilities::UnapplyTransform(Element &element)
  473. {
  474. return ApplyTransform(element, false);
  475. }
  476. }
  477. }