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.

832 lines
37KB

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