2015-04-29 22:28:18 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-04-29 22:28:18 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-04-29 22:28:18 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-05-19 20:43:46 +00:00
|
|
|
'use strict';
|
2015-04-29 22:28:18 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const Color = require('art/core/color');
|
|
|
|
const Path = require('ARTSerializablePath');
|
|
|
|
const Transform = require('art/core/transform');
|
2015-04-29 22:28:18 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const React = require('React');
|
|
|
|
const PropTypes = require('prop-types');
|
|
|
|
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
2015-04-29 22:28:18 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const createReactNativeComponentClass = require('createReactNativeComponentClass');
|
|
|
|
const merge = require('merge');
|
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
// Diff Helpers
|
|
|
|
|
|
|
|
function arrayDiffer(a, b) {
|
2016-04-25 20:27:53 +00:00
|
|
|
if (a == null || b == null) {
|
2015-04-29 22:28:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (a.length !== b.length) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
for (let i = 0; i < a.length; i++) {
|
2015-04-29 22:28:18 +00:00
|
|
|
if (a[i] !== b[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fontAndLinesDiffer(a, b) {
|
|
|
|
if (a === b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (a.font !== b.font) {
|
|
|
|
if (a.font === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (b.font === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
a.font.fontFamily !== b.font.fontFamily ||
|
|
|
|
a.font.fontSize !== b.font.fontSize ||
|
|
|
|
a.font.fontWeight !== b.font.fontWeight ||
|
2015-04-30 20:18:52 +00:00
|
|
|
a.font.fontStyle !== b.font.fontStyle
|
2015-04-29 22:28:18 +00:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arrayDiffer(a.lines, b.lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Native Attributes
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const SurfaceViewAttributes = merge(ReactNativeViewAttributes.UIView, {
|
2015-04-29 22:28:18 +00:00
|
|
|
// This should contain pixel information such as width, height and
|
|
|
|
// resolution to know what kind of buffer needs to be allocated.
|
|
|
|
// Currently we rely on UIViews and style to figure that out.
|
|
|
|
});
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const NodeAttributes = {
|
2018-05-11 02:06:46 +00:00
|
|
|
transform: {diff: arrayDiffer},
|
2015-04-29 22:28:18 +00:00
|
|
|
opacity: true,
|
|
|
|
};
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const GroupAttributes = merge(NodeAttributes, {
|
2018-05-11 02:06:46 +00:00
|
|
|
clipping: {diff: arrayDiffer},
|
2015-04-29 22:28:18 +00:00
|
|
|
});
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const RenderableAttributes = merge(NodeAttributes, {
|
2018-05-11 02:06:46 +00:00
|
|
|
fill: {diff: arrayDiffer},
|
|
|
|
stroke: {diff: arrayDiffer},
|
2015-04-29 22:28:18 +00:00
|
|
|
strokeWidth: true,
|
|
|
|
strokeCap: true,
|
|
|
|
strokeJoin: true,
|
2018-05-11 02:06:46 +00:00
|
|
|
strokeDash: {diff: arrayDiffer},
|
2015-04-29 22:28:18 +00:00
|
|
|
});
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const ShapeAttributes = merge(RenderableAttributes, {
|
2018-05-11 02:06:46 +00:00
|
|
|
d: {diff: arrayDiffer},
|
2015-04-29 22:28:18 +00:00
|
|
|
});
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const TextAttributes = merge(RenderableAttributes, {
|
2015-04-29 22:28:18 +00:00
|
|
|
alignment: true,
|
2018-05-11 02:06:46 +00:00
|
|
|
frame: {diff: fontAndLinesDiffer},
|
|
|
|
path: {diff: arrayDiffer},
|
2015-04-29 22:28:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Native Components
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
const NativeSurfaceView = createReactNativeComponentClass(
|
|
|
|
'ARTSurfaceView',
|
2017-09-15 01:01:25 +00:00
|
|
|
() => ({
|
|
|
|
validAttributes: SurfaceViewAttributes,
|
|
|
|
uiViewClassName: 'ARTSurfaceView',
|
2018-05-11 02:06:46 +00:00
|
|
|
}),
|
|
|
|
);
|
2017-09-15 01:01:25 +00:00
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
const NativeGroup = createReactNativeComponentClass('ARTGroup', () => ({
|
|
|
|
validAttributes: GroupAttributes,
|
|
|
|
uiViewClassName: 'ARTGroup',
|
|
|
|
}));
|
2017-09-15 01:01:25 +00:00
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
const NativeShape = createReactNativeComponentClass('ARTShape', () => ({
|
|
|
|
validAttributes: ShapeAttributes,
|
|
|
|
uiViewClassName: 'ARTShape',
|
|
|
|
}));
|
2017-09-15 01:01:25 +00:00
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
const NativeText = createReactNativeComponentClass('ARTText', () => ({
|
|
|
|
validAttributes: TextAttributes,
|
|
|
|
uiViewClassName: 'ARTText',
|
|
|
|
}));
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
// Utilities
|
|
|
|
|
|
|
|
function childrenAsString(children) {
|
|
|
|
if (!children) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (typeof children === 'string') {
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
if (children.length) {
|
|
|
|
return children.join('\n');
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Surface - Root node of all ART
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class Surface extends React.Component {
|
2016-12-08 01:03:37 +00:00
|
|
|
static childContextTypes = {
|
2017-04-12 23:09:48 +00:00
|
|
|
isInSurface: PropTypes.bool,
|
2016-12-08 01:03:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
getChildContext() {
|
2018-05-11 02:06:46 +00:00
|
|
|
return {isInSurface: true};
|
2016-12-08 01:03:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2018-07-12 14:58:40 +00:00
|
|
|
const height = extractNumber(this.props.height, 0);
|
|
|
|
const width = extractNumber(this.props.width, 0);
|
|
|
|
|
2015-04-29 22:28:18 +00:00
|
|
|
return (
|
2018-07-20 02:00:38 +00:00
|
|
|
<NativeSurfaceView style={[this.props.style, {height, width}]}>
|
2015-04-29 22:28:18 +00:00
|
|
|
{this.props.children}
|
|
|
|
</NativeSurfaceView>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
// Node Props
|
|
|
|
|
|
|
|
// TODO: The desktop version of ART has title and cursor. We should have
|
|
|
|
// accessibility support here too even though hovering doesn't work.
|
|
|
|
|
|
|
|
function extractNumber(value, defaultValue) {
|
|
|
|
if (value == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
return +value;
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const pooledTransform = new Transform();
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
function extractTransform(props) {
|
2018-05-11 02:06:46 +00:00
|
|
|
const scaleX =
|
|
|
|
props.scaleX != null ? props.scaleX : props.scale != null ? props.scale : 1;
|
|
|
|
const scaleY =
|
|
|
|
props.scaleY != null ? props.scaleY : props.scale != null ? props.scale : 1;
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
pooledTransform
|
|
|
|
.transformTo(1, 0, 0, 1, 0, 0)
|
|
|
|
.move(props.x || 0, props.y || 0)
|
|
|
|
.rotate(props.rotation || 0, props.originX, props.originY)
|
|
|
|
.scale(scaleX, scaleY, props.originX, props.originY);
|
|
|
|
|
|
|
|
if (props.transform != null) {
|
|
|
|
pooledTransform.transform(props.transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2018-05-11 02:06:46 +00:00
|
|
|
pooledTransform.xx,
|
|
|
|
pooledTransform.yx,
|
|
|
|
pooledTransform.xy,
|
|
|
|
pooledTransform.yy,
|
|
|
|
pooledTransform.x,
|
|
|
|
pooledTransform.y,
|
2015-04-29 22:28:18 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractOpacity(props) {
|
|
|
|
// TODO: visible === false should also have no hit detection
|
|
|
|
if (props.visible === false) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (props.opacity == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return +props.opacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Groups
|
|
|
|
|
|
|
|
// Note: ART has a notion of width and height on Group but AFAIK it's a noop in
|
|
|
|
// ReactART.
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class Group extends React.Component {
|
2016-12-08 01:03:37 +00:00
|
|
|
static contextTypes = {
|
2017-04-12 23:09:48 +00:00
|
|
|
isInSurface: PropTypes.bool.isRequired,
|
2016-12-08 01:03:37 +00:00
|
|
|
};
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2018-05-10 22:44:52 +00:00
|
|
|
const props = this.props;
|
2016-12-08 01:03:37 +00:00
|
|
|
invariant(
|
|
|
|
this.context.isInSurface,
|
2018-05-11 02:06:46 +00:00
|
|
|
'ART: <Group /> must be a child of a <Surface />',
|
2016-12-08 01:03:37 +00:00
|
|
|
);
|
2015-04-29 22:28:18 +00:00
|
|
|
return (
|
|
|
|
<NativeGroup
|
|
|
|
opacity={extractOpacity(props)}
|
|
|
|
transform={extractTransform(props)}>
|
|
|
|
{this.props.children}
|
|
|
|
</NativeGroup>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-04-29 22:28:18 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class ClippingRectangle extends React.Component {
|
|
|
|
render() {
|
2018-05-10 22:44:52 +00:00
|
|
|
const props = this.props;
|
|
|
|
const x = extractNumber(props.x, 0);
|
|
|
|
const y = extractNumber(props.y, 0);
|
|
|
|
const w = extractNumber(props.width, 0);
|
|
|
|
const h = extractNumber(props.height, 0);
|
|
|
|
const clipping = [x, y, w, h];
|
2015-04-29 22:28:18 +00:00
|
|
|
// The current clipping API requires x and y to be ignored in the transform
|
2018-05-10 22:44:52 +00:00
|
|
|
const propsExcludingXAndY = merge(props);
|
2015-04-29 22:28:18 +00:00
|
|
|
delete propsExcludingXAndY.x;
|
|
|
|
delete propsExcludingXAndY.y;
|
|
|
|
return (
|
|
|
|
<NativeGroup
|
|
|
|
clipping={clipping}
|
|
|
|
opacity={extractOpacity(props)}
|
|
|
|
transform={extractTransform(propsExcludingXAndY)}>
|
|
|
|
{this.props.children}
|
|
|
|
</NativeGroup>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
// Renderables
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const SOLID_COLOR = 0;
|
|
|
|
const LINEAR_GRADIENT = 1;
|
|
|
|
const RADIAL_GRADIENT = 2;
|
|
|
|
const PATTERN = 3;
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
function insertColorIntoArray(color, targetArray, atIndex) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const c = new Color(color);
|
2015-04-29 22:28:18 +00:00
|
|
|
targetArray[atIndex + 0] = c.red / 255;
|
|
|
|
targetArray[atIndex + 1] = c.green / 255;
|
|
|
|
targetArray[atIndex + 2] = c.blue / 255;
|
|
|
|
targetArray[atIndex + 3] = c.alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertColorsIntoArray(stops, targetArray, atIndex) {
|
2018-05-10 22:44:52 +00:00
|
|
|
let i = 0;
|
2015-04-29 22:28:18 +00:00
|
|
|
if ('length' in stops) {
|
|
|
|
while (i < stops.length) {
|
|
|
|
insertColorIntoArray(stops[i], targetArray, atIndex + i * 4);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-10 22:44:52 +00:00
|
|
|
for (const offset in stops) {
|
2015-04-29 22:28:18 +00:00
|
|
|
insertColorIntoArray(stops[offset], targetArray, atIndex + i * 4);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return atIndex + i * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertOffsetsIntoArray(stops, targetArray, atIndex, multi, reverse) {
|
2018-05-10 22:44:52 +00:00
|
|
|
let offsetNumber;
|
|
|
|
let i = 0;
|
2015-04-29 22:28:18 +00:00
|
|
|
if ('length' in stops) {
|
|
|
|
while (i < stops.length) {
|
2018-06-06 12:20:40 +00:00
|
|
|
offsetNumber = (i / (stops.length - 1)) * multi;
|
2015-04-29 22:28:18 +00:00
|
|
|
targetArray[atIndex + i] = reverse ? 1 - offsetNumber : offsetNumber;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-10 22:44:52 +00:00
|
|
|
for (const offsetString in stops) {
|
2018-05-11 02:06:46 +00:00
|
|
|
offsetNumber = +offsetString * multi;
|
2015-04-29 22:28:18 +00:00
|
|
|
targetArray[atIndex + i] = reverse ? 1 - offsetNumber : offsetNumber;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return atIndex + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertColorStopsIntoArray(stops, targetArray, atIndex) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const lastIndex = insertColorsIntoArray(stops, targetArray, atIndex);
|
2015-04-29 22:28:18 +00:00
|
|
|
insertOffsetsIntoArray(stops, targetArray, lastIndex, 1, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertDoubleColorStopsIntoArray(stops, targetArray, atIndex) {
|
2018-05-10 22:44:52 +00:00
|
|
|
let lastIndex = insertColorsIntoArray(stops, targetArray, atIndex);
|
2015-04-29 22:28:18 +00:00
|
|
|
lastIndex = insertColorsIntoArray(stops, targetArray, lastIndex);
|
|
|
|
lastIndex = insertOffsetsIntoArray(stops, targetArray, lastIndex, 0.5, false);
|
|
|
|
insertOffsetsIntoArray(stops, targetArray, lastIndex, 0.5, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyBoundingBoxToBrushData(brushData, props) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const type = brushData[0];
|
|
|
|
const width = +props.width;
|
|
|
|
const height = +props.height;
|
2015-04-29 22:28:18 +00:00
|
|
|
if (type === LINEAR_GRADIENT) {
|
|
|
|
brushData[1] *= width;
|
|
|
|
brushData[2] *= height;
|
|
|
|
brushData[3] *= width;
|
|
|
|
brushData[4] *= height;
|
|
|
|
} else if (type === RADIAL_GRADIENT) {
|
|
|
|
brushData[1] *= width;
|
|
|
|
brushData[2] *= height;
|
|
|
|
brushData[3] *= width;
|
|
|
|
brushData[4] *= height;
|
|
|
|
brushData[5] *= width;
|
|
|
|
brushData[6] *= height;
|
|
|
|
} else if (type === PATTERN) {
|
|
|
|
// todo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractBrush(colorOrBrush, props) {
|
|
|
|
if (colorOrBrush == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (colorOrBrush._brush) {
|
|
|
|
if (colorOrBrush._bb) {
|
|
|
|
// The legacy API for Gradients allow for the bounding box to be used
|
|
|
|
// as a convenience for specifying gradient positions. This should be
|
|
|
|
// deprecated. It's not properly implemented in canvas mode. ReactART
|
|
|
|
// doesn't handle update to the bounding box correctly. That's why we
|
|
|
|
// mutate this so that if it's reused, we reuse the same resolved box.
|
|
|
|
applyBoundingBoxToBrushData(colorOrBrush._brush, props);
|
|
|
|
colorOrBrush._bb = false;
|
|
|
|
}
|
|
|
|
return colorOrBrush._brush;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const c = new Color(colorOrBrush);
|
2015-04-29 22:28:18 +00:00
|
|
|
return [SOLID_COLOR, c.red / 255, c.green / 255, c.blue / 255, c.alpha];
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractColor(color) {
|
|
|
|
if (color == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const c = new Color(color);
|
2015-04-29 22:28:18 +00:00
|
|
|
return [c.red / 255, c.green / 255, c.blue / 255, c.alpha];
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractStrokeCap(strokeCap) {
|
|
|
|
switch (strokeCap) {
|
2018-05-11 02:06:46 +00:00
|
|
|
case 'butt':
|
|
|
|
return 0;
|
|
|
|
case 'square':
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
return 1; // round
|
2015-04-29 22:28:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractStrokeJoin(strokeJoin) {
|
|
|
|
switch (strokeJoin) {
|
2018-05-11 02:06:46 +00:00
|
|
|
case 'miter':
|
|
|
|
return 0;
|
|
|
|
case 'bevel':
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
return 1; // round
|
2015-04-29 22:28:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shape
|
|
|
|
|
|
|
|
// Note: ART has a notion of width and height on Shape but AFAIK it's a noop in
|
|
|
|
// ReactART.
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class Shape extends React.Component {
|
|
|
|
render() {
|
2018-05-10 22:44:52 +00:00
|
|
|
const props = this.props;
|
|
|
|
const path = props.d || childrenAsString(props.children);
|
|
|
|
const d = (path instanceof Path ? path : new Path(path)).toJSON();
|
2015-04-29 22:28:18 +00:00
|
|
|
return (
|
|
|
|
<NativeShape
|
|
|
|
fill={extractBrush(props.fill, props)}
|
|
|
|
opacity={extractOpacity(props)}
|
|
|
|
stroke={extractColor(props.stroke)}
|
|
|
|
strokeCap={extractStrokeCap(props.strokeCap)}
|
|
|
|
strokeDash={props.strokeDash || null}
|
|
|
|
strokeJoin={extractStrokeJoin(props.strokeJoin)}
|
|
|
|
strokeWidth={extractNumber(props.strokeWidth, 1)}
|
|
|
|
transform={extractTransform(props)}
|
|
|
|
d={d}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
// Text
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const cachedFontObjectsFromString = {};
|
2015-04-29 22:28:18 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const fontFamilyPrefix = /^[\s"']*/;
|
|
|
|
const fontFamilySuffix = /[\s"']*$/;
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
function extractSingleFontFamily(fontFamilyString) {
|
|
|
|
// ART on the web allows for multiple font-families to be specified.
|
|
|
|
// For compatibility, we extract the first font-family, hoping
|
|
|
|
// we'll get a match.
|
2018-05-11 02:06:46 +00:00
|
|
|
return fontFamilyString
|
|
|
|
.split(',')[0]
|
|
|
|
.replace(fontFamilyPrefix, '')
|
|
|
|
.replace(fontFamilySuffix, '');
|
2015-04-29 22:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseFontString(font) {
|
|
|
|
if (cachedFontObjectsFromString.hasOwnProperty(font)) {
|
|
|
|
return cachedFontObjectsFromString[font];
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const regexp = /^\s*((?:(?:normal|bold|italic)\s+)*)(?:(\d+(?:\.\d+)?)[ptexm\%]*(?:\s*\/.*?)?\s+)?\s*\"?([^\"]*)/i;
|
|
|
|
const match = regexp.exec(font);
|
2015-04-29 22:28:18 +00:00
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const fontFamily = extractSingleFontFamily(match[3]);
|
|
|
|
const fontSize = +match[2] || 12;
|
|
|
|
const isBold = /bold/.exec(match[1]);
|
|
|
|
const isItalic = /italic/.exec(match[1]);
|
2015-04-29 22:28:18 +00:00
|
|
|
cachedFontObjectsFromString[font] = {
|
|
|
|
fontFamily: fontFamily,
|
|
|
|
fontSize: fontSize,
|
|
|
|
fontWeight: isBold ? 'bold' : 'normal',
|
|
|
|
fontStyle: isItalic ? 'italic' : 'normal',
|
|
|
|
};
|
|
|
|
return cachedFontObjectsFromString[font];
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractFont(font) {
|
|
|
|
if (font == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (typeof font === 'string') {
|
|
|
|
return parseFontString(font);
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const fontFamily = extractSingleFontFamily(font.fontFamily);
|
|
|
|
const fontSize = +font.fontSize || 12;
|
2018-05-11 02:06:46 +00:00
|
|
|
const fontWeight =
|
|
|
|
font.fontWeight != null ? font.fontWeight.toString() : '400';
|
2015-04-29 22:28:18 +00:00
|
|
|
return {
|
|
|
|
// Normalize
|
|
|
|
fontFamily: fontFamily,
|
|
|
|
fontSize: fontSize,
|
2017-05-25 08:21:45 +00:00
|
|
|
fontWeight: fontWeight,
|
2015-04-29 22:28:18 +00:00
|
|
|
fontStyle: font.fontStyle,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const newLine = /\n/g;
|
2015-04-29 22:28:18 +00:00
|
|
|
function extractFontAndLines(font, text) {
|
2018-05-11 02:06:46 +00:00
|
|
|
return {font: extractFont(font), lines: text.split(newLine)};
|
2015-04-29 22:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function extractAlignment(alignment) {
|
|
|
|
switch (alignment) {
|
|
|
|
case 'right':
|
|
|
|
return 1;
|
|
|
|
case 'center':
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class Text extends React.Component {
|
|
|
|
render() {
|
2018-05-10 22:44:52 +00:00
|
|
|
const props = this.props;
|
|
|
|
const path = props.path;
|
2018-05-11 02:06:46 +00:00
|
|
|
const textPath = path
|
|
|
|
? (path instanceof Path ? path : new Path(path)).toJSON()
|
|
|
|
: null;
|
2018-05-10 22:44:52 +00:00
|
|
|
const textFrame = extractFontAndLines(
|
2015-04-29 22:28:18 +00:00
|
|
|
props.font,
|
2018-05-11 02:06:46 +00:00
|
|
|
childrenAsString(props.children),
|
2015-04-29 22:28:18 +00:00
|
|
|
);
|
|
|
|
return (
|
|
|
|
<NativeText
|
|
|
|
fill={extractBrush(props.fill, props)}
|
|
|
|
opacity={extractOpacity(props)}
|
|
|
|
stroke={extractColor(props.stroke)}
|
|
|
|
strokeCap={extractStrokeCap(props.strokeCap)}
|
|
|
|
strokeDash={props.strokeDash || null}
|
|
|
|
strokeJoin={extractStrokeJoin(props.strokeJoin)}
|
|
|
|
strokeWidth={extractNumber(props.strokeWidth, 1)}
|
|
|
|
transform={extractTransform(props)}
|
|
|
|
alignment={extractAlignment(props.alignment)}
|
|
|
|
frame={textFrame}
|
|
|
|
path={textPath}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
// Declarative fill type objects - API design not finalized
|
|
|
|
|
|
|
|
function LinearGradient(stops, x1, y1, x2, y2) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const type = LINEAR_GRADIENT;
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
if (arguments.length < 5) {
|
2018-06-06 12:20:40 +00:00
|
|
|
const angle = ((x1 == null ? 270 : x1) * Math.PI) / 180;
|
2015-04-29 22:28:18 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
let x = Math.cos(angle);
|
|
|
|
let y = -Math.sin(angle);
|
|
|
|
const l = (Math.abs(x) + Math.abs(y)) / 2;
|
2015-04-29 22:28:18 +00:00
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
x *= l;
|
|
|
|
y *= l;
|
2015-04-29 22:28:18 +00:00
|
|
|
|
|
|
|
x1 = 0.5 - x;
|
|
|
|
x2 = 0.5 + x;
|
|
|
|
y1 = 0.5 - y;
|
|
|
|
y2 = 0.5 + y;
|
|
|
|
this._bb = true;
|
|
|
|
} else {
|
|
|
|
this._bb = false;
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const brushData = [type, +x1, +y1, +x2, +y2];
|
2015-04-29 22:28:18 +00:00
|
|
|
insertColorStopsIntoArray(stops, brushData, 5);
|
|
|
|
this._brush = brushData;
|
|
|
|
}
|
|
|
|
|
|
|
|
function RadialGradient(stops, fx, fy, rx, ry, cx, cy) {
|
|
|
|
if (ry == null) {
|
|
|
|
ry = rx;
|
|
|
|
}
|
|
|
|
if (cx == null) {
|
|
|
|
cx = fx;
|
|
|
|
}
|
|
|
|
if (cy == null) {
|
|
|
|
cy = fy;
|
|
|
|
}
|
|
|
|
if (fx == null) {
|
|
|
|
// As a convenience we allow the whole radial gradient to cover the
|
|
|
|
// bounding box. We should consider dropping this API.
|
|
|
|
fx = fy = rx = ry = cx = cy = 0.5;
|
|
|
|
this._bb = true;
|
|
|
|
} else {
|
|
|
|
this._bb = false;
|
|
|
|
}
|
|
|
|
// The ART API expects the radial gradient to be repeated at the edges.
|
|
|
|
// To simulate this we render the gradient twice as large and add double
|
|
|
|
// color stops. Ideally this API would become more restrictive so that this
|
|
|
|
// extra work isn't needed.
|
2018-05-10 22:44:52 +00:00
|
|
|
const brushData = [RADIAL_GRADIENT, +fx, +fy, +rx * 2, +ry * 2, +cx, +cy];
|
2015-04-29 22:28:18 +00:00
|
|
|
insertDoubleColorStopsIntoArray(stops, brushData, 7);
|
|
|
|
this._brush = brushData;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Pattern(url, width, height, left, top) {
|
|
|
|
this._brush = [PATTERN, url, +left || 0, +top || 0, +width, +height];
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const ReactART = {
|
2015-04-29 22:28:18 +00:00
|
|
|
LinearGradient: LinearGradient,
|
|
|
|
RadialGradient: RadialGradient,
|
|
|
|
Pattern: Pattern,
|
|
|
|
Transform: Transform,
|
|
|
|
Path: Path,
|
|
|
|
Surface: Surface,
|
|
|
|
Group: Group,
|
|
|
|
ClippingRectangle: ClippingRectangle,
|
|
|
|
Shape: Shape,
|
|
|
|
Text: Text,
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ReactART;
|