summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/graphics/cg
diff options
context:
space:
mode:
authorUpstream <upstream-import@none>1970-01-12 13:46:40 +0000
committerUpstream <upstream-import@none>1970-01-12 13:46:40 +0000
commitd8543bb6618c17b12da906afa77d216f58cf4058 (patch)
treec58dc05ed86825bd0ef8d305d58c8205106b540f /WebCore/platform/graphics/cg
downloadexternal_webkit-d8543bb6618c17b12da906afa77d216f58cf4058.zip
external_webkit-d8543bb6618c17b12da906afa77d216f58cf4058.tar.gz
external_webkit-d8543bb6618c17b12da906afa77d216f58cf4058.tar.bz2
external/webkit r30707
Diffstat (limited to 'WebCore/platform/graphics/cg')
-rw-r--r--WebCore/platform/graphics/cg/AffineTransformCG.cpp216
-rw-r--r--WebCore/platform/graphics/cg/ColorCG.cpp96
-rw-r--r--WebCore/platform/graphics/cg/FloatPointCG.cpp47
-rw-r--r--WebCore/platform/graphics/cg/FloatRectCG.cpp47
-rw-r--r--WebCore/platform/graphics/cg/FloatSizeCG.cpp47
-rw-r--r--WebCore/platform/graphics/cg/GraphicsContextCG.cpp976
-rw-r--r--WebCore/platform/graphics/cg/GraphicsContextPlatformPrivateCG.h85
-rw-r--r--WebCore/platform/graphics/cg/ImageBufferCG.cpp212
-rw-r--r--WebCore/platform/graphics/cg/ImageCG.cpp283
-rw-r--r--WebCore/platform/graphics/cg/ImageSourceCG.cpp212
-rw-r--r--WebCore/platform/graphics/cg/IntPointCG.cpp46
-rw-r--r--WebCore/platform/graphics/cg/IntRectCG.cpp51
-rw-r--r--WebCore/platform/graphics/cg/IntSizeCG.cpp46
-rw-r--r--WebCore/platform/graphics/cg/PDFDocumentImage.cpp179
-rw-r--r--WebCore/platform/graphics/cg/PDFDocumentImage.h64
-rw-r--r--WebCore/platform/graphics/cg/PathCG.cpp289
16 files changed, 2896 insertions, 0 deletions
diff --git a/WebCore/platform/graphics/cg/AffineTransformCG.cpp b/WebCore/platform/graphics/cg/AffineTransformCG.cpp
new file mode 100644
index 0000000..8fdd1e6
--- /dev/null
+++ b/WebCore/platform/graphics/cg/AffineTransformCG.cpp
@@ -0,0 +1,216 @@
+/*
+ * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "AffineTransform.h"
+
+#if PLATFORM(CG)
+
+#include "FloatConversion.h"
+#include "FloatRect.h"
+#include "IntRect.h"
+
+#include <wtf/MathExtras.h>
+
+namespace WebCore {
+
+AffineTransform::AffineTransform()
+{
+ m_transform = CGAffineTransformIdentity;
+}
+
+AffineTransform::AffineTransform(double a, double b, double c, double d, double tx, double ty)
+{
+ m_transform = CGAffineTransformMake(narrowPrecisionToCGFloat(a),
+ narrowPrecisionToCGFloat(b),
+ narrowPrecisionToCGFloat(c),
+ narrowPrecisionToCGFloat(d),
+ narrowPrecisionToCGFloat(tx),
+ narrowPrecisionToCGFloat(ty));
+}
+
+AffineTransform::AffineTransform(CGAffineTransform t)
+{
+ m_transform = t;
+}
+
+void AffineTransform::setMatrix(double a, double b, double c, double d, double tx, double ty)
+{
+ m_transform = CGAffineTransformMake(narrowPrecisionToCGFloat(a),
+ narrowPrecisionToCGFloat(b),
+ narrowPrecisionToCGFloat(c),
+ narrowPrecisionToCGFloat(d),
+ narrowPrecisionToCGFloat(tx),
+ narrowPrecisionToCGFloat(ty));
+}
+
+void AffineTransform::map(double x, double y, double *x2, double *y2) const
+{
+ CGPoint result = CGPointApplyAffineTransform(CGPointMake(narrowPrecisionToCGFloat(x), narrowPrecisionToCGFloat(y)), m_transform);
+ *x2 = result.x;
+ *y2 = result.y;
+}
+
+IntRect AffineTransform::mapRect(const IntRect &rect) const
+{
+ return enclosingIntRect(CGRectApplyAffineTransform(CGRect(rect), m_transform));
+}
+
+FloatRect AffineTransform::mapRect(const FloatRect &rect) const
+{
+ return FloatRect(CGRectApplyAffineTransform(CGRect(rect), m_transform));
+}
+
+bool AffineTransform::isIdentity() const
+{
+ return CGAffineTransformIsIdentity(m_transform);
+}
+
+double AffineTransform::a() const
+{
+ return m_transform.a;
+}
+
+void AffineTransform::setA(double a)
+{
+ m_transform.a = narrowPrecisionToCGFloat(a);
+}
+
+double AffineTransform::b() const
+{
+ return m_transform.b;
+}
+
+void AffineTransform::setB(double b)
+{
+ m_transform.b = narrowPrecisionToCGFloat(b);
+}
+
+double AffineTransform::c() const
+{
+ return m_transform.c;
+}
+
+void AffineTransform::setC(double c)
+{
+ m_transform.c = narrowPrecisionToCGFloat(c);
+}
+
+double AffineTransform::d() const
+{
+ return m_transform.d;
+}
+
+void AffineTransform::setD(double d)
+{
+ m_transform.d = narrowPrecisionToCGFloat(d);
+}
+
+double AffineTransform::e() const
+{
+ return m_transform.tx;
+}
+
+void AffineTransform::setE(double e)
+{
+ m_transform.tx = narrowPrecisionToCGFloat(e);
+}
+
+double AffineTransform::f() const
+{
+ return m_transform.ty;
+}
+
+void AffineTransform::setF(double f)
+{
+ m_transform.ty = narrowPrecisionToCGFloat(f);
+}
+
+void AffineTransform::reset()
+{
+ m_transform = CGAffineTransformIdentity;
+}
+
+AffineTransform &AffineTransform::scale(double sx, double sy)
+{
+ m_transform = CGAffineTransformScale(m_transform, narrowPrecisionToCGFloat(sx), narrowPrecisionToCGFloat(sy));
+ return *this;
+}
+
+AffineTransform &AffineTransform::rotate(double d)
+{
+ m_transform = CGAffineTransformRotate(m_transform, narrowPrecisionToCGFloat(deg2rad(d)));
+ return *this;
+}
+
+AffineTransform &AffineTransform::translate(double tx, double ty)
+{
+ m_transform = CGAffineTransformTranslate(m_transform, narrowPrecisionToCGFloat(tx), narrowPrecisionToCGFloat(ty));
+ return *this;
+}
+
+AffineTransform &AffineTransform::shear(double sx, double sy)
+{
+ CGAffineTransform shear = CGAffineTransformMake(1.0f, narrowPrecisionToCGFloat(sy), narrowPrecisionToCGFloat(sx), 1.0f, 0.0f, 0.0f);
+ m_transform = CGAffineTransformConcat(shear, m_transform);
+ return *this;
+}
+
+double AffineTransform::det() const
+{
+ return m_transform.a * m_transform.d - m_transform.b * m_transform.c;
+}
+
+AffineTransform AffineTransform::inverse() const
+{
+ if (isInvertible())
+ return AffineTransform(CGAffineTransformInvert(m_transform));
+ return AffineTransform();
+}
+
+AffineTransform::operator CGAffineTransform() const
+{
+ return m_transform;
+}
+
+bool AffineTransform::operator== (const AffineTransform &m2) const
+{
+ return CGAffineTransformEqualToTransform(m_transform, CGAffineTransform(m2));
+}
+
+AffineTransform &AffineTransform::operator*= (const AffineTransform &m2)
+{
+ m_transform = CGAffineTransformConcat(m_transform, CGAffineTransform(m2));
+ return *this;
+}
+
+AffineTransform AffineTransform::operator* (const AffineTransform &m2)
+{
+ return CGAffineTransformConcat(m_transform, CGAffineTransform(m2));
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/ColorCG.cpp b/WebCore/platform/graphics/cg/ColorCG.cpp
new file mode 100644
index 0000000..48ce9f2
--- /dev/null
+++ b/WebCore/platform/graphics/cg/ColorCG.cpp
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "Color.h"
+
+#if PLATFORM(CG)
+
+#include <wtf/Assertions.h>
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+Color::Color(CGColorRef color)
+{
+ if (!color) {
+ m_color = 0;
+ m_valid = false;
+ return;
+ }
+
+ size_t numComponents = CGColorGetNumberOfComponents(color);
+ const CGFloat* components = CGColorGetComponents(color);
+
+ float r = 0;
+ float g = 0;
+ float b = 0;
+ float a = 0;
+
+ switch (numComponents) {
+ case 2:
+ r = g = b = components[0];
+ a = components[1];
+ break;
+ case 4:
+ r = components[0];
+ g = components[1];
+ b = components[2];
+ a = components[3];
+ break;
+ default:
+ ASSERT_NOT_REACHED();
+ }
+
+ m_color = makeRGBA(r * 255, g * 255, b * 255, a * 255);
+}
+
+#if !PLATFORM(MAC)
+
+CGColorRef cgColor(const Color& c)
+{
+ CGColorRef color = NULL;
+ CMProfileRef prof = NULL;
+ CMGetSystemProfile(&prof);
+
+ CGColorSpaceRef rgbSpace = CGColorSpaceCreateWithPlatformColorSpace(prof);
+
+ if (rgbSpace != NULL)
+ {
+ float components[4] = {c.red() / 255.0f, c.green() / 255.0f, c.blue() / 255.0f, c.alpha() / 255.0f};
+ color = CGColorCreate(rgbSpace, components);
+ CGColorSpaceRelease(rgbSpace);
+ }
+
+ CMCloseProfile(prof);
+
+ return color;
+}
+
+#endif // !PLATFORM(MAC)
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/FloatPointCG.cpp b/WebCore/platform/graphics/cg/FloatPointCG.cpp
new file mode 100644
index 0000000..f9c3353
--- /dev/null
+++ b/WebCore/platform/graphics/cg/FloatPointCG.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (C) 2005 Nokia. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "FloatPoint.h"
+
+#if PLATFORM(CG)
+
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+FloatPoint::FloatPoint(const CGPoint& p) : m_x(p.x), m_y(p.y)
+{
+}
+
+FloatPoint::operator CGPoint() const
+{
+ return CGPointMake(m_x, m_y);
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/FloatRectCG.cpp b/WebCore/platform/graphics/cg/FloatRectCG.cpp
new file mode 100644
index 0000000..a1ce367
--- /dev/null
+++ b/WebCore/platform/graphics/cg/FloatRectCG.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (C) 2005 Nokia. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "FloatRect.h"
+
+#if PLATFORM(CG)
+
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+FloatRect::FloatRect(const CGRect& r) : m_location(r.origin), m_size(r.size)
+{
+}
+
+FloatRect::operator CGRect() const
+{
+ return CGRectMake(x(), y(), width(), height());
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/FloatSizeCG.cpp b/WebCore/platform/graphics/cg/FloatSizeCG.cpp
new file mode 100644
index 0000000..383af21
--- /dev/null
+++ b/WebCore/platform/graphics/cg/FloatSizeCG.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (C) 2005 Nokia. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "FloatSize.h"
+
+#if PLATFORM(CG)
+
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+FloatSize::FloatSize(const CGSize& s) : m_width(s.width), m_height(s.height)
+{
+}
+
+FloatSize::operator CGSize() const
+{
+ return CGSizeMake(m_width, m_height);
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/GraphicsContextCG.cpp b/WebCore/platform/graphics/cg/GraphicsContextCG.cpp
new file mode 100644
index 0000000..de546ac
--- /dev/null
+++ b/WebCore/platform/graphics/cg/GraphicsContextCG.cpp
@@ -0,0 +1,976 @@
+/*
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define _USE_MATH_DEFINES 1
+#include "config.h"
+#include "GraphicsContext.h"
+
+#include "AffineTransform.h"
+#include "FloatConversion.h"
+#include "GraphicsContextPlatformPrivateCG.h"
+#include "ImageBuffer.h"
+#include "KURL.h"
+#include "Path.h"
+#include <CoreGraphics/CGBitmapContext.h>
+#include <CoreGraphics/CGPDFContext.h>
+#include <wtf/MathExtras.h>
+#include <wtf/OwnArrayPtr.h>
+#include <wtf/RetainPtr.h>
+
+using namespace std;
+
+namespace WebCore {
+
+static void setCGFillColor(CGContextRef context, const Color& color)
+{
+ CGFloat red, green, blue, alpha;
+ color.getRGBA(red, green, blue, alpha);
+ CGContextSetRGBFillColor(context, red, green, blue, alpha);
+}
+
+static void setCGStrokeColor(CGContextRef context, const Color& color)
+{
+ CGFloat red, green, blue, alpha;
+ color.getRGBA(red, green, blue, alpha);
+ CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
+}
+
+GraphicsContext::GraphicsContext(CGContextRef cgContext)
+ : m_common(createGraphicsContextPrivate())
+ , m_data(new GraphicsContextPlatformPrivate(cgContext))
+{
+ setPaintingDisabled(!cgContext);
+ if (cgContext) {
+ // Make sure the context starts in sync with our state.
+ setPlatformFillColor(fillColor());
+ setPlatformStrokeColor(strokeColor());
+ }
+}
+
+GraphicsContext::~GraphicsContext()
+{
+ destroyGraphicsContextPrivate(m_common);
+ delete m_data;
+}
+
+CGContextRef GraphicsContext::platformContext() const
+{
+ ASSERT(!paintingDisabled());
+ ASSERT(m_data->m_cgContext);
+ return m_data->m_cgContext;
+}
+
+void GraphicsContext::savePlatformState()
+{
+ // Note: Do not use this function within this class implementation, since we want to avoid the extra
+ // save of the secondary context (in GraphicsContextPlatformPrivateCG.h).
+ CGContextSaveGState(platformContext());
+ m_data->save();
+}
+
+void GraphicsContext::restorePlatformState()
+{
+ // Note: Do not use this function within this class implementation, since we want to avoid the extra
+ // restore of the secondary context (in GraphicsContextPlatformPrivateCG.h).
+ CGContextRestoreGState(platformContext());
+ m_data->restore();
+ m_data->m_userToDeviceTransformKnownToBeIdentity = false;
+}
+
+// Draws a filled rectangle with a stroked border.
+void GraphicsContext::drawRect(const IntRect& rect)
+{
+ if (paintingDisabled())
+ return;
+
+ CGContextRef context = platformContext();
+
+ if (fillColor().alpha())
+ CGContextFillRect(context, rect);
+
+ if (strokeStyle() != NoStroke && strokeColor().alpha()) {
+ // We do a fill of four rects to simulate the stroke of a border.
+ Color oldFillColor = fillColor();
+ if (oldFillColor != strokeColor())
+ setCGFillColor(context, strokeColor());
+ CGRect rects[4] = {
+ FloatRect(rect.x(), rect.y(), rect.width(), 1),
+ FloatRect(rect.x(), rect.bottom() - 1, rect.width(), 1),
+ FloatRect(rect.x(), rect.y() + 1, 1, rect.height() - 2),
+ FloatRect(rect.right() - 1, rect.y() + 1, 1, rect.height() - 2)
+ };
+ CGContextFillRects(context, rects, 4);
+ if (oldFillColor != strokeColor())
+ setCGFillColor(context, oldFillColor);
+ }
+}
+
+// This is only used to draw borders.
+void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
+{
+ if (paintingDisabled())
+ return;
+
+ if (strokeStyle() == NoStroke || !strokeColor().alpha())
+ return;
+
+ float width = strokeThickness();
+
+ FloatPoint p1 = point1;
+ FloatPoint p2 = point2;
+ bool isVerticalLine = (p1.x() == p2.x());
+
+ // For odd widths, we add in 0.5 to the appropriate x/y so that the float arithmetic
+ // works out. For example, with a border width of 3, KHTML will pass us (y1+y2)/2, e.g.,
+ // (50+53)/2 = 103/2 = 51 when we want 51.5. It is always true that an even width gave
+ // us a perfect position, but an odd width gave us a position that is off by exactly 0.5.
+ if (strokeStyle() == DottedStroke || strokeStyle() == DashedStroke) {
+ if (isVerticalLine) {
+ p1.move(0, width);
+ p2.move(0, -width);
+ } else {
+ p1.move(width, 0);
+ p2.move(-width, 0);
+ }
+ }
+
+ if (((int)width) % 2) {
+ if (isVerticalLine) {
+ // We're a vertical line. Adjust our x.
+ p1.move(0.5f, 0.0f);
+ p2.move(0.5f, 0.0f);
+ } else {
+ // We're a horizontal line. Adjust our y.
+ p1.move(0.0f, 0.5f);
+ p2.move(0.0f, 0.5f);
+ }
+ }
+
+ int patWidth = 0;
+ switch (strokeStyle()) {
+ case NoStroke:
+ case SolidStroke:
+ break;
+ case DottedStroke:
+ patWidth = (int)width;
+ break;
+ case DashedStroke:
+ patWidth = 3 * (int)width;
+ break;
+ }
+
+ CGContextRef context = platformContext();
+ CGContextSaveGState(context);
+
+ CGContextSetShouldAntialias(context, false);
+
+ if (patWidth) {
+ // Do a rect fill of our endpoints. This ensures we always have the
+ // appearance of being a border. We then draw the actual dotted/dashed line.
+ setCGFillColor(context, strokeColor()); // The save/restore make it safe to mutate the fill color here without setting it back to the old color.
+ if (isVerticalLine) {
+ CGContextFillRect(context, FloatRect(p1.x() - width / 2, p1.y() - width, width, width));
+ CGContextFillRect(context, FloatRect(p2.x() - width / 2, p2.y(), width, width));
+ } else {
+ CGContextFillRect(context, FloatRect(p1.x() - width, p1.y() - width / 2, width, width));
+ CGContextFillRect(context, FloatRect(p2.x(), p2.y() - width / 2, width, width));
+ }
+
+ // Example: 80 pixels with a width of 30 pixels.
+ // Remainder is 20. The maximum pixels of line we could paint
+ // will be 50 pixels.
+ int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2*(int)width;
+ int remainder = distance % patWidth;
+ int coverage = distance - remainder;
+ int numSegments = coverage / patWidth;
+
+ float patternOffset = 0.0f;
+ // Special case 1px dotted borders for speed.
+ if (patWidth == 1)
+ patternOffset = 1.0f;
+ else {
+ bool evenNumberOfSegments = numSegments % 2 == 0;
+ if (remainder)
+ evenNumberOfSegments = !evenNumberOfSegments;
+ if (evenNumberOfSegments) {
+ if (remainder) {
+ patternOffset += patWidth - remainder;
+ patternOffset += remainder / 2;
+ } else
+ patternOffset = patWidth / 2;
+ } else {
+ if (remainder)
+ patternOffset = (patWidth - remainder)/2;
+ }
+ }
+
+ const CGFloat dottedLine[2] = { patWidth, patWidth };
+ CGContextSetLineDash(context, patternOffset, dottedLine, 2);
+ }
+
+ CGContextBeginPath(context);
+ CGContextMoveToPoint(context, p1.x(), p1.y());
+ CGContextAddLineToPoint(context, p2.x(), p2.y());
+
+ CGContextStrokePath(context);
+
+ CGContextRestoreGState(context);
+}
+
+// This method is only used to draw the little circles used in lists.
+void GraphicsContext::drawEllipse(const IntRect& rect)
+{
+ // FIXME: CG added CGContextAddEllipseinRect in Tiger, so we should be able to quite easily draw an ellipse.
+ // This code can only handle circles, not ellipses. But khtml only
+ // uses it for circles.
+ ASSERT(rect.width() == rect.height());
+
+ if (paintingDisabled())
+ return;
+
+ CGContextRef context = platformContext();
+ CGContextBeginPath(context);
+ float r = (float)rect.width() / 2;
+ CGContextAddArc(context, rect.x() + r, rect.y() + r, r, 0.0f, 2.0f * piFloat, 0);
+ CGContextClosePath(context);
+
+ if (fillColor().alpha()) {
+ if (strokeStyle() != NoStroke)
+ // stroke and fill
+ CGContextDrawPath(context, kCGPathFillStroke);
+ else
+ CGContextFillPath(context);
+ } else if (strokeStyle() != NoStroke)
+ CGContextStrokePath(context);
+}
+
+
+void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
+{
+ if (paintingDisabled() || strokeStyle() == NoStroke || strokeThickness() <= 0.0f || !strokeColor().alpha())
+ return;
+
+ CGContextRef context = platformContext();
+ CGContextSaveGState(context);
+ CGContextBeginPath(context);
+ CGContextSetShouldAntialias(context, false);
+
+ int x = rect.x();
+ int y = rect.y();
+ float w = (float)rect.width();
+ float h = (float)rect.height();
+ float scaleFactor = h / w;
+ float reverseScaleFactor = w / h;
+
+ if (w != h)
+ scale(FloatSize(1, scaleFactor));
+
+ float hRadius = w / 2;
+ float vRadius = h / 2;
+ float fa = startAngle;
+ float falen = fa + angleSpan;
+ float start = -fa * piFloat / 180.0f;
+ float end = -falen * piFloat / 180.0f;
+ CGContextAddArc(context, x + hRadius, (y + vRadius) * reverseScaleFactor, hRadius, start, end, true);
+
+ if (w != h)
+ scale(FloatSize(1, reverseScaleFactor));
+
+
+ float width = strokeThickness();
+ int patWidth = 0;
+
+ switch (strokeStyle()) {
+ case DottedStroke:
+ patWidth = (int)(width / 2);
+ break;
+ case DashedStroke:
+ patWidth = 3 * (int)(width / 2);
+ break;
+ default:
+ break;
+ }
+
+ if (patWidth) {
+ // Example: 80 pixels with a width of 30 pixels.
+ // Remainder is 20. The maximum pixels of line we could paint
+ // will be 50 pixels.
+ int distance;
+ if (hRadius == vRadius)
+ distance = static_cast<int>((piFloat * hRadius) / 2.0f);
+ else // We are elliptical and will have to estimate the distance
+ distance = static_cast<int>((piFloat * sqrtf((hRadius * hRadius + vRadius * vRadius) / 2.0f)) / 2.0f);
+
+ int remainder = distance % patWidth;
+ int coverage = distance - remainder;
+ int numSegments = coverage / patWidth;
+
+ float patternOffset = 0.0f;
+ // Special case 1px dotted borders for speed.
+ if (patWidth == 1)
+ patternOffset = 1.0f;
+ else {
+ bool evenNumberOfSegments = numSegments % 2 == 0;
+ if (remainder)
+ evenNumberOfSegments = !evenNumberOfSegments;
+ if (evenNumberOfSegments) {
+ if (remainder) {
+ patternOffset += patWidth - remainder;
+ patternOffset += remainder / 2.0f;
+ } else
+ patternOffset = patWidth / 2.0f;
+ } else {
+ if (remainder)
+ patternOffset = (patWidth - remainder) / 2.0f;
+ }
+ }
+
+ const CGFloat dottedLine[2] = { patWidth, patWidth };
+ CGContextSetLineDash(context, patternOffset, dottedLine, 2);
+ }
+
+ CGContextStrokePath(context);
+
+ CGContextRestoreGState(context);
+}
+
+void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias)
+{
+ if (paintingDisabled() || !fillColor().alpha() && (strokeThickness() <= 0 || strokeStyle() == NoStroke))
+ return;
+
+ if (npoints <= 1)
+ return;
+
+ CGContextRef context = platformContext();
+
+ CGContextSaveGState(context);
+
+ CGContextSetShouldAntialias(context, shouldAntialias);
+
+ CGContextBeginPath(context);
+ CGContextMoveToPoint(context, points[0].x(), points[0].y());
+ for (size_t i = 1; i < npoints; i++)
+ CGContextAddLineToPoint(context, points[i].x(), points[i].y());
+ CGContextClosePath(context);
+
+ if (fillColor().alpha()) {
+ if (strokeStyle() != NoStroke)
+ CGContextDrawPath(context, kCGPathEOFillStroke);
+ else
+ CGContextEOFillPath(context);
+ } else
+ CGContextStrokePath(context);
+
+ CGContextRestoreGState(context);
+}
+
+void GraphicsContext::fillRect(const IntRect& rect, const Color& color)
+{
+ if (paintingDisabled())
+ return;
+ if (color.alpha()) {
+ CGContextRef context = platformContext();
+ Color oldFillColor = fillColor();
+ if (oldFillColor != color)
+ setCGFillColor(context, color);
+ CGContextFillRect(context, rect);
+ if (oldFillColor != color)
+ setCGFillColor(context, oldFillColor);
+ }
+}
+
+void GraphicsContext::fillRect(const FloatRect& rect, const Color& color)
+{
+ if (paintingDisabled())
+ return;
+ if (color.alpha()) {
+ CGContextRef context = platformContext();
+ Color oldFillColor = fillColor();
+ if (oldFillColor != color)
+ setCGFillColor(context, color);
+ CGContextFillRect(context, rect);
+ if (oldFillColor != color)
+ setCGFillColor(context, oldFillColor);
+ }
+}
+
+void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color)
+{
+ if (paintingDisabled() || !color.alpha())
+ return;
+
+ CGContextRef context = platformContext();
+ Color oldFillColor = fillColor();
+ if (oldFillColor != color)
+ setCGFillColor(context, color);
+
+ addPath(Path::createRoundedRectangle(rect, topLeft, topRight, bottomLeft, bottomRight));
+ CGContextFillPath(context);
+
+ if (oldFillColor != color)
+ setCGFillColor(context, oldFillColor);
+}
+
+
+void GraphicsContext::clip(const IntRect& rect)
+{
+ if (paintingDisabled())
+ return;
+ CGContextClipToRect(platformContext(), rect);
+ m_data->clip(rect);
+}
+
+void GraphicsContext::clipOut(const IntRect& rect)
+{
+ if (paintingDisabled())
+ return;
+
+ CGRect rects[2] = { CGContextGetClipBoundingBox(platformContext()), rect };
+ CGContextBeginPath(platformContext());
+ CGContextAddRects(platformContext(), rects, 2);
+ CGContextEOClip(platformContext());
+}
+
+void GraphicsContext::clipOutEllipseInRect(const IntRect& rect)
+{
+ if (paintingDisabled())
+ return;
+
+ CGContextBeginPath(platformContext());
+ CGContextAddRect(platformContext(), CGContextGetClipBoundingBox(platformContext()));
+ CGContextAddEllipseInRect(platformContext(), rect);
+ CGContextEOClip(platformContext());
+}
+
+void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
+{
+ if (paintingDisabled())
+ return;
+
+ clip(rect);
+ CGContextRef context = platformContext();
+
+ // Add outer ellipse
+ CGContextAddEllipseInRect(context, CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()));
+ // Add inner ellipse.
+ CGContextAddEllipseInRect(context, CGRectMake(rect.x() + thickness, rect.y() + thickness,
+ rect.width() - (thickness * 2), rect.height() - (thickness * 2)));
+
+ CGContextEOClip(context);
+}
+
+void GraphicsContext::beginTransparencyLayer(float opacity)
+{
+ if (paintingDisabled())
+ return;
+ CGContextRef context = platformContext();
+ CGContextSaveGState(context);
+ CGContextSetAlpha(context, opacity);
+ CGContextBeginTransparencyLayer(context, 0);
+ m_data->beginTransparencyLayer();
+ m_data->m_userToDeviceTransformKnownToBeIdentity = false;
+}
+
+void GraphicsContext::endTransparencyLayer()
+{
+ if (paintingDisabled())
+ return;
+ CGContextRef context = platformContext();
+ CGContextEndTransparencyLayer(context);
+ CGContextRestoreGState(context);
+ m_data->endTransparencyLayer();
+ m_data->m_userToDeviceTransformKnownToBeIdentity = false;
+}
+
+void GraphicsContext::setShadow(const IntSize& size, int blur, const Color& color)
+{
+ // Extreme "blur" values can make text drawing crash or take crazy long times, so clamp
+ blur = min(blur, 1000);
+
+ if (paintingDisabled())
+ return;
+ CGContextRef context = platformContext();
+
+ CGFloat width = size.width();
+ CGFloat height = size.height();
+
+#ifdef BUILDING_ON_TIGER
+ // Work around <rdar://problem/5539388> by ensuring that the offsets will get truncated
+ // to the desired integer.
+ static const CGFloat extraShadowOffset = narrowPrecisionToCGFloat(1.0 / 128);
+ if (width > 0)
+ width += extraShadowOffset;
+ else if (width < 0)
+ width -= extraShadowOffset;
+
+ if (height > 0)
+ height += extraShadowOffset;
+ else if (height < 0)
+ height -= extraShadowOffset;
+#endif
+
+ // Check for an invalid color, as this means that the color was not set for the shadow
+ // and we should therefore just use the default shadow color.
+ if (!color.isValid())
+ CGContextSetShadow(context, CGSizeMake(width, -height), blur); // y is flipped.
+ else {
+ CGColorRef colorCG = cgColor(color);
+ CGContextSetShadowWithColor(context,
+ CGSizeMake(width, -height), // y is flipped.
+ blur,
+ colorCG);
+ CGColorRelease(colorCG);
+ }
+}
+
+void GraphicsContext::clearShadow()
+{
+ if (paintingDisabled())
+ return;
+ CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
+}
+
+void GraphicsContext::setMiterLimit(float limit)
+{
+ if (paintingDisabled())
+ return;
+ CGContextSetMiterLimit(platformContext(), limit);
+}
+
+void GraphicsContext::setAlpha(float alpha)
+{
+ if (paintingDisabled())
+ return;
+ CGContextSetAlpha(platformContext(), alpha);
+}
+
+void GraphicsContext::clearRect(const FloatRect& r)
+{
+ if (paintingDisabled())
+ return;
+ CGContextClearRect(platformContext(), r);
+}
+
+void GraphicsContext::strokeRect(const FloatRect& r, float lineWidth)
+{
+ if (paintingDisabled())
+ return;
+ CGContextStrokeRectWithWidth(platformContext(), r, lineWidth);
+}
+
+void GraphicsContext::setLineCap(LineCap cap)
+{
+ if (paintingDisabled())
+ return;
+ switch (cap) {
+ case ButtCap:
+ CGContextSetLineCap(platformContext(), kCGLineCapButt);
+ break;
+ case RoundCap:
+ CGContextSetLineCap(platformContext(), kCGLineCapRound);
+ break;
+ case SquareCap:
+ CGContextSetLineCap(platformContext(), kCGLineCapSquare);
+ break;
+ }
+}
+
+void GraphicsContext::setLineJoin(LineJoin join)
+{
+ if (paintingDisabled())
+ return;
+ switch (join) {
+ case MiterJoin:
+ CGContextSetLineJoin(platformContext(), kCGLineJoinMiter);
+ break;
+ case RoundJoin:
+ CGContextSetLineJoin(platformContext(), kCGLineJoinRound);
+ break;
+ case BevelJoin:
+ CGContextSetLineJoin(platformContext(), kCGLineJoinBevel);
+ break;
+ }
+}
+
+void GraphicsContext::beginPath()
+{
+ CGContextBeginPath(platformContext());
+}
+
+void GraphicsContext::addPath(const Path& path)
+{
+ CGContextAddPath(platformContext(), path.platformPath());
+}
+
+void GraphicsContext::clip(const Path& path)
+{
+ if (paintingDisabled())
+ return;
+ CGContextRef context = platformContext();
+ CGContextBeginPath(context);
+ CGContextAddPath(context, path.platformPath());
+ CGContextClip(context);
+ m_data->clip(path);
+}
+
+void GraphicsContext::clipOut(const Path& path)
+{
+ if (paintingDisabled())
+ return;
+
+ CGContextBeginPath(platformContext());
+ CGContextAddRect(platformContext(), CGContextGetClipBoundingBox(platformContext()));
+ CGContextAddPath(platformContext(), path.platformPath());
+ CGContextEOClip(platformContext());
+}
+
+void GraphicsContext::scale(const FloatSize& size)
+{
+ if (paintingDisabled())
+ return;
+ CGContextScaleCTM(platformContext(), size.width(), size.height());
+ m_data->scale(size);
+ m_data->m_userToDeviceTransformKnownToBeIdentity = false;
+}
+
+void GraphicsContext::rotate(float angle)
+{
+ if (paintingDisabled())
+ return;
+ CGContextRotateCTM(platformContext(), angle);
+ m_data->rotate(angle);
+ m_data->m_userToDeviceTransformKnownToBeIdentity = false;
+}
+
+void GraphicsContext::translate(float x, float y)
+{
+ if (paintingDisabled())
+ return;
+ CGContextTranslateCTM(platformContext(), x, y);
+ m_data->translate(x, y);
+ m_data->m_userToDeviceTransformKnownToBeIdentity = false;
+}
+
+void GraphicsContext::concatCTM(const AffineTransform& transform)
+{
+ if (paintingDisabled())
+ return;
+ CGContextConcatCTM(platformContext(), transform);
+ m_data->concatCTM(transform);
+ m_data->m_userToDeviceTransformKnownToBeIdentity = false;
+}
+
+AffineTransform GraphicsContext::getCTM() const
+{
+ return CGContextGetCTM(platformContext());
+}
+
+FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect)
+{
+ // It is not enough just to round to pixels in device space. The rotation part of the
+ // affine transform matrix to device space can mess with this conversion if we have a
+ // rotating image like the hands of the world clock widget. We just need the scale, so
+ // we get the affine transform matrix and extract the scale.
+
+ if (m_data->m_userToDeviceTransformKnownToBeIdentity)
+ return rect;
+
+ CGAffineTransform deviceMatrix = CGContextGetUserSpaceToDeviceSpaceTransform(platformContext());
+ if (CGAffineTransformIsIdentity(deviceMatrix)) {
+ m_data->m_userToDeviceTransformKnownToBeIdentity = true;
+ return rect;
+ }
+
+ float deviceScaleX = sqrtf(deviceMatrix.a * deviceMatrix.a + deviceMatrix.b * deviceMatrix.b);
+ float deviceScaleY = sqrtf(deviceMatrix.c * deviceMatrix.c + deviceMatrix.d * deviceMatrix.d);
+
+ CGPoint deviceOrigin = CGPointMake(rect.x() * deviceScaleX, rect.y() * deviceScaleY);
+ CGPoint deviceLowerRight = CGPointMake((rect.x() + rect.width()) * deviceScaleX,
+ (rect.y() + rect.height()) * deviceScaleY);
+
+ deviceOrigin.x = roundf(deviceOrigin.x);
+ deviceOrigin.y = roundf(deviceOrigin.y);
+ deviceLowerRight.x = roundf(deviceLowerRight.x);
+ deviceLowerRight.y = roundf(deviceLowerRight.y);
+
+ // Don't let the height or width round to 0 unless either was originally 0
+ if (deviceOrigin.y == deviceLowerRight.y && rect.height() != 0)
+ deviceLowerRight.y += 1;
+ if (deviceOrigin.x == deviceLowerRight.x && rect.width() != 0)
+ deviceLowerRight.x += 1;
+
+ FloatPoint roundedOrigin = FloatPoint(deviceOrigin.x / deviceScaleX, deviceOrigin.y / deviceScaleY);
+ FloatPoint roundedLowerRight = FloatPoint(deviceLowerRight.x / deviceScaleX, deviceLowerRight.y / deviceScaleY);
+ return FloatRect(roundedOrigin, roundedLowerRight - roundedOrigin);
+}
+
+void GraphicsContext::drawLineForText(const IntPoint& point, int width, bool printing)
+{
+ if (paintingDisabled())
+ return;
+
+ if (width <= 0)
+ return;
+
+ CGContextSaveGState(platformContext());
+
+ float x = point.x();
+ float y = point.y();
+ float lineLength = width;
+
+ // Use a minimum thickness of 0.5 in user space.
+ // See http://bugs.webkit.org/show_bug.cgi?id=4255 for details of why 0.5 is the right minimum thickness to use.
+ float thickness = max(strokeThickness(), 0.5f);
+
+ if (!printing) {
+ // On screen, use a minimum thickness of 1.0 in user space (later rounded to an integral number in device space).
+ float adjustedThickness = max(thickness, 1.0f);
+
+ // FIXME: This should be done a better way.
+ // We try to round all parameters to integer boundaries in device space. If rounding pixels in device space
+ // makes our thickness more than double, then there must be a shrinking-scale factor and rounding to pixels
+ // in device space will make the underlines too thick.
+ CGRect lineRect = roundToDevicePixels(FloatRect(x, y, lineLength, adjustedThickness));
+ if (lineRect.size.height < thickness * 2.0) {
+ x = lineRect.origin.x;
+ y = lineRect.origin.y;
+ lineLength = lineRect.size.width;
+ thickness = lineRect.size.height;
+ CGContextSetShouldAntialias(platformContext(), false);
+ }
+ }
+
+ if (fillColor() != strokeColor())
+ setCGFillColor(platformContext(), strokeColor());
+ CGContextFillRect(platformContext(), CGRectMake(x, y, lineLength, thickness));
+
+ CGContextRestoreGState(platformContext());
+}
+
+void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
+{
+ if (paintingDisabled())
+ return;
+
+ CFURLRef urlRef = link.createCFURL();
+ if (urlRef) {
+ CGContextRef context = platformContext();
+
+ // Get the bounding box to handle clipping.
+ CGRect box = CGContextGetClipBoundingBox(context);
+
+ IntRect intBox((int)box.origin.x, (int)box.origin.y, (int)box.size.width, (int)box.size.height);
+ IntRect rect = destRect;
+ rect.intersect(intBox);
+
+ CGPDFContextSetURLForRect(context, urlRef,
+ CGRectApplyAffineTransform(rect, CGContextGetCTM(context)));
+
+ CFRelease(urlRef);
+ }
+}
+
+void GraphicsContext::setUseLowQualityImageInterpolation(bool lowQualityMode)
+{
+ if (paintingDisabled())
+ return;
+
+ CGContextSetInterpolationQuality(platformContext(), lowQualityMode ? kCGInterpolationNone : kCGInterpolationDefault);
+}
+
+bool GraphicsContext::useLowQualityImageInterpolation() const
+{
+ if (paintingDisabled())
+ return false;
+
+ return CGContextGetInterpolationQuality(platformContext());
+}
+
+void GraphicsContext::setPlatformTextDrawingMode(int mode)
+{
+ if (paintingDisabled())
+ return;
+
+ // Wow, wish CG had used bits here.
+ CGContextRef context = platformContext();
+ switch (mode) {
+ case cTextInvisible: // Invisible
+ CGContextSetTextDrawingMode(context, kCGTextInvisible);
+ break;
+ case cTextFill: // Fill
+ CGContextSetTextDrawingMode(context, kCGTextFill);
+ break;
+ case cTextStroke: // Stroke
+ CGContextSetTextDrawingMode(context, kCGTextStroke);
+ break;
+ case 3: // Fill | Stroke
+ CGContextSetTextDrawingMode(context, kCGTextFillStroke);
+ break;
+ case cTextClip: // Clip
+ CGContextSetTextDrawingMode(context, kCGTextClip);
+ break;
+ case 5: // Fill | Clip
+ CGContextSetTextDrawingMode(context, kCGTextFillClip);
+ break;
+ case 6: // Stroke | Clip
+ CGContextSetTextDrawingMode(context, kCGTextStrokeClip);
+ break;
+ case 7: // Fill | Stroke | Clip
+ CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip);
+ break;
+ default:
+ break;
+ }
+}
+
+void GraphicsContext::setPlatformStrokeColor(const Color& color)
+{
+ if (paintingDisabled())
+ return;
+ setCGStrokeColor(platformContext(), color);
+}
+
+void GraphicsContext::setPlatformStrokeThickness(float thickness)
+{
+ if (paintingDisabled())
+ return;
+ CGContextSetLineWidth(platformContext(), thickness);
+}
+
+void GraphicsContext::setPlatformFillColor(const Color& color)
+{
+ if (paintingDisabled())
+ return;
+ setCGFillColor(platformContext(), color);
+}
+
+void GraphicsContext::setUseAntialiasing(bool enable)
+{
+ if (paintingDisabled())
+ return;
+ CGContextSetShouldAntialias(platformContext(), enable);
+}
+
+#ifndef BUILDING_ON_TIGER // Tiger's setCompositeOperation() is defined in GraphicsContextMac.mm.
+void GraphicsContext::setCompositeOperation(CompositeOperator mode)
+{
+ if (paintingDisabled())
+ return;
+
+ CGBlendMode target = kCGBlendModeNormal;
+ switch (mode) {
+ case CompositeClear:
+ target = kCGBlendModeClear;
+ break;
+ case CompositeCopy:
+ target = kCGBlendModeCopy;
+ break;
+ case CompositeSourceOver:
+ //kCGBlendModeNormal
+ break;
+ case CompositeSourceIn:
+ target = kCGBlendModeSourceIn;
+ break;
+ case CompositeSourceOut:
+ target = kCGBlendModeSourceOut;
+ break;
+ case CompositeSourceAtop:
+ target = kCGBlendModeSourceAtop;
+ break;
+ case CompositeDestinationOver:
+ target = kCGBlendModeDestinationOver;
+ break;
+ case CompositeDestinationIn:
+ target = kCGBlendModeDestinationIn;
+ break;
+ case CompositeDestinationOut:
+ target = kCGBlendModeDestinationOut;
+ break;
+ case CompositeDestinationAtop:
+ target = kCGBlendModeDestinationAtop;
+ break;
+ case CompositeXOR:
+ target = kCGBlendModeXOR;
+ break;
+ case CompositePlusDarker:
+ target = kCGBlendModePlusDarker;
+ break;
+ case CompositeHighlight:
+ // currently unsupported
+ break;
+ case CompositePlusLighter:
+ target = kCGBlendModePlusLighter;
+ break;
+ }
+ CGContextSetBlendMode(platformContext(), target);
+}
+#endif
+
+void GraphicsContext::paintBuffer(ImageBuffer* buffer, const IntRect& r)
+{
+ CGContextRef context = buffer->context()->platformContext();
+ if (!context)
+ return;
+ CGContextFlush(context);
+ if (CGImageRef image = CGBitmapContextCreateImage(context)) {
+ CGContextDrawImage(platformContext(), roundToDevicePixels(r), image);
+ CGImageRelease(image);
+ }
+}
+
+void GraphicsContext::drawImage(ImageBuffer* buffer, const FloatRect& srcRect, const FloatRect& destRect)
+{
+ CGContextRef context = buffer->context()->platformContext();
+ CGContextFlush(context);
+ RetainPtr<CGImageRef> image(AdoptCF, CGBitmapContextCreateImage(context));
+ float iw = CGImageGetWidth(image.get());
+ float ih = CGImageGetHeight(image.get());
+ if (srcRect.x() == 0 && srcRect.y() == 0 && iw == srcRect.width() && ih == srcRect.height()) {
+ // Fast path, yay!
+ CGContextDrawImage(platformContext(), destRect, image.get());
+ } else {
+ // Slow path, boo!
+ // FIXME: We can do this without creating a separate image
+
+ size_t csw = static_cast<size_t>(ceilf(srcRect.width()));
+ size_t csh = static_cast<size_t>(ceilf(srcRect.height()));
+
+ RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
+ size_t bytesPerRow = csw * 4;
+ OwnArrayPtr<char> buffer(new char[csh * bytesPerRow]);
+ RetainPtr<CGContextRef> clippedSourceContext(AdoptCF, CGBitmapContextCreate(buffer.get(), csw, csh,
+ 8, bytesPerRow, colorSpace.get(), kCGImageAlphaPremultipliedLast));
+ CGContextTranslateCTM(clippedSourceContext.get(), -srcRect.x(), -srcRect.y());
+ CGContextDrawImage(clippedSourceContext.get(), CGRectMake(0, 0, iw, ih), image.get());
+
+ RetainPtr<CGImageRef> clippedSourceImage(AdoptCF, CGBitmapContextCreateImage(clippedSourceContext.get()));
+
+ CGContextDrawImage(platformContext(), destRect, clippedSourceImage.get());
+ }
+}
+
+}
+
diff --git a/WebCore/platform/graphics/cg/GraphicsContextPlatformPrivateCG.h b/WebCore/platform/graphics/cg/GraphicsContextPlatformPrivateCG.h
new file mode 100644
index 0000000..937481b
--- /dev/null
+++ b/WebCore/platform/graphics/cg/GraphicsContextPlatformPrivateCG.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <CoreGraphics/CGContext.h>
+
+namespace WebCore {
+
+class GraphicsContextPlatformPrivate {
+public:
+ GraphicsContextPlatformPrivate(CGContextRef cgContext)
+ : m_cgContext(cgContext)
+#if PLATFORM(WIN)
+ , m_hdc(0)
+ , m_transparencyCount(0)
+#endif
+ , m_userToDeviceTransformKnownToBeIdentity(false)
+ {
+ CGContextRetain(m_cgContext);
+ }
+
+ ~GraphicsContextPlatformPrivate()
+ {
+ CGContextRelease(m_cgContext);
+ }
+
+#if PLATFORM(MAC)
+ // These methods do nothing on Mac.
+ void save() {}
+ void restore() {}
+ void clip(const IntRect&) {}
+ void clip(const Path&) {}
+ void scale(const FloatSize&) {}
+ void rotate(float) {}
+ void translate(float, float) {}
+ void concatCTM(const AffineTransform&) {}
+ void beginTransparencyLayer() {}
+ void endTransparencyLayer() {}
+#endif
+
+#if PLATFORM(WIN)
+ // On Windows, we need to update the HDC for form controls to draw in the right place.
+ void save();
+ void restore();
+ void clip(const IntRect&);
+ void clip(const Path&);
+ void scale(const FloatSize&);
+ void rotate(float);
+ void translate(float, float);
+ void concatCTM(const AffineTransform&);
+ void beginTransparencyLayer() { m_transparencyCount++; }
+ void endTransparencyLayer() { m_transparencyCount--; }
+#endif
+
+#if PLATFORM(WIN)
+ HDC m_hdc;
+ unsigned m_transparencyCount;
+#endif
+
+ CGContextRef m_cgContext;
+ bool m_userToDeviceTransformKnownToBeIdentity;
+};
+
+}
diff --git a/WebCore/platform/graphics/cg/ImageBufferCG.cpp b/WebCore/platform/graphics/cg/ImageBufferCG.cpp
new file mode 100644
index 0000000..2e48ceb
--- /dev/null
+++ b/WebCore/platform/graphics/cg/ImageBufferCG.cpp
@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
+ * Copyright (C) 2008 Apple, Inc
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "ImageBuffer.h"
+
+#include "GraphicsContext.h"
+#include "ImageData.h"
+
+#include <ApplicationServices/ApplicationServices.h>
+#include <wtf/Assertions.h>
+
+using namespace std;
+
+namespace WebCore {
+
+auto_ptr<ImageBuffer> ImageBuffer::create(const IntSize& size, bool grayScale)
+{
+ if (size.width() < 0 || size.height() < 0)
+ return auto_ptr<ImageBuffer>();
+ unsigned int bytesPerRow = size.width();
+ if (!grayScale) {
+ // Protect against overflow
+ if (bytesPerRow > 0x3FFFFFFF)
+ return auto_ptr<ImageBuffer>();
+ bytesPerRow *= 4;
+ }
+
+ void* imageBuffer = fastCalloc(size.height(), bytesPerRow);
+ if (!imageBuffer)
+ return auto_ptr<ImageBuffer>();
+
+ CGColorSpaceRef colorSpace = grayScale ? CGColorSpaceCreateDeviceGray() : CGColorSpaceCreateDeviceRGB();
+ CGContextRef cgContext = CGBitmapContextCreate(imageBuffer, size.width(), size.height(), 8, bytesPerRow,
+ colorSpace, grayScale ? kCGImageAlphaNone : kCGImageAlphaPremultipliedLast);
+ CGColorSpaceRelease(colorSpace);
+ if (!cgContext) {
+ fastFree(imageBuffer);
+ return auto_ptr<ImageBuffer>();
+ }
+
+ auto_ptr<GraphicsContext> context(new GraphicsContext(cgContext));
+ CGContextRelease(cgContext);
+
+ return auto_ptr<ImageBuffer>(new ImageBuffer(imageBuffer, size, context));
+}
+
+
+ImageBuffer::ImageBuffer(void* imageData, const IntSize& size, auto_ptr<GraphicsContext> context)
+ : m_data(imageData)
+ , m_size(size)
+ , m_context(context.release())
+ , m_cgImage(0)
+{
+ ASSERT((reinterpret_cast<size_t>(imageData) & 2) == 0);
+}
+
+ImageBuffer::~ImageBuffer()
+{
+ fastFree(m_data);
+ CGImageRelease(m_cgImage);
+}
+
+GraphicsContext* ImageBuffer::context() const
+{
+ return m_context.get();
+}
+
+CGImageRef ImageBuffer::cgImage() const
+{
+ // It's assumed that if cgImage() is called, the actual rendering to the
+ // contained GraphicsContext must be done, as we create the CGImageRef here.
+ if (!m_cgImage) {
+ ASSERT(context());
+ m_cgImage = CGBitmapContextCreateImage(context()->platformContext());
+ }
+
+ return m_cgImage;
+}
+
+PassRefPtr<ImageData> ImageBuffer::getImageData(const IntRect& rect) const
+{
+ if (!m_data)
+ return 0;
+
+ PassRefPtr<ImageData> result = ImageData::create(rect.width(), rect.height());
+ unsigned char* data = result->data()->data().data();
+
+ if (rect.x() < 0 || rect.y() < 0 || (rect.x() + rect.width()) > m_size.width() || (rect.y() + rect.height()) > m_size.height())
+ memset(data, 0, result->data()->length());
+
+ int originx = rect.x();
+ int destx = 0;
+ if (originx < 0) {
+ destx = -originx;
+ originx = 0;
+ }
+ int endx = rect.x() + rect.width();
+ if (endx > m_size.width())
+ endx = m_size.width();
+ int numColumns = endx - originx;
+
+ int originy = rect.y();
+ int desty = 0;
+ if (originy < 0) {
+ desty = -originy;
+ originy = 0;
+ }
+ int endy = rect.y() + rect.height();
+ if (endy > m_size.height())
+ endy = m_size.height();
+ int numRows = endy - originy;
+
+ unsigned srcBytesPerRow = 4 * m_size.width();
+ unsigned destBytesPerRow = 4 * rect.width();
+
+ // m_size.height() - originy to handle the accursed flipped y axis in CG backing store
+ unsigned char* srcRows = reinterpret_cast<unsigned char*>(m_data) + (m_size.height() - originy - 1) * srcBytesPerRow + originx * 4;
+ unsigned char* destRows = data + desty * destBytesPerRow + destx * 4;
+ for (int y = 0; y < numRows; ++y) {
+ for (int x = 0; x < numColumns; x++) {
+ if (unsigned char alpha = srcRows[3]) {
+ destRows[0] = (srcRows[0] * 255) / alpha;
+ destRows[1] = (srcRows[1] * 255) / alpha;
+ destRows[2] = (srcRows[2] * 255) / alpha;
+ destRows[3] = alpha;
+ } else {
+ reinterpret_cast<uint32_t*>(destRows)[0] = reinterpret_cast<uint32_t*>(srcRows)[0];
+ }
+ destRows += 4;
+ }
+ srcRows -= srcBytesPerRow;
+ }
+ return result;
+}
+
+void ImageBuffer::putImageData(ImageData* source, const IntRect& sourceRect, const IntPoint& destPoint)
+{
+ ASSERT(sourceRect.width() > 0);
+ ASSERT(sourceRect.height() > 0);
+
+ int originx = sourceRect.x();
+ int destx = destPoint.x() + sourceRect.x();
+ ASSERT(destx >= 0);
+ ASSERT(destx < m_size.width());
+ ASSERT(originx >= 0);
+ ASSERT(originx <= sourceRect.right());
+
+ int endx = destPoint.x() + sourceRect.right();
+ ASSERT(endx <= m_size.width());
+
+ int numColumns = endx - destx;
+
+ int originy = sourceRect.y();
+ int desty = destPoint.y() + sourceRect.y();
+ ASSERT(desty >= 0);
+ ASSERT(desty < m_size.height());
+ ASSERT(originy >= 0);
+ ASSERT(originy <= sourceRect.bottom());
+
+ int endy = destPoint.y() + sourceRect.bottom();
+ ASSERT(endx <= m_size.height());
+ int numRows = endy - desty;
+
+ unsigned srcBytesPerRow = 4 * source->width();
+ unsigned destBytesPerRow = 4 * m_size.width();
+
+ unsigned char* srcRows = source->data()->data().data() + originy * srcBytesPerRow + originx * 4;
+
+ // -desty to handle the accursed flipped y axis
+ unsigned char* destRows = reinterpret_cast<unsigned char*>(m_data) + (m_size.height() - desty - 1) * destBytesPerRow + destx * 4;
+ for (int y = 0; y < numRows; ++y) {
+ for (int x = 0; x < numColumns; x++) {
+ unsigned char alpha = srcRows[x * 4 + 3];
+ if (alpha != 255) {
+ destRows[x * 4 + 0] = (srcRows[0] * alpha) / 255;
+ destRows[x * 4 + 1] = (srcRows[1] * alpha) / 255;
+ destRows[x * 4 + 2] = (srcRows[2] * alpha) / 255;
+ destRows[x * 4 + 3] = alpha;
+ } else {
+ reinterpret_cast<uint32_t*>(destRows + x * 4)[0] = reinterpret_cast<uint32_t*>(srcRows + x * 4)[0];
+ }
+ }
+ destRows -= destBytesPerRow;
+ srcRows += srcBytesPerRow;
+ }
+}
+
+}
diff --git a/WebCore/platform/graphics/cg/ImageCG.cpp b/WebCore/platform/graphics/cg/ImageCG.cpp
new file mode 100644
index 0000000..5958d86
--- /dev/null
+++ b/WebCore/platform/graphics/cg/ImageCG.cpp
@@ -0,0 +1,283 @@
+/*
+ * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "BitmapImage.h"
+
+#if PLATFORM(CG)
+
+#include "AffineTransform.h"
+#include "FloatConversion.h"
+#include "FloatRect.h"
+#include "GraphicsContext.h"
+#include "ImageObserver.h"
+#include "PDFDocumentImage.h"
+#include "PlatformString.h"
+#include <ApplicationServices/ApplicationServices.h>
+
+#if PLATFORM(MAC)
+#include "WebCoreSystemInterface.h"
+#endif
+
+#if PLATFORM(WIN)
+#include <WebKitSystemInterface/WebKitSystemInterface.h>
+#endif
+
+namespace WebCore {
+
+void FrameData::clear()
+{
+ if (m_frame) {
+ CGImageRelease(m_frame);
+ m_frame = 0;
+ m_duration = 0.0f;
+ m_hasAlpha = true;
+ }
+}
+
+// ================================================
+// Image Class
+// ================================================
+
+// Drawing Routines
+
+void BitmapImage::checkForSolidColor()
+{
+ if (frameCount() > 1)
+ m_isSolidColor = false;
+ else {
+ CGImageRef image = frameAtIndex(0);
+
+ // Currently we only check for solid color in the important special case of a 1x1 image.
+ if (image && CGImageGetWidth(image) == 1 && CGImageGetHeight(image) == 1) {
+ unsigned char pixel[4]; // RGBA
+ CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
+ CGContextRef bmap = CGBitmapContextCreate(pixel, 1, 1, 8, sizeof(pixel), space,
+ kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
+ if (bmap) {
+ GraphicsContext(bmap).setCompositeOperation(CompositeCopy);
+ CGRect dst = { {0, 0}, {1, 1} };
+ CGContextDrawImage(bmap, dst, image);
+ if (pixel[3] == 0)
+ m_solidColor = Color(0, 0, 0, 0);
+ else
+ m_solidColor = Color(pixel[0] * 255 / pixel[3], pixel[1] * 255 / pixel[3], pixel[2] * 255 / pixel[3], pixel[3]);
+ m_isSolidColor = true;
+ CFRelease(bmap);
+ }
+ CFRelease(space);
+ }
+ }
+}
+
+CGImageRef BitmapImage::getCGImageRef()
+{
+ return frameAtIndex(0);
+}
+
+void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp)
+{
+ if (!m_source.initialized())
+ return;
+
+ CGRect fr = ctxt->roundToDevicePixels(srcRect);
+ CGRect ir = ctxt->roundToDevicePixels(dstRect);
+
+ CGImageRef image = frameAtIndex(m_currentFrame);
+ if (!image) // If it's too early we won't have an image yet.
+ return;
+
+ if (mayFillWithSolidColor()) {
+ fillWithSolidColor(ctxt, ir, solidColor(), compositeOp);
+ return;
+ }
+
+ // Get the height (in adjusted, i.e. scaled, coords) of the portion of the image
+ // that is currently decoded. This could be less that the actual height.
+ CGSize selfSize = size(); // full image size, in pixels
+ float curHeight = CGImageGetHeight(image); // height of loaded portion, in pixels
+
+ CGSize adjustedSize = selfSize;
+ if (curHeight < selfSize.height) {
+ adjustedSize.height *= curHeight / selfSize.height;
+
+ // Is the amount of available bands less than what we need to draw? If so,
+ // we may have to clip 'fr' if it goes outside the available bounds.
+ if (CGRectGetMaxY(fr) > adjustedSize.height) {
+ float frHeight = adjustedSize.height - fr.origin.y; // clip fr to available bounds
+ if (frHeight <= 0)
+ return; // clipped out entirely
+ ir.size.height *= (frHeight / fr.size.height); // scale ir proportionally to fr
+ fr.size.height = frHeight;
+ }
+ }
+
+ CGContextRef context = ctxt->platformContext();
+ ctxt->save();
+
+ // Flip the coords.
+ ctxt->setCompositeOperation(compositeOp);
+ CGContextTranslateCTM(context, ir.origin.x, ir.origin.y);
+ CGContextScaleCTM(context, 1, -1);
+ CGContextTranslateCTM(context, 0, -ir.size.height);
+
+ // Translated to origin, now draw at 0,0.
+ ir.origin.x = ir.origin.y = 0;
+
+ // If we're drawing a sub portion of the image then create
+ // a image for the sub portion and draw that.
+ // Test using example site at http://www.meyerweb.com/eric/css/edge/complexspiral/demo.html
+ if (fr.size.width != adjustedSize.width || fr.size.height != adjustedSize.height) {
+ // Convert ft to image pixel coords:
+ float xscale = adjustedSize.width / selfSize.width;
+ float yscale = adjustedSize.height / curHeight; // yes, curHeight, not selfSize.height!
+ fr.origin.x /= xscale;
+ fr.origin.y /= yscale;
+ fr.size.width /= xscale;
+ fr.size.height /= yscale;
+
+ image = CGImageCreateWithImageInRect(image, fr);
+ if (image) {
+ CGContextDrawImage(context, ir, image);
+ CFRelease(image);
+ }
+ } else // Draw the whole image.
+ CGContextDrawImage(context, ir, image);
+
+ ctxt->restore();
+
+ startAnimation();
+
+ if (imageObserver())
+ imageObserver()->didDraw(this);
+}
+
+void Image::drawPatternCallback(void* info, CGContextRef context)
+{
+ CGImageRef image = (CGImageRef)info;
+ CGContextDrawImage(context, GraphicsContext(context).roundToDevicePixels(FloatRect(0, 0, CGImageGetWidth(image), CGImageGetHeight(image))), image);
+}
+
+void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& patternTransform,
+ const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect)
+{
+ ASSERT(patternTransform.isInvertible());
+ if (!patternTransform.isInvertible())
+ // Avoid a hang under CGContextDrawTiledImage on release builds.
+ return;
+
+ CGContextRef context = ctxt->platformContext();
+ ctxt->save();
+ CGContextClipToRect(context, destRect);
+ ctxt->setCompositeOperation(op);
+ CGContextTranslateCTM(context, destRect.x(), destRect.y());
+ CGContextScaleCTM(context, 1, -1);
+ CGContextTranslateCTM(context, 0, -destRect.height());
+
+ // Compute the scaled tile size.
+ float scaledTileHeight = tileRect.height() * narrowPrecisionToFloat(patternTransform.d());
+
+ // We have to adjust the phase to deal with the fact we're in Cartesian space now (with the bottom left corner of destRect being
+ // the origin).
+ float adjustedX = phase.x() - destRect.x() + tileRect.x() * narrowPrecisionToFloat(patternTransform.a()); // We translated the context so that destRect.x() is the origin, so subtract it out.
+ float adjustedY = destRect.height() - (phase.y() - destRect.y() + tileRect.y() * narrowPrecisionToFloat(patternTransform.d()) + scaledTileHeight);
+
+ CGImageRef tileImage = nativeImageForCurrentFrame();
+ float h = CGImageGetHeight(tileImage);
+
+ CGImageRef subImage;
+ if (tileRect.size() == size())
+ subImage = tileImage;
+ else {
+ // Copying a sub-image out of a partially-decoded image stops the decoding of the original image. It should never happen
+ // because sub-images are only used for border-image, which only renders when the image is fully decoded.
+ ASSERT(h == height());
+ subImage = CGImageCreateWithImageInRect(tileImage, tileRect);
+ }
+
+#ifndef BUILDING_ON_TIGER
+ // Leopard has an optimized call for the tiling of image patterns, but we can only use it if the image has been decoded enough that
+ // its buffer is the same size as the overall image. Because a partially decoded CGImageRef with a smaller width or height than the
+ // overall image buffer needs to tile with "gaps", we can't use the optimized tiling call in that case.
+ // FIXME: Could create WebKitSystemInterface SPI for CGCreatePatternWithImage2 and probably make Tiger tile faster as well.
+ float scaledTileWidth = tileRect.width() * narrowPrecisionToFloat(patternTransform.a());
+ float w = CGImageGetWidth(tileImage);
+ if (w == size().width() && h == size().height())
+ CGContextDrawTiledImage(context, FloatRect(adjustedX, adjustedY, scaledTileWidth, scaledTileHeight), subImage);
+ else {
+#endif
+
+ // On Leopard, this code now only runs for partially decoded images whose buffers do not yet match the overall size of the image.
+ // On Tiger this code runs all the time. This code is suboptimal because the pattern does not reference the image directly, and the
+ // pattern is destroyed before exiting the function. This means any decoding the pattern does doesn't end up cached anywhere, so we
+ // redecode every time we paint.
+ static const CGPatternCallbacks patternCallbacks = { 0, drawPatternCallback, NULL };
+ CGAffineTransform matrix = CGAffineTransformMake(narrowPrecisionToCGFloat(patternTransform.a()), 0, 0, narrowPrecisionToCGFloat(patternTransform.d()), adjustedX, adjustedY);
+ matrix = CGAffineTransformConcat(matrix, CGContextGetCTM(context));
+ // The top of a partially-decoded image is drawn at the bottom of the tile. Map it to the top.
+ matrix = CGAffineTransformTranslate(matrix, 0, size().height() - h);
+ CGPatternRef pattern = CGPatternCreate(subImage, CGRectMake(0, 0, tileRect.width(), tileRect.height()),
+ matrix, tileRect.width(), tileRect.height(),
+ kCGPatternTilingConstantSpacing, true, &patternCallbacks);
+ if (pattern == NULL) {
+ if (subImage != tileImage)
+ CGImageRelease(subImage);
+ ctxt->restore();
+ return;
+ }
+
+ CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
+
+ CGFloat alpha = 1;
+ CGColorRef color = CGColorCreateWithPattern(patternSpace, pattern, &alpha);
+ CGContextSetFillColorSpace(context, patternSpace);
+ CGColorSpaceRelease(patternSpace);
+ CGPatternRelease(pattern);
+
+ // FIXME: Really want a public API for this. It is just CGContextSetBaseCTM(context, CGAffineTransformIdentiy).
+ wkSetPatternBaseCTM(context, CGAffineTransformIdentity);
+ CGContextSetPatternPhase(context, CGSizeZero);
+
+ CGContextSetFillColorWithColor(context, color);
+ CGContextFillRect(context, CGContextGetClipBoundingBox(context));
+
+ CGColorRelease(color);
+
+#ifndef BUILDING_ON_TIGER
+ }
+#endif
+
+ if (subImage != tileImage)
+ CGImageRelease(subImage);
+ ctxt->restore();
+
+ if (imageObserver())
+ imageObserver()->didDraw(this);
+}
+
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/ImageSourceCG.cpp b/WebCore/platform/graphics/cg/ImageSourceCG.cpp
new file mode 100644
index 0000000..08e8172
--- /dev/null
+++ b/WebCore/platform/graphics/cg/ImageSourceCG.cpp
@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "ImageSource.h"
+
+#if PLATFORM(CG)
+
+#include "IntSize.h"
+#include "SharedBuffer.h"
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+static const CFStringRef kCGImageSourceShouldPreferRGB32 = CFSTR("kCGImageSourceShouldPreferRGB32");
+
+ImageSource::ImageSource()
+ : m_decoder(0)
+{
+}
+
+ImageSource::~ImageSource()
+{
+ clear();
+}
+
+void ImageSource::clear()
+{
+ if (m_decoder) {
+ CFRelease(m_decoder);
+ m_decoder = 0;
+ }
+}
+
+CFDictionaryRef imageSourceOptions()
+{
+ static CFDictionaryRef options;
+
+ if (!options) {
+ const void* keys[2] = { kCGImageSourceShouldCache, kCGImageSourceShouldPreferRGB32 };
+ const void* values[2] = { kCFBooleanTrue, kCFBooleanTrue };
+ options = CFDictionaryCreate(NULL, keys, values, 2,
+ &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+ }
+ return options;
+}
+
+bool ImageSource::initialized() const
+{
+ return m_decoder;
+}
+
+void ImageSource::setData(SharedBuffer* data, bool allDataReceived)
+{
+ if (!m_decoder)
+ m_decoder = CGImageSourceCreateIncremental(NULL);
+#if PLATFORM(MAC)
+ // On Mac the NSData inside the SharedBuffer can be secretly appended to without the SharedBuffer's knowledge. We use SharedBuffer's ability
+ // to wrap itself inside CFData to get around this, ensuring that ImageIO is really looking at the SharedBuffer.
+ CFDataRef cfData = data->createCFData();
+#else
+ // If no NSData is available, then we know SharedBuffer will always just be a vector. That means no secret changes can occur to it behind the
+ // scenes. We use CFDataCreateWithBytesNoCopy in that case.
+ CFDataRef cfData = CFDataCreateWithBytesNoCopy(0, reinterpret_cast<const UInt8*>(data->data()), data->size(), kCFAllocatorNull);
+#endif
+ CGImageSourceUpdateData(m_decoder, cfData, allDataReceived);
+ CFRelease(cfData);
+}
+
+bool ImageSource::isSizeAvailable()
+{
+ bool result = false;
+ CGImageSourceStatus imageSourceStatus = CGImageSourceGetStatus(m_decoder);
+
+ // Ragnaros yells: TOO SOON! You have awakened me TOO SOON, Executus!
+ if (imageSourceStatus >= kCGImageStatusIncomplete) {
+ CFDictionaryRef image0Properties = CGImageSourceCopyPropertiesAtIndex(m_decoder, 0, imageSourceOptions());
+ if (image0Properties) {
+ CFNumberRef widthNumber = (CFNumberRef)CFDictionaryGetValue(image0Properties, kCGImagePropertyPixelWidth);
+ CFNumberRef heightNumber = (CFNumberRef)CFDictionaryGetValue(image0Properties, kCGImagePropertyPixelHeight);
+ result = widthNumber && heightNumber;
+ CFRelease(image0Properties);
+ }
+ }
+
+ return result;
+}
+
+IntSize ImageSource::size() const
+{
+ IntSize result;
+ CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(m_decoder, 0, imageSourceOptions());
+ if (properties) {
+ int w = 0, h = 0;
+ CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
+ if (num)
+ CFNumberGetValue(num, kCFNumberIntType, &w);
+ num = (CFNumberRef)CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
+ if (num)
+ CFNumberGetValue(num, kCFNumberIntType, &h);
+ result = IntSize(w, h);
+ CFRelease(properties);
+ }
+ return result;
+}
+
+int ImageSource::repetitionCount()
+{
+ int result = cAnimationLoopOnce; // No property means loop once.
+
+ // A property with value 0 means loop forever.
+ CFDictionaryRef properties = CGImageSourceCopyProperties(m_decoder, imageSourceOptions());
+ if (properties) {
+ CFDictionaryRef gifProperties = (CFDictionaryRef)CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
+ if (gifProperties) {
+ CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFLoopCount);
+ if (num)
+ CFNumberGetValue(num, kCFNumberIntType, &result);
+ } else
+ result = cAnimationNone; // Turns out we're not a GIF after all, so we don't animate.
+
+ CFRelease(properties);
+ }
+
+ return result;
+}
+
+size_t ImageSource::frameCount() const
+{
+ return m_decoder ? CGImageSourceGetCount(m_decoder) : 0;
+}
+
+CGImageRef ImageSource::createFrameAtIndex(size_t index)
+{
+ CGImageRef image = CGImageSourceCreateImageAtIndex(m_decoder, index, imageSourceOptions());
+ CFStringRef imageUTI = CGImageSourceGetType(m_decoder);
+ static const CFStringRef xbmUTI = CFSTR("public.xbitmap-image");
+ if (!imageUTI || !CFEqual(imageUTI, xbmUTI))
+ return image;
+
+ // If it is an xbm image, mask out all the white areas to render them transparent.
+ const CGFloat maskingColors[6] = {255, 255, 255, 255, 255, 255};
+ CGImageRef maskedImage = CGImageCreateWithMaskingColors(image, maskingColors);
+ if (!maskedImage)
+ return image;
+
+ CGImageRelease(image);
+ return maskedImage;
+}
+
+bool ImageSource::frameIsCompleteAtIndex(size_t index)
+{
+ return CGImageSourceGetStatusAtIndex(m_decoder, index) == kCGImageStatusComplete;
+}
+
+float ImageSource::frameDurationAtIndex(size_t index)
+{
+ float duration = 0;
+ CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(m_decoder, index, imageSourceOptions());
+ if (properties) {
+ CFDictionaryRef typeProperties = (CFDictionaryRef)CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
+ if (typeProperties) {
+ CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(typeProperties, kCGImagePropertyGIFDelayTime);
+ if (num)
+ CFNumberGetValue(num, kCFNumberFloatType, &duration);
+ }
+ CFRelease(properties);
+ }
+
+ // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
+ // We follow WinIE's behavior and use a duration of 100 ms for any frames that specify
+ // a duration of <= 50 ms. See <http://bugs.webkit.org/show_bug.cgi?id=14413> or Radar 4051389 for more.
+ if (duration < 0.051f)
+ return 0.100f;
+ return duration;
+}
+
+bool ImageSource::frameHasAlphaAtIndex(size_t index)
+{
+ // Might be interesting to do this optimization on Mac some day, but for now we're just using this
+ // for the Cairo source, since it uses our decoders, and our decoders can answer this question.
+ // FIXME: Could return false for JPEG and other non-transparent image formats.
+ // FIXME: Could maybe return false for a GIF Frame if we have enough info in the GIF properties dictionary
+ // to determine whether or not a transparent color was defined.
+ return true;
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/IntPointCG.cpp b/WebCore/platform/graphics/cg/IntPointCG.cpp
new file mode 100644
index 0000000..95dbe5f
--- /dev/null
+++ b/WebCore/platform/graphics/cg/IntPointCG.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "IntPoint.h"
+
+#if PLATFORM(CG)
+
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+IntPoint::IntPoint(const CGPoint& p) : m_x(static_cast<int>(p.x)), m_y(static_cast<int>(p.y))
+{
+}
+
+IntPoint::operator CGPoint() const
+{
+ return CGPointMake(m_x, m_y);
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/IntRectCG.cpp b/WebCore/platform/graphics/cg/IntRectCG.cpp
new file mode 100644
index 0000000..73fd63f
--- /dev/null
+++ b/WebCore/platform/graphics/cg/IntRectCG.cpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "IntRect.h"
+
+#if PLATFORM(CG)
+
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+IntRect::operator CGRect() const
+{
+ return CGRectMake(x(), y(), width(), height());
+}
+
+IntRect enclosingIntRect(const CGRect& rect)
+{
+ int l = static_cast<int>(floorf(rect.origin.x));
+ int t = static_cast<int>(floorf(rect.origin.y));
+ int r = static_cast<int>(ceilf(CGRectGetMaxX(rect)));
+ int b = static_cast<int>(ceilf(CGRectGetMaxY(rect)));
+ return IntRect(l, t, r - l, b - t);
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/IntSizeCG.cpp b/WebCore/platform/graphics/cg/IntSizeCG.cpp
new file mode 100644
index 0000000..d8e8c83
--- /dev/null
+++ b/WebCore/platform/graphics/cg/IntSizeCG.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "IntSize.h"
+
+#if PLATFORM(CG)
+
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+IntSize::IntSize(const CGSize& s) : m_width(static_cast<int>(s.width)), m_height(static_cast<int>(s.height))
+{
+}
+
+IntSize::operator CGSize() const
+{
+ return CGSizeMake(m_width, m_height);
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/PDFDocumentImage.cpp b/WebCore/platform/graphics/cg/PDFDocumentImage.cpp
new file mode 100644
index 0000000..2578f08
--- /dev/null
+++ b/WebCore/platform/graphics/cg/PDFDocumentImage.cpp
@@ -0,0 +1,179 @@
+/*
+ * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define _USE_MATH_DEFINES 1
+#include "config.h"
+#include "PDFDocumentImage.h"
+
+#if PLATFORM(CG)
+
+#include "GraphicsContext.h"
+#include "ImageObserver.h"
+#include <wtf/MathExtras.h>
+
+using namespace std;
+
+namespace WebCore {
+
+PDFDocumentImage::PDFDocumentImage()
+ : Image(0) // PDFs don't animate
+ , m_document(0)
+ , m_rotation(0.0f)
+ , m_currentPage(-1)
+{
+}
+
+PDFDocumentImage::~PDFDocumentImage()
+{
+ CGPDFDocumentRelease(m_document);
+}
+
+IntSize PDFDocumentImage::size() const
+{
+ const float sina = sinf(-m_rotation);
+ const float cosa = cosf(-m_rotation);
+ const float width = m_mediaBox.size().width();
+ const float height = m_mediaBox.size().height();
+ const float rotWidth = width * cosa - height * sina;
+ const float rotHeight = width * sina + height * cosa;
+
+ return IntSize((int)(fabsf(rotWidth) + 0.5f), (int)(fabsf(rotHeight) + 0.5f));
+}
+
+bool PDFDocumentImage::dataChanged(bool allDataReceived)
+{
+ if (allDataReceived && !m_document) {
+#if PLATFORM(MAC)
+ // On Mac the NSData inside the SharedBuffer can be secretly appended to without the SharedBuffer's knowledge. We use SharedBuffer's ability
+ // to wrap itself inside CFData to get around this, ensuring that ImageIO is really looking at the SharedBuffer.
+ CFDataRef data = m_data->createCFData();
+#else
+ // If no NSData is available, then we know SharedBuffer will always just be a vector. That means no secret changes can occur to it behind the
+ // scenes. We use CFDataCreateWithBytesNoCopy in that case.
+ CFDataRef data = CFDataCreateWithBytesNoCopy(0, reinterpret_cast<const UInt8*>(m_data->data()), m_data->size(), kCFAllocatorNull);
+#endif
+ CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(data);
+ CFRelease(data);
+ m_document = CGPDFDocumentCreateWithProvider(dataProvider);
+ CGDataProviderRelease(dataProvider);
+ setCurrentPage(0);
+ }
+ return m_document; // return true if size is available
+}
+
+void PDFDocumentImage::adjustCTM(GraphicsContext* context) const
+{
+ // rotate the crop box and calculate bounding box
+ float sina = sinf(-m_rotation);
+ float cosa = cosf(-m_rotation);
+ float width = m_cropBox.width();
+ float height = m_cropBox.height();
+
+ // calculate rotated x and y edges of the corp box. if they're negative, it means part of the image has
+ // been rotated outside of the bounds and we need to shift over the image so it lies inside the bounds again
+ CGPoint rx = CGPointMake(width * cosa, width * sina);
+ CGPoint ry = CGPointMake(-height * sina, height * cosa);
+
+ // adjust so we are at the crop box origin
+ const CGFloat zero = 0;
+ CGContextTranslateCTM(context->platformContext(), floorf(-min(zero, min(rx.x, ry.x))), floorf(-min(zero, min(rx.y, ry.y))));
+
+ // rotate -ve to remove rotation
+ CGContextRotateCTM(context->platformContext(), -m_rotation);
+
+ // shift so we are completely within media box
+ CGContextTranslateCTM(context->platformContext(), m_mediaBox.x() - m_cropBox.x(), m_mediaBox.y() - m_cropBox.y());
+}
+
+void PDFDocumentImage::setCurrentPage(int page)
+{
+ if (!m_document)
+ return;
+
+ if (page == m_currentPage)
+ return;
+
+ if (!(page >= 0 && page < pageCount()))
+ return;
+
+ m_currentPage = page;
+
+ CGPDFPageRef cgPage = CGPDFDocumentGetPage(m_document, page + 1);
+
+ // get media box (guaranteed)
+ m_mediaBox = CGPDFPageGetBoxRect(cgPage, kCGPDFMediaBox);
+
+ // get crop box (not always there). if not, use media box
+ CGRect r = CGPDFPageGetBoxRect(cgPage, kCGPDFCropBox);
+ if (!CGRectIsEmpty(r))
+ m_cropBox = r;
+ else
+ m_cropBox = m_mediaBox;
+
+ // get page rotation angle
+ m_rotation = CGPDFPageGetRotationAngle(cgPage) * piFloat / 180.0f; // to radians
+}
+
+int PDFDocumentImage::pageCount() const
+{
+ return m_document ? CGPDFDocumentGetNumberOfPages(m_document) : 0;
+}
+
+void PDFDocumentImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator op)
+{
+ if (!m_document || m_currentPage == -1)
+ return;
+
+ context->save();
+
+ context->setCompositeOperation(op);
+
+ float hScale = dstRect.width() / srcRect.width();
+ float vScale = dstRect.height() / srcRect.height();
+
+ // Scale and translate so the document is rendered in the correct location,
+ // including accounting for the fact that a GraphicsContext is always flipped
+ // and doing appropriate flipping.
+ CGContextTranslateCTM(context->platformContext(), dstRect.x() - srcRect.x() * hScale, dstRect.y() - srcRect.y() * vScale);
+ CGContextScaleCTM(context->platformContext(), hScale, vScale);
+ CGContextScaleCTM(context->platformContext(), 1, -1);
+ CGContextTranslateCTM(context->platformContext(), 0, -srcRect.height());
+ CGContextClipToRect(context->platformContext(), CGRectIntegral(srcRect));
+
+ // Rotate translate image into position according to doc properties.
+ adjustCTM(context);
+
+ CGContextTranslateCTM(context->platformContext(), -m_mediaBox.x(), -m_mediaBox.y());
+ CGContextDrawPDFPage(context->platformContext(), CGPDFDocumentGetPage(m_document, m_currentPage + 1));
+
+ context->restore();
+
+ if (imageObserver())
+ imageObserver()->didDraw(this);
+}
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/PDFDocumentImage.h b/WebCore/platform/graphics/cg/PDFDocumentImage.h
new file mode 100644
index 0000000..caddc05
--- /dev/null
+++ b/WebCore/platform/graphics/cg/PDFDocumentImage.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "Image.h"
+
+#include "FloatRect.h"
+#include "GraphicsTypes.h"
+
+#if PLATFORM(CG)
+
+#include <ApplicationServices/ApplicationServices.h>
+
+namespace WebCore {
+
+ class GraphicsContext;
+
+ class PDFDocumentImage : public Image {
+ public:
+ PDFDocumentImage();
+ ~PDFDocumentImage();
+
+ virtual bool dataChanged(bool allDataReceived);
+
+ virtual IntSize size() const;
+
+ private:
+ virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator);
+
+ void setCurrentPage(int);
+ int pageCount() const;
+ void adjustCTM(GraphicsContext*) const;
+
+ CGPDFDocumentRef m_document;
+ FloatRect m_mediaBox;
+ FloatRect m_cropBox;
+ float m_rotation;
+ int m_currentPage;
+ };
+
+}
+
+#endif // PLATFORM(CG)
diff --git a/WebCore/platform/graphics/cg/PathCG.cpp b/WebCore/platform/graphics/cg/PathCG.cpp
new file mode 100644
index 0000000..c0a0caf
--- /dev/null
+++ b/WebCore/platform/graphics/cg/PathCG.cpp
@@ -0,0 +1,289 @@
+/*
+ * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
+ * 2006 Rob Buis <buis@kde.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "Path.h"
+
+#if PLATFORM(CG)
+
+#include "AffineTransform.h"
+#include <ApplicationServices/ApplicationServices.h>
+#include "FloatRect.h"
+#include "IntRect.h"
+#include "PlatformString.h"
+
+#include <wtf/MathExtras.h>
+
+namespace WebCore {
+
+Path::Path()
+ : m_path(CGPathCreateMutable())
+{
+}
+
+Path::~Path()
+{
+ CGPathRelease(m_path);
+}
+
+Path::Path(const Path& other)
+ : m_path(CGPathCreateMutableCopy(other.m_path))
+{
+}
+
+Path& Path::operator=(const Path& other)
+{
+ CGMutablePathRef path = CGPathCreateMutableCopy(other.m_path);
+ CGPathRelease(m_path);
+ m_path = path;
+ return *this;
+}
+
+
+static void copyClosingSubpathsApplierFunction(void* info, const CGPathElement* element)
+{
+ CGMutablePathRef path = static_cast<CGMutablePathRef>(info);
+ CGPoint* points = element->points;
+
+ switch (element->type) {
+ case kCGPathElementMoveToPoint:
+ if (!CGPathIsEmpty(path)) // to silence a warning when trying to close an empty path
+ CGPathCloseSubpath(path); // This is the only change from CGPathCreateMutableCopy
+ CGPathMoveToPoint(path, 0, points[0].x, points[0].y);
+ break;
+ case kCGPathElementAddLineToPoint:
+ CGPathAddLineToPoint(path, 0, points[0].x, points[0].y);
+ break;
+ case kCGPathElementAddQuadCurveToPoint:
+ CGPathAddQuadCurveToPoint(path, 0, points[0].x, points[0].y, points[1].x, points[1].y);
+ break;
+ case kCGPathElementAddCurveToPoint:
+ CGPathAddCurveToPoint(path, 0, points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y);
+ break;
+ case kCGPathElementCloseSubpath:
+ CGPathCloseSubpath(path);
+ break;
+ }
+}
+
+static CGMutablePathRef copyCGPathClosingSubpaths(CGPathRef originalPath)
+{
+ CGMutablePathRef path = CGPathCreateMutable();
+ CGPathApply(originalPath, path, copyClosingSubpathsApplierFunction);
+ CGPathCloseSubpath(path);
+ return path;
+}
+
+bool Path::contains(const FloatPoint &point, WindRule rule) const
+{
+ if (!boundingRect().contains(point))
+ return false;
+
+ // 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
+ CGMutablePathRef path = copyCGPathClosingSubpaths(m_path);
+ bool ret = CGPathContainsPoint(path, 0, point, rule == RULE_EVENODD ? true : false);
+ CGPathRelease(path);
+ return ret;
+}
+
+void Path::translate(const FloatSize& size)
+{
+ CGAffineTransform translation = CGAffineTransformMake(1, 0, 0, 1, size.width(), size.height());
+ CGMutablePathRef newPath = CGPathCreateMutable();
+ CGPathAddPath(newPath, &translation, m_path);
+ CGPathRelease(m_path);
+ m_path = newPath;
+}
+
+FloatRect Path::boundingRect() const
+{
+ return CGPathGetBoundingBox(m_path);
+}
+
+void Path::moveTo(const FloatPoint& point)
+{
+ CGPathMoveToPoint(m_path, 0, point.x(), point.y());
+}
+
+void Path::addLineTo(const FloatPoint& p)
+{
+ CGPathAddLineToPoint(m_path, 0, p.x(), p.y());
+}
+
+void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& p)
+{
+ CGPathAddQuadCurveToPoint(m_path, 0, cp.x(), cp.y(), p.x(), p.y());
+}
+
+void Path::addBezierCurveTo(const FloatPoint& cp1, const FloatPoint& cp2, const FloatPoint& p)
+{
+ CGPathAddCurveToPoint(m_path, 0, cp1.x(), cp1.y(), cp2.x(), cp2.y(), p.x(), p.y());
+}
+
+void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
+{
+ CGPathAddArcToPoint(m_path, 0, p1.x(), p1.y(), p2.x(), p2.y(), radius);
+}
+
+void Path::closeSubpath()
+{
+ if (!CGPathIsEmpty(m_path)) // to silence a warning when trying to close an empty path
+ CGPathCloseSubpath(m_path);
+}
+
+void Path::addArc(const FloatPoint& p, float r, float sa, float ea, bool clockwise)
+{
+ // Workaround for <rdar://problem/5189233> CGPathAddArc hangs or crashes when passed inf as start or end angle
+ if (isfinite(sa) && isfinite(ea))
+ CGPathAddArc(m_path, 0, p.x(), p.y(), r, sa, ea, clockwise);
+}
+
+void Path::addRect(const FloatRect& r)
+{
+ CGPathAddRect(m_path, 0, r);
+}
+
+void Path::addEllipse(const FloatRect& r)
+{
+ CGPathAddEllipseInRect(m_path, 0, r);
+}
+
+void Path::clear()
+{
+ CGPathRelease(m_path);
+ m_path = CGPathCreateMutable();
+}
+
+bool Path::isEmpty() const
+{
+ return CGPathIsEmpty(m_path);
+ }
+
+static void CGPathToCFStringApplierFunction(void* info, const CGPathElement *element)
+{
+ CFMutableStringRef string = (CFMutableStringRef)info;
+ CFStringRef typeString = CFSTR("");
+ CGPoint* points = element->points;
+ switch (element->type) {
+ case kCGPathElementMoveToPoint:
+ CFStringAppendFormat(string, 0, CFSTR("M%.2f,%.2f"), points[0].x, points[0].y);
+ break;
+ case kCGPathElementAddLineToPoint:
+ CFStringAppendFormat(string, 0, CFSTR("L%.2f,%.2f"), points[0].x, points[0].y);
+ break;
+ case kCGPathElementAddQuadCurveToPoint:
+ CFStringAppendFormat(string, 0, CFSTR("Q%.2f,%.2f,%.2f,%.2f"),
+ points[0].x, points[0].y, points[1].x, points[1].y);
+ break;
+ case kCGPathElementAddCurveToPoint:
+ CFStringAppendFormat(string, 0, CFSTR("C%.2f,%.2f,%.2f,%.2f,%.2f,%.2f"),
+ points[0].x, points[0].y, points[1].x, points[1].y,
+ points[2].x, points[2].y);
+ break;
+ case kCGPathElementCloseSubpath:
+ typeString = CFSTR("X"); break;
+ }
+}
+
+static CFStringRef CFStringFromCGPath(CGPathRef path)
+{
+ if (!path)
+ return 0;
+
+ CFMutableStringRef string = CFStringCreateMutable(NULL, 0);
+ CGPathApply(path, string, CGPathToCFStringApplierFunction);
+
+ return string;
+}
+
+
+#pragma mark -
+#pragma mark Path Management
+
+String Path::debugString() const
+{
+ String result;
+ if (!isEmpty()) {
+ CFStringRef pathString = CFStringFromCGPath(m_path);
+ result = String(pathString);
+ CFRelease(pathString);
+ }
+ return result;
+}
+
+struct PathApplierInfo {
+ void* info;
+ PathApplierFunction function;
+};
+
+void CGPathApplierToPathApplier(void *info, const CGPathElement *element)
+{
+ PathApplierInfo* pinfo = (PathApplierInfo*)info;
+ FloatPoint points[3];
+ PathElement pelement;
+ pelement.type = (PathElementType)element->type;
+ pelement.points = points;
+ CGPoint* cgPoints = element->points;
+ switch (element->type) {
+ case kCGPathElementMoveToPoint:
+ case kCGPathElementAddLineToPoint:
+ points[0] = cgPoints[0];
+ break;
+ case kCGPathElementAddQuadCurveToPoint:
+ points[0] = cgPoints[0];
+ points[1] = cgPoints[1];
+ break;
+ case kCGPathElementAddCurveToPoint:
+ points[0] = cgPoints[0];
+ points[1] = cgPoints[1];
+ points[2] = cgPoints[2];
+ break;
+ case kCGPathElementCloseSubpath:
+ break;
+ }
+ pinfo->function(pinfo->info, &pelement);
+}
+
+void Path::apply(void* info, PathApplierFunction function) const
+{
+ PathApplierInfo pinfo;
+ pinfo.info = info;
+ pinfo.function = function;
+ CGPathApply(m_path, &pinfo, CGPathApplierToPathApplier);
+}
+
+void Path::transform(const AffineTransform& transform)
+{
+ CGMutablePathRef path = CGPathCreateMutable();
+ CGAffineTransform transformCG = transform;
+ CGPathAddPath(path, &transformCG, m_path);
+ CGPathRelease(m_path);
+ m_path = path;
+}
+
+}
+
+#endif // PLATFORM(CG)