blob: a54f453ecfe2c313ceb4eea3757facb8a5b026c4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "Sk2DPathEffect.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPath.h"
reed@google.com6db93752012-08-09 19:18:02 +000014#include "SkRegion.h"
halcanary435657f2015-09-15 12:53:07 -070015#include "SkStrokeRec.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
reed@google.com16edff22011-08-12 14:10:27 +000017Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000018 fMatrixIsInvertible = mat.invert(&fInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000019}
20
reed@google.com548a1f32012-12-18 16:12:09 +000021bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000022 SkStrokeRec*, const SkRect*) const {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000023 if (!fMatrixIsInvertible) {
24 return false;
25 }
26
reed@google.com6db93752012-08-09 19:18:02 +000027 SkPath tmp;
28 SkIRect ir;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
30 src.transform(fInverse, &tmp);
reed@android.comd252db02009-04-01 18:31:44 +000031 tmp.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 if (!ir.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 this->begin(ir, dst);
reed@google.com6db93752012-08-09 19:18:02 +000034
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
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 this->end(dst);
46 }
47 return true;
48}
49
reed@google.com548a1f32012-12-18 16:12:09 +000050void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) const {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000051 if (!fMatrixIsInvertible) {
52 return;
53 }
54
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 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
reed@google.com548a1f32012-12-18 16:12:09 +000066void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) const {}
67void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) const {}
68void Sk2DPathEffect::end(SkPath* dst) const {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
reed@google.com16edff22011-08-12 14:10:27 +000070///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000071
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000072void Sk2DPathEffect::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com54924242012-03-29 15:18:04 +000073 this->INHERITED::flatten(buffer);
djsollen@google.comd2700ee2012-05-30 16:54:13 +000074 buffer.writeMatrix(fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075}
76
robertphillips42dbfa82015-01-26 06:08:52 -080077#ifndef SK_IGNORE_TO_STRING
78void 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
reed@google.come28b9172011-08-09 18:14:31 +000086///////////////////////////////////////////////////////////////////////////////
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000087
reed@google.com548a1f32012-12-18 16:12:09 +000088bool SkLine2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000089 SkStrokeRec* rec, const SkRect* cullRect) const {
90 if (this->INHERITED::filterPath(dst, src, rec, cullRect)) {
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000091 rec->setStrokeStyle(fWidth);
92 return true;
93 }
94 return false;
95}
96
reed@google.com548a1f32012-12-18 16:12:09 +000097void SkLine2DPathEffect::nextSpan(int u, int v, int ucount, SkPath* dst) const {
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000098 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
reed9fa60da2014-08-21 07:59:51 -0700110SkFlattenable* SkLine2DPathEffect::CreateProc(SkReadBuffer& buffer) {
111 SkMatrix matrix;
112 buffer.readMatrix(&matrix);
113 SkScalar width = buffer.readScalar();
reeda4393342016-03-18 11:22:57 -0700114 return SkLine2DPathEffect::Make(width, matrix).release();
reed9fa60da2014-08-21 07:59:51 -0700115}
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000116
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000117void SkLine2DPathEffect::flatten(SkWriteBuffer &buffer) const {
reed9fa60da2014-08-21 07:59:51 -0700118 buffer.writeMatrix(this->getMatrix());
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000119 buffer.writeScalar(fWidth);
120}
121
robertphillips42dbfa82015-01-26 06:08:52 -0800122
123#ifndef SK_IGNORE_TO_STRING
124void 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
reed@google.com18dc4772011-08-09 18:47:40 +0000132///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133
reed@google.com18dc4772011-08-09 18:47:40 +0000134SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
135 : INHERITED(m), fPath(p) {
136}
137
reed9fa60da2014-08-21 07:59:51 -0700138SkFlattenable* SkPath2DPathEffect::CreateProc(SkReadBuffer& buffer) {
139 SkMatrix matrix;
140 buffer.readMatrix(&matrix);
141 SkPath path;
142 buffer.readPath(&path);
reeda4393342016-03-18 11:22:57 -0700143 return SkPath2DPathEffect::Make(matrix, path).release();
reed9fa60da2014-08-21 07:59:51 -0700144}
reed@google.com18dc4772011-08-09 18:47:40 +0000145
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000146void SkPath2DPathEffect::flatten(SkWriteBuffer& buffer) const {
reed9fa60da2014-08-21 07:59:51 -0700147 buffer.writeMatrix(this->getMatrix());
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000148 buffer.writePath(fPath);
reed@google.com18dc4772011-08-09 18:47:40 +0000149}
150
reed@google.com548a1f32012-12-18 16:12:09 +0000151void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v,
152 SkPath* dst) const {
reed@google.com18dc4772011-08-09 18:47:40 +0000153 dst->addPath(fPath, loc.fX, loc.fY);
154}
robertphillips42dbfa82015-01-26 06:08:52 -0800155
156#ifndef SK_IGNORE_TO_STRING
157void 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