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.

juce_Path.cpp 45KB

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