1
2 /*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10 #include "Sk2DPathEffect.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
13 #include "SkPath.h"
14 #include "SkRegion.h"
15 #include "SkStrokeRec.h"
16
Sk2DPathEffect(const SkMatrix & mat)17 Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) {
18 fMatrixIsInvertible = mat.invert(&fInverse);
19 }
20
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec *,const SkRect *) const21 bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
22 SkStrokeRec*, const SkRect*) const {
23 if (!fMatrixIsInvertible) {
24 return false;
25 }
26
27 SkPath tmp;
28 SkIRect ir;
29
30 src.transform(fInverse, &tmp);
31 tmp.getBounds().round(&ir);
32 if (!ir.isEmpty()) {
33 this->begin(ir, dst);
34
35 SkRegion rgn;
36 rgn.setPath(tmp, SkRegion(ir));
37 SkRegion::Iterator iter(rgn);
38 for (; !iter.done(); iter.next()) {
39 const SkIRect& rect = iter.rect();
40 for (int y = rect.fTop; y < rect.fBottom; ++y) {
41 this->nextSpan(rect.fLeft, y, rect.width(), dst);
42 }
43 }
44
45 this->end(dst);
46 }
47 return true;
48 }
49
nextSpan(int x,int y,int count,SkPath * path) const50 void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) const {
51 if (!fMatrixIsInvertible) {
52 return;
53 }
54
55 const SkMatrix& mat = this->getMatrix();
56 SkPoint src, dst;
57
58 src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
59 do {
60 mat.mapPoints(&dst, &src, 1);
61 this->next(dst, x++, y, path);
62 src.fX += SK_Scalar1;
63 } while (--count > 0);
64 }
65
begin(const SkIRect & uvBounds,SkPath * dst) const66 void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) const {}
next(const SkPoint & loc,int u,int v,SkPath * dst) const67 void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) const {}
end(SkPath * dst) const68 void Sk2DPathEffect::end(SkPath* dst) const {}
69
70 ///////////////////////////////////////////////////////////////////////////////
71
flatten(SkWriteBuffer & buffer) const72 void Sk2DPathEffect::flatten(SkWriteBuffer& buffer) const {
73 this->INHERITED::flatten(buffer);
74 buffer.writeMatrix(fMatrix);
75 }
76
77 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const78 void Sk2DPathEffect::toString(SkString* str) const {
79 str->appendf("(matrix: %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f)",
80 fMatrix[SkMatrix::kMScaleX], fMatrix[SkMatrix::kMSkewX], fMatrix[SkMatrix::kMTransX],
81 fMatrix[SkMatrix::kMSkewY], fMatrix[SkMatrix::kMScaleY], fMatrix[SkMatrix::kMTransY],
82 fMatrix[SkMatrix::kMPersp0], fMatrix[SkMatrix::kMPersp1], fMatrix[SkMatrix::kMPersp2]);
83 }
84 #endif
85
86 ///////////////////////////////////////////////////////////////////////////////
87
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect) const88 bool SkLine2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
89 SkStrokeRec* rec, const SkRect* cullRect) const {
90 if (this->INHERITED::filterPath(dst, src, rec, cullRect)) {
91 rec->setStrokeStyle(fWidth);
92 return true;
93 }
94 return false;
95 }
96
nextSpan(int u,int v,int ucount,SkPath * dst) const97 void SkLine2DPathEffect::nextSpan(int u, int v, int ucount, SkPath* dst) const {
98 if (ucount > 1) {
99 SkPoint src[2], dstP[2];
100
101 src[0].set(SkIntToScalar(u) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
102 src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
103 this->getMatrix().mapPoints(dstP, src, 2);
104
105 dst->moveTo(dstP[0]);
106 dst->lineTo(dstP[1]);
107 }
108 }
109
CreateProc(SkReadBuffer & buffer)110 SkFlattenable* SkLine2DPathEffect::CreateProc(SkReadBuffer& buffer) {
111 SkMatrix matrix;
112 buffer.readMatrix(&matrix);
113 SkScalar width = buffer.readScalar();
114 return SkLine2DPathEffect::Create(width, matrix);
115 }
116
flatten(SkWriteBuffer & buffer) const117 void SkLine2DPathEffect::flatten(SkWriteBuffer &buffer) const {
118 buffer.writeMatrix(this->getMatrix());
119 buffer.writeScalar(fWidth);
120 }
121
122
123 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const124 void SkLine2DPathEffect::toString(SkString* str) const {
125 str->appendf("SkLine2DPathEffect: (");
126 this->INHERITED::toString(str);
127 str->appendf("width: %f", fWidth);
128 str->appendf(")");
129 }
130 #endif
131
132 ///////////////////////////////////////////////////////////////////////////////
133
SkPath2DPathEffect(const SkMatrix & m,const SkPath & p)134 SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
135 : INHERITED(m), fPath(p) {
136 }
137
CreateProc(SkReadBuffer & buffer)138 SkFlattenable* SkPath2DPathEffect::CreateProc(SkReadBuffer& buffer) {
139 SkMatrix matrix;
140 buffer.readMatrix(&matrix);
141 SkPath path;
142 buffer.readPath(&path);
143 return SkPath2DPathEffect::Create(matrix, path);
144 }
145
flatten(SkWriteBuffer & buffer) const146 void SkPath2DPathEffect::flatten(SkWriteBuffer& buffer) const {
147 buffer.writeMatrix(this->getMatrix());
148 buffer.writePath(fPath);
149 }
150
next(const SkPoint & loc,int u,int v,SkPath * dst) const151 void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v,
152 SkPath* dst) const {
153 dst->addPath(fPath, loc.fX, loc.fY);
154 }
155
156 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const157 void SkPath2DPathEffect::toString(SkString* str) const {
158 str->appendf("SkPath2DPathEffect: (");
159 this->INHERITED::toString(str);
160 // TODO: print out path information
161 str->appendf(")");
162 }
163 #endif
164