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
185
186
187
188
189
190
|
#include "EdgeWalker_Test.h"
#include "Intersection_Tests.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkPaint.h"
static bool gDrawLastAsciiPaths = true;
static bool gDrawAllAsciiPaths = false;
static bool gShowPath = true;
static void showPath(const char* str, const SkPath& path) {
if (!gShowPath) {
return;
}
SkDebugf("%s\n", str);
SkPath::Iter iter(path, true);
uint8_t verb;
SkPoint pts[4];
while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
switch (verb) {
case SkPath::kMove_Verb:
SkDebugf("path.moveTo(%g, %g);\n", pts[0].fX, pts[0].fY);
continue;
case SkPath::kLine_Verb:
SkDebugf("path.lineTo(%g, %g);\n", pts[1].fX, pts[1].fY);
break;
case SkPath::kQuad_Verb:
SkDebugf("path.quadTo(%g, %g, %g, %g);\n",
pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
break;
case SkPath::kCubic_Verb:
SkDebugf("path.cubicTo(%g, %g, %g, %g);\n",
pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
pts[3].fX, pts[3].fY);
break;
case SkPath::kClose_Verb:
SkDebugf("path.close();\n");
continue;
default:
SkDEBUGFAIL("bad verb");
return;
}
}
}
static bool pathsDrawTheSame(const SkPath& one, const SkPath& two) {
const SkRect& bounds1 = one.getBounds();
const SkRect& bounds2 = two.getBounds();
SkRect larger = bounds1;
larger.join(bounds2);
SkBitmap bits;
int bitWidth = SkScalarCeil(larger.width()) + 2;
int bitHeight = SkScalarCeil(larger.height()) + 2;
bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
bits.allocPixels();
SkCanvas canvas(bits);
canvas.drawColor(SK_ColorWHITE);
SkPaint paint;
canvas.save();
canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
canvas.drawPath(one, paint);
canvas.restore();
canvas.save();
canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
canvas.drawPath(two, paint);
canvas.restore();
for (int y = 0; y < bitHeight; ++y) {
uint32_t* addr1 = bits.getAddr32(0, y);
uint32_t* addr2 = bits.getAddr32(bitWidth, y);
for (int x = 0; x < bitWidth; ++x) {
if (addr1[x] != addr2[x]) {
return false;
break;
}
}
}
return true;
}
static void drawAsciiPaths(const SkPath& one, const SkPath& two,
bool drawPaths) {
if (!drawPaths) {
return;
}
if (0) {
showPath("one:", one);
showPath("two:", two);
}
const SkRect& bounds1 = one.getBounds();
const SkRect& bounds2 = two.getBounds();
SkRect larger = bounds1;
larger.join(bounds2);
SkBitmap bits;
int bitWidth = SkScalarCeil(larger.width()) + 2;
int bitHeight = SkScalarCeil(larger.height()) + 2;
bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight);
bits.allocPixels();
SkCanvas canvas(bits);
canvas.drawColor(SK_ColorWHITE);
SkPaint paint;
canvas.save();
canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
canvas.drawPath(one, paint);
canvas.restore();
canvas.save();
canvas.translate(-bounds2.fLeft + 1 + bitWidth, -bounds2.fTop + 1);
canvas.drawPath(two, paint);
canvas.restore();
char out[1024];
SkASSERT(bitWidth * 2 + 1 < (int) sizeof(out));
for (int y = 0; y < bitHeight; ++y) {
uint32_t* addr1 = bits.getAddr32(0, y);
int x;
char* outPtr = out;
for (x = 0; x < bitWidth; ++x) {
*outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
}
*outPtr++ = '|';
for (x = bitWidth; x < bitWidth * 2; ++x) {
*outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
}
*outPtr++ = '\0';
SkDebugf("%s\n", out);
}
}
static bool scaledDrawTheSame(const SkPath& one, const SkPath& two,
int a, int b, bool drawPaths) {
SkMatrix scale;
scale.reset();
scale.preScale(a * 1.21f, b * 1.11f);
SkPath scaledOne, scaledTwo;
one.transform(scale, &scaledOne);
two.transform(scale, &scaledTwo);
if (pathsDrawTheSame(scaledOne, scaledTwo)) {
return true;
}
drawAsciiPaths(scaledOne, scaledTwo, drawPaths);
return false;
}
void comparePaths(const SkPath& one, const SkPath& two) {
if (pathsDrawTheSame(one, two)) {
return;
}
drawAsciiPaths(one, two, gDrawAllAsciiPaths);
for (int x = 9; x <= 33; ++x) {
if (scaledDrawTheSame(one, two, x, x - (x >> 2), gDrawAllAsciiPaths)) {
return;
}
}
if (!gDrawAllAsciiPaths) {
scaledDrawTheSame(one, two, 9, 7, gDrawLastAsciiPaths);
}
showPath("original:", one);
showPath("simplified:", two);
SkASSERT(0);
}
// doesn't work yet
void comparePathsTiny(const SkPath& one, const SkPath& two) {
const SkRect& bounds1 = one.getBounds();
const SkRect& bounds2 = two.getBounds();
SkRect larger = bounds1;
larger.join(bounds2);
SkBitmap bits;
int bitWidth = SkScalarCeil(larger.width()) + 2;
int bitHeight = SkScalarCeil(larger.height()) + 2;
bits.setConfig(SkBitmap::kA1_Config, bitWidth * 2, bitHeight);
bits.allocPixels();
SkCanvas canvas(bits);
canvas.drawColor(SK_ColorWHITE);
SkPaint paint;
canvas.save();
canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
canvas.drawPath(one, paint);
canvas.restore();
canvas.save();
canvas.translate(-bounds2.fLeft + 1, -bounds2.fTop + 1);
canvas.drawPath(two, paint);
canvas.restore();
for (int y = 0; y < bitHeight; ++y) {
uint8_t* addr1 = bits.getAddr1(0, y);
uint8_t* addr2 = bits.getAddr1(bitWidth, y);
for (int x = 0; x < bits.rowBytes(); ++x) {
SkASSERT(addr1[x] == addr2[x]);
}
}
}
|