gfx/2d/PathCG.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/PathCG.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,336 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "PathCG.h"
    1.10 +#include <math.h>
    1.11 +#include "DrawTargetCG.h"
    1.12 +#include "Logging.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +namespace gfx {
    1.16 +
    1.17 +PathBuilderCG::~PathBuilderCG()
    1.18 +{
    1.19 +  CGPathRelease(mCGPath);
    1.20 +}
    1.21 +
    1.22 +void
    1.23 +PathBuilderCG::MoveTo(const Point &aPoint)
    1.24 +{
    1.25 +  CGPathMoveToPoint(mCGPath, nullptr, aPoint.x, aPoint.y);
    1.26 +}
    1.27 +
    1.28 +void
    1.29 +PathBuilderCG::LineTo(const Point &aPoint)
    1.30 +{
    1.31 +  if (CGPathIsEmpty(mCGPath))
    1.32 +    MoveTo(aPoint);
    1.33 +  else
    1.34 +    CGPathAddLineToPoint(mCGPath, nullptr, aPoint.x, aPoint.y);
    1.35 +}
    1.36 +
    1.37 +void
    1.38 +PathBuilderCG::BezierTo(const Point &aCP1,
    1.39 +                         const Point &aCP2,
    1.40 +                         const Point &aCP3)
    1.41 +{
    1.42 +
    1.43 +  if (CGPathIsEmpty(mCGPath))
    1.44 +    MoveTo(aCP1);
    1.45 +  CGPathAddCurveToPoint(mCGPath, nullptr,
    1.46 +                          aCP1.x, aCP1.y,
    1.47 +                          aCP2.x, aCP2.y,
    1.48 +                          aCP3.x, aCP3.y);
    1.49 +
    1.50 +}
    1.51 +
    1.52 +void
    1.53 +PathBuilderCG::QuadraticBezierTo(const Point &aCP1,
    1.54 +                                  const Point &aCP2)
    1.55 +{
    1.56 +  if (CGPathIsEmpty(mCGPath))
    1.57 +    MoveTo(aCP1);
    1.58 +  CGPathAddQuadCurveToPoint(mCGPath, nullptr,
    1.59 +                              aCP1.x, aCP1.y,
    1.60 +                              aCP2.x, aCP2.y);
    1.61 +}
    1.62 +
    1.63 +void
    1.64 +PathBuilderCG::Close()
    1.65 +{
    1.66 +  if (!CGPathIsEmpty(mCGPath))
    1.67 +    CGPathCloseSubpath(mCGPath);
    1.68 +}
    1.69 +
    1.70 +void
    1.71 +PathBuilderCG::Arc(const Point &aOrigin, Float aRadius, Float aStartAngle,
    1.72 +                 Float aEndAngle, bool aAntiClockwise)
    1.73 +{
    1.74 +  // Core Graphic's initial coordinate system is y-axis up, whereas Moz2D's is
    1.75 +  // y-axis down. Core Graphics therefore considers "clockwise" to mean "sweep
    1.76 +  // in the direction of decreasing angle" whereas Moz2D considers it to mean
    1.77 +  // "sweep in the direction of increasing angle". In other words if this
    1.78 +  // Moz2D method is instructed to sweep anti-clockwise we need to tell
    1.79 +  // CGPathAddArc to sweep clockwise, and vice versa. Hence why we pass the
    1.80 +  // value of aAntiClockwise directly to CGPathAddArc's "clockwise" bool
    1.81 +  // parameter.
    1.82 +  CGPathAddArc(mCGPath, nullptr,
    1.83 +               aOrigin.x, aOrigin.y,
    1.84 +               aRadius,
    1.85 +               aStartAngle,
    1.86 +               aEndAngle,
    1.87 +               aAntiClockwise);
    1.88 +}
    1.89 +
    1.90 +Point
    1.91 +PathBuilderCG::CurrentPoint() const
    1.92 +{
    1.93 +  CGPoint pt = CGPathGetCurrentPoint(mCGPath);
    1.94 +  Point ret(pt.x, pt.y);
    1.95 +  return ret;
    1.96 +}
    1.97 +
    1.98 +void
    1.99 +PathBuilderCG::EnsureActive(const Point &aPoint)
   1.100 +{
   1.101 +}
   1.102 +
   1.103 +TemporaryRef<Path>
   1.104 +PathBuilderCG::Finish()
   1.105 +{
   1.106 +  RefPtr<PathCG> path = new PathCG(mCGPath, mFillRule);
   1.107 +  return path;
   1.108 +}
   1.109 +
   1.110 +TemporaryRef<PathBuilder>
   1.111 +PathCG::CopyToBuilder(FillRule aFillRule) const
   1.112 +{
   1.113 +  CGMutablePathRef path = CGPathCreateMutableCopy(mPath);
   1.114 +  RefPtr<PathBuilderCG> builder = new PathBuilderCG(path, aFillRule);
   1.115 +  return builder;
   1.116 +}
   1.117 +
   1.118 +
   1.119 +
   1.120 +TemporaryRef<PathBuilder>
   1.121 +PathCG::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
   1.122 +{
   1.123 +  // 10.7 adds CGPathCreateMutableCopyByTransformingPath it might be faster than doing
   1.124 +  // this by hand
   1.125 +
   1.126 +  struct TransformApplier {
   1.127 +    CGMutablePathRef path;
   1.128 +    CGAffineTransform transform;
   1.129 +    static void
   1.130 +    TranformCGPathApplierFunc(void *vinfo, const CGPathElement *element)
   1.131 +    {
   1.132 +      TransformApplier *info = reinterpret_cast<TransformApplier*>(vinfo);
   1.133 +      switch (element->type) {
   1.134 +        case kCGPathElementMoveToPoint:
   1.135 +          {
   1.136 +            CGPoint pt = element->points[0];
   1.137 +            CGPathMoveToPoint(info->path, &info->transform, pt.x, pt.y);
   1.138 +            break;
   1.139 +          }
   1.140 +        case kCGPathElementAddLineToPoint:
   1.141 +          {
   1.142 +            CGPoint pt = element->points[0];
   1.143 +            CGPathAddLineToPoint(info->path, &info->transform, pt.x, pt.y);
   1.144 +            break;
   1.145 +          }
   1.146 +        case kCGPathElementAddQuadCurveToPoint:
   1.147 +          {
   1.148 +            CGPoint cpt = element->points[0];
   1.149 +            CGPoint pt  = element->points[1];
   1.150 +            CGPathAddQuadCurveToPoint(info->path, &info->transform, cpt.x, cpt.y, pt.x, pt.y);
   1.151 +            break;
   1.152 +          }
   1.153 +        case kCGPathElementAddCurveToPoint:
   1.154 +          {
   1.155 +            CGPoint cpt1 = element->points[0];
   1.156 +            CGPoint cpt2 = element->points[1];
   1.157 +            CGPoint pt   = element->points[2];
   1.158 +            CGPathAddCurveToPoint(info->path, &info->transform, cpt1.x, cpt1.y, cpt2.x, cpt2.y, pt.x, pt.y);
   1.159 +            break;
   1.160 +          }
   1.161 +        case kCGPathElementCloseSubpath:
   1.162 +          {
   1.163 +            CGPathCloseSubpath(info->path);
   1.164 +            break;
   1.165 +          }
   1.166 +      }
   1.167 +    }
   1.168 +  };
   1.169 +
   1.170 +  TransformApplier ta;
   1.171 +  ta.path = CGPathCreateMutable();
   1.172 +  ta.transform = GfxMatrixToCGAffineTransform(aTransform);
   1.173 +
   1.174 +  CGPathApply(mPath, &ta, TransformApplier::TranformCGPathApplierFunc);
   1.175 +  RefPtr<PathBuilderCG> builder = new PathBuilderCG(ta.path, aFillRule);
   1.176 +  return builder;
   1.177 +}
   1.178 +
   1.179 +static void
   1.180 +StreamPathToSinkApplierFunc(void *vinfo, const CGPathElement *element)
   1.181 +{
   1.182 +  PathSink *sink = reinterpret_cast<PathSink*>(vinfo);
   1.183 +  switch (element->type) {
   1.184 +    case kCGPathElementMoveToPoint:
   1.185 +      {
   1.186 +        CGPoint pt = element->points[0];
   1.187 +        sink->MoveTo(CGPointToPoint(pt));
   1.188 +        break;
   1.189 +      }
   1.190 +    case kCGPathElementAddLineToPoint:
   1.191 +      {
   1.192 +        CGPoint pt = element->points[0];
   1.193 +        sink->LineTo(CGPointToPoint(pt));
   1.194 +        break;
   1.195 +      }
   1.196 +    case kCGPathElementAddQuadCurveToPoint:
   1.197 +      {
   1.198 +        CGPoint cpt = element->points[0];
   1.199 +        CGPoint pt  = element->points[1];
   1.200 +        sink->QuadraticBezierTo(CGPointToPoint(cpt),
   1.201 +                                CGPointToPoint(pt));
   1.202 +        break;
   1.203 +      }
   1.204 +    case kCGPathElementAddCurveToPoint:
   1.205 +      {
   1.206 +        CGPoint cpt1 = element->points[0];
   1.207 +        CGPoint cpt2 = element->points[1];
   1.208 +        CGPoint pt   = element->points[2];
   1.209 +        sink->BezierTo(CGPointToPoint(cpt1),
   1.210 +                       CGPointToPoint(cpt2),
   1.211 +                       CGPointToPoint(pt));
   1.212 +        break;
   1.213 +      }
   1.214 +    case kCGPathElementCloseSubpath:
   1.215 +      {
   1.216 +        sink->Close();
   1.217 +        break;
   1.218 +      }
   1.219 +  }
   1.220 +}
   1.221 +
   1.222 +void
   1.223 +PathCG::StreamToSink(PathSink *aSink) const
   1.224 +{
   1.225 +  CGPathApply(mPath, aSink, StreamPathToSinkApplierFunc);
   1.226 +}
   1.227 +
   1.228 +bool
   1.229 +PathCG::ContainsPoint(const Point &aPoint, const Matrix &aTransform) const
   1.230 +{
   1.231 +  Matrix inverse = aTransform;
   1.232 +  inverse.Invert();
   1.233 +  Point transformedPoint = inverse*aPoint;
   1.234 +  // We could probably drop the input transform and just transform the point at the caller?
   1.235 +  CGPoint point = {transformedPoint.x, transformedPoint.y};
   1.236 +
   1.237 +  // The transform parameter of CGPathContainsPoint doesn't seem to work properly on OS X 10.5
   1.238 +  // so we transform aPoint ourselves.
   1.239 +  return CGPathContainsPoint(mPath, nullptr, point, mFillRule == FillRule::FILL_EVEN_ODD);
   1.240 +}
   1.241 +
   1.242 +static size_t
   1.243 +PutBytesNull(void *info, const void *buffer, size_t count)
   1.244 +{
   1.245 +  return count;
   1.246 +}
   1.247 +
   1.248 +/* The idea of a scratch context comes from WebKit */
   1.249 +static CGContextRef
   1.250 +CreateScratchContext()
   1.251 +{
   1.252 +  CGDataConsumerCallbacks callbacks = {PutBytesNull, nullptr};
   1.253 +  CGDataConsumerRef consumer = CGDataConsumerCreate(nullptr, &callbacks);
   1.254 +  CGContextRef cg = CGPDFContextCreate(consumer, nullptr, nullptr);
   1.255 +  CGDataConsumerRelease(consumer);
   1.256 +  return cg;
   1.257 +}
   1.258 +
   1.259 +static CGContextRef
   1.260 +ScratchContext()
   1.261 +{
   1.262 +  static CGContextRef cg = CreateScratchContext();
   1.263 +  return cg;
   1.264 +}
   1.265 +
   1.266 +bool
   1.267 +PathCG::StrokeContainsPoint(const StrokeOptions &aStrokeOptions,
   1.268 +                            const Point &aPoint,
   1.269 +                            const Matrix &aTransform) const
   1.270 +{
   1.271 +  Matrix inverse = aTransform;
   1.272 +  inverse.Invert();
   1.273 +  Point transformedPoint = inverse*aPoint;
   1.274 +  // We could probably drop the input transform and just transform the point at the caller?
   1.275 +  CGPoint point = {transformedPoint.x, transformedPoint.y};
   1.276 +
   1.277 +  CGContextRef cg = ScratchContext();
   1.278 +
   1.279 +  CGContextSaveGState(cg);
   1.280 +
   1.281 +  CGContextBeginPath(cg);
   1.282 +  CGContextAddPath(cg, mPath);
   1.283 +
   1.284 +  SetStrokeOptions(cg, aStrokeOptions);
   1.285 +
   1.286 +  CGContextReplacePathWithStrokedPath(cg);
   1.287 +  CGContextRestoreGState(cg);
   1.288 +
   1.289 +  CGPathRef sPath = CGContextCopyPath(cg);
   1.290 +  bool inStroke = CGPathContainsPoint(sPath, nullptr, point, false);
   1.291 +  CGPathRelease(sPath);
   1.292 +
   1.293 +  return inStroke;
   1.294 +}
   1.295 +
   1.296 +//XXX: what should these functions return for an empty path?
   1.297 +// currently they return CGRectNull {inf,inf, 0, 0}
   1.298 +Rect
   1.299 +PathCG::GetBounds(const Matrix &aTransform) const
   1.300 +{
   1.301 +  //XXX: are these bounds tight enough
   1.302 +  Rect bounds = CGRectToRect(CGPathGetBoundingBox(mPath));
   1.303 +
   1.304 +  //XXX: currently this returns the bounds of the transformed bounds
   1.305 +  // this is strictly looser than the bounds of the transformed path
   1.306 +  return aTransform.TransformBounds(bounds);
   1.307 +}
   1.308 +
   1.309 +Rect
   1.310 +PathCG::GetStrokedBounds(const StrokeOptions &aStrokeOptions,
   1.311 +                         const Matrix &aTransform) const
   1.312 +{
   1.313 +  // 10.7 has CGPathCreateCopyByStrokingPath which we could use
   1.314 +  // instead of this scratch context business
   1.315 +  CGContextRef cg = ScratchContext();
   1.316 +
   1.317 +  CGContextSaveGState(cg);
   1.318 +
   1.319 +  CGContextBeginPath(cg);
   1.320 +  CGContextAddPath(cg, mPath);
   1.321 +
   1.322 +  SetStrokeOptions(cg, aStrokeOptions);
   1.323 +
   1.324 +  CGContextReplacePathWithStrokedPath(cg);
   1.325 +  Rect bounds = CGRectToRect(CGContextGetPathBoundingBox(cg));
   1.326 +
   1.327 +  CGContextRestoreGState(cg);
   1.328 +
   1.329 +  if (!bounds.IsFinite()) {
   1.330 +    return Rect();
   1.331 +  }
   1.332 +
   1.333 +  return aTransform.TransformBounds(bounds);
   1.334 +}
   1.335 +
   1.336 +
   1.337 +}
   1.338 +
   1.339 +}

mercurial