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 48KB

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