1// tslint:disable max-classes-per-file
2import React from 'react';
3import * as Svg from 'react-native-svg';
4import Example from './Example';
5
6const { LinearGradient, Stop, Defs, Path, G, TSpan, TextPath } = Svg;
7
8class TextExample extends React.Component {
9  static title = 'Text';
10
11  render() {
12    return (
13      <Svg.Svg height="30" width="100">
14        <Svg.Text x="50" y="9" fill="red" textAnchor="middle">
15          I love SVG!
16        </Svg.Text>
17      </Svg.Svg>
18    );
19  }
20}
21
22class TextRotate extends React.Component {
23  static title = 'Transform the text';
24
25  render() {
26    return (
27      <Svg.Svg height="60" width="200">
28        <Svg.Text x="0" y="15" fill="red" rotate="30" origin="20,40">
29          I love SVG
30        </Svg.Text>
31        <Svg.Text x="95" y="47" fill="blue" rotate="-25" origin="95, 20">
32          I love SVG
33        </Svg.Text>
34        <Svg.Text x="126" y="5" fill="#f60" rotate="106" scale="1.36" origin="140, 0">
35          I love SVG
36        </Svg.Text>
37      </Svg.Svg>
38    );
39  }
40}
41
42class TextStroke extends React.Component {
43  static title = 'Stroke the text';
44  render() {
45    return (
46      <Svg.Svg height="60" width="200">
47        <Defs>
48          <LinearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
49            <Stop offset="100%" stopColor="red" stopOpacity="0" />
50            <Stop offset="0%" stopColor="blue" stopOpacity="0.5" />
51          </LinearGradient>
52        </Defs>
53        <Svg.Text
54          stroke="url(#grad)"
55          strokeWidth="2"
56          fill="none"
57          fontSize="30"
58          fontWeight="bold"
59          x="100"
60          y="40"
61        >
62          <TSpan textAnchor="middle">{['STROKE TEXT']}</TSpan>
63        </Svg.Text>
64      </Svg.Svg>
65    );
66  }
67}
68
69class TextFill extends React.Component {
70  static title = 'Fill the text with LinearGradient';
71  render() {
72    return (
73      <Svg.Svg height="60" width="200">
74        <Defs>
75          <LinearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
76            <Stop offset="0%" stopColor="rgb(255,255,0)" stopOpacity="0.5" />
77            <Stop offset="100%" stopColor="red" stopOpacity="1" />
78          </LinearGradient>
79        </Defs>
80
81        <Svg.Text
82          fill="url(#grad)"
83          stroke="purple"
84          strokeWidth="1"
85          fontSize="30"
86          fontWeight="bold"
87          fontStyle="italic"
88          x="100"
89          y="40"
90          textAnchor="middle"
91        >
92          FILL TEXT
93        </Svg.Text>
94      </Svg.Svg>
95    );
96  }
97}
98
99class TextPathExample extends React.Component {
100  static title = 'Draw text along path';
101
102  render() {
103    const path = `
104M 10 20
105C 40 10 60  0 80 10
106C 100 20 120 30 140 20
107C 160 10 180 10 180 10
108`;
109
110    return (
111      <Svg.Svg height="100" width="200">
112        <Defs>
113          <Path id="path" d={path} />
114        </Defs>
115        <G y="20">
116          <Svg.Text fill="blue">
117            <Svg.TextPath href="#path" startOffset="-10%" midLine="sharp">
118              We go up and down,
119              <TSpan fill="red" dy="5,5,5">
120                then up again
121              </TSpan>
122            </Svg.TextPath>
123          </Svg.Text>
124          <Path d={path} fill="none" stroke="red" strokeWidth="1" />
125        </G>
126      </Svg.Svg>
127    );
128  }
129}
130
131class TSpanExample extends React.Component {
132  static title = 'TSpan nest';
133
134  render() {
135    return (
136      <Svg.Svg height="160" width="200">
137        <Svg.Text y="20" dx="5 5">
138          <TSpan x="10">tspan line 1</TSpan>
139          <TSpan x="10" dy="15">
140            tspan line 2
141          </TSpan>
142          <TSpan x="10" dx="10" dy="15">
143            tspan line 3
144          </TSpan>
145        </Svg.Text>
146        <Svg.Text x="10" y="60" fill="red" fontSize="14">
147          <TSpan dy="5 10 20">12345</TSpan>
148          <TSpan fill="blue" dy="15" dx="0 5 5">
149            <TSpan>6</TSpan>
150            <TSpan>7</TSpan>
151          </TSpan>
152          <TSpan dx="0 10 20" dy="0 20" fontWeight="bold" fontSize="12">
153            89a
154          </TSpan>
155        </Svg.Text>
156        <Svg.Text y="140" dx="0 5 5" dy="0 -5 -5">
157          delta on text
158        </Svg.Text>
159      </Svg.Svg>
160    );
161  }
162}
163
164const icon = (
165  <Svg.Svg height="20" width="20">
166    <Svg.Text
167      x="10"
168      y="15"
169      fontSize="14"
170      fontWeight="bold"
171      fontFamily="PingFang HK"
172      textAnchor="middle"
173      fill="none"
174      stroke="blue"
175      strokeWidth="1"
176    >
177      字
178    </Svg.Text>
179  </Svg.Svg>
180);
181
182const Text: Example = {
183  icon,
184  samples: [TextExample, TextRotate, TextStroke, TextFill, TextPathExample, TSpanExample],
185};
186
187export default Text;
188