2014-04-18 16:00:53 -07:00
|
|
|
|
2014-04-18 09:06:42 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-04-18 09:16:46 -07:00
|
|
|
#include <stdbool.h>
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-04-18 16:00:53 -07:00
|
|
|
#include "Layout.h"
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static bool eq(float a, float b) {
|
|
|
|
return fabs(a - b) < 0.0001;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_css_node(css_node_t *node) {
|
2014-04-18 09:06:42 -07:00
|
|
|
node->style.align_items = CSS_ALIGN_FLEX_START;
|
|
|
|
|
|
|
|
// Some of the fields default to undefined and not 0
|
|
|
|
node->style.dimensions[CSS_WIDTH] = CSS_UNDEFINED;
|
|
|
|
node->style.dimensions[CSS_HEIGHT] = CSS_UNDEFINED;
|
|
|
|
|
|
|
|
node->style.position[CSS_LEFT] = CSS_UNDEFINED;
|
|
|
|
node->style.position[CSS_TOP] = CSS_UNDEFINED;
|
|
|
|
node->style.position[CSS_RIGHT] = CSS_UNDEFINED;
|
|
|
|
node->style.position[CSS_BOTTOM] = CSS_UNDEFINED;
|
|
|
|
|
2014-04-18 09:32:13 -07:00
|
|
|
node->layout.dimensions[CSS_WIDTH] = CSS_UNDEFINED;
|
|
|
|
node->layout.dimensions[CSS_HEIGHT] = CSS_UNDEFINED;
|
2014-04-18 14:35:12 -07:00
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-04-18 14:35:12 -07:00
|
|
|
css_node_t *new_css_node() {
|
2014-04-18 16:03:29 -07:00
|
|
|
css_node_t *node = calloc(1, sizeof(*node));
|
2014-04-18 14:35:12 -07:00
|
|
|
init_css_node(node);
|
2014-04-18 09:06:42 -07:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:00:53 -07:00
|
|
|
void init_css_node_children(css_node_t *node, int children_count) {
|
2014-05-16 18:04:24 -07:00
|
|
|
node->children = calloc((unsigned long)children_count, sizeof(css_node_t));
|
2014-04-18 13:17:47 -07:00
|
|
|
for (int i = 0; i < children_count; ++i) {
|
2014-04-18 14:35:12 -07:00
|
|
|
init_css_node(&node->children[i]);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
node->children_count = children_count;
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static void cleanup_css_node(css_node_t *node) {
|
2014-04-18 13:17:47 -07:00
|
|
|
for (int i = 0; i < node->children_count; ++i) {
|
2014-04-18 14:35:12 -07:00
|
|
|
cleanup_css_node(&node->children[i]);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
2014-04-18 13:45:57 -07:00
|
|
|
free(node->children);
|
2014-04-18 14:35:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_css_node(css_node_t *node) {
|
|
|
|
cleanup_css_node(node);
|
2014-04-18 09:06:42 -07:00
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static void indent(int n) {
|
2014-04-18 13:17:47 -07:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2014-04-18 09:06:42 -07:00
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 16:00:53 -07:00
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static void print_number_0(const char *str, float number) {
|
|
|
|
if (!eq(number, 0)) {
|
2014-04-18 09:06:42 -07:00
|
|
|
printf("%s: %g, ", str, number);
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 16:00:53 -07:00
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static void print_number_nan(const char *str, float number) {
|
2014-04-18 13:41:14 -07:00
|
|
|
if (!isnan(number)) {
|
2014-04-18 09:06:42 -07:00
|
|
|
printf("%s: %g, ", str, number);
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 16:00:53 -07:00
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static bool four_equal(float four[4]) {
|
2014-04-19 12:19:02 -07:00
|
|
|
return
|
2014-05-16 18:04:24 -07:00
|
|
|
eq(four[0], four[1]) &&
|
|
|
|
eq(four[0], four[2]) &&
|
|
|
|
eq(four[0], four[3]);
|
2014-04-19 12:19:02 -07:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
|
|
|
|
static void print_css_node_rec(
|
|
|
|
css_node_t *node,
|
|
|
|
css_print_options_t options,
|
|
|
|
int level
|
|
|
|
) {
|
2014-04-18 09:06:42 -07:00
|
|
|
indent(level);
|
|
|
|
printf("{");
|
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (options & CSS_PRINT_LAYOUT) {
|
|
|
|
printf("width: %g, ", node->layout.dimensions[CSS_WIDTH]);
|
|
|
|
printf("height: %g, ", node->layout.dimensions[CSS_HEIGHT]);
|
|
|
|
printf("top: %g, ", node->layout.position[CSS_TOP]);
|
|
|
|
printf("left: %g, ", node->layout.position[CSS_LEFT]);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (options & CSS_PRINT_STYLE) {
|
|
|
|
if (node->style.flex_direction == CSS_FLEX_DIRECTION_ROW) {
|
|
|
|
printf("flexDirection: 'row', ");
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (node->style.justify_content == CSS_JUSTIFY_CENTER) {
|
|
|
|
printf("justifyContent: 'center', ");
|
|
|
|
} else if (node->style.justify_content == CSS_JUSTIFY_FLEX_END) {
|
|
|
|
printf("justifyContent: 'flex-end', ");
|
|
|
|
} else if (node->style.justify_content == CSS_JUSTIFY_SPACE_AROUND) {
|
|
|
|
printf("justifyContent: 'space-around', ");
|
|
|
|
} else if (node->style.justify_content == CSS_JUSTIFY_SPACE_BETWEEN) {
|
|
|
|
printf("justifyContent: 'space-between', ");
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (node->style.align_items == CSS_ALIGN_CENTER) {
|
|
|
|
printf("alignItems: 'center', ");
|
|
|
|
} else if (node->style.align_items == CSS_ALIGN_FLEX_END) {
|
|
|
|
printf("alignItems: 'flex-end', ");
|
|
|
|
} else if (node->style.align_items == CSS_ALIGN_STRETCH) {
|
|
|
|
printf("alignItems: 'stretch', ");
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (node->style.align_self == CSS_ALIGN_FLEX_START) {
|
|
|
|
printf("alignSelf: 'flex-start', ");
|
|
|
|
} else if (node->style.align_self == CSS_ALIGN_CENTER) {
|
|
|
|
printf("alignSelf: 'center', ");
|
|
|
|
} else if (node->style.align_self == CSS_ALIGN_FLEX_END) {
|
|
|
|
printf("alignSelf: 'flex-end', ");
|
|
|
|
} else if (node->style.align_self == CSS_ALIGN_STRETCH) {
|
|
|
|
printf("alignSelf: 'stretch', ");
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (node->style.flex == CSS_FLEX_ONE) {
|
|
|
|
printf("flex: 1, ");
|
|
|
|
}
|
2014-04-22 13:18:05 -07:00
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (four_equal(node->style.margin)) {
|
|
|
|
print_number_0("margin", node->style.margin[CSS_LEFT]);
|
|
|
|
} else {
|
|
|
|
print_number_0("marginLeft", node->style.margin[CSS_LEFT]);
|
|
|
|
print_number_0("marginRight", node->style.margin[CSS_RIGHT]);
|
|
|
|
print_number_0("marginTop", node->style.margin[CSS_TOP]);
|
|
|
|
print_number_0("marginBottom", node->style.margin[CSS_BOTTOM]);
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (four_equal(node->style.padding)) {
|
|
|
|
print_number_0("padding", node->style.margin[CSS_LEFT]);
|
|
|
|
} else {
|
|
|
|
print_number_0("paddingLeft", node->style.padding[CSS_LEFT]);
|
|
|
|
print_number_0("paddingRight", node->style.padding[CSS_RIGHT]);
|
|
|
|
print_number_0("paddingTop", node->style.padding[CSS_TOP]);
|
|
|
|
print_number_0("paddingBottom", node->style.padding[CSS_BOTTOM]);
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
if (four_equal(node->style.border)) {
|
|
|
|
print_number_0("borderWidth", node->style.border[CSS_LEFT]);
|
|
|
|
} else {
|
|
|
|
print_number_0("borderLeftWidth", node->style.border[CSS_LEFT]);
|
|
|
|
print_number_0("borderRightWidth", node->style.border[CSS_RIGHT]);
|
|
|
|
print_number_0("borderTopWidth", node->style.border[CSS_TOP]);
|
|
|
|
print_number_0("borderBottomWidth", node->style.border[CSS_BOTTOM]);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
print_number_nan("width", node->style.dimensions[CSS_WIDTH]);
|
|
|
|
print_number_nan("height", node->style.dimensions[CSS_HEIGHT]);
|
|
|
|
|
|
|
|
print_number_nan("left", node->style.position[CSS_LEFT]);
|
|
|
|
print_number_nan("right", node->style.position[CSS_RIGHT]);
|
|
|
|
print_number_nan("top", node->style.position[CSS_TOP]);
|
|
|
|
print_number_nan("bottom", node->style.position[CSS_BOTTOM]);
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
|
|
|
if (node->children_count > 0) {
|
|
|
|
printf("children: [\n");
|
2014-04-18 13:17:47 -07:00
|
|
|
for (int i = 0; i < node->children_count; ++i) {
|
2014-06-04 10:51:23 -07:00
|
|
|
print_css_node_rec(&node->children[i], options, level + 1);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
indent(level);
|
|
|
|
printf("]},\n");
|
|
|
|
} else {
|
|
|
|
printf("},\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
void print_css_node(css_node_t *node, css_print_options_t options) {
|
|
|
|
print_css_node_rec(node, options, 0);
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_position_t leading[2] = {
|
2014-04-18 09:06:42 -07:00
|
|
|
/* CSS_FLEX_DIRECTION_COLUMN = */ CSS_TOP,
|
|
|
|
/* CSS_FLEX_DIRECTION_ROW = */ CSS_LEFT
|
|
|
|
};
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_position_t trailing[2] = {
|
2014-04-18 09:06:42 -07:00
|
|
|
/* CSS_FLEX_DIRECTION_COLUMN = */ CSS_BOTTOM,
|
|
|
|
/* CSS_FLEX_DIRECTION_ROW = */ CSS_RIGHT
|
|
|
|
};
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_position_t pos[2] = {
|
2014-04-18 09:06:42 -07:00
|
|
|
/* CSS_FLEX_DIRECTION_COLUMN = */ CSS_TOP,
|
|
|
|
/* CSS_FLEX_DIRECTION_ROW = */ CSS_LEFT
|
|
|
|
};
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_dimension_t dim[2] = {
|
2014-04-18 09:06:42 -07:00
|
|
|
/* CSS_FLEX_DIRECTION_COLUMN = */ CSS_HEIGHT,
|
|
|
|
/* CSS_FLEX_DIRECTION_ROW = */ CSS_WIDTH
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static bool isUndefined(float value) {
|
2014-04-18 13:41:14 -07:00
|
|
|
return isnan(value);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getMargin(css_node_t *node, int location) {
|
2014-04-18 09:06:42 -07:00
|
|
|
return node->style.margin[location];
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getPadding(css_node_t *node, int location) {
|
2014-04-22 13:18:05 -07:00
|
|
|
if (node->style.padding[location] >= 0) {
|
|
|
|
return node->style.padding[location];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getBorder(css_node_t *node, int location) {
|
2014-04-22 13:18:05 -07:00
|
|
|
if (node->style.border[location] >= 0) {
|
|
|
|
return node->style.border[location];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getPaddingAndBorder(css_node_t *node, int location) {
|
2014-04-22 13:18:05 -07:00
|
|
|
return getPadding(node, location) + getBorder(node, location);
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getMarginAxis(css_node_t *node, css_flex_direction_t axis) {
|
2014-04-22 13:18:05 -07:00
|
|
|
return getMargin(node, leading[axis]) + getMargin(node, trailing[axis]);
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getPaddingAndBorderAxis(css_node_t *node, css_flex_direction_t axis) {
|
2014-04-22 13:18:05 -07:00
|
|
|
return getPaddingAndBorder(node, leading[axis]) + getPaddingAndBorder(node, trailing[axis]);
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_position_type_t getPositionType(css_node_t *node) {
|
2014-04-22 13:18:05 -07:00
|
|
|
return node->style.position_type;
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_justify_t getJustifyContent(css_node_t *node) {
|
2014-04-18 09:06:42 -07:00
|
|
|
return node->style.justify_content;
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_align_t getAlignItem(css_node_t *node, css_node_t *child) {
|
2014-04-18 09:06:42 -07:00
|
|
|
if (child->style.align_self != CSS_ALIGN_AUTO) {
|
|
|
|
return child->style.align_self;
|
|
|
|
}
|
|
|
|
return node->style.align_items;
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_flex_direction_t getFlexDirection(css_node_t *node) {
|
2014-04-18 09:06:42 -07:00
|
|
|
return node->style.flex_direction;
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static css_flex_t getFlex(css_node_t *node) {
|
2014-04-18 09:06:42 -07:00
|
|
|
return node->style.flex;
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static bool isFlex(css_node_t *node) {
|
2014-04-25 15:46:56 -07:00
|
|
|
return getPositionType(node) == CSS_POSITION_RELATIVE && getFlex(node);
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getDimWithMargin(css_node_t *node, css_flex_direction_t axis) {
|
2014-04-18 09:06:42 -07:00
|
|
|
return node->layout.dimensions[dim[axis]] +
|
|
|
|
getMargin(node, leading[axis]) +
|
|
|
|
getMargin(node, trailing[axis]);
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static bool isDimDefined(css_node_t *node, css_flex_direction_t axis) {
|
2014-04-18 10:11:37 -07:00
|
|
|
return !isUndefined(node->style.dimensions[dim[axis]]);
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static bool isPosDefined(css_node_t *node, css_position_t position) {
|
|
|
|
return !isUndefined(node->style.position[position]);
|
2014-04-22 13:18:05 -07:00
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static bool isMeasureDefined(css_node_t *node) {
|
2014-04-28 13:06:00 -07:00
|
|
|
return node->style.measure;
|
|
|
|
}
|
|
|
|
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getPosition(css_node_t *node, css_position_t position) {
|
|
|
|
float result = node->style.position[position];
|
2014-04-18 09:06:42 -07:00
|
|
|
if (!isUndefined(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// When the user specifically sets a value for width or height
|
2014-05-16 18:04:24 -07:00
|
|
|
static void setDimensionFromStyle(css_node_t *node, css_flex_direction_t axis) {
|
2014-04-25 15:46:56 -07:00
|
|
|
// The parent already computed us a width or height. We just skip it
|
|
|
|
if (!isUndefined(node->layout.dimensions[dim[axis]])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We only run if there's a width or height defined
|
|
|
|
if (!isDimDefined(node, axis)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The dimensions can never be smaller than the padding and border
|
|
|
|
node->layout.dimensions[dim[axis]] = fmaxf(
|
|
|
|
node->style.dimensions[dim[axis]],
|
|
|
|
getPaddingAndBorderAxis(node, axis)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-04-18 09:06:42 -07:00
|
|
|
// If both left and right are defined, then use left. Otherwise return
|
|
|
|
// +left or -right depending on which is defined.
|
2014-05-16 18:04:24 -07:00
|
|
|
static float getRelativePosition(css_node_t *node, css_flex_direction_t axis) {
|
2014-04-18 09:06:42 -07:00
|
|
|
float lead = node->style.position[leading[axis]];
|
|
|
|
if (!isUndefined(lead)) {
|
|
|
|
return lead;
|
|
|
|
}
|
2014-04-19 14:43:51 -07:00
|
|
|
return -getPosition(node, trailing[axis]);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
void layoutNode(css_node_t *node, float parentMaxWidth) {
|
2014-04-18 09:06:42 -07:00
|
|
|
css_flex_direction_t mainAxis = getFlexDirection(node);
|
|
|
|
css_flex_direction_t crossAxis = mainAxis == CSS_FLEX_DIRECTION_ROW ?
|
2014-04-18 10:37:01 -07:00
|
|
|
CSS_FLEX_DIRECTION_COLUMN :
|
|
|
|
CSS_FLEX_DIRECTION_ROW;
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// Handle width and height style attributes
|
|
|
|
setDimensionFromStyle(node, mainAxis);
|
|
|
|
setDimensionFromStyle(node, crossAxis);
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// The position is set by the parent, but we need to complete it with a
|
|
|
|
// delta composed of the margin and left/top/right/bottom
|
|
|
|
node->layout.position[leading[mainAxis]] += getMargin(node, leading[mainAxis]) +
|
|
|
|
getRelativePosition(node, mainAxis);
|
|
|
|
node->layout.position[leading[crossAxis]] += getMargin(node, leading[crossAxis]) +
|
|
|
|
getRelativePosition(node, crossAxis);
|
|
|
|
|
2014-04-28 13:06:00 -07:00
|
|
|
if (isMeasureDefined(node)) {
|
2014-04-28 12:34:04 -07:00
|
|
|
float width = CSS_UNDEFINED;
|
|
|
|
if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
|
|
|
|
width = node->style.dimensions[CSS_WIDTH];
|
2014-06-04 10:51:23 -07:00
|
|
|
} else if (!isUndefined(node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]])) {
|
|
|
|
width = node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]];
|
2014-04-28 12:34:04 -07:00
|
|
|
} else {
|
2014-06-04 10:51:23 -07:00
|
|
|
width = parentMaxWidth -
|
|
|
|
getMarginAxis(node, CSS_FLEX_DIRECTION_ROW);
|
2014-04-28 12:34:04 -07:00
|
|
|
}
|
2014-06-04 10:51:23 -07:00
|
|
|
width -= getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
|
|
|
|
|
|
|
// We only need to give a dimension for the text if we haven't got any
|
|
|
|
// for it computed yet. It can either be from the style attribute or because
|
|
|
|
// the element is flexible.
|
|
|
|
bool isRowUndefined = !isDimDefined(node, CSS_FLEX_DIRECTION_ROW) &&
|
|
|
|
isUndefined(node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]]);
|
|
|
|
bool isColumnUndefined = !isDimDefined(node, CSS_FLEX_DIRECTION_COLUMN) &&
|
|
|
|
isUndefined(node->layout.dimensions[dim[CSS_FLEX_DIRECTION_COLUMN]]);
|
|
|
|
|
|
|
|
// Let's not measure the text if we already know both dimensions
|
|
|
|
if (isRowUndefined || isColumnUndefined) {
|
|
|
|
css_dim_t measure_dim = node->style.measure(
|
|
|
|
node->style.measure_context,
|
|
|
|
width
|
|
|
|
);
|
|
|
|
if (isRowUndefined) {
|
|
|
|
node->layout.dimensions[CSS_WIDTH] = measure_dim.dimensions[CSS_WIDTH] +
|
|
|
|
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
|
|
|
}
|
|
|
|
if (isColumnUndefined) {
|
|
|
|
node->layout.dimensions[CSS_HEIGHT] = measure_dim.dimensions[CSS_HEIGHT] +
|
|
|
|
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_COLUMN);
|
|
|
|
}
|
2014-04-28 12:34:04 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-19 12:39:27 -07:00
|
|
|
// Pre-fill cross axis dimensions when the child is using stretch before
|
|
|
|
// we call the recursive layout pass
|
|
|
|
for (int i = 0; i < node->children_count; ++i) {
|
|
|
|
css_node_t* child = &node->children[i];
|
|
|
|
if (getAlignItem(node, child) == CSS_ALIGN_STRETCH &&
|
|
|
|
getPositionType(child) == CSS_POSITION_RELATIVE &&
|
|
|
|
!isUndefined(node->layout.dimensions[dim[crossAxis]]) &&
|
|
|
|
!isDimDefined(child, crossAxis) &&
|
|
|
|
!isPosDefined(child, leading[crossAxis])) {
|
|
|
|
child->layout.dimensions[dim[crossAxis]] = fmaxf(
|
|
|
|
node->layout.dimensions[dim[crossAxis]] -
|
|
|
|
getPaddingAndBorderAxis(node, crossAxis) -
|
|
|
|
getMarginAxis(child, crossAxis),
|
|
|
|
// You never want to go smaller than padding
|
|
|
|
getPaddingAndBorderAxis(child, crossAxis)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// <Loop A> Layout non flexible children and count children by type
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// mainContentDim is accumulation of the dimensions and margin of all the
|
|
|
|
// non flexible children. This will be used in order to either set the
|
|
|
|
// dimensions of the node if none already exist, or to compute the
|
|
|
|
// remaining space left for the flexible children.
|
2014-04-18 09:06:42 -07:00
|
|
|
float mainContentDim = 0;
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// There are three kind of children, non flexible, flexible and absolute.
|
|
|
|
// We need to know how many there are in order to distribute the space.
|
2014-04-18 13:17:47 -07:00
|
|
|
int flexibleChildrenCount = 0;
|
2014-04-25 15:46:56 -07:00
|
|
|
int nonFlexibleChildrenCount = 0;
|
2014-04-18 13:17:47 -07:00
|
|
|
for (int i = 0; i < node->children_count; ++i) {
|
2014-04-18 14:35:12 -07:00
|
|
|
css_node_t* child = &node->children[i];
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// It only makes sense to consider a child flexible if we have a computed
|
|
|
|
// dimension for the node->
|
|
|
|
if (!isUndefined(node->layout.dimensions[dim[mainAxis]]) && isFlex(child)) {
|
|
|
|
flexibleChildrenCount++;
|
|
|
|
|
|
|
|
// Even if we don't know its exact size yet, we already know the padding,
|
|
|
|
// border and margin. We'll use this partial information to compute the
|
|
|
|
// remaining space.
|
|
|
|
mainContentDim += getPaddingAndBorderAxis(child, mainAxis) +
|
|
|
|
getMarginAxis(child, mainAxis);
|
|
|
|
|
|
|
|
} else {
|
2014-06-04 10:51:23 -07:00
|
|
|
float maxWidth = CSS_UNDEFINED;
|
|
|
|
if (mainAxis == CSS_FLEX_DIRECTION_ROW) {
|
|
|
|
// do nothing
|
|
|
|
} else if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
|
|
|
|
maxWidth = node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]] -
|
|
|
|
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
|
|
|
} else {
|
|
|
|
maxWidth = parentMaxWidth -
|
|
|
|
getMarginAxis(node, CSS_FLEX_DIRECTION_ROW) -
|
|
|
|
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// This is the main recursive call. We layout non flexible children.
|
2014-06-04 10:51:23 -07:00
|
|
|
layoutNode(child, maxWidth);
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// Absolute positioned elements do not take part of the layout, so we
|
|
|
|
// don't use them to compute mainContentDim
|
2014-04-22 13:18:05 -07:00
|
|
|
if (getPositionType(child) == CSS_POSITION_RELATIVE) {
|
2014-04-25 15:46:56 -07:00
|
|
|
nonFlexibleChildrenCount++;
|
|
|
|
// At this point we know the final size and margin of the element.
|
2014-04-22 13:18:05 -07:00
|
|
|
mainContentDim += getDimWithMargin(child, mainAxis);
|
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 13:06:00 -07:00
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// <Loop B> Layout flexible children and allocate empty space
|
|
|
|
|
|
|
|
// In order to position the elements in the main axis, we have two
|
|
|
|
// controls. The space between the beginning and the first element
|
|
|
|
// and the space between each two elements.
|
2014-04-18 09:06:42 -07:00
|
|
|
float leadingMainDim = 0;
|
|
|
|
float betweenMainDim = 0;
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// If the dimensions of the current node is defined by its children, they
|
|
|
|
// are all going to be packed together and we don't need to compute
|
|
|
|
// anything.
|
2014-04-18 10:22:38 -07:00
|
|
|
if (!isUndefined(node->layout.dimensions[dim[mainAxis]])) {
|
2014-06-04 10:51:23 -07:00
|
|
|
// The remaining available space that needs to be allocated
|
2014-04-18 09:06:42 -07:00
|
|
|
float remainingMainDim = node->layout.dimensions[dim[mainAxis]] -
|
2014-04-22 13:18:05 -07:00
|
|
|
getPaddingAndBorderAxis(node, mainAxis) -
|
2014-04-18 09:06:42 -07:00
|
|
|
mainContentDim;
|
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// If there are flexible children in the mix, they are going to fill the
|
|
|
|
// remaining space
|
2014-04-18 09:06:42 -07:00
|
|
|
if (flexibleChildrenCount) {
|
|
|
|
float flexibleMainDim = remainingMainDim / flexibleChildrenCount;
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// The non flexible children can overflow the container, in this case
|
|
|
|
// we should just assume that there is no space available.
|
2014-04-22 13:18:05 -07:00
|
|
|
if (flexibleMainDim < 0) {
|
|
|
|
flexibleMainDim = 0;
|
|
|
|
}
|
2014-04-25 15:46:56 -07:00
|
|
|
// We iterate over the full array and only apply the action on flexible
|
|
|
|
// children. This is faster than actually allocating a new array that
|
|
|
|
// contains only flexible children.
|
2014-04-18 13:17:47 -07:00
|
|
|
for (int i = 0; i < node->children_count; ++i) {
|
2014-04-18 14:35:12 -07:00
|
|
|
css_node_t* child = &node->children[i];
|
2014-04-25 15:46:56 -07:00
|
|
|
if (isFlex(child)) {
|
|
|
|
// At this point we know the final size of the element in the main
|
|
|
|
// dimension
|
|
|
|
child->layout.dimensions[dim[mainAxis]] = flexibleMainDim +
|
|
|
|
getPaddingAndBorderAxis(child, mainAxis);
|
|
|
|
|
2014-06-04 10:51:23 -07:00
|
|
|
float maxWidth = CSS_UNDEFINED;
|
|
|
|
if (mainAxis == CSS_FLEX_DIRECTION_ROW) {
|
|
|
|
// do nothing
|
|
|
|
} else if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
|
|
|
|
maxWidth = node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]] -
|
|
|
|
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
|
|
|
} else {
|
|
|
|
maxWidth = parentMaxWidth -
|
|
|
|
getMarginAxis(node, CSS_FLEX_DIRECTION_ROW) -
|
|
|
|
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// And we recursively call the layout algorithm for this child
|
2014-06-04 10:51:23 -07:00
|
|
|
layoutNode(child, maxWidth);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// We use justifyContent to figure out how to allocate the remaining
|
|
|
|
// space available
|
2014-04-18 09:06:42 -07:00
|
|
|
} else {
|
|
|
|
css_justify_t justifyContent = getJustifyContent(node);
|
|
|
|
if (justifyContent == CSS_JUSTIFY_FLEX_START) {
|
|
|
|
// Do nothing
|
|
|
|
} else if (justifyContent == CSS_JUSTIFY_CENTER) {
|
|
|
|
leadingMainDim = remainingMainDim / 2;
|
2014-04-18 10:37:01 -07:00
|
|
|
} else if (justifyContent == CSS_JUSTIFY_FLEX_END) {
|
|
|
|
leadingMainDim = remainingMainDim;
|
2014-04-18 09:06:42 -07:00
|
|
|
} else if (justifyContent == CSS_JUSTIFY_SPACE_BETWEEN) {
|
2014-06-04 10:51:23 -07:00
|
|
|
remainingMainDim = fmaxf(remainingMainDim, 0);
|
2014-04-25 15:46:56 -07:00
|
|
|
betweenMainDim = remainingMainDim /
|
|
|
|
(flexibleChildrenCount + nonFlexibleChildrenCount - 1);
|
2014-04-18 09:06:42 -07:00
|
|
|
} else if (justifyContent == CSS_JUSTIFY_SPACE_AROUND) {
|
2014-04-25 15:46:56 -07:00
|
|
|
// Space on the edges is half of the space between elements
|
|
|
|
betweenMainDim = remainingMainDim /
|
|
|
|
(flexibleChildrenCount + nonFlexibleChildrenCount);
|
2014-04-18 09:06:42 -07:00
|
|
|
leadingMainDim = betweenMainDim / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// <Loop C> Position elements in the main axis and compute dimensions
|
|
|
|
|
|
|
|
// At this point, all the children have their dimensions set. We need to
|
|
|
|
// find their position. In order to do that, we accumulate data in
|
|
|
|
// variables that are also useful to compute the total dimensions of the
|
|
|
|
// container!
|
2014-04-18 09:06:42 -07:00
|
|
|
float crossDim = 0;
|
2014-04-25 15:46:56 -07:00
|
|
|
float mainDim = leadingMainDim +
|
|
|
|
getPaddingAndBorder(node, leading[mainAxis]);
|
2014-04-18 13:17:47 -07:00
|
|
|
for (int i = 0; i < node->children_count; ++i) {
|
2014-04-18 14:35:12 -07:00
|
|
|
css_node_t* child = &node->children[i];
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
if (getPositionType(child) == CSS_POSITION_ABSOLUTE &&
|
|
|
|
isPosDefined(child, leading[mainAxis])) {
|
|
|
|
// In case the child is position absolute and has left/top being
|
|
|
|
// defined, we override the position to whatever the user said
|
|
|
|
// (and margin/border).
|
2014-04-22 13:18:05 -07:00
|
|
|
child->layout.position[pos[mainAxis]] = getPosition(child, leading[mainAxis]) +
|
|
|
|
getBorder(node, leading[mainAxis]) +
|
|
|
|
getMargin(child, leading[mainAxis]);
|
|
|
|
} else {
|
2014-04-25 15:46:56 -07:00
|
|
|
// If the child is position absolute (without top/left) or relative,
|
|
|
|
// we put it at the current accumulated offset.
|
|
|
|
child->layout.position[pos[mainAxis]] += mainDim;
|
2014-04-22 13:18:05 -07:00
|
|
|
}
|
2014-04-18 09:06:42 -07:00
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// Now that we placed the element, we need to update the variables
|
|
|
|
// We only need to do that for relative elements. Absolute elements
|
|
|
|
// do not take part in that phase.
|
|
|
|
if (getPositionType(child) == CSS_POSITION_RELATIVE) {
|
|
|
|
// The main dimension is the sum of all the elements dimension plus
|
|
|
|
// the spacing.
|
|
|
|
mainDim += betweenMainDim + getDimWithMargin(child, mainAxis);
|
|
|
|
// The cross dimension is the max of the elements dimension since there
|
|
|
|
// can only be one element in that cross dimension.
|
|
|
|
crossDim = fmaxf(crossDim, getDimWithMargin(child, crossAxis));
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-22 17:37:55 -07:00
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
// If the user didn't specify a width or height, and it has not been set
|
|
|
|
// by the container, then we set it via the children.
|
|
|
|
if (isUndefined(node->layout.dimensions[dim[mainAxis]])) {
|
|
|
|
node->layout.dimensions[dim[mainAxis]] = fmaxf(
|
|
|
|
// We're missing the last padding at this point to get the final
|
|
|
|
// dimension
|
|
|
|
mainDim + getPaddingAndBorder(node, trailing[mainAxis]),
|
|
|
|
// We can never assign a width smaller than the padding and borders
|
|
|
|
getPaddingAndBorderAxis(node, mainAxis)
|
|
|
|
);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
2014-04-25 15:46:56 -07:00
|
|
|
|
2014-04-18 09:06:42 -07:00
|
|
|
if (isUndefined(node->layout.dimensions[dim[crossAxis]])) {
|
2014-04-25 15:46:56 -07:00
|
|
|
node->layout.dimensions[dim[crossAxis]] = fmaxf(
|
|
|
|
// For the cross dim, we add both sides at the end because the value
|
|
|
|
// is aggregate via a max function. Intermediate negative values
|
|
|
|
// can mess this computation otherwise
|
|
|
|
crossDim + getPaddingAndBorderAxis(node, crossAxis),
|
|
|
|
getPaddingAndBorderAxis(node, crossAxis)
|
|
|
|
);
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// <Loop D> Position elements in the cross axis
|
|
|
|
|
2014-04-18 13:17:47 -07:00
|
|
|
for (int i = 0; i < node->children_count; ++i) {
|
2014-04-18 14:35:12 -07:00
|
|
|
css_node_t* child = &node->children[i];
|
2014-04-22 13:18:05 -07:00
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
if (getPositionType(child) == CSS_POSITION_ABSOLUTE &&
|
|
|
|
isPosDefined(child, leading[crossAxis])) {
|
|
|
|
// In case the child is absolutely positionned and has a
|
|
|
|
// top/left/bottom/right being set, we override all the previously
|
|
|
|
// computed positions to set it correctly.
|
|
|
|
child->layout.position[pos[crossAxis]] = getPosition(child, leading[crossAxis]) +
|
|
|
|
getBorder(node, leading[crossAxis]) +
|
|
|
|
getMargin(child, leading[crossAxis]);
|
2014-04-22 13:18:05 -07:00
|
|
|
|
2014-04-25 15:46:56 -07:00
|
|
|
} else {
|
2014-04-22 13:18:05 -07:00
|
|
|
float leadingCrossDim = getPaddingAndBorder(node, leading[crossAxis]);
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// For a relative children, we're either using alignItems (parent) or
|
|
|
|
// alignSelf (child) in order to determine the position in the cross axis
|
|
|
|
if (getPositionType(child) == CSS_POSITION_RELATIVE) {
|
|
|
|
css_align_t alignItem = getAlignItem(node, child);
|
|
|
|
if (alignItem == CSS_ALIGN_FLEX_START) {
|
|
|
|
// Do nothing
|
|
|
|
} else if (alignItem == CSS_ALIGN_STRETCH) {
|
|
|
|
// You can only stretch if the dimension has not already been set
|
|
|
|
// previously.
|
|
|
|
if (!isDimDefined(child, crossAxis)) {
|
|
|
|
child->layout.dimensions[dim[crossAxis]] = fmaxf(
|
|
|
|
node->layout.dimensions[dim[crossAxis]] -
|
|
|
|
getPaddingAndBorderAxis(node, crossAxis) -
|
|
|
|
getMarginAxis(child, crossAxis),
|
|
|
|
// You never want to go smaller than padding
|
|
|
|
getPaddingAndBorderAxis(child, crossAxis)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The remaining space between the parent dimensions+padding and child
|
|
|
|
// dimensions+margin.
|
|
|
|
float remainingCrossDim = node->layout.dimensions[dim[crossAxis]] -
|
|
|
|
getPaddingAndBorderAxis(node, crossAxis) -
|
|
|
|
getDimWithMargin(child, crossAxis);
|
|
|
|
|
|
|
|
if (alignItem == CSS_ALIGN_CENTER) {
|
|
|
|
leadingCrossDim += remainingCrossDim / 2;
|
|
|
|
} else { // CSS_ALIGN_FLEX_END
|
|
|
|
leadingCrossDim += remainingCrossDim;
|
|
|
|
}
|
2014-04-22 13:18:05 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-25 15:46:56 -07:00
|
|
|
|
|
|
|
// And we apply the position
|
2014-04-22 13:18:05 -07:00
|
|
|
child->layout.position[pos[crossAxis]] += leadingCrossDim;
|
2014-04-18 09:06:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|