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

9 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_GRAPHICSCONTEXT_H_INCLUDED
  18. #define JUCE_GRAPHICSCONTEXT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A graphics context, used for drawing a component or image.
  22. When a Component needs painting, a Graphics context is passed to its
  23. Component::paint() method, and this you then call methods within this
  24. object to actually draw the component's content.
  25. A Graphics can also be created from an image, to allow drawing directly onto
  26. that image.
  27. @see Component::paint
  28. */
  29. class JUCE_API Graphics
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a Graphics object to draw directly onto the given image.
  34. The graphics object that is created will be set up to draw onto the image,
  35. with the context's clipping area being the entire size of the image, and its
  36. origin being the image's origin. To draw into a subsection of an image, use the
  37. reduceClipRegion() and setOrigin() methods.
  38. Obviously you shouldn't delete the image before this context is deleted.
  39. */
  40. explicit Graphics (const Image& imageToDrawOnto);
  41. /** Destructor. */
  42. ~Graphics();
  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. */
  61. void setGradientFill (const ColourGradient& gradient);
  62. /** Sets the context to use a tiled image pattern for filling.
  63. Make sure that you don't delete this image while it's still being used by
  64. this context!
  65. */
  66. void setTiledImageFill (const Image& imageToUse,
  67. int anchorX, int anchorY,
  68. float opacity);
  69. /** Changes the current fill settings.
  70. @see setColour, setGradientFill, setTiledImageFill
  71. */
  72. void setFillType (const FillType& newFill);
  73. //==============================================================================
  74. /** Changes the font to use for subsequent text-drawing functions.
  75. Note there's also a setFont (float, int) method to quickly change the size and
  76. style of the current font.
  77. @see drawSingleLineText, drawMultiLineText, drawText, drawFittedText
  78. */
  79. void setFont (const Font& newFont);
  80. /** Changes the size of the currently-selected font.
  81. This is a convenient shortcut that changes the context's current font to a
  82. different size. The typeface won't be changed.
  83. @see Font
  84. */
  85. void setFont (float newFontHeight);
  86. /** Returns the currently selected font. */
  87. Font getCurrentFont() const;
  88. /** Draws a one-line text string.
  89. This will use the current colour (or brush) to fill the text. The font is the last
  90. one specified by setFont().
  91. @param text the string to draw
  92. @param startX the position to draw the left-hand edge of the text
  93. @param baselineY the position of the text's baseline
  94. @param justification the horizontal flags indicate which end of the text string is
  95. anchored at the specified point.
  96. @see drawMultiLineText, drawText, drawFittedText, GlyphArrangement::addLineOfText
  97. */
  98. void drawSingleLineText (const String& text,
  99. int startX, int baselineY,
  100. Justification justification = Justification::left) const;
  101. /** Draws text across multiple lines.
  102. This will break the text onto a new line where there's a new-line or
  103. carriage-return character, or at a word-boundary when the text becomes wider
  104. than the size specified by the maximumLineWidth parameter.
  105. @see setFont, drawSingleLineText, drawFittedText, GlyphArrangement::addJustifiedText
  106. */
  107. void drawMultiLineText (const String& text,
  108. int startX, int baselineY,
  109. int maximumLineWidth) const;
  110. /** Draws a line of text within a specified rectangle.
  111. The text will be positioned within the rectangle based on the justification
  112. flags passed-in. If the string is too long to fit inside the rectangle, it will
  113. either be truncated or will have ellipsis added to its end (if the useEllipsesIfTooBig
  114. flag is true).
  115. @see drawSingleLineText, drawFittedText, drawMultiLineText, GlyphArrangement::addJustifiedText
  116. */
  117. void drawText (const String& text,
  118. int x, int y, int width, int height,
  119. Justification justificationType,
  120. bool useEllipsesIfTooBig = true) const;
  121. /** Draws a line of text within a specified rectangle.
  122. The text will be positioned within the rectangle based on the justification
  123. flags passed-in. If the string is too long to fit inside the rectangle, it will
  124. either be truncated or will have ellipsis added to its end (if the useEllipsesIfTooBig
  125. flag is true).
  126. @see drawSingleLineText, drawFittedText, drawMultiLineText, GlyphArrangement::addJustifiedText
  127. */
  128. void drawText (const String& text,
  129. const Rectangle<int>& area,
  130. Justification justificationType,
  131. bool useEllipsesIfTooBig = true) const;
  132. /** Draws a line of text within a specified rectangle.
  133. The text will be positioned within the rectangle based on the justification
  134. flags passed-in. If the string is too long to fit inside the rectangle, it will
  135. either be truncated or will have ellipsis added to its end (if the useEllipsesIfTooBig
  136. flag is true).
  137. @see drawSingleLineText, drawFittedText, drawMultiLineText, GlyphArrangement::addJustifiedText
  138. */
  139. void drawText (const String& text,
  140. const Rectangle<float>& area,
  141. Justification justificationType,
  142. bool useEllipsesIfTooBig = true) const;
  143. /** Tries to draw a text string inside a given space.
  144. This does its best to make the given text readable within the specified rectangle,
  145. so it useful for labelling things.
  146. If the text is too big, it'll be squashed horizontally or broken over multiple lines
  147. if the maximumLinesToUse value allows this. If the text just won't fit into the space,
  148. it'll cram as much as possible in there, and put some ellipsis at the end to show that
  149. it's been truncated.
  150. A Justification parameter lets you specify how the text is laid out within the rectangle,
  151. both horizontally and vertically.
  152. The minimumHorizontalScale parameter specifies how much the text can be squashed horizontally
  153. to try to squeeze it into the space. If you don't want any horizontal scaling to occur, you
  154. can set this value to 1.0f. Pass 0 if you want it to use a default value.
  155. @see GlyphArrangement::addFittedText
  156. */
  157. void drawFittedText (const String& text,
  158. int x, int y, int width, int height,
  159. Justification justificationFlags,
  160. int maximumNumberOfLines,
  161. float minimumHorizontalScale = 0.0f) const;
  162. /** Tries to draw a text string inside a given space.
  163. This does its best to make the given text readable within the specified rectangle,
  164. so it useful for labelling things.
  165. If the text is too big, it'll be squashed horizontally or broken over multiple lines
  166. if the maximumLinesToUse value allows this. If the text just won't fit into the space,
  167. it'll cram as much as possible in there, and put some ellipsis at the end to show that
  168. it's been truncated.
  169. A Justification parameter lets you specify how the text is laid out within the rectangle,
  170. both horizontally and vertically.
  171. The minimumHorizontalScale parameter specifies how much the text can be squashed horizontally
  172. to try to squeeze it into the space. If you don't want any horizontal scaling to occur, you
  173. can set this value to 1.0f. Pass 0 if you want it to use a default value.
  174. @see GlyphArrangement::addFittedText
  175. */
  176. void drawFittedText (const String& text,
  177. const Rectangle<int>& area,
  178. Justification justificationFlags,
  179. int maximumNumberOfLines,
  180. float minimumHorizontalScale = 0.0f) const;
  181. //==============================================================================
  182. /** Fills the context's entire clip region with the current colour or brush.
  183. (See also the fillAll (Colour) method which is a quick way of filling
  184. it with a given colour).
  185. */
  186. void fillAll() const;
  187. /** Fills the context's entire clip region with a given colour.
  188. This leaves the context's current colour and brush unchanged, it just
  189. uses the specified colour temporarily.
  190. */
  191. void fillAll (Colour colourToUse) const;
  192. //==============================================================================
  193. /** Fills a rectangle with the current colour or brush.
  194. @see drawRect, fillRoundedRectangle
  195. */
  196. void fillRect (const Rectangle<int>& rectangle) const;
  197. /** Fills a rectangle with the current colour or brush.
  198. @see drawRect, fillRoundedRectangle
  199. */
  200. void fillRect (const Rectangle<float>& rectangle) const;
  201. /** Fills a rectangle with the current colour or brush.
  202. @see drawRect, fillRoundedRectangle
  203. */
  204. void fillRect (int x, int y, int width, int height) const;
  205. /** Fills a rectangle with the current colour or brush.
  206. @see drawRect, fillRoundedRectangle
  207. */
  208. void fillRect (float x, float y, float width, float height) const;
  209. /** Fills a set of rectangles using the current colour or brush.
  210. If you have a lot of rectangles to draw, it may be more efficient
  211. to create a RectangleList and use this method than to call fillRect()
  212. multiple times.
  213. */
  214. void fillRectList (const RectangleList<float>& rectangles) const;
  215. /** Fills a set of rectangles using the current colour or brush.
  216. If you have a lot of rectangles to draw, it may be more efficient
  217. to create a RectangleList and use this method than to call fillRect()
  218. multiple times.
  219. */
  220. void fillRectList (const RectangleList<int>& rectangles) const;
  221. /** Uses the current colour or brush to fill a rectangle with rounded corners.
  222. @see drawRoundedRectangle, Path::addRoundedRectangle
  223. */
  224. void fillRoundedRectangle (float x, float y, float width, float height,
  225. float cornerSize) const;
  226. /** Uses the current colour or brush to fill a rectangle with rounded corners.
  227. @see drawRoundedRectangle, Path::addRoundedRectangle
  228. */
  229. void fillRoundedRectangle (const Rectangle<float>& rectangle,
  230. float cornerSize) const;
  231. /** Fills a rectangle with a checkerboard pattern, alternating between two colours. */
  232. void fillCheckerBoard (const Rectangle<int>& area,
  233. int checkWidth, int checkHeight,
  234. Colour colour1, Colour colour2) const;
  235. /** Draws a rectangular outline, using the current colour or brush.
  236. The lines are drawn inside the given rectangle, and greater line thicknesses extend inwards.
  237. @see fillRect
  238. */
  239. void drawRect (int x, int y, int width, int height, int lineThickness = 1) const;
  240. /** Draws a rectangular outline, using the current colour or brush.
  241. The lines are drawn inside the given rectangle, and greater line thicknesses extend inwards.
  242. @see fillRect
  243. */
  244. void drawRect (float x, float y, float width, float height, float lineThickness = 1.0f) const;
  245. /** Draws a rectangular outline, using the current colour or brush.
  246. The lines are drawn inside the given rectangle, and greater line thicknesses extend inwards.
  247. @see fillRect
  248. */
  249. void drawRect (const Rectangle<int>& rectangle, int lineThickness = 1) const;
  250. /** Draws a rectangular outline, using the current colour or brush.
  251. The lines are drawn inside the given rectangle, and greater line thicknesses extend inwards.
  252. @see fillRect
  253. */
  254. void drawRect (Rectangle<float> rectangle, float lineThickness = 1.0f) const;
  255. /** Uses the current colour or brush to draw the outline of a rectangle with rounded corners.
  256. @see fillRoundedRectangle, Path::addRoundedRectangle
  257. */
  258. void drawRoundedRectangle (float x, float y, float width, float height,
  259. float cornerSize, float lineThickness) const;
  260. /** Uses the current colour or brush to draw the outline of a rectangle with rounded corners.
  261. @see fillRoundedRectangle, Path::addRoundedRectangle
  262. */
  263. void drawRoundedRectangle (const Rectangle<float>& rectangle,
  264. float cornerSize, float lineThickness) const;
  265. /** Fills a 1x1 pixel using the current colour or brush.
  266. Note that because the context may be transformed, this is effectively the same as
  267. calling fillRect (x, y, 1, 1), and the actual result may involve multiple pixels.
  268. */
  269. void setPixel (int x, int y) const;
  270. //==============================================================================
  271. /** Fills an ellipse with the current colour or brush.
  272. The ellipse is drawn to fit inside the given rectangle.
  273. @see drawEllipse, Path::addEllipse
  274. */
  275. void fillEllipse (float x, float y, float width, float height) const;
  276. /** Fills an ellipse with the current colour or brush.
  277. The ellipse is drawn to fit inside the given rectangle.
  278. @see drawEllipse, Path::addEllipse
  279. */
  280. void fillEllipse (const Rectangle<float>& area) const;
  281. /** Draws an elliptical stroke using the current colour or brush.
  282. @see fillEllipse, Path::addEllipse
  283. */
  284. void drawEllipse (float x, float y, float width, float height,
  285. float lineThickness) const;
  286. /** Draws an elliptical stroke using the current colour or brush.
  287. @see fillEllipse, Path::addEllipse
  288. */
  289. void drawEllipse (const Rectangle<float>& area, float lineThickness) const;
  290. //==============================================================================
  291. /** Draws a line between two points.
  292. The line is 1 pixel wide and drawn with the current colour or brush.
  293. TIP: If you're trying to draw horizontal or vertical lines, don't use this -
  294. it's better to use fillRect() instead unless you really need an angled line.
  295. */
  296. void drawLine (float startX, float startY, float endX, float endY) const;
  297. /** Draws a line between two points with a given thickness.
  298. TIP: If you're trying to draw horizontal or vertical lines, don't use this -
  299. it's better to use fillRect() instead unless you really need an angled line.
  300. @see Path::addLineSegment
  301. */
  302. void drawLine (float startX, float startY, float endX, float endY, float lineThickness) const;
  303. /** Draws a line between two points.
  304. The line is 1 pixel wide and drawn with the current colour or brush.
  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 (const Line<float>& line) const;
  309. /** Draws a line between two points with a given thickness.
  310. @see Path::addLineSegment
  311. TIP: If you're trying to draw horizontal or vertical lines, don't use this -
  312. it's better to use fillRect() instead unless you really need an angled line.
  313. */
  314. void drawLine (const Line<float>& line, float lineThickness) const;
  315. /** Draws a dashed line using a custom set of dash-lengths.
  316. @param line the line to draw
  317. @param dashLengths a series of lengths to specify the on/off lengths - e.g.
  318. { 4, 5, 6, 7 } will draw a line of 4 pixels, skip 5 pixels,
  319. draw 6 pixels, skip 7 pixels, and then repeat.
  320. @param numDashLengths the number of elements in the array (this must be an even number).
  321. @param lineThickness the thickness of the line to draw
  322. @param dashIndexToStartFrom the index in the dash-length array to use for the first segment
  323. @see PathStrokeType::createDashedStroke
  324. */
  325. void drawDashedLine (const Line<float>& line,
  326. const float* dashLengths, int numDashLengths,
  327. float lineThickness = 1.0f,
  328. int dashIndexToStartFrom = 0) const;
  329. /** Draws a vertical line of pixels at a given x position.
  330. The x position is an integer, but the top and bottom of the line can be sub-pixel
  331. positions, and these will be anti-aliased if necessary.
  332. The bottom parameter must be greater than or equal to the top parameter.
  333. */
  334. void drawVerticalLine (int x, float top, float bottom) const;
  335. /** Draws a horizontal line of pixels at a given y position.
  336. The y position is an integer, but the left and right ends of the line can be sub-pixel
  337. positions, and these will be anti-aliased if necessary.
  338. The right parameter must be greater than or equal to the left parameter.
  339. */
  340. void drawHorizontalLine (int y, float left, float right) const;
  341. //==============================================================================
  342. /** Fills a path using the currently selected colour or brush. */
  343. void fillPath (const Path& path,
  344. const AffineTransform& transform = AffineTransform::identity) 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 = AffineTransform::identity) 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 (const 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 blendImage, 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. specifed 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. If the image is too big or too small for the space, it will be rescaled
  428. to fit as nicely as it can do without affecting its aspect ratio. It will
  429. then be placed within the target rectangle according to the justification flags
  430. specified.
  431. @param imageToDraw the source image to draw
  432. @param destX top-left of the target rectangle to fit it into
  433. @param destY top-left of the target rectangle to fit it into
  434. @param destWidth size of the target rectangle to fit the image into
  435. @param destHeight size of the target rectangle to fit the image into
  436. @param placementWithinTarget this specifies how the image should be positioned
  437. within the target rectangle - see the RectanglePlacement
  438. class for more details about this.
  439. @param fillAlphaChannelWithCurrentBrush if true, then instead of drawing the image, just its
  440. alpha channel will be used as a mask with which to
  441. draw with the current brush or colour. This is
  442. similar to fillAlphaMap(), and see also drawImage()
  443. @see setImageResamplingQuality, drawImage, drawImageTransformed, drawImageAt, RectanglePlacement
  444. */
  445. void drawImageWithin (const Image& imageToDraw,
  446. int destX, int destY, int destWidth, int destHeight,
  447. RectanglePlacement placementWithinTarget,
  448. bool fillAlphaChannelWithCurrentBrush = false) const;
  449. //==============================================================================
  450. /** Returns the position of the bounding box for the current clipping region.
  451. @see getClipRegion, clipRegionIntersects
  452. */
  453. Rectangle<int> getClipBounds() const;
  454. /** Checks whether a rectangle overlaps the context's clipping region.
  455. If this returns false, no part of the given area can be drawn onto, so this
  456. method can be used to optimise a component's paint() method, by letting it
  457. avoid drawing complex objects that aren't within the region being repainted.
  458. */
  459. bool clipRegionIntersects (const Rectangle<int>& area) const;
  460. /** Intersects the current clipping region with another region.
  461. @returns true if the resulting clipping region is non-zero in size
  462. @see setOrigin, clipRegionIntersects
  463. */
  464. bool reduceClipRegion (int x, int y, int width, int height);
  465. /** Intersects the current clipping region with another region.
  466. @returns true if the resulting clipping region is non-zero in size
  467. @see setOrigin, clipRegionIntersects
  468. */
  469. bool reduceClipRegion (const Rectangle<int>& area);
  470. /** Intersects the current clipping region with a rectangle list region.
  471. @returns true if the resulting clipping region is non-zero in size
  472. @see setOrigin, clipRegionIntersects
  473. */
  474. bool reduceClipRegion (const RectangleList<int>& clipRegion);
  475. /** Intersects the current clipping region with a path.
  476. @returns true if the resulting clipping region is non-zero in size
  477. @see reduceClipRegion
  478. */
  479. bool reduceClipRegion (const Path& path, const AffineTransform& transform = AffineTransform::identity);
  480. /** Intersects the current clipping region with an image's alpha-channel.
  481. The current clipping path is intersected with the area covered by this image's
  482. alpha-channel, after the image has been transformed by the specified matrix.
  483. @param image the image whose alpha-channel should be used. If the image doesn't
  484. have an alpha-channel, it is treated as entirely opaque.
  485. @param transform a matrix to apply to the image
  486. @returns true if the resulting clipping region is non-zero in size
  487. @see reduceClipRegion
  488. */
  489. bool reduceClipRegion (const Image& image, const AffineTransform& transform);
  490. /** Excludes a rectangle to stop it being drawn into. */
  491. void excludeClipRegion (const Rectangle<int>& rectangleToExclude);
  492. /** Returns true if no drawing can be done because the clip region is zero. */
  493. bool isClipEmpty() const;
  494. //==============================================================================
  495. /** Saves the current graphics state on an internal stack.
  496. To restore the state, use restoreState().
  497. @see ScopedSaveState
  498. */
  499. void saveState();
  500. /** Restores a graphics state that was previously saved with saveState().
  501. @see ScopedSaveState
  502. */
  503. void restoreState();
  504. /** Uses RAII to save and restore the state of a graphics context.
  505. On construction, this calls Graphics::saveState(), and on destruction it calls
  506. Graphics::restoreState() on the Graphics object that you supply.
  507. */
  508. class ScopedSaveState
  509. {
  510. public:
  511. ScopedSaveState (Graphics&);
  512. ~ScopedSaveState();
  513. private:
  514. Graphics& context;
  515. JUCE_DECLARE_NON_COPYABLE (ScopedSaveState)
  516. };
  517. //==============================================================================
  518. /** Begins rendering to an off-screen bitmap which will later be flattened onto the current
  519. context with the given opacity.
  520. The context uses an internal stack of temporary image layers to do this. When you've
  521. finished drawing to the layer, call endTransparencyLayer() to complete the operation and
  522. composite the finished layer. Every call to beginTransparencyLayer() MUST be matched
  523. by a corresponding call to endTransparencyLayer()!
  524. This call also saves the current state, and endTransparencyLayer() restores it.
  525. */
  526. void beginTransparencyLayer (float layerOpacity);
  527. /** Completes a drawing operation to a temporary semi-transparent buffer.
  528. See beginTransparencyLayer() for more details.
  529. */
  530. void endTransparencyLayer();
  531. /** Moves the position of the context's origin.
  532. This changes the position that the context considers to be (0, 0) to
  533. the specified position.
  534. So if you call setOrigin with (100, 100), then the position that was previously
  535. referred to as (100, 100) will subsequently be considered to be (0, 0).
  536. @see reduceClipRegion, addTransform
  537. */
  538. void setOrigin (Point<int> newOrigin);
  539. /** Moves the position of the context's origin.
  540. This changes the position that the context considers to be (0, 0) to
  541. the specified position.
  542. So if you call setOrigin (100, 100), then the position that was previously
  543. referred to as (100, 100) will subsequently be considered to be (0, 0).
  544. @see reduceClipRegion, addTransform
  545. */
  546. void setOrigin (int newOriginX, int newOriginY);
  547. /** Adds a transformation which will be performed on all the graphics operations that
  548. the context subsequently performs.
  549. After calling this, all the coordinates that are passed into the context will be
  550. transformed by this matrix.
  551. @see setOrigin
  552. */
  553. void addTransform (const AffineTransform& transform);
  554. /** Resets the current colour, brush, and font to default settings. */
  555. void resetToDefaultState();
  556. /** Returns true if this context is drawing to a vector-based device, such as a printer. */
  557. bool isVectorDevice() const;
  558. //==============================================================================
  559. /** Create a graphics that draws with a given low-level renderer.
  560. This method is intended for use only by people who know what they're doing.
  561. Note that the LowLevelGraphicsContext will NOT be deleted by this object.
  562. */
  563. Graphics (LowLevelGraphicsContext&) noexcept;
  564. /** @internal */
  565. LowLevelGraphicsContext& getInternalContext() const noexcept { return context; }
  566. private:
  567. //==============================================================================
  568. LowLevelGraphicsContext& context;
  569. ScopedPointer<LowLevelGraphicsContext> contextToDelete;
  570. bool saveStatePending;
  571. void saveStateIfPending();
  572. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Graphics)
  573. };
  574. #endif // JUCE_GRAPHICSCONTEXT_H_INCLUDED