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.

793 lines
35KB

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