OSDN Git Service

af870f5d13384c371ddf29d80e9f71f3885c5285
[android-x86/external-webkit.git] / WebCore / platform / graphics / android / PathAndroid.cpp
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
41 Path::Path()
42 {
43     m_path = new SkPath;
44 //    m_path->setFlags(SkPath::kWinding_FillType);
45 }
46
47 Path::Path(const Path& other)
48 {
49     m_path = new SkPath(*other.m_path);
50 }
51
52 Path::~Path()
53 {
54     delete m_path;
55 }
56
57 Path& Path::operator=(const Path& other)
58 {
59     *m_path = *other.m_path;
60     return *this;
61 }
62
63 bool Path::isEmpty() const
64 {
65     return m_path->isEmpty();
66 }
67
68 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
85 void Path::translate(const FloatSize& size)
86 {
87     m_path->offset(SkFloatToScalar(size.width()), SkFloatToScalar(size.height()));
88 }
89
90 FloatRect Path::boundingRect() const
91 {
92     const SkRect& r = m_path->getBounds();
93     return FloatRect(   SkScalarToFloat(r.fLeft),
94                         SkScalarToFloat(r.fTop),
95                         SkScalarToFloat(r.width()),
96                         SkScalarToFloat(r.height()));
97 }
98
99 void Path::moveTo(const FloatPoint& point)
100 {
101     m_path->moveTo(SkFloatToScalar(point.x()), SkFloatToScalar(point.y()));
102 }
103
104 void Path::addLineTo(const FloatPoint& p)
105 {
106     m_path->lineTo(SkFloatToScalar(p.x()), SkFloatToScalar(p.y()));
107 }
108
109 void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& ep)
110 {
111     m_path->quadTo( SkFloatToScalar(cp.x()), SkFloatToScalar(cp.y()),
112                     SkFloatToScalar(ep.x()), SkFloatToScalar(ep.y()));
113 }
114
115 void Path::addBezierCurveTo(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& ep)
116 {
117     m_path->cubicTo(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()),
118                     SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()),
119                     SkFloatToScalar(ep.x()), SkFloatToScalar(ep.y()));
120 }
121
122 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
123 {
124     m_path->arcTo(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()),
125                   SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()),
126                   SkFloatToScalar(radius));
127 }
128
129 void Path::closeSubpath()
130 {
131     m_path->close();
132 }
133
134 static const float gPI  = 3.14159265f;
135 static const float g2PI = 6.28318531f;
136 static const float g180OverPI = 57.29577951308f;
137
138 static float fast_mod(float angle, float max) {
139     if (angle >= max || angle <= -max) {
140         angle = fmodf(angle, max); 
141     }
142     return angle;
143 }
144
145 void Path::addArc(const FloatPoint& p, float r, float sa, float ea,
146                   bool clockwise) {
147     SkScalar    cx = SkFloatToScalar(p.x());
148     SkScalar    cy = SkFloatToScalar(p.y());
149     SkScalar    radius = SkFloatToScalar(r);
150
151     SkRect  oval;
152     oval.set(cx - radius, cy - radius, cx + radius, cy + radius);
153     
154     float sweep = ea - sa;
155     bool prependOval = false;
156
157     /*  Note if clockwise and the sign of the sweep disagree. This particular
158         logic was deduced from http://canvex.lazyilluminati.com/misc/arc.html
159     */
160     if (clockwise && (sweep > 0 || sweep < -g2PI)) {
161         sweep = fmodf(sweep, g2PI) - g2PI;
162     } else if (!clockwise && (sweep < 0 || sweep > g2PI)) {
163         sweep = fmodf(sweep, g2PI) + g2PI;
164     }
165     
166     // If the abs(sweep) >= 2PI, then we need to add a circle before we call
167     // arcTo, since it treats the sweep mod 2PI. We don't have a prepend call,
168     // so we just remember this, and at the end create a new path with an oval
169     // and our current path, and then swap then.
170     //
171     if (sweep >= g2PI || sweep <= -g2PI) {
172         prependOval = true;
173 //        SkDebugf("addArc sa=%g ea=%g cw=%d sweep %g treat as circle\n", sa, ea, clockwise, sweep);
174
175         // now reduce sweep to just the amount we need, so that the current
176         // point is left where the caller expects it.
177         sweep = fmodf(sweep, g2PI);
178     }
179
180     sa = fast_mod(sa, g2PI);
181     SkScalar startDegrees = SkFloatToScalar(sa * g180OverPI);
182     SkScalar sweepDegrees = SkFloatToScalar(sweep * g180OverPI);
183
184 //    SkDebugf("addArc sa=%g ea=%g cw=%d sweep=%g ssweep=%g\n", sa, ea, clockwise, sweep, SkScalarToFloat(sweepDegrees));
185     m_path->arcTo(oval, startDegrees, sweepDegrees, false);
186     
187     if (prependOval) {
188         SkPath tmp;
189         tmp.addOval(oval);
190         tmp.addPath(*m_path);
191         m_path->swap(tmp);
192     }
193 }
194
195 void Path::addRect(const FloatRect& rect)
196 {
197     SkRect  r;
198     
199     android_setrect(&r, rect);
200     m_path->addRect(r);
201 }
202
203 void Path::addEllipse(const FloatRect& rect)
204 {
205     SkRect  r;
206     
207     android_setrect(&r, rect);
208     m_path->addOval(r);
209 }
210
211 void Path::clear()
212 {
213     m_path->reset();
214 }
215
216 static FloatPoint* setfpts(FloatPoint dst[], const SkPoint src[], int count)
217 {
218     for (int i = 0; i < count; i++)
219     {
220         dst[i].setX(SkScalarToFloat(src[i].fX));
221         dst[i].setY(SkScalarToFloat(src[i].fY));
222     }
223     return dst;
224 }
225
226 void Path::apply(void* info, PathApplierFunction function) const
227 {
228     SkPath::Iter    iter(*m_path, false);
229     SkPoint         pts[4];
230     
231     PathElement     elem;
232     FloatPoint      fpts[3];
233
234     for (;;)
235     {
236         switch (iter.next(pts)) {
237         case SkPath::kMove_Verb:
238             elem.type = PathElementMoveToPoint;
239             elem.points = setfpts(fpts, &pts[0], 1);
240             break;
241         case SkPath::kLine_Verb:
242             elem.type = PathElementAddLineToPoint;
243             elem.points = setfpts(fpts, &pts[1], 1);
244             break;
245         case SkPath::kQuad_Verb:
246             elem.type = PathElementAddQuadCurveToPoint;
247             elem.points = setfpts(fpts, &pts[1], 2);
248             break;
249         case SkPath::kCubic_Verb:
250             elem.type = PathElementAddCurveToPoint;
251             elem.points = setfpts(fpts, &pts[1], 3);
252             break;
253         case SkPath::kClose_Verb:
254             elem.type = PathElementCloseSubpath;
255             elem.points = NULL;
256             break;
257         case SkPath::kDone_Verb:
258             return;
259         }
260         function(info, &elem);
261     }
262 }
263
264 void Path::transform(const TransformationMatrix& xform)
265 {
266     m_path->transform(xform);
267 }
268     
269 ///////////////////////////////////////////////////////////////////////////////
270
271 // Computes the bounding box for the stroke and style currently selected into
272 // the given bounding box. This also takes into account the stroke width.
273 static FloatRect boundingBoxForCurrentStroke(GraphicsContext* context)
274 {
275     const SkPath* path = context->getCurrPath();
276     if (NULL == path) {
277         return FloatRect();
278     }
279
280     SkPaint paint;
281     context->setupStrokePaint(&paint);
282     SkPath fillPath;
283     paint.getFillPath(*path, &fillPath);
284     const SkRect& r = fillPath.getBounds();
285     return FloatRect(SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
286                      SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
287 }
288     
289 static GraphicsContext* scratchContext()
290 {
291     static ImageBuffer* scratch = 0;
292     if (!scratch)
293         scratch = ImageBuffer::create(IntSize(1, 1), false).release();
294     // We don't bother checking for failure creating the ImageBuffer, since our
295     // ImageBuffer initializer won't fail.
296     return scratch->context();
297 }    
298
299 FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier)
300 {   
301     GraphicsContext* scratch = scratchContext();
302     scratch->save();
303     scratch->beginPath();
304     scratch->addPath(*this);
305     
306     if (applier)
307         applier->strokeStyle(scratch);
308     
309     FloatRect r = boundingBoxForCurrentStroke(scratch);
310     scratch->restore();
311     return r;
312 }
313
314 }