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.

690 lines
23KB

  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::fillRectList (const RectangleList<float>& rectangles) const
  293. {
  294. context.fillRectList (rectangles);
  295. }
  296. void Graphics::setPixel (int x, int y) const
  297. {
  298. context.fillRect (Rectangle<int> (x, y, 1, 1), 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. const Rectangle<int> 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 AffineTransform& transform) const
  317. {
  318. if ((! context.isClipEmpty()) && ! path.isEmpty())
  319. context.fillPath (path, transform);
  320. }
  321. void Graphics::strokePath (const Path& path,
  322. const PathStrokeType& strokeType,
  323. const AffineTransform& transform) const
  324. {
  325. Path stroke;
  326. strokeType.createStrokedPath (stroke, path, transform, context.getPhysicalPixelScaleFactor());
  327. fillPath (stroke);
  328. }
  329. //==============================================================================
  330. void Graphics::drawRect (const int x, const int y, const int width, const int height,
  331. const int lineThickness) const
  332. {
  333. // passing in a silly number can cause maths problems in rendering!
  334. jassert (areCoordsSensibleNumbers (x, y, width, height));
  335. context.fillRect (Rectangle<int> (x, y, width, lineThickness), false);
  336. context.fillRect (Rectangle<int> (x, y + lineThickness, lineThickness, height - lineThickness * 2), false);
  337. context.fillRect (Rectangle<int> (x + width - lineThickness, y + lineThickness, lineThickness, height - lineThickness * 2), false);
  338. context.fillRect (Rectangle<int> (x, y + height - lineThickness, width, lineThickness), false);
  339. }
  340. void Graphics::drawRect (const float x, const float y, const float width, const float height,
  341. const float lineThickness) const
  342. {
  343. // passing in a silly number can cause maths problems in rendering!
  344. jassert (areCoordsSensibleNumbers (x, y, width, height));
  345. Path p;
  346. p.addRectangle (x, y, width, lineThickness);
  347. p.addRectangle (x, y + lineThickness, lineThickness, height - lineThickness * 2.0f);
  348. p.addRectangle (x + width - lineThickness, y + lineThickness, lineThickness, height - lineThickness * 2.0f);
  349. p.addRectangle (x, y + height - lineThickness, width, lineThickness);
  350. fillPath (p);
  351. }
  352. void Graphics::drawRect (const Rectangle<int>& r, const int lineThickness) const
  353. {
  354. drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(), lineThickness);
  355. }
  356. void Graphics::drawRect (const Rectangle<float>& r, const float lineThickness) const
  357. {
  358. drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(), lineThickness);
  359. }
  360. //==============================================================================
  361. void Graphics::fillEllipse (const Rectangle<float>& area) const
  362. {
  363. fillEllipse (area.getX(), area.getY(), area.getWidth(), area.getHeight());
  364. }
  365. void Graphics::fillEllipse (const float x, const float y, const float width, const float height) const
  366. {
  367. // passing in a silly number can cause maths problems in rendering!
  368. jassert (areCoordsSensibleNumbers (x, y, width, height));
  369. Path p;
  370. p.addEllipse (x, y, width, height);
  371. fillPath (p);
  372. }
  373. void Graphics::drawEllipse (const float x, const float y, const float width, const float height,
  374. const float lineThickness) const
  375. {
  376. // passing in a silly number can cause maths problems in rendering!
  377. jassert (areCoordsSensibleNumbers (x, y, width, height));
  378. Path p;
  379. p.addEllipse (x, y, width, height);
  380. strokePath (p, PathStrokeType (lineThickness));
  381. }
  382. void Graphics::fillRoundedRectangle (const float x, const float y, const float width, const float height, const float cornerSize) const
  383. {
  384. // passing in a silly number can cause maths problems in rendering!
  385. jassert (areCoordsSensibleNumbers (x, y, width, height));
  386. Path p;
  387. p.addRoundedRectangle (x, y, width, height, cornerSize);
  388. fillPath (p);
  389. }
  390. void Graphics::fillRoundedRectangle (const Rectangle<float>& r, const float cornerSize) const
  391. {
  392. fillRoundedRectangle (r.getX(), r.getY(), r.getWidth(), r.getHeight(), cornerSize);
  393. }
  394. void Graphics::drawRoundedRectangle (const float x, const float y, const float width, const float height,
  395. const float cornerSize, const float lineThickness) const
  396. {
  397. // passing in a silly number can cause maths problems in rendering!
  398. jassert (areCoordsSensibleNumbers (x, y, width, height));
  399. Path p;
  400. p.addRoundedRectangle (x, y, width, height, cornerSize);
  401. strokePath (p, PathStrokeType (lineThickness));
  402. }
  403. void Graphics::drawRoundedRectangle (const Rectangle<float>& r, const float cornerSize, const float lineThickness) const
  404. {
  405. drawRoundedRectangle (r.getX(), r.getY(), r.getWidth(), r.getHeight(), cornerSize, lineThickness);
  406. }
  407. void Graphics::drawArrow (const Line<float>& line, const float lineThickness, const float arrowheadWidth, const float arrowheadLength) const
  408. {
  409. Path p;
  410. p.addArrow (line, lineThickness, arrowheadWidth, arrowheadLength);
  411. fillPath (p);
  412. }
  413. void Graphics::fillCheckerBoard (const Rectangle<int>& area,
  414. const int checkWidth, const int checkHeight,
  415. Colour colour1, Colour colour2) const
  416. {
  417. jassert (checkWidth > 0 && checkHeight > 0); // can't be zero or less!
  418. if (checkWidth > 0 && checkHeight > 0)
  419. {
  420. context.saveState();
  421. if (colour1 == colour2)
  422. {
  423. context.setFill (colour1);
  424. context.fillRect (area, false);
  425. }
  426. else
  427. {
  428. const Rectangle<int> clipped (context.getClipBounds().getIntersection (area));
  429. if (! clipped.isEmpty())
  430. {
  431. context.clipToRectangle (clipped);
  432. const int checkNumX = (clipped.getX() - area.getX()) / checkWidth;
  433. const int checkNumY = (clipped.getY() - area.getY()) / checkHeight;
  434. const int startX = area.getX() + checkNumX * checkWidth;
  435. const int startY = area.getY() + checkNumY * checkHeight;
  436. const int right = clipped.getRight();
  437. const int bottom = clipped.getBottom();
  438. for (int i = 0; i < 2; ++i)
  439. {
  440. context.setFill (i == ((checkNumX ^ checkNumY) & 1) ? colour1 : colour2);
  441. int cy = i;
  442. for (int y = startY; y < bottom; y += checkHeight)
  443. for (int x = startX + (cy++ & 1) * checkWidth; x < right; x += checkWidth * 2)
  444. context.fillRect (Rectangle<int> (x, y, checkWidth, checkHeight), false);
  445. }
  446. }
  447. }
  448. context.restoreState();
  449. }
  450. }
  451. //==============================================================================
  452. void Graphics::drawVerticalLine (const int x, float top, float bottom) const
  453. {
  454. context.drawVerticalLine (x, top, bottom);
  455. }
  456. void Graphics::drawHorizontalLine (const int y, float left, float right) const
  457. {
  458. context.drawHorizontalLine (y, left, right);
  459. }
  460. void Graphics::drawLine (const float x1, const float y1, const float x2, const float y2) const
  461. {
  462. context.drawLine (Line<float> (x1, y1, x2, y2));
  463. }
  464. void Graphics::drawLine (const Line<float>& line) const
  465. {
  466. context.drawLine (line);
  467. }
  468. void Graphics::drawLine (const float x1, const float y1, const float x2, const float y2, const float lineThickness) const
  469. {
  470. drawLine (Line<float> (x1, y1, x2, y2), lineThickness);
  471. }
  472. void Graphics::drawLine (const Line<float>& line, const float lineThickness) const
  473. {
  474. Path p;
  475. p.addLineSegment (line, lineThickness);
  476. fillPath (p);
  477. }
  478. void Graphics::drawDashedLine (const Line<float>& line, const float* const dashLengths,
  479. const int numDashLengths, const float lineThickness, int n) const
  480. {
  481. jassert (n >= 0 && n < numDashLengths); // your start index must be valid!
  482. const Point<double> delta ((line.getEnd() - line.getStart()).toDouble());
  483. const double totalLen = delta.getDistanceFromOrigin();
  484. if (totalLen >= 0.1)
  485. {
  486. const double onePixAlpha = 1.0 / totalLen;
  487. for (double alpha = 0.0; alpha < 1.0;)
  488. {
  489. jassert (dashLengths[n] > 0); // can't have zero-length dashes!
  490. const double lastAlpha = alpha;
  491. alpha += dashLengths [n] * onePixAlpha;
  492. n = (n + 1) % numDashLengths;
  493. if ((n & 1) != 0)
  494. {
  495. const Line<float> segment (line.getStart() + (delta * lastAlpha).toFloat(),
  496. line.getStart() + (delta * jmin (1.0, alpha)).toFloat());
  497. if (lineThickness != 1.0f)
  498. drawLine (segment, lineThickness);
  499. else
  500. context.drawLine (segment);
  501. }
  502. }
  503. }
  504. }
  505. //==============================================================================
  506. void Graphics::setImageResamplingQuality (const Graphics::ResamplingQuality newQuality)
  507. {
  508. saveStateIfPending();
  509. context.setInterpolationQuality (newQuality);
  510. }
  511. //==============================================================================
  512. void Graphics::drawImageAt (const Image& imageToDraw,
  513. const int topLeftX, const int topLeftY,
  514. const bool fillAlphaChannelWithCurrentBrush) const
  515. {
  516. drawImageTransformed (imageToDraw,
  517. AffineTransform::translation ((float) topLeftX, (float) topLeftY),
  518. fillAlphaChannelWithCurrentBrush);
  519. }
  520. void Graphics::drawImageWithin (const Image& imageToDraw,
  521. int dx, int dy, int dw, int dh,
  522. RectanglePlacement placementWithinTarget,
  523. const bool fillAlphaChannelWithCurrentBrush) const
  524. {
  525. // passing in a silly number can cause maths problems in rendering!
  526. jassert (areCoordsSensibleNumbers (dx, dy, dw, dh));
  527. if (imageToDraw.isValid())
  528. drawImageTransformed (imageToDraw,
  529. placementWithinTarget.getTransformToFit (imageToDraw.getBounds().toFloat(),
  530. Rectangle<int> (dx, dy, dw, dh).toFloat()),
  531. fillAlphaChannelWithCurrentBrush);
  532. }
  533. void Graphics::drawImage (const Image& imageToDraw,
  534. int dx, int dy, int dw, int dh,
  535. int sx, int sy, int sw, int sh,
  536. const bool fillAlphaChannelWithCurrentBrush) const
  537. {
  538. // passing in a silly number can cause maths problems in rendering!
  539. jassert (areCoordsSensibleNumbers (dx, dy, dw, dh));
  540. jassert (areCoordsSensibleNumbers (sx, sy, sw, sh));
  541. if (imageToDraw.isValid() && context.clipRegionIntersects (Rectangle<int> (dx, dy, dw, dh)))
  542. drawImageTransformed (imageToDraw.getClippedImage (Rectangle<int> (sx, sy, sw, sh)),
  543. AffineTransform::scale (dw / (float) sw, dh / (float) sh)
  544. .translated ((float) dx, (float) dy),
  545. fillAlphaChannelWithCurrentBrush);
  546. }
  547. void Graphics::drawImageTransformed (const Image& imageToDraw,
  548. const AffineTransform& transform,
  549. const bool fillAlphaChannelWithCurrentBrush) const
  550. {
  551. if (imageToDraw.isValid() && ! context.isClipEmpty())
  552. {
  553. if (fillAlphaChannelWithCurrentBrush)
  554. {
  555. context.saveState();
  556. context.clipToImageAlpha (imageToDraw, transform);
  557. fillAll();
  558. context.restoreState();
  559. }
  560. else
  561. {
  562. context.drawImage (imageToDraw, transform);
  563. }
  564. }
  565. }
  566. //==============================================================================
  567. Graphics::ScopedSaveState::ScopedSaveState (Graphics& g)
  568. : context (g)
  569. {
  570. context.saveState();
  571. }
  572. Graphics::ScopedSaveState::~ScopedSaveState()
  573. {
  574. context.restoreState();
  575. }