juce_Path.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #ifndef JUCE_PATH_H_INCLUDED
  18. #define JUCE_PATH_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A path is a sequence of lines and curves that may either form a closed shape
  22. or be open-ended.
  23. To use a path, you can create an empty one, then add lines and curves to it
  24. to create shapes, then it can be rendered by a Graphics context or used
  25. for geometric operations.
  26. e.g. @code
  27. Path myPath;
  28. myPath.startNewSubPath (10.0f, 10.0f); // move the current position to (10, 10)
  29. myPath.lineTo (100.0f, 200.0f); // draw a line from here to (100, 200)
  30. myPath.quadraticTo (0.0f, 150.0f, 5.0f, 50.0f); // draw a curve that ends at (5, 50)
  31. myPath.closeSubPath(); // close the subpath with a line back to (10, 10)
  32. // add an ellipse as well, which will form a second sub-path within the path..
  33. myPath.addEllipse (50.0f, 50.0f, 40.0f, 30.0f);
  34. // double the width of the whole thing..
  35. myPath.applyTransform (AffineTransform::scale (2.0f, 1.0f));
  36. // and draw it to a graphics context with a 5-pixel thick outline.
  37. g.strokePath (myPath, PathStrokeType (5.0f));
  38. @endcode
  39. A path object can actually contain multiple sub-paths, which may themselves
  40. be open or closed.
  41. @see PathFlatteningIterator, PathStrokeType, Graphics
  42. */
  43. class JUCE_API Path
  44. {
  45. public:
  46. //==============================================================================
  47. /** Creates an empty path. */
  48. Path();
  49. /** Creates a copy of another path. */
  50. Path (const Path&);
  51. /** Destructor. */
  52. ~Path();
  53. /** Copies this path from another one. */
  54. Path& operator= (const Path&);
  55. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  56. Path (Path&&) noexcept;
  57. Path& operator= (Path&&) noexcept;
  58. #endif
  59. bool operator== (const Path&) const noexcept;
  60. bool operator!= (const Path&) const noexcept;
  61. static const float defaultToleranceForTesting;
  62. static const float defaultToleranceForMeasurement;
  63. //==============================================================================
  64. /** Returns true if the path doesn't contain any lines or curves. */
  65. bool isEmpty() const noexcept;
  66. /** Returns the smallest rectangle that contains all points within the path. */
  67. Rectangle<float> getBounds() const noexcept;
  68. /** Returns the smallest rectangle that contains all points within the path
  69. after it's been transformed with the given tranasform matrix.
  70. */
  71. Rectangle<float> getBoundsTransformed (const AffineTransform& transform) const noexcept;
  72. /** Checks whether a point lies within the path.
  73. This is only relevant for closed paths (see closeSubPath()), and
  74. may produce false results if used on a path which has open sub-paths.
  75. The path's winding rule is taken into account by this method.
  76. The tolerance parameter is the maximum error allowed when flattening the path,
  77. so this method could return a false positive when your point is up to this distance
  78. outside the path's boundary.
  79. @see closeSubPath, setUsingNonZeroWinding
  80. */
  81. bool contains (float x, float y,
  82. float tolerance = defaultToleranceForTesting) const;
  83. /** Checks whether a point lies within the path.
  84. This is only relevant for closed paths (see closeSubPath()), and
  85. may produce false results if used on a path which has open sub-paths.
  86. The path's winding rule is taken into account by this method.
  87. The tolerance parameter is the maximum error allowed when flattening the path,
  88. so this method could return a false positive when your point is up to this distance
  89. outside the path's boundary.
  90. @see closeSubPath, setUsingNonZeroWinding
  91. */
  92. bool contains (const Point<float> point,
  93. float tolerance = defaultToleranceForTesting) const;
  94. /** Checks whether a line crosses the path.
  95. This will return positive if the line crosses any of the paths constituent
  96. lines or curves. It doesn't take into account whether the line is inside
  97. or outside the path, or whether the path is open or closed.
  98. The tolerance parameter is the maximum error allowed when flattening the path,
  99. so this method could return a false positive when your point is up to this distance
  100. outside the path's boundary.
  101. */
  102. bool intersectsLine (Line<float> line,
  103. float tolerance = defaultToleranceForTesting);
  104. /** Cuts off parts of a line to keep the parts that are either inside or
  105. outside this path.
  106. Note that this isn't smart enough to cope with situations where the
  107. line would need to be cut into multiple pieces to correctly clip against
  108. a re-entrant shape.
  109. @param line the line to clip
  110. @param keepSectionOutsidePath if true, it's the section outside the path
  111. that will be kept; if false its the section inside
  112. the path
  113. */
  114. Line<float> getClippedLine (Line<float> line, bool keepSectionOutsidePath) const;
  115. /** Returns the length of the path.
  116. @see getPointAlongPath
  117. */
  118. float getLength (const AffineTransform& transform = AffineTransform(),
  119. float tolerance = defaultToleranceForMeasurement) const;
  120. /** Returns a point that is the specified distance along the path.
  121. If the distance is greater than the total length of the path, this will return the
  122. end point.
  123. @see getLength
  124. */
  125. Point<float> getPointAlongPath (float distanceFromStart,
  126. const AffineTransform& transform = AffineTransform(),
  127. float tolerance = defaultToleranceForMeasurement) const;
  128. /** Finds the point along the path which is nearest to a given position.
  129. This sets pointOnPath to the nearest point, and returns the distance of this point from the start
  130. of the path.
  131. */
  132. float getNearestPoint (Point<float> targetPoint,
  133. Point<float>& pointOnPath,
  134. const AffineTransform& transform = AffineTransform(),
  135. float tolerance = defaultToleranceForMeasurement) const;
  136. //==============================================================================
  137. /** Removes all lines and curves, resetting the path completely. */
  138. void clear() noexcept;
  139. /** Begins a new subpath with a given starting position.
  140. This will move the path's current position to the coordinates passed in and
  141. make it ready to draw lines or curves starting from this position.
  142. After adding whatever lines and curves are needed, you can either
  143. close the current sub-path using closeSubPath() or call startNewSubPath()
  144. to move to a new sub-path, leaving the old one open-ended.
  145. @see lineTo, quadraticTo, cubicTo, closeSubPath
  146. */
  147. void startNewSubPath (float startX, float startY);
  148. /** Begins a new subpath with a given starting position.
  149. This will move the path's current position to the coordinates passed in and
  150. make it ready to draw lines or curves starting from this position.
  151. After adding whatever lines and curves are needed, you can either
  152. close the current sub-path using closeSubPath() or call startNewSubPath()
  153. to move to a new sub-path, leaving the old one open-ended.
  154. @see lineTo, quadraticTo, cubicTo, closeSubPath
  155. */
  156. void startNewSubPath (const Point<float> start);
  157. /** Closes a the current sub-path with a line back to its start-point.
  158. When creating a closed shape such as a triangle, don't use 3 lineTo()
  159. calls - instead use two lineTo() calls, followed by a closeSubPath()
  160. to join the final point back to the start.
  161. This ensures that closes shapes are recognised as such, and this is
  162. important for tasks like drawing strokes, which needs to know whether to
  163. draw end-caps or not.
  164. @see startNewSubPath, lineTo, quadraticTo, cubicTo, closeSubPath
  165. */
  166. void closeSubPath();
  167. /** Adds a line from the shape's last position to a new end-point.
  168. This will connect the end-point of the last line or curve that was added
  169. to a new point, using a straight line.
  170. See the class description for an example of how to add lines and curves to a path.
  171. @see startNewSubPath, quadraticTo, cubicTo, closeSubPath
  172. */
  173. void lineTo (float endX, float endY);
  174. /** Adds a line from the shape's last position to a new end-point.
  175. This will connect the end-point of the last line or curve that was added
  176. to a new point, using a straight line.
  177. See the class description for an example of how to add lines and curves to a path.
  178. @see startNewSubPath, quadraticTo, cubicTo, closeSubPath
  179. */
  180. void lineTo (const Point<float> end);
  181. /** Adds a quadratic bezier curve from the shape's last position to a new position.
  182. This will connect the end-point of the last line or curve that was added
  183. to a new point, using a quadratic spline with one control-point.
  184. See the class description for an example of how to add lines and curves to a path.
  185. @see startNewSubPath, lineTo, cubicTo, closeSubPath
  186. */
  187. void quadraticTo (float controlPointX,
  188. float controlPointY,
  189. float endPointX,
  190. float endPointY);
  191. /** Adds a quadratic bezier curve from the shape's last position to a new position.
  192. This will connect the end-point of the last line or curve that was added
  193. to a new point, using a quadratic spline with one control-point.
  194. See the class description for an example of how to add lines and curves to a path.
  195. @see startNewSubPath, lineTo, cubicTo, closeSubPath
  196. */
  197. void quadraticTo (const Point<float> controlPoint,
  198. const Point<float> endPoint);
  199. /** Adds a cubic bezier curve from the shape's last position to a new position.
  200. This will connect the end-point of the last line or curve that was added
  201. to a new point, using a cubic spline with two control-points.
  202. See the class description for an example of how to add lines and curves to a path.
  203. @see startNewSubPath, lineTo, quadraticTo, closeSubPath
  204. */
  205. void cubicTo (float controlPoint1X,
  206. float controlPoint1Y,
  207. float controlPoint2X,
  208. float controlPoint2Y,
  209. float endPointX,
  210. float endPointY);
  211. /** Adds a cubic bezier curve from the shape's last position to a new position.
  212. This will connect the end-point of the last line or curve that was added
  213. to a new point, using a cubic spline with two control-points.
  214. See the class description for an example of how to add lines and curves to a path.
  215. @see startNewSubPath, lineTo, quadraticTo, closeSubPath
  216. */
  217. void cubicTo (const Point<float> controlPoint1,
  218. const Point<float> controlPoint2,
  219. const Point<float> endPoint);
  220. /** Returns the last point that was added to the path by one of the drawing methods.
  221. */
  222. Point<float> getCurrentPosition() const;
  223. //==============================================================================
  224. /** Adds a rectangle to the path.
  225. The rectangle is added as a new sub-path. (Any currently open paths will be left open).
  226. @see addRoundedRectangle, addTriangle
  227. */
  228. void addRectangle (float x, float y, float width, float height);
  229. /** Adds a rectangle to the path.
  230. The rectangle is added as a new sub-path. (Any currently open paths will be left open).
  231. @see addRoundedRectangle, addTriangle
  232. */
  233. template <typename ValueType>
  234. void addRectangle (const Rectangle<ValueType>& rectangle)
  235. {
  236. addRectangle (static_cast<float> (rectangle.getX()), static_cast<float> (rectangle.getY()),
  237. static_cast<float> (rectangle.getWidth()), static_cast<float> (rectangle.getHeight()));
  238. }
  239. /** Adds a rectangle with rounded corners to the path.
  240. The rectangle is added as a new sub-path. (Any currently open paths will be left open).
  241. @see addRectangle, addTriangle
  242. */
  243. void addRoundedRectangle (float x, float y, float width, float height,
  244. float cornerSize);
  245. /** Adds a rectangle with rounded corners to the path.
  246. The rectangle is added as a new sub-path. (Any currently open paths will be left open).
  247. @see addRectangle, addTriangle
  248. */
  249. void addRoundedRectangle (float x, float y, float width, float height,
  250. float cornerSizeX,
  251. float cornerSizeY);
  252. /** Adds a rectangle with rounded corners to the path.
  253. The rectangle is added as a new sub-path. (Any currently open paths will be left open).
  254. @see addRectangle, addTriangle
  255. */
  256. void addRoundedRectangle (float x, float y, float width, float height,
  257. float cornerSizeX, float cornerSizeY,
  258. bool curveTopLeft, bool curveTopRight,
  259. bool curveBottomLeft, bool curveBottomRight);
  260. /** Adds a rectangle with rounded corners to the path.
  261. The rectangle is added as a new sub-path. (Any currently open paths will be left open).
  262. @see addRectangle, addTriangle
  263. */
  264. template <typename ValueType>
  265. void addRoundedRectangle (const Rectangle<ValueType>& rectangle, float cornerSizeX, float cornerSizeY)
  266. {
  267. addRoundedRectangle (static_cast<float> (rectangle.getX()), static_cast<float> (rectangle.getY()),
  268. static_cast<float> (rectangle.getWidth()), static_cast<float> (rectangle.getHeight()),
  269. cornerSizeX, cornerSizeY);
  270. }
  271. /** Adds a rectangle with rounded corners to the path.
  272. The rectangle is added as a new sub-path. (Any currently open paths will be left open).
  273. @see addRectangle, addTriangle
  274. */
  275. template <typename ValueType>
  276. void addRoundedRectangle (const Rectangle<ValueType>& rectangle, float cornerSize)
  277. {
  278. addRoundedRectangle (rectangle, cornerSize, cornerSize);
  279. }
  280. /** Adds a triangle to the path.
  281. The triangle is added as a new closed sub-path. (Any currently open paths will be left open).
  282. Note that whether the vertices are specified in clockwise or anticlockwise
  283. order will affect how the triangle is filled when it overlaps other
  284. shapes (the winding order setting will affect this of course).
  285. */
  286. void addTriangle (float x1, float y1,
  287. float x2, float y2,
  288. float x3, float y3);
  289. /** Adds a triangle to the path.
  290. The triangle is added as a new closed sub-path. (Any currently open paths will be left open).
  291. Note that whether the vertices are specified in clockwise or anticlockwise
  292. order will affect how the triangle is filled when it overlaps other
  293. shapes (the winding order setting will affect this of course).
  294. */
  295. void addTriangle (Point<float> point1,
  296. Point<float> point2,
  297. Point<float> point3);
  298. /** Adds a quadrilateral to the path.
  299. The quad is added as a new closed sub-path. (Any currently open paths will be left open).
  300. Note that whether the vertices are specified in clockwise or anticlockwise
  301. order will affect how the quad is filled when it overlaps other
  302. shapes (the winding order setting will affect this of course).
  303. */
  304. void addQuadrilateral (float x1, float y1,
  305. float x2, float y2,
  306. float x3, float y3,
  307. float x4, float y4);
  308. /** Adds an ellipse to the path.
  309. The shape is added as a new sub-path. (Any currently open paths will be left open).
  310. @see addArc
  311. */
  312. void addEllipse (float x, float y, float width, float height);
  313. /** Adds an ellipse to the path.
  314. The shape is added as a new sub-path. (Any currently open paths will be left open).
  315. @see addArc
  316. */
  317. void addEllipse (Rectangle<float> area);
  318. /** Adds an elliptical arc to the current path.
  319. Note that when specifying the start and end angles, the curve will be drawn either clockwise
  320. or anti-clockwise according to whether the end angle is greater than the start. This means
  321. that sometimes you may need to use values greater than 2*Pi for the end angle.
  322. @param x the left-hand edge of the rectangle in which the elliptical outline fits
  323. @param y the top edge of the rectangle in which the elliptical outline fits
  324. @param width the width of the rectangle in which the elliptical outline fits
  325. @param height the height of the rectangle in which the elliptical outline fits
  326. @param fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the
  327. top-centre of the ellipse)
  328. @param toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the
  329. top-centre of the ellipse). This angle can be greater than 2*Pi, so for example to
  330. draw a curve clockwise from the 9 o'clock position to the 3 o'clock position via
  331. 12 o'clock, you'd use 1.5*Pi and 2.5*Pi as the start and finish points.
  332. @param startAsNewSubPath if true, the arc will begin a new subpath from its starting point; if false,
  333. it will be added to the current sub-path, continuing from the current postition
  334. @see addCentredArc, arcTo, addPieSegment, addEllipse
  335. */
  336. void addArc (float x, float y, float width, float height,
  337. float fromRadians,
  338. float toRadians,
  339. bool startAsNewSubPath = false);
  340. /** Adds an arc which is centred at a given point, and can have a rotation specified.
  341. Note that when specifying the start and end angles, the curve will be drawn either clockwise
  342. or anti-clockwise according to whether the end angle is greater than the start. This means
  343. that sometimes you may need to use values greater than 2*Pi for the end angle.
  344. @param centreX the centre x of the ellipse
  345. @param centreY the centre y of the ellipse
  346. @param radiusX the horizontal radius of the ellipse
  347. @param radiusY the vertical radius of the ellipse
  348. @param rotationOfEllipse an angle by which the whole ellipse should be rotated about its centre, in radians (clockwise)
  349. @param fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the
  350. top-centre of the ellipse)
  351. @param toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the
  352. top-centre of the ellipse). This angle can be greater than 2*Pi, so for example to
  353. draw a curve clockwise from the 9 o'clock position to the 3 o'clock position via
  354. 12 o'clock, you'd use 1.5*Pi and 2.5*Pi as the start and finish points.
  355. @param startAsNewSubPath if true, the arc will begin a new subpath from its starting point; if false,
  356. it will be added to the current sub-path, continuing from the current postition
  357. @see addArc, arcTo
  358. */
  359. void addCentredArc (float centreX, float centreY,
  360. float radiusX, float radiusY,
  361. float rotationOfEllipse,
  362. float fromRadians,
  363. float toRadians,
  364. bool startAsNewSubPath = false);
  365. /** Adds a "pie-chart" shape to the path.
  366. The shape is added as a new sub-path. (Any currently open paths will be
  367. left open).
  368. Note that when specifying the start and end angles, the curve will be drawn either clockwise
  369. or anti-clockwise according to whether the end angle is greater than the start. This means
  370. that sometimes you may need to use values greater than 2*Pi for the end angle.
  371. @param x the left-hand edge of the rectangle in which the elliptical outline fits
  372. @param y the top edge of the rectangle in which the elliptical outline fits
  373. @param width the width of the rectangle in which the elliptical outline fits
  374. @param height the height of the rectangle in which the elliptical outline fits
  375. @param fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the
  376. top-centre of the ellipse)
  377. @param toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the
  378. top-centre of the ellipse)
  379. @param innerCircleProportionalSize if this is > 0, then the pie will be drawn as a curved band around a hollow
  380. ellipse at its centre, where this value indicates the inner ellipse's size with
  381. respect to the outer one.
  382. @see addArc
  383. */
  384. void addPieSegment (float x, float y,
  385. float width, float height,
  386. float fromRadians,
  387. float toRadians,
  388. float innerCircleProportionalSize);
  389. /** Adds a "pie-chart" shape to the path.
  390. The shape is added as a new sub-path. (Any currently open paths will be left open).
  391. Note that when specifying the start and end angles, the curve will be drawn either clockwise
  392. or anti-clockwise according to whether the end angle is greater than the start. This means
  393. that sometimes you may need to use values greater than 2*Pi for the end angle.
  394. @param segmentBounds the outer rectangle in which the elliptical outline fits
  395. @param fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the
  396. top-centre of the ellipse)
  397. @param toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the
  398. top-centre of the ellipse)
  399. @param innerCircleProportionalSize if this is > 0, then the pie will be drawn as a curved band around a hollow
  400. ellipse at its centre, where this value indicates the inner ellipse's size with
  401. respect to the outer one.
  402. @see addArc
  403. */
  404. void addPieSegment (Rectangle<float> segmentBounds,
  405. float fromRadians,
  406. float toRadians,
  407. float innerCircleProportionalSize);
  408. /** Adds a line with a specified thickness.
  409. The line is added as a new closed sub-path. (Any currently open paths will be
  410. left open).
  411. @see addArrow
  412. */
  413. void addLineSegment (const Line<float>& line, float lineThickness);
  414. /** Adds a line with an arrowhead on the end.
  415. The arrow is added as a new closed sub-path. (Any currently open paths will be left open).
  416. @see PathStrokeType::createStrokeWithArrowheads
  417. */
  418. void addArrow (const Line<float>& line,
  419. float lineThickness,
  420. float arrowheadWidth,
  421. float arrowheadLength);
  422. /** Adds a polygon shape to the path.
  423. @see addStar
  424. */
  425. void addPolygon (const Point<float> centre,
  426. int numberOfSides,
  427. float radius,
  428. float startAngle = 0.0f);
  429. /** Adds a star shape to the path.
  430. @see addPolygon
  431. */
  432. void addStar (const Point<float> centre,
  433. int numberOfPoints,
  434. float innerRadius,
  435. float outerRadius,
  436. float startAngle = 0.0f);
  437. /** Adds a speech-bubble shape to the path.
  438. @param bodyArea the area of the body of the bubble shape
  439. @param maximumArea an area which encloses the body area and defines the limits within which
  440. the arrow tip can be drawn - if the tip lies outside this area, the bubble
  441. will be drawn without an arrow
  442. @param arrowTipPosition the location of the tip of the arrow
  443. @param cornerSize the size of the rounded corners
  444. @param arrowBaseWidth the width of the base of the arrow where it joins the main rectangle
  445. */
  446. void addBubble (const Rectangle<float>& bodyArea,
  447. const Rectangle<float>& maximumArea,
  448. const Point<float> arrowTipPosition,
  449. const float cornerSize,
  450. const float arrowBaseWidth);
  451. /** Adds another path to this one.
  452. The new path is added as a new sub-path. (Any currently open paths in this
  453. path will be left open).
  454. @param pathToAppend the path to add
  455. */
  456. void addPath (const Path& pathToAppend);
  457. /** Adds another path to this one, transforming it on the way in.
  458. The new path is added as a new sub-path, its points being transformed by the given
  459. matrix before being added.
  460. @param pathToAppend the path to add
  461. @param transformToApply an optional transform to apply to the incoming vertices
  462. */
  463. void addPath (const Path& pathToAppend,
  464. const AffineTransform& transformToApply);
  465. /** Swaps the contents of this path with another one.
  466. The internal data of the two paths is swapped over, so this is much faster than
  467. copying it to a temp variable and back.
  468. */
  469. void swapWithPath (Path&) noexcept;
  470. //==============================================================================
  471. /** Preallocates enough space for adding the given number of coordinates to the path.
  472. If you're about to add a large number of lines or curves to the path, it can make
  473. the task much more efficient to call this first and avoid costly reallocations
  474. as the structure grows.
  475. The actual value to pass is a bit tricky to calculate because the space required
  476. depends on what you're adding - e.g. each lineTo() or startNewSubPath() will
  477. require 3 coords (x, y and a type marker). Each quadraticTo() will need 5, and
  478. a cubicTo() will require 7. Closing a sub-path will require 1.
  479. */
  480. void preallocateSpace (int numExtraCoordsToMakeSpaceFor);
  481. //==============================================================================
  482. /** Applies a 2D transform to all the vertices in the path.
  483. @see AffineTransform, scaleToFit, getTransformToScaleToFit
  484. */
  485. void applyTransform (const AffineTransform& transform) noexcept;
  486. /** Rescales this path to make it fit neatly into a given space.
  487. This is effectively a quick way of calling
  488. applyTransform (getTransformToScaleToFit (x, y, w, h, preserveProportions))
  489. @param x the x position of the rectangle to fit the path inside
  490. @param y the y position of the rectangle to fit the path inside
  491. @param width the width of the rectangle to fit the path inside
  492. @param height the height of the rectangle to fit the path inside
  493. @param preserveProportions if true, it will fit the path into the space without altering its
  494. horizontal/vertical scale ratio; if false, it will distort the
  495. path to fill the specified ratio both horizontally and vertically
  496. @see applyTransform, getTransformToScaleToFit
  497. */
  498. void scaleToFit (float x, float y, float width, float height,
  499. bool preserveProportions) noexcept;
  500. /** Returns a transform that can be used to rescale the path to fit into a given space.
  501. @param x the x position of the rectangle to fit the path inside
  502. @param y the y position of the rectangle to fit the path inside
  503. @param width the width of the rectangle to fit the path inside
  504. @param height the height of the rectangle to fit the path inside
  505. @param preserveProportions if true, it will fit the path into the space without altering its
  506. horizontal/vertical scale ratio; if false, it will distort the
  507. path to fill the specified ratio both horizontally and vertically
  508. @param justificationType if the proportions are preseved, the resultant path may be smaller
  509. than the available rectangle, so this describes how it should be
  510. positioned within the space.
  511. @returns an appropriate transformation
  512. @see applyTransform, scaleToFit
  513. */
  514. AffineTransform getTransformToScaleToFit (float x, float y, float width, float height,
  515. bool preserveProportions,
  516. Justification justificationType = Justification::centred) const;
  517. /** Returns a transform that can be used to rescale the path to fit into a given space.
  518. @param area the rectangle to fit the path inside
  519. @param preserveProportions if true, it will fit the path into the space without altering its
  520. horizontal/vertical scale ratio; if false, it will distort the
  521. path to fill the specified ratio both horizontally and vertically
  522. @param justificationType if the proportions are preseved, the resultant path may be smaller
  523. than the available rectangle, so this describes how it should be
  524. positioned within the space.
  525. @returns an appropriate transformation
  526. @see applyTransform, scaleToFit
  527. */
  528. AffineTransform getTransformToScaleToFit (const Rectangle<float>& area,
  529. bool preserveProportions,
  530. Justification justificationType = Justification::centred) const;
  531. /** Creates a version of this path where all sharp corners have been replaced by curves.
  532. Wherever two lines meet at an angle, this will replace the corner with a curve
  533. of the given radius.
  534. */
  535. Path createPathWithRoundedCorners (float cornerRadius) const;
  536. //==============================================================================
  537. /** Changes the winding-rule to be used when filling the path.
  538. If set to true (which is the default), then the path uses a non-zero-winding rule
  539. to determine which points are inside the path. If set to false, it uses an
  540. alternate-winding rule.
  541. The winding-rule comes into play when areas of the shape overlap other
  542. areas, and determines whether the overlapping regions are considered to be
  543. inside or outside.
  544. Changing this value just sets a flag - it doesn't affect the contents of the
  545. path.
  546. @see isUsingNonZeroWinding
  547. */
  548. void setUsingNonZeroWinding (bool isNonZeroWinding) noexcept;
  549. /** Returns the flag that indicates whether the path should use a non-zero winding rule.
  550. The default for a new path is true.
  551. @see setUsingNonZeroWinding
  552. */
  553. bool isUsingNonZeroWinding() const { return useNonZeroWinding; }
  554. //==============================================================================
  555. /** Iterates the lines and curves that a path contains.
  556. @see Path, PathFlatteningIterator
  557. */
  558. class JUCE_API Iterator
  559. {
  560. public:
  561. //==============================================================================
  562. Iterator (const Path& path) noexcept;
  563. ~Iterator() noexcept;
  564. //==============================================================================
  565. /** Moves onto the next element in the path.
  566. If this returns false, there are no more elements. If it returns true,
  567. the elementType variable will be set to the type of the current element,
  568. and some of the x and y variables will be filled in with values.
  569. */
  570. bool next() noexcept;
  571. //==============================================================================
  572. enum PathElementType
  573. {
  574. startNewSubPath, /**< For this type, x1 and y1 will be set to indicate the first point in the subpath. */
  575. lineTo, /**< For this type, x1 and y1 indicate the end point of the line. */
  576. quadraticTo, /**< For this type, x1, y1, x2, y2 indicate the control point and endpoint of a quadratic curve. */
  577. cubicTo, /**< For this type, x1, y1, x2, y2, x3, y3 indicate the two control points and the endpoint of a cubic curve. */
  578. closePath /**< Indicates that the sub-path is being closed. None of the x or y values are valid in this case. */
  579. };
  580. PathElementType elementType;
  581. float x1, y1, x2, y2, x3, y3;
  582. //==============================================================================
  583. private:
  584. const Path& path;
  585. size_t index;
  586. JUCE_DECLARE_NON_COPYABLE (Iterator)
  587. };
  588. //==============================================================================
  589. /** Loads a stored path from a data stream.
  590. The data in the stream must have been written using writePathToStream().
  591. Note that this will append the stored path to whatever is currently in
  592. this path, so you might need to call clear() beforehand.
  593. @see loadPathFromData, writePathToStream
  594. */
  595. void loadPathFromStream (InputStream& source);
  596. /** Loads a stored path from a block of data.
  597. This is similar to loadPathFromStream(), but just reads from a block
  598. of data. Useful if you're including stored shapes in your code as a
  599. block of static data.
  600. @see loadPathFromStream, writePathToStream
  601. */
  602. void loadPathFromData (const void* data, size_t numberOfBytes);
  603. /** Stores the path by writing it out to a stream.
  604. After writing out a path, you can reload it using loadPathFromStream().
  605. @see loadPathFromStream, loadPathFromData
  606. */
  607. void writePathToStream (OutputStream& destination) const;
  608. //==============================================================================
  609. /** Creates a string containing a textual representation of this path.
  610. @see restoreFromString
  611. */
  612. String toString() const;
  613. /** Restores this path from a string that was created with the toString() method.
  614. @see toString()
  615. */
  616. void restoreFromString (StringRef stringVersion);
  617. private:
  618. //==============================================================================
  619. friend class PathFlatteningIterator;
  620. friend class Path::Iterator;
  621. ArrayAllocationBase<float, DummyCriticalSection> data;
  622. size_t numElements;
  623. struct PathBounds
  624. {
  625. PathBounds() noexcept;
  626. Rectangle<float> getRectangle() const noexcept;
  627. void reset() noexcept;
  628. void reset (float, float) noexcept;
  629. void extend (float, float) noexcept;
  630. void extend (float, float, float, float) noexcept;
  631. float pathXMin, pathXMax, pathYMin, pathYMax;
  632. };
  633. PathBounds bounds;
  634. bool useNonZeroWinding;
  635. static const float lineMarker;
  636. static const float moveMarker;
  637. static const float quadMarker;
  638. static const float cubicMarker;
  639. static const float closeSubPathMarker;
  640. JUCE_LEAK_DETECTOR (Path)
  641. };
  642. #endif // JUCE_PATH_H_INCLUDED