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.

778 lines
34KB

  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 elliptical arc to the current path.
  301. Note that when specifying the start and end angles, the curve will be drawn either clockwise
  302. or anti-clockwise according to whether the end angle is greater than the start. This means
  303. that sometimes you may need to use values greater than 2*Pi for the end angle.
  304. @param x the left-hand edge of the rectangle in which the elliptical outline fits
  305. @param y the top edge of the rectangle in which the elliptical outline fits
  306. @param width the width of the rectangle in which the elliptical outline fits
  307. @param height the height of the rectangle in which the elliptical outline fits
  308. @param fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the
  309. top-centre of the ellipse)
  310. @param toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the
  311. top-centre of the ellipse). This angle can be greater than 2*Pi, so for example to
  312. draw a curve clockwise from the 9 o'clock position to the 3 o'clock position via
  313. 12 o'clock, you'd use 1.5*Pi and 2.5*Pi as the start and finish points.
  314. @param startAsNewSubPath if true, the arc will begin a new subpath from its starting point; if false,
  315. it will be added to the current sub-path, continuing from the current postition
  316. @see addCentredArc, arcTo, addPieSegment, addEllipse
  317. */
  318. void addArc (float x, float y, float width, float height,
  319. float fromRadians,
  320. float toRadians,
  321. bool startAsNewSubPath = false);
  322. /** Adds an arc which is centred at a given point, and can have a rotation specified.
  323. Note that when specifying the start and end angles, the curve will be drawn either clockwise
  324. or anti-clockwise according to whether the end angle is greater than the start. This means
  325. that sometimes you may need to use values greater than 2*Pi for the end angle.
  326. @param centreX the centre x of the ellipse
  327. @param centreY the centre y of the ellipse
  328. @param radiusX the horizontal radius of the ellipse
  329. @param radiusY the vertical radius of the ellipse
  330. @param rotationOfEllipse an angle by which the whole ellipse should be rotated about its centre, in radians (clockwise)
  331. @param fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the
  332. top-centre of the ellipse)
  333. @param toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the
  334. top-centre of the ellipse). This angle can be greater than 2*Pi, so for example to
  335. draw a curve clockwise from the 9 o'clock position to the 3 o'clock position via
  336. 12 o'clock, you'd use 1.5*Pi and 2.5*Pi as the start and finish points.
  337. @param startAsNewSubPath if true, the arc will begin a new subpath from its starting point; if false,
  338. it will be added to the current sub-path, continuing from the current postition
  339. @see addArc, arcTo
  340. */
  341. void addCentredArc (float centreX, float centreY,
  342. float radiusX, float radiusY,
  343. float rotationOfEllipse,
  344. float fromRadians,
  345. float toRadians,
  346. bool startAsNewSubPath = false);
  347. /** Adds a "pie-chart" shape to the path.
  348. The shape is added as a new sub-path. (Any currently open paths will be
  349. left open).
  350. Note that when specifying the start and end angles, the curve will be drawn either clockwise
  351. or anti-clockwise according to whether the end angle is greater than the start. This means
  352. that sometimes you may need to use values greater than 2*Pi for the end angle.
  353. @param x the left-hand edge of the rectangle in which the elliptical outline fits
  354. @param y the top edge of the rectangle in which the elliptical outline fits
  355. @param width the width of the rectangle in which the elliptical outline fits
  356. @param height the height of the rectangle in which the elliptical outline fits
  357. @param fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the
  358. top-centre of the ellipse)
  359. @param toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the
  360. top-centre of the ellipse)
  361. @param innerCircleProportionalSize if this is > 0, then the pie will be drawn as a curved band around a hollow
  362. ellipse at its centre, where this value indicates the inner ellipse's size with
  363. respect to the outer one.
  364. @see addArc
  365. */
  366. void addPieSegment (float x, float y,
  367. float width, float height,
  368. float fromRadians,
  369. float toRadians,
  370. float innerCircleProportionalSize);
  371. /** Adds a line with a specified thickness.
  372. The line is added as a new closed sub-path. (Any currently open paths will be
  373. left open).
  374. @see addArrow
  375. */
  376. void addLineSegment (const Line<float>& line, float lineThickness);
  377. /** Adds a line with an arrowhead on the end.
  378. The arrow is added as a new closed sub-path. (Any currently open paths will be left open).
  379. @see PathStrokeType::createStrokeWithArrowheads
  380. */
  381. void addArrow (const Line<float>& line,
  382. float lineThickness,
  383. float arrowheadWidth,
  384. float arrowheadLength);
  385. /** Adds a polygon shape to the path.
  386. @see addStar
  387. */
  388. void addPolygon (const Point<float> centre,
  389. int numberOfSides,
  390. float radius,
  391. float startAngle = 0.0f);
  392. /** Adds a star shape to the path.
  393. @see addPolygon
  394. */
  395. void addStar (const Point<float> centre,
  396. int numberOfPoints,
  397. float innerRadius,
  398. float outerRadius,
  399. float startAngle = 0.0f);
  400. /** Adds a speech-bubble shape to the path.
  401. @param bodyArea the area of the body of the bubble shape
  402. @param maximumArea an area which encloses the body area and defines the limits within which
  403. the arrow tip can be drawn - if the tip lies outside this area, the bubble
  404. will be drawn without an arrow
  405. @param arrowTipPosition the location of the tip of the arrow
  406. @param cornerSize the size of the rounded corners
  407. @param arrowBaseWidth the width of the base of the arrow where it joins the main rectangle
  408. */
  409. void addBubble (const Rectangle<float>& bodyArea,
  410. const Rectangle<float>& maximumArea,
  411. const Point<float> arrowTipPosition,
  412. const float cornerSize,
  413. const float arrowBaseWidth);
  414. /** Adds another path to this one.
  415. The new path is added as a new sub-path. (Any currently open paths in this
  416. path will be left open).
  417. @param pathToAppend the path to add
  418. */
  419. void addPath (const Path& pathToAppend);
  420. /** Adds another path to this one, transforming it on the way in.
  421. The new path is added as a new sub-path, its points being transformed by the given
  422. matrix before being added.
  423. @param pathToAppend the path to add
  424. @param transformToApply an optional transform to apply to the incoming vertices
  425. */
  426. void addPath (const Path& pathToAppend,
  427. const AffineTransform& transformToApply);
  428. /** Swaps the contents of this path with another one.
  429. The internal data of the two paths is swapped over, so this is much faster than
  430. copying it to a temp variable and back.
  431. */
  432. void swapWithPath (Path&) noexcept;
  433. //==============================================================================
  434. /** Applies a 2D transform to all the vertices in the path.
  435. @see AffineTransform, scaleToFit, getTransformToScaleToFit
  436. */
  437. void applyTransform (const AffineTransform& transform) noexcept;
  438. /** Rescales this path to make it fit neatly into a given space.
  439. This is effectively a quick way of calling
  440. applyTransform (getTransformToScaleToFit (x, y, w, h, preserveProportions))
  441. @param x the x position of the rectangle to fit the path inside
  442. @param y the y position of the rectangle to fit the path inside
  443. @param width the width of the rectangle to fit the path inside
  444. @param height the height of the rectangle to fit the path inside
  445. @param preserveProportions if true, it will fit the path into the space without altering its
  446. horizontal/vertical scale ratio; if false, it will distort the
  447. path to fill the specified ratio both horizontally and vertically
  448. @see applyTransform, getTransformToScaleToFit
  449. */
  450. void scaleToFit (float x, float y, float width, float height,
  451. bool preserveProportions) noexcept;
  452. /** Returns a transform that can be used to rescale the path to fit into a given space.
  453. @param x the x position of the rectangle to fit the path inside
  454. @param y the y position of the rectangle to fit the path inside
  455. @param width the width of the rectangle to fit the path inside
  456. @param height the height of the rectangle to fit the path inside
  457. @param preserveProportions if true, it will fit the path into the space without altering its
  458. horizontal/vertical scale ratio; if false, it will distort the
  459. path to fill the specified ratio both horizontally and vertically
  460. @param justificationType if the proportions are preseved, the resultant path may be smaller
  461. than the available rectangle, so this describes how it should be
  462. positioned within the space.
  463. @returns an appropriate transformation
  464. @see applyTransform, scaleToFit
  465. */
  466. AffineTransform getTransformToScaleToFit (float x, float y, float width, float height,
  467. bool preserveProportions,
  468. Justification justificationType = Justification::centred) const;
  469. /** Returns a transform that can be used to rescale the path to fit into a given space.
  470. @param area the rectangle to fit the path inside
  471. @param preserveProportions if true, it will fit the path into the space without altering its
  472. horizontal/vertical scale ratio; if false, it will distort the
  473. path to fill the specified ratio both horizontally and vertically
  474. @param justificationType if the proportions are preseved, the resultant path may be smaller
  475. than the available rectangle, so this describes how it should be
  476. positioned within the space.
  477. @returns an appropriate transformation
  478. @see applyTransform, scaleToFit
  479. */
  480. AffineTransform getTransformToScaleToFit (const Rectangle<float>& area,
  481. bool preserveProportions,
  482. Justification justificationType = Justification::centred) const;
  483. /** Creates a version of this path where all sharp corners have been replaced by curves.
  484. Wherever two lines meet at an angle, this will replace the corner with a curve
  485. of the given radius.
  486. */
  487. Path createPathWithRoundedCorners (float cornerRadius) const;
  488. //==============================================================================
  489. /** Changes the winding-rule to be used when filling the path.
  490. If set to true (which is the default), then the path uses a non-zero-winding rule
  491. to determine which points are inside the path. If set to false, it uses an
  492. alternate-winding rule.
  493. The winding-rule comes into play when areas of the shape overlap other
  494. areas, and determines whether the overlapping regions are considered to be
  495. inside or outside.
  496. Changing this value just sets a flag - it doesn't affect the contents of the
  497. path.
  498. @see isUsingNonZeroWinding
  499. */
  500. void setUsingNonZeroWinding (bool isNonZeroWinding) noexcept;
  501. /** Returns the flag that indicates whether the path should use a non-zero winding rule.
  502. The default for a new path is true.
  503. @see setUsingNonZeroWinding
  504. */
  505. bool isUsingNonZeroWinding() const { return useNonZeroWinding; }
  506. //==============================================================================
  507. /** Iterates the lines and curves that a path contains.
  508. @see Path, PathFlatteningIterator
  509. */
  510. class JUCE_API Iterator
  511. {
  512. public:
  513. //==============================================================================
  514. Iterator (const Path& path);
  515. ~Iterator();
  516. //==============================================================================
  517. /** Moves onto the next element in the path.
  518. If this returns false, there are no more elements. If it returns true,
  519. the elementType variable will be set to the type of the current element,
  520. and some of the x and y variables will be filled in with values.
  521. */
  522. bool next();
  523. //==============================================================================
  524. enum PathElementType
  525. {
  526. startNewSubPath, /**< For this type, x1 and y1 will be set to indicate the first point in the subpath. */
  527. lineTo, /**< For this type, x1 and y1 indicate the end point of the line. */
  528. quadraticTo, /**< For this type, x1, y1, x2, y2 indicate the control point and endpoint of a quadratic curve. */
  529. cubicTo, /**< For this type, x1, y1, x2, y2, x3, y3 indicate the two control points and the endpoint of a cubic curve. */
  530. closePath /**< Indicates that the sub-path is being closed. None of the x or y values are valid in this case. */
  531. };
  532. PathElementType elementType;
  533. float x1, y1, x2, y2, x3, y3;
  534. //==============================================================================
  535. private:
  536. const Path& path;
  537. size_t index;
  538. JUCE_DECLARE_NON_COPYABLE (Iterator)
  539. };
  540. //==============================================================================
  541. /** Loads a stored path from a data stream.
  542. The data in the stream must have been written using writePathToStream().
  543. Note that this will append the stored path to whatever is currently in
  544. this path, so you might need to call clear() beforehand.
  545. @see loadPathFromData, writePathToStream
  546. */
  547. void loadPathFromStream (InputStream& source);
  548. /** Loads a stored path from a block of data.
  549. This is similar to loadPathFromStream(), but just reads from a block
  550. of data. Useful if you're including stored shapes in your code as a
  551. block of static data.
  552. @see loadPathFromStream, writePathToStream
  553. */
  554. void loadPathFromData (const void* data, size_t numberOfBytes);
  555. /** Stores the path by writing it out to a stream.
  556. After writing out a path, you can reload it using loadPathFromStream().
  557. @see loadPathFromStream, loadPathFromData
  558. */
  559. void writePathToStream (OutputStream& destination) const;
  560. //==============================================================================
  561. /** Creates a string containing a textual representation of this path.
  562. @see restoreFromString
  563. */
  564. String toString() const;
  565. /** Restores this path from a string that was created with the toString() method.
  566. @see toString()
  567. */
  568. void restoreFromString (StringRef stringVersion);
  569. private:
  570. //==============================================================================
  571. friend class PathFlatteningIterator;
  572. friend class Path::Iterator;
  573. ArrayAllocationBase <float, DummyCriticalSection> data;
  574. size_t numElements;
  575. struct PathBounds
  576. {
  577. PathBounds() noexcept;
  578. Rectangle<float> getRectangle() const noexcept;
  579. void reset() noexcept;
  580. void reset (float, float) noexcept;
  581. void extend (float, float) noexcept;
  582. void extend (float, float, float, float) noexcept;
  583. float pathXMin, pathXMax, pathYMin, pathYMax;
  584. };
  585. PathBounds bounds;
  586. bool useNonZeroWinding;
  587. static const float lineMarker;
  588. static const float moveMarker;
  589. static const float quadMarker;
  590. static const float cubicMarker;
  591. static const float closeSubPathMarker;
  592. JUCE_LEAK_DETECTOR (Path)
  593. };
  594. #endif // JUCE_PATH_H_INCLUDED