The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

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