blob: 52e3fa5e1c88b63d3f7c797a5ab736a7e8feb81f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
9#include "Sk2DPathEffect.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPath.h"
reed@google.com6db93752012-08-09 19:18:02 +000013#include "SkRegion.h"
halcanary435657f2015-09-15 12:53:07 -070014#include "SkStrokeRec.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reed@google.com16edff22011-08-12 14:10:27 +000016Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000017 fMatrixIsInvertible = mat.invert(&fInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000018}
19
reed@google.com548a1f32012-12-18 16:12:09 +000020bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000021 SkStrokeRec*, const SkRect*) const {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000022 if (!fMatrixIsInvertible) {
23 return false;
24 }
25
reed@google.com6db93752012-08-09 19:18:02 +000026 SkPath tmp;
27 SkIRect ir;
reed@android.com8a1c16f2008-12-17 15:59:43 +000028
29 src.transform(fInverse, &tmp);
reed@android.comd252db02009-04-01 18:31:44 +000030 tmp.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 if (!ir.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 this->begin(ir, dst);
reed@google.com6db93752012-08-09 19:18:02 +000033
34 SkRegion rgn;
35 rgn.setPath(tmp, SkRegion(ir));
36 SkRegion::Iterator iter(rgn);
37 for (; !iter.done(); iter.next()) {
38 const SkIRect& rect = iter.rect();
39 for (int y = rect.fTop; y < rect.fBottom; ++y) {
40 this->nextSpan(rect.fLeft, y, rect.width(), dst);
41 }
42 }
43
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 this->end(dst);
45 }
46 return true;
47}
48
reed@google.com548a1f32012-12-18 16:12:09 +000049void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) const {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000050 if (!fMatrixIsInvertible) {
51 return;
52 }
53
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 const SkMatrix& mat = this->getMatrix();
55 SkPoint src, dst;
56
57 src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
58 do {
59 mat.mapPoints(&dst, &src, 1);
60 this->next(dst, x++, y, path);
61 src.fX += SK_Scalar1;
62 } while (--count > 0);
63}
64
reed@google.com548a1f32012-12-18 16:12:09 +000065void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) const {}
66void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) const {}
67void Sk2DPathEffect::end(SkPath* dst) const {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000068
reed@google.com16edff22011-08-12 14:10:27 +000069///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000071void Sk2DPathEffect::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com54924242012-03-29 15:18:04 +000072 this->INHERITED::flatten(buffer);
djsollen@google.comd2700ee2012-05-30 16:54:13 +000073 buffer.writeMatrix(fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074}
75
robertphillips42dbfa82015-01-26 06:08:52 -080076#ifndef SK_IGNORE_TO_STRING
77void Sk2DPathEffect::toString(SkString* str) const {
78 str->appendf("(matrix: %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f)",
79 fMatrix[SkMatrix::kMScaleX], fMatrix[SkMatrix::kMSkewX], fMatrix[SkMatrix::kMTransX],
80 fMatrix[SkMatrix::kMSkewY], fMatrix[SkMatrix::kMScaleY], fMatrix[SkMatrix::kMTransY],
81 fMatrix[SkMatrix::kMPersp0], fMatrix[SkMatrix::kMPersp1], fMatrix[SkMatrix::kMPersp2]);
82}
83#endif
84
reed@google.come28b9172011-08-09 18:14:31 +000085///////////////////////////////////////////////////////////////////////////////
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000086
reed@google.com548a1f32012-12-18 16:12:09 +000087bool SkLine2DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000088 SkStrokeRec* rec, const SkRect* cullRect) const {
89 if (this->INHERITED::filterPath(dst, src, rec, cullRect)) {
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000090 rec->setStrokeStyle(fWidth);
91 return true;
92 }
93 return false;
94}
95
reed@google.com548a1f32012-12-18 16:12:09 +000096void SkLine2DPathEffect::nextSpan(int u, int v, int ucount, SkPath* dst) const {
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000097 if (ucount > 1) {
98 SkPoint src[2], dstP[2];
99
100 src[0].set(SkIntToScalar(u) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
101 src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
102 this->getMatrix().mapPoints(dstP, src, 2);
103
104 dst->moveTo(dstP[0]);
105 dst->lineTo(dstP[1]);
106 }
107}
108
reed60c9b582016-04-03 09:11:13 -0700109sk_sp<SkFlattenable> SkLine2DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700110 SkMatrix matrix;
111 buffer.readMatrix(&matrix);
112 SkScalar width = buffer.readScalar();
reed60c9b582016-04-03 09:11:13 -0700113 return SkLine2DPathEffect::Make(width, matrix);
reed9fa60da2014-08-21 07:59:51 -0700114}
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000115
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000116void SkLine2DPathEffect::flatten(SkWriteBuffer &buffer) const {
reed9fa60da2014-08-21 07:59:51 -0700117 buffer.writeMatrix(this->getMatrix());
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000118 buffer.writeScalar(fWidth);
119}
120
robertphillips42dbfa82015-01-26 06:08:52 -0800121
122#ifndef SK_IGNORE_TO_STRING
123void SkLine2DPathEffect::toString(SkString* str) const {
124 str->appendf("SkLine2DPathEffect: (");
125 this->INHERITED::toString(str);
126 str->appendf("width: %f", fWidth);
127 str->appendf(")");
128}
129#endif
130
reed@google.com18dc4772011-08-09 18:47:40 +0000131///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132
reed@google.com18dc4772011-08-09 18:47:40 +0000133SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
134 : INHERITED(m), fPath(p) {
135}
136
reed60c9b582016-04-03 09:11:13 -0700137sk_sp<SkFlattenable> SkPath2DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700138 SkMatrix matrix;
139 buffer.readMatrix(&matrix);
140 SkPath path;
141 buffer.readPath(&path);
reed60c9b582016-04-03 09:11:13 -0700142 return SkPath2DPathEffect::Make(matrix, path);
reed9fa60da2014-08-21 07:59:51 -0700143}
reed@google.com18dc4772011-08-09 18:47:40 +0000144
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000145void SkPath2DPathEffect::flatten(SkWriteBuffer& buffer) const {
reed9fa60da2014-08-21 07:59:51 -0700146 buffer.writeMatrix(this->getMatrix());
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000147 buffer.writePath(fPath);
reed@google.com18dc4772011-08-09 18:47:40 +0000148}
149
reed@google.com548a1f32012-12-18 16:12:09 +0000150void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v,
151 SkPath* dst) const {
reed@google.com18dc4772011-08-09 18:47:40 +0000152 dst->addPath(fPath, loc.fX, loc.fY);
153}
robertphillips42dbfa82015-01-26 06:08:52 -0800154
155#ifndef SK_IGNORE_TO_STRING
156void SkPath2DPathEffect::toString(SkString* str) const {
157 str->appendf("SkPath2DPathEffect: (");
158 this->INHERITED::toString(str);
159 // TODO: print out path information
160 str->appendf(")");
161}
162#endif