JavaCAD
BezierListProducer.java
Go to the documentation of this file.
1 /*
2 
3  Licensed to the Apache Software Foundation (ASF) under one or more
4  contributor license agreements. See the NOTICE file distributed with
5  this work for additional information regarding copyright ownership.
6  The ASF licenses this file to You under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with
8  the License. You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 
18  */
19 package com.piro.bezier;
20 
21 import java.util.ArrayList;
22 
30 public class BezierListProducer implements PathHandler {
31 
32  final ArrayList<Bezier> bezierSegs = new ArrayList<Bezier>();
33  float[] coords = new float[6];
34  float curveLength = 0f;
35  BezierHistory hist = new BezierHistory();
36 
37  @Override
38  public void startPath() throws ParseException {
39  curveLength = 0f;
40  bezierSegs.clear();
41  }
42 
43  @Override
44  public void movetoRel(float x, float y) throws ParseException {
45  float offx = hist.lastPoint.x;
46  float offy = hist.lastPoint.y;
47 
48  movetoAbs(offx + x, offy + y);;
49  }
50 
51  @Override
52  public void movetoAbs(float x, float y) throws ParseException {
53  hist.setLastPoint(x, y);
54  }
55 
56  @Override
57  public void closePath() throws ParseException {
58  //command(SVGPathSeg.PATHSEG_CLOSEPATH);
59  }
60 
61  @Override
62  public void linetoRel(float x, float y) throws ParseException {
63  float offx = hist.lastPoint.x;
64  float offy = hist.lastPoint.y;
65 
66  linetoAbs(offx + x, offy + y);
67  }
68 
69  @Override
70  public void linetoAbs(float x, float y) throws ParseException {
71 
72  coords[0] = x;
73  coords[1] = y;
74 
75  Bezier b = new Bezier(hist.lastPoint.x, hist.lastPoint.y, coords, 1);
76  bezierSegs.add(b);
77  curveLength += b.getLength();
78 
79  hist.setLastPoint(x, y);
80  hist.setLastKnot(x, y);
81 
82  }
83 
84  @Override
85  public void linetoHorizontalRel(float x) throws ParseException {
86  linetoAbs(x + hist.lastPoint.x, hist.lastPoint.y);
87  }
88 
89  @Override
90  public void linetoHorizontalAbs(float x) throws ParseException {
91  linetoAbs(x, hist.lastPoint.y);
92  }
93 
94  @Override
95  public void linetoVerticalRel(float y) throws ParseException {
96  linetoAbs(hist.lastPoint.x, y + hist.lastPoint.y);
97  }
98 
99  @Override
100  public void linetoVerticalAbs(float y) throws ParseException {
101  linetoAbs(hist.lastPoint.x, y);
102  }
103 
104  @Override
105  public void curvetoCubicRel(float x1, float y1,
106  float x2, float y2,
107  float x, float y) throws ParseException {
108  float offx = hist.lastPoint.x;
109  float offy = hist.lastPoint.y;
110 
111  curvetoCubicAbs(x1 + offx, y1 + offy,
112  x2 + offx, y2 + offy,
113  x + offx, y + offy);
114  }
115 
116  @Override
117  public void curvetoCubicAbs(float x1, float y1,
118  float x2, float y2,
119  float x, float y) throws ParseException {
120 
121  coords[0] = x1;
122  coords[1] = y1;
123  coords[2] = x2;
124  coords[3] = y2;
125  coords[4] = x;
126  coords[5] = y;
127 
128  Bezier b = new Bezier(hist.lastPoint.x, hist.lastPoint.y, coords, 3);
129  bezierSegs.add(b);
130  curveLength += b.getLength();
131  hist.setLastPoint(x, y);
132  hist.setLastKnot(x2, y2);
133  }
134 
135  @Override
136  public void curvetoCubicSmoothRel(float x2, float y2,
137  float x, float y) throws ParseException {
138  float offx = hist.lastPoint.x;
139  float offy = hist.lastPoint.y;
140 
141  curvetoCubicSmoothAbs(x2 + offx, y2 + offy, x + offx, y + offy);
142  }
143 
144  @Override
145  public void curvetoCubicSmoothAbs(float x2, float y2,
146  float x, float y) throws ParseException {
147 
148  float oldKx = hist.lastKnot.x;
149  float oldKy = hist.lastKnot.y;
150  float oldX = hist.lastPoint.x;
151  float oldY = hist.lastPoint.y;
152  //Calc knot as reflection of old knot
153  float k1x = oldX * 2f - oldKx;
154  float k1y = oldY * 2f - oldKy;
155 
156  coords[0] = k1x;
157  coords[1] = k1y;
158  coords[2] = x2;
159  coords[3] = y2;
160  coords[4] = x;
161  coords[5] = y;
162 
163  Bezier b = new Bezier(hist.lastPoint.x, hist.lastPoint.y, coords, 3);
164  bezierSegs.add(b);
165  curveLength += b.getLength();
166  hist.setLastPoint(x, y);
167  hist.setLastKnot(x2, y2);
168  }
169 
170  @Override
171  public void curvetoQuadraticRel(float x1, float y1,
172  float x, float y) throws ParseException {
173  float offx = hist.lastPoint.x;
174  float offy = hist.lastPoint.y;
175 
176  curvetoQuadraticAbs(x1 + offx, y1 + offy, x + offx, y + offy);
177  }
178 
179  @Override
180  public void curvetoQuadraticAbs(float x1, float y1,
181  float x, float y) throws ParseException {
182 
183  coords[0] = x1;
184  coords[1] = y1;
185  coords[2] = x;
186  coords[3] = y;
187 
188  Bezier b = new Bezier(hist.lastPoint.x, hist.lastPoint.y, coords, 2);
189  bezierSegs.add(b);
190  curveLength += b.getLength();
191 
192  hist.setLastPoint(x, y);
193  hist.setLastKnot(x1, y1);
194  }
195 
196  @Override
197  public void curvetoQuadraticSmoothRel(float x, float y) {
198  float offx = hist.lastPoint.x;
199  float offy = hist.lastPoint.y;
200 
201  curvetoQuadraticSmoothAbs(x + offx, y + offy);
202  }
203 
204  @Override
205  public void curvetoQuadraticSmoothAbs(float x, float y)
206  throws ParseException {
207 
208  curvetoQuadraticAbs(hist.lastKnot.x, hist.lastKnot.y, x, y);
209  }
210 
211  @Override
212  public void arcRel(float rx, float ry,
213  float xAxisRotation,
214  boolean largeArcFlag, boolean sweepFlag,
215  float x, float y) throws ParseException {
216 
217  }
218 
219  @Override
220  public void arcAbs(float rx, float ry,
221  float xAxisRotation,
222  boolean largeArcFlag, boolean sweepFlag,
223  float x, float y) throws ParseException {
224 
225  }
226 
227  @Override
228  public void endPath() throws ParseException {
229  hist.setLastPoint(hist.startPoint.x, hist.startPoint.y);
230  hist.setLastKnot(hist.startPoint.x, hist.startPoint.y);
231  }
232 }
void setLastKnot(float x, float y)
void setLastPoint(float x, float y)
void curvetoQuadraticAbs(float x1, float y1, float x, float y)
void curvetoCubicSmoothRel(float x2, float y2, float x, float y)
void curvetoCubicRel(float x1, float y1, float x2, float y2, float x, float y)
void curvetoCubicSmoothAbs(float x2, float y2, float x, float y)
void arcAbs(float rx, float ry, float xAxisRotation, boolean largeArcFlag, boolean sweepFlag, float x, float y)
void curvetoCubicAbs(float x1, float y1, float x2, float y2, float x, float y)
void arcRel(float rx, float ry, float xAxisRotation, boolean largeArcFlag, boolean sweepFlag, float x, float y)
void curvetoQuadraticRel(float x1, float y1, float x, float y)
void curvetoQuadraticSmoothRel(float x, float y)
void curvetoQuadraticSmoothAbs(float x, float y)