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.

827 lines
37KB

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