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.

830 lines
37KB

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