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

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