The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
8.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #if JUCE_MSVC && JUCE_DEBUG
  20. #pragma optimize ("t", on)
  21. #endif
  22. //==============================================================================
  23. PathFlatteningIterator::PathFlatteningIterator (const Path& path_,
  24. const AffineTransform& transform_,
  25. const float tolerance)
  26. : x2 (0),
  27. y2 (0),
  28. closesSubPath (false),
  29. subPathIndex (-1),
  30. path (path_),
  31. transform (transform_),
  32. points (path_.data.elements),
  33. toleranceSquared (tolerance * tolerance),
  34. isIdentityTransform (transform_.isIdentity())
  35. {
  36. stackPos = stackBase;
  37. }
  38. PathFlatteningIterator::~PathFlatteningIterator()
  39. {
  40. }
  41. bool PathFlatteningIterator::isLastInSubpath() const noexcept
  42. {
  43. return stackPos == stackBase.getData()
  44. && (index >= path.numElements || isMarker (points[index], Path::moveMarker));
  45. }
  46. bool PathFlatteningIterator::next()
  47. {
  48. x1 = x2;
  49. y1 = y2;
  50. float x3 = 0;
  51. float y3 = 0;
  52. float x4 = 0;
  53. float y4 = 0;
  54. for (;;)
  55. {
  56. float type;
  57. if (stackPos == stackBase)
  58. {
  59. if (index >= path.numElements)
  60. return false;
  61. type = points [index++];
  62. if (! isMarker (type, Path::closeSubPathMarker))
  63. {
  64. x2 = points [index++];
  65. y2 = points [index++];
  66. if (isMarker (type, Path::quadMarker))
  67. {
  68. x3 = points [index++];
  69. y3 = points [index++];
  70. if (! isIdentityTransform)
  71. transform.transformPoints (x2, y2, x3, y3);
  72. }
  73. else if (isMarker (type, Path::cubicMarker))
  74. {
  75. x3 = points [index++];
  76. y3 = points [index++];
  77. x4 = points [index++];
  78. y4 = points [index++];
  79. if (! isIdentityTransform)
  80. transform.transformPoints (x2, y2, x3, y3, x4, y4);
  81. }
  82. else
  83. {
  84. if (! isIdentityTransform)
  85. transform.transformPoint (x2, y2);
  86. }
  87. }
  88. }
  89. else
  90. {
  91. type = *--stackPos;
  92. if (! isMarker (type, Path::closeSubPathMarker))
  93. {
  94. x2 = *--stackPos;
  95. y2 = *--stackPos;
  96. if (isMarker (type, Path::quadMarker))
  97. {
  98. x3 = *--stackPos;
  99. y3 = *--stackPos;
  100. }
  101. else if (isMarker (type, Path::cubicMarker))
  102. {
  103. x3 = *--stackPos;
  104. y3 = *--stackPos;
  105. x4 = *--stackPos;
  106. y4 = *--stackPos;
  107. }
  108. }
  109. }
  110. if (isMarker (type, Path::lineMarker))
  111. {
  112. ++subPathIndex;
  113. closesSubPath = (stackPos == stackBase)
  114. && (index < path.numElements)
  115. && (points [index] == Path::closeSubPathMarker)
  116. && x2 == subPathCloseX
  117. && y2 == subPathCloseY;
  118. return true;
  119. }
  120. if (isMarker (type, Path::quadMarker))
  121. {
  122. const size_t offset = (size_t) (stackPos - stackBase);
  123. if (offset >= stackSize - 10)
  124. {
  125. stackSize <<= 1;
  126. stackBase.realloc (stackSize);
  127. stackPos = stackBase + offset;
  128. }
  129. auto m1x = (x1 + x2) * 0.5f;
  130. auto m1y = (y1 + y2) * 0.5f;
  131. auto m2x = (x2 + x3) * 0.5f;
  132. auto m2y = (y2 + y3) * 0.5f;
  133. auto m3x = (m1x + m2x) * 0.5f;
  134. auto m3y = (m1y + m2y) * 0.5f;
  135. auto errorX = m3x - x2;
  136. auto errorY = m3y - y2;
  137. if (errorX * errorX + errorY * errorY > toleranceSquared)
  138. {
  139. *stackPos++ = y3;
  140. *stackPos++ = x3;
  141. *stackPos++ = m2y;
  142. *stackPos++ = m2x;
  143. *stackPos++ = Path::quadMarker;
  144. *stackPos++ = m3y;
  145. *stackPos++ = m3x;
  146. *stackPos++ = m1y;
  147. *stackPos++ = m1x;
  148. *stackPos++ = Path::quadMarker;
  149. }
  150. else
  151. {
  152. *stackPos++ = y3;
  153. *stackPos++ = x3;
  154. *stackPos++ = Path::lineMarker;
  155. *stackPos++ = m3y;
  156. *stackPos++ = m3x;
  157. *stackPos++ = Path::lineMarker;
  158. }
  159. jassert (stackPos < stackBase + stackSize);
  160. }
  161. else if (isMarker (type, Path::cubicMarker))
  162. {
  163. const size_t offset = (size_t) (stackPos - stackBase);
  164. if (offset >= stackSize - 16)
  165. {
  166. stackSize <<= 1;
  167. stackBase.realloc (stackSize);
  168. stackPos = stackBase + offset;
  169. }
  170. auto m1x = (x1 + x2) * 0.5f;
  171. auto m1y = (y1 + y2) * 0.5f;
  172. auto m2x = (x3 + x2) * 0.5f;
  173. auto m2y = (y3 + y2) * 0.5f;
  174. auto m3x = (x3 + x4) * 0.5f;
  175. auto m3y = (y3 + y4) * 0.5f;
  176. auto m4x = (m1x + m2x) * 0.5f;
  177. auto m4y = (m1y + m2y) * 0.5f;
  178. auto m5x = (m3x + m2x) * 0.5f;
  179. auto m5y = (m3y + m2y) * 0.5f;
  180. auto error1X = m4x - x2;
  181. auto error1Y = m4y - y2;
  182. auto error2X = m5x - x3;
  183. auto error2Y = m5y - y3;
  184. if (error1X * error1X + error1Y * error1Y > toleranceSquared
  185. || error2X * error2X + error2Y * error2Y > toleranceSquared)
  186. {
  187. *stackPos++ = y4;
  188. *stackPos++ = x4;
  189. *stackPos++ = m3y;
  190. *stackPos++ = m3x;
  191. *stackPos++ = m5y;
  192. *stackPos++ = m5x;
  193. *stackPos++ = Path::cubicMarker;
  194. *stackPos++ = (m4y + m5y) * 0.5f;
  195. *stackPos++ = (m4x + m5x) * 0.5f;
  196. *stackPos++ = m4y;
  197. *stackPos++ = m4x;
  198. *stackPos++ = m1y;
  199. *stackPos++ = m1x;
  200. *stackPos++ = Path::cubicMarker;
  201. }
  202. else
  203. {
  204. *stackPos++ = y4;
  205. *stackPos++ = x4;
  206. *stackPos++ = Path::lineMarker;
  207. *stackPos++ = m5y;
  208. *stackPos++ = m5x;
  209. *stackPos++ = Path::lineMarker;
  210. *stackPos++ = m4y;
  211. *stackPos++ = m4x;
  212. *stackPos++ = Path::lineMarker;
  213. }
  214. }
  215. else if (isMarker (type, Path::closeSubPathMarker))
  216. {
  217. if (x2 != subPathCloseX || y2 != subPathCloseY)
  218. {
  219. x1 = x2;
  220. y1 = y2;
  221. x2 = subPathCloseX;
  222. y2 = subPathCloseY;
  223. closesSubPath = true;
  224. return true;
  225. }
  226. }
  227. else
  228. {
  229. jassert (isMarker (type, Path::moveMarker));
  230. subPathIndex = -1;
  231. subPathCloseX = x1 = x2;
  232. subPathCloseY = y1 = y2;
  233. }
  234. }
  235. }
  236. #if JUCE_MSVC && JUCE_DEBUG
  237. #pragma optimize ("", on) // resets optimisations to the project defaults
  238. #endif