DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "PathCapture.h"

namespace mozilla {
namespace gfx {

void PathBuilderCapture::MoveTo(const Point& aPoint) {
  PathOp op;
  op.mType = PathOp::OP_MOVETO;
  op.mP1 = aPoint;
  mPathOps.push_back(op);
  mCurrentPoint = aPoint;
  mBeginPoint = aPoint;
}

void PathBuilderCapture::LineTo(const Point& aPoint) {
  PathOp op;
  op.mType = PathOp::OP_LINETO;
  op.mP1 = aPoint;
  mPathOps.push_back(op);
  mCurrentPoint = aPoint;
}

void PathBuilderCapture::BezierTo(const Point& aCP1, const Point& aCP2,
                                  const Point& aCP3) {
  PathOp op;
  op.mType = PathOp::OP_BEZIERTO;
  op.mP1 = aCP1;
  op.mP2 = aCP2;
  op.mP3 = aCP3;
  mPathOps.push_back(op);
  mCurrentPoint = aCP3;
}

void PathBuilderCapture::QuadraticBezierTo(const Point& aCP1,
                                           const Point& aCP2) {
  PathOp op;
  op.mType = PathOp::OP_QUADRATICBEZIERTO;
  op.mP1 = aCP1;
  op.mP2 = aCP2;
  mPathOps.push_back(op);
  mCurrentPoint = aCP2;
}

void PathBuilderCapture::Arc(const Point& aCenter, float aRadius,
                             float aStartAngle, float aEndAngle,
                             bool aAntiClockwise) {
  PathOp op;
  op.mType = PathOp::OP_ARC;
  op.mP1 = aCenter;
  op.mRadius = aRadius;
  op.mStartAngle = aStartAngle;
  op.mEndAngle = aEndAngle;
  op.mAntiClockwise = aAntiClockwise;
  mPathOps.push_back(op);
  mCurrentPoint = Point(aCenter.x + aRadius * cosf(aEndAngle),
                        aCenter.y + aRadius * sinf(aEndAngle));
}

void PathBuilderCapture::Close() {
  PathOp op;
  op.mType = PathOp::OP_CLOSE;
  mPathOps.push_back(op);
  mCurrentPoint = mBeginPoint;
}

already_AddRefed<Path> PathBuilderCapture::Finish() {
  Point currentPoint = mCurrentPoint;
  Point beginPoint = mBeginPoint;
  mCurrentPoint = Point(0.0, 0.0);
  mBeginPoint = Point(0.0, 0.0);
  return MakeAndAddRef<PathCapture>(std::move(mPathOps), mFillRule, mDT,
                                    currentPoint, beginPoint);
}

already_AddRefed<PathBuilder> PathCapture::CopyToBuilder(
    FillRule aFillRule) const {
  RefPtr<PathBuilderCapture> capture = new PathBuilderCapture(aFillRule, mDT);
  capture->mPathOps = mPathOps;
  capture->mCurrentPoint = mCurrentPoint;
  capture->mBeginPoint = mBeginPoint;
  return capture.forget();
}

already_AddRefed<PathBuilder> PathCapture::TransformedCopyToBuilder(
    const Matrix& aTransform, FillRule aFillRule) const {
  RefPtr<PathBuilderCapture> capture = new PathBuilderCapture(aFillRule, mDT);
  typedef std::vector<PathOp> pathOpVec;
  for (pathOpVec::const_iterator iter = mPathOps.begin();
       iter != mPathOps.end(); iter++) {
    PathOp newPathOp;
    newPathOp.mType = iter->mType;
    if (newPathOp.mType == PathOp::OpType::OP_ARC) {
      struct ArcTransformer {
        ArcTransformer(pathOpVec& aVector, const Matrix& aTransform)
            : mVector(&aVector), mTransform(&aTransform) {}
        void BezierTo(const Point& aCP1, const Point& aCP2, const Point& aCP3) {
          PathOp newPathOp;
          newPathOp.mType = PathOp::OP_BEZIERTO;
          newPathOp.mP1 = mTransform->TransformPoint(aCP1);
          newPathOp.mP2 = mTransform->TransformPoint(aCP2);
          newPathOp.mP3 = mTransform->TransformPoint(aCP3);
          mVector->push_back(newPathOp);
        }
        void LineTo(const Point& aPoint) {
          PathOp newPathOp;
          newPathOp.mType = PathOp::OP_LINETO;
          newPathOp.mP1 = mTransform->TransformPoint(aPoint);
          mVector->push_back(newPathOp);
        }
        pathOpVec* mVector;
        const Matrix* mTransform;
      };

      ArcTransformer arcTransformer(capture->mPathOps, aTransform);
      ArcToBezier(&arcTransformer, iter->mP1,
                  Size(iter->mRadius, iter->mRadius), iter->mStartAngle,
                  iter->mEndAngle, iter->mAntiClockwise);
    } else {
      if (sPointCount[newPathOp.mType] >= 1) {
        newPathOp.mP1 = aTransform.TransformPoint(iter->mP1);
      }
      if (sPointCount[newPathOp.mType] >= 2) {
        newPathOp.mP2 = aTransform.TransformPoint(iter->mP2);
      }
      if (sPointCount[newPathOp.mType] >= 3) {
        newPathOp.mP3 = aTransform.TransformPoint(iter->mP3);
      }
      capture->mPathOps.push_back(newPathOp);
    }
  }
  capture->mCurrentPoint = aTransform.TransformPoint(mCurrentPoint);
  capture->mBeginPoint = aTransform.TransformPoint(mBeginPoint);
  return capture.forget();
}
bool PathCapture::ContainsPoint(const Point& aPoint,
                                const Matrix& aTransform) const {
  if (!EnsureRealizedPath()) {
    return false;
  }
  return mRealizedPath->ContainsPoint(aPoint, aTransform);
}

bool PathCapture::StrokeContainsPoint(const StrokeOptions& aStrokeOptions,
                                      const Point& aPoint,
                                      const Matrix& aTransform) const {
  if (!EnsureRealizedPath()) {
    return false;
  }
  return mRealizedPath->StrokeContainsPoint(aStrokeOptions, aPoint, aTransform);
}

Rect PathCapture::GetBounds(const Matrix& aTransform) const {
  if (!EnsureRealizedPath()) {
    return Rect();
  }
  return mRealizedPath->GetBounds(aTransform);
}

Rect PathCapture::GetStrokedBounds(const StrokeOptions& aStrokeOptions,
                                   const Matrix& aTransform) const {
  if (!EnsureRealizedPath()) {
    return Rect();
  }
  return mRealizedPath->GetStrokedBounds(aStrokeOptions, aTransform);
}

void PathCapture::StreamToSink(PathSink* aSink) const {
  for (const PathOp& op : mPathOps) {
    switch (op.mType) {
      case PathOp::OP_MOVETO:
        aSink->MoveTo(op.mP1);
        break;
      case PathOp::OP_LINETO:
        aSink->LineTo(op.mP1);
        break;
      case PathOp::OP_BEZIERTO:
        aSink->BezierTo(op.mP1, op.mP2, op.mP3);
        break;
      case PathOp::OP_QUADRATICBEZIERTO:
        aSink->QuadraticBezierTo(op.mP1, op.mP2);
        break;
      case PathOp::OP_ARC:
        aSink->Arc(op.mP1, op.mRadius, op.mStartAngle, op.mEndAngle,
                   op.mAntiClockwise);
        break;
      case PathOp::OP_CLOSE:
        aSink->Close();
        break;
    }
  }
}

bool PathCapture::EnsureRealizedPath() const {
  RefPtr<PathBuilder> builder = mDT->CreatePathBuilder(mFillRule);
  if (!builder) {
    return false;
  }
  StreamToSink(builder);
  mRealizedPath = builder->Finish();
  return true;
}

Path* PathCapture::GetRealizedPath() const {
  if (!EnsureRealizedPath()) {
    return nullptr;
  }

  return mRealizedPath.get();
}

}  // namespace gfx
}  // namespace mozilla