Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_Path.cpp 45KB

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