From 2eda0e8ce20657431f4eaaff73071997f9a1f604 Mon Sep 17 00:00:00 2001 From: Horcrux Date: Fri, 22 Apr 2016 22:21:44 +0800 Subject: [PATCH] add support for text clipping and support for text fill pattern --- Example/examples/Clipping.js | 35 ++++++++++++++++++++++++- Example/examples/Stroking.js | 35 ++++++++++--------------- Example/examples/Text.js | 51 +++++++++++++++++++++++++++++++----- ios/RNSVGText.m | 11 +++----- 4 files changed, 97 insertions(+), 35 deletions(-) diff --git a/Example/examples/Clipping.js b/Example/examples/Clipping.js index 59acfcc3..7b8fcb46 100644 --- a/Example/examples/Clipping.js +++ b/Example/examples/Clipping.js @@ -138,6 +138,39 @@ class ClipPathElement extends Component{ } } +class TextClipping extends Component{ + static title = 'Transform the text'; + render() { + return + + + + + + + + + + + + NOT THE FACE + ; + } +} + const icon = ; -const samples = [ClipPathAttr, ClipRule, ClipPathElement]; +const samples = [ClipPathAttr, ClipRule, ClipPathElement, TextClipping]; export { icon, diff --git a/Example/examples/Stroking.js b/Example/examples/Stroking.js index bec27219..099e0ee5 100644 --- a/Example/examples/Stroking.js +++ b/Example/examples/Stroking.js @@ -9,7 +9,9 @@ import Svg, { Defs, Stop, RadialGradient, - Polyline + Polyline, + ClipPath, + Circle } from 'react-native-svg'; class StrokeExample extends Component{ @@ -25,34 +27,21 @@ class StrokeExample extends Component{ } } -class StrokeWidth extends Component{ - static title = 'The stroke property defines the color of a line, text or outline of an element'; - render() { - return - - - - - - ; - } -} - class StrokeLinecap extends Component{ static title = 'The stroke-linecap property defines different types of endings to an open path'; render() { return - - - - + + + + ; } } class StrokeDasharray extends Component{ - static title = 'The stroke-linecap property defines different types of endings to an open path'; + static title = 'strokeDasharray'; render() { return @@ -65,7 +54,7 @@ class StrokeDasharray extends Component{ } class StrokePattern extends Component{ - static title = 'Stroke with gradient.'; + static title = 'Advanced stroke example.'; render() { return @@ -81,6 +70,9 @@ class StrokePattern extends Component{ stopOpacity="1" /> + + + ; -const samples = [StrokeExample, StrokeWidth, StrokeLinecap, StrokeDasharray, StrokePattern]; +const samples = [StrokeExample, StrokeLinecap, StrokeDasharray, StrokePattern]; export { icon, diff --git a/Example/examples/Text.js b/Example/examples/Text.js index bade1dc7..e2ef146f 100644 --- a/Example/examples/Text.js +++ b/Example/examples/Text.js @@ -3,7 +3,11 @@ import React, { } from 'react-native'; import Svg, { - Text + Text, + LinearGradient, + Stop, + Defs, + ClipPath } from 'react-native-svg'; class TextExample extends Component{ @@ -55,6 +59,34 @@ class TextRotate extends Component{ } } +class TextFill extends Component{ + static title = 'Fill the text with LinearGradient'; + render() { + return + + + + + + + + FILL TEXT + ; + } +} + +// TODO: not supported class TextStroke extends Component{ static title = 'Stroke the text'; render() { @@ -62,21 +94,28 @@ class TextStroke extends Component{ height="60" width="200" > + + + + + + + STROKED TEXT + textAnchor="center" + >STROKE TEXT ; } } -// TODO: wait for official done class TextPath extends Component{ static title = 'Transform the text'; render() { @@ -112,7 +151,7 @@ const icon = ; -const samples = [TextExample, TextRotate, TextStroke, TextPath]; +const samples = [TextExample, TextRotate, TextFill, TextPath]; export { icon, diff --git a/ios/RNSVGText.m b/ios/RNSVGText.m index 73f0f19e..64e14193 100644 --- a/ios/RNSVGText.m +++ b/ios/RNSVGText.m @@ -53,14 +53,13 @@ static void RNSVGFreeTextFrame(RNSVGTextFrame frame) } // to-do: draw along a path - // to-do: fill-rule - // to-do: clip CGTextDrawingMode mode = kCGTextStroke; + if (self.fill) { if ([self.fill applyFillColor:context]) { mode = kCGTextFill; } else { - + [self clip: context]; for (int i = 0; i < frame.count; i++) { CGContextSaveGState(context); // Inverse the coordinate space since CoreText assumes a bottom-up coordinate space @@ -80,9 +79,7 @@ static void RNSVGFreeTextFrame(RNSVGTextFrame frame) } } if (self.stroke) { - if ([self.stroke applyStrokeColor:context] == NO) { - [self.stroke paint:context]; - } + [self.stroke applyStrokeColor:context]; CGContextSetLineWidth(context, self.strokeWidth); CGContextSetLineCap(context, self.strokeLinecap); @@ -97,7 +94,7 @@ static void RNSVGFreeTextFrame(RNSVGTextFrame frame) } CGContextSetTextDrawingMode(context, mode); - + [self clip:context]; // Inverse the coordinate space since CoreText assumes a bottom-up coordinate space CGContextScaleCTM(context, 1.0, -1.0); for (int i = 0; i < frame.count; i++) {