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.

691 lines
22KB

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