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.

833 lines
37KB

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