LayoutDetails.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_LAYOUT_LAYOUTDETAILS_H
  29. #define RMLUI_CORE_LAYOUT_LAYOUTDETAILS_H
  30. #include "../../../Include/RmlUi/Core/StyleTypes.h"
  31. #include "../../../Include/RmlUi/Core/Types.h"
  32. namespace Rml {
  33. class Box;
  34. class BlockContainer;
  35. class ContainerBox;
  36. struct FormattingMode;
  37. /**
  38. ComputedAxisSize is an abstraction of an element's computed size properties along a single axis, either horizontally or vertically,
  39. allowing eg. rows and columns alike to use the same algorithms. Here, 'a' means left or top, 'b' means right or bottom.
  40. */
  41. struct ComputedAxisSize {
  42. Style::LengthPercentageAuto size;
  43. Style::LengthPercentage min_size, max_size;
  44. Style::Padding padding_a, padding_b;
  45. Style::Margin margin_a, margin_b;
  46. float border_a, border_b;
  47. Style::BoxSizing box_sizing;
  48. };
  49. enum class BuildBoxMode {
  50. StretchFit, // Sets edges and size if available, auto width fills the available width, auto margins are used for alignment.
  51. FitContent, // Sets edges and size if available, auto width returns -1 to indicate to the caller that fit-content should be applied, auto margins
  52. // should then be re-evaluated once sized.
  53. Unaligned, // Sets edges and size if available, auto width returns -1, auto margins are always resolved to zero.
  54. Inline, // Sets edges, ignores width, height, and auto margins.
  55. };
  56. /**
  57. Layout functions for sizing elements.
  58. Some procedures based on the CSS 2.1 specification, 'Section 10. Visual formatting model details'.
  59. */
  60. class LayoutDetails {
  61. public:
  62. /// Generates the box dimensions for an element.
  63. /// @param[out] box The box to be built.
  64. /// @param[in] containing_block The dimensions of the content area of the block containing the element.
  65. /// @param[in] element The element to build the box for.
  66. /// @param[in] box_mode The mode which determines how the box is built.
  67. static void BuildBox(Box& box, Vector2f containing_block, Element* element, BuildBoxMode box_mode);
  68. // Retrieves the minimum and maximum width from an element's computed values.
  69. static void GetMinMaxWidth(float& min_width, float& max_width, const ComputedValues& computed, const Box& box, float containing_block_width);
  70. // Retrieves the minimum and maximum height from an element's computed values.
  71. static void GetMinMaxHeight(float& min_height, float& max_height, const ComputedValues& computed, const Box& box, float containing_block_height);
  72. // Retrieves the minimum and maximum height, set to the box's content height if it is definite (>= 0), otherwise retrieves the minimum and maximum
  73. // heights from an element's computed values.
  74. static void GetDefiniteMinMaxHeight(float& min_height, float& max_height, const ComputedValues& computed, const Box& box,
  75. float containing_block_height);
  76. /// Builds the box margins for a block box, assumes the size and other edges are already set.
  77. /// @param[in,out] box The box to generate.
  78. /// @param[in] containing_block The size of the containing block.
  79. /// @param[in] element The element the box is being generated for.
  80. static void BuildAutoMarginsForBlockBox(Box& box, Vector2f containing_block, Element* element);
  81. /// Clamps the width and build box margins for a block box, the other edges are already set.
  82. /// @param[in,out] box The box to generate.
  83. /// @param[in] containing_block The size of the containing block.
  84. /// @param[in] element The element the box is being generated for.
  85. static void ClampSizeAndBuildAutoMarginsForBlockWidth(Box& box, Vector2f containing_block, Element* element);
  86. /// Build computed axis size along the horizontal direction (width and friends).
  87. static ComputedAxisSize BuildComputedHorizontalSize(const ComputedValues& computed);
  88. /// Build computed axis size along the vertical direction (height and friends).
  89. static ComputedAxisSize BuildComputedVerticalSize(const ComputedValues& computed);
  90. /// Resolve edge sizes from a computed axis size.
  91. static void GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b,
  92. const ComputedAxisSize& computed_size, float base_value);
  93. static String GetDebugElementName(Element* element);
  94. static bool IsScrollContainer(Style::Overflow overflow_x, Style::Overflow overflow_y)
  95. {
  96. return overflow_x != Style::Overflow::Visible || overflow_y != Style::Overflow::Visible;
  97. }
  98. private:
  99. /// Builds margins of a Box, and resolves any auto width or height for non-inline elements. The height may be left unresolved if it depends on the
  100. /// element's children.
  101. /// @param[in,out] box The box to generate. The padding and borders, in addition to any definite content area, must be set on the box already.
  102. /// Auto width and height are specified by negative content size.
  103. /// @param[in] min_size The element's minimum width and height.
  104. /// @param[in] max_size The element's maximum width and height.
  105. /// @param[in] containing_block The size of the containing block.
  106. /// @param[in] element The element the box is being generated for.
  107. /// @param[in] box_mode The mode which determines how the box is built.
  108. static void BuildBoxSizeAndMargins(Box& box, Vector2f min_size, Vector2f max_size, Vector2f containing_block, Element* element,
  109. BuildBoxMode box_mode);
  110. /// Calculates and returns the content size for replaced elements.
  111. static Vector2f CalculateSizeForReplacedElement(Vector2f specified_content_size, Vector2f min_size, Vector2f max_size, Vector2f intrinsic_size,
  112. float intrinsic_ratio);
  113. /// Builds the block-specific width and horizontal margins of a Box.
  114. /// @param[in,out] box The box to generate. The padding and borders must be set on the box already. The content area is used instead of the width
  115. /// property, and -1 means auto width.
  116. /// @param[in] computed The computed values of the element the box is being generated for.
  117. /// @param[in] min_width The minimum content width of the element.
  118. /// @param[in] max_width The maximum content width of the element.
  119. /// @param[in] containing_block The size of the containing block.
  120. /// @param[in] element The element the box is being generated for.
  121. /// @param[in] box_mode The mode which determines how the box is built.
  122. static void BuildBoxWidth(Box& box, const ComputedValues& computed, float min_width, float max_width, Vector2f containing_block, Element* element,
  123. BuildBoxMode box_mode);
  124. /// Builds the block-specific height and vertical margins of a Box.
  125. /// @param[in,out] box The box to generate. The padding and borders must be set on the box already. The content area is used instead of the height
  126. /// property, and -1 means auto height.
  127. /// @param[in] computed The computed values of the element the box is being generated for.
  128. /// @param[in] min_height The minimum content height of the element.
  129. /// @param[in] max_height The maximum content height of the element.
  130. /// @param[in] containing_block_height The height of the containing block.
  131. static void BuildBoxHeight(Box& box, const ComputedValues& computed, float min_height, float max_height, float containing_block_height);
  132. };
  133. } // namespace Rml
  134. #endif