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.

juce_GraphicsContext.h 34KB

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