The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

710 lines
24KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. namespace
  19. {
  20. template <typename Type>
  21. bool areCoordsSensibleNumbers (Type x, Type y, Type w, Type h)
  22. {
  23. const int maxVal = 0x3fffffff;
  24. return (int) x >= -maxVal && (int) x <= maxVal
  25. && (int) y >= -maxVal && (int) y <= maxVal
  26. && (int) w >= -maxVal && (int) w <= maxVal
  27. && (int) h >= -maxVal && (int) h <= maxVal;
  28. }
  29. }
  30. //==============================================================================
  31. LowLevelGraphicsContext::LowLevelGraphicsContext() {}
  32. LowLevelGraphicsContext::~LowLevelGraphicsContext() {}
  33. //==============================================================================
  34. Graphics::Graphics (const Image& imageToDrawOnto)
  35. : context (*imageToDrawOnto.createLowLevelContext()),
  36. contextToDelete (&context),
  37. saveStatePending (false)
  38. {
  39. }
  40. Graphics::Graphics (LowLevelGraphicsContext* const internalContext) noexcept
  41. : context (*internalContext),
  42. saveStatePending (false)
  43. {
  44. jassert (internalContext != nullptr);
  45. }
  46. Graphics::~Graphics()
  47. {
  48. }
  49. //==============================================================================
  50. void Graphics::resetToDefaultState()
  51. {
  52. saveStateIfPending();
  53. context.setFill (FillType());
  54. context.setFont (Font());
  55. context.setInterpolationQuality (Graphics::mediumResamplingQuality);
  56. }
  57. bool Graphics::isVectorDevice() const
  58. {
  59. return context.isVectorDevice();
  60. }
  61. bool Graphics::reduceClipRegion (const Rectangle<int>& area)
  62. {
  63. saveStateIfPending();
  64. return context.clipToRectangle (area);
  65. }
  66. bool Graphics::reduceClipRegion (const int x, const int y, const int w, const int h)
  67. {
  68. return reduceClipRegion (Rectangle<int> (x, y, w, h));
  69. }
  70. bool Graphics::reduceClipRegion (const RectangleList& clipRegion)
  71. {
  72. saveStateIfPending();
  73. return context.clipToRectangleList (clipRegion);
  74. }
  75. bool Graphics::reduceClipRegion (const Path& path, const AffineTransform& transform)
  76. {
  77. saveStateIfPending();
  78. context.clipToPath (path, transform);
  79. return ! context.isClipEmpty();
  80. }
  81. bool Graphics::reduceClipRegion (const Image& image, const AffineTransform& transform)
  82. {
  83. saveStateIfPending();
  84. context.clipToImageAlpha (image, transform);
  85. return ! context.isClipEmpty();
  86. }
  87. void Graphics::excludeClipRegion (const Rectangle<int>& rectangleToExclude)
  88. {
  89. saveStateIfPending();
  90. context.excludeClipRectangle (rectangleToExclude);
  91. }
  92. bool Graphics::isClipEmpty() const
  93. {
  94. return context.isClipEmpty();
  95. }
  96. Rectangle<int> Graphics::getClipBounds() const
  97. {
  98. return context.getClipBounds();
  99. }
  100. void Graphics::saveState()
  101. {
  102. saveStateIfPending();
  103. saveStatePending = true;
  104. }
  105. void Graphics::restoreState()
  106. {
  107. if (saveStatePending)
  108. saveStatePending = false;
  109. else
  110. context.restoreState();
  111. }
  112. void Graphics::saveStateIfPending()
  113. {
  114. if (saveStatePending)
  115. {
  116. saveStatePending = false;
  117. context.saveState();
  118. }
  119. }
  120. void Graphics::setOrigin (const int newOriginX, const int newOriginY)
  121. {
  122. saveStateIfPending();
  123. context.setOrigin (newOriginX, newOriginY);
  124. }
  125. void Graphics::addTransform (const AffineTransform& transform)
  126. {
  127. saveStateIfPending();
  128. context.addTransform (transform);
  129. }
  130. bool Graphics::clipRegionIntersects (const Rectangle<int>& area) const
  131. {
  132. return context.clipRegionIntersects (area);
  133. }
  134. void Graphics::beginTransparencyLayer (float layerOpacity)
  135. {
  136. saveStateIfPending();
  137. context.beginTransparencyLayer (layerOpacity);
  138. }
  139. void Graphics::endTransparencyLayer()
  140. {
  141. context.endTransparencyLayer();
  142. }
  143. //==============================================================================
  144. void Graphics::setColour (const Colour& newColour)
  145. {
  146. saveStateIfPending();
  147. context.setFill (newColour);
  148. }
  149. void Graphics::setOpacity (const float newOpacity)
  150. {
  151. saveStateIfPending();
  152. context.setOpacity (newOpacity);
  153. }
  154. void Graphics::setGradientFill (const ColourGradient& gradient)
  155. {
  156. setFillType (gradient);
  157. }
  158. void Graphics::setTiledImageFill (const Image& imageToUse, const int anchorX, const int anchorY, const float opacity)
  159. {
  160. saveStateIfPending();
  161. context.setFill (FillType (imageToUse, AffineTransform::translation ((float) anchorX, (float) anchorY)));
  162. context.setOpacity (opacity);
  163. }
  164. void Graphics::setFillType (const FillType& newFill)
  165. {
  166. saveStateIfPending();
  167. context.setFill (newFill);
  168. }
  169. //==============================================================================
  170. void Graphics::setFont (const Font& newFont)
  171. {
  172. saveStateIfPending();
  173. context.setFont (newFont);
  174. }
  175. void Graphics::setFont (const float newFontHeight)
  176. {
  177. setFont (context.getFont().withHeight (newFontHeight));
  178. }
  179. Font Graphics::getCurrentFont() const
  180. {
  181. return context.getFont();
  182. }
  183. //==============================================================================
  184. void Graphics::drawSingleLineText (const String& text, const int startX, const int baselineY,
  185. const Justification& justification) const
  186. {
  187. if (text.isNotEmpty()
  188. && startX < context.getClipBounds().getRight())
  189. {
  190. GlyphArrangement arr;
  191. arr.addLineOfText (context.getFont(), text, (float) startX, (float) baselineY);
  192. // Don't pass any vertical placement flags to this method - they'll be ignored.
  193. jassert (justification.getOnlyVerticalFlags() == 0);
  194. const int flags = justification.getOnlyHorizontalFlags();
  195. if (flags != Justification::left)
  196. {
  197. float w = arr.getBoundingBox (0, -1, true).getWidth();
  198. if ((flags & (Justification::horizontallyCentred | Justification::horizontallyJustified)) != 0)
  199. w /= 2.0f;
  200. arr.draw (*this, AffineTransform::translation (-w, 0));
  201. }
  202. else
  203. {
  204. arr.draw (*this);
  205. }
  206. }
  207. }
  208. void Graphics::drawTextAsPath (const String& text, const AffineTransform& transform) const
  209. {
  210. if (text.isNotEmpty())
  211. {
  212. GlyphArrangement arr;
  213. arr.addLineOfText (context.getFont(), text, 0.0f, 0.0f);
  214. arr.draw (*this, transform);
  215. }
  216. }
  217. void Graphics::drawMultiLineText (const String& text, const int startX,
  218. const int baselineY, const int maximumLineWidth) const
  219. {
  220. if (text.isNotEmpty()
  221. && startX < context.getClipBounds().getRight())
  222. {
  223. GlyphArrangement arr;
  224. arr.addJustifiedText (context.getFont(), text,
  225. (float) startX, (float) baselineY, (float) maximumLineWidth,
  226. Justification::left);
  227. arr.draw (*this);
  228. }
  229. }
  230. void Graphics::drawText (const String& text, const Rectangle<int>& area,
  231. const Justification& justificationType,
  232. const bool useEllipsesIfTooBig) const
  233. {
  234. if (text.isNotEmpty() && context.clipRegionIntersects (area))
  235. {
  236. GlyphArrangement arr;
  237. arr.addCurtailedLineOfText (context.getFont(), text,
  238. 0.0f, 0.0f, (float) area.getWidth(),
  239. useEllipsesIfTooBig);
  240. arr.justifyGlyphs (0, arr.getNumGlyphs(),
  241. (float) area.getX(), (float) area.getY(),
  242. (float) area.getWidth(), (float) area.getHeight(),
  243. justificationType);
  244. arr.draw (*this);
  245. }
  246. }
  247. void Graphics::drawText (const String& text, const int x, const int y, const int width, const int height,
  248. const Justification& justificationType,
  249. const bool useEllipsesIfTooBig) const
  250. {
  251. drawText (text, Rectangle<int> (x, y, width, height), justificationType, useEllipsesIfTooBig);
  252. }
  253. void Graphics::drawFittedText (const String& text, const Rectangle<int>& area,
  254. const Justification& justification,
  255. const int maximumNumberOfLines,
  256. const float minimumHorizontalScale) const
  257. {
  258. if (text.isNotEmpty() && (! area.isEmpty()) && context.clipRegionIntersects (area))
  259. {
  260. GlyphArrangement arr;
  261. arr.addFittedText (context.getFont(), text,
  262. (float) area.getX(), (float) area.getY(),
  263. (float) area.getWidth(), (float) area.getHeight(),
  264. justification,
  265. maximumNumberOfLines,
  266. minimumHorizontalScale);
  267. arr.draw (*this);
  268. }
  269. }
  270. void Graphics::drawFittedText (const String& text, const int x, const int y, const int width, const int height,
  271. const Justification& justification,
  272. const int maximumNumberOfLines,
  273. const float minimumHorizontalScale) const
  274. {
  275. drawFittedText (text,Rectangle<int> (x, y, width, height),
  276. justification, maximumNumberOfLines, minimumHorizontalScale);
  277. }
  278. //==============================================================================
  279. void Graphics::fillRect (int x, int y, int width, int height) const
  280. {
  281. // passing in a silly number can cause maths problems in rendering!
  282. jassert (areCoordsSensibleNumbers (x, y, width, height));
  283. context.fillRect (Rectangle<int> (x, y, width, height), false);
  284. }
  285. void Graphics::fillRect (const Rectangle<int>& r) const
  286. {
  287. context.fillRect (r, false);
  288. }
  289. void Graphics::fillRect (const float x, const float y, const float width, const float height) const
  290. {
  291. // passing in a silly number can cause maths problems in rendering!
  292. jassert (areCoordsSensibleNumbers (x, y, width, height));
  293. Path p;
  294. p.addRectangle (x, y, width, height);
  295. fillPath (p);
  296. }
  297. void Graphics::setPixel (int x, int y) const
  298. {
  299. context.fillRect (Rectangle<int> (x, y, 1, 1), false);
  300. }
  301. void Graphics::fillAll() const
  302. {
  303. fillRect (context.getClipBounds());
  304. }
  305. void Graphics::fillAll (const Colour& colourToUse) const
  306. {
  307. if (! colourToUse.isTransparent())
  308. {
  309. const Rectangle<int> clip (context.getClipBounds());
  310. context.saveState();
  311. context.setFill (colourToUse);
  312. context.fillRect (clip, false);
  313. context.restoreState();
  314. }
  315. }
  316. //==============================================================================
  317. void Graphics::fillPath (const Path& path, const AffineTransform& transform) const
  318. {
  319. if ((! context.isClipEmpty()) && ! path.isEmpty())
  320. context.fillPath (path, transform);
  321. }
  322. void Graphics::strokePath (const Path& path,
  323. const PathStrokeType& strokeType,
  324. const AffineTransform& transform) const
  325. {
  326. Path stroke;
  327. strokeType.createStrokedPath (stroke, path, transform, context.getScaleFactor());
  328. fillPath (stroke);
  329. }
  330. //==============================================================================
  331. void Graphics::drawRect (const int x, const int y, const int width, const int height,
  332. const int lineThickness) const
  333. {
  334. // passing in a silly number can cause maths problems in rendering!
  335. jassert (areCoordsSensibleNumbers (x, y, width, height));
  336. context.fillRect (Rectangle<int> (x, y, width, lineThickness), false);
  337. context.fillRect (Rectangle<int> (x, y + lineThickness, lineThickness, height - lineThickness * 2), false);
  338. context.fillRect (Rectangle<int> (x + width - lineThickness, y + lineThickness, lineThickness, height - lineThickness * 2), false);
  339. context.fillRect (Rectangle<int> (x, y + height - lineThickness, width, lineThickness), false);
  340. }
  341. void Graphics::drawRect (const float x, const float y, const float width, const float height, const float lineThickness) const
  342. {
  343. // passing in a silly number can cause maths problems in rendering!
  344. jassert (areCoordsSensibleNumbers (x, y, width, height));
  345. Path p;
  346. p.addRectangle (x, y, width, lineThickness);
  347. p.addRectangle (x, y + lineThickness, lineThickness, height - lineThickness * 2.0f);
  348. p.addRectangle (x + width - lineThickness, y + lineThickness, lineThickness, height - lineThickness * 2.0f);
  349. p.addRectangle (x, y + height - lineThickness, width, lineThickness);
  350. fillPath (p);
  351. }
  352. void Graphics::drawRect (const Rectangle<int>& r, const int lineThickness) const
  353. {
  354. drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(), lineThickness);
  355. }
  356. //==============================================================================
  357. void Graphics::fillEllipse (const Rectangle<float>& area) const
  358. {
  359. fillEllipse (area.getX(), area.getY(), area.getWidth(), area.getHeight());
  360. }
  361. void Graphics::fillEllipse (const float x, const float y, const float width, const float height) const
  362. {
  363. // passing in a silly number can cause maths problems in rendering!
  364. jassert (areCoordsSensibleNumbers (x, y, width, height));
  365. Path p;
  366. p.addEllipse (x, y, width, height);
  367. fillPath (p);
  368. }
  369. void Graphics::drawEllipse (const float x, const float y, const float width, const float height,
  370. const float lineThickness) const
  371. {
  372. // passing in a silly number can cause maths problems in rendering!
  373. jassert (areCoordsSensibleNumbers (x, y, width, height));
  374. Path p;
  375. p.addEllipse (x, y, width, height);
  376. strokePath (p, PathStrokeType (lineThickness));
  377. }
  378. void Graphics::fillRoundedRectangle (const float x, const float y, const float width, const float height, const float cornerSize) const
  379. {
  380. // passing in a silly number can cause maths problems in rendering!
  381. jassert (areCoordsSensibleNumbers (x, y, width, height));
  382. Path p;
  383. p.addRoundedRectangle (x, y, width, height, cornerSize);
  384. fillPath (p);
  385. }
  386. void Graphics::fillRoundedRectangle (const Rectangle<float>& r, const float cornerSize) const
  387. {
  388. fillRoundedRectangle (r.getX(), r.getY(), r.getWidth(), r.getHeight(), cornerSize);
  389. }
  390. void Graphics::drawRoundedRectangle (const float x, const float y, const float width, const float height,
  391. const float cornerSize, const float lineThickness) const
  392. {
  393. // passing in a silly number can cause maths problems in rendering!
  394. jassert (areCoordsSensibleNumbers (x, y, width, height));
  395. Path p;
  396. p.addRoundedRectangle (x, y, width, height, cornerSize);
  397. strokePath (p, PathStrokeType (lineThickness));
  398. }
  399. void Graphics::drawRoundedRectangle (const Rectangle<float>& r, const float cornerSize, const float lineThickness) const
  400. {
  401. drawRoundedRectangle (r.getX(), r.getY(), r.getWidth(), r.getHeight(), cornerSize, lineThickness);
  402. }
  403. void Graphics::drawArrow (const Line<float>& line, const float lineThickness, const float arrowheadWidth, const float arrowheadLength) const
  404. {
  405. Path p;
  406. p.addArrow (line, lineThickness, arrowheadWidth, arrowheadLength);
  407. fillPath (p);
  408. }
  409. void Graphics::fillCheckerBoard (const Rectangle<int>& area,
  410. const int checkWidth, const int checkHeight,
  411. const Colour& colour1, const Colour& colour2) const
  412. {
  413. jassert (checkWidth > 0 && checkHeight > 0); // can't be zero or less!
  414. if (checkWidth > 0 && checkHeight > 0)
  415. {
  416. context.saveState();
  417. if (colour1 == colour2)
  418. {
  419. context.setFill (colour1);
  420. context.fillRect (area, false);
  421. }
  422. else
  423. {
  424. const Rectangle<int> clipped (context.getClipBounds().getIntersection (area));
  425. if (! clipped.isEmpty())
  426. {
  427. context.clipToRectangle (clipped);
  428. const int checkNumX = (clipped.getX() - area.getX()) / checkWidth;
  429. const int checkNumY = (clipped.getY() - area.getY()) / checkHeight;
  430. const int startX = area.getX() + checkNumX * checkWidth;
  431. const int startY = area.getY() + checkNumY * checkHeight;
  432. const int right = clipped.getRight();
  433. const int bottom = clipped.getBottom();
  434. for (int i = 0; i < 2; ++i)
  435. {
  436. context.setFill (i == ((checkNumX ^ checkNumY) & 1) ? colour1 : colour2);
  437. int cy = i;
  438. for (int y = startY; y < bottom; y += checkHeight)
  439. for (int x = startX + (cy++ & 1) * checkWidth; x < right; x += checkWidth * 2)
  440. context.fillRect (Rectangle<int> (x, y, checkWidth, checkHeight), false);
  441. }
  442. }
  443. }
  444. context.restoreState();
  445. }
  446. }
  447. //==============================================================================
  448. void Graphics::drawVerticalLine (const int x, float top, float bottom) const
  449. {
  450. context.drawVerticalLine (x, top, bottom);
  451. }
  452. void Graphics::drawHorizontalLine (const int y, float left, float right) const
  453. {
  454. context.drawHorizontalLine (y, left, right);
  455. }
  456. void Graphics::drawLine (const float x1, const float y1, const float x2, const float y2) const
  457. {
  458. context.drawLine (Line<float> (x1, y1, x2, y2));
  459. }
  460. void Graphics::drawLine (const Line<float>& line) const
  461. {
  462. context.drawLine (line);
  463. }
  464. void Graphics::drawLine (const float x1, const float y1, const float x2, const float y2, const float lineThickness) const
  465. {
  466. drawLine (Line<float> (x1, y1, x2, y2), lineThickness);
  467. }
  468. void Graphics::drawLine (const Line<float>& line, const float lineThickness) const
  469. {
  470. Path p;
  471. p.addLineSegment (line, lineThickness);
  472. fillPath (p);
  473. }
  474. void Graphics::drawDashedLine (const Line<float>& line, const float* const dashLengths,
  475. const int numDashLengths, const float lineThickness, int n) const
  476. {
  477. jassert (n >= 0 && n < numDashLengths); // your start index must be valid!
  478. const Point<double> delta ((line.getEnd() - line.getStart()).toDouble());
  479. const double totalLen = delta.getDistanceFromOrigin();
  480. if (totalLen >= 0.1)
  481. {
  482. const double onePixAlpha = 1.0 / totalLen;
  483. for (double alpha = 0.0; alpha < 1.0;)
  484. {
  485. jassert (dashLengths[n] > 0); // can't have zero-length dashes!
  486. const double lastAlpha = alpha;
  487. alpha += dashLengths [n] * onePixAlpha;
  488. n = (n + 1) % numDashLengths;
  489. if ((n & 1) != 0)
  490. {
  491. const Line<float> segment (line.getStart() + (delta * lastAlpha).toFloat(),
  492. line.getStart() + (delta * jmin (1.0, alpha)).toFloat());
  493. if (lineThickness != 1.0f)
  494. drawLine (segment, lineThickness);
  495. else
  496. context.drawLine (segment);
  497. }
  498. }
  499. }
  500. }
  501. //==============================================================================
  502. void Graphics::setImageResamplingQuality (const Graphics::ResamplingQuality newQuality)
  503. {
  504. saveStateIfPending();
  505. context.setInterpolationQuality (newQuality);
  506. }
  507. //==============================================================================
  508. void Graphics::drawImageAt (const Image& imageToDraw,
  509. const int topLeftX, const int topLeftY,
  510. const bool fillAlphaChannelWithCurrentBrush) const
  511. {
  512. const int imageW = imageToDraw.getWidth();
  513. const int imageH = imageToDraw.getHeight();
  514. drawImage (imageToDraw,
  515. topLeftX, topLeftY, imageW, imageH,
  516. 0, 0, imageW, imageH,
  517. fillAlphaChannelWithCurrentBrush);
  518. }
  519. void Graphics::drawImageWithin (const Image& imageToDraw,
  520. const int destX, const int destY,
  521. const int destW, const int destH,
  522. const RectanglePlacement& placementWithinTarget,
  523. const bool fillAlphaChannelWithCurrentBrush) const
  524. {
  525. // passing in a silly number can cause maths problems in rendering!
  526. jassert (areCoordsSensibleNumbers (destX, destY, destW, destH));
  527. if (imageToDraw.isValid())
  528. {
  529. const int imageW = imageToDraw.getWidth();
  530. const int imageH = imageToDraw.getHeight();
  531. if (imageW > 0 && imageH > 0)
  532. {
  533. double newX = 0.0, newY = 0.0;
  534. double newW = imageW;
  535. double newH = imageH;
  536. placementWithinTarget.applyTo (newX, newY, newW, newH,
  537. destX, destY, destW, destH);
  538. if (newW > 0 && newH > 0)
  539. {
  540. drawImage (imageToDraw,
  541. roundToInt (newX), roundToInt (newY),
  542. roundToInt (newW), roundToInt (newH),
  543. 0, 0, imageW, imageH,
  544. fillAlphaChannelWithCurrentBrush);
  545. }
  546. }
  547. }
  548. }
  549. void Graphics::drawImage (const Image& imageToDraw,
  550. int dx, int dy, int dw, int dh,
  551. int sx, int sy, int sw, int sh,
  552. const bool fillAlphaChannelWithCurrentBrush) const
  553. {
  554. // passing in a silly number can cause maths problems in rendering!
  555. jassert (areCoordsSensibleNumbers (dx, dy, dw, dh));
  556. jassert (areCoordsSensibleNumbers (sx, sy, sw, sh));
  557. if (imageToDraw.isValid() && context.clipRegionIntersects (Rectangle<int> (dx, dy, dw, dh)))
  558. {
  559. drawImageTransformed (imageToDraw.getClippedImage (Rectangle<int> (sx, sy, sw, sh)),
  560. AffineTransform::scale (dw / (float) sw, dh / (float) sh)
  561. .translated ((float) dx, (float) dy),
  562. fillAlphaChannelWithCurrentBrush);
  563. }
  564. }
  565. void Graphics::drawImageTransformed (const Image& imageToDraw,
  566. const AffineTransform& transform,
  567. const bool fillAlphaChannelWithCurrentBrush) const
  568. {
  569. if (imageToDraw.isValid() && ! context.isClipEmpty())
  570. {
  571. if (fillAlphaChannelWithCurrentBrush)
  572. {
  573. context.saveState();
  574. context.clipToImageAlpha (imageToDraw, transform);
  575. fillAll();
  576. context.restoreState();
  577. }
  578. else
  579. {
  580. context.drawImage (imageToDraw, transform);
  581. }
  582. }
  583. }
  584. //==============================================================================
  585. Graphics::ScopedSaveState::ScopedSaveState (Graphics& g)
  586. : context (g)
  587. {
  588. context.saveState();
  589. }
  590. Graphics::ScopedSaveState::~ScopedSaveState()
  591. {
  592. context.restoreState();
  593. }