1 // Copyright (c) 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "FloatRect.h"
34 #include "ImageBuffer.h"
35 #include "StrokeStyleApplier.h"
39 #include "SkiaUtils.h"
41 #include <wtf/MathExtras.h>
50 Path::Path(const Path& other)
52 m_path = new SkPath(*other.m_path);
60 Path& Path::operator=(const Path& other)
62 *m_path = *other.m_path;
66 bool Path::isEmpty() const
68 return m_path->isEmpty();
71 bool Path::hasCurrentPoint() const
76 bool Path::contains(const FloatPoint& point, WindRule rule) const
78 return SkPathContainsPoint(m_path, point,
79 rule == RULE_NONZERO ? SkPath::kWinding_FillType : SkPath::kEvenOdd_FillType);
82 void Path::translate(const FloatSize& size)
84 m_path->offset(WebCoreFloatToSkScalar(size.width()), WebCoreFloatToSkScalar(size.height()));
87 FloatRect Path::boundingRect() const
89 return m_path->getBounds();
92 void Path::moveTo(const FloatPoint& point)
94 m_path->moveTo(point);
97 void Path::addLineTo(const FloatPoint& point)
99 m_path->lineTo(point);
102 void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& ep)
104 m_path->quadTo(cp, ep);
107 void Path::addBezierCurveTo(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& ep)
109 m_path->cubicTo(p1, p2, ep);
112 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
114 m_path->arcTo(p1, p2, WebCoreFloatToSkScalar(radius));
117 void Path::closeSubpath()
122 void Path::addArc(const FloatPoint& p, float r, float sa, float ea, bool anticlockwise) {
123 SkScalar cx = WebCoreFloatToSkScalar(p.x());
124 SkScalar cy = WebCoreFloatToSkScalar(p.y());
125 SkScalar radius = WebCoreFloatToSkScalar(r);
128 oval.set(cx - radius, cy - radius, cx + radius, cy + radius);
130 float sweep = ea - sa;
131 // check for a circle
132 if (sweep >= 2 * piFloat || sweep <= -2 * piFloat)
133 m_path->addOval(oval);
135 SkScalar startDegrees = WebCoreFloatToSkScalar(sa * 180 / piFloat);
136 SkScalar sweepDegrees = WebCoreFloatToSkScalar(sweep * 180 / piFloat);
138 // Counterclockwise arcs should be drawn with negative sweeps, while
139 // clockwise arcs should be drawn with positive sweeps. Check to see
140 // if the situation is reversed and correct it by adding or subtracting
142 if (anticlockwise && sweepDegrees > 0) {
143 sweepDegrees -= SkIntToScalar(360);
144 } else if (!anticlockwise && sweepDegrees < 0) {
145 sweepDegrees += SkIntToScalar(360);
148 m_path->arcTo(oval, startDegrees, sweepDegrees, false);
152 void Path::addRect(const FloatRect& rect)
154 m_path->addRect(rect);
157 void Path::addEllipse(const FloatRect& rect)
159 m_path->addOval(rect);
167 static FloatPoint* convertPathPoints(FloatPoint dst[], const SkPoint src[], int count)
169 for (int i = 0; i < count; i++) {
170 dst[i].setX(SkScalarToFloat(src[i].fX));
171 dst[i].setY(SkScalarToFloat(src[i].fY));
176 void Path::apply(void* info, PathApplierFunction function) const
178 SkPath::Iter iter(*m_path, false);
180 PathElement pathElement;
181 FloatPoint pathPoints[3];
184 switch (iter.next(pts)) {
185 case SkPath::kMove_Verb:
186 pathElement.type = PathElementMoveToPoint;
187 pathElement.points = convertPathPoints(pathPoints, &pts[0], 1);
189 case SkPath::kLine_Verb:
190 pathElement.type = PathElementAddLineToPoint;
191 pathElement.points = convertPathPoints(pathPoints, &pts[1], 1);
193 case SkPath::kQuad_Verb:
194 pathElement.type = PathElementAddQuadCurveToPoint;
195 pathElement.points = convertPathPoints(pathPoints, &pts[1], 2);
197 case SkPath::kCubic_Verb:
198 pathElement.type = PathElementAddCurveToPoint;
199 pathElement.points = convertPathPoints(pathPoints, &pts[1], 3);
201 case SkPath::kClose_Verb:
202 pathElement.type = PathElementCloseSubpath;
203 pathElement.points = convertPathPoints(pathPoints, 0, 0);
205 case SkPath::kDone_Verb:
208 function(info, &pathElement);
212 void Path::transform(const TransformationMatrix& xform)
214 m_path->transform(xform);
217 String Path::debugString() const
221 SkPath::Iter iter(*m_path, false);
224 int numPoints = m_path->getPoints(0, 0);
228 verb = iter.next(pts);
230 case SkPath::kMove_Verb:
231 result += String::format("M%.2f,%.2f ", pts[0].fX, pts[0].fY);
234 case SkPath::kLine_Verb:
235 if (!iter.isCloseLine()) {
236 result += String::format("L%.2f,%.2f ", pts[1].fX, pts[1].fY);
240 case SkPath::kQuad_Verb:
241 result += String::format("Q%.2f,%.2f,%.2f,%.2f ",
242 pts[1].fX, pts[1].fY,
243 pts[2].fX, pts[2].fY);
246 case SkPath::kCubic_Verb:
247 result += String::format("C%.2f,%.2f,%.2f,%.2f,%.2f,%.2f ",
248 pts[1].fX, pts[1].fY,
249 pts[2].fX, pts[2].fY,
250 pts[3].fX, pts[3].fY);
253 case SkPath::kClose_Verb:
256 case SkPath::kDone_Verb:
259 } while (verb != SkPath::kDone_Verb);
261 // If you have a path that ends with an M, Skia will not iterate the
262 // trailing M. That's nice of it, but Apple's paths output the trailing M
263 // and we want out layout dumps to look like theirs
265 ASSERT(numPoints==1);
266 m_path->getLastPt(pts);
267 result += String::format("M%.2f,%.2f ", pts[0].fX, pts[0].fY);
270 return result.stripWhiteSpace();
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.
275 static FloatRect boundingBoxForCurrentStroke(const GraphicsContext* context)
278 context->platformContext()->setupPaintForStroking(&paint, 0, 0);
280 paint.getFillPath(context->platformContext()->currentPathInLocalCoordinates(), &boundingPath);
281 return boundingPath.getBounds();
284 FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier)
286 GraphicsContext* scratch = scratchContext();
288 scratch->beginPath();
289 scratch->addPath(*this);
292 applier->strokeStyle(scratch);
294 FloatRect r = boundingBoxForCurrentStroke(scratch);
299 bool Path::strokeContains(StrokeStyleApplier* applier, const FloatPoint& point) const
302 GraphicsContext* scratch = scratchContext();
305 applier->strokeStyle(scratch);
308 scratch->platformContext()->setupPaintForStroking(&paint, 0, 0);
310 paint.getFillPath(*platformPath(), &strokePath);
311 bool contains = SkPathContainsPoint(&strokePath, point,
312 SkPath::kWinding_FillType);
317 } // namespace WebCore