gfx/skia/trunk/src/effects/Sk2DPathEffect.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/effects/Sk2DPathEffect.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,133 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#include "Sk2DPathEffect.h"
    1.14 +#include "SkReadBuffer.h"
    1.15 +#include "SkWriteBuffer.h"
    1.16 +#include "SkPath.h"
    1.17 +#include "SkRegion.h"
    1.18 +
    1.19 +Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) {
    1.20 +    fMatrixIsInvertible = mat.invert(&fInverse);
    1.21 +}
    1.22 +
    1.23 +bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
    1.24 +                                SkStrokeRec*, const SkRect*) const {
    1.25 +    if (!fMatrixIsInvertible) {
    1.26 +        return false;
    1.27 +    }
    1.28 +
    1.29 +    SkPath  tmp;
    1.30 +    SkIRect ir;
    1.31 +
    1.32 +    src.transform(fInverse, &tmp);
    1.33 +    tmp.getBounds().round(&ir);
    1.34 +    if (!ir.isEmpty()) {
    1.35 +        this->begin(ir, dst);
    1.36 +
    1.37 +        SkRegion rgn;
    1.38 +        rgn.setPath(tmp, SkRegion(ir));
    1.39 +        SkRegion::Iterator iter(rgn);
    1.40 +        for (; !iter.done(); iter.next()) {
    1.41 +            const SkIRect& rect = iter.rect();
    1.42 +            for (int y = rect.fTop; y < rect.fBottom; ++y) {
    1.43 +                this->nextSpan(rect.fLeft, y, rect.width(), dst);
    1.44 +            }
    1.45 +        }
    1.46 +
    1.47 +        this->end(dst);
    1.48 +    }
    1.49 +    return true;
    1.50 +}
    1.51 +
    1.52 +void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) const {
    1.53 +    if (!fMatrixIsInvertible) {
    1.54 +        return;
    1.55 +    }
    1.56 +
    1.57 +    const SkMatrix& mat = this->getMatrix();
    1.58 +    SkPoint src, dst;
    1.59 +
    1.60 +    src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
    1.61 +    do {
    1.62 +        mat.mapPoints(&dst, &src, 1);
    1.63 +        this->next(dst, x++, y, path);
    1.64 +        src.fX += SK_Scalar1;
    1.65 +    } while (--count > 0);
    1.66 +}
    1.67 +
    1.68 +void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) const {}
    1.69 +void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) const {}
    1.70 +void Sk2DPathEffect::end(SkPath* dst) const {}
    1.71 +
    1.72 +///////////////////////////////////////////////////////////////////////////////
    1.73 +
    1.74 +void Sk2DPathEffect::flatten(SkWriteBuffer& buffer) const {
    1.75 +    this->INHERITED::flatten(buffer);
    1.76 +    buffer.writeMatrix(fMatrix);
    1.77 +}
    1.78 +
    1.79 +Sk2DPathEffect::Sk2DPathEffect(SkReadBuffer& buffer) {
    1.80 +    buffer.readMatrix(&fMatrix);
    1.81 +    fMatrixIsInvertible = fMatrix.invert(&fInverse);
    1.82 +}
    1.83 +
    1.84 +///////////////////////////////////////////////////////////////////////////////
    1.85 +
    1.86 +bool SkLine2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
    1.87 +                            SkStrokeRec* rec, const SkRect* cullRect) const {
    1.88 +    if (this->INHERITED::filterPath(dst, src, rec, cullRect)) {
    1.89 +        rec->setStrokeStyle(fWidth);
    1.90 +        return true;
    1.91 +    }
    1.92 +    return false;
    1.93 +}
    1.94 +
    1.95 +void SkLine2DPathEffect::nextSpan(int u, int v, int ucount, SkPath* dst) const {
    1.96 +    if (ucount > 1) {
    1.97 +        SkPoint    src[2], dstP[2];
    1.98 +
    1.99 +        src[0].set(SkIntToScalar(u) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
   1.100 +        src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
   1.101 +        this->getMatrix().mapPoints(dstP, src, 2);
   1.102 +
   1.103 +        dst->moveTo(dstP[0]);
   1.104 +        dst->lineTo(dstP[1]);
   1.105 +    }
   1.106 +}
   1.107 +
   1.108 +SkLine2DPathEffect::SkLine2DPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
   1.109 +    fWidth = buffer.readScalar();
   1.110 +}
   1.111 +
   1.112 +void SkLine2DPathEffect::flatten(SkWriteBuffer &buffer) const {
   1.113 +    this->INHERITED::flatten(buffer);
   1.114 +    buffer.writeScalar(fWidth);
   1.115 +}
   1.116 +
   1.117 +///////////////////////////////////////////////////////////////////////////////
   1.118 +
   1.119 +SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
   1.120 +    : INHERITED(m), fPath(p) {
   1.121 +}
   1.122 +
   1.123 +SkPath2DPathEffect::SkPath2DPathEffect(SkReadBuffer& buffer)
   1.124 +        : INHERITED(buffer) {
   1.125 +    buffer.readPath(&fPath);
   1.126 +}
   1.127 +
   1.128 +void SkPath2DPathEffect::flatten(SkWriteBuffer& buffer) const {
   1.129 +    this->INHERITED::flatten(buffer);
   1.130 +    buffer.writePath(fPath);
   1.131 +}
   1.132 +
   1.133 +void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v,
   1.134 +                              SkPath* dst) const {
   1.135 +    dst->addPath(fPath, loc.fX, loc.fY);
   1.136 +}

mercurial