• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2007, The Android Open Source Project
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *  * Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "Path.h"
28 #include "FloatRect.h"
29 #include "GraphicsContext.h"
30 #include "ImageBuffer.h"
31 #include "StrokeStyleApplier.h"
32 #include "TransformationMatrix.h"
33 
34 #include "SkPath.h"
35 #include "SkRegion.h"
36 
37 #include "android_graphics.h"
38 
39 namespace WebCore {
40 
Path()41 Path::Path()
42 {
43     m_path = new SkPath;
44 //    m_path->setFlags(SkPath::kWinding_FillType);
45 }
46 
Path(const Path & other)47 Path::Path(const Path& other)
48 {
49     m_path = new SkPath(*other.m_path);
50 }
51 
~Path()52 Path::~Path()
53 {
54     delete m_path;
55 }
56 
operator =(const Path & other)57 Path& Path::operator=(const Path& other)
58 {
59     *m_path = *other.m_path;
60     return *this;
61 }
62 
isEmpty() const63 bool Path::isEmpty() const
64 {
65     return m_path->isEmpty();
66 }
67 
contains(const FloatPoint & point,WindRule rule) const68 bool Path::contains(const FloatPoint& point, WindRule rule) const
69 {
70     SkRegion    rgn, clip;
71 
72     int x = (int)floorf(point.x());
73     int y = (int)floorf(point.y());
74     clip.setRect(x, y, x + 1, y + 1);
75 
76     SkPath::FillType ft = m_path->getFillType();    // save
77     m_path->setFillType(rule == RULE_NONZERO ? SkPath::kWinding_FillType : SkPath::kEvenOdd_FillType);
78 
79     bool contains = rgn.setPath(*m_path, clip);
80 
81     m_path->setFillType(ft);    // restore
82     return contains;
83 }
84 
translate(const FloatSize & size)85 void Path::translate(const FloatSize& size)
86 {
87     m_path->offset(SkFloatToScalar(size.width()), SkFloatToScalar(size.height()));
88 }
89 
boundingRect() const90 FloatRect Path::boundingRect() const
91 {
92     SkRect  r;
93 
94     m_path->computeBounds(&r, SkPath::kExact_BoundsType);
95     return FloatRect(   SkScalarToFloat(r.fLeft),
96                         SkScalarToFloat(r.fTop),
97                         SkScalarToFloat(r.width()),
98                         SkScalarToFloat(r.height()));
99 }
100 
moveTo(const FloatPoint & point)101 void Path::moveTo(const FloatPoint& point)
102 {
103     m_path->moveTo(SkFloatToScalar(point.x()), SkFloatToScalar(point.y()));
104 }
105 
addLineTo(const FloatPoint & p)106 void Path::addLineTo(const FloatPoint& p)
107 {
108     m_path->lineTo(SkFloatToScalar(p.x()), SkFloatToScalar(p.y()));
109 }
110 
addQuadCurveTo(const FloatPoint & cp,const FloatPoint & ep)111 void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& ep)
112 {
113     m_path->quadTo( SkFloatToScalar(cp.x()), SkFloatToScalar(cp.y()),
114                     SkFloatToScalar(ep.x()), SkFloatToScalar(ep.y()));
115 }
116 
addBezierCurveTo(const FloatPoint & p1,const FloatPoint & p2,const FloatPoint & ep)117 void Path::addBezierCurveTo(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& ep)
118 {
119     m_path->cubicTo(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()),
120                     SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()),
121                     SkFloatToScalar(ep.x()), SkFloatToScalar(ep.y()));
122 }
123 
addArcTo(const FloatPoint & p1,const FloatPoint & p2,float radius)124 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
125 {
126     m_path->arcTo(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()),
127                   SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()),
128                   SkFloatToScalar(radius));
129 }
130 
closeSubpath()131 void Path::closeSubpath()
132 {
133     m_path->close();
134 }
135 
136 static const float gPI  = 3.14159265f;
137 static const float g2PI = 6.28318531f;
138 static const float g180OverPI = 57.29577951308f;
139 
fast_mod(float angle,float max)140 static float fast_mod(float angle, float max) {
141     if (angle >= max || angle <= -max) {
142         angle = fmodf(angle, max);
143     }
144     return angle;
145 }
146 
addArc(const FloatPoint & p,float r,float sa,float ea,bool clockwise)147 void Path::addArc(const FloatPoint& p, float r, float sa, float ea,
148                   bool clockwise) {
149     SkScalar    cx = SkFloatToScalar(p.x());
150     SkScalar    cy = SkFloatToScalar(p.y());
151     SkScalar    radius = SkFloatToScalar(r);
152 
153     SkRect  oval;
154     oval.set(cx - radius, cy - radius, cx + radius, cy + radius);
155 
156     float sweep = ea - sa;
157     bool prependOval = false;
158 
159     /*  Note if clockwise and the sign of the sweep disagree. This particular
160         logic was deduced from http://canvex.lazyilluminati.com/misc/arc.html
161     */
162     if (clockwise && (sweep > 0 || sweep < -g2PI)) {
163         sweep = fmodf(sweep, g2PI) - g2PI;
164     } else if (!clockwise && (sweep < 0 || sweep > g2PI)) {
165         sweep = fmodf(sweep, g2PI) + g2PI;
166     }
167 
168     // If the abs(sweep) >= 2PI, then we need to add a circle before we call
169     // arcTo, since it treats the sweep mod 2PI. We don't have a prepend call,
170     // so we just remember this, and at the end create a new path with an oval
171     // and our current path, and then swap then.
172     //
173     if (sweep >= g2PI || sweep <= -g2PI) {
174         prependOval = true;
175 //        SkDebugf("addArc sa=%g ea=%g cw=%d sweep %g treat as circle\n", sa, ea, clockwise, sweep);
176 
177         // now reduce sweep to just the amount we need, so that the current
178         // point is left where the caller expects it.
179         sweep = fmodf(sweep, g2PI);
180     }
181 
182     sa = fast_mod(sa, g2PI);
183     SkScalar startDegrees = SkFloatToScalar(sa * g180OverPI);
184     SkScalar sweepDegrees = SkFloatToScalar(sweep * g180OverPI);
185 
186 //    SkDebugf("addArc sa=%g ea=%g cw=%d sweep=%g ssweep=%g\n", sa, ea, clockwise, sweep, SkScalarToFloat(sweepDegrees));
187     m_path->arcTo(oval, startDegrees, sweepDegrees, false);
188 
189     if (prependOval) {
190         SkPath tmp;
191         tmp.addOval(oval);
192         tmp.addPath(*m_path);
193         m_path->swap(tmp);
194     }
195 }
196 
addRect(const FloatRect & rect)197 void Path::addRect(const FloatRect& rect)
198 {
199     SkRect  r;
200 
201     android_setrect(&r, rect);
202     m_path->addRect(r);
203 }
204 
addEllipse(const FloatRect & rect)205 void Path::addEllipse(const FloatRect& rect)
206 {
207     SkRect  r;
208 
209     android_setrect(&r, rect);
210     m_path->addOval(r);
211 }
212 
clear()213 void Path::clear()
214 {
215     m_path->reset();
216 }
217 
setfpts(FloatPoint dst[],const SkPoint src[],int count)218 static FloatPoint* setfpts(FloatPoint dst[], const SkPoint src[], int count)
219 {
220     for (int i = 0; i < count; i++)
221     {
222         dst[i].setX(SkScalarToFloat(src[i].fX));
223         dst[i].setY(SkScalarToFloat(src[i].fY));
224     }
225     return dst;
226 }
227 
apply(void * info,PathApplierFunction function) const228 void Path::apply(void* info, PathApplierFunction function) const
229 {
230     SkPath::Iter    iter(*m_path, false);
231     SkPoint         pts[4];
232 
233     PathElement     elem;
234     FloatPoint      fpts[3];
235 
236     for (;;)
237     {
238         switch (iter.next(pts)) {
239         case SkPath::kMove_Verb:
240             elem.type = PathElementMoveToPoint;
241             elem.points = setfpts(fpts, &pts[0], 1);
242             break;
243         case SkPath::kLine_Verb:
244             elem.type = PathElementAddLineToPoint;
245             elem.points = setfpts(fpts, &pts[1], 1);
246             break;
247         case SkPath::kQuad_Verb:
248             elem.type = PathElementAddQuadCurveToPoint;
249             elem.points = setfpts(fpts, &pts[1], 2);
250             break;
251         case SkPath::kCubic_Verb:
252             elem.type = PathElementAddCurveToPoint;
253             elem.points = setfpts(fpts, &pts[1], 3);
254             break;
255         case SkPath::kClose_Verb:
256             elem.type = PathElementCloseSubpath;
257             elem.points = NULL;
258             break;
259         case SkPath::kDone_Verb:
260             return;
261         }
262         function(info, &elem);
263     }
264 }
265 
transform(const TransformationMatrix & xform)266 void Path::transform(const TransformationMatrix& xform)
267 {
268     m_path->transform(xform);
269 }
270 
271 ///////////////////////////////////////////////////////////////////////////////
272 
273 // Computes the bounding box for the stroke and style currently selected into
274 // the given bounding box. This also takes into account the stroke width.
boundingBoxForCurrentStroke(GraphicsContext * context)275 static FloatRect boundingBoxForCurrentStroke(GraphicsContext* context)
276 {
277     const SkPath* path = context->getCurrPath();
278     if (NULL == path) {
279         return FloatRect();
280     }
281 
282     SkPaint paint;
283     context->setupStrokePaint(&paint);
284     SkPath fillPath;
285     paint.getFillPath(*path, &fillPath);
286     SkRect r;
287     fillPath.computeBounds(&r, SkPath::kExact_BoundsType);
288     return FloatRect(r.fLeft, r.fTop, r.width(), r.height());
289 }
290 
scratchContext()291 static GraphicsContext* scratchContext()
292 {
293     static ImageBuffer* scratch = 0;
294     if (!scratch)
295         scratch = ImageBuffer::create(IntSize(1, 1), false).release();
296     // We don't bother checking for failure creating the ImageBuffer, since our
297     // ImageBuffer initializer won't fail.
298     return scratch->context();
299 }
300 
strokeBoundingRect(StrokeStyleApplier * applier)301 FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier)
302 {
303     GraphicsContext* scratch = scratchContext();
304     scratch->save();
305     scratch->beginPath();
306     scratch->addPath(*this);
307 
308     if (applier)
309         applier->strokeStyle(scratch);
310 
311     FloatRect r = boundingBoxForCurrentStroke(scratch);
312     scratch->restore();
313     return r;
314 }
315 
316 }
317