1 /*
2 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
3 * 2006, 2008 Rob Buis <buis@kde.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
25 */
26
27 #include "config.h"
28 #include "Path.h"
29
30 #if PLATFORM(CG)
31
32 #include "AffineTransform.h"
33 #include <ApplicationServices/ApplicationServices.h>
34 #include "FloatRect.h"
35 #include "GraphicsContext.h"
36 #include "IntRect.h"
37 #include "PlatformString.h"
38 #include "StrokeStyleApplier.h"
39
40 #include <wtf/MathExtras.h>
41
42 namespace WebCore {
43
putBytesNowhere(void *,const void *,size_t count)44 static size_t putBytesNowhere(void*, const void*, size_t count)
45 {
46 return count;
47 }
48
createScratchContext()49 static CGContextRef createScratchContext()
50 {
51 CGDataConsumerCallbacks callbacks = { putBytesNowhere, 0 };
52 RetainPtr<CGDataConsumerRef> consumer(AdoptCF, CGDataConsumerCreate(0, &callbacks));
53 CGContextRef context = CGPDFContextCreate(consumer.get(), 0, 0);
54
55 CGFloat black[4] = { 0, 0, 0, 1 };
56 CGContextSetFillColor(context, black);
57 CGContextSetStrokeColor(context, black);
58
59 return context;
60 }
61
scratchContext()62 static inline CGContextRef scratchContext()
63 {
64 static CGContextRef context = createScratchContext();
65 return context;
66 }
67
Path()68 Path::Path()
69 : m_path(CGPathCreateMutable())
70 {
71 }
72
~Path()73 Path::~Path()
74 {
75 CGPathRelease(m_path);
76 }
77
Path(const Path & other)78 Path::Path(const Path& other)
79 : m_path(CGPathCreateMutableCopy(other.m_path))
80 {
81 }
82
operator =(const Path & other)83 Path& Path::operator=(const Path& other)
84 {
85 CGMutablePathRef path = CGPathCreateMutableCopy(other.m_path);
86 CGPathRelease(m_path);
87 m_path = path;
88 return *this;
89 }
90
copyClosingSubpathsApplierFunction(void * info,const CGPathElement * element)91 static void copyClosingSubpathsApplierFunction(void* info, const CGPathElement* element)
92 {
93 CGMutablePathRef path = static_cast<CGMutablePathRef>(info);
94 CGPoint* points = element->points;
95
96 switch (element->type) {
97 case kCGPathElementMoveToPoint:
98 if (!CGPathIsEmpty(path)) // to silence a warning when trying to close an empty path
99 CGPathCloseSubpath(path); // This is the only change from CGPathCreateMutableCopy
100 CGPathMoveToPoint(path, 0, points[0].x, points[0].y);
101 break;
102 case kCGPathElementAddLineToPoint:
103 CGPathAddLineToPoint(path, 0, points[0].x, points[0].y);
104 break;
105 case kCGPathElementAddQuadCurveToPoint:
106 CGPathAddQuadCurveToPoint(path, 0, points[0].x, points[0].y, points[1].x, points[1].y);
107 break;
108 case kCGPathElementAddCurveToPoint:
109 CGPathAddCurveToPoint(path, 0, points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y);
110 break;
111 case kCGPathElementCloseSubpath:
112 CGPathCloseSubpath(path);
113 break;
114 }
115 }
116
copyCGPathClosingSubpaths(CGPathRef originalPath)117 static CGMutablePathRef copyCGPathClosingSubpaths(CGPathRef originalPath)
118 {
119 CGMutablePathRef path = CGPathCreateMutable();
120 CGPathApply(originalPath, path, copyClosingSubpathsApplierFunction);
121 CGPathCloseSubpath(path);
122 return path;
123 }
124
contains(const FloatPoint & point,WindRule rule) const125 bool Path::contains(const FloatPoint &point, WindRule rule) const
126 {
127 if (!boundingRect().contains(point))
128 return false;
129
130 // 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
131 RetainPtr<CGMutablePathRef> path(AdoptCF, copyCGPathClosingSubpaths(m_path));
132 bool ret = CGPathContainsPoint(path.get(), 0, point, rule == RULE_EVENODD ? true : false);
133 return ret;
134 }
135
strokeContains(StrokeStyleApplier * applier,const FloatPoint & point) const136 bool Path::strokeContains(StrokeStyleApplier* applier, const FloatPoint& point) const
137 {
138 ASSERT(applier);
139
140 CGContextRef context = scratchContext();
141
142 CGContextSaveGState(context);
143 CGContextBeginPath(context);
144 CGContextAddPath(context, platformPath());
145
146 GraphicsContext gc(context);
147 applier->strokeStyle(&gc);
148
149 bool hitSuccess = CGContextPathContainsPoint(context, point, kCGPathStroke);
150 CGContextRestoreGState(context);
151
152 return hitSuccess;
153 }
154
translate(const FloatSize & size)155 void Path::translate(const FloatSize& size)
156 {
157 CGAffineTransform translation = CGAffineTransformMake(1, 0, 0, 1, size.width(), size.height());
158 CGMutablePathRef newPath = CGPathCreateMutable();
159 CGPathAddPath(newPath, &translation, m_path);
160 CGPathRelease(m_path);
161 m_path = newPath;
162 }
163
boundingRect() const164 FloatRect Path::boundingRect() const
165 {
166 return CGPathGetBoundingBox(m_path);
167 }
168
strokeBoundingRect(StrokeStyleApplier * applier)169 FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier)
170 {
171 CGContextRef context = scratchContext();
172
173 CGContextSaveGState(context);
174 CGContextBeginPath(context);
175 CGContextAddPath(context, platformPath());
176
177 if (applier) {
178 GraphicsContext graphicsContext(context);
179 applier->strokeStyle(&graphicsContext);
180 }
181
182 CGContextReplacePathWithStrokedPath(context);
183 CGRect box = CGContextIsPathEmpty(context) ? CGRectZero : CGContextGetPathBoundingBox(context);
184 CGContextRestoreGState(context);
185
186 return box;
187 }
188
moveTo(const FloatPoint & point)189 void Path::moveTo(const FloatPoint& point)
190 {
191 CGPathMoveToPoint(m_path, 0, point.x(), point.y());
192 }
193
addLineTo(const FloatPoint & p)194 void Path::addLineTo(const FloatPoint& p)
195 {
196 CGPathAddLineToPoint(m_path, 0, p.x(), p.y());
197 }
198
addQuadCurveTo(const FloatPoint & cp,const FloatPoint & p)199 void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& p)
200 {
201 CGPathAddQuadCurveToPoint(m_path, 0, cp.x(), cp.y(), p.x(), p.y());
202 }
203
addBezierCurveTo(const FloatPoint & cp1,const FloatPoint & cp2,const FloatPoint & p)204 void Path::addBezierCurveTo(const FloatPoint& cp1, const FloatPoint& cp2, const FloatPoint& p)
205 {
206 CGPathAddCurveToPoint(m_path, 0, cp1.x(), cp1.y(), cp2.x(), cp2.y(), p.x(), p.y());
207 }
208
addArcTo(const FloatPoint & p1,const FloatPoint & p2,float radius)209 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
210 {
211 CGPathAddArcToPoint(m_path, 0, p1.x(), p1.y(), p2.x(), p2.y(), radius);
212 }
213
closeSubpath()214 void Path::closeSubpath()
215 {
216 if (!CGPathIsEmpty(m_path)) // to silence a warning when trying to close an empty path
217 CGPathCloseSubpath(m_path);
218 }
219
addArc(const FloatPoint & p,float r,float sa,float ea,bool clockwise)220 void Path::addArc(const FloatPoint& p, float r, float sa, float ea, bool clockwise)
221 {
222 // Workaround for <rdar://problem/5189233> CGPathAddArc hangs or crashes when passed inf as start or end angle
223 if (isfinite(sa) && isfinite(ea))
224 CGPathAddArc(m_path, 0, p.x(), p.y(), r, sa, ea, clockwise);
225 }
226
addRect(const FloatRect & r)227 void Path::addRect(const FloatRect& r)
228 {
229 CGPathAddRect(m_path, 0, r);
230 }
231
addEllipse(const FloatRect & r)232 void Path::addEllipse(const FloatRect& r)
233 {
234 CGPathAddEllipseInRect(m_path, 0, r);
235 }
236
clear()237 void Path::clear()
238 {
239 CGPathRelease(m_path);
240 m_path = CGPathCreateMutable();
241 }
242
isEmpty() const243 bool Path::isEmpty() const
244 {
245 return CGPathIsEmpty(m_path);
246 }
247
hasCurrentPoint() const248 bool Path::hasCurrentPoint() const
249 {
250 return !isEmpty();
251 }
252
CGPathToCFStringApplierFunction(void * info,const CGPathElement * element)253 static void CGPathToCFStringApplierFunction(void* info, const CGPathElement *element)
254 {
255 CFMutableStringRef string = static_cast<CFMutableStringRef>(info);
256
257 CGPoint* points = element->points;
258 switch (element->type) {
259 case kCGPathElementMoveToPoint:
260 CFStringAppendFormat(string, 0, CFSTR("M%.2f,%.2f "), points[0].x, points[0].y);
261 break;
262 case kCGPathElementAddLineToPoint:
263 CFStringAppendFormat(string, 0, CFSTR("L%.2f,%.2f "), points[0].x, points[0].y);
264 break;
265 case kCGPathElementAddQuadCurveToPoint:
266 CFStringAppendFormat(string, 0, CFSTR("Q%.2f,%.2f,%.2f,%.2f "),
267 points[0].x, points[0].y, points[1].x, points[1].y);
268 break;
269 case kCGPathElementAddCurveToPoint:
270 CFStringAppendFormat(string, 0, CFSTR("C%.2f,%.2f,%.2f,%.2f,%.2f,%.2f "),
271 points[0].x, points[0].y, points[1].x, points[1].y,
272 points[2].x, points[2].y);
273 break;
274 case kCGPathElementCloseSubpath:
275 CFStringAppendFormat(string, 0, CFSTR("Z "));
276 break;
277 }
278 }
279
CFStringFromCGPath(CGPathRef path)280 static CFStringRef CFStringFromCGPath(CGPathRef path)
281 {
282 if (!path)
283 return 0;
284
285 CFMutableStringRef string = CFStringCreateMutable(NULL, 0);
286 CGPathApply(path, string, CGPathToCFStringApplierFunction);
287 CFStringTrimWhitespace(string);
288
289
290 return string;
291 }
292
293
294 #pragma mark -
295 #pragma mark Path Management
296
debugString() const297 String Path::debugString() const
298 {
299 String result;
300 if (!isEmpty()) {
301 CFStringRef pathString = CFStringFromCGPath(m_path);
302 result = String(pathString);
303 CFRelease(pathString);
304 }
305 return result;
306 }
307
308 struct PathApplierInfo {
309 void* info;
310 PathApplierFunction function;
311 };
312
CGPathApplierToPathApplier(void * info,const CGPathElement * element)313 static void CGPathApplierToPathApplier(void *info, const CGPathElement *element)
314 {
315 PathApplierInfo* pinfo = (PathApplierInfo*)info;
316 FloatPoint points[3];
317 PathElement pelement;
318 pelement.type = (PathElementType)element->type;
319 pelement.points = points;
320 CGPoint* cgPoints = element->points;
321 switch (element->type) {
322 case kCGPathElementMoveToPoint:
323 case kCGPathElementAddLineToPoint:
324 points[0] = cgPoints[0];
325 break;
326 case kCGPathElementAddQuadCurveToPoint:
327 points[0] = cgPoints[0];
328 points[1] = cgPoints[1];
329 break;
330 case kCGPathElementAddCurveToPoint:
331 points[0] = cgPoints[0];
332 points[1] = cgPoints[1];
333 points[2] = cgPoints[2];
334 break;
335 case kCGPathElementCloseSubpath:
336 break;
337 }
338 pinfo->function(pinfo->info, &pelement);
339 }
340
apply(void * info,PathApplierFunction function) const341 void Path::apply(void* info, PathApplierFunction function) const
342 {
343 PathApplierInfo pinfo;
344 pinfo.info = info;
345 pinfo.function = function;
346 CGPathApply(m_path, &pinfo, CGPathApplierToPathApplier);
347 }
348
transform(const AffineTransform & transform)349 void Path::transform(const AffineTransform& transform)
350 {
351 CGMutablePathRef path = CGPathCreateMutable();
352 CGAffineTransform transformCG = transform;
353 CGPathAddPath(path, &transformCG, m_path);
354 CGPathRelease(m_path);
355 m_path = path;
356 }
357
358 }
359
360 #endif // PLATFORM(CG)
361