Quellcode durchsuchen

Abstract and move some methods from table details to layout details

Michael Ragazzon vor 4 Jahren
Ursprung
Commit
c7cda708c5

+ 22 - 0
Source/Core/LayoutDetails.cpp

@@ -266,6 +266,28 @@ float LayoutDetails::GetShrinkToFitWidth(Element* element, Vector2f containing_b
 	return Math::Min(containing_block.x, block_context_box->GetShrinkToFitWidth());
 }
 
+ComputedAxisSize LayoutDetails::BuildComputedHorizontalSize(const ComputedValues& computed)
+{
+	return ComputedAxisSize{computed.width, computed.min_width, computed.max_width, computed.padding_left, computed.padding_right,
+		computed.margin_left, computed.margin_right, computed.border_left_width, computed.border_right_width, computed.box_sizing};
+}
+
+ComputedAxisSize LayoutDetails::BuildComputedVerticalSize(const ComputedValues& computed)
+{
+	return ComputedAxisSize{computed.height, computed.min_height, computed.max_height, computed.padding_top, computed.padding_bottom,
+		computed.margin_top, computed.margin_bottom, computed.border_top_width, computed.border_bottom_width, computed.box_sizing};
+}
+
+void LayoutDetails::GetEdgeSizes(
+	float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedAxisSize& computed_size, const float base_value)
+{
+	margin_a = ResolveValue(computed_size.margin_a, base_value);
+	margin_b = ResolveValue(computed_size.margin_b, base_value);
+
+	padding_border_a = Math::Max(0.0f, ResolveValue(computed_size.padding_a, base_value)) + Math::Max(0.0f, computed_size.border_a);
+	padding_border_b = Math::Max(0.0f, ResolveValue(computed_size.padding_b, base_value)) + Math::Max(0.0f, computed_size.border_b);
+}
+
 Vector2f LayoutDetails::CalculateSizeForReplacedElement(const Vector2f specified_content_size, const Vector2f min_size, const Vector2f max_size, const Vector2f intrinsic_size, const float intrinsic_ratio)
 {
 	// Start with the element's specified width and height. If any of them are auto, use the element's intrinsic

+ 26 - 3
Source/Core/LayoutDetails.h

@@ -29,16 +29,31 @@
 #ifndef RMLUI_CORE_LAYOUTDETAILS_H
 #define RMLUI_CORE_LAYOUTDETAILS_H
 
-#include "LayoutBlockBox.h"
+#include "../../Include/RmlUi/Core/ComputedValues.h"
+#include "../../Include/RmlUi/Core/Types.h"
 
 namespace Rml {
 
 class Box;
+class LayoutBlockBox;
+
+/**
+    ComputedAxisSize is an abstraction of an element's computed size properties along a single axis, either horizontally or vertically,
+    allowing eg. rows and columns alike to use the same algorithms. Here, 'a' means left or top, 'b' means right or bottom.
+*/
+struct ComputedAxisSize {
+	Style::LengthPercentageAuto size;
+	Style::LengthPercentage min_size, max_size;
+	Style::Padding padding_a, padding_b;
+	Style::Margin margin_a, margin_b;
+	float border_a, border_b;
+	Style::BoxSizing box_sizing;
+};
 
 /**
 	Layout functions for sizing elements.
 	
-	Corresponds to the CSS 2.1 specification, 'Section 10. Visual formatting model details'.
+	Some procedures based on the CSS 2.1 specification, 'Section 10. Visual formatting model details'.
  */
 
 class LayoutDetails
@@ -86,10 +101,18 @@ public:
 	/// @param[in] override_shrink_to_fit_width Provide a fixed shrink-to-fit width instead of formatting the element when its properties allow shrinking.
 	static void BuildBoxSizeAndMargins(Box& box, Vector2f min_size, Vector2f max_size, Vector2f containing_block, Element* element, bool inline_element, bool replaced_element, float override_shrink_to_fit_width = -1);
 
-private:
 	/// Formats the element and returns the width of its contents.
 	static float GetShrinkToFitWidth(Element* element, Vector2f containing_block);
 
+	/// Build computed axis size along the horizontal direction (width and friends).
+	static ComputedAxisSize BuildComputedHorizontalSize(const ComputedValues& computed);
+	/// Build computed axis size along the vertical direction (height and friends).
+	static ComputedAxisSize BuildComputedVerticalSize(const ComputedValues& computed);
+	/// Resolve edge sizes from a computed axis size.
+	static void GetEdgeSizes(
+		float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedAxisSize& computed_size, float base_value);
+
+private:
 	/// Calculates and returns the content size for replaced elements.
 	static Vector2f CalculateSizeForReplacedElement(Vector2f specified_content_size, Vector2f min_size, Vector2f max_size, Vector2f intrinsic_size, float intrinsic_ratio);
 

+ 5 - 5
Source/Core/LayoutTable.cpp

@@ -123,7 +123,7 @@ void LayoutTable::DetermineColumnWidths()
 	{
 		if (Element* element_group = grid.columns[i].element_group)
 		{
-			const ComputedTrackSize computed = BuildComputedColumnSize(element_group->GetComputedValues());
+			const ComputedAxisSize computed = LayoutDetails::BuildComputedHorizontalSize(element_group->GetComputedValues());
 			const int span = grid.columns[i].group_span;
 
 			sizing.ApplyGroupElement(i, span, computed);
@@ -131,7 +131,7 @@ void LayoutTable::DetermineColumnWidths()
 
 		if (Element* element_column = grid.columns[i].element_column)
 		{
-			const ComputedTrackSize computed = BuildComputedColumnSize(element_column->GetComputedValues());
+			const ComputedAxisSize computed = LayoutDetails::BuildComputedHorizontalSize(element_column->GetComputedValues());
 			const int span = grid.columns[i].column_span;
 
 			sizing.ApplyTrackElement(i, span, computed);
@@ -143,7 +143,7 @@ void LayoutTable::DetermineColumnWidths()
 	{
 		if (Element* element_cell = grid.columns[i].element_cell)
 		{
-			const ComputedTrackSize computed = BuildComputedColumnSize(element_cell->GetComputedValues());
+			const ComputedAxisSize computed = LayoutDetails::BuildComputedHorizontalSize(element_cell->GetComputedValues());
 			const int colspan = grid.columns[i].cell_span;
 
 			sizing.ApplyCellElement(i, colspan, computed);
@@ -223,7 +223,7 @@ void LayoutTable::DetermineRowHeights()
 		if (Element* element_group = grid.rows[i].element_group)
 		{
 			// The padding/border/margin of column groups are used, but their widths are ignored.
-			const ComputedTrackSize computed = BuildComputedRowSize(element_group->GetComputedValues());
+			const ComputedAxisSize computed = LayoutDetails::BuildComputedVerticalSize(element_group->GetComputedValues());
 			const int span = grid.rows[i].group_span;
 
 			sizing.ApplyGroupElement(i, span, computed);
@@ -232,7 +232,7 @@ void LayoutTable::DetermineRowHeights()
 		if (Element* element_row = grid.rows[i].element_row)
 		{
 			// The padding/border/margin and widths of columns are used.
-			const ComputedTrackSize computed = BuildComputedRowSize(element_row->GetComputedValues());
+			const ComputedAxisSize computed = LayoutDetails::BuildComputedVerticalSize(element_row->GetComputedValues());
 			
 			if (computed.size.type == Style::LengthPercentageAuto::Percentage)
 				percentage_size_used = true;

+ 7 - 36
Source/Core/LayoutTableDetails.cpp

@@ -27,6 +27,7 @@
  */
 
 #include "LayoutTableDetails.h"
+#include "LayoutDetails.h"
 #include "../../Include/RmlUi/Core/Element.h"
 #include <algorithm>
 #include <float.h>
@@ -296,42 +297,12 @@ void TableGrid::PushRow(Element* element_row, ElementList cell_elements)
 	open_cells.erase(open_cells.begin(), it_cells_in_row_end);
 }
 
-
-ComputedTrackSize BuildComputedColumnSize(const ComputedValues& computed)
-{
-	return ComputedTrackSize{
-		computed.width,
-		computed.min_width, computed.max_width,
-		computed.padding_left, computed.padding_right,
-		computed.margin_left, computed.margin_right,
-		computed.border_left_width, computed.border_right_width,
-		computed.box_sizing
-	};
-}
-
-
-ComputedTrackSize BuildComputedRowSize(const ComputedValues& computed)
+void TracksSizing::GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedAxisSize& computed) const
 {
-	return ComputedTrackSize{
-		computed.height,
-		computed.min_height, computed.max_height,
-		computed.padding_top, computed.padding_bottom,
-		computed.margin_top, computed.margin_bottom,
-		computed.border_top_width, computed.border_bottom_width,
-		computed.box_sizing
-	};
-}
-
-void TracksSizing::GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedTrackSize& computed) const
-{
-	margin_a = ResolveValue(computed.margin_a, table_initial_content_size);
-	margin_b = ResolveValue(computed.margin_b, table_initial_content_size);
-
-	padding_border_a = Math::Max(0.0f, ResolveValue(computed.padding_a, table_initial_content_size)) + Math::Max(0.0f, computed.border_a);
-	padding_border_b = Math::Max(0.0f, ResolveValue(computed.padding_b, table_initial_content_size)) + Math::Max(0.0f, computed.border_b);
+	LayoutDetails::GetEdgeSizes(margin_a, margin_b, padding_border_a, padding_border_b, computed, table_initial_content_size);
 }
 
-void TracksSizing::ApplyGroupElement(const int index, const int span, const ComputedTrackSize& computed)
+void TracksSizing::ApplyGroupElement(const int index, const int span, const ComputedAxisSize& computed)
 {
 	RMLUI_ASSERT(span >= 1 && index + span - 1 < (int)metrics.size());
 
@@ -351,7 +322,7 @@ void TracksSizing::ApplyGroupElement(const int index, const int span, const Comp
 	metric_last.sum_margin_b = margin_b;
 }
 
-void TracksSizing::ApplyTrackElement(const int index, const int span, const ComputedTrackSize& computed)
+void TracksSizing::ApplyTrackElement(const int index, const int span, const ComputedAxisSize& computed)
 {
 	RMLUI_ASSERT(span >= 1 && index + span - 1 < (int)metrics.size());
 
@@ -384,7 +355,7 @@ void TracksSizing::ApplyTrackElement(const int index, const int span, const Comp
 	}
 }
 
-void TracksSizing::ApplyCellElement(const int index, const int span, const ComputedTrackSize& computed)
+void TracksSizing::ApplyCellElement(const int index, const int span, const ComputedAxisSize& computed)
 {
 	//  Merge the metrics of the cell with the existing track: If the existing track
 	//  has auto min-/max-/size, we use the cell's min-/max-/size if it has any.
@@ -429,7 +400,7 @@ void TracksSizing::ApplyCellElement(const int index, const int span, const Compu
 	}
 }
 
-void TracksSizing::InitializeSize(TrackMetric& metric, float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedTrackSize& computed, const int span, const Style::BoxSizing target_box) const
+void TracksSizing::InitializeSize(TrackMetric& metric, float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedAxisSize& computed, const int span, const Style::BoxSizing target_box) const
 {
 	RMLUI_ASSERT(span >= 1);
 

+ 7 - 23
Source/Core/LayoutTableDetails.h

@@ -35,6 +35,8 @@
 
 namespace Rml {
 
+struct ComputedAxisSize;
+
 /*
 	TableGrid builds the structure of the table, that is a list of rows, columns, and cells, taking
 	spanning attributes into account to position cells.
@@ -84,24 +86,6 @@ private:
 	CellList open_cells;
 };
 
-
-/*
-	ComputedTrackSize is an abstraction for computed size properties of rows and column elements alike which
-	allows them to use the same algorithms. Here, 'a' means left or top, 'b' means right or bottom.
-*/
-struct ComputedTrackSize {
-	Style::LengthPercentageAuto size;
-	Style::LengthPercentage min_size, max_size;
-	Style::Padding padding_a, padding_b;
-	Style::Margin margin_a, margin_b;
-	float border_a, border_b;
-	Style::BoxSizing box_sizing;
-};
-
-ComputedTrackSize BuildComputedColumnSize(const ComputedValues& computed);
-ComputedTrackSize BuildComputedRowSize(const ComputedValues& computed);
-
-
 enum class TrackSizingMode { Auto, Fixed, Flexible };
 
 /*
@@ -136,21 +120,21 @@ public:
 	{}
 
 	// Apply group element. This sets the initial size of edges.
-	void ApplyGroupElement(const int index, const int span, const ComputedTrackSize& computed);
+	void ApplyGroupElement(const int index, const int span, const ComputedAxisSize& computed);
 	// Apply track element. This merges its edges, and sets the initial content size.
-	void ApplyTrackElement(const int index, const int span, const ComputedTrackSize& computed);
+	void ApplyTrackElement(const int index, const int span, const ComputedAxisSize& computed);
 	// Apply cell element for column sizing. This merges its content size and margins.
-	void ApplyCellElement(const int index, const int span, const ComputedTrackSize& computed);
+	void ApplyCellElement(const int index, const int span, const ComputedAxisSize& computed);
 
 	// Convert flexible size to fixed size for all tracks.
 	void ResolveFlexibleSize();
 
 private:
-	void GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedTrackSize& computed) const;
+	void GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedAxisSize& computed) const;
 
 	// Fill the track metric with fixed, flexible and min/max size, based on the element's computed values.
 	void InitializeSize(TrackMetric& metric, float& margin_a, float& margin_b, float& padding_border_a,
-		float& padding_border_b, const ComputedTrackSize& computed, const int span, const Style::BoxSizing target_box) const;
+		float& padding_border_b, const ComputedAxisSize& computed, const int span, const Style::BoxSizing target_box) const;
 
 	TrackMetricList& metrics;
 	const float table_initial_content_size;