2 * Copyright 2007, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "FloatRect.h"
29 #include "GraphicsContext.h"
30 #include "ImageBuffer.h"
31 #include "StrokeStyleApplier.h"
32 #include "TransformationMatrix.h"
37 #include "android_graphics.h"
44 // m_path->setFlags(SkPath::kWinding_FillType);
47 Path::Path(const Path& other)
49 m_path = new SkPath(*other.m_path);
57 Path& Path::operator=(const Path& other)
59 *m_path = *other.m_path;
63 bool Path::isEmpty() const
65 return m_path->isEmpty();
68 bool Path::contains(const FloatPoint& point, WindRule rule) const
72 int x = (int)floorf(point.x());
73 int y = (int)floorf(point.y());
74 clip.setRect(x, y, x + 1, y + 1);
76 SkPath::FillType ft = m_path->getFillType(); // save
77 m_path->setFillType(rule == RULE_NONZERO ? SkPath::kWinding_FillType : SkPath::kEvenOdd_FillType);
79 bool contains = rgn.setPath(*m_path, clip);
81 m_path->setFillType(ft); // restore
85 void Path::translate(const FloatSize& size)
87 m_path->offset(SkFloatToScalar(size.width()), SkFloatToScalar(size.height()));
90 FloatRect Path::boundingRect() const
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()));
99 void Path::moveTo(const FloatPoint& point)
101 m_path->moveTo(SkFloatToScalar(point.x()), SkFloatToScalar(point.y()));
104 void Path::addLineTo(const FloatPoint& p)
106 m_path->lineTo(SkFloatToScalar(p.x()), SkFloatToScalar(p.y()));
109 void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& ep)
111 m_path->quadTo( SkFloatToScalar(cp.x()), SkFloatToScalar(cp.y()),
112 SkFloatToScalar(ep.x()), SkFloatToScalar(ep.y()));
115 void Path::addBezierCurveTo(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& ep)
117 m_path->cubicTo(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()),
118 SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()),
119 SkFloatToScalar(ep.x()), SkFloatToScalar(ep.y()));
122 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
124 m_path->arcTo(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()),
125 SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()),
126 SkFloatToScalar(radius));
129 void Path::closeSubpath()
134 static const float gPI = 3.14159265f;
135 static const float g2PI = 6.28318531f;
136 static const float g180OverPI = 57.29577951308f;
138 static float fast_mod(float angle, float max) {
139 if (angle >= max || angle <= -max) {
140 angle = fmodf(angle, max);
145 void Path::addArc(const FloatPoint& p, float r, float sa, float ea,
147 SkScalar cx = SkFloatToScalar(p.x());
148 SkScalar cy = SkFloatToScalar(p.y());
149 SkScalar radius = SkFloatToScalar(r);
152 oval.set(cx - radius, cy - radius, cx + radius, cy + radius);
154 float sweep = ea - sa;
155 bool prependOval = false;
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
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;
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.
171 if (sweep >= g2PI || sweep <= -g2PI) {
173 // SkDebugf("addArc sa=%g ea=%g cw=%d sweep %g treat as circle\n", sa, ea, clockwise, sweep);
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);
180 sa = fast_mod(sa, g2PI);
181 SkScalar startDegrees = SkFloatToScalar(sa * g180OverPI);
182 SkScalar sweepDegrees = SkFloatToScalar(sweep * g180OverPI);
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);
190 tmp.addPath(*m_path);
195 void Path::addRect(const FloatRect& rect)
199 android_setrect(&r, rect);
203 void Path::addEllipse(const FloatRect& rect)
207 android_setrect(&r, rect);
216 static FloatPoint* setfpts(FloatPoint dst[], const SkPoint src[], int count)
218 for (int i = 0; i < count; i++)
220 dst[i].setX(SkScalarToFloat(src[i].fX));
221 dst[i].setY(SkScalarToFloat(src[i].fY));
226 void Path::apply(void* info, PathApplierFunction function) const
228 SkPath::Iter iter(*m_path, false);
236 switch (iter.next(pts)) {
237 case SkPath::kMove_Verb:
238 elem.type = PathElementMoveToPoint;
239 elem.points = setfpts(fpts, &pts[0], 1);
241 case SkPath::kLine_Verb:
242 elem.type = PathElementAddLineToPoint;
243 elem.points = setfpts(fpts, &pts[1], 1);
245 case SkPath::kQuad_Verb:
246 elem.type = PathElementAddQuadCurveToPoint;
247 elem.points = setfpts(fpts, &pts[1], 2);
249 case SkPath::kCubic_Verb:
250 elem.type = PathElementAddCurveToPoint;
251 elem.points = setfpts(fpts, &pts[1], 3);
253 case SkPath::kClose_Verb:
254 elem.type = PathElementCloseSubpath;
257 case SkPath::kDone_Verb:
260 function(info, &elem);
264 void Path::transform(const TransformationMatrix& xform)
266 m_path->transform(xform);
269 ///////////////////////////////////////////////////////////////////////////////
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)
275 const SkPath* path = context->getCurrPath();
281 context->setupStrokePaint(&paint);
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()));
289 static GraphicsContext* scratchContext()
291 static ImageBuffer* scratch = 0;
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();
299 FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier)
301 GraphicsContext* scratch = scratchContext();
303 scratch->beginPath();
304 scratch->addPath(*this);
307 applier->strokeStyle(scratch);
309 FloatRect r = boundingBoxForCurrentStroke(scratch);