Import new C source of truth css-layout

fbshipit-source-id: e866918d6c62fc1cf3a04c269f782b94db9b875a
This commit is contained in:
FBShipIt
2016-07-25 06:31:32 -07:00
committed by Emil Sjolander
parent 93809b69c8
commit fdd8552c4e
118 changed files with 11099 additions and 34574 deletions

View File

@@ -0,0 +1,105 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#ifndef __CSS_LAYOUT_INTERNAL_H
#define __CSS_LAYOUT_INTERNAL_H
#include <stdio.h>
#include <stdlib.h>
#include "CSSLayout.h"
#include "CSSNodeList.h"
CSS_EXTERN_C_BEGIN
typedef struct CSSCachedMeasurement {
float availableWidth;
float availableHeight;
CSSMeasureMode widthMeasureMode;
CSSMeasureMode heightMeasureMode;
float computedWidth;
float computedHeight;
} CSSCachedMeasurement;
// This value was chosen based on empiracle data. Even the most complicated
// layouts should not require more than 16 entries to fit within the cache.
enum {
CSS_MAX_CACHED_RESULT_COUNT = 16
};
typedef struct CSSLayout {
float position[4];
float dimensions[2];
CSSDirection direction;
float flexBasis;
// Instead of recomputing the entire layout every single time, we
// cache some information to break early when nothing changed
int generationCount;
CSSDirection lastParentDirection;
int nextCachedMeasurementsIndex;
CSSCachedMeasurement cachedMeasurements[CSS_MAX_CACHED_RESULT_COUNT];
float measuredDimensions[2];
CSSCachedMeasurement cached_layout;
} CSSLayout;
typedef struct CSSStyle {
CSSDirection direction;
CSSFlexDirection flexDirection;
CSSJustify justifyContent;
CSSAlign alignContent;
CSSAlign alignItems;
CSSAlign alignSelf;
CSSPositionType positionType;
CSSWrapType flexWrap;
CSSOverflow overflow;
float flex;
float margin[6];
float position[4];
/**
* You should skip all the rules that contain negative values for the
* following attributes. For example:
* {padding: 10, paddingLeft: -5}
* should output:
* {left: 10 ...}
* the following two are incorrect:
* {left: -5 ...}
* {left: 0 ...}
*/
float padding[6];
float border[6];
float dimensions[2];
float minDimensions[2];
float maxDimensions[2];
} CSSStyle;
typedef struct CSSNode {
CSSStyle style;
CSSLayout layout;
int lineIndex;
bool shouldUpdate;
bool isTextNode;
CSSNodeRef parent;
CSSNodeListRef children;
bool isDirty;
struct CSSNode* nextChild;
CSSSize (*measure)(void *context, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode);
void (*print)(void *context);
void *context;
} CSSNode;
CSS_EXTERN_C_END
#endif

1939
CSSLayout/CSSLayout.c Normal file

File diff suppressed because it is too large Load Diff

210
CSSLayout/CSSLayout.h Normal file
View File

@@ -0,0 +1,210 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#ifndef __CSS_LAYOUT_H
#define __CSS_LAYOUT_H
#include <math.h>
#ifndef __cplusplus
#include <stdbool.h>
#endif
// Not defined in MSVC++
#ifndef NAN
static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
#define NAN (*(const float *)__nan)
#endif
#define CSSUndefined NAN
#include <CSSLayout/CSSMacros.h>
CSS_EXTERN_C_BEGIN
typedef enum CSSDirection {
CSSDirectionInherit,
CSSDirectionLTR,
CSSDirectionRTL,
} CSSDirection;
typedef enum CSSFlexDirection {
CSSFlexDirectionColumn,
CSSFlexDirectionColumnReverse,
CSSFlexDirectionRow,
CSSFlexDirectionRowReverse,
} CSSFlexDirection;
typedef enum CSSJustify {
CSSJustifyFlexStart,
CSSJustifyCenter,
CSSJustifyFlexEnd,
CSSJustifySpaceBetween,
CSSJustifySpaceAround,
} CSSJustify;
typedef enum CSSOverflow {
CSSOverflowVisible,
CSSOverflowHidden,
} CSSOverflow;
// Note: auto is only a valid value for alignSelf. It is NOT a valid value for
// alignItems.
typedef enum CSSAlign {
CSSAlignAuto,
CSSAlignFlexStart,
CSSAlignCenter,
CSSAlignFlexEnd,
CSSAlignStretch,
} CSSAlign;
typedef enum CSSPositionType {
CSSPositionTypeRelative,
CSSPositionTypeAbsolute,
} CSSPositionType;
typedef enum CSSWrapType {
CSSWrapTypeNoWrap,
CSSWrapTypeWrap,
} CSSWrapType;
// Note: left and top are shared between position[2] and position[4], so
// they have to be before right and bottom.
typedef enum CSSPosition {
CSSPositionLeft,
CSSPositionTop,
CSSPositionRight,
CSSPositionBottom,
CSSPositionStart,
CSSPositionEnd,
CSSPositionCount,
} CSSPosition;
typedef enum CSSMeasureMode {
CSSMeasureModeUndefined,
CSSMeasureModeExactly,
CSSMeasureModeAtMost,
CSSMeasureModeCount,
} CSSMeasureMode;
typedef enum CSSDimension {
CSSDimensionWidth,
CSSDimensionHeight,
} CSSDimension;
typedef enum CSSPrintOptions {
CSSPrintOptionsLayout = 1,
CSSPrintOptionsStyle = 2,
CSSPrintOptionsChildren = 4,
} CSSPrintOptions;
typedef struct CSSSize {
float width;
float height;
} CSSSize;
typedef struct CSSNode * CSSNodeRef;
typedef CSSSize (*CSSMeasureFunc)(void *context, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode);
typedef void (*CSSPrintFunc)(void *context);
// CSSNode
CSSNodeRef CSSNodeNew();
void CSSNodeInit(CSSNodeRef node);
void CSSNodeFree(CSSNodeRef node);
void CSSNodeInsertChild(CSSNodeRef node, CSSNodeRef child, unsigned int index);
void CSSNodeRemoveChild(CSSNodeRef node, CSSNodeRef child);
CSSNodeRef CSSNodeGetChild(CSSNodeRef node, unsigned int index);
unsigned int CSSNodeChildCount(CSSNodeRef node);
void CSSNodeCalculateLayout(
CSSNodeRef node,
float availableWidth,
float availableHeight,
CSSDirection parentDirection);
// Mark a node as dirty. Only valid for nodes with a custom measure function set.
// CSSLayout knows when to mark all other nodes as dirty but because nodes with measure functions
// depends on information not known to CSSLayout they must perform this dirty marking manually.
void CSSNodeMarkDirty(CSSNodeRef node);
void CSSNodePrint(CSSNodeRef node, CSSPrintOptions options);
bool isUndefined(float value);
#define CSS_NODE_PROPERTY(type, name, paramName) \
void CSSNodeSet##name(CSSNodeRef node, type paramName); \
type CSSNodeGet##name(CSSNodeRef node);
#define CSS_NODE_STYLE_PROPERTY(type, name, paramName) \
void CSSNodeStyleSet##name(CSSNodeRef node, type paramName); \
type CSSNodeStyleGet##name(CSSNodeRef node);
#define CSS_NODE_LAYOUT_PROPERTY(type, name) \
type CSSNodeLayoutGet##name(CSSNodeRef node);
CSS_NODE_PROPERTY(void*, Context, context);
CSS_NODE_PROPERTY(CSSMeasureFunc, MeasureFunc, measureFunc);
CSS_NODE_PROPERTY(CSSPrintFunc, PrintFunc, printFunc);
CSS_NODE_PROPERTY(bool, IsTextnode, isTextNode);
CSS_NODE_PROPERTY(bool, ShouldUpdate, shouldUpdate);
CSS_NODE_STYLE_PROPERTY(CSSDirection, Direction, direction);
CSS_NODE_STYLE_PROPERTY(CSSFlexDirection, FlexDirection, flexDirection);
CSS_NODE_STYLE_PROPERTY(CSSJustify, JustifyContent, justifyContent);
CSS_NODE_STYLE_PROPERTY(CSSAlign, AlignContent, alignContent);
CSS_NODE_STYLE_PROPERTY(CSSAlign, AlignItems, alignItems);
CSS_NODE_STYLE_PROPERTY(CSSAlign, AlignSelf, alignSelf);
CSS_NODE_STYLE_PROPERTY(CSSPositionType, PositionType, positionType);
CSS_NODE_STYLE_PROPERTY(CSSWrapType, FlexWrap, flexWrap);
CSS_NODE_STYLE_PROPERTY(CSSOverflow, Overflow, overflow);
CSS_NODE_STYLE_PROPERTY(float, Flex, flex);
CSS_NODE_STYLE_PROPERTY(float, PositionLeft, positionLeft);
CSS_NODE_STYLE_PROPERTY(float, PositionTop, positionTop);
CSS_NODE_STYLE_PROPERTY(float, PositionRight, positionRight);
CSS_NODE_STYLE_PROPERTY(float, PositionBottom, positionBottom);
CSS_NODE_STYLE_PROPERTY(float, MarginLeft, marginLeft);
CSS_NODE_STYLE_PROPERTY(float, MarginTop, marginTop);
CSS_NODE_STYLE_PROPERTY(float, MarginRight, marginRight);
CSS_NODE_STYLE_PROPERTY(float, MarginBottom, marginBottom);
CSS_NODE_STYLE_PROPERTY(float, MarginStart, marginStart);
CSS_NODE_STYLE_PROPERTY(float, MarginEnd, marginEnd);
CSS_NODE_STYLE_PROPERTY(float, PaddingLeft, paddingLeft);
CSS_NODE_STYLE_PROPERTY(float, PaddingTop, paddingTop);
CSS_NODE_STYLE_PROPERTY(float, PaddingRight, paddingRight);
CSS_NODE_STYLE_PROPERTY(float, PaddingBottom, paddingBottom);
CSS_NODE_STYLE_PROPERTY(float, PaddingStart, paddingStart);
CSS_NODE_STYLE_PROPERTY(float, PaddingEnd, paddingEnd);
CSS_NODE_STYLE_PROPERTY(float, BorderLeft, borderLeft);
CSS_NODE_STYLE_PROPERTY(float, BorderTop, borderTop);
CSS_NODE_STYLE_PROPERTY(float, BorderRight, borderRight);
CSS_NODE_STYLE_PROPERTY(float, BorderBottom, borderBottom);
CSS_NODE_STYLE_PROPERTY(float, BorderStart, borderStart);
CSS_NODE_STYLE_PROPERTY(float, BorderEnd, borderEnd);
CSS_NODE_STYLE_PROPERTY(float, Width, width);
CSS_NODE_STYLE_PROPERTY(float, Height, height);
CSS_NODE_STYLE_PROPERTY(float, MinWidth, minWidth);
CSS_NODE_STYLE_PROPERTY(float, MinHeight, minHeight);
CSS_NODE_STYLE_PROPERTY(float, MaxWidth, maxWidth);
CSS_NODE_STYLE_PROPERTY(float, MaxHeight, maxHeight);
CSS_NODE_LAYOUT_PROPERTY(float, Left);
CSS_NODE_LAYOUT_PROPERTY(float, Top);
CSS_NODE_LAYOUT_PROPERTY(float, Right);
CSS_NODE_LAYOUT_PROPERTY(float, Bottom);
CSS_NODE_LAYOUT_PROPERTY(float, Width);
CSS_NODE_LAYOUT_PROPERTY(float, Height);
CSS_EXTERN_C_END
#endif

21
CSSLayout/CSSMacros.h Normal file
View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#ifndef __CSS_MACROS_H
#define __CSS_MACROS_H
#ifdef __cplusplus
# define CSS_EXTERN_C_BEGIN extern "C" {
# define CSS_EXTERN_C_END }
#else
# define CSS_EXTERN_C_BEGIN
# define CSS_EXTERN_C_END
#endif
#endif

86
CSSLayout/CSSNodeList.c Normal file
View File

@@ -0,0 +1,86 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "CSSNodeList.h"
struct CSSNodeList {
int capacity;
int count;
void **items;
};
CSSNodeListRef CSSNodeListNew(unsigned int initialCapacity) {
CSSNodeListRef list = malloc(sizeof(struct CSSNodeList));
assert(list != NULL);
list->capacity = initialCapacity;
list->count = 0;
list->items = malloc(sizeof(void*) * list->capacity);
assert(list->items != NULL);
return list;
}
void CSSNodeListFree(CSSNodeListRef list) {
free(list);
}
unsigned int CSSNodeListCount(CSSNodeListRef list) {
return list->count;
}
void CSSNodeListAdd(CSSNodeListRef list, CSSNodeRef node) {
CSSNodeListInsert(list, node, list->count);
}
void CSSNodeListInsert(CSSNodeListRef list, CSSNodeRef node, unsigned int index) {
if (list->count == list->capacity) {
list->capacity *= 2;
list->items = realloc(list->items, sizeof(void*) * list->capacity);
assert(list->items != NULL);
}
for (unsigned int i = list->count; i > index; i--) {
list->items[i] = list->items[i - 1];
}
list->count++;
list->items[index] = node;
}
CSSNodeRef CSSNodeListRemove(CSSNodeListRef list, unsigned int index) {
CSSNodeRef removed = list->items[index];
list->items[index] = NULL;
for (unsigned int i = index; i < list->count - 1; i++) {
list->items[i] = list->items[i + 1];
list->items[i + 1] = NULL;
}
list->count--;
return removed;
}
CSSNodeRef CSSNodeListDelete(CSSNodeListRef list, CSSNodeRef node) {
for (unsigned int i = 0; i < list->count; i++) {
if (list->items[i] == node) {
return CSSNodeListRemove(list, i);
}
}
return NULL;
}
CSSNodeRef CSSNodeListGet(CSSNodeListRef list, unsigned int index) {
return list->items[index];
}

30
CSSLayout/CSSNodeList.h Normal file
View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#ifndef __CSS_NODE_LIST_H
#define __CSS_NODE_LIST_H
#include <CSSLayout/CSSLayout.h>
CSS_EXTERN_C_BEGIN
typedef struct CSSNodeList * CSSNodeListRef;
CSSNodeListRef CSSNodeListNew(unsigned int initialCapacity);
void CSSNodeListFree(CSSNodeListRef list);
unsigned int CSSNodeListCount(CSSNodeListRef list);
void CSSNodeListAdd(CSSNodeListRef list, CSSNodeRef node);
void CSSNodeListInsert(CSSNodeListRef list, CSSNodeRef node, unsigned int index);
CSSNodeRef CSSNodeListRemove(CSSNodeListRef list, unsigned int index);
CSSNodeRef CSSNodeListDelete(CSSNodeListRef list, CSSNodeRef node);
CSSNodeRef CSSNodeListGet(CSSNodeListRef list, unsigned int index);
CSS_EXTERN_C_END
#endif