Audio plugin host https://kx.studio/carla
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.

1609 lines
48KB

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