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.

1630 lines
49KB

  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 (float x1, float y1,
  416. float x2, float y2,
  417. float x3, float y3)
  418. {
  419. addTriangle (Point<float> (x1, y1),
  420. Point<float> (x2, y2),
  421. Point<float> (x3, y3));
  422. }
  423. void Path::addTriangle (Point<float> p1, Point<float> p2, Point<float> p3)
  424. {
  425. startNewSubPath (p1);
  426. lineTo (p2);
  427. lineTo (p3);
  428. closeSubPath();
  429. }
  430. void Path::addQuadrilateral (const float x1, const float y1,
  431. const float x2, const float y2,
  432. const float x3, const float y3,
  433. const float x4, const float y4)
  434. {
  435. startNewSubPath (x1, y1);
  436. lineTo (x2, y2);
  437. lineTo (x3, y3);
  438. lineTo (x4, y4);
  439. closeSubPath();
  440. }
  441. void Path::addEllipse (float x, float y, float w, float h)
  442. {
  443. addEllipse (Rectangle<float> (x, y, w, h));
  444. }
  445. void Path::addEllipse (Rectangle<float> area)
  446. {
  447. const float hw = area.getWidth() * 0.5f;
  448. const float hw55 = hw * 0.55f;
  449. const float hh = area.getHeight() * 0.5f;
  450. const float hh55 = hh * 0.55f;
  451. const float cx = area.getX() + hw;
  452. const float cy = area.getY() + hh;
  453. startNewSubPath (cx, cy - hh);
  454. cubicTo (cx + hw55, cy - hh, cx + hw, cy - hh55, cx + hw, cy);
  455. cubicTo (cx + hw, cy + hh55, cx + hw55, cy + hh, cx, cy + hh);
  456. cubicTo (cx - hw55, cy + hh, cx - hw, cy + hh55, cx - hw, cy);
  457. cubicTo (cx - hw, cy - hh55, cx - hw55, cy - hh, cx, cy - hh);
  458. closeSubPath();
  459. }
  460. void Path::addArc (const float x, const float y,
  461. const float w, const float h,
  462. const float fromRadians,
  463. const float toRadians,
  464. const bool startAsNewSubPath)
  465. {
  466. const float radiusX = w / 2.0f;
  467. const float radiusY = h / 2.0f;
  468. addCentredArc (x + radiusX,
  469. y + radiusY,
  470. radiusX, radiusY,
  471. 0.0f,
  472. fromRadians, toRadians,
  473. startAsNewSubPath);
  474. }
  475. void Path::addCentredArc (const float centreX, const float centreY,
  476. const float radiusX, const float radiusY,
  477. const float rotationOfEllipse,
  478. const float fromRadians,
  479. float toRadians,
  480. const bool startAsNewSubPath)
  481. {
  482. if (radiusX > 0.0f && radiusY > 0.0f)
  483. {
  484. const Point<float> centre (centreX, centreY);
  485. const AffineTransform rotation (AffineTransform::rotation (rotationOfEllipse, centreX, centreY));
  486. float angle = fromRadians;
  487. if (startAsNewSubPath)
  488. startNewSubPath (centre.getPointOnCircumference (radiusX, radiusY, angle).transformedBy (rotation));
  489. if (fromRadians < toRadians)
  490. {
  491. if (startAsNewSubPath)
  492. angle += PathHelpers::ellipseAngularIncrement;
  493. while (angle < toRadians)
  494. {
  495. lineTo (centre.getPointOnCircumference (radiusX, radiusY, angle).transformedBy (rotation));
  496. angle += PathHelpers::ellipseAngularIncrement;
  497. }
  498. }
  499. else
  500. {
  501. if (startAsNewSubPath)
  502. angle -= PathHelpers::ellipseAngularIncrement;
  503. while (angle > toRadians)
  504. {
  505. lineTo (centre.getPointOnCircumference (radiusX, radiusY, angle).transformedBy (rotation));
  506. angle -= PathHelpers::ellipseAngularIncrement;
  507. }
  508. }
  509. lineTo (centre.getPointOnCircumference (radiusX, radiusY, toRadians).transformedBy (rotation));
  510. }
  511. }
  512. void Path::addPieSegment (const float x, const float y,
  513. const float width, const float height,
  514. const float fromRadians,
  515. const float toRadians,
  516. const float innerCircleProportionalSize)
  517. {
  518. float radiusX = width * 0.5f;
  519. float radiusY = height * 0.5f;
  520. const Point<float> centre (x + radiusX, y + radiusY);
  521. startNewSubPath (centre.getPointOnCircumference (radiusX, radiusY, fromRadians));
  522. addArc (x, y, width, height, fromRadians, toRadians);
  523. if (std::abs (fromRadians - toRadians) > float_Pi * 1.999f)
  524. {
  525. closeSubPath();
  526. if (innerCircleProportionalSize > 0)
  527. {
  528. radiusX *= innerCircleProportionalSize;
  529. radiusY *= innerCircleProportionalSize;
  530. startNewSubPath (centre.getPointOnCircumference (radiusX, radiusY, toRadians));
  531. addArc (centre.x - radiusX, centre.y - radiusY, radiusX * 2.0f, radiusY * 2.0f, toRadians, fromRadians);
  532. }
  533. }
  534. else
  535. {
  536. if (innerCircleProportionalSize > 0)
  537. {
  538. radiusX *= innerCircleProportionalSize;
  539. radiusY *= innerCircleProportionalSize;
  540. addArc (centre.x - radiusX, centre.y - radiusY, radiusX * 2.0f, radiusY * 2.0f, toRadians, fromRadians);
  541. }
  542. else
  543. {
  544. lineTo (centre);
  545. }
  546. }
  547. closeSubPath();
  548. }
  549. void Path::addPieSegment (Rectangle<float> segmentBounds,
  550. const float fromRadians,
  551. const float toRadians,
  552. const float innerCircleProportionalSize)
  553. {
  554. addPieSegment (segmentBounds.getX(),
  555. segmentBounds.getY(),
  556. segmentBounds.getWidth(),
  557. segmentBounds.getHeight(),
  558. fromRadians,
  559. toRadians,
  560. innerCircleProportionalSize);
  561. }
  562. //==============================================================================
  563. void Path::addLineSegment (const Line<float>& line, float lineThickness)
  564. {
  565. const Line<float> reversed (line.reversed());
  566. lineThickness *= 0.5f;
  567. startNewSubPath (line.getPointAlongLine (0, lineThickness));
  568. lineTo (line.getPointAlongLine (0, -lineThickness));
  569. lineTo (reversed.getPointAlongLine (0, lineThickness));
  570. lineTo (reversed.getPointAlongLine (0, -lineThickness));
  571. closeSubPath();
  572. }
  573. void Path::addArrow (const Line<float>& line, float lineThickness,
  574. float arrowheadWidth, float arrowheadLength)
  575. {
  576. const Line<float> reversed (line.reversed());
  577. lineThickness *= 0.5f;
  578. arrowheadWidth *= 0.5f;
  579. arrowheadLength = jmin (arrowheadLength, 0.8f * line.getLength());
  580. startNewSubPath (line.getPointAlongLine (0, lineThickness));
  581. lineTo (line.getPointAlongLine (0, -lineThickness));
  582. lineTo (reversed.getPointAlongLine (arrowheadLength, lineThickness));
  583. lineTo (reversed.getPointAlongLine (arrowheadLength, arrowheadWidth));
  584. lineTo (line.getEnd());
  585. lineTo (reversed.getPointAlongLine (arrowheadLength, -arrowheadWidth));
  586. lineTo (reversed.getPointAlongLine (arrowheadLength, -lineThickness));
  587. closeSubPath();
  588. }
  589. void Path::addPolygon (const Point<float> centre, const int numberOfSides,
  590. const float radius, const float startAngle)
  591. {
  592. jassert (numberOfSides > 1); // this would be silly.
  593. if (numberOfSides > 1)
  594. {
  595. const float angleBetweenPoints = float_Pi * 2.0f / numberOfSides;
  596. for (int i = 0; i < numberOfSides; ++i)
  597. {
  598. const float angle = startAngle + i * angleBetweenPoints;
  599. const Point<float> p (centre.getPointOnCircumference (radius, angle));
  600. if (i == 0)
  601. startNewSubPath (p);
  602. else
  603. lineTo (p);
  604. }
  605. closeSubPath();
  606. }
  607. }
  608. void Path::addStar (const Point<float> centre, const int numberOfPoints,
  609. const float innerRadius, const float outerRadius, const float startAngle)
  610. {
  611. jassert (numberOfPoints > 1); // this would be silly.
  612. if (numberOfPoints > 1)
  613. {
  614. const float angleBetweenPoints = float_Pi * 2.0f / numberOfPoints;
  615. for (int i = 0; i < numberOfPoints; ++i)
  616. {
  617. const float angle = startAngle + i * angleBetweenPoints;
  618. const Point<float> p (centre.getPointOnCircumference (outerRadius, angle));
  619. if (i == 0)
  620. startNewSubPath (p);
  621. else
  622. lineTo (p);
  623. lineTo (centre.getPointOnCircumference (innerRadius, angle + angleBetweenPoints * 0.5f));
  624. }
  625. closeSubPath();
  626. }
  627. }
  628. void Path::addBubble (const Rectangle<float>& bodyArea,
  629. const Rectangle<float>& maximumArea,
  630. const Point<float> arrowTip,
  631. const float cornerSize,
  632. const float arrowBaseWidth)
  633. {
  634. const float halfW = bodyArea.getWidth() / 2.0f;
  635. const float halfH = bodyArea.getHeight() / 2.0f;
  636. const float cornerSizeW = jmin (cornerSize, halfW);
  637. const float cornerSizeH = jmin (cornerSize, halfH);
  638. const float cornerSizeW2 = 2.0f * cornerSizeW;
  639. const float cornerSizeH2 = 2.0f * cornerSizeH;
  640. startNewSubPath (bodyArea.getX() + cornerSizeW, bodyArea.getY());
  641. const Rectangle<float> targetLimit (bodyArea.reduced (jmin (halfW - 1.0f, cornerSizeW + arrowBaseWidth),
  642. jmin (halfH - 1.0f, cornerSizeH + arrowBaseWidth)));
  643. if (Rectangle<float> (targetLimit.getX(), maximumArea.getY(),
  644. targetLimit.getWidth(), bodyArea.getY() - maximumArea.getY()).contains (arrowTip))
  645. {
  646. lineTo (arrowTip.x - arrowBaseWidth, bodyArea.getY());
  647. lineTo (arrowTip.x, arrowTip.y);
  648. lineTo (arrowTip.x + arrowBaseWidth, bodyArea.getY());
  649. }
  650. lineTo (bodyArea.getRight() - cornerSizeW, bodyArea.getY());
  651. addArc (bodyArea.getRight() - cornerSizeW2, bodyArea.getY(), cornerSizeW2, cornerSizeH2, 0, float_Pi * 0.5f);
  652. if (Rectangle<float> (bodyArea.getRight(), targetLimit.getY(),
  653. maximumArea.getRight() - bodyArea.getRight(), targetLimit.getHeight()).contains (arrowTip))
  654. {
  655. lineTo (bodyArea.getRight(), arrowTip.y - arrowBaseWidth);
  656. lineTo (arrowTip.x, arrowTip.y);
  657. lineTo (bodyArea.getRight(), arrowTip.y + arrowBaseWidth);
  658. }
  659. lineTo (bodyArea.getRight(), bodyArea.getBottom() - cornerSizeH);
  660. addArc (bodyArea.getRight() - cornerSizeW2, bodyArea.getBottom() - cornerSizeH2, cornerSizeW2, cornerSizeH2, float_Pi * 0.5f, float_Pi);
  661. if (Rectangle<float> (targetLimit.getX(), bodyArea.getBottom(),
  662. targetLimit.getWidth(), maximumArea.getBottom() - bodyArea.getBottom()).contains (arrowTip))
  663. {
  664. lineTo (arrowTip.x + arrowBaseWidth, bodyArea.getBottom());
  665. lineTo (arrowTip.x, arrowTip.y);
  666. lineTo (arrowTip.x - arrowBaseWidth, bodyArea.getBottom());
  667. }
  668. lineTo (bodyArea.getX() + cornerSizeW, bodyArea.getBottom());
  669. addArc (bodyArea.getX(), bodyArea.getBottom() - cornerSizeH2, cornerSizeW2, cornerSizeH2, float_Pi, float_Pi * 1.5f);
  670. if (Rectangle<float> (maximumArea.getX(), targetLimit.getY(),
  671. bodyArea.getX() - maximumArea.getX(), targetLimit.getHeight()).contains (arrowTip))
  672. {
  673. lineTo (bodyArea.getX(), arrowTip.y + arrowBaseWidth);
  674. lineTo (arrowTip.x, arrowTip.y);
  675. lineTo (bodyArea.getX(), arrowTip.y - arrowBaseWidth);
  676. }
  677. lineTo (bodyArea.getX(), bodyArea.getY() + cornerSizeH);
  678. addArc (bodyArea.getX(), bodyArea.getY(), cornerSizeW2, cornerSizeH2, float_Pi * 1.5f, float_Pi * 2.0f - 0.05f);
  679. closeSubPath();
  680. }
  681. void Path::addPath (const Path& other)
  682. {
  683. size_t i = 0;
  684. const float* const d = other.data.elements;
  685. while (i < other.numElements)
  686. {
  687. const float type = d[i++];
  688. if (type == moveMarker)
  689. {
  690. startNewSubPath (d[i], d[i + 1]);
  691. i += 2;
  692. }
  693. else if (type == lineMarker)
  694. {
  695. lineTo (d[i], d[i + 1]);
  696. i += 2;
  697. }
  698. else if (type == quadMarker)
  699. {
  700. quadraticTo (d[i], d[i + 1], d[i + 2], d[i + 3]);
  701. i += 4;
  702. }
  703. else if (type == cubicMarker)
  704. {
  705. cubicTo (d[i], d[i + 1], d[i + 2], d[i + 3], d[i + 4], d[i + 5]);
  706. i += 6;
  707. }
  708. else if (type == closeSubPathMarker)
  709. {
  710. closeSubPath();
  711. }
  712. else
  713. {
  714. // something's gone wrong with the element list!
  715. jassertfalse;
  716. }
  717. }
  718. }
  719. void Path::addPath (const Path& other,
  720. const AffineTransform& transformToApply)
  721. {
  722. size_t i = 0;
  723. const float* const d = other.data.elements;
  724. while (i < other.numElements)
  725. {
  726. const float type = d [i++];
  727. if (type == closeSubPathMarker)
  728. {
  729. closeSubPath();
  730. }
  731. else
  732. {
  733. float x = d[i++];
  734. float y = d[i++];
  735. transformToApply.transformPoint (x, y);
  736. if (type == moveMarker)
  737. {
  738. startNewSubPath (x, y);
  739. }
  740. else if (type == lineMarker)
  741. {
  742. lineTo (x, y);
  743. }
  744. else if (type == quadMarker)
  745. {
  746. float x2 = d [i++];
  747. float y2 = d [i++];
  748. transformToApply.transformPoint (x2, y2);
  749. quadraticTo (x, y, x2, y2);
  750. }
  751. else if (type == cubicMarker)
  752. {
  753. float x2 = d [i++];
  754. float y2 = d [i++];
  755. float x3 = d [i++];
  756. float y3 = d [i++];
  757. transformToApply.transformPoints (x2, y2, x3, y3);
  758. cubicTo (x, y, x2, y2, x3, y3);
  759. }
  760. else
  761. {
  762. // something's gone wrong with the element list!
  763. jassertfalse;
  764. }
  765. }
  766. }
  767. }
  768. //==============================================================================
  769. void Path::applyTransform (const AffineTransform& transform) noexcept
  770. {
  771. bounds.reset();
  772. bool firstPoint = true;
  773. float* d = data.elements;
  774. float* const end = d + numElements;
  775. while (d < end)
  776. {
  777. const float type = *d++;
  778. if (type == moveMarker)
  779. {
  780. transform.transformPoint (d[0], d[1]);
  781. if (firstPoint)
  782. {
  783. firstPoint = false;
  784. bounds.reset (d[0], d[1]);
  785. }
  786. else
  787. {
  788. bounds.extend (d[0], d[1]);
  789. }
  790. d += 2;
  791. }
  792. else if (type == lineMarker)
  793. {
  794. transform.transformPoint (d[0], d[1]);
  795. bounds.extend (d[0], d[1]);
  796. d += 2;
  797. }
  798. else if (type == quadMarker)
  799. {
  800. transform.transformPoints (d[0], d[1], d[2], d[3]);
  801. bounds.extend (d[0], d[1], d[2], d[3]);
  802. d += 4;
  803. }
  804. else if (type == cubicMarker)
  805. {
  806. transform.transformPoints (d[0], d[1], d[2], d[3], d[4], d[5]);
  807. bounds.extend (d[0], d[1], d[2], d[3]);
  808. bounds.extend (d[4], d[5]);
  809. d += 6;
  810. }
  811. }
  812. }
  813. //==============================================================================
  814. AffineTransform Path::getTransformToScaleToFit (const Rectangle<float>& area,
  815. bool preserveProportions, Justification justification) const
  816. {
  817. return getTransformToScaleToFit (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  818. preserveProportions, justification);
  819. }
  820. AffineTransform Path::getTransformToScaleToFit (const float x, const float y,
  821. const float w, const float h,
  822. const bool preserveProportions,
  823. Justification justification) const
  824. {
  825. Rectangle<float> boundsRect (getBounds());
  826. if (preserveProportions)
  827. {
  828. if (w <= 0 || h <= 0 || boundsRect.isEmpty())
  829. return AffineTransform::identity;
  830. float newW, newH;
  831. const float srcRatio = boundsRect.getHeight() / boundsRect.getWidth();
  832. if (srcRatio > h / w)
  833. {
  834. newW = h / srcRatio;
  835. newH = h;
  836. }
  837. else
  838. {
  839. newW = w;
  840. newH = w * srcRatio;
  841. }
  842. float newXCentre = x;
  843. float newYCentre = y;
  844. if (justification.testFlags (Justification::left)) newXCentre += newW * 0.5f;
  845. else if (justification.testFlags (Justification::right)) newXCentre += w - newW * 0.5f;
  846. else newXCentre += w * 0.5f;
  847. if (justification.testFlags (Justification::top)) newYCentre += newH * 0.5f;
  848. else if (justification.testFlags (Justification::bottom)) newYCentre += h - newH * 0.5f;
  849. else newYCentre += h * 0.5f;
  850. return AffineTransform::translation (boundsRect.getWidth() * -0.5f - boundsRect.getX(),
  851. boundsRect.getHeight() * -0.5f - boundsRect.getY())
  852. .scaled (newW / boundsRect.getWidth(),
  853. newH / boundsRect.getHeight())
  854. .translated (newXCentre, newYCentre);
  855. }
  856. else
  857. {
  858. return AffineTransform::translation (-boundsRect.getX(), -boundsRect.getY())
  859. .scaled (w / boundsRect.getWidth(),
  860. h / boundsRect.getHeight())
  861. .translated (x, y);
  862. }
  863. }
  864. //==============================================================================
  865. bool Path::contains (const float x, const float y, const float tolerance) const
  866. {
  867. if (x <= bounds.pathXMin || x >= bounds.pathXMax
  868. || y <= bounds.pathYMin || y >= bounds.pathYMax)
  869. return false;
  870. PathFlatteningIterator i (*this, AffineTransform::identity, tolerance);
  871. int positiveCrossings = 0;
  872. int negativeCrossings = 0;
  873. while (i.next())
  874. {
  875. if ((i.y1 <= y && i.y2 > y) || (i.y2 <= y && i.y1 > y))
  876. {
  877. const float intersectX = i.x1 + (i.x2 - i.x1) * (y - i.y1) / (i.y2 - i.y1);
  878. if (intersectX <= x)
  879. {
  880. if (i.y1 < i.y2)
  881. ++positiveCrossings;
  882. else
  883. ++negativeCrossings;
  884. }
  885. }
  886. }
  887. return useNonZeroWinding ? (negativeCrossings != positiveCrossings)
  888. : ((negativeCrossings + positiveCrossings) & 1) != 0;
  889. }
  890. bool Path::contains (const Point<float> point, const float tolerance) const
  891. {
  892. return contains (point.x, point.y, tolerance);
  893. }
  894. bool Path::intersectsLine (const Line<float>& line, const float tolerance)
  895. {
  896. PathFlatteningIterator i (*this, AffineTransform::identity, tolerance);
  897. Point<float> intersection;
  898. while (i.next())
  899. if (line.intersects (Line<float> (i.x1, i.y1, i.x2, i.y2), intersection))
  900. return true;
  901. return false;
  902. }
  903. Line<float> Path::getClippedLine (const Line<float>& line, const bool keepSectionOutsidePath) const
  904. {
  905. Line<float> result (line);
  906. const bool startInside = contains (line.getStart());
  907. const bool endInside = contains (line.getEnd());
  908. if (startInside == endInside)
  909. {
  910. if (keepSectionOutsidePath == startInside)
  911. result = Line<float>();
  912. }
  913. else
  914. {
  915. PathFlatteningIterator i (*this, AffineTransform::identity);
  916. Point<float> intersection;
  917. while (i.next())
  918. {
  919. if (line.intersects (Line<float> (i.x1, i.y1, i.x2, i.y2), intersection))
  920. {
  921. if ((startInside && keepSectionOutsidePath) || (endInside && ! keepSectionOutsidePath))
  922. result.setStart (intersection);
  923. else
  924. result.setEnd (intersection);
  925. }
  926. }
  927. }
  928. return result;
  929. }
  930. float Path::getLength (const AffineTransform& transform) const
  931. {
  932. float length = 0;
  933. PathFlatteningIterator i (*this, transform);
  934. while (i.next())
  935. length += Line<float> (i.x1, i.y1, i.x2, i.y2).getLength();
  936. return length;
  937. }
  938. Point<float> Path::getPointAlongPath (float distanceFromStart, const AffineTransform& transform) const
  939. {
  940. PathFlatteningIterator i (*this, transform);
  941. while (i.next())
  942. {
  943. const Line<float> line (i.x1, i.y1, i.x2, i.y2);
  944. const float lineLength = line.getLength();
  945. if (distanceFromStart <= lineLength)
  946. return line.getPointAlongLine (distanceFromStart);
  947. distanceFromStart -= lineLength;
  948. }
  949. return Point<float> (i.x2, i.y2);
  950. }
  951. float Path::getNearestPoint (const Point<float> targetPoint, Point<float>& pointOnPath,
  952. const AffineTransform& transform) const
  953. {
  954. PathFlatteningIterator i (*this, transform);
  955. float bestPosition = 0, bestDistance = std::numeric_limits<float>::max();
  956. float length = 0;
  957. Point<float> pointOnLine;
  958. while (i.next())
  959. {
  960. const Line<float> line (i.x1, i.y1, i.x2, i.y2);
  961. const float distance = line.getDistanceFromPoint (targetPoint, pointOnLine);
  962. if (distance < bestDistance)
  963. {
  964. bestDistance = distance;
  965. bestPosition = length + pointOnLine.getDistanceFrom (line.getStart());
  966. pointOnPath = pointOnLine;
  967. }
  968. length += line.getLength();
  969. }
  970. return bestPosition;
  971. }
  972. //==============================================================================
  973. Path Path::createPathWithRoundedCorners (const float cornerRadius) const
  974. {
  975. if (cornerRadius <= 0.01f)
  976. return *this;
  977. size_t indexOfPathStart = 0, indexOfPathStartThis = 0;
  978. size_t n = 0;
  979. bool lastWasLine = false, firstWasLine = false;
  980. Path p;
  981. while (n < numElements)
  982. {
  983. const float type = data.elements [n++];
  984. if (type == moveMarker)
  985. {
  986. indexOfPathStart = p.numElements;
  987. indexOfPathStartThis = n - 1;
  988. const float x = data.elements [n++];
  989. const float y = data.elements [n++];
  990. p.startNewSubPath (x, y);
  991. lastWasLine = false;
  992. firstWasLine = (data.elements [n] == lineMarker);
  993. }
  994. else if (type == lineMarker || type == closeSubPathMarker)
  995. {
  996. float startX = 0, startY = 0, joinX = 0, joinY = 0, endX, endY;
  997. if (type == lineMarker)
  998. {
  999. endX = data.elements [n++];
  1000. endY = data.elements [n++];
  1001. if (n > 8)
  1002. {
  1003. startX = data.elements [n - 8];
  1004. startY = data.elements [n - 7];
  1005. joinX = data.elements [n - 5];
  1006. joinY = data.elements [n - 4];
  1007. }
  1008. }
  1009. else
  1010. {
  1011. endX = data.elements [indexOfPathStartThis + 1];
  1012. endY = data.elements [indexOfPathStartThis + 2];
  1013. if (n > 6)
  1014. {
  1015. startX = data.elements [n - 6];
  1016. startY = data.elements [n - 5];
  1017. joinX = data.elements [n - 3];
  1018. joinY = data.elements [n - 2];
  1019. }
  1020. }
  1021. if (lastWasLine)
  1022. {
  1023. const double len1 = PathHelpers::lengthOf (startX, startY, joinX, joinY);
  1024. if (len1 > 0)
  1025. {
  1026. const double propNeeded = jmin (0.5, cornerRadius / len1);
  1027. p.data.elements [p.numElements - 2] = (float) (joinX - (joinX - startX) * propNeeded);
  1028. p.data.elements [p.numElements - 1] = (float) (joinY - (joinY - startY) * propNeeded);
  1029. }
  1030. const double len2 = PathHelpers::lengthOf (endX, endY, joinX, joinY);
  1031. if (len2 > 0)
  1032. {
  1033. const double propNeeded = jmin (0.5, cornerRadius / len2);
  1034. p.quadraticTo (joinX, joinY,
  1035. (float) (joinX + (endX - joinX) * propNeeded),
  1036. (float) (joinY + (endY - joinY) * propNeeded));
  1037. }
  1038. p.lineTo (endX, endY);
  1039. }
  1040. else if (type == lineMarker)
  1041. {
  1042. p.lineTo (endX, endY);
  1043. lastWasLine = true;
  1044. }
  1045. if (type == closeSubPathMarker)
  1046. {
  1047. if (firstWasLine)
  1048. {
  1049. startX = data.elements [n - 3];
  1050. startY = data.elements [n - 2];
  1051. joinX = endX;
  1052. joinY = endY;
  1053. endX = data.elements [indexOfPathStartThis + 4];
  1054. endY = data.elements [indexOfPathStartThis + 5];
  1055. const double len1 = PathHelpers::lengthOf (startX, startY, joinX, joinY);
  1056. if (len1 > 0)
  1057. {
  1058. const double propNeeded = jmin (0.5, cornerRadius / len1);
  1059. p.data.elements [p.numElements - 2] = (float) (joinX - (joinX - startX) * propNeeded);
  1060. p.data.elements [p.numElements - 1] = (float) (joinY - (joinY - startY) * propNeeded);
  1061. }
  1062. const double len2 = PathHelpers::lengthOf (endX, endY, joinX, joinY);
  1063. if (len2 > 0)
  1064. {
  1065. const double propNeeded = jmin (0.5, cornerRadius / len2);
  1066. endX = (float) (joinX + (endX - joinX) * propNeeded);
  1067. endY = (float) (joinY + (endY - joinY) * propNeeded);
  1068. p.quadraticTo (joinX, joinY, endX, endY);
  1069. p.data.elements [indexOfPathStart + 1] = endX;
  1070. p.data.elements [indexOfPathStart + 2] = endY;
  1071. }
  1072. }
  1073. p.closeSubPath();
  1074. }
  1075. }
  1076. else if (type == quadMarker)
  1077. {
  1078. lastWasLine = false;
  1079. const float x1 = data.elements [n++];
  1080. const float y1 = data.elements [n++];
  1081. const float x2 = data.elements [n++];
  1082. const float y2 = data.elements [n++];
  1083. p.quadraticTo (x1, y1, x2, y2);
  1084. }
  1085. else if (type == cubicMarker)
  1086. {
  1087. lastWasLine = false;
  1088. const float x1 = data.elements [n++];
  1089. const float y1 = data.elements [n++];
  1090. const float x2 = data.elements [n++];
  1091. const float y2 = data.elements [n++];
  1092. const float x3 = data.elements [n++];
  1093. const float y3 = data.elements [n++];
  1094. p.cubicTo (x1, y1, x2, y2, x3, y3);
  1095. }
  1096. }
  1097. return p;
  1098. }
  1099. //==============================================================================
  1100. void Path::loadPathFromStream (InputStream& source)
  1101. {
  1102. while (! source.isExhausted())
  1103. {
  1104. switch (source.readByte())
  1105. {
  1106. case 'm':
  1107. {
  1108. const float x = source.readFloat();
  1109. const float y = source.readFloat();
  1110. startNewSubPath (x, y);
  1111. break;
  1112. }
  1113. case 'l':
  1114. {
  1115. const float x = source.readFloat();
  1116. const float y = source.readFloat();
  1117. lineTo (x, y);
  1118. break;
  1119. }
  1120. case 'q':
  1121. {
  1122. const float x1 = source.readFloat();
  1123. const float y1 = source.readFloat();
  1124. const float x2 = source.readFloat();
  1125. const float y2 = source.readFloat();
  1126. quadraticTo (x1, y1, x2, y2);
  1127. break;
  1128. }
  1129. case 'b':
  1130. {
  1131. const float x1 = source.readFloat();
  1132. const float y1 = source.readFloat();
  1133. const float x2 = source.readFloat();
  1134. const float y2 = source.readFloat();
  1135. const float x3 = source.readFloat();
  1136. const float y3 = source.readFloat();
  1137. cubicTo (x1, y1, x2, y2, x3, y3);
  1138. break;
  1139. }
  1140. case 'c':
  1141. closeSubPath();
  1142. break;
  1143. case 'n':
  1144. useNonZeroWinding = true;
  1145. break;
  1146. case 'z':
  1147. useNonZeroWinding = false;
  1148. break;
  1149. case 'e':
  1150. return; // end of path marker
  1151. default:
  1152. jassertfalse; // illegal char in the stream
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. void Path::loadPathFromData (const void* const pathData, const size_t numberOfBytes)
  1158. {
  1159. MemoryInputStream in (pathData, numberOfBytes, false);
  1160. loadPathFromStream (in);
  1161. }
  1162. void Path::writePathToStream (OutputStream& dest) const
  1163. {
  1164. dest.writeByte (useNonZeroWinding ? 'n' : 'z');
  1165. size_t i = 0;
  1166. while (i < numElements)
  1167. {
  1168. const float type = data.elements [i++];
  1169. if (type == moveMarker)
  1170. {
  1171. dest.writeByte ('m');
  1172. dest.writeFloat (data.elements [i++]);
  1173. dest.writeFloat (data.elements [i++]);
  1174. }
  1175. else if (type == lineMarker)
  1176. {
  1177. dest.writeByte ('l');
  1178. dest.writeFloat (data.elements [i++]);
  1179. dest.writeFloat (data.elements [i++]);
  1180. }
  1181. else if (type == quadMarker)
  1182. {
  1183. dest.writeByte ('q');
  1184. dest.writeFloat (data.elements [i++]);
  1185. dest.writeFloat (data.elements [i++]);
  1186. dest.writeFloat (data.elements [i++]);
  1187. dest.writeFloat (data.elements [i++]);
  1188. }
  1189. else if (type == cubicMarker)
  1190. {
  1191. dest.writeByte ('b');
  1192. dest.writeFloat (data.elements [i++]);
  1193. dest.writeFloat (data.elements [i++]);
  1194. dest.writeFloat (data.elements [i++]);
  1195. dest.writeFloat (data.elements [i++]);
  1196. dest.writeFloat (data.elements [i++]);
  1197. dest.writeFloat (data.elements [i++]);
  1198. }
  1199. else if (type == closeSubPathMarker)
  1200. {
  1201. dest.writeByte ('c');
  1202. }
  1203. }
  1204. dest.writeByte ('e'); // marks the end-of-path
  1205. }
  1206. String Path::toString() const
  1207. {
  1208. MemoryOutputStream s (2048);
  1209. if (! useNonZeroWinding)
  1210. s << 'a';
  1211. size_t i = 0;
  1212. float lastMarker = 0.0f;
  1213. while (i < numElements)
  1214. {
  1215. const float marker = data.elements [i++];
  1216. char markerChar = 0;
  1217. int numCoords = 0;
  1218. if (marker == moveMarker)
  1219. {
  1220. markerChar = 'm';
  1221. numCoords = 2;
  1222. }
  1223. else if (marker == lineMarker)
  1224. {
  1225. markerChar = 'l';
  1226. numCoords = 2;
  1227. }
  1228. else if (marker == quadMarker)
  1229. {
  1230. markerChar = 'q';
  1231. numCoords = 4;
  1232. }
  1233. else if (marker == cubicMarker)
  1234. {
  1235. markerChar = 'c';
  1236. numCoords = 6;
  1237. }
  1238. else
  1239. {
  1240. jassert (marker == closeSubPathMarker);
  1241. markerChar = 'z';
  1242. }
  1243. if (marker != lastMarker)
  1244. {
  1245. if (s.getDataSize() != 0)
  1246. s << ' ';
  1247. s << markerChar;
  1248. lastMarker = marker;
  1249. }
  1250. while (--numCoords >= 0 && i < numElements)
  1251. {
  1252. String coord (data.elements [i++], 3);
  1253. while (coord.endsWithChar ('0') && coord != "0")
  1254. coord = coord.dropLastCharacters (1);
  1255. if (coord.endsWithChar ('.'))
  1256. coord = coord.dropLastCharacters (1);
  1257. if (s.getDataSize() != 0)
  1258. s << ' ';
  1259. s << coord;
  1260. }
  1261. }
  1262. return s.toUTF8();
  1263. }
  1264. void Path::restoreFromString (StringRef stringVersion)
  1265. {
  1266. clear();
  1267. setUsingNonZeroWinding (true);
  1268. String::CharPointerType t (stringVersion.text);
  1269. juce_wchar marker = 'm';
  1270. int numValues = 2;
  1271. float values [6];
  1272. for (;;)
  1273. {
  1274. const String token (PathHelpers::nextToken (t));
  1275. const juce_wchar firstChar = token[0];
  1276. int startNum = 0;
  1277. if (firstChar == 0)
  1278. break;
  1279. if (firstChar == 'm' || firstChar == 'l')
  1280. {
  1281. marker = firstChar;
  1282. numValues = 2;
  1283. }
  1284. else if (firstChar == 'q')
  1285. {
  1286. marker = firstChar;
  1287. numValues = 4;
  1288. }
  1289. else if (firstChar == 'c')
  1290. {
  1291. marker = firstChar;
  1292. numValues = 6;
  1293. }
  1294. else if (firstChar == 'z')
  1295. {
  1296. marker = firstChar;
  1297. numValues = 0;
  1298. }
  1299. else if (firstChar == 'a')
  1300. {
  1301. setUsingNonZeroWinding (false);
  1302. continue;
  1303. }
  1304. else
  1305. {
  1306. ++startNum;
  1307. values [0] = token.getFloatValue();
  1308. }
  1309. for (int i = startNum; i < numValues; ++i)
  1310. values [i] = PathHelpers::nextToken (t).getFloatValue();
  1311. switch (marker)
  1312. {
  1313. case 'm': startNewSubPath (values[0], values[1]); break;
  1314. case 'l': lineTo (values[0], values[1]); break;
  1315. case 'q': quadraticTo (values[0], values[1], values[2], values[3]); break;
  1316. case 'c': cubicTo (values[0], values[1], values[2], values[3], values[4], values[5]); break;
  1317. case 'z': closeSubPath(); break;
  1318. default: jassertfalse; break; // illegal string format?
  1319. }
  1320. }
  1321. }
  1322. //==============================================================================
  1323. Path::Iterator::Iterator (const Path& p) noexcept
  1324. : x1 (0), y1 (0), x2 (0), y2 (0), x3 (0), y3 (0),
  1325. path (p), index (0)
  1326. {
  1327. }
  1328. Path::Iterator::~Iterator() noexcept
  1329. {
  1330. }
  1331. bool Path::Iterator::next() noexcept
  1332. {
  1333. const float* const elements = path.data.elements;
  1334. if (index < path.numElements)
  1335. {
  1336. const float type = elements [index++];
  1337. if (type == moveMarker)
  1338. {
  1339. elementType = startNewSubPath;
  1340. x1 = elements [index++];
  1341. y1 = elements [index++];
  1342. }
  1343. else if (type == lineMarker)
  1344. {
  1345. elementType = lineTo;
  1346. x1 = elements [index++];
  1347. y1 = elements [index++];
  1348. }
  1349. else if (type == quadMarker)
  1350. {
  1351. elementType = quadraticTo;
  1352. x1 = elements [index++];
  1353. y1 = elements [index++];
  1354. x2 = elements [index++];
  1355. y2 = elements [index++];
  1356. }
  1357. else if (type == cubicMarker)
  1358. {
  1359. elementType = cubicTo;
  1360. x1 = elements [index++];
  1361. y1 = elements [index++];
  1362. x2 = elements [index++];
  1363. y2 = elements [index++];
  1364. x3 = elements [index++];
  1365. y3 = elements [index++];
  1366. }
  1367. else if (type == closeSubPathMarker)
  1368. {
  1369. elementType = closePath;
  1370. }
  1371. return true;
  1372. }
  1373. return false;
  1374. }
  1375. #undef JUCE_CHECK_COORDS_ARE_VALID