aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pathops/SkPathWriter.cpp
blob: bd8c72134c1cef41e492927a37b9d4550d49afd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkPathOpsPoint.h"
#include "SkPathWriter.h"

// wrap path to keep track of whether the contour is initialized and non-empty
SkPathWriter::SkPathWriter(SkPath& path)
    : fPathPtr(&path)
    , fCloses(0)
    , fMoves(0)
{
    init();
}

void SkPathWriter::close() {
    if (!fHasMove) {
        return;
    }
    bool callClose = isClosed();
    lineTo();
    if (fEmpty) {
        return;
    }
    if (callClose) {
#if DEBUG_PATH_CONSTRUCTION
        SkDebugf("path.close();\n");
#endif
        fPathPtr->close();
        fCloses++;
    }
    init();
}

void SkPathWriter::conicTo(const SkPoint& pt1, const SkPoint& pt2, SkScalar weight) {
    lineTo();
    if (fEmpty && AlmostEqualUlps(fDefer[0], pt1) && AlmostEqualUlps(pt1, pt2)) {
        deferredLine(pt2);
        return;
    }
    moveTo();
    fDefer[1] = pt2;
    nudge();
    fDefer[0] = fDefer[1];
#if DEBUG_PATH_CONSTRUCTION
    SkDebugf("path.conicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g);\n",
            pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY, weight);
#endif
    fPathPtr->conicTo(pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY, weight);
    fEmpty = false;
}

void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkPoint& pt3) {
    lineTo();
    if (fEmpty && AlmostEqualUlps(fDefer[0], pt1) && AlmostEqualUlps(pt1, pt2)
            && AlmostEqualUlps(pt2, pt3)) {
        deferredLine(pt3);
        return;
    }
    moveTo();
    fDefer[1] = pt3;
    nudge();
    fDefer[0] = fDefer[1];
#if DEBUG_PATH_CONSTRUCTION
    SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n",
            pt1.fX, pt1.fY, pt2.fX, pt2.fY, fDefer[1].fX, fDefer[1].fY);
#endif
    fPathPtr->cubicTo(pt1.fX, pt1.fY, pt2.fX, pt2.fY, fDefer[1].fX, fDefer[1].fY);
    fEmpty = false;
}

void SkPathWriter::deferredLine(const SkPoint& pt) {
    if (pt == fDefer[1]) {
        return;
    }
    if (changedSlopes(pt)) {
        lineTo();
        fDefer[0] = fDefer[1];
    }
    fDefer[1] = pt;
}

void SkPathWriter::deferredMove(const SkPoint& pt) {
    fMoved = true;
    fHasMove = true;
    fEmpty = true;
    fDefer[0] = fDefer[1] = pt;
}

void SkPathWriter::deferredMoveLine(const SkPoint& pt) {
    if (!fHasMove) {
        deferredMove(pt);
    }
    deferredLine(pt);
}

bool SkPathWriter::hasMove() const {
    return fHasMove;
}

void SkPathWriter::init() {
    fEmpty = true;
    fHasMove = false;
    fMoved = false;
}

bool SkPathWriter::isClosed() const {
    return !fEmpty && SkDPoint::ApproximatelyEqual(fFirstPt, fDefer[1]);
}

void SkPathWriter::lineTo() {
    if (fDefer[0] == fDefer[1]) {
        return;
    }
    moveTo();
    nudge();
    fEmpty = false;
#if DEBUG_PATH_CONSTRUCTION
    SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1].fX, fDefer[1].fY);
#endif
    fPathPtr->lineTo(fDefer[1].fX, fDefer[1].fY);
    fDefer[0] = fDefer[1];
}

const SkPath* SkPathWriter::nativePath() const {
    return fPathPtr;
}

void SkPathWriter::nudge() {
    if (fEmpty || !AlmostEqualUlps(fDefer[1].fX, fFirstPt.fX)
            || !AlmostEqualUlps(fDefer[1].fY, fFirstPt.fY)) {
        return;
    }
    fDefer[1] = fFirstPt;
}

void SkPathWriter::quadTo(const SkPoint& pt1, const SkPoint& pt2) {
    lineTo();
    if (fEmpty && AlmostEqualUlps(fDefer[0], pt1) && AlmostEqualUlps(pt1, pt2)) {
        deferredLine(pt2);
        return;
    }
    moveTo();
    fDefer[1] = pt2;
    nudge();
    fDefer[0] = fDefer[1];
#if DEBUG_PATH_CONSTRUCTION
    SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
            pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY);
#endif
    fPathPtr->quadTo(pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY);
    fEmpty = false;
}

bool SkPathWriter::someAssemblyRequired() const {
    return fCloses < fMoves;
}

bool SkPathWriter::changedSlopes(const SkPoint& pt) const {
    if (fDefer[0] == fDefer[1]) {
        return false;
    }
    SkScalar deferDx = fDefer[1].fX - fDefer[0].fX;
    SkScalar deferDy = fDefer[1].fY - fDefer[0].fY;
    SkScalar lineDx = pt.fX - fDefer[1].fX;
    SkScalar lineDy = pt.fY - fDefer[1].fY;
    return deferDx * lineDy != deferDy * lineDx;
}

void SkPathWriter::moveTo() {
    if (!fMoved) {
        return;
    }
    fFirstPt = fDefer[0];
#if DEBUG_PATH_CONSTRUCTION
    SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fDefer[0].fX, fDefer[0].fY);
#endif
    fPathPtr->moveTo(fDefer[0].fX, fDefer[0].fY);
    fMoved = false;
    fMoves++;
}