OLD | NEW |
| (Empty) |
1 /* libs/graphics/effects/Sk1DPathEffect.cpp | |
2 ** | |
3 ** Copyright 2006, The Android Open Source Project | |
4 ** | |
5 ** Licensed under the Apache License, Version 2.0 (the "License"); | |
6 ** you may not use this file except in compliance with the License. | |
7 ** You may obtain a copy of the License at | |
8 ** | |
9 ** http://www.apache.org/licenses/LICENSE-2.0 | |
10 ** | |
11 ** Unless required by applicable law or agreed to in writing, software | |
12 ** distributed under the License is distributed on an "AS IS" BASIS, | |
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 ** See the License for the specific language governing permissions and | |
15 ** limitations under the License. | |
16 */ | |
17 | |
18 #include "Sk1DPathEffect.h" | |
19 #include "SkPathMeasure.h" | |
20 | |
21 bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) | |
22 { | |
23 SkPathMeasure meas(src, false); | |
24 do { | |
25 SkScalar length = meas.getLength(); | |
26 SkScalar distance = this->begin(length); | |
27 while (distance < length) | |
28 { | |
29 SkScalar delta = this->next(dst, distance, meas); | |
30 if (delta <= 0) | |
31 break; | |
32 distance += delta; | |
33 } | |
34 } while (meas.nextContour()); | |
35 return true; | |
36 } | |
37 | |
38 ////////////////////////////////////////////////////////////////////////////////
/////////// | |
39 | |
40 SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance, | |
41 SkScalar phase, Style style) : fPath(path) | |
42 { | |
43 if (advance <= 0 || path.isEmpty()) | |
44 { | |
45 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n")); | |
46 fAdvance = 0; // signals we can't draw anything | |
47 } | |
48 else | |
49 { | |
50 // cleanup their phase parameter, inverting it so that it becomes an | |
51 // offset along the path (to match the interpretation in PostScript) | |
52 if (phase < 0) | |
53 { | |
54 phase = -phase; | |
55 if (phase > advance) | |
56 phase = SkScalarMod(phase, advance); | |
57 } | |
58 else | |
59 { | |
60 if (phase > advance) | |
61 phase = SkScalarMod(phase, advance); | |
62 phase = advance - phase; | |
63 } | |
64 // now catch the edge case where phase == advance (within epsilon) | |
65 if (phase >= advance) | |
66 phase = 0; | |
67 SkASSERT(phase >= 0); | |
68 | |
69 fAdvance = advance; | |
70 fInitialOffset = phase; | |
71 | |
72 if ((unsigned)style >= kStyleCount) { | |
73 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style))
; | |
74 } | |
75 fStyle = style; | |
76 } | |
77 } | |
78 | |
79 bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* wi
dth) | |
80 { | |
81 if (fAdvance > 0) | |
82 { | |
83 *width = -1; | |
84 return this->INHERITED::filterPath(dst, src, width); | |
85 } | |
86 return false; | |
87 } | |
88 | |
89 static void morphpoints(SkPoint dst[], const SkPoint src[], int count, | |
90 SkPathMeasure& meas, SkScalar dist) | |
91 { | |
92 for (int i = 0; i < count; i++) | |
93 { | |
94 SkPoint pos; | |
95 SkVector tangent; | |
96 | |
97 SkScalar sx = src[i].fX; | |
98 SkScalar sy = src[i].fY; | |
99 | |
100 meas.getPosTan(dist + sx, &pos, &tangent); | |
101 | |
102 SkMatrix matrix; | |
103 SkPoint pt; | |
104 | |
105 pt.set(sx, sy); | |
106 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0); | |
107 matrix.preTranslate(-sx, 0); | |
108 matrix.postTranslate(pos.fX, pos.fY); | |
109 matrix.mapPoints(&dst[i], &pt, 1); | |
110 } | |
111 } | |
112 | |
113 /* TODO | |
114 | |
115 Need differentially more subdivisions when the follow-path is curvy. Not sure ho
w to | |
116 determine that, but we need it. I guess a cheap answer is let the caller tell us
, | |
117 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it
out. | |
118 */ | |
119 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, SkSca
lar dist) | |
120 { | |
121 SkPath::Iter iter(src, false); | |
122 SkPoint srcP[4], dstP[3]; | |
123 SkPath::Verb verb; | |
124 | |
125 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) | |
126 { | |
127 switch (verb) { | |
128 case SkPath::kMove_Verb: | |
129 morphpoints(dstP, srcP, 1, meas, dist); | |
130 dst->moveTo(dstP[0]); | |
131 break; | |
132 case SkPath::kLine_Verb: | |
133 srcP[2] = srcP[1]; | |
134 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX), | |
135 SkScalarAve(srcP[0].fY, srcP[2].fY)); | |
136 // fall through to quad | |
137 case SkPath::kQuad_Verb: | |
138 morphpoints(dstP, &srcP[1], 2, meas, dist); | |
139 dst->quadTo(dstP[0], dstP[1]); | |
140 break; | |
141 case SkPath::kCubic_Verb: | |
142 morphpoints(dstP, &srcP[1], 3, meas, dist); | |
143 dst->cubicTo(dstP[0], dstP[1], dstP[2]); | |
144 break; | |
145 case SkPath::kClose_Verb: | |
146 dst->close(); | |
147 break; | |
148 default: | |
149 SkASSERT(!"unknown verb"); | |
150 break; | |
151 } | |
152 } | |
153 } | |
154 | |
155 SkPath1DPathEffect::SkPath1DPathEffect(SkFlattenableReadBuffer& buffer) | |
156 { | |
157 fAdvance = buffer.readScalar(); | |
158 if (fAdvance > 0) { | |
159 fPath.unflatten(buffer); | |
160 fInitialOffset = buffer.readScalar(); | |
161 fStyle = (Style) buffer.readU8(); | |
162 } | |
163 } | |
164 | |
165 SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) | |
166 { | |
167 return fInitialOffset; | |
168 } | |
169 | |
170 void SkPath1DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) | |
171 { | |
172 buffer.writeScalar(fAdvance); | |
173 if (fAdvance > 0) { | |
174 fPath.flatten(buffer); | |
175 buffer.writeScalar(fInitialOffset); | |
176 buffer.write8(fStyle); | |
177 } | |
178 } | |
179 | |
180 SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance, SkPathMeasure&
meas) | |
181 { | |
182 switch (fStyle) { | |
183 case kTranslate_Style: | |
184 { | |
185 SkPoint pos; | |
186 meas.getPosTan(distance, &pos, NULL); | |
187 dst->addPath(fPath, pos.fX, pos.fY); | |
188 } | |
189 break; | |
190 case kRotate_Style: | |
191 { | |
192 SkMatrix matrix; | |
193 meas.getMatrix(distance, &matrix); | |
194 dst->addPath(fPath, matrix); | |
195 } | |
196 break; | |
197 case kMorph_Style: | |
198 morphpath(dst, fPath, meas, distance); | |
199 break; | |
200 default: | |
201 SkASSERT(!"unknown Style enum"); | |
202 break; | |
203 } | |
204 return fAdvance; | |
205 } | |
206 | |
OLD | NEW |