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.

1634 lines
48KB

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