LayoutEngine.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifndef ROCKETCORELAYOUTENGINE_H
  28. #define ROCKETCORELAYOUTENGINE_H
  29. #include "LayoutBlockBox.h"
  30. namespace Rocket {
  31. namespace Core {
  32. class Box;
  33. /**
  34. @author Robert Curry
  35. */
  36. class LayoutEngine
  37. {
  38. public:
  39. /// Constructs a new layout engine.
  40. LayoutEngine();
  41. ~LayoutEngine();
  42. /// Formats the contents for a root-level element (usually a document, floating or replaced element).
  43. /// @param element[in] The element to lay out.
  44. /// @param containing_block[in] The size of the containing block.
  45. bool FormatElement(Element* element, const Vector2f& containing_block);
  46. /// Generates the box for an element.
  47. /// @param[out] box The box to be built.
  48. /// @param[in] containing_block The dimensions of the content area of the block containing the element.
  49. /// @param[in] element The element to build the box for.
  50. /// @param[in] inline_element True if the element is placed in an inline context, false if not.
  51. static void BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element = false);
  52. /// Generates the box for an element placed in a block box.
  53. /// @param[out] box The box to be built.
  54. /// @param[out] min_height The minimum height of the element's box.
  55. /// @param[out] max_height The maximum height of the element's box.
  56. /// @param[in] containing_box The block box containing the element.
  57. /// @param[in] element The element to build the box for.
  58. /// @param[in] inline_element True if the element is placed in an inline context, false if not.
  59. static void BuildBox(Box& box, float& min_height, float& max_height, LayoutBlockBox* containing_box, Element* element, bool inline_element = false);
  60. /// Clamps the width of an element based from its min-width and max-width properties.
  61. /// @param[in] width The width to clamp.
  62. /// @param[in] element The element to read the properties from.
  63. /// @param[in] containing_block_width The width of the element's containing block.
  64. /// @return The clamped width.
  65. static float ClampWidth(float width, Element* element, float containing_block_width);
  66. /// Clamps the height of an element based from its min-height and max-height properties.
  67. /// @param[in] height The height to clamp.
  68. /// @param[in] element The element to read the properties from.
  69. /// @param[in] containing_block_height The height of the element's containing block.
  70. /// @return The clamped height.
  71. static float ClampHeight(float height, Element* element, float containing_block_height);
  72. /// Rounds a vector of two floating-point values to integral values.
  73. /// @param[inout] value The vector to round.
  74. /// @return The rounded vector.
  75. static Vector2f& Round(Vector2f& value);
  76. /// Rounds a floating-point value to an integral value.
  77. /// @param[in] value The value to round.
  78. /// @return The rounded value.
  79. static float Round(float value);
  80. static void* AllocateLayoutChunk(size_t size);
  81. static void DeallocateLayoutChunk(void* chunk);
  82. private:
  83. /// Positions a single element and its children within this layout.
  84. /// @param[in] element The element to lay out.
  85. bool FormatElement(Element* element);
  86. /// Formats and positions an element as a block element.
  87. /// @param[in] element The block element.
  88. bool FormatElementBlock(Element* element);
  89. /// Formats and positions an element as an inline element.
  90. /// @param[in] element The inline element.
  91. bool FormatElementInline(Element* element);
  92. /// Positions an element as a sized inline element, formatting its internal hierarchy as a block element.
  93. /// @param[in] element The replaced element.
  94. void FormatElementReplaced(Element* element);
  95. /// Executes any special formatting for special elements.
  96. /// @param[in] element The element to parse.
  97. /// @return True if the element was parsed as a special element, false otherwise.
  98. bool FormatElementSpecial(Element* element);
  99. /// Returns the fully-resolved, fixed-width and -height containing block from a block box.
  100. /// @param[in] containing_box The leaf box.
  101. /// @return The dimensions of the content area, using the latest fixed dimensions for width and height in the hierarchy.
  102. static Vector2f GetContainingBlock(const LayoutBlockBox* containing_box);
  103. /// Builds the block-specific width and horizontal margins of a Box.
  104. /// @param[in,out] box The box to generate. The padding and borders must be set on the box already. If the content area is sized, then it will be used instead of the width property.
  105. /// @param[in] element The element the box is being generated for.
  106. /// @param[in] containing_block_width The width of the containing block.
  107. static void BuildBoxWidth(Box& box, Element* element, float containing_block_width);
  108. /// Builds the block-specific height and vertical margins of a Box.
  109. /// @param[in,out] box The box to generate. The padding and borders must be set on the box already. If the content area is sized, then it will be used instead of the height property.
  110. /// @param[in] element The element the box is being generated for.
  111. /// @param[in] containing_block_height The height of the containing block.
  112. static void BuildBoxHeight(Box& box, Element* element, float containing_block_height);
  113. // The root block box, representing the document.
  114. LayoutBlockBox* block_box;
  115. // The open block box containing displaying in a block-context.
  116. LayoutBlockBox* block_context_box;
  117. };
  118. }
  119. }
  120. #endif