2 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
3 * 2006, 2008 Rob Buis <buis@kde.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "TransformationMatrix.h"
33 #include <ApplicationServices/ApplicationServices.h>
34 #include "FloatRect.h"
35 #include "GraphicsContext.h"
37 #include "PlatformString.h"
38 #include "StrokeStyleApplier.h"
40 #include <wtf/MathExtras.h>
44 static size_t putBytesNowhere(void*, const void*, size_t count)
49 static CGContextRef createScratchContext()
51 CGDataConsumerCallbacks callbacks = { putBytesNowhere, 0 };
52 CGDataConsumerRef consumer = CGDataConsumerCreate(0, &callbacks);
53 CGContextRef context = CGPDFContextCreate(consumer, 0, 0);
54 CGDataConsumerRelease(consumer);
56 CGFloat black[4] = { 0, 0, 0, 1 };
57 CGContextSetFillColor(context, black);
58 CGContextSetStrokeColor(context, black);
63 static inline CGContextRef scratchContext()
65 static CGContextRef context = createScratchContext();
70 : m_path(CGPathCreateMutable())
76 CGPathRelease(m_path);
79 Path::Path(const Path& other)
80 : m_path(CGPathCreateMutableCopy(other.m_path))
84 Path& Path::operator=(const Path& other)
86 CGMutablePathRef path = CGPathCreateMutableCopy(other.m_path);
87 CGPathRelease(m_path);
92 static void copyClosingSubpathsApplierFunction(void* info, const CGPathElement* element)
94 CGMutablePathRef path = static_cast<CGMutablePathRef>(info);
95 CGPoint* points = element->points;
97 switch (element->type) {
98 case kCGPathElementMoveToPoint:
99 if (!CGPathIsEmpty(path)) // to silence a warning when trying to close an empty path
100 CGPathCloseSubpath(path); // This is the only change from CGPathCreateMutableCopy
101 CGPathMoveToPoint(path, 0, points[0].x, points[0].y);
103 case kCGPathElementAddLineToPoint:
104 CGPathAddLineToPoint(path, 0, points[0].x, points[0].y);
106 case kCGPathElementAddQuadCurveToPoint:
107 CGPathAddQuadCurveToPoint(path, 0, points[0].x, points[0].y, points[1].x, points[1].y);
109 case kCGPathElementAddCurveToPoint:
110 CGPathAddCurveToPoint(path, 0, points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y);
112 case kCGPathElementCloseSubpath:
113 CGPathCloseSubpath(path);
118 static CGMutablePathRef copyCGPathClosingSubpaths(CGPathRef originalPath)
120 CGMutablePathRef path = CGPathCreateMutable();
121 CGPathApply(originalPath, path, copyClosingSubpathsApplierFunction);
122 CGPathCloseSubpath(path);
126 bool Path::contains(const FloatPoint &point, WindRule rule) const
128 if (!boundingRect().contains(point))
131 // CGPathContainsPoint returns false for non-closed paths, as a work-around, we copy and close the path first. Radar 4758998 asks for a better CG API to use
132 CGMutablePathRef path = copyCGPathClosingSubpaths(m_path);
133 bool ret = CGPathContainsPoint(path, 0, point, rule == RULE_EVENODD ? true : false);
138 bool Path::strokeContains(StrokeStyleApplier* applier, const FloatPoint& point) const
142 CGContextRef context = scratchContext();
144 CGContextSaveGState(context);
145 CGContextBeginPath(context);
146 CGContextAddPath(context, platformPath());
148 GraphicsContext gc(context);
149 applier->strokeStyle(&gc);
151 bool hitSuccess = CGContextPathContainsPoint(context, point, kCGPathStroke);
152 CGContextRestoreGState(context);
157 void Path::translate(const FloatSize& size)
159 CGAffineTransform translation = CGAffineTransformMake(1, 0, 0, 1, size.width(), size.height());
160 CGMutablePathRef newPath = CGPathCreateMutable();
161 CGPathAddPath(newPath, &translation, m_path);
162 CGPathRelease(m_path);
166 FloatRect Path::boundingRect() const
168 return CGPathGetBoundingBox(m_path);
171 FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier)
173 CGContextRef context = scratchContext();
175 CGContextSaveGState(context);
176 CGContextBeginPath(context);
177 CGContextAddPath(context, platformPath());
180 GraphicsContext graphicsContext(context);
181 applier->strokeStyle(&graphicsContext);
184 CGContextReplacePathWithStrokedPath(context);
185 CGRect box = CGContextIsPathEmpty(context) ? CGRectZero : CGContextGetPathBoundingBox(context);
186 CGContextRestoreGState(context);
191 void Path::moveTo(const FloatPoint& point)
193 CGPathMoveToPoint(m_path, 0, point.x(), point.y());
196 void Path::addLineTo(const FloatPoint& p)
198 CGPathAddLineToPoint(m_path, 0, p.x(), p.y());
201 void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& p)
203 CGPathAddQuadCurveToPoint(m_path, 0, cp.x(), cp.y(), p.x(), p.y());
206 void Path::addBezierCurveTo(const FloatPoint& cp1, const FloatPoint& cp2, const FloatPoint& p)
208 CGPathAddCurveToPoint(m_path, 0, cp1.x(), cp1.y(), cp2.x(), cp2.y(), p.x(), p.y());
211 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
213 CGPathAddArcToPoint(m_path, 0, p1.x(), p1.y(), p2.x(), p2.y(), radius);
216 void Path::closeSubpath()
218 if (!CGPathIsEmpty(m_path)) // to silence a warning when trying to close an empty path
219 CGPathCloseSubpath(m_path);
222 void Path::addArc(const FloatPoint& p, float r, float sa, float ea, bool clockwise)
224 // Workaround for <rdar://problem/5189233> CGPathAddArc hangs or crashes when passed inf as start or end angle
225 if (isfinite(sa) && isfinite(ea))
226 CGPathAddArc(m_path, 0, p.x(), p.y(), r, sa, ea, clockwise);
229 void Path::addRect(const FloatRect& r)
231 CGPathAddRect(m_path, 0, r);
234 void Path::addEllipse(const FloatRect& r)
236 CGPathAddEllipseInRect(m_path, 0, r);
241 CGPathRelease(m_path);
242 m_path = CGPathCreateMutable();
245 bool Path::isEmpty() const
247 return CGPathIsEmpty(m_path);
250 bool Path::hasCurrentPoint() const
255 static void CGPathToCFStringApplierFunction(void* info, const CGPathElement *element)
257 CFMutableStringRef string = static_cast<CFMutableStringRef>(info);
259 CGPoint* points = element->points;
260 switch (element->type) {
261 case kCGPathElementMoveToPoint:
262 CFStringAppendFormat(string, 0, CFSTR("M%.2f,%.2f "), points[0].x, points[0].y);
264 case kCGPathElementAddLineToPoint:
265 CFStringAppendFormat(string, 0, CFSTR("L%.2f,%.2f "), points[0].x, points[0].y);
267 case kCGPathElementAddQuadCurveToPoint:
268 CFStringAppendFormat(string, 0, CFSTR("Q%.2f,%.2f,%.2f,%.2f "),
269 points[0].x, points[0].y, points[1].x, points[1].y);
271 case kCGPathElementAddCurveToPoint:
272 CFStringAppendFormat(string, 0, CFSTR("C%.2f,%.2f,%.2f,%.2f,%.2f,%.2f "),
273 points[0].x, points[0].y, points[1].x, points[1].y,
274 points[2].x, points[2].y);
276 case kCGPathElementCloseSubpath:
277 CFStringAppendFormat(string, 0, CFSTR("Z "));
282 static CFStringRef CFStringFromCGPath(CGPathRef path)
287 CFMutableStringRef string = CFStringCreateMutable(NULL, 0);
288 CGPathApply(path, string, CGPathToCFStringApplierFunction);
289 CFStringTrimWhitespace(string);
297 #pragma mark Path Management
299 String Path::debugString() const
303 CFStringRef pathString = CFStringFromCGPath(m_path);
304 result = String(pathString);
305 CFRelease(pathString);
310 struct PathApplierInfo {
312 PathApplierFunction function;
315 static void CGPathApplierToPathApplier(void *info, const CGPathElement *element)
317 PathApplierInfo* pinfo = (PathApplierInfo*)info;
318 FloatPoint points[3];
319 PathElement pelement;
320 pelement.type = (PathElementType)element->type;
321 pelement.points = points;
322 CGPoint* cgPoints = element->points;
323 switch (element->type) {
324 case kCGPathElementMoveToPoint:
325 case kCGPathElementAddLineToPoint:
326 points[0] = cgPoints[0];
328 case kCGPathElementAddQuadCurveToPoint:
329 points[0] = cgPoints[0];
330 points[1] = cgPoints[1];
332 case kCGPathElementAddCurveToPoint:
333 points[0] = cgPoints[0];
334 points[1] = cgPoints[1];
335 points[2] = cgPoints[2];
337 case kCGPathElementCloseSubpath:
340 pinfo->function(pinfo->info, &pelement);
343 void Path::apply(void* info, PathApplierFunction function) const
345 PathApplierInfo pinfo;
347 pinfo.function = function;
348 CGPathApply(m_path, &pinfo, CGPathApplierToPathApplier);
351 void Path::transform(const TransformationMatrix& transform)
353 CGMutablePathRef path = CGPathCreateMutable();
354 CGAffineTransform transformCG = transform;
355 CGPathAddPath(path, &transformCG, m_path);
356 CGPathRelease(m_path);
362 #endif // PLATFORM(CG)