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.

1595 lines
48KB

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