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.

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