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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#if JUCE_MSVC && JUCE_DEBUG
#pragma optimize ("t", on)
#endif
//==============================================================================
PathFlatteningIterator::PathFlatteningIterator (const Path& path_,
const AffineTransform& transform_,
const float tolerance)
: x2 (0),
y2 (0),
closesSubPath (false),
subPathIndex (-1),
path (path_),
transform (transform_),
points (path_.data.elements),
toleranceSquared (tolerance * tolerance),
subPathCloseX (0),
subPathCloseY (0),
isIdentityTransform (transform_.isIdentity()),
stackBase (32),
index (0),
stackSize (32)
{
stackPos = stackBase;
}
PathFlatteningIterator::~PathFlatteningIterator()
{
}
bool PathFlatteningIterator::isLastInSubpath() const noexcept
{
return stackPos == stackBase.getData()
&& (index >= path.numElements || points [index] == Path::moveMarker);
}
bool PathFlatteningIterator::next()
{
x1 = x2;
y1 = y2;
float x3 = 0;
float y3 = 0;
float x4 = 0;
float y4 = 0;
for (;;)
{
float type;
if (stackPos == stackBase)
{
if (index >= path.numElements)
return false;
type = points [index++];
if (type != Path::closeSubPathMarker)
{
x2 = points [index++];
y2 = points [index++];
if (type == Path::quadMarker)
{
x3 = points [index++];
y3 = points [index++];
if (! isIdentityTransform)
transform.transformPoints (x2, y2, x3, y3);
}
else if (type == Path::cubicMarker)
{
x3 = points [index++];
y3 = points [index++];
x4 = points [index++];
y4 = points [index++];
if (! isIdentityTransform)
transform.transformPoints (x2, y2, x3, y3, x4, y4);
}
else
{
if (! isIdentityTransform)
transform.transformPoint (x2, y2);
}
}
}
else
{
type = *--stackPos;
if (type != Path::closeSubPathMarker)
{
x2 = *--stackPos;
y2 = *--stackPos;
if (type == Path::quadMarker)
{
x3 = *--stackPos;
y3 = *--stackPos;
}
else if (type == Path::cubicMarker)
{
x3 = *--stackPos;
y3 = *--stackPos;
x4 = *--stackPos;
y4 = *--stackPos;
}
}
}
if (type == Path::lineMarker)
{
++subPathIndex;
closesSubPath = (stackPos == stackBase)
&& (index < path.numElements)
&& (points [index] == Path::closeSubPathMarker)
&& x2 == subPathCloseX
&& y2 == subPathCloseY;
return true;
}
if (type == Path::quadMarker)
{
const size_t offset = (size_t) (stackPos - stackBase);
if (offset >= stackSize - 10)
{
stackSize <<= 1;
stackBase.realloc (stackSize);
stackPos = stackBase + offset;
}
const float m1x = (x1 + x2) * 0.5f;
const float m1y = (y1 + y2) * 0.5f;
const float m2x = (x2 + x3) * 0.5f;
const float m2y = (y2 + y3) * 0.5f;
const float m3x = (m1x + m2x) * 0.5f;
const float m3y = (m1y + m2y) * 0.5f;
const float errorX = m3x - x2;
const float errorY = m3y - y2;
if (errorX * errorX + errorY * errorY > toleranceSquared)
{
*stackPos++ = y3;
*stackPos++ = x3;
*stackPos++ = m2y;
*stackPos++ = m2x;
*stackPos++ = Path::quadMarker;
*stackPos++ = m3y;
*stackPos++ = m3x;
*stackPos++ = m1y;
*stackPos++ = m1x;
*stackPos++ = Path::quadMarker;
}
else
{
*stackPos++ = y3;
*stackPos++ = x3;
*stackPos++ = Path::lineMarker;
*stackPos++ = m3y;
*stackPos++ = m3x;
*stackPos++ = Path::lineMarker;
}
jassert (stackPos < stackBase + stackSize);
}
else if (type == Path::cubicMarker)
{
const size_t offset = (size_t) (stackPos - stackBase);
if (offset >= stackSize - 16)
{
stackSize <<= 1;
stackBase.realloc (stackSize);
stackPos = stackBase + offset;
}
const float m1x = (x1 + x2) * 0.5f;
const float m1y = (y1 + y2) * 0.5f;
const float m2x = (x3 + x2) * 0.5f;
const float m2y = (y3 + y2) * 0.5f;
const float m3x = (x3 + x4) * 0.5f;
const float m3y = (y3 + y4) * 0.5f;
const float m4x = (m1x + m2x) * 0.5f;
const float m4y = (m1y + m2y) * 0.5f;
const float m5x = (m3x + m2x) * 0.5f;
const float m5y = (m3y + m2y) * 0.5f;
const float error1X = m4x - x2;
const float error1Y = m4y - y2;
const float error2X = m5x - x3;
const float error2Y = m5y - y3;
if (error1X * error1X + error1Y * error1Y > toleranceSquared
|| error2X * error2X + error2Y * error2Y > toleranceSquared)
{
*stackPos++ = y4;
*stackPos++ = x4;
*stackPos++ = m3y;
*stackPos++ = m3x;
*stackPos++ = m5y;
*stackPos++ = m5x;
*stackPos++ = Path::cubicMarker;
*stackPos++ = (m4y + m5y) * 0.5f;
*stackPos++ = (m4x + m5x) * 0.5f;
*stackPos++ = m4y;
*stackPos++ = m4x;
*stackPos++ = m1y;
*stackPos++ = m1x;
*stackPos++ = Path::cubicMarker;
}
else
{
*stackPos++ = y4;
*stackPos++ = x4;
*stackPos++ = Path::lineMarker;
*stackPos++ = m5y;
*stackPos++ = m5x;
*stackPos++ = Path::lineMarker;
*stackPos++ = m4y;
*stackPos++ = m4x;
*stackPos++ = Path::lineMarker;
}
}
else if (type == Path::closeSubPathMarker)
{
if (x2 != subPathCloseX || y2 != subPathCloseY)
{
x1 = x2;
y1 = y2;
x2 = subPathCloseX;
y2 = subPathCloseY;
closesSubPath = true;
return true;
}
}
else
{
jassert (type == Path::moveMarker);
subPathIndex = -1;
subPathCloseX = x1 = x2;
subPathCloseY = y1 = y2;
}
}
}
#if JUCE_MSVC && JUCE_DEBUG
#pragma optimize ("", on) // resets optimisations to the project defaults
#endif
|