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

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