|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- /*
- ==============================================================================
-
- This file is part of the JUCE 6 technical preview.
- Copyright (c) 2017 - ROLI Ltd.
-
- You may use this code under the terms of the GPL v3
- (see www.gnu.org/licenses).
-
- For this technical preview, this file is not subject to commercial licensing.
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- #if JUCE_MSVC && JUCE_DEBUG
- #pragma optimize ("t", on)
- #endif
-
- //==============================================================================
- PathFlatteningIterator::PathFlatteningIterator (const Path& pathToUse,
- const AffineTransform& t,
- float tolerance)
- : x2 (0),
- y2 (0),
- closesSubPath (false),
- subPathIndex (-1),
- path (pathToUse),
- transform (t),
- source (path.data.begin()),
- toleranceSquared (tolerance * tolerance),
- isIdentityTransform (t.isIdentity())
- {
- stackPos = stackBase;
- }
-
- PathFlatteningIterator::~PathFlatteningIterator()
- {
- }
-
- bool PathFlatteningIterator::isLastInSubpath() const noexcept
- {
- return stackPos == stackBase.get()
- && (source == path.data.end() || isMarker (*source, 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 (source == path.data.end())
- return false;
-
- type = *source++;
-
- if (! isMarker (type, Path::closeSubPathMarker))
- {
- x2 = *source++;
- y2 = *source++;
-
- if (isMarker (type, Path::quadMarker))
- {
- x3 = *source++;
- y3 = *source++;
-
- if (! isIdentityTransform)
- transform.transformPoints (x2, y2, x3, y3);
- }
- else if (isMarker (type, Path::cubicMarker))
- {
- x3 = *source++;
- y3 = *source++;
- x4 = *source++;
- y4 = *source++;
-
- if (! isIdentityTransform)
- transform.transformPoints (x2, y2, x3, y3, x4, y4);
- }
- else
- {
- if (! isIdentityTransform)
- transform.transformPoint (x2, y2);
- }
- }
- }
- else
- {
- type = *--stackPos;
-
- if (! isMarker (type, Path::closeSubPathMarker))
- {
- x2 = *--stackPos;
- y2 = *--stackPos;
-
- if (isMarker (type, Path::quadMarker))
- {
- x3 = *--stackPos;
- y3 = *--stackPos;
- }
- else if (isMarker (type, Path::cubicMarker))
- {
- x3 = *--stackPos;
- y3 = *--stackPos;
- x4 = *--stackPos;
- y4 = *--stackPos;
- }
- }
- }
-
- if (isMarker (type, Path::lineMarker))
- {
- ++subPathIndex;
-
- closesSubPath = stackPos == stackBase
- && source != path.data.end()
- && *source == Path::closeSubPathMarker
- && x2 == subPathCloseX
- && y2 == subPathCloseY;
-
- return true;
- }
-
- if (isMarker (type, Path::quadMarker))
- {
- const size_t offset = (size_t) (stackPos - stackBase);
-
- if (offset >= stackSize - 10)
- {
- stackSize <<= 1;
- stackBase.realloc (stackSize);
- stackPos = stackBase + offset;
- }
-
- auto m1x = (x1 + x2) * 0.5f;
- auto m1y = (y1 + y2) * 0.5f;
- auto m2x = (x2 + x3) * 0.5f;
- auto m2y = (y2 + y3) * 0.5f;
- auto m3x = (m1x + m2x) * 0.5f;
- auto m3y = (m1y + m2y) * 0.5f;
-
- auto errorX = m3x - x2;
- auto 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 (isMarker (type, Path::cubicMarker))
- {
- const size_t offset = (size_t) (stackPos - stackBase);
-
- if (offset >= stackSize - 16)
- {
- stackSize <<= 1;
- stackBase.realloc (stackSize);
- stackPos = stackBase + offset;
- }
-
- auto m1x = (x1 + x2) * 0.5f;
- auto m1y = (y1 + y2) * 0.5f;
- auto m2x = (x3 + x2) * 0.5f;
- auto m2y = (y3 + y2) * 0.5f;
- auto m3x = (x3 + x4) * 0.5f;
- auto m3y = (y3 + y4) * 0.5f;
- auto m4x = (m1x + m2x) * 0.5f;
- auto m4y = (m1y + m2y) * 0.5f;
- auto m5x = (m3x + m2x) * 0.5f;
- auto m5y = (m3y + m2y) * 0.5f;
-
- auto error1X = m4x - x2;
- auto error1Y = m4y - y2;
- auto error2X = m5x - x3;
- auto 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 (isMarker (type, Path::closeSubPathMarker))
- {
- if (x2 != subPathCloseX || y2 != subPathCloseY)
- {
- x1 = x2;
- y1 = y2;
- x2 = subPathCloseX;
- y2 = subPathCloseY;
- closesSubPath = true;
-
- return true;
- }
- }
- else
- {
- jassert (isMarker (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
-
- } // namespace juce
|