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.

669 lines
21KB

  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. Rectangle<Type> coordsToRectangle (Type x, Type y, Type w, Type h)
  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 >= -maxVal && (int) w <= maxVal
  27. && (int) h >= -maxVal && (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 (const 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 (Rectangle<int> (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 (const 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 (const 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. && startX < context.getClipBounds().getRight())
  195. {
  196. GlyphArrangement arr;
  197. arr.addLineOfText (context.getFont(), text, (float) startX, (float) baselineY);
  198. // Don't pass any vertical placement flags to this method - they'll be ignored.
  199. jassert (justification.getOnlyVerticalFlags() == 0);
  200. const int flags = justification.getOnlyHorizontalFlags();
  201. if (flags != Justification::left)
  202. {
  203. float w = arr.getBoundingBox (0, -1, true).getWidth();
  204. if ((flags & (Justification::horizontallyCentred | Justification::horizontallyJustified)) != 0)
  205. w /= 2.0f;
  206. arr.draw (*this, AffineTransform::translation (-w, 0));
  207. }
  208. else
  209. {
  210. arr.draw (*this);
  211. }
  212. }
  213. }
  214. void Graphics::drawMultiLineText (const String& text, const int startX,
  215. const int baselineY, const int maximumLineWidth) const
  216. {
  217. if (text.isNotEmpty()
  218. && startX < context.getClipBounds().getRight())
  219. {
  220. GlyphArrangement arr;
  221. arr.addJustifiedText (context.getFont(), text,
  222. (float) startX, (float) baselineY, (float) maximumLineWidth,
  223. Justification::left);
  224. arr.draw (*this);
  225. }
  226. }
  227. void Graphics::drawText (const String& text, const Rectangle<int>& area,
  228. Justification justificationType,
  229. const bool useEllipsesIfTooBig) const
  230. {
  231. if (text.isNotEmpty() && context.clipRegionIntersects (area))
  232. {
  233. GlyphArrangement arr;
  234. arr.addCurtailedLineOfText (context.getFont(), text,
  235. 0.0f, 0.0f, (float) area.getWidth(),
  236. useEllipsesIfTooBig);
  237. arr.justifyGlyphs (0, arr.getNumGlyphs(),
  238. (float) area.getX(), (float) area.getY(),
  239. (float) area.getWidth(), (float) area.getHeight(),
  240. justificationType);
  241. arr.draw (*this);
  242. }
  243. }
  244. void Graphics::drawText (const String& text, const int x, const int y, const int width, const int height,
  245. Justification justificationType,
  246. const bool useEllipsesIfTooBig) const
  247. {
  248. drawText (text, Rectangle<int> (x, y, width, height), justificationType, useEllipsesIfTooBig);
  249. }
  250. void Graphics::drawFittedText (const String& text, const Rectangle<int>& area,
  251. Justification justification,
  252. const int maximumNumberOfLines,
  253. const float minimumHorizontalScale) const
  254. {
  255. if (text.isNotEmpty() && (! area.isEmpty()) && context.clipRegionIntersects (area))
  256. {
  257. GlyphArrangement arr;
  258. arr.addFittedText (context.getFont(), text,
  259. (float) area.getX(), (float) area.getY(),
  260. (float) area.getWidth(), (float) area.getHeight(),
  261. justification,
  262. maximumNumberOfLines,
  263. minimumHorizontalScale);
  264. arr.draw (*this);
  265. }
  266. }
  267. void Graphics::drawFittedText (const String& text, const int x, const int y, const int width, const int height,
  268. Justification justification,
  269. const int maximumNumberOfLines,
  270. const float minimumHorizontalScale) const
  271. {
  272. drawFittedText (text, coordsToRectangle (x, y, width, height),
  273. justification, maximumNumberOfLines, minimumHorizontalScale);
  274. }
  275. //==============================================================================
  276. void Graphics::fillRect (const Rectangle<int>& r) const
  277. {
  278. context.fillRect (r, false);
  279. }
  280. void Graphics::fillRect (const Rectangle<float>& r) const
  281. {
  282. context.fillRect (r);
  283. }
  284. void Graphics::fillRect (int x, int y, int width, int height) const
  285. {
  286. context.fillRect (coordsToRectangle (x, y, width, height), false);
  287. }
  288. void Graphics::fillRect (float x, float y, float width, float height) const
  289. {
  290. fillRect (coordsToRectangle (x, y, width, height));
  291. }
  292. void Graphics::fillRectList (const RectangleList<float>& rectangles) const
  293. {
  294. context.fillRectList (rectangles);
  295. }
  296. void Graphics::fillRectList (const RectangleList<int>& rects) const
  297. {
  298. for (const Rectangle<int>* r = rects.begin(), * const e = rects.end(); r != e; ++r)
  299. context.fillRect (*r, false);
  300. }
  301. void Graphics::setPixel (int x, int y) const
  302. {
  303. context.fillRect (Rectangle<int> (x, y, 1, 1), 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. const Rectangle<int> 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 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 (const 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. RectangleList<float> rects;
  350. rects.addWithoutMerging (r.removeFromTop (lineThickness));
  351. rects.addWithoutMerging (r.removeFromBottom (lineThickness));
  352. rects.addWithoutMerging (r.removeFromLeft (lineThickness));
  353. rects.addWithoutMerging (r.removeFromRight (lineThickness));
  354. context.fillRectList (rects);
  355. }
  356. //==============================================================================
  357. void Graphics::fillEllipse (const Rectangle<float>& area) const
  358. {
  359. fillEllipse (area.getX(), area.getY(), area.getWidth(), area.getHeight());
  360. }
  361. void Graphics::fillEllipse (float x, float y, float width, float height) const
  362. {
  363. Path p;
  364. p.addEllipse (x, y, width, height);
  365. fillPath (p);
  366. }
  367. void Graphics::drawEllipse (float x, float y, float width, float height, float lineThickness) const
  368. {
  369. Path p;
  370. p.addEllipse (x, y, width, height);
  371. strokePath (p, PathStrokeType (lineThickness));
  372. }
  373. void Graphics::drawEllipse (const Rectangle<float>& area, float lineThickness) const
  374. {
  375. drawEllipse (area.getX(), area.getY(), area.getWidth(), area.getHeight(), lineThickness);
  376. }
  377. void Graphics::fillRoundedRectangle (float x, float y, float width, float height, float cornerSize) const
  378. {
  379. fillRoundedRectangle (coordsToRectangle (x, y, width, height), cornerSize);
  380. }
  381. void Graphics::fillRoundedRectangle (const Rectangle<float>& r, const float cornerSize) const
  382. {
  383. Path p;
  384. p.addRoundedRectangle (r, cornerSize);
  385. fillPath (p);
  386. }
  387. void Graphics::drawRoundedRectangle (float x, float y, float width, float height,
  388. float cornerSize, float lineThickness) const
  389. {
  390. drawRoundedRectangle (coordsToRectangle (x, y, width, height), cornerSize, lineThickness);
  391. }
  392. void Graphics::drawRoundedRectangle (const Rectangle<float>& r, float cornerSize, float lineThickness) const
  393. {
  394. Path p;
  395. p.addRoundedRectangle (r, cornerSize);
  396. strokePath (p, PathStrokeType (lineThickness));
  397. }
  398. void Graphics::drawArrow (const Line<float>& line, float lineThickness, float arrowheadWidth, float arrowheadLength) const
  399. {
  400. Path p;
  401. p.addArrow (line, lineThickness, arrowheadWidth, arrowheadLength);
  402. fillPath (p);
  403. }
  404. void Graphics::fillCheckerBoard (const Rectangle<int>& area,
  405. const int checkWidth, const int checkHeight,
  406. Colour colour1, Colour colour2) const
  407. {
  408. jassert (checkWidth > 0 && checkHeight > 0); // can't be zero or less!
  409. if (checkWidth > 0 && checkHeight > 0)
  410. {
  411. context.saveState();
  412. if (colour1 == colour2)
  413. {
  414. context.setFill (colour1);
  415. context.fillRect (area, false);
  416. }
  417. else
  418. {
  419. const Rectangle<int> clipped (context.getClipBounds().getIntersection (area));
  420. if (! clipped.isEmpty())
  421. {
  422. context.clipToRectangle (clipped);
  423. const int checkNumX = (clipped.getX() - area.getX()) / checkWidth;
  424. const int checkNumY = (clipped.getY() - area.getY()) / checkHeight;
  425. const int startX = area.getX() + checkNumX * checkWidth;
  426. const int startY = area.getY() + checkNumY * checkHeight;
  427. const int right = clipped.getRight();
  428. const int bottom = clipped.getBottom();
  429. for (int i = 0; i < 2; ++i)
  430. {
  431. context.setFill (i == ((checkNumX ^ checkNumY) & 1) ? colour1 : colour2);
  432. int cy = i;
  433. for (int y = startY; y < bottom; y += checkHeight)
  434. for (int x = startX + (cy++ & 1) * checkWidth; x < right; x += checkWidth * 2)
  435. context.fillRect (Rectangle<int> (x, y, checkWidth, checkHeight), false);
  436. }
  437. }
  438. }
  439. context.restoreState();
  440. }
  441. }
  442. //==============================================================================
  443. void Graphics::drawVerticalLine (const int x, float top, float bottom) const
  444. {
  445. if (top < bottom)
  446. context.fillRect (Rectangle<float> ((float) x, top, 1.0f, bottom - top));
  447. }
  448. void Graphics::drawHorizontalLine (const int y, float left, float right) const
  449. {
  450. if (left < right)
  451. context.fillRect (Rectangle<float> (left, (float) y, right - left, 1.0f));
  452. }
  453. void Graphics::drawLine (const Line<float>& line) const
  454. {
  455. context.drawLine (line);
  456. }
  457. void Graphics::drawLine (float x1, float y1, float x2, float y2) const
  458. {
  459. context.drawLine (Line<float> (x1, y1, x2, y2));
  460. }
  461. void Graphics::drawLine (float x1, float y1, float x2, float y2, float lineThickness) const
  462. {
  463. drawLine (Line<float> (x1, y1, x2, y2), lineThickness);
  464. }
  465. void Graphics::drawLine (const Line<float>& line, const float lineThickness) const
  466. {
  467. Path p;
  468. p.addLineSegment (line, lineThickness);
  469. fillPath (p);
  470. }
  471. void Graphics::drawDashedLine (const Line<float>& line, const float* const dashLengths,
  472. const int numDashLengths, const float lineThickness, int n) const
  473. {
  474. jassert (n >= 0 && n < numDashLengths); // your start index must be valid!
  475. const Point<double> delta ((line.getEnd() - line.getStart()).toDouble());
  476. const double totalLen = delta.getDistanceFromOrigin();
  477. if (totalLen >= 0.1)
  478. {
  479. const double onePixAlpha = 1.0 / totalLen;
  480. for (double alpha = 0.0; alpha < 1.0;)
  481. {
  482. jassert (dashLengths[n] > 0); // can't have zero-length dashes!
  483. const double lastAlpha = alpha;
  484. alpha += dashLengths [n] * onePixAlpha;
  485. n = (n + 1) % numDashLengths;
  486. if ((n & 1) != 0)
  487. {
  488. const Line<float> segment (line.getStart() + (delta * lastAlpha).toFloat(),
  489. line.getStart() + (delta * jmin (1.0, alpha)).toFloat());
  490. if (lineThickness != 1.0f)
  491. drawLine (segment, lineThickness);
  492. else
  493. context.drawLine (segment);
  494. }
  495. }
  496. }
  497. }
  498. //==============================================================================
  499. void Graphics::setImageResamplingQuality (const Graphics::ResamplingQuality newQuality)
  500. {
  501. saveStateIfPending();
  502. context.setInterpolationQuality (newQuality);
  503. }
  504. //==============================================================================
  505. void Graphics::drawImageAt (const Image& imageToDraw, int x, int y, bool fillAlphaChannel) const
  506. {
  507. drawImageTransformed (imageToDraw,
  508. AffineTransform::translation ((float) x, (float) y),
  509. fillAlphaChannel);
  510. }
  511. void Graphics::drawImageWithin (const Image& imageToDraw,
  512. int dx, int dy, int dw, int dh,
  513. RectanglePlacement placementWithinTarget,
  514. const bool fillAlphaChannelWithCurrentBrush) const
  515. {
  516. if (imageToDraw.isValid())
  517. drawImageTransformed (imageToDraw,
  518. placementWithinTarget.getTransformToFit (imageToDraw.getBounds().toFloat(),
  519. coordsToRectangle (dx, dy, dw, dh).toFloat()),
  520. fillAlphaChannelWithCurrentBrush);
  521. }
  522. void Graphics::drawImage (const Image& imageToDraw,
  523. int dx, int dy, int dw, int dh,
  524. int sx, int sy, int sw, int sh,
  525. const bool fillAlphaChannelWithCurrentBrush) const
  526. {
  527. if (imageToDraw.isValid() && context.clipRegionIntersects (coordsToRectangle (dx, dy, dw, dh)))
  528. drawImageTransformed (imageToDraw.getClippedImage (coordsToRectangle (sx, sy, sw, sh)),
  529. AffineTransform::scale (dw / (float) sw, dh / (float) sh)
  530. .translated ((float) dx, (float) dy),
  531. fillAlphaChannelWithCurrentBrush);
  532. }
  533. void Graphics::drawImageTransformed (const Image& imageToDraw,
  534. const AffineTransform& transform,
  535. const bool fillAlphaChannelWithCurrentBrush) const
  536. {
  537. if (imageToDraw.isValid() && ! context.isClipEmpty())
  538. {
  539. if (fillAlphaChannelWithCurrentBrush)
  540. {
  541. context.saveState();
  542. context.clipToImageAlpha (imageToDraw, transform);
  543. fillAll();
  544. context.restoreState();
  545. }
  546. else
  547. {
  548. context.drawImage (imageToDraw, transform);
  549. }
  550. }
  551. }
  552. //==============================================================================
  553. Graphics::ScopedSaveState::ScopedSaveState (Graphics& g) : context (g)
  554. {
  555. context.saveState();
  556. }
  557. Graphics::ScopedSaveState::~ScopedSaveState()
  558. {
  559. context.restoreState();
  560. }