support stroke in pattern.

support stroke="url(#pattern)"
This commit is contained in:
Horcrux
2016-04-22 19:04:07 +08:00
parent bfcc39f73e
commit 74b5e82781
20 changed files with 456 additions and 369 deletions

View File

@@ -185,7 +185,7 @@ class FillGradientWithOpacity extends Component{
} }
class FillGradientInRect extends Component{ class FillGradientInRect extends Component{
static title = 'Fill a radial gradient inside a rect'; static title = 'Fill a radial gradient inside a rect and stroke it';
render() { render() {
return <Svg return <Svg
height="150" height="150"
@@ -205,7 +205,7 @@ class FillGradientInRect extends Component{
/> />
</RadialGradient> </RadialGradient>
</Defs> </Defs>
<Rect x="0" y="0" width="300" height="150" fill="url(#grad)" /> <Rect x="5" y="5" width="290" height="130" fill="url(#grad)" stroke="pink" strokeWidth="5" />
</Svg>; </Svg>;
} }
} }

View File

@@ -21,6 +21,7 @@ class RectExample extends Component{
fill="rgb(0,0,255)" fill="rgb(0,0,255)"
strokeWidth="3" strokeWidth="3"
stroke="rgb(0,0,0)" stroke="rgb(0,0,0)"
strokeDasharray="5,10"
/> />
</Svg>; </Svg>;
} }
@@ -39,10 +40,10 @@ class RectStrokeFill extends Component{
width="75" width="75"
height="75" height="75"
fill="blue" fill="blue"
fillOpacity="0.1" fillOpacity="0.5"
stroke="pink" stroke="red"
strokeWidth="5" strokeWidth="5"
strokeOpacity="0.9" strokeOpacity="0.5"
/> />
</Svg>; </Svg>;
} }

View File

@@ -4,7 +4,12 @@ import React, {
import Svg, { import Svg, {
Path, Path,
G Rect,
G,
Defs,
Stop,
RadialGradient,
Polyline
} from 'react-native-svg'; } from 'react-native-svg';
class StrokeExample extends Component{ class StrokeExample extends Component{
@@ -59,6 +64,48 @@ class StrokeDasharray extends Component{
} }
} }
class StrokePattern extends Component{
static title = 'Stroke with gradient.';
render() {
return <Svg height="80" width="200">
<Defs>
<RadialGradient id="grad" cx="50%" cy="50%" rx="80%" ry="80%" fx="50%" fy="50%">
<Stop
offset="50%"
stopColor="#fff"
stopOpacity="1"
/>
<Stop
offset="100%"
stopColor="#f00"
stopOpacity="1"
/>
</RadialGradient>
</Defs>
<Rect
x="5"
y="5"
height="70"
width="190"
fill="blue"
stroke="url(#grad)"
strokeWidth="5"
strokeDasharray="10"
/>
<Polyline
strokeDasharray="20,10,5,5,5,10"
points="10,10 20,12 30,20 40,60 60,70 90,55"
fill="none"
stroke="url(#grad)"
strokeLinecap="round"
strokeWidth="5"
/>
</Svg>;
}
}
const icon = <Svg const icon = <Svg
height="20" height="20"
width="20" width="20"
@@ -70,7 +117,7 @@ const icon = <Svg
</G> </G>
</Svg>; </Svg>;
const samples = [StrokeExample, StrokeWidth, StrokeLinecap, StrokeDasharray]; const samples = [StrokeExample, StrokeWidth, StrokeLinecap, StrokeDasharray, StrokePattern];
export { export {
icon, icon,

View File

@@ -4,7 +4,7 @@ import React, {
Children Children
} from 'react-native'; } from 'react-native';
import {NativeGroup} from './G'; import {NativeGroup} from './G';
import {set, remove} from '../lib/extract/extractFill'; import {set, remove} from '../lib/extract/patterns';
import percentFactory from '../lib/percentFactory'; import percentFactory from '../lib/percentFactory';
import percentToFloat from '../lib/percentToFloat'; import percentToFloat from '../lib/percentToFloat';
import Stop from './Stop'; import Stop from './Stop';

View File

@@ -24,6 +24,8 @@
*/ */
- (BOOL)applyFillColor:(CGContextRef)context; - (BOOL)applyFillColor:(CGContextRef)context;
- (BOOL)applyStrokeColor:(CGContextRef)context;
/** /**
* paint fills the context with a brush. The context is assumed to * paint fills the context with a brush. The context is assumed to
* be clipped. * be clipped.

View File

@@ -24,6 +24,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
return NO; return NO;
} }
- (BOOL)applyStrokeColor:(CGContextRef)context
{
return NO;
}
- (void)paint:(CGContextRef)context - (void)paint:(CGContextRef)context
{ {
// abstract // abstract

View File

@@ -35,4 +35,10 @@
return YES; return YES;
} }
- (BOOL)applyStrokeColor:(CGContextRef)context
{
CGContextSetStrokeColorWithColor(context, _color);
return YES;
}
@end @end

View File

@@ -150,6 +150,7 @@ RCT_ENUM_CONVERTER(RNSVGCGFCRule, (@{
{ {
NSArray *arr = [self NSArray:json]; NSArray *arr = [self NSArray:json];
NSUInteger type = [self NSUInteger:arr.firstObject]; NSUInteger type = [self NSUInteger:arr.firstObject];
switch (type) { switch (type) {
case 0: // solid color case 0: // solid color
// These are probably expensive allocations since it's often the same value. // These are probably expensive allocations since it's often the same value.

View File

@@ -32,10 +32,13 @@
} }
CGPathDrawingMode mode = kCGPathStroke; CGPathDrawingMode mode = kCGPathStroke;
BOOL fillColor = YES;
if (self.fill) { if (self.fill) {
if ([self.fill applyFillColor:context]) {
mode = self.fillRule == kRNSVGCGFCRuleEvenodd ? kCGPathEOFill : kCGPathFill; mode = self.fillRule == kRNSVGCGFCRuleEvenodd ? kCGPathEOFill : kCGPathFill;
} else { fillColor = [self.fill applyFillColor:context];
if (!fillColor) {
if (self.clipPath) { if (self.clipPath) {
[self clip:context]; [self clip:context];
} }
@@ -51,23 +54,46 @@
} }
} }
if (self.stroke) { if (self.stroke) {
CGContextSetStrokeColorWithColor(context, self.stroke);
CGContextSetLineWidth(context, self.strokeWidth); CGContextSetLineWidth(context, self.strokeWidth);
CGContextSetLineCap(context, self.strokeLinecap); CGContextSetLineCap(context, self.strokeLinecap);
CGContextSetLineJoin(context, self.strokeLinejoin); CGContextSetLineJoin(context, self.strokeLinejoin);
RNSVGCGFloatArray dash = self.strokeDash; RNSVGCGFloatArray dash = self.strokeDash;
// TODO: render as web svgs do
if (dash.count) { if (dash.count) {
CGContextSetLineDash(context, 0, dash.array, dash.count); CGContextSetLineDash(context, 0, dash.array, dash.count);
} }
if (!fillColor) {
CGContextAddPath(context, self.d);
CGContextReplacePathWithStrokedPath(context);
CGContextClip(context);
}
if ([self.stroke applyStrokeColor:context]) {
if (mode == kCGPathFill) { if (mode == kCGPathFill) {
mode = kCGPathFillStroke; mode = kCGPathFillStroke;
} else if (mode == kCGPathEOFill) { } else if (mode == kCGPathEOFill) {
mode = kCGPathEOFillStroke; mode = kCGPathEOFillStroke;
} }
} else {
// draw fill
[self clip:context];
CGContextAddPath(context, self.d);
CGContextDrawPath(context, mode);
// draw stroke
CGContextAddPath(context, self.d);
CGContextReplacePathWithStrokedPath(context);
CGContextClip(context);
[self.stroke paint:context];
return;
}
} }
[self clip:context]; [self clip:context];
CGContextAddPath(context, self.d); CGContextAddPath(context, self.d);
CGContextDrawPath(context, mode); CGContextDrawPath(context, mode);
} }

View File

@@ -17,7 +17,7 @@
@property (nonatomic, strong) RNSVGBrush *fill; @property (nonatomic, strong) RNSVGBrush *fill;
@property (nonatomic, assign) RNSVGCGFCRule fillRule; @property (nonatomic, assign) RNSVGCGFCRule fillRule;
@property (nonatomic, assign) CGColorRef stroke; @property (nonatomic, strong) RNSVGBrush *stroke;
@property (nonatomic, assign) CGFloat strokeWidth; @property (nonatomic, assign) CGFloat strokeWidth;
@property (nonatomic, assign) CGLineCap strokeLinecap; @property (nonatomic, assign) CGLineCap strokeLinecap;
@property (nonatomic, assign) CGLineJoin strokeLinejoin; @property (nonatomic, assign) CGLineJoin strokeLinejoin;

View File

@@ -16,14 +16,10 @@
_fill = fill; _fill = fill;
} }
- (void)setStroke:(CGColorRef)stroke - (void)setStroke:(RNSVGBrush *)stroke
{ {
if (stroke == _stroke) {
return;
}
[self invalidate]; [self invalidate];
CGColorRelease(_stroke); _stroke = stroke;
_stroke = CGColorRetain(stroke);
} }
- (void)setStrokeWidth:(CGFloat)strokeWidth - (void)setStrokeWidth:(CGFloat)strokeWidth
@@ -58,7 +54,6 @@
- (void)dealloc - (void)dealloc
{ {
CGColorRelease(_stroke);
if (_strokeDash.array) { if (_strokeDash.array) {
free(_strokeDash.array); free(_strokeDash.array);
} }

View File

@@ -80,7 +80,10 @@ static void RNSVGFreeTextFrame(RNSVGTextFrame frame)
} }
} }
if (self.stroke) { if (self.stroke) {
CGContextSetStrokeColorWithColor(context, self.stroke); if ([self.stroke applyStrokeColor:context] == NO) {
[self.stroke paint:context];
}
CGContextSetLineWidth(context, self.strokeWidth); CGContextSetLineWidth(context, self.strokeWidth);
CGContextSetLineCap(context, self.strokeLinecap); CGContextSetLineCap(context, self.strokeLinecap);
CGContextSetLineJoin(context, self.strokeLinejoin); CGContextSetLineJoin(context, self.strokeLinejoin);

View File

@@ -20,12 +20,12 @@ RCT_EXPORT_MODULE()
return [RNSVGRenderable new]; return [RNSVGRenderable new];
} }
RCT_EXPORT_VIEW_PROPERTY(fill, RNSVGBrush)
RCT_EXPORT_VIEW_PROPERTY(fillRule, RNSVGCGFCRule)
RCT_EXPORT_VIEW_PROPERTY(stroke, RNSVGBrush)
RCT_EXPORT_VIEW_PROPERTY(strokeWidth, CGFloat) RCT_EXPORT_VIEW_PROPERTY(strokeWidth, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(strokeLinecap, CGLineCap) RCT_EXPORT_VIEW_PROPERTY(strokeLinecap, CGLineCap)
RCT_EXPORT_VIEW_PROPERTY(strokeLinejoin, CGLineJoin) RCT_EXPORT_VIEW_PROPERTY(strokeLinejoin, CGLineJoin)
RCT_EXPORT_VIEW_PROPERTY(fill, RNSVGBrush)
RCT_EXPORT_VIEW_PROPERTY(fillRule, RNSVGCGFCRule)
RCT_EXPORT_VIEW_PROPERTY(stroke, CGColor)
RCT_EXPORT_VIEW_PROPERTY(strokeDash, RNSVGCGFloatArray) RCT_EXPORT_VIEW_PROPERTY(strokeDash, RNSVGCGFloatArray)
RCT_EXPORT_VIEW_PROPERTY(clipPath, CGPath) RCT_EXPORT_VIEW_PROPERTY(clipPath, CGPath)
RCT_EXPORT_VIEW_PROPERTY(clipRule, RNSVGCGFCRule) RCT_EXPORT_VIEW_PROPERTY(clipRule, RNSVGCGFCRule)

View File

@@ -26,7 +26,7 @@ function applyBoundingBoxToBrushData(brushData, props) {
} }
export default function (colorOrBrush, props) { export default function (colorOrBrush, props) {
if (colorOrBrush == null) { if (!colorOrBrush) {
return null; return null;
} }
if (colorOrBrush._brush) { if (colorOrBrush._brush) {

View File

@@ -1,63 +1,19 @@
import rgba from '../rgba'; import rgba from '../rgba';
import {LinearGradientGenerator} from '../../elements/LinearGradient';
import {RadialGradientGenerator} from '../../elements/RadialGradient';
import extractBrush from './extractBrush'; import extractBrush from './extractBrush';
import fillReg from './patternReg'; import patterns from './patterns';
let fillPatterns = {};
function isGradient(obj) {
return obj instanceof LinearGradientGenerator || obj instanceof RadialGradientGenerator;
}
function set(id, pattern) {
fillPatterns[id] = pattern;
}
function remove(id) {
delete fillPatterns[id];
}
const fillRules = { const fillRules = {
evenodd: 0, evenodd: 0,
nonzero: 1 nonzero: 1
}; };
function fillFilter(props) { function fillFilter(props, dimensions) {
let {fill} = props; let {fill} = props;
if (fill === 'none') { if (fill === 'none') {
return null; return null;
} if (fill) { } else if (fill) {
return patterns(fill, +props.fillOpacity, dimensions, props.svgId);
if (isGradient(fill)) {
return fill;
}
let fillOpacity = +props.fillOpacity;
if (isNaN(fillOpacity)) {
fillOpacity = 1;
}
// 尝试匹配 fill="url(#pattern)"
let matched = fill.match(fillReg);
if (matched) {
let patternName = `${matched[1]}:${props.svgId}`;
let pattern = fillPatterns[patternName];
if (pattern) {
if (pattern.length === 2) {
let dimensions = this.getBoundingBox();
return pattern(dimensions, fillOpacity);
} else {
return pattern(fillOpacity);
}
}
return null;
}
return rgba(props.fill, fillOpacity).rgbaString();
} else if (props.fill === undefined) { } else if (props.fill === undefined) {
let fillOpacity = +props.fillOpacity; let fillOpacity = +props.fillOpacity;
if (isNaN(fillOpacity)) { if (isNaN(fillOpacity)) {
@@ -67,20 +23,14 @@ function fillFilter(props) {
} else { } else {
return null; return null;
} }
} }
export default function(props) { export default function(props, dimensions) {
let fill = fillFilter.call(this, props); let fill = extractBrush(fillFilter(props, dimensions), props);
let fillRule = fillRules[props.fillRule] === 0 ? 0 : 1; let fillRule = fillRules[props.fillRule] === 0 ? 0 : 1;
return { return {
fill: extractBrush(fill, props), fill,
fillRule fillRule
}; };
} }
export {
set,
remove
}

View File

@@ -14,16 +14,18 @@ export default function(props, options = {stroke: true, join: true, transform: t
opacity: +props.opacity || 1 opacity: +props.opacity || 1
}; };
let dimensions = this.getBoundingBox ? this.getBoundingBox() : null;
if (props.clipPath) { if (props.clipPath) {
_.assign(extractedProps, extractClipping(props)); _.assign(extractedProps, extractClipping(props));
} }
if (options.stroke) { if (options.stroke) {
_.assign(extractedProps, extractStroke(props)); _.assign(extractedProps, extractStroke(props, dimensions));
} }
if (options.fill) { if (options.fill) {
_.assign(extractedProps, extractFill.call(this, props)); _.assign(extractedProps, extractFill(props, dimensions));
} }
if (options.transform) { if (options.transform) {

View File

@@ -1,5 +1,6 @@
import rgba from '../rgba';
import extractBrush from './extractBrush'; import extractBrush from './extractBrush';
import patterns from './patterns';
let separator = /\s*,\s*/; let separator = /\s*,\s*/;
const caps = { const caps = {
@@ -14,9 +15,10 @@ const joins = {
round: 1 round: 1
}; };
function strokeFilter(props) { function strokeFilter(props, dimensions) {
if (!props.stroke && !props.strokeLinecap && !props.strokeOpacity && let strokeWidth = +props.strokeWidth;
!props.strokeLinejoin && !props.strokeDasharray && !props.strokeWidth) { let {stroke} = props;
if (!strokeWidth && !stroke) {
return null; return null;
} }
@@ -26,30 +28,25 @@ function strokeFilter(props) {
strokeDasharray = strokeDasharray.split(separator).map(dash => +dash); strokeDasharray = strokeDasharray.split(separator).map(dash => +dash);
} }
if (!stroke) {
stroke = '#000';
}
// TODO: propTypes check
return { return {
stroke: rgba(props.stroke, props.strokeOpacity).rgbaArray(), stroke: patterns(stroke, +props.strokeOpacity, dimensions, props.svgId),
strokeLinecap: caps[props.strokeLinecap] || 2, strokeLinecap: caps[props.strokeLinecap] || 2,
strokeLinejoin: joins[props.strokeLinejoin] || 0, strokeLinejoin: joins[props.strokeLinejoin] || 0,
strokeDash: strokeDasharray || null, strokeDash: strokeDasharray || null,
strokeWidth: +props.strokeWidth || 1 strokeWidth: strokeWidth || 1
}; };
} }
export default function(props) { export default function(props, dimensions) {
let strokeProps = strokeFilter(props); let strokeProps = strokeFilter(props, dimensions);
if (!strokeProps) { return strokeProps ? {
return {};
}
let {stroke} = strokeProps;
return {
...strokeProps, ...strokeProps,
stroke: stroke ? [stroke[0] / 255, stroke[1] / 255, stroke[2] / 255, stroke[3]] : null stroke: extractBrush(strokeProps.stroke, props)
}; } : null;
// TODO: implement brush on stroke prop
//return {
// ...strokeProps,
// stroke: extractBrush(strokeProps.stroke, props)
//};
} }

View File

@@ -1 +0,0 @@
export default /^url\(#(\w+?)\)$/;

53
lib/extract/patterns.js Normal file
View File

@@ -0,0 +1,53 @@
import rgba from '../rgba';
let patterns = {};
let patternReg = /^url\(#(\w+?)\)$/;
import {LinearGradientGenerator} from '../../elements/LinearGradient';
import {RadialGradientGenerator} from '../../elements/RadialGradient';
function isGradient(obj) {
return obj instanceof LinearGradientGenerator || obj instanceof RadialGradientGenerator;
}
function set(id, pattern) {
patterns[id] = pattern;
}
function remove(id) {
delete patterns[id];
}
export {
set,
remove
}
export default function(patternSting, opacity, dimensions, svgId) {
if (isGradient(patternSting)) {
return patternSting;
}
if (isNaN(opacity)) {
opacity = 1;
}
// 尝试匹配 "url(#pattern)"
let matched = patternSting.match(patternReg);
if (matched) {
let patternName = `${matched[1]}:${svgId}`;
let pattern = patterns[patternName];
if (pattern) {
if (pattern.length === 2) {
return pattern(dimensions, opacity);
} else {
return pattern(opacity);
}
}
return null;
}
return rgba(patternSting, opacity).rgbaString();
}

View File

@@ -1,5 +1,5 @@
{ {
"version": "1.0.0", "version": "1.0.1",
"name": "react-native-svg", "name": "react-native-svg",
"description": "react native svg library", "description": "react native svg library",
"repository": { "repository": {