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.

703 lines
22KB

  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. namespace
  18. {
  19. template <typename Type>
  20. Rectangle<Type> coordsToRectangle (Type x, Type y, Type w, Type h) noexcept
  21. {
  22. #if JUCE_DEBUG
  23. const int maxVal = 0x3fffffff;
  24. jassert ((int) x >= -maxVal && (int) x <= maxVal
  25. && (int) y >= -maxVal && (int) y <= maxVal
  26. && (int) w >= 0 && (int) w <= maxVal
  27. && (int) h >= 0 && (int) h <= maxVal);
  28. #endif
  29. return Rectangle<Type> (x, y, w, h);
  30. }
  31. }
  32. //==============================================================================
  33. LowLevelGraphicsContext::LowLevelGraphicsContext() {}
  34. LowLevelGraphicsContext::~LowLevelGraphicsContext() {}
  35. //==============================================================================
  36. Graphics::Graphics (const Image& imageToDrawOnto)
  37. : context (*imageToDrawOnto.createLowLevelContext()),
  38. contextToDelete (&context),
  39. saveStatePending (false)
  40. {
  41. jassert (imageToDrawOnto.isValid()); // Can't draw into a null image!
  42. }
  43. Graphics::Graphics (LowLevelGraphicsContext& internalContext) noexcept
  44. : context (internalContext),
  45. saveStatePending (false)
  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 (const int x, const int y, const int w, const 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 (Point<int> (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. const int flags = justification.getOnlyHorizontalFlags();
  198. if (flags == Justification::right)
  199. {
  200. if (startX < context.getClipBounds().getX())
  201. return;
  202. }
  203. else if (flags == Justification::left)
  204. if (startX > context.getClipBounds().getRight())
  205. return;
  206. GlyphArrangement arr;
  207. arr.addLineOfText (context.getFont(), text, (float) startX, (float) baselineY);
  208. if (flags != Justification::left)
  209. {
  210. float w = arr.getBoundingBox (0, -1, true).getWidth();
  211. if ((flags & (Justification::horizontallyCentred | Justification::horizontallyJustified)) != 0)
  212. w /= 2.0f;
  213. arr.draw (*this, AffineTransform::translation (-w, 0));
  214. }
  215. else
  216. {
  217. arr.draw (*this);
  218. }
  219. }
  220. }
  221. void Graphics::drawMultiLineText (const String& text, const int startX,
  222. const int baselineY, const int maximumLineWidth) const
  223. {
  224. if (text.isNotEmpty()
  225. && startX < context.getClipBounds().getRight())
  226. {
  227. GlyphArrangement arr;
  228. arr.addJustifiedText (context.getFont(), text,
  229. (float) startX, (float) baselineY, (float) maximumLineWidth,
  230. Justification::left);
  231. arr.draw (*this);
  232. }
  233. }
  234. void Graphics::drawText (const String& text, Rectangle<float> area,
  235. Justification justificationType, bool useEllipsesIfTooBig) const
  236. {
  237. if (text.isNotEmpty() && context.clipRegionIntersects (area.getSmallestIntegerContainer()))
  238. {
  239. GlyphArrangement arr;
  240. arr.addCurtailedLineOfText (context.getFont(), text, 0.0f, 0.0f,
  241. area.getWidth(), useEllipsesIfTooBig);
  242. arr.justifyGlyphs (0, arr.getNumGlyphs(),
  243. area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  244. justificationType);
  245. arr.draw (*this);
  246. }
  247. }
  248. void Graphics::drawText (const String& text, Rectangle<int> area,
  249. Justification justificationType, bool useEllipsesIfTooBig) const
  250. {
  251. drawText (text, area.toFloat(), justificationType, useEllipsesIfTooBig);
  252. }
  253. void Graphics::drawText (const String& text, int x, int y, int width, int height,
  254. Justification justificationType, const bool useEllipsesIfTooBig) const
  255. {
  256. drawText (text, coordsToRectangle (x, y, width, height), justificationType, useEllipsesIfTooBig);
  257. }
  258. void Graphics::drawFittedText (const String& text, Rectangle<int> area,
  259. Justification justification,
  260. const int maximumNumberOfLines,
  261. const float minimumHorizontalScale) const
  262. {
  263. if (text.isNotEmpty() && (! area.isEmpty()) && context.clipRegionIntersects (area))
  264. {
  265. GlyphArrangement arr;
  266. arr.addFittedText (context.getFont(), text,
  267. (float) area.getX(), (float) area.getY(),
  268. (float) area.getWidth(), (float) area.getHeight(),
  269. justification,
  270. maximumNumberOfLines,
  271. minimumHorizontalScale);
  272. arr.draw (*this);
  273. }
  274. }
  275. void Graphics::drawFittedText (const String& text, int x, int y, int width, int height,
  276. Justification justification,
  277. const int maximumNumberOfLines,
  278. const float minimumHorizontalScale) const
  279. {
  280. drawFittedText (text, coordsToRectangle (x, y, width, height),
  281. justification, maximumNumberOfLines, minimumHorizontalScale);
  282. }
  283. //==============================================================================
  284. void Graphics::fillRect (Rectangle<int> r) const
  285. {
  286. context.fillRect (r, false);
  287. }
  288. void Graphics::fillRect (Rectangle<float> r) const
  289. {
  290. context.fillRect (r);
  291. }
  292. void Graphics::fillRect (int x, int y, int width, int height) const
  293. {
  294. context.fillRect (coordsToRectangle (x, y, width, height), false);
  295. }
  296. void Graphics::fillRect (float x, float y, float width, float height) const
  297. {
  298. fillRect (coordsToRectangle (x, y, width, height));
  299. }
  300. void Graphics::fillRectList (const RectangleList<float>& rectangles) const
  301. {
  302. context.fillRectList (rectangles);
  303. }
  304. void Graphics::fillRectList (const RectangleList<int>& rects) const
  305. {
  306. for (const Rectangle<int>* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  307. context.fillRect (*r, false);
  308. }
  309. void Graphics::setPixel (int x, int y) const
  310. {
  311. context.fillRect (coordsToRectangle (x, y, 1, 1), false);
  312. }
  313. void Graphics::fillAll() const
  314. {
  315. fillRect (context.getClipBounds());
  316. }
  317. void Graphics::fillAll (Colour colourToUse) const
  318. {
  319. if (! colourToUse.isTransparent())
  320. {
  321. const Rectangle<int> clip (context.getClipBounds());
  322. context.saveState();
  323. context.setFill (colourToUse);
  324. context.fillRect (clip, false);
  325. context.restoreState();
  326. }
  327. }
  328. //==============================================================================
  329. void Graphics::fillPath (const Path& path) const
  330. {
  331. if (! (context.isClipEmpty() || path.isEmpty()))
  332. context.fillPath (path, AffineTransform());
  333. }
  334. void Graphics::fillPath (const Path& path, const AffineTransform& transform) const
  335. {
  336. if (! (context.isClipEmpty() || path.isEmpty()))
  337. context.fillPath (path, transform);
  338. }
  339. void Graphics::strokePath (const Path& path,
  340. const PathStrokeType& strokeType,
  341. const AffineTransform& transform) const
  342. {
  343. Path stroke;
  344. strokeType.createStrokedPath (stroke, path, transform, context.getPhysicalPixelScaleFactor());
  345. fillPath (stroke);
  346. }
  347. //==============================================================================
  348. void Graphics::drawRect (float x, float y, float width, float height, float lineThickness) const
  349. {
  350. drawRect (coordsToRectangle (x, y, width, height), lineThickness);
  351. }
  352. void Graphics::drawRect (int x, int y, int width, int height, int lineThickness) const
  353. {
  354. drawRect (coordsToRectangle (x, y, width, height), lineThickness);
  355. }
  356. void Graphics::drawRect (Rectangle<int> r, int lineThickness) const
  357. {
  358. drawRect (r.toFloat(), (float) lineThickness);
  359. }
  360. void Graphics::drawRect (Rectangle<float> r, const float lineThickness) const
  361. {
  362. jassert (r.getWidth() >= 0.0f && r.getHeight() >= 0.0f);
  363. RectangleList<float> rects;
  364. rects.addWithoutMerging (r.removeFromTop (lineThickness));
  365. rects.addWithoutMerging (r.removeFromBottom (lineThickness));
  366. rects.addWithoutMerging (r.removeFromLeft (lineThickness));
  367. rects.addWithoutMerging (r.removeFromRight (lineThickness));
  368. context.fillRectList (rects);
  369. }
  370. //==============================================================================
  371. void Graphics::fillEllipse (Rectangle<float> area) const
  372. {
  373. Path p;
  374. p.addEllipse (area);
  375. fillPath (p);
  376. }
  377. void Graphics::fillEllipse (float x, float y, float w, float h) const
  378. {
  379. fillEllipse (coordsToRectangle (x, y, w, h));
  380. }
  381. void Graphics::drawEllipse (float x, float y, float width, float height, float lineThickness) const
  382. {
  383. drawEllipse (coordsToRectangle (x, y, width, height), lineThickness);
  384. }
  385. void Graphics::drawEllipse (Rectangle<float> area, float lineThickness) const
  386. {
  387. Path p;
  388. if (area.getWidth() == area.getHeight())
  389. {
  390. // For a circle, we can avoid having to generate a stroke
  391. p.addEllipse (area.expanded (lineThickness * 0.5f));
  392. p.addEllipse (area.reduced (lineThickness * 0.5f));
  393. p.setUsingNonZeroWinding (false);
  394. fillPath (p);
  395. }
  396. else
  397. {
  398. p.addEllipse (area);
  399. strokePath (p, PathStrokeType (lineThickness));
  400. }
  401. }
  402. void Graphics::fillRoundedRectangle (float x, float y, float width, float height, float cornerSize) const
  403. {
  404. fillRoundedRectangle (coordsToRectangle (x, y, width, height), cornerSize);
  405. }
  406. void Graphics::fillRoundedRectangle (Rectangle<float> r, const float cornerSize) const
  407. {
  408. Path p;
  409. p.addRoundedRectangle (r, cornerSize);
  410. fillPath (p);
  411. }
  412. void Graphics::drawRoundedRectangle (float x, float y, float width, float height,
  413. float cornerSize, float lineThickness) const
  414. {
  415. drawRoundedRectangle (coordsToRectangle (x, y, width, height), cornerSize, lineThickness);
  416. }
  417. void Graphics::drawRoundedRectangle (Rectangle<float> r, float cornerSize, float lineThickness) const
  418. {
  419. Path p;
  420. p.addRoundedRectangle (r, cornerSize);
  421. strokePath (p, PathStrokeType (lineThickness));
  422. }
  423. void Graphics::drawArrow (const Line<float>& line, float lineThickness, float arrowheadWidth, float arrowheadLength) const
  424. {
  425. Path p;
  426. p.addArrow (line, lineThickness, arrowheadWidth, arrowheadLength);
  427. fillPath (p);
  428. }
  429. void Graphics::fillCheckerBoard (Rectangle<int> area,
  430. const int checkWidth, const int checkHeight,
  431. Colour colour1, Colour colour2) const
  432. {
  433. jassert (checkWidth > 0 && checkHeight > 0); // can't be zero or less!
  434. if (checkWidth > 0 && checkHeight > 0)
  435. {
  436. context.saveState();
  437. if (colour1 == colour2)
  438. {
  439. context.setFill (colour1);
  440. context.fillRect (area, false);
  441. }
  442. else
  443. {
  444. const Rectangle<int> clipped (context.getClipBounds().getIntersection (area));
  445. if (! clipped.isEmpty())
  446. {
  447. context.clipToRectangle (clipped);
  448. const int checkNumX = (clipped.getX() - area.getX()) / checkWidth;
  449. const int checkNumY = (clipped.getY() - area.getY()) / checkHeight;
  450. const int startX = area.getX() + checkNumX * checkWidth;
  451. const int startY = area.getY() + checkNumY * checkHeight;
  452. const int right = clipped.getRight();
  453. const int bottom = clipped.getBottom();
  454. for (int i = 0; i < 2; ++i)
  455. {
  456. context.setFill (i == ((checkNumX ^ checkNumY) & 1) ? colour1 : colour2);
  457. int cy = i;
  458. for (int y = startY; y < bottom; y += checkHeight)
  459. for (int x = startX + (cy++ & 1) * checkWidth; x < right; x += checkWidth * 2)
  460. context.fillRect (Rectangle<int> (x, y, checkWidth, checkHeight), false);
  461. }
  462. }
  463. }
  464. context.restoreState();
  465. }
  466. }
  467. //==============================================================================
  468. void Graphics::drawVerticalLine (const int x, float top, float bottom) const
  469. {
  470. if (top < bottom)
  471. context.fillRect (Rectangle<float> ((float) x, top, 1.0f, bottom - top));
  472. }
  473. void Graphics::drawHorizontalLine (const int y, float left, float right) const
  474. {
  475. if (left < right)
  476. context.fillRect (Rectangle<float> (left, (float) y, right - left, 1.0f));
  477. }
  478. void Graphics::drawLine (const Line<float>& line) const
  479. {
  480. context.drawLine (line);
  481. }
  482. void Graphics::drawLine (float x1, float y1, float x2, float y2) const
  483. {
  484. context.drawLine (Line<float> (x1, y1, x2, y2));
  485. }
  486. void Graphics::drawLine (float x1, float y1, float x2, float y2, float lineThickness) const
  487. {
  488. drawLine (Line<float> (x1, y1, x2, y2), lineThickness);
  489. }
  490. void Graphics::drawLine (const Line<float>& line, const float lineThickness) const
  491. {
  492. Path p;
  493. p.addLineSegment (line, lineThickness);
  494. fillPath (p);
  495. }
  496. void Graphics::drawDashedLine (const Line<float>& line, const float* const dashLengths,
  497. const int numDashLengths, const float lineThickness, int n) const
  498. {
  499. jassert (n >= 0 && n < numDashLengths); // your start index must be valid!
  500. const Point<double> delta ((line.getEnd() - line.getStart()).toDouble());
  501. const double totalLen = delta.getDistanceFromOrigin();
  502. if (totalLen >= 0.1)
  503. {
  504. const double onePixAlpha = 1.0 / totalLen;
  505. for (double alpha = 0.0; alpha < 1.0;)
  506. {
  507. jassert (dashLengths[n] > 0); // can't have zero-length dashes!
  508. const double lastAlpha = alpha;
  509. alpha += dashLengths [n] * onePixAlpha;
  510. n = (n + 1) % numDashLengths;
  511. if ((n & 1) != 0)
  512. {
  513. const Line<float> segment (line.getStart() + (delta * lastAlpha).toFloat(),
  514. line.getStart() + (delta * jmin (1.0, alpha)).toFloat());
  515. if (lineThickness != 1.0f)
  516. drawLine (segment, lineThickness);
  517. else
  518. context.drawLine (segment);
  519. }
  520. }
  521. }
  522. }
  523. //==============================================================================
  524. void Graphics::setImageResamplingQuality (const Graphics::ResamplingQuality newQuality)
  525. {
  526. saveStateIfPending();
  527. context.setInterpolationQuality (newQuality);
  528. }
  529. //==============================================================================
  530. void Graphics::drawImageAt (const Image& imageToDraw, int x, int y, bool fillAlphaChannel) const
  531. {
  532. drawImageTransformed (imageToDraw,
  533. AffineTransform::translation ((float) x, (float) y),
  534. fillAlphaChannel);
  535. }
  536. void Graphics::drawImage (const Image& imageToDraw, Rectangle<float> targetArea,
  537. RectanglePlacement placementWithinTarget, bool fillAlphaChannelWithCurrentBrush) const
  538. {
  539. if (imageToDraw.isValid())
  540. drawImageTransformed (imageToDraw,
  541. placementWithinTarget.getTransformToFit (imageToDraw.getBounds().toFloat(), targetArea),
  542. fillAlphaChannelWithCurrentBrush);
  543. }
  544. void Graphics::drawImageWithin (const Image& imageToDraw, int dx, int dy, int dw, int dh,
  545. RectanglePlacement placementWithinTarget, bool fillAlphaChannelWithCurrentBrush) const
  546. {
  547. drawImage (imageToDraw, coordsToRectangle (dx, dy, dw, dh).toFloat(),
  548. placementWithinTarget, fillAlphaChannelWithCurrentBrush);
  549. }
  550. void Graphics::drawImage (const Image& imageToDraw,
  551. int dx, int dy, int dw, int dh,
  552. int sx, int sy, int sw, int sh,
  553. const bool fillAlphaChannelWithCurrentBrush) const
  554. {
  555. if (imageToDraw.isValid() && context.clipRegionIntersects (coordsToRectangle (dx, dy, dw, dh)))
  556. drawImageTransformed (imageToDraw.getClippedImage (coordsToRectangle (sx, sy, sw, sh)),
  557. AffineTransform::scale (dw / (float) sw, dh / (float) sh)
  558. .translated ((float) dx, (float) dy),
  559. fillAlphaChannelWithCurrentBrush);
  560. }
  561. void Graphics::drawImageTransformed (const Image& imageToDraw,
  562. const AffineTransform& transform,
  563. const bool fillAlphaChannelWithCurrentBrush) const
  564. {
  565. if (imageToDraw.isValid() && ! context.isClipEmpty())
  566. {
  567. if (fillAlphaChannelWithCurrentBrush)
  568. {
  569. context.saveState();
  570. context.clipToImageAlpha (imageToDraw, transform);
  571. fillAll();
  572. context.restoreState();
  573. }
  574. else
  575. {
  576. context.drawImage (imageToDraw, transform);
  577. }
  578. }
  579. }
  580. //==============================================================================
  581. Graphics::ScopedSaveState::ScopedSaveState (Graphics& g) : context (g)
  582. {
  583. context.saveState();
  584. }
  585. Graphics::ScopedSaveState::~ScopedSaveState()
  586. {
  587. context.restoreState();
  588. }