Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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