autogen c version of text from js

This commit is contained in:
Christopher Chedeau
2014-04-28 13:06:00 -07:00
parent e9550a6116
commit 0708b5eb75
4 changed files with 43 additions and 18 deletions

View File

@@ -238,21 +238,21 @@ var layoutTestUtils = (function() {
reduceTest: reduceTest, reduceTest: reduceTest,
text: function(text) { text: function(text) {
var body = iframeText.contentDocument.body; var body = iframeText.contentDocument.body;
var fn = function(width) { var fn = function(type, width) {
// Constants for testing purposes between C/JS and other platforms // Constants for testing purposes between C/JS and other platforms
// Comment this block of code if you want to use the browser to // Comment this block of code if you want to use the browser to
// generate proper sizes // generate proper sizes
if (text === 'small') { if (text === 'small') {
if (width === 'grow' || width === 'shrink') { if (type === 'grow' || type === 'shrink') {
return {width: 33, height: 18} return {width: 33, height: 18}
} }
return {width: width, height: 18}; return {width: width, height: 18};
} }
if (text === 'loooooooooong with space') { if (text === 'loooooooooong with space') {
if (width === 'grow') { if (type === 'grow') {
return {width: 171, height: 18}; return {width: 171, height: 18};
} }
if (width === 'shrink') { if (type === 'shrink') {
return {width: 100, height: 36}; return {width: 100, height: 36};
} }
return {width: width, height: width >= 171 ? 18 : 36}; return {width: width, height: width >= 171 ? 18 : 36};

View File

@@ -275,6 +275,10 @@ bool isPosDefined(css_node_t *node, css_position_t pos) {
return !isUndefined(node->style.position[pos]); return !isUndefined(node->style.position[pos]);
} }
bool isMeasureDefined(css_node_t *node) {
return node->style.measure;
}
float getPosition(css_node_t *node, css_position_t pos) { float getPosition(css_node_t *node, css_position_t pos) {
float result = node->style.position[pos]; float result = node->style.position[pos];
if (!isUndefined(result)) { if (!isUndefined(result)) {
@@ -328,7 +332,7 @@ void layoutNode(css_node_t *node) {
node->layout.position[leading[crossAxis]] += getMargin(node, leading[crossAxis]) + node->layout.position[leading[crossAxis]] += getMargin(node, leading[crossAxis]) +
getRelativePosition(node, crossAxis); getRelativePosition(node, crossAxis);
if (node->style.measure) { if (isMeasureDefined(node)) {
float width = CSS_UNDEFINED; float width = CSS_UNDEFINED;
css_measure_type_t type = CSS_MEASURE_VALUE; css_measure_type_t type = CSS_MEASURE_VALUE;
@@ -340,23 +344,22 @@ void layoutNode(css_node_t *node) {
type = CSS_MEASURE_GROW; type = CSS_MEASURE_GROW;
} }
css_dim_t dim = node->style.measure( css_dim_t measure_dim = node->style.measure(
node->style.measure_context, node->style.measure_context,
type, type,
width width
); );
if (!isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) { if (!isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
node->layout.dimensions[CSS_WIDTH] = dim.dimensions[CSS_WIDTH] + node->layout.dimensions[CSS_WIDTH] = measure_dim.dimensions[CSS_WIDTH] +
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW); getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
} }
if (!isDimDefined(node, CSS_FLEX_DIRECTION_COLUMN)) { if (!isDimDefined(node, CSS_FLEX_DIRECTION_COLUMN)) {
node->layout.dimensions[CSS_HEIGHT] = dim.dimensions[CSS_HEIGHT] + node->layout.dimensions[CSS_HEIGHT] = measure_dim.dimensions[CSS_HEIGHT] +
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_COLUMN); getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_COLUMN);
} }
return; return;
} }
// <Loop A> Layout non flexible children and count children by type // <Loop A> Layout non flexible children and count children by type
// mainContentDim is accumulation of the dimensions and margin of all the // mainContentDim is accumulation of the dimensions and margin of all the
@@ -397,6 +400,7 @@ void layoutNode(css_node_t *node) {
} }
} }
// <Loop B> Layout flexible children and allocate empty space // <Loop B> Layout flexible children and allocate empty space
// In order to position the elements in the main axis, we have two // In order to position the elements in the main axis, we have two

View File

@@ -112,6 +112,10 @@ var computeLayout = (function() {
return !isUndefined(node.style[pos]); return !isUndefined(node.style[pos]);
} }
function isMeasureDefined(node) {
return 'measure' in node.style;
}
function getPosition(node, pos) { function getPosition(node, pos) {
if (pos in node.style) { if (pos in node.style) {
return node.style[pos]; return node.style[pos];
@@ -170,6 +174,8 @@ var computeLayout = (function() {
return b; return b;
} }
var CSS_UNDEFINED = undefined;
var CSS_FLEX_DIRECTION_ROW = 'row'; var CSS_FLEX_DIRECTION_ROW = 'row';
var CSS_FLEX_DIRECTION_COLUMN = 'column'; var CSS_FLEX_DIRECTION_COLUMN = 'column';
@@ -187,6 +193,10 @@ var computeLayout = (function() {
var CSS_POSITION_RELATIVE = 'relative'; var CSS_POSITION_RELATIVE = 'relative';
var CSS_POSITION_ABSOLUTE = 'absolute'; var CSS_POSITION_ABSOLUTE = 'absolute';
var CSS_MEASURE_VALUE = 'value';
var CSS_MEASURE_GROW = 'grow';
var CSS_MEASURE_SHRINK = 'shrink';
return function layoutNode(node) { return function layoutNode(node) {
var/*css_flex_direction_t*/ mainAxis = getFlexDirection(node); var/*css_flex_direction_t*/ mainAxis = getFlexDirection(node);
var/*css_flex_direction_t*/ crossAxis = mainAxis === CSS_FLEX_DIRECTION_ROW ? var/*css_flex_direction_t*/ crossAxis = mainAxis === CSS_FLEX_DIRECTION_ROW ?
@@ -204,22 +214,29 @@ var computeLayout = (function() {
node.layout[leading[crossAxis]] += getMargin(node, leading[crossAxis]) + node.layout[leading[crossAxis]] += getMargin(node, leading[crossAxis]) +
getRelativePosition(node, crossAxis); getRelativePosition(node, crossAxis);
if ('measure' in node.style) { if (isMeasureDefined(node)) {
var width; var/*float*/ width = CSS_UNDEFINED;
var/*css_measure_type_t*/ type = CSS_MEASURE_VALUE;
if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) { if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
width = node.style.width; width = node.style.width;
} else if (getPositionType(node) === CSS_POSITION_ABSOLUTE) { } else if (getPositionType(node) == CSS_POSITION_ABSOLUTE) {
width = 'shrink'; type = CSS_MEASURE_SHRINK;
} else { } else {
width = 'grow'; type = CSS_MEASURE_GROW;
} }
var dimensions = node.style.measure(width);
var/*css_dim_t*/ measure_dim = node.style.measure(
/*!node->style.measure_context,*/
type,
width
);
if (!isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) { if (!isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
node.layout.width = dimensions.width + node.layout.width = measure_dim.width +
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW); getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
} }
if (!isDimDefined(node, CSS_FLEX_DIRECTION_COLUMN)) { if (!isDimDefined(node, CSS_FLEX_DIRECTION_COLUMN)) {
node.layout.height = dimensions.height + node.layout.height = measure_dim.height +
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_COLUMN); getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_COLUMN);
} }
return; return;

View File

@@ -15,6 +15,8 @@ textarea {
<script> <script>
document.getElementById('layout_code').value = computeLayout.toString() document.getElementById('layout_code').value = computeLayout.toString()
.replace(/\.children\.length/g, '.children_count') .replace(/\.children\.length/g, '.children_count')
.replace(/\.width/g, '.dimensions[CSS_WIDTH]')
.replace(/\.height/g, '.dimensions[CSS_HEIGHT]')
.replace(/layout\[dim/g, 'layout.dimensions[dim') .replace(/layout\[dim/g, 'layout.dimensions[dim')
.replace(/layout\[pos/g, 'layout.position[pos') .replace(/layout\[pos/g, 'layout.position[pos')
.replace(/layout\[leading/g, 'layout.position[leading') .replace(/layout\[leading/g, 'layout.position[leading')
@@ -24,7 +26,9 @@ document.getElementById('layout_code').value = computeLayout.toString()
.replace(/child\./g, 'child->') .replace(/child\./g, 'child->')
.replace(/var\/\*([^\/]+)\*\//g, '$1') .replace(/var\/\*([^\/]+)\*\//g, '$1')
.replace(/ === /g, ' == ') .replace(/ === /g, ' == ')
.replace(/\n /g, '\n'); .replace(/\n /g, '\n')
.replace(/\/[*]!([^*]+)[*]\//g, '$1')
.split('\n').slice(1).join('\n');
</script> </script>
<h1>Tests</h1> <h1>Tests</h1>