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.

711 lines
24KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. namespace
  18. {
  19. template <typename Type>
  20. bool areCoordsSensibleNumbers (Type x, Type y, Type w, Type h)
  21. {
  22. const int maxVal = 0x3fffffff;
  23. return (int) x >= -maxVal && (int) x <= maxVal
  24. && (int) y >= -maxVal && (int) y <= maxVal
  25. && (int) w >= -maxVal && (int) w <= maxVal
  26. && (int) h >= -maxVal && (int) h <= maxVal;
  27. }
  28. }
  29. //==============================================================================
  30. LowLevelGraphicsContext::LowLevelGraphicsContext() {}
  31. LowLevelGraphicsContext::~LowLevelGraphicsContext() {}
  32. //==============================================================================
  33. Graphics::Graphics (const Image& imageToDrawOnto)
  34. : context (*imageToDrawOnto.createLowLevelContext()),
  35. contextToDelete (&context),
  36. saveStatePending (false)
  37. {
  38. jassert (imageToDrawOnto.isValid()); // Can't draw into a null image!
  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<int>& 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 (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. 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::drawMultiLineText (const String& text, const int startX,
  209. const int baselineY, const int maximumLineWidth) const
  210. {
  211. if (text.isNotEmpty()
  212. && startX < context.getClipBounds().getRight())
  213. {
  214. GlyphArrangement arr;
  215. arr.addJustifiedText (context.getFont(), text,
  216. (float) startX, (float) baselineY, (float) maximumLineWidth,
  217. Justification::left);
  218. arr.draw (*this);
  219. }
  220. }
  221. void Graphics::drawText (const String& text, const Rectangle<int>& area,
  222. Justification justificationType,
  223. const bool useEllipsesIfTooBig) const
  224. {
  225. if (text.isNotEmpty() && context.clipRegionIntersects (area))
  226. {
  227. GlyphArrangement arr;
  228. arr.addCurtailedLineOfText (context.getFont(), text,
  229. 0.0f, 0.0f, (float) area.getWidth(),
  230. useEllipsesIfTooBig);
  231. arr.justifyGlyphs (0, arr.getNumGlyphs(),
  232. (float) area.getX(), (float) area.getY(),
  233. (float) area.getWidth(), (float) area.getHeight(),
  234. justificationType);
  235. arr.draw (*this);
  236. }
  237. }
  238. void Graphics::drawText (const String& text, const int x, const int y, const int width, const int height,
  239. Justification justificationType,
  240. const bool useEllipsesIfTooBig) const
  241. {
  242. drawText (text, Rectangle<int> (x, y, width, height), justificationType, useEllipsesIfTooBig);
  243. }
  244. void Graphics::drawFittedText (const String& text, const Rectangle<int>& area,
  245. Justification justification,
  246. const int maximumNumberOfLines,
  247. const float minimumHorizontalScale) const
  248. {
  249. if (text.isNotEmpty() && (! area.isEmpty()) && context.clipRegionIntersects (area))
  250. {
  251. GlyphArrangement arr;
  252. arr.addFittedText (context.getFont(), text,
  253. (float) area.getX(), (float) area.getY(),
  254. (float) area.getWidth(), (float) area.getHeight(),
  255. justification,
  256. maximumNumberOfLines,
  257. minimumHorizontalScale);
  258. arr.draw (*this);
  259. }
  260. }
  261. void Graphics::drawFittedText (const String& text, const int x, const int y, const int width, const int height,
  262. Justification justification,
  263. const int maximumNumberOfLines,
  264. const float minimumHorizontalScale) const
  265. {
  266. drawFittedText (text,Rectangle<int> (x, y, width, height),
  267. justification, maximumNumberOfLines, minimumHorizontalScale);
  268. }
  269. //==============================================================================
  270. void Graphics::fillRect (int x, int y, int width, int height) const
  271. {
  272. // passing in a silly number can cause maths problems in rendering!
  273. jassert (areCoordsSensibleNumbers (x, y, width, height));
  274. context.fillRect (Rectangle<int> (x, y, width, height), false);
  275. }
  276. void Graphics::fillRect (const Rectangle<int>& r) const
  277. {
  278. context.fillRect (r, false);
  279. }
  280. void Graphics::fillRect (const Rectangle<float>& rectangle) const
  281. {
  282. Path p;
  283. p.addRectangle (rectangle);
  284. fillPath (p);
  285. }
  286. void Graphics::fillRect (const float x, const float y, const float width, const float height) const
  287. {
  288. // passing in a silly number can cause maths problems in rendering!
  289. jassert (areCoordsSensibleNumbers (x, y, width, height));
  290. fillRect (Rectangle<float> (x, y, width, height));
  291. }
  292. void Graphics::setPixel (int x, int y) const
  293. {
  294. context.fillRect (Rectangle<int> (x, y, 1, 1), false);
  295. }
  296. void Graphics::fillAll() const
  297. {
  298. fillRect (context.getClipBounds());
  299. }
  300. void Graphics::fillAll (Colour colourToUse) const
  301. {
  302. if (! colourToUse.isTransparent())
  303. {
  304. const Rectangle<int> clip (context.getClipBounds());
  305. context.saveState();
  306. context.setFill (colourToUse);
  307. context.fillRect (clip, false);
  308. context.restoreState();
  309. }
  310. }
  311. //==============================================================================
  312. void Graphics::fillPath (const Path& path, const AffineTransform& transform) const
  313. {
  314. if ((! context.isClipEmpty()) && ! path.isEmpty())
  315. context.fillPath (path, transform);
  316. }
  317. void Graphics::strokePath (const Path& path,
  318. const PathStrokeType& strokeType,
  319. const AffineTransform& transform) const
  320. {
  321. Path stroke;
  322. strokeType.createStrokedPath (stroke, path, transform, context.getScaleFactor());
  323. fillPath (stroke);
  324. }
  325. //==============================================================================
  326. void Graphics::drawRect (const int x, const int y, const int width, const int height,
  327. const int lineThickness) const
  328. {
  329. // passing in a silly number can cause maths problems in rendering!
  330. jassert (areCoordsSensibleNumbers (x, y, width, height));
  331. context.fillRect (Rectangle<int> (x, y, width, lineThickness), false);
  332. context.fillRect (Rectangle<int> (x, y + lineThickness, lineThickness, height - lineThickness * 2), false);
  333. context.fillRect (Rectangle<int> (x + width - lineThickness, y + lineThickness, lineThickness, height - lineThickness * 2), false);
  334. context.fillRect (Rectangle<int> (x, y + height - lineThickness, width, lineThickness), false);
  335. }
  336. void Graphics::drawRect (const float x, const float y, const float width, const float height,
  337. const float lineThickness) const
  338. {
  339. // passing in a silly number can cause maths problems in rendering!
  340. jassert (areCoordsSensibleNumbers (x, y, width, height));
  341. Path p;
  342. p.addRectangle (x, y, width, lineThickness);
  343. p.addRectangle (x, y + lineThickness, lineThickness, height - lineThickness * 2.0f);
  344. p.addRectangle (x + width - lineThickness, y + lineThickness, lineThickness, height - lineThickness * 2.0f);
  345. p.addRectangle (x, y + height - lineThickness, width, lineThickness);
  346. fillPath (p);
  347. }
  348. void Graphics::drawRect (const Rectangle<int>& r, const int lineThickness) const
  349. {
  350. drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(), lineThickness);
  351. }
  352. void Graphics::drawRect (const Rectangle<float>& r, const float 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. Colour colour1, 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. 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. }