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

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