Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: src/pathops/SkPathWriter.cpp

Issue 13094010: Add implementation of path ops (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/pathops/SkPathWriter.h ('k') | src/pathops/TSearch.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2012 Google Inc.
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 #include "SkPathOpsTypes.h"
8 #include "SkPathWriter.h"
9
10 // wrap path to keep track of whether the contour is initialized and non-empty
11 SkPathWriter::SkPathWriter(SkPath& path)
12 : fPathPtr(&path)
13 , fCloses(0)
14 , fMoves(0)
15 {
16 init();
17 }
18
19 void SkPathWriter::close() {
20 if (!fHasMove) {
21 return;
22 }
23 bool callClose = isClosed();
24 lineTo();
25 if (fEmpty) {
26 return;
27 }
28 if (callClose) {
29 #if DEBUG_PATH_CONSTRUCTION
30 SkDebugf("path.close();\n");
31 #endif
32 fPathPtr->close();
33 fCloses++;
34 }
35 init();
36 }
37
38 void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkPoint & pt3) {
39 lineTo();
40 moveTo();
41 fDefer[1] = pt3;
42 nudge();
43 fDefer[0] = fDefer[1];
44 #if DEBUG_PATH_CONSTRUCTION
45 SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n",
46 pt1.fX, pt1.fY, pt2.fX, pt2.fY, fDefer[1].fX, fDefer[1].fY);
47 #endif
48 fPathPtr->cubicTo(pt1.fX, pt1.fY, pt2.fX, pt2.fY, fDefer[1].fX, fDefer[1].fY );
49 fEmpty = false;
50 }
51
52 void SkPathWriter::deferredLine(const SkPoint& pt) {
53 if (pt == fDefer[1]) {
54 return;
55 }
56 if (changedSlopes(pt)) {
57 lineTo();
58 fDefer[0] = fDefer[1];
59 }
60 fDefer[1] = pt;
61 }
62
63 void SkPathWriter::deferredMove(const SkPoint& pt) {
64 fMoved = true;
65 fHasMove = true;
66 fEmpty = true;
67 fDefer[0] = fDefer[1] = pt;
68 }
69
70 void SkPathWriter::deferredMoveLine(const SkPoint& pt) {
71 if (!fHasMove) {
72 deferredMove(pt);
73 }
74 deferredLine(pt);
75 }
76
77 bool SkPathWriter::hasMove() const {
78 return fHasMove;
79 }
80
81 void SkPathWriter::init() {
82 fEmpty = true;
83 fHasMove = false;
84 fMoved = false;
85 }
86
87 bool SkPathWriter::isClosed() const {
88 return !fEmpty && fFirstPt == fDefer[1];
89 }
90
91 void SkPathWriter::lineTo() {
92 if (fDefer[0] == fDefer[1]) {
93 return;
94 }
95 moveTo();
96 nudge();
97 fEmpty = false;
98 #if DEBUG_PATH_CONSTRUCTION
99 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1].fX, fDefer[1].fY);
100 #endif
101 fPathPtr->lineTo(fDefer[1].fX, fDefer[1].fY);
102 fDefer[0] = fDefer[1];
103 }
104
105 const SkPath* SkPathWriter::nativePath() const {
106 return fPathPtr;
107 }
108
109 void SkPathWriter::nudge() {
110 if (fEmpty || !AlmostEqualUlps(fDefer[1].fX, fFirstPt.fX)
111 || !AlmostEqualUlps(fDefer[1].fY, fFirstPt.fY)) {
112 return;
113 }
114 fDefer[1] = fFirstPt;
115 }
116
117 void SkPathWriter::quadTo(const SkPoint& pt1, const SkPoint& pt2) {
118 lineTo();
119 moveTo();
120 fDefer[1] = pt2;
121 nudge();
122 fDefer[0] = fDefer[1];
123 #if DEBUG_PATH_CONSTRUCTION
124 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
125 pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY);
126 #endif
127 fPathPtr->quadTo(pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY);
128 fEmpty = false;
129 }
130
131 bool SkPathWriter::someAssemblyRequired() const {
132 return fCloses < fMoves;
133 }
134
135 bool SkPathWriter::changedSlopes(const SkPoint& pt) const {
136 if (fDefer[0] == fDefer[1]) {
137 return false;
138 }
139 SkScalar deferDx = fDefer[1].fX - fDefer[0].fX;
140 SkScalar deferDy = fDefer[1].fY - fDefer[0].fY;
141 SkScalar lineDx = pt.fX - fDefer[1].fX;
142 SkScalar lineDy = pt.fY - fDefer[1].fY;
143 return deferDx * lineDy != deferDy * lineDx;
144 }
145
146 void SkPathWriter::moveTo() {
147 if (!fMoved) {
148 return;
149 }
150 fFirstPt = fDefer[0];
151 #if DEBUG_PATH_CONSTRUCTION
152 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fDefer[0].fX, fDefer[0].fY);
153 #endif
154 fPathPtr->moveTo(fDefer[0].fX, fDefer[0].fY);
155 fMoved = false;
156 fMoves++;
157 }
OLDNEW
« no previous file with comments | « src/pathops/SkPathWriter.h ('k') | src/pathops/TSearch.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698