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.

747 lines
34KB

  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A graphics context, used for drawing a component or image.
  24. When a Component needs painting, a Graphics context is passed to its
  25. Component::paint() method, and this you then call methods within this
  26. object to actually draw the component's content.
  27. A Graphics can also be created from an image, to allow drawing directly onto
  28. that image.
  29. @see Component::paint
  30. */
  31. class JUCE_API Graphics
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a Graphics object to draw directly onto the given image.
  36. The graphics object that is created will be set up to draw onto the image,
  37. with the context's clipping area being the entire size of the image, and its
  38. origin being the image's origin. To draw into a subsection of an image, use the
  39. reduceClipRegion() and setOrigin() methods.
  40. Obviously you shouldn't delete the image before this context is deleted.
  41. */
  42. explicit Graphics (const Image& imageToDrawOnto);
  43. /** Destructor. */
  44. ~Graphics();
  45. //==============================================================================
  46. /** Changes the current drawing colour.
  47. This sets the colour that will now be used for drawing operations - it also
  48. sets the opacity to that of the colour passed-in.
  49. If a brush is being used when this method is called, the brush will be deselected,
  50. and any subsequent drawing will be done with a solid colour brush instead.
  51. @see setOpacity
  52. */
  53. void setColour (Colour newColour);
  54. /** Changes the opacity to use with the current colour.
  55. If a solid colour is being used for drawing, this changes its opacity
  56. to this new value (i.e. it doesn't multiply the colour's opacity by this amount).
  57. If a gradient is being used, this will have no effect on it.
  58. A value of 0.0 is completely transparent, 1.0 is completely opaque.
  59. */
  60. void setOpacity (float newOpacity);
  61. /** Sets the context to use a gradient for its fill pattern.
  62. */
  63. void setGradientFill (const ColourGradient& gradient);
  64. /** Sets the context to use a tiled image pattern for filling.
  65. Make sure that you don't delete this image while it's still being used by
  66. this context!
  67. */
  68. void setTiledImageFill (const Image& imageToUse,
  69. int anchorX, int anchorY,
  70. float opacity);
  71. /** Changes the current fill settings.
  72. @see setColour, setGradientFill, setTiledImageFill
  73. */
  74. void setFillType (const FillType& newFill);
  75. //==============================================================================
  76. /** Changes the font to use for subsequent text-drawing functions.
  77. @see drawSingleLineText, drawMultiLineText, drawText, drawFittedText
  78. */
  79. void setFont (const Font& newFont);
  80. /** Changes the size of the currently-selected font.
  81. This is a convenient shortcut that changes the context's current font to a
  82. different size. The typeface won't be changed.
  83. @see Font
  84. */
  85. void setFont (float newFontHeight);
  86. /** Returns the currently selected font. */
  87. Font getCurrentFont() const;
  88. /** Draws a one-line text string.
  89. This will use the current colour (or brush) to fill the text. The font is the last
  90. one specified by setFont().
  91. @param text the string to draw
  92. @param startX the position to draw the left-hand edge of the text
  93. @param baselineY the position of the text's baseline
  94. @param justification the horizontal flags indicate which end of the text string is
  95. anchored at the specified point.
  96. @see drawMultiLineText, drawText, drawFittedText, GlyphArrangement::addLineOfText
  97. */
  98. void drawSingleLineText (const String& text,
  99. int startX, int baselineY,
  100. Justification justification = Justification::left) const;
  101. /** Draws text across multiple lines.
  102. This will break the text onto a new line where there's a new-line or
  103. carriage-return character, or at a word-boundary when the text becomes wider
  104. than the size specified by the maximumLineWidth parameter.
  105. @see setFont, drawSingleLineText, drawFittedText, GlyphArrangement::addJustifiedText
  106. */
  107. void drawMultiLineText (const String& text,
  108. int startX, int baselineY,
  109. int maximumLineWidth) const;
  110. /** Draws a line of text within a specified rectangle.
  111. The text will be positioned within the rectangle based on the justification
  112. flags passed-in. If the string is too long to fit inside the rectangle, it will
  113. either be truncated or will have ellipsis added to its end (if the useEllipsesIfTooBig
  114. flag is true).
  115. @see drawSingleLineText, drawFittedText, drawMultiLineText, GlyphArrangement::addJustifiedText
  116. */
  117. void drawText (const String& text,
  118. int x, int y, int width, int height,
  119. Justification justificationType,
  120. bool useEllipsesIfTooBig = true) const;
  121. /** Draws a line of text within a specified rectangle.
  122. The text will be positioned within the rectangle based on the justification
  123. flags passed-in. If the string is too long to fit inside the rectangle, it will
  124. either be truncated or will have ellipsis added to its end (if the useEllipsesIfTooBig
  125. flag is true).
  126. @see drawSingleLineText, drawFittedText, drawMultiLineText, GlyphArrangement::addJustifiedText
  127. */
  128. void drawText (const String& text,
  129. Rectangle<int> area,
  130. Justification justificationType,
  131. bool useEllipsesIfTooBig = true) const;
  132. /** Draws a line of text within a specified rectangle.
  133. The text will be positioned within the rectangle based on the justification
  134. flags passed-in. If the string is too long to fit inside the rectangle, it will
  135. either be truncated or will have ellipsis added to its end (if the useEllipsesIfTooBig
  136. flag is true).
  137. @see drawSingleLineText, drawFittedText, drawMultiLineText, GlyphArrangement::addJustifiedText
  138. */
  139. void drawText (const String& text,
  140. Rectangle<float> area,
  141. Justification justificationType,
  142. bool useEllipsesIfTooBig = true) const;
  143. /** Tries to draw a text string inside a given space.
  144. This does its best to make the given text readable within the specified rectangle,
  145. so it useful for labelling things.
  146. If the text is too big, it'll be squashed horizontally or broken over multiple lines
  147. if the maximumLinesToUse value allows this. If the text just won't fit into the space,
  148. it'll cram as much as possible in there, and put some ellipsis at the end to show that
  149. it's been truncated.
  150. A Justification parameter lets you specify how the text is laid out within the rectangle,
  151. both horizontally and vertically.
  152. The minimumHorizontalScale parameter specifies how much the text can be squashed horizontally
  153. to try to squeeze it into the space. If you don't want any horizontal scaling to occur, you
  154. can set this value to 1.0f. Pass 0 if you want it to use a default value.
  155. @see GlyphArrangement::addFittedText
  156. */
  157. void drawFittedText (const String& text,
  158. int x, int y, int width, int height,
  159. Justification justificationFlags,
  160. int maximumNumberOfLines,
  161. float minimumHorizontalScale = 0.0f) const;
  162. /** Tries to draw a text string inside a given space.
  163. This does its best to make the given text readable within the specified rectangle,
  164. so it useful for labelling things.
  165. If the text is too big, it'll be squashed horizontally or broken over multiple lines
  166. if the maximumLinesToUse value allows this. If the text just won't fit into the space,
  167. it'll cram as much as possible in there, and put some ellipsis at the end to show that
  168. it's been truncated.
  169. A Justification parameter lets you specify how the text is laid out within the rectangle,
  170. both horizontally and vertically.
  171. The minimumHorizontalScale parameter specifies how much the text can be squashed horizontally
  172. to try to squeeze it into the space. If you don't want any horizontal scaling to occur, you
  173. can set this value to 1.0f. Pass 0 if you want it to use a default value.
  174. @see GlyphArrangement::addFittedText
  175. */
  176. void drawFittedText (const String& text,
  177. Rectangle<int> area,
  178. Justification justificationFlags,
  179. int maximumNumberOfLines,
  180. float minimumHorizontalScale = 0.0f) const;
  181. //==============================================================================
  182. /** Fills the context's entire clip region with the current colour or brush.
  183. (See also the fillAll (Colour) method which is a quick way of filling
  184. it with a given colour).
  185. */
  186. void fillAll() const;
  187. /** Fills the context's entire clip region with a given colour.
  188. This leaves the context's current colour and brush unchanged, it just
  189. uses the specified colour temporarily.
  190. */
  191. void fillAll (Colour colourToUse) const;
  192. //==============================================================================
  193. /** Fills a rectangle with the current colour or brush.
  194. @see drawRect, fillRoundedRectangle
  195. */
  196. void fillRect (Rectangle<int> rectangle) const;
  197. /** Fills a rectangle with the current colour or brush.
  198. @see drawRect, fillRoundedRectangle
  199. */
  200. void fillRect (Rectangle<float> rectangle) const;
  201. /** Fills a rectangle with the current colour or brush.
  202. @see drawRect, fillRoundedRectangle
  203. */
  204. void fillRect (int x, int y, int width, int height) const;
  205. /** Fills a rectangle with the current colour or brush.
  206. @see drawRect, fillRoundedRectangle
  207. */
  208. void fillRect (float x, float y, float width, float height) const;
  209. /** Fills a set of rectangles using the current colour or brush.
  210. If you have a lot of rectangles to draw, it may be more efficient
  211. to create a RectangleList and use this method than to call fillRect()
  212. multiple times.
  213. */
  214. void fillRectList (const RectangleList<float>& rectangles) const;
  215. /** Fills a set of rectangles using the current colour or brush.
  216. If you have a lot of rectangles to draw, it may be more efficient
  217. to create a RectangleList and use this method than to call fillRect()
  218. multiple times.
  219. */
  220. void fillRectList (const RectangleList<int>& rectangles) const;
  221. /** Uses the current colour or brush to fill a rectangle with rounded corners.
  222. @see drawRoundedRectangle, Path::addRoundedRectangle
  223. */
  224. void fillRoundedRectangle (float x, float y, float width, float height,
  225. float cornerSize) const;
  226. /** Uses the current colour or brush to fill a rectangle with rounded corners.
  227. @see drawRoundedRectangle, Path::addRoundedRectangle
  228. */
  229. void fillRoundedRectangle (Rectangle<float> rectangle,
  230. float cornerSize) const;
  231. /** Fills a rectangle with a checkerboard pattern, alternating between two colours. */
  232. void fillCheckerBoard (Rectangle<int> area,
  233. int checkWidth, int checkHeight,
  234. Colour colour1, Colour colour2) const;
  235. /** Draws a rectangular outline, using the current colour or brush.
  236. The lines are drawn inside the given rectangle, and greater line thicknesses extend inwards.
  237. @see fillRect
  238. */
  239. void drawRect (int x, int y, int width, int height, int lineThickness = 1) const;
  240. /** Draws a rectangular outline, using the current colour or brush.
  241. The lines are drawn inside the given rectangle, and greater line thicknesses extend inwards.
  242. @see fillRect
  243. */
  244. void drawRect (float x, float y, float width, float height, float lineThickness = 1.0f) const;
  245. /** Draws a rectangular outline, using the current colour or brush.
  246. The lines are drawn inside the given rectangle, and greater line thicknesses extend inwards.
  247. @see fillRect
  248. */
  249. void drawRect (Rectangle<int> rectangle, int lineThickness = 1) const;
  250. /** Draws a rectangular outline, using the current colour or brush.
  251. The lines are drawn inside the given rectangle, and greater line thicknesses extend inwards.
  252. @see fillRect
  253. */
  254. void drawRect (Rectangle<float> rectangle, float lineThickness = 1.0f) const;
  255. /** Uses the current colour or brush to draw the outline of a rectangle with rounded corners.
  256. @see fillRoundedRectangle, Path::addRoundedRectangle
  257. */
  258. void drawRoundedRectangle (float x, float y, float width, float height,
  259. float cornerSize, float lineThickness) const;
  260. /** Uses the current colour or brush to draw the outline of a rectangle with rounded corners.
  261. @see fillRoundedRectangle, Path::addRoundedRectangle
  262. */
  263. void drawRoundedRectangle (Rectangle<float> rectangle,
  264. float cornerSize, float lineThickness) const;
  265. //==============================================================================
  266. /** Fills an ellipse with the current colour or brush.
  267. The ellipse is drawn to fit inside the given rectangle.
  268. @see drawEllipse, Path::addEllipse
  269. */
  270. void fillEllipse (float x, float y, float width, float height) const;
  271. /** Fills an ellipse with the current colour or brush.
  272. The ellipse is drawn to fit inside the given rectangle.
  273. @see drawEllipse, Path::addEllipse
  274. */
  275. void fillEllipse (Rectangle<float> area) const;
  276. /** Draws an elliptical stroke using the current colour or brush.
  277. @see fillEllipse, Path::addEllipse
  278. */
  279. void drawEllipse (float x, float y, float width, float height,
  280. float lineThickness) const;
  281. /** Draws an elliptical stroke using the current colour or brush.
  282. @see fillEllipse, Path::addEllipse
  283. */
  284. void drawEllipse (Rectangle<float> area, float lineThickness) const;
  285. //==============================================================================
  286. /** Draws a line between two points.
  287. The line is 1 pixel wide and drawn with the current colour or brush.
  288. TIP: If you're trying to draw horizontal or vertical lines, don't use this -
  289. it's better to use fillRect() instead unless you really need an angled line.
  290. */
  291. void drawLine (float startX, float startY, float endX, float endY) const;
  292. /** Draws a line between two points with a given thickness.
  293. TIP: If you're trying to draw horizontal or vertical lines, don't use this -
  294. it's better to use fillRect() instead unless you really need an angled line.
  295. @see Path::addLineSegment
  296. */
  297. void drawLine (float startX, float startY, float endX, float endY, float lineThickness) const;
  298. /** Draws a line between two points.
  299. The line is 1 pixel wide and drawn with the current colour or brush.
  300. TIP: If you're trying to draw horizontal or vertical lines, don't use this -
  301. it's better to use fillRect() instead unless you really need an angled line.
  302. */
  303. void drawLine (Line<float> line) const;
  304. /** Draws a line between two points with a given thickness.
  305. @see Path::addLineSegment
  306. TIP: If you're trying to draw horizontal or vertical lines, don't use this -
  307. it's better to use fillRect() instead unless you really need an angled line.
  308. */
  309. void drawLine (Line<float> line, float lineThickness) const;
  310. /** Draws a dashed line using a custom set of dash-lengths.
  311. @param line the line to draw
  312. @param dashLengths a series of lengths to specify the on/off lengths - e.g.
  313. { 4, 5, 6, 7 } will draw a line of 4 pixels, skip 5 pixels,
  314. draw 6 pixels, skip 7 pixels, and then repeat.
  315. @param numDashLengths the number of elements in the array (this must be an even number).
  316. @param lineThickness the thickness of the line to draw
  317. @param dashIndexToStartFrom the index in the dash-length array to use for the first segment
  318. @see PathStrokeType::createDashedStroke
  319. */
  320. void drawDashedLine (Line<float> line,
  321. const float* dashLengths, int numDashLengths,
  322. float lineThickness = 1.0f,
  323. int dashIndexToStartFrom = 0) const;
  324. /** Draws a vertical line of pixels at a given x position.
  325. The x position is an integer, but the top and bottom of the line can be sub-pixel
  326. positions, and these will be anti-aliased if necessary.
  327. The bottom parameter must be greater than or equal to the top parameter.
  328. */
  329. void drawVerticalLine (int x, float top, float bottom) const;
  330. /** Draws a horizontal line of pixels at a given y position.
  331. The y position is an integer, but the left and right ends of the line can be sub-pixel
  332. positions, and these will be anti-aliased if necessary.
  333. The right parameter must be greater than or equal to the left parameter.
  334. */
  335. void drawHorizontalLine (int y, float left, float right) const;
  336. //==============================================================================
  337. /** Fills a path using the currently selected colour or brush. */
  338. void fillPath (const Path& path) const;
  339. /** Fills a path using the currently selected colour or brush, and adds a transform. */
  340. void fillPath (const Path& path, const AffineTransform& transform) const;
  341. /** Draws a path's outline using the currently selected colour or brush. */
  342. void strokePath (const Path& path,
  343. const PathStrokeType& strokeType,
  344. const AffineTransform& transform = {}) const;
  345. /** Draws a line with an arrowhead at its end.
  346. @param line the line to draw
  347. @param lineThickness the thickness of the line
  348. @param arrowheadWidth the width of the arrow head (perpendicular to the line)
  349. @param arrowheadLength the length of the arrow head (along the length of the line)
  350. */
  351. void drawArrow (Line<float> line,
  352. float lineThickness,
  353. float arrowheadWidth,
  354. float arrowheadLength) const;
  355. //==============================================================================
  356. /** Types of rendering quality that can be specified when drawing images.
  357. @see Graphics::setImageResamplingQuality
  358. */
  359. enum ResamplingQuality
  360. {
  361. lowResamplingQuality = 0, /**< Just uses a nearest-neighbour algorithm for resampling. */
  362. mediumResamplingQuality = 1, /**< Uses bilinear interpolation for upsampling and area-averaging for downsampling. */
  363. highResamplingQuality = 2, /**< Uses bicubic interpolation for upsampling and area-averaging for downsampling. */
  364. };
  365. /** Changes the quality that will be used when resampling images.
  366. By default a Graphics object will be set to mediumRenderingQuality.
  367. @see Graphics::drawImage, Graphics::drawImageTransformed, Graphics::drawImageWithin
  368. */
  369. void setImageResamplingQuality (const ResamplingQuality newQuality);
  370. /** Draws an image.
  371. This will draw the whole of an image, positioning its top-left corner at the
  372. given coordinates, and keeping its size the same. This is the simplest image
  373. drawing method - the others give more control over the scaling and clipping
  374. of the images.
  375. Images are composited using the context's current opacity, so if you
  376. don't want it to be drawn semi-transparently, be sure to call setOpacity (1.0f)
  377. (or setColour() with an opaque colour) before drawing images.
  378. */
  379. void drawImageAt (const Image& imageToDraw, int topLeftX, int topLeftY,
  380. bool fillAlphaChannelWithCurrentBrush = false) const;
  381. /** Draws part of an image, rescaling it to fit in a given target region.
  382. The specified area of the source image is rescaled and drawn to fill the
  383. specifed destination rectangle.
  384. Images are composited using the context's current opacity, so if you
  385. don't want it to be drawn semi-transparently, be sure to call setOpacity (1.0f)
  386. (or setColour() with an opaque colour) before drawing images.
  387. @param imageToDraw the image to overlay
  388. @param destX the left of the destination rectangle
  389. @param destY the top of the destination rectangle
  390. @param destWidth the width of the destination rectangle
  391. @param destHeight the height of the destination rectangle
  392. @param sourceX the left of the rectangle to copy from the source image
  393. @param sourceY the top of the rectangle to copy from the source image
  394. @param sourceWidth the width of the rectangle to copy from the source image
  395. @param sourceHeight the height of the rectangle to copy from the source image
  396. @param fillAlphaChannelWithCurrentBrush if true, then instead of drawing the source image's pixels,
  397. the source image's alpha channel is used as a mask with
  398. which to fill the destination using the current colour
  399. or brush. (If the source is has no alpha channel, then
  400. it will just fill the target with a solid rectangle)
  401. @see setImageResamplingQuality, drawImageAt, drawImageWithin, fillAlphaMap
  402. */
  403. void drawImage (const Image& imageToDraw,
  404. int destX, int destY, int destWidth, int destHeight,
  405. int sourceX, int sourceY, int sourceWidth, int sourceHeight,
  406. bool fillAlphaChannelWithCurrentBrush = false) const;
  407. /** Draws an image, having applied an affine transform to it.
  408. This lets you throw the image around in some wacky ways, rotate it, shear,
  409. scale it, etc.
  410. Images are composited using the context's current opacity, so if you
  411. don't want it to be drawn semi-transparently, be sure to call setOpacity (1.0f)
  412. (or setColour() with an opaque colour) before drawing images.
  413. If fillAlphaChannelWithCurrentBrush is set to true, then the image's RGB channels
  414. are ignored and it is filled with the current brush, masked by its alpha channel.
  415. If you want to render only a subsection of an image, use Image::getClippedImage() to
  416. create the section that you need.
  417. @see setImageResamplingQuality, drawImage
  418. */
  419. void drawImageTransformed (const Image& imageToDraw,
  420. const AffineTransform& transform,
  421. bool fillAlphaChannelWithCurrentBrush = false) const;
  422. /** Draws an image to fit within a designated rectangle.
  423. @param imageToDraw the source image to draw
  424. @param targetArea the target rectangle to fit it into
  425. @param placementWithinTarget this specifies how the image should be positioned
  426. within the target rectangle - see the RectanglePlacement
  427. class for more details about this.
  428. @param fillAlphaChannelWithCurrentBrush if true, then instead of drawing the image, just its
  429. alpha channel will be used as a mask with which to
  430. draw with the current brush or colour. This is
  431. similar to fillAlphaMap(), and see also drawImage()
  432. @see drawImage, drawImageTransformed, drawImageAt, RectanglePlacement
  433. */
  434. void drawImage (const Image& imageToDraw, Rectangle<float> targetArea,
  435. RectanglePlacement placementWithinTarget = RectanglePlacement::stretchToFit,
  436. bool fillAlphaChannelWithCurrentBrush = false) const;
  437. /** Draws an image to fit within a designated rectangle.
  438. If the image is too big or too small for the space, it will be rescaled
  439. to fit as nicely as it can do without affecting its aspect ratio. It will
  440. then be placed within the target rectangle according to the justification flags
  441. specified.
  442. @param imageToDraw the source image to draw
  443. @param destX top-left of the target rectangle to fit it into
  444. @param destY top-left of the target rectangle to fit it into
  445. @param destWidth size of the target rectangle to fit the image into
  446. @param destHeight size of the target rectangle to fit the image into
  447. @param placementWithinTarget this specifies how the image should be positioned
  448. within the target rectangle - see the RectanglePlacement
  449. class for more details about this.
  450. @param fillAlphaChannelWithCurrentBrush if true, then instead of drawing the image, just its
  451. alpha channel will be used as a mask with which to
  452. draw with the current brush or colour. This is
  453. similar to fillAlphaMap(), and see also drawImage()
  454. @see setImageResamplingQuality, drawImage, drawImageTransformed, drawImageAt, RectanglePlacement
  455. */
  456. void drawImageWithin (const Image& imageToDraw,
  457. int destX, int destY, int destWidth, int destHeight,
  458. RectanglePlacement placementWithinTarget,
  459. bool fillAlphaChannelWithCurrentBrush = false) const;
  460. //==============================================================================
  461. /** Returns the position of the bounding box for the current clipping region.
  462. @see getClipRegion, clipRegionIntersects
  463. */
  464. Rectangle<int> getClipBounds() const;
  465. /** Checks whether a rectangle overlaps the context's clipping region.
  466. If this returns false, no part of the given area can be drawn onto, so this
  467. method can be used to optimise a component's paint() method, by letting it
  468. avoid drawing complex objects that aren't within the region being repainted.
  469. */
  470. bool clipRegionIntersects (Rectangle<int> area) const;
  471. /** Intersects the current clipping region with another region.
  472. @returns true if the resulting clipping region is non-zero in size
  473. @see setOrigin, clipRegionIntersects
  474. */
  475. bool reduceClipRegion (int x, int y, int width, int height);
  476. /** Intersects the current clipping region with another region.
  477. @returns true if the resulting clipping region is non-zero in size
  478. @see setOrigin, clipRegionIntersects
  479. */
  480. bool reduceClipRegion (Rectangle<int> area);
  481. /** Intersects the current clipping region with a rectangle list region.
  482. @returns true if the resulting clipping region is non-zero in size
  483. @see setOrigin, clipRegionIntersects
  484. */
  485. bool reduceClipRegion (const RectangleList<int>& clipRegion);
  486. /** Intersects the current clipping region with a path.
  487. @returns true if the resulting clipping region is non-zero in size
  488. @see reduceClipRegion
  489. */
  490. bool reduceClipRegion (const Path& path, const AffineTransform& transform = AffineTransform());
  491. /** Intersects the current clipping region with an image's alpha-channel.
  492. The current clipping path is intersected with the area covered by this image's
  493. alpha-channel, after the image has been transformed by the specified matrix.
  494. @param image the image whose alpha-channel should be used. If the image doesn't
  495. have an alpha-channel, it is treated as entirely opaque.
  496. @param transform a matrix to apply to the image
  497. @returns true if the resulting clipping region is non-zero in size
  498. @see reduceClipRegion
  499. */
  500. bool reduceClipRegion (const Image& image, const AffineTransform& transform);
  501. /** Excludes a rectangle to stop it being drawn into. */
  502. void excludeClipRegion (Rectangle<int> rectangleToExclude);
  503. /** Returns true if no drawing can be done because the clip region is zero. */
  504. bool isClipEmpty() const;
  505. //==============================================================================
  506. /** Saves the current graphics state on an internal stack.
  507. To restore the state, use restoreState().
  508. @see ScopedSaveState
  509. */
  510. void saveState();
  511. /** Restores a graphics state that was previously saved with saveState().
  512. @see ScopedSaveState
  513. */
  514. void restoreState();
  515. /** Uses RAII to save and restore the state of a graphics context.
  516. On construction, this calls Graphics::saveState(), and on destruction it calls
  517. Graphics::restoreState() on the Graphics object that you supply.
  518. */
  519. class ScopedSaveState
  520. {
  521. public:
  522. ScopedSaveState (Graphics&);
  523. ~ScopedSaveState();
  524. private:
  525. Graphics& context;
  526. JUCE_DECLARE_NON_COPYABLE (ScopedSaveState)
  527. };
  528. //==============================================================================
  529. /** Begins rendering to an off-screen bitmap which will later be flattened onto the current
  530. context with the given opacity.
  531. The context uses an internal stack of temporary image layers to do this. When you've
  532. finished drawing to the layer, call endTransparencyLayer() to complete the operation and
  533. composite the finished layer. Every call to beginTransparencyLayer() MUST be matched
  534. by a corresponding call to endTransparencyLayer()!
  535. This call also saves the current state, and endTransparencyLayer() restores it.
  536. */
  537. void beginTransparencyLayer (float layerOpacity);
  538. /** Completes a drawing operation to a temporary semi-transparent buffer.
  539. See beginTransparencyLayer() for more details.
  540. */
  541. void endTransparencyLayer();
  542. /** Moves the position of the context's origin.
  543. This changes the position that the context considers to be (0, 0) to
  544. the specified position.
  545. So if you call setOrigin with (100, 100), then the position that was previously
  546. referred to as (100, 100) will subsequently be considered to be (0, 0).
  547. @see reduceClipRegion, addTransform
  548. */
  549. void setOrigin (Point<int> newOrigin);
  550. /** Moves the position of the context's origin.
  551. This changes the position that the context considers to be (0, 0) to
  552. the specified position.
  553. So if you call setOrigin (100, 100), then the position that was previously
  554. referred to as (100, 100) will subsequently be considered to be (0, 0).
  555. @see reduceClipRegion, addTransform
  556. */
  557. void setOrigin (int newOriginX, int newOriginY);
  558. /** Adds a transformation which will be performed on all the graphics operations that
  559. the context subsequently performs.
  560. After calling this, all the coordinates that are passed into the context will be
  561. transformed by this matrix.
  562. @see setOrigin
  563. */
  564. void addTransform (const AffineTransform& transform);
  565. /** Resets the current colour, brush, and font to default settings. */
  566. void resetToDefaultState();
  567. /** Returns true if this context is drawing to a vector-based device, such as a printer. */
  568. bool isVectorDevice() const;
  569. //==============================================================================
  570. /** Create a graphics that draws with a given low-level renderer.
  571. This method is intended for use only by people who know what they're doing.
  572. Note that the LowLevelGraphicsContext will NOT be deleted by this object.
  573. */
  574. Graphics (LowLevelGraphicsContext&) noexcept;
  575. /** @internal */
  576. LowLevelGraphicsContext& getInternalContext() const noexcept { return context; }
  577. private:
  578. //==============================================================================
  579. LowLevelGraphicsContext& context;
  580. ScopedPointer<LowLevelGraphicsContext> contextToDelete;
  581. bool saveStatePending = false;
  582. void saveStateIfPending();
  583. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Graphics)
  584. };
  585. } // namespace juce