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 35KB

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