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.

695 lines
22KB

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