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.

1560 lines
45KB

  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. namespace juce
  20. {
  21. // tests that some coordinates aren't NaNs
  22. #define JUCE_CHECK_COORDS_ARE_VALID(x, y) \
  23. jassert (x == x && y == y);
  24. //==============================================================================
  25. namespace PathHelpers
  26. {
  27. const float ellipseAngularIncrement = 0.05f;
  28. static String nextToken (String::CharPointerType& t)
  29. {
  30. t = t.findEndOfWhitespace();
  31. auto start = t;
  32. size_t numChars = 0;
  33. while (! (t.isEmpty() || t.isWhitespace()))
  34. {
  35. ++t;
  36. ++numChars;
  37. }
  38. return { start, numChars };
  39. }
  40. inline double lengthOf (float x1, float y1, float x2, float y2) noexcept
  41. {
  42. return juce_hypot ((double) (x1 - x2), (double) (y1 - y2));
  43. }
  44. }
  45. //==============================================================================
  46. const float Path::lineMarker = 100001.0f;
  47. const float Path::moveMarker = 100002.0f;
  48. const float Path::quadMarker = 100003.0f;
  49. const float Path::cubicMarker = 100004.0f;
  50. const float Path::closeSubPathMarker = 100005.0f;
  51. const float Path::defaultToleranceForTesting = 1.0f;
  52. const float Path::defaultToleranceForMeasurement = 0.6f;
  53. static inline bool isMarker (float value, float marker) noexcept
  54. {
  55. return value == marker;
  56. }
  57. //==============================================================================
  58. Path::PathBounds::PathBounds() noexcept
  59. {
  60. }
  61. Rectangle<float> Path::PathBounds::getRectangle() const noexcept
  62. {
  63. return { pathXMin, pathYMin, pathXMax - pathXMin, pathYMax - pathYMin };
  64. }
  65. void Path::PathBounds::reset() noexcept
  66. {
  67. pathXMin = pathYMin = pathYMax = pathXMax = 0;
  68. }
  69. void Path::PathBounds::reset (const float x, const float y) noexcept
  70. {
  71. pathXMin = pathXMax = x;
  72. pathYMin = pathYMax = y;
  73. }
  74. void Path::PathBounds::extend (const float x, const float y) noexcept
  75. {
  76. pathXMin = jmin (pathXMin, x);
  77. pathXMax = jmax (pathXMax, x);
  78. pathYMin = jmin (pathYMin, y);
  79. pathYMax = jmax (pathYMax, y);
  80. }
  81. void Path::PathBounds::extend (const float x1, const float y1, const float x2, const float y2) noexcept
  82. {
  83. if (x1 < x2)
  84. {
  85. pathXMin = jmin (pathXMin, x1);
  86. pathXMax = jmax (pathXMax, x2);
  87. }
  88. else
  89. {
  90. pathXMin = jmin (pathXMin, x2);
  91. pathXMax = jmax (pathXMax, x1);
  92. }
  93. if (y1 < y2)
  94. {
  95. pathYMin = jmin (pathYMin, y1);
  96. pathYMax = jmax (pathYMax, y2);
  97. }
  98. else
  99. {
  100. pathYMin = jmin (pathYMin, y2);
  101. pathYMax = jmax (pathYMax, y1);
  102. }
  103. }
  104. //==============================================================================
  105. Path::Path()
  106. {
  107. }
  108. Path::~Path()
  109. {
  110. }
  111. Path::Path (const Path& other)
  112. : data (other.data),
  113. bounds (other.bounds),
  114. useNonZeroWinding (other.useNonZeroWinding)
  115. {
  116. }
  117. Path& Path::operator= (const Path& other)
  118. {
  119. if (this != &other)
  120. {
  121. data = other.data;
  122. bounds = other.bounds;
  123. useNonZeroWinding = other.useNonZeroWinding;
  124. }
  125. return *this;
  126. }
  127. Path::Path (Path&& other) noexcept
  128. : data (static_cast<Array<float>&&> (other.data)),
  129. bounds (other.bounds),
  130. useNonZeroWinding (other.useNonZeroWinding)
  131. {
  132. }
  133. Path& Path::operator= (Path&& other) noexcept
  134. {
  135. data = static_cast<Array<float>&&> (other.data);
  136. bounds = other.bounds;
  137. useNonZeroWinding = other.useNonZeroWinding;
  138. return *this;
  139. }
  140. bool Path::operator== (const Path& other) const noexcept { return useNonZeroWinding == other.useNonZeroWinding && data == other.data; }
  141. bool Path::operator!= (const Path& other) const noexcept { return ! operator== (other); }
  142. void Path::clear() noexcept
  143. {
  144. data.clearQuick();
  145. bounds.reset();
  146. }
  147. void Path::swapWithPath (Path& other) noexcept
  148. {
  149. data.swapWith (other.data);
  150. std::swap (bounds.pathXMin, other.bounds.pathXMin);
  151. std::swap (bounds.pathXMax, other.bounds.pathXMax);
  152. std::swap (bounds.pathYMin, other.bounds.pathYMin);
  153. std::swap (bounds.pathYMax, other.bounds.pathYMax);
  154. std::swap (useNonZeroWinding, other.useNonZeroWinding);
  155. }
  156. //==============================================================================
  157. void Path::setUsingNonZeroWinding (const bool isNonZero) noexcept
  158. {
  159. useNonZeroWinding = isNonZero;
  160. }
  161. void Path::scaleToFit (float x, float y, float w, float h, bool preserveProportions) noexcept
  162. {
  163. applyTransform (getTransformToScaleToFit (x, y, w, h, preserveProportions));
  164. }
  165. //==============================================================================
  166. bool Path::isEmpty() const noexcept
  167. {
  168. for (auto i = data.begin(), e = data.end(); i != e; ++i)
  169. {
  170. auto type = *i;
  171. if (isMarker (type, moveMarker))
  172. {
  173. i += 2;
  174. }
  175. else if (isMarker (type, lineMarker)
  176. || isMarker (type, quadMarker)
  177. || isMarker (type, cubicMarker))
  178. {
  179. return false;
  180. }
  181. }
  182. return true;
  183. }
  184. Rectangle<float> Path::getBounds() const noexcept
  185. {
  186. return bounds.getRectangle();
  187. }
  188. Rectangle<float> Path::getBoundsTransformed (const AffineTransform& transform) const noexcept
  189. {
  190. return getBounds().transformedBy (transform);
  191. }
  192. //==============================================================================
  193. void Path::preallocateSpace (int numExtraCoordsToMakeSpaceFor)
  194. {
  195. data.ensureStorageAllocated (data.size() + numExtraCoordsToMakeSpaceFor);
  196. }
  197. void Path::startNewSubPath (const float x, const float y)
  198. {
  199. JUCE_CHECK_COORDS_ARE_VALID (x, y);
  200. if (data.isEmpty())
  201. bounds.reset (x, y);
  202. else
  203. bounds.extend (x, y);
  204. data.add (moveMarker, x, y);
  205. }
  206. void Path::startNewSubPath (Point<float> start)
  207. {
  208. startNewSubPath (start.x, start.y);
  209. }
  210. void Path::lineTo (const float x, const float y)
  211. {
  212. JUCE_CHECK_COORDS_ARE_VALID (x, y);
  213. if (data.isEmpty())
  214. startNewSubPath (0, 0);
  215. data.add (lineMarker, x, y);
  216. bounds.extend (x, y);
  217. }
  218. void Path::lineTo (Point<float> end)
  219. {
  220. lineTo (end.x, end.y);
  221. }
  222. void Path::quadraticTo (const float x1, const float y1,
  223. const float x2, const float y2)
  224. {
  225. JUCE_CHECK_COORDS_ARE_VALID (x1, y1);
  226. JUCE_CHECK_COORDS_ARE_VALID (x2, y2);
  227. if (data.isEmpty())
  228. startNewSubPath (0, 0);
  229. data.add (quadMarker, x1, y1, x2, y2);
  230. bounds.extend (x1, y1, x2, y2);
  231. }
  232. void Path::quadraticTo (Point<float> controlPoint, Point<float> endPoint)
  233. {
  234. quadraticTo (controlPoint.x, controlPoint.y,
  235. endPoint.x, endPoint.y);
  236. }
  237. void Path::cubicTo (const float x1, const float y1,
  238. const float x2, const float y2,
  239. const float x3, const float y3)
  240. {
  241. JUCE_CHECK_COORDS_ARE_VALID (x1, y1);
  242. JUCE_CHECK_COORDS_ARE_VALID (x2, y2);
  243. JUCE_CHECK_COORDS_ARE_VALID (x3, y3);
  244. if (data.isEmpty())
  245. startNewSubPath (0, 0);
  246. data.add (cubicMarker, x1, y1, x2, y2, x3, y3);
  247. bounds.extend (x1, y1, x2, y2);
  248. bounds.extend (x3, y3);
  249. }
  250. void Path::cubicTo (Point<float> controlPoint1,
  251. Point<float> controlPoint2,
  252. Point<float> endPoint)
  253. {
  254. cubicTo (controlPoint1.x, controlPoint1.y,
  255. controlPoint2.x, controlPoint2.y,
  256. endPoint.x, endPoint.y);
  257. }
  258. void Path::closeSubPath()
  259. {
  260. if (! (data.isEmpty() || isMarker (data.getLast(), closeSubPathMarker)))
  261. data.add (closeSubPathMarker);
  262. }
  263. Point<float> Path::getCurrentPosition() const
  264. {
  265. if (data.isEmpty())
  266. return {};
  267. auto* i = data.end() - 1;
  268. if (isMarker (*i, closeSubPathMarker))
  269. {
  270. while (i != data.begin())
  271. {
  272. if (isMarker (*--i, moveMarker))
  273. {
  274. i += 2;
  275. break;
  276. }
  277. }
  278. }
  279. if (i != data.begin())
  280. return { *(i - 1), *i };
  281. return {};
  282. }
  283. void Path::addRectangle (float x, float y, float w, float h)
  284. {
  285. auto x1 = x, y1 = y, x2 = x + w, y2 = y + h;
  286. if (w < 0) std::swap (x1, x2);
  287. if (h < 0) std::swap (y1, y2);
  288. if (data.isEmpty())
  289. {
  290. bounds.pathXMin = x1;
  291. bounds.pathXMax = x2;
  292. bounds.pathYMin = y1;
  293. bounds.pathYMax = y2;
  294. }
  295. else
  296. {
  297. bounds.pathXMin = jmin (bounds.pathXMin, x1);
  298. bounds.pathXMax = jmax (bounds.pathXMax, x2);
  299. bounds.pathYMin = jmin (bounds.pathYMin, y1);
  300. bounds.pathYMax = jmax (bounds.pathYMax, y2);
  301. }
  302. data.add (moveMarker, x1, y2,
  303. lineMarker, x1, y1,
  304. lineMarker, x2, y1,
  305. lineMarker, x2, y2,
  306. closeSubPathMarker);
  307. }
  308. void Path::addRoundedRectangle (float x, float y, float w, float h, float csx, float csy)
  309. {
  310. addRoundedRectangle (x, y, w, h, csx, csy, true, true, true, true);
  311. }
  312. void Path::addRoundedRectangle (const float x, const float y, const float w, const float h,
  313. float csx, float csy,
  314. const bool curveTopLeft, const bool curveTopRight,
  315. const bool curveBottomLeft, const bool curveBottomRight)
  316. {
  317. csx = jmin (csx, w * 0.5f);
  318. csy = jmin (csy, h * 0.5f);
  319. auto cs45x = csx * 0.45f;
  320. auto cs45y = csy * 0.45f;
  321. auto x2 = x + w;
  322. auto y2 = y + h;
  323. if (curveTopLeft)
  324. {
  325. startNewSubPath (x, y + csy);
  326. cubicTo (x, y + cs45y, x + cs45x, y, x + csx, y);
  327. }
  328. else
  329. {
  330. startNewSubPath (x, y);
  331. }
  332. if (curveTopRight)
  333. {
  334. lineTo (x2 - csx, y);
  335. cubicTo (x2 - cs45x, y, x2, y + cs45y, x2, y + csy);
  336. }
  337. else
  338. {
  339. lineTo (x2, y);
  340. }
  341. if (curveBottomRight)
  342. {
  343. lineTo (x2, y2 - csy);
  344. cubicTo (x2, y2 - cs45y, x2 - cs45x, y2, x2 - csx, y2);
  345. }
  346. else
  347. {
  348. lineTo (x2, y2);
  349. }
  350. if (curveBottomLeft)
  351. {
  352. lineTo (x + csx, y2);
  353. cubicTo (x + cs45x, y2, x, y2 - cs45y, x, y2 - csy);
  354. }
  355. else
  356. {
  357. lineTo (x, y2);
  358. }
  359. closeSubPath();
  360. }
  361. void Path::addRoundedRectangle (float x, float y, float w, float h, float cs)
  362. {
  363. addRoundedRectangle (x, y, w, h, cs, cs);
  364. }
  365. void Path::addTriangle (float x1, float y1,
  366. float x2, float y2,
  367. float x3, float y3)
  368. {
  369. addTriangle ({ x1, y1 },
  370. { x2, y2 },
  371. { x3, y3 });
  372. }
  373. void Path::addTriangle (Point<float> p1, Point<float> p2, Point<float> p3)
  374. {
  375. startNewSubPath (p1);
  376. lineTo (p2);
  377. lineTo (p3);
  378. closeSubPath();
  379. }
  380. void Path::addQuadrilateral (float x1, float y1,
  381. float x2, float y2,
  382. float x3, float y3,
  383. float x4, float y4)
  384. {
  385. startNewSubPath (x1, y1);
  386. lineTo (x2, y2);
  387. lineTo (x3, y3);
  388. lineTo (x4, y4);
  389. closeSubPath();
  390. }
  391. void Path::addEllipse (float x, float y, float w, float h)
  392. {
  393. addEllipse ({ x, y, w, h });
  394. }
  395. void Path::addEllipse (Rectangle<float> area)
  396. {
  397. auto hw = area.getWidth() * 0.5f;
  398. auto hw55 = hw * 0.55f;
  399. auto hh = area.getHeight() * 0.5f;
  400. auto hh55 = hh * 0.55f;
  401. auto cx = area.getX() + hw;
  402. auto cy = area.getY() + hh;
  403. startNewSubPath (cx, cy - hh);
  404. cubicTo (cx + hw55, cy - hh, cx + hw, cy - hh55, cx + hw, cy);
  405. cubicTo (cx + hw, cy + hh55, cx + hw55, cy + hh, cx, cy + hh);
  406. cubicTo (cx - hw55, cy + hh, cx - hw, cy + hh55, cx - hw, cy);
  407. cubicTo (cx - hw, cy - hh55, cx - hw55, cy - hh, cx, cy - hh);
  408. closeSubPath();
  409. }
  410. void Path::addArc (float x, float y, float w, float h,
  411. float fromRadians, float toRadians,
  412. bool startAsNewSubPath)
  413. {
  414. auto radiusX = w / 2.0f;
  415. auto radiusY = h / 2.0f;
  416. addCentredArc (x + radiusX,
  417. y + radiusY,
  418. radiusX, radiusY,
  419. 0.0f,
  420. fromRadians, toRadians,
  421. startAsNewSubPath);
  422. }
  423. void Path::addCentredArc (float centreX, float centreY,
  424. float radiusX, float radiusY,
  425. float rotationOfEllipse,
  426. float fromRadians, float toRadians,
  427. bool startAsNewSubPath)
  428. {
  429. if (radiusX > 0.0f && radiusY > 0.0f)
  430. {
  431. Point<float> centre (centreX, centreY);
  432. auto rotation = AffineTransform::rotation (rotationOfEllipse, centreX, centreY);
  433. auto angle = fromRadians;
  434. if (startAsNewSubPath)
  435. startNewSubPath (centre.getPointOnCircumference (radiusX, radiusY, angle).transformedBy (rotation));
  436. if (fromRadians < toRadians)
  437. {
  438. if (startAsNewSubPath)
  439. angle += PathHelpers::ellipseAngularIncrement;
  440. while (angle < toRadians)
  441. {
  442. lineTo (centre.getPointOnCircumference (radiusX, radiusY, angle).transformedBy (rotation));
  443. angle += PathHelpers::ellipseAngularIncrement;
  444. }
  445. }
  446. else
  447. {
  448. if (startAsNewSubPath)
  449. angle -= PathHelpers::ellipseAngularIncrement;
  450. while (angle > toRadians)
  451. {
  452. lineTo (centre.getPointOnCircumference (radiusX, radiusY, angle).transformedBy (rotation));
  453. angle -= PathHelpers::ellipseAngularIncrement;
  454. }
  455. }
  456. lineTo (centre.getPointOnCircumference (radiusX, radiusY, toRadians).transformedBy (rotation));
  457. }
  458. }
  459. void Path::addPieSegment (float x, float y, float width, float height,
  460. float fromRadians, float toRadians,
  461. float innerCircleProportionalSize)
  462. {
  463. auto radiusX = width * 0.5f;
  464. auto radiusY = height * 0.5f;
  465. Point<float> centre (x + radiusX, y + radiusY);
  466. startNewSubPath (centre.getPointOnCircumference (radiusX, radiusY, fromRadians));
  467. addArc (x, y, width, height, fromRadians, toRadians);
  468. if (std::abs (fromRadians - toRadians) > MathConstants<float>::pi * 1.999f)
  469. {
  470. closeSubPath();
  471. if (innerCircleProportionalSize > 0)
  472. {
  473. radiusX *= innerCircleProportionalSize;
  474. radiusY *= innerCircleProportionalSize;
  475. startNewSubPath (centre.getPointOnCircumference (radiusX, radiusY, toRadians));
  476. addArc (centre.x - radiusX, centre.y - radiusY, radiusX * 2.0f, radiusY * 2.0f, toRadians, fromRadians);
  477. }
  478. }
  479. else
  480. {
  481. if (innerCircleProportionalSize > 0)
  482. {
  483. radiusX *= innerCircleProportionalSize;
  484. radiusY *= innerCircleProportionalSize;
  485. addArc (centre.x - radiusX, centre.y - radiusY, radiusX * 2.0f, radiusY * 2.0f, toRadians, fromRadians);
  486. }
  487. else
  488. {
  489. lineTo (centre);
  490. }
  491. }
  492. closeSubPath();
  493. }
  494. void Path::addPieSegment (Rectangle<float> segmentBounds,
  495. float fromRadians, float toRadians,
  496. float innerCircleProportionalSize)
  497. {
  498. addPieSegment (segmentBounds.getX(),
  499. segmentBounds.getY(),
  500. segmentBounds.getWidth(),
  501. segmentBounds.getHeight(),
  502. fromRadians,
  503. toRadians,
  504. innerCircleProportionalSize);
  505. }
  506. //==============================================================================
  507. void Path::addLineSegment (Line<float> line, float lineThickness)
  508. {
  509. auto reversed = line.reversed();
  510. lineThickness *= 0.5f;
  511. startNewSubPath (line.getPointAlongLine (0, lineThickness));
  512. lineTo (line.getPointAlongLine (0, -lineThickness));
  513. lineTo (reversed.getPointAlongLine (0, lineThickness));
  514. lineTo (reversed.getPointAlongLine (0, -lineThickness));
  515. closeSubPath();
  516. }
  517. void Path::addArrow (Line<float> line, float lineThickness,
  518. float arrowheadWidth, float arrowheadLength)
  519. {
  520. auto reversed = line.reversed();
  521. lineThickness *= 0.5f;
  522. arrowheadWidth *= 0.5f;
  523. arrowheadLength = jmin (arrowheadLength, 0.8f * line.getLength());
  524. startNewSubPath (line.getPointAlongLine (0, lineThickness));
  525. lineTo (line.getPointAlongLine (0, -lineThickness));
  526. lineTo (reversed.getPointAlongLine (arrowheadLength, lineThickness));
  527. lineTo (reversed.getPointAlongLine (arrowheadLength, arrowheadWidth));
  528. lineTo (line.getEnd());
  529. lineTo (reversed.getPointAlongLine (arrowheadLength, -arrowheadWidth));
  530. lineTo (reversed.getPointAlongLine (arrowheadLength, -lineThickness));
  531. closeSubPath();
  532. }
  533. void Path::addPolygon (Point<float> centre, int numberOfSides,
  534. float radius, float startAngle)
  535. {
  536. jassert (numberOfSides > 1); // this would be silly.
  537. if (numberOfSides > 1)
  538. {
  539. auto angleBetweenPoints = MathConstants<float>::twoPi / numberOfSides;
  540. for (int i = 0; i < numberOfSides; ++i)
  541. {
  542. auto angle = startAngle + i * angleBetweenPoints;
  543. auto p = centre.getPointOnCircumference (radius, angle);
  544. if (i == 0)
  545. startNewSubPath (p);
  546. else
  547. lineTo (p);
  548. }
  549. closeSubPath();
  550. }
  551. }
  552. void Path::addStar (Point<float> centre, int numberOfPoints, float innerRadius,
  553. float outerRadius, float startAngle)
  554. {
  555. jassert (numberOfPoints > 1); // this would be silly.
  556. if (numberOfPoints > 1)
  557. {
  558. auto angleBetweenPoints = MathConstants<float>::twoPi / numberOfPoints;
  559. for (int i = 0; i < numberOfPoints; ++i)
  560. {
  561. auto angle = startAngle + i * angleBetweenPoints;
  562. auto p = centre.getPointOnCircumference (outerRadius, angle);
  563. if (i == 0)
  564. startNewSubPath (p);
  565. else
  566. lineTo (p);
  567. lineTo (centre.getPointOnCircumference (innerRadius, angle + angleBetweenPoints * 0.5f));
  568. }
  569. closeSubPath();
  570. }
  571. }
  572. void Path::addBubble (Rectangle<float> bodyArea,
  573. Rectangle<float> maximumArea,
  574. Point<float> arrowTip,
  575. float cornerSize,
  576. float arrowBaseWidth)
  577. {
  578. auto halfW = bodyArea.getWidth() / 2.0f;
  579. auto halfH = bodyArea.getHeight() / 2.0f;
  580. auto cornerSizeW = jmin (cornerSize, halfW);
  581. auto cornerSizeH = jmin (cornerSize, halfH);
  582. auto cornerSizeW2 = 2.0f * cornerSizeW;
  583. auto cornerSizeH2 = 2.0f * cornerSizeH;
  584. startNewSubPath (bodyArea.getX() + cornerSizeW, bodyArea.getY());
  585. const Rectangle<float> targetLimit (bodyArea.reduced (jmin (halfW - 1.0f, cornerSizeW + arrowBaseWidth),
  586. jmin (halfH - 1.0f, cornerSizeH + arrowBaseWidth)));
  587. if (Rectangle<float> (targetLimit.getX(), maximumArea.getY(),
  588. targetLimit.getWidth(), bodyArea.getY() - maximumArea.getY()).contains (arrowTip))
  589. {
  590. lineTo (arrowTip.x - arrowBaseWidth, bodyArea.getY());
  591. lineTo (arrowTip.x, arrowTip.y);
  592. lineTo (arrowTip.x + arrowBaseWidth, bodyArea.getY());
  593. }
  594. lineTo (bodyArea.getRight() - cornerSizeW, bodyArea.getY());
  595. addArc (bodyArea.getRight() - cornerSizeW2, bodyArea.getY(), cornerSizeW2, cornerSizeH2, 0, MathConstants<float>::halfPi);
  596. if (Rectangle<float> (bodyArea.getRight(), targetLimit.getY(),
  597. maximumArea.getRight() - bodyArea.getRight(), targetLimit.getHeight()).contains (arrowTip))
  598. {
  599. lineTo (bodyArea.getRight(), arrowTip.y - arrowBaseWidth);
  600. lineTo (arrowTip.x, arrowTip.y);
  601. lineTo (bodyArea.getRight(), arrowTip.y + arrowBaseWidth);
  602. }
  603. lineTo (bodyArea.getRight(), bodyArea.getBottom() - cornerSizeH);
  604. addArc (bodyArea.getRight() - cornerSizeW2, bodyArea.getBottom() - cornerSizeH2, cornerSizeW2, cornerSizeH2, MathConstants<float>::halfPi, MathConstants<float>::pi);
  605. if (Rectangle<float> (targetLimit.getX(), bodyArea.getBottom(),
  606. targetLimit.getWidth(), maximumArea.getBottom() - bodyArea.getBottom()).contains (arrowTip))
  607. {
  608. lineTo (arrowTip.x + arrowBaseWidth, bodyArea.getBottom());
  609. lineTo (arrowTip.x, arrowTip.y);
  610. lineTo (arrowTip.x - arrowBaseWidth, bodyArea.getBottom());
  611. }
  612. lineTo (bodyArea.getX() + cornerSizeW, bodyArea.getBottom());
  613. addArc (bodyArea.getX(), bodyArea.getBottom() - cornerSizeH2, cornerSizeW2, cornerSizeH2, MathConstants<float>::pi, MathConstants<float>::pi * 1.5f);
  614. if (Rectangle<float> (maximumArea.getX(), targetLimit.getY(),
  615. bodyArea.getX() - maximumArea.getX(), targetLimit.getHeight()).contains (arrowTip))
  616. {
  617. lineTo (bodyArea.getX(), arrowTip.y + arrowBaseWidth);
  618. lineTo (arrowTip.x, arrowTip.y);
  619. lineTo (bodyArea.getX(), arrowTip.y - arrowBaseWidth);
  620. }
  621. lineTo (bodyArea.getX(), bodyArea.getY() + cornerSizeH);
  622. addArc (bodyArea.getX(), bodyArea.getY(), cornerSizeW2, cornerSizeH2, MathConstants<float>::pi * 1.5f, MathConstants<float>::twoPi - 0.05f);
  623. closeSubPath();
  624. }
  625. void Path::addPath (const Path& other)
  626. {
  627. const auto* d = other.data.begin();
  628. for (int i = 0; i < other.data.size();)
  629. {
  630. auto type = d[i++];
  631. if (isMarker (type, moveMarker))
  632. {
  633. startNewSubPath (d[i], d[i + 1]);
  634. i += 2;
  635. }
  636. else if (isMarker (type, lineMarker))
  637. {
  638. lineTo (d[i], d[i + 1]);
  639. i += 2;
  640. }
  641. else if (isMarker (type, quadMarker))
  642. {
  643. quadraticTo (d[i], d[i + 1], d[i + 2], d[i + 3]);
  644. i += 4;
  645. }
  646. else if (isMarker (type, cubicMarker))
  647. {
  648. cubicTo (d[i], d[i + 1], d[i + 2], d[i + 3], d[i + 4], d[i + 5]);
  649. i += 6;
  650. }
  651. else if (isMarker (type, closeSubPathMarker))
  652. {
  653. closeSubPath();
  654. }
  655. else
  656. {
  657. // something's gone wrong with the element list!
  658. jassertfalse;
  659. }
  660. }
  661. }
  662. void Path::addPath (const Path& other,
  663. const AffineTransform& transformToApply)
  664. {
  665. const auto* d = other.data.begin();
  666. for (int i = 0; i < other.data.size();)
  667. {
  668. auto type = d[i++];
  669. if (isMarker (type, closeSubPathMarker))
  670. {
  671. closeSubPath();
  672. }
  673. else
  674. {
  675. auto x = d[i++];
  676. auto y = d[i++];
  677. transformToApply.transformPoint (x, y);
  678. if (isMarker (type, moveMarker))
  679. {
  680. startNewSubPath (x, y);
  681. }
  682. else if (isMarker (type, lineMarker))
  683. {
  684. lineTo (x, y);
  685. }
  686. else if (isMarker (type, quadMarker))
  687. {
  688. auto x2 = d[i++];
  689. auto y2 = d[i++];
  690. transformToApply.transformPoint (x2, y2);
  691. quadraticTo (x, y, x2, y2);
  692. }
  693. else if (isMarker (type, cubicMarker))
  694. {
  695. auto x2 = d[i++];
  696. auto y2 = d[i++];
  697. auto x3 = d[i++];
  698. auto y3 = d[i++];
  699. transformToApply.transformPoints (x2, y2, x3, y3);
  700. cubicTo (x, y, x2, y2, x3, y3);
  701. }
  702. else
  703. {
  704. // something's gone wrong with the element list!
  705. jassertfalse;
  706. }
  707. }
  708. }
  709. }
  710. //==============================================================================
  711. void Path::applyTransform (const AffineTransform& transform) noexcept
  712. {
  713. bounds.reset();
  714. bool firstPoint = true;
  715. float* d = data.begin();
  716. auto* end = data.end();
  717. while (d < end)
  718. {
  719. auto type = *d++;
  720. if (isMarker (type, moveMarker))
  721. {
  722. transform.transformPoint (d[0], d[1]);
  723. if (firstPoint)
  724. {
  725. firstPoint = false;
  726. bounds.reset (d[0], d[1]);
  727. }
  728. else
  729. {
  730. bounds.extend (d[0], d[1]);
  731. }
  732. d += 2;
  733. }
  734. else if (isMarker (type, lineMarker))
  735. {
  736. transform.transformPoint (d[0], d[1]);
  737. bounds.extend (d[0], d[1]);
  738. d += 2;
  739. }
  740. else if (isMarker (type, quadMarker))
  741. {
  742. transform.transformPoints (d[0], d[1], d[2], d[3]);
  743. bounds.extend (d[0], d[1], d[2], d[3]);
  744. d += 4;
  745. }
  746. else if (isMarker (type, cubicMarker))
  747. {
  748. transform.transformPoints (d[0], d[1], d[2], d[3], d[4], d[5]);
  749. bounds.extend (d[0], d[1], d[2], d[3]);
  750. bounds.extend (d[4], d[5]);
  751. d += 6;
  752. }
  753. }
  754. }
  755. //==============================================================================
  756. AffineTransform Path::getTransformToScaleToFit (Rectangle<float> area, bool preserveProportions,
  757. Justification justification) const
  758. {
  759. return getTransformToScaleToFit (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  760. preserveProportions, justification);
  761. }
  762. AffineTransform Path::getTransformToScaleToFit (float x, float y, float w, float h,
  763. bool preserveProportions,
  764. Justification justification) const
  765. {
  766. auto boundsRect = getBounds();
  767. if (preserveProportions)
  768. {
  769. if (w <= 0 || h <= 0 || boundsRect.isEmpty())
  770. return AffineTransform();
  771. float newW, newH;
  772. auto srcRatio = boundsRect.getHeight() / boundsRect.getWidth();
  773. if (srcRatio > h / w)
  774. {
  775. newW = h / srcRatio;
  776. newH = h;
  777. }
  778. else
  779. {
  780. newW = w;
  781. newH = w * srcRatio;
  782. }
  783. auto newXCentre = x;
  784. auto newYCentre = y;
  785. if (justification.testFlags (Justification::left)) newXCentre += newW * 0.5f;
  786. else if (justification.testFlags (Justification::right)) newXCentre += w - newW * 0.5f;
  787. else newXCentre += w * 0.5f;
  788. if (justification.testFlags (Justification::top)) newYCentre += newH * 0.5f;
  789. else if (justification.testFlags (Justification::bottom)) newYCentre += h - newH * 0.5f;
  790. else newYCentre += h * 0.5f;
  791. return AffineTransform::translation (boundsRect.getWidth() * -0.5f - boundsRect.getX(),
  792. boundsRect.getHeight() * -0.5f - boundsRect.getY())
  793. .scaled (newW / boundsRect.getWidth(),
  794. newH / boundsRect.getHeight())
  795. .translated (newXCentre, newYCentre);
  796. }
  797. else
  798. {
  799. return AffineTransform::translation (-boundsRect.getX(), -boundsRect.getY())
  800. .scaled (w / boundsRect.getWidth(),
  801. h / boundsRect.getHeight())
  802. .translated (x, y);
  803. }
  804. }
  805. //==============================================================================
  806. bool Path::contains (float x, float y, float tolerance) const
  807. {
  808. if (x <= bounds.pathXMin || x >= bounds.pathXMax
  809. || y <= bounds.pathYMin || y >= bounds.pathYMax)
  810. return false;
  811. PathFlatteningIterator i (*this, AffineTransform(), tolerance);
  812. int positiveCrossings = 0;
  813. int negativeCrossings = 0;
  814. while (i.next())
  815. {
  816. if ((i.y1 <= y && i.y2 > y) || (i.y2 <= y && i.y1 > y))
  817. {
  818. auto intersectX = i.x1 + (i.x2 - i.x1) * (y - i.y1) / (i.y2 - i.y1);
  819. if (intersectX <= x)
  820. {
  821. if (i.y1 < i.y2)
  822. ++positiveCrossings;
  823. else
  824. ++negativeCrossings;
  825. }
  826. }
  827. }
  828. return useNonZeroWinding ? (negativeCrossings != positiveCrossings)
  829. : ((negativeCrossings + positiveCrossings) & 1) != 0;
  830. }
  831. bool Path::contains (Point<float> point, float tolerance) const
  832. {
  833. return contains (point.x, point.y, tolerance);
  834. }
  835. bool Path::intersectsLine (Line<float> line, float tolerance)
  836. {
  837. PathFlatteningIterator i (*this, AffineTransform(), tolerance);
  838. Point<float> intersection;
  839. while (i.next())
  840. if (line.intersects (Line<float> (i.x1, i.y1, i.x2, i.y2), intersection))
  841. return true;
  842. return false;
  843. }
  844. Line<float> Path::getClippedLine (Line<float> line, bool keepSectionOutsidePath) const
  845. {
  846. Line<float> result (line);
  847. const bool startInside = contains (line.getStart());
  848. const bool endInside = contains (line.getEnd());
  849. if (startInside == endInside)
  850. {
  851. if (keepSectionOutsidePath == startInside)
  852. result = Line<float>();
  853. }
  854. else
  855. {
  856. PathFlatteningIterator i (*this, AffineTransform());
  857. Point<float> intersection;
  858. while (i.next())
  859. {
  860. if (line.intersects ({ i.x1, i.y1, i.x2, i.y2 }, intersection))
  861. {
  862. if ((startInside && keepSectionOutsidePath) || (endInside && ! keepSectionOutsidePath))
  863. result.setStart (intersection);
  864. else
  865. result.setEnd (intersection);
  866. }
  867. }
  868. }
  869. return result;
  870. }
  871. float Path::getLength (const AffineTransform& transform, float tolerance) const
  872. {
  873. float length = 0;
  874. PathFlatteningIterator i (*this, transform, tolerance);
  875. while (i.next())
  876. length += Line<float> (i.x1, i.y1, i.x2, i.y2).getLength();
  877. return length;
  878. }
  879. Point<float> Path::getPointAlongPath (float distanceFromStart,
  880. const AffineTransform& transform,
  881. float tolerance) const
  882. {
  883. PathFlatteningIterator i (*this, transform, tolerance);
  884. while (i.next())
  885. {
  886. const Line<float> line (i.x1, i.y1, i.x2, i.y2);
  887. auto lineLength = line.getLength();
  888. if (distanceFromStart <= lineLength)
  889. return line.getPointAlongLine (distanceFromStart);
  890. distanceFromStart -= lineLength;
  891. }
  892. return { i.x2, i.y2 };
  893. }
  894. float Path::getNearestPoint (Point<float> targetPoint, Point<float>& pointOnPath,
  895. const AffineTransform& transform,
  896. float tolerance) const
  897. {
  898. PathFlatteningIterator i (*this, transform, tolerance);
  899. float bestPosition = 0, bestDistance = std::numeric_limits<float>::max();
  900. float length = 0;
  901. Point<float> pointOnLine;
  902. while (i.next())
  903. {
  904. const Line<float> line (i.x1, i.y1, i.x2, i.y2);
  905. auto distance = line.getDistanceFromPoint (targetPoint, pointOnLine);
  906. if (distance < bestDistance)
  907. {
  908. bestDistance = distance;
  909. bestPosition = length + pointOnLine.getDistanceFrom (line.getStart());
  910. pointOnPath = pointOnLine;
  911. }
  912. length += line.getLength();
  913. }
  914. return bestPosition;
  915. }
  916. //==============================================================================
  917. Path Path::createPathWithRoundedCorners (const float cornerRadius) const
  918. {
  919. if (cornerRadius <= 0.01f)
  920. return *this;
  921. Path p;
  922. int n = 0, indexOfPathStart = 0, indexOfPathStartThis = 0;
  923. auto* elements = data.begin();
  924. bool lastWasLine = false, firstWasLine = false;
  925. while (n < data.size())
  926. {
  927. auto type = elements[n++];
  928. if (isMarker (type, moveMarker))
  929. {
  930. indexOfPathStart = p.data.size();
  931. indexOfPathStartThis = n - 1;
  932. auto x = elements[n++];
  933. auto y = elements[n++];
  934. p.startNewSubPath (x, y);
  935. lastWasLine = false;
  936. firstWasLine = (isMarker (elements[n], lineMarker));
  937. }
  938. else if (isMarker (type, lineMarker) || isMarker (type, closeSubPathMarker))
  939. {
  940. float startX = 0, startY = 0, joinX = 0, joinY = 0, endX, endY;
  941. if (isMarker (type, lineMarker))
  942. {
  943. endX = elements[n++];
  944. endY = elements[n++];
  945. if (n > 8)
  946. {
  947. startX = elements[n - 8];
  948. startY = elements[n - 7];
  949. joinX = elements[n - 5];
  950. joinY = elements[n - 4];
  951. }
  952. }
  953. else
  954. {
  955. endX = elements[indexOfPathStartThis + 1];
  956. endY = elements[indexOfPathStartThis + 2];
  957. if (n > 6)
  958. {
  959. startX = elements[n - 6];
  960. startY = elements[n - 5];
  961. joinX = elements[n - 3];
  962. joinY = elements[n - 2];
  963. }
  964. }
  965. if (lastWasLine)
  966. {
  967. auto len1 = PathHelpers::lengthOf (startX, startY, joinX, joinY);
  968. if (len1 > 0)
  969. {
  970. auto propNeeded = jmin (0.5, cornerRadius / len1);
  971. *(p.data.end() - 2) = (float) (joinX - (joinX - startX) * propNeeded);
  972. *(p.data.end() - 1) = (float) (joinY - (joinY - startY) * propNeeded);
  973. }
  974. auto len2 = PathHelpers::lengthOf (endX, endY, joinX, joinY);
  975. if (len2 > 0)
  976. {
  977. auto propNeeded = jmin (0.5, cornerRadius / len2);
  978. p.quadraticTo (joinX, joinY,
  979. (float) (joinX + (endX - joinX) * propNeeded),
  980. (float) (joinY + (endY - joinY) * propNeeded));
  981. }
  982. p.lineTo (endX, endY);
  983. }
  984. else if (isMarker (type, lineMarker))
  985. {
  986. p.lineTo (endX, endY);
  987. lastWasLine = true;
  988. }
  989. if (isMarker (type, closeSubPathMarker))
  990. {
  991. if (firstWasLine)
  992. {
  993. startX = elements[n - 3];
  994. startY = elements[n - 2];
  995. joinX = endX;
  996. joinY = endY;
  997. endX = elements[indexOfPathStartThis + 4];
  998. endY = elements[indexOfPathStartThis + 5];
  999. auto len1 = PathHelpers::lengthOf (startX, startY, joinX, joinY);
  1000. if (len1 > 0)
  1001. {
  1002. auto propNeeded = jmin (0.5, cornerRadius / len1);
  1003. *(p.data.end() - 2) = (float) (joinX - (joinX - startX) * propNeeded);
  1004. *(p.data.end() - 1) = (float) (joinY - (joinY - startY) * propNeeded);
  1005. }
  1006. auto len2 = PathHelpers::lengthOf (endX, endY, joinX, joinY);
  1007. if (len2 > 0)
  1008. {
  1009. auto propNeeded = jmin (0.5, cornerRadius / len2);
  1010. endX = (float) (joinX + (endX - joinX) * propNeeded);
  1011. endY = (float) (joinY + (endY - joinY) * propNeeded);
  1012. p.quadraticTo (joinX, joinY, endX, endY);
  1013. p.data.begin()[indexOfPathStart + 1] = endX;
  1014. p.data.begin()[indexOfPathStart + 2] = endY;
  1015. }
  1016. }
  1017. p.closeSubPath();
  1018. }
  1019. }
  1020. else if (isMarker (type, quadMarker))
  1021. {
  1022. lastWasLine = false;
  1023. auto x1 = elements[n++];
  1024. auto y1 = elements[n++];
  1025. auto x2 = elements[n++];
  1026. auto y2 = elements[n++];
  1027. p.quadraticTo (x1, y1, x2, y2);
  1028. }
  1029. else if (isMarker (type, cubicMarker))
  1030. {
  1031. lastWasLine = false;
  1032. auto x1 = elements[n++];
  1033. auto y1 = elements[n++];
  1034. auto x2 = elements[n++];
  1035. auto y2 = elements[n++];
  1036. auto x3 = elements[n++];
  1037. auto y3 = elements[n++];
  1038. p.cubicTo (x1, y1, x2, y2, x3, y3);
  1039. }
  1040. }
  1041. return p;
  1042. }
  1043. //==============================================================================
  1044. void Path::loadPathFromStream (InputStream& source)
  1045. {
  1046. while (! source.isExhausted())
  1047. {
  1048. switch (source.readByte())
  1049. {
  1050. case 'm':
  1051. {
  1052. auto x = source.readFloat();
  1053. auto y = source.readFloat();
  1054. startNewSubPath (x, y);
  1055. break;
  1056. }
  1057. case 'l':
  1058. {
  1059. auto x = source.readFloat();
  1060. auto y = source.readFloat();
  1061. lineTo (x, y);
  1062. break;
  1063. }
  1064. case 'q':
  1065. {
  1066. auto x1 = source.readFloat();
  1067. auto y1 = source.readFloat();
  1068. auto x2 = source.readFloat();
  1069. auto y2 = source.readFloat();
  1070. quadraticTo (x1, y1, x2, y2);
  1071. break;
  1072. }
  1073. case 'b':
  1074. {
  1075. auto x1 = source.readFloat();
  1076. auto y1 = source.readFloat();
  1077. auto x2 = source.readFloat();
  1078. auto y2 = source.readFloat();
  1079. auto x3 = source.readFloat();
  1080. auto y3 = source.readFloat();
  1081. cubicTo (x1, y1, x2, y2, x3, y3);
  1082. break;
  1083. }
  1084. case 'c':
  1085. closeSubPath();
  1086. break;
  1087. case 'n':
  1088. useNonZeroWinding = true;
  1089. break;
  1090. case 'z':
  1091. useNonZeroWinding = false;
  1092. break;
  1093. case 'e':
  1094. return; // end of path marker
  1095. default:
  1096. jassertfalse; // illegal char in the stream
  1097. break;
  1098. }
  1099. }
  1100. }
  1101. void Path::loadPathFromData (const void* const pathData, const size_t numberOfBytes)
  1102. {
  1103. MemoryInputStream in (pathData, numberOfBytes, false);
  1104. loadPathFromStream (in);
  1105. }
  1106. void Path::writePathToStream (OutputStream& dest) const
  1107. {
  1108. dest.writeByte (useNonZeroWinding ? 'n' : 'z');
  1109. for (auto* i = data.begin(); i != data.end();)
  1110. {
  1111. auto type = *i++;
  1112. if (isMarker (type, moveMarker))
  1113. {
  1114. dest.writeByte ('m');
  1115. dest.writeFloat (*i++);
  1116. dest.writeFloat (*i++);
  1117. }
  1118. else if (isMarker (type, lineMarker))
  1119. {
  1120. dest.writeByte ('l');
  1121. dest.writeFloat (*i++);
  1122. dest.writeFloat (*i++);
  1123. }
  1124. else if (isMarker (type, quadMarker))
  1125. {
  1126. dest.writeByte ('q');
  1127. dest.writeFloat (*i++);
  1128. dest.writeFloat (*i++);
  1129. dest.writeFloat (*i++);
  1130. dest.writeFloat (*i++);
  1131. }
  1132. else if (isMarker (type, cubicMarker))
  1133. {
  1134. dest.writeByte ('b');
  1135. dest.writeFloat (*i++);
  1136. dest.writeFloat (*i++);
  1137. dest.writeFloat (*i++);
  1138. dest.writeFloat (*i++);
  1139. dest.writeFloat (*i++);
  1140. dest.writeFloat (*i++);
  1141. }
  1142. else if (isMarker (type, closeSubPathMarker))
  1143. {
  1144. dest.writeByte ('c');
  1145. }
  1146. }
  1147. dest.writeByte ('e'); // marks the end-of-path
  1148. }
  1149. String Path::toString() const
  1150. {
  1151. MemoryOutputStream s (2048);
  1152. if (! useNonZeroWinding)
  1153. s << 'a';
  1154. float lastMarker = 0.0f;
  1155. for (int i = 0; i < data.size();)
  1156. {
  1157. auto type = data.begin()[i++];
  1158. char markerChar = 0;
  1159. int numCoords = 0;
  1160. if (isMarker (type, moveMarker))
  1161. {
  1162. markerChar = 'm';
  1163. numCoords = 2;
  1164. }
  1165. else if (isMarker (type, lineMarker))
  1166. {
  1167. markerChar = 'l';
  1168. numCoords = 2;
  1169. }
  1170. else if (isMarker (type, quadMarker))
  1171. {
  1172. markerChar = 'q';
  1173. numCoords = 4;
  1174. }
  1175. else if (isMarker (type, cubicMarker))
  1176. {
  1177. markerChar = 'c';
  1178. numCoords = 6;
  1179. }
  1180. else
  1181. {
  1182. jassert (isMarker (type, closeSubPathMarker));
  1183. markerChar = 'z';
  1184. }
  1185. if (! isMarker (type, lastMarker))
  1186. {
  1187. if (s.getDataSize() != 0)
  1188. s << ' ';
  1189. s << markerChar;
  1190. lastMarker = type;
  1191. }
  1192. while (--numCoords >= 0 && i < data.size())
  1193. {
  1194. String coord (data.begin()[i++], 3);
  1195. while (coord.endsWithChar ('0') && coord != "0")
  1196. coord = coord.dropLastCharacters (1);
  1197. if (coord.endsWithChar ('.'))
  1198. coord = coord.dropLastCharacters (1);
  1199. if (s.getDataSize() != 0)
  1200. s << ' ';
  1201. s << coord;
  1202. }
  1203. }
  1204. return s.toUTF8();
  1205. }
  1206. void Path::restoreFromString (StringRef stringVersion)
  1207. {
  1208. clear();
  1209. setUsingNonZeroWinding (true);
  1210. auto t = stringVersion.text;
  1211. juce_wchar marker = 'm';
  1212. int numValues = 2;
  1213. float values[6];
  1214. for (;;)
  1215. {
  1216. auto token = PathHelpers::nextToken (t);
  1217. auto firstChar = token[0];
  1218. int startNum = 0;
  1219. if (firstChar == 0)
  1220. break;
  1221. if (firstChar == 'm' || firstChar == 'l')
  1222. {
  1223. marker = firstChar;
  1224. numValues = 2;
  1225. }
  1226. else if (firstChar == 'q')
  1227. {
  1228. marker = firstChar;
  1229. numValues = 4;
  1230. }
  1231. else if (firstChar == 'c')
  1232. {
  1233. marker = firstChar;
  1234. numValues = 6;
  1235. }
  1236. else if (firstChar == 'z')
  1237. {
  1238. marker = firstChar;
  1239. numValues = 0;
  1240. }
  1241. else if (firstChar == 'a')
  1242. {
  1243. setUsingNonZeroWinding (false);
  1244. continue;
  1245. }
  1246. else
  1247. {
  1248. ++startNum;
  1249. values [0] = token.getFloatValue();
  1250. }
  1251. for (int i = startNum; i < numValues; ++i)
  1252. values [i] = PathHelpers::nextToken (t).getFloatValue();
  1253. switch (marker)
  1254. {
  1255. case 'm': startNewSubPath (values[0], values[1]); break;
  1256. case 'l': lineTo (values[0], values[1]); break;
  1257. case 'q': quadraticTo (values[0], values[1], values[2], values[3]); break;
  1258. case 'c': cubicTo (values[0], values[1], values[2], values[3], values[4], values[5]); break;
  1259. case 'z': closeSubPath(); break;
  1260. default: jassertfalse; break; // illegal string format?
  1261. }
  1262. }
  1263. }
  1264. //==============================================================================
  1265. Path::Iterator::Iterator (const Path& p) noexcept
  1266. : elementType (startNewSubPath), path (p), index (path.data.begin())
  1267. {
  1268. }
  1269. Path::Iterator::~Iterator() noexcept
  1270. {
  1271. }
  1272. bool Path::Iterator::next() noexcept
  1273. {
  1274. if (index != path.data.end())
  1275. {
  1276. auto type = *index++;
  1277. if (isMarker (type, moveMarker))
  1278. {
  1279. elementType = startNewSubPath;
  1280. x1 = *index++;
  1281. y1 = *index++;
  1282. }
  1283. else if (isMarker (type, lineMarker))
  1284. {
  1285. elementType = lineTo;
  1286. x1 = *index++;
  1287. y1 = *index++;
  1288. }
  1289. else if (isMarker (type, quadMarker))
  1290. {
  1291. elementType = quadraticTo;
  1292. x1 = *index++;
  1293. y1 = *index++;
  1294. x2 = *index++;
  1295. y2 = *index++;
  1296. }
  1297. else if (isMarker (type, cubicMarker))
  1298. {
  1299. elementType = cubicTo;
  1300. x1 = *index++;
  1301. y1 = *index++;
  1302. x2 = *index++;
  1303. y2 = *index++;
  1304. x3 = *index++;
  1305. y3 = *index++;
  1306. }
  1307. else if (isMarker (type, closeSubPathMarker))
  1308. {
  1309. elementType = closePath;
  1310. }
  1311. return true;
  1312. }
  1313. return false;
  1314. }
  1315. #undef JUCE_CHECK_COORDS_ARE_VALID
  1316. } // namespace juce