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.

868 lines
29KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. struct GraphicsFontHelpers
  21. {
  22. static auto compareFont (const Font& a, const Font& b) { return Font::compare (a, b); }
  23. };
  24. static auto operator< (const Font& a, const Font& b)
  25. {
  26. return GraphicsFontHelpers::compareFont (a, b);
  27. }
  28. template <typename T>
  29. static auto operator< (const Rectangle<T>& a, const Rectangle<T>& b)
  30. {
  31. const auto tie = [] (auto& t) { return std::make_tuple (t.getX(), t.getY(), t.getWidth(), t.getHeight()); };
  32. return tie (a) < tie (b);
  33. }
  34. static auto operator< (const Justification& a, const Justification& b)
  35. {
  36. return a.getFlags() < b.getFlags();
  37. }
  38. //==============================================================================
  39. namespace
  40. {
  41. struct ConfiguredArrangement
  42. {
  43. void draw (const Graphics& g) const { arrangement.draw (g, transform); }
  44. GlyphArrangement arrangement;
  45. AffineTransform transform;
  46. };
  47. template <typename ArrangementArgs>
  48. class GlyphArrangementCache final : public DeletedAtShutdown
  49. {
  50. public:
  51. GlyphArrangementCache() = default;
  52. ~GlyphArrangementCache() override
  53. {
  54. clearSingletonInstance();
  55. }
  56. template <typename ConfigureArrangement>
  57. void draw (const Graphics& g, ArrangementArgs&& args, ConfigureArrangement&& configureArrangement)
  58. {
  59. const ScopedTryLock stl (lock);
  60. if (! stl.isLocked())
  61. {
  62. configureArrangement (args).draw (g);
  63. return;
  64. }
  65. const auto cached = [&]
  66. {
  67. const auto iter = cache.find (args);
  68. if (iter != cache.end())
  69. {
  70. if (iter->second.cachePosition != cacheOrder.begin())
  71. cacheOrder.splice (cacheOrder.begin(), cacheOrder, iter->second.cachePosition);
  72. return iter;
  73. }
  74. auto result = cache.emplace (std::move (args), CachedGlyphArrangement { configureArrangement (args), {} }).first;
  75. cacheOrder.push_front (result);
  76. return result;
  77. }();
  78. cached->second.cachePosition = cacheOrder.begin();
  79. cached->second.configured.draw (g);
  80. while (cache.size() > cacheSize)
  81. {
  82. cache.erase (cacheOrder.back());
  83. cacheOrder.pop_back();
  84. }
  85. }
  86. JUCE_DECLARE_SINGLETON (GlyphArrangementCache<ArrangementArgs>, false)
  87. private:
  88. struct CachedGlyphArrangement
  89. {
  90. using CachePtr = typename std::map<ArrangementArgs, CachedGlyphArrangement>::const_iterator;
  91. ConfiguredArrangement configured;
  92. typename std::list<CachePtr>::const_iterator cachePosition;
  93. };
  94. static constexpr size_t cacheSize = 128;
  95. std::map<ArrangementArgs, CachedGlyphArrangement> cache;
  96. std::list<typename CachedGlyphArrangement::CachePtr> cacheOrder;
  97. CriticalSection lock;
  98. };
  99. template <typename ArrangementArgs>
  100. juce::SingletonHolder<GlyphArrangementCache<ArrangementArgs>, juce::CriticalSection, false> GlyphArrangementCache<ArrangementArgs>::singletonHolder;
  101. //==============================================================================
  102. template <typename Type>
  103. Rectangle<Type> coordsToRectangle (Type x, Type y, Type w, Type h) noexcept
  104. {
  105. #if JUCE_DEBUG
  106. const int maxVal = 0x3fffffff;
  107. jassertquiet ((int) x >= -maxVal && (int) x <= maxVal
  108. && (int) y >= -maxVal && (int) y <= maxVal
  109. && (int) w >= 0 && (int) w <= maxVal
  110. && (int) h >= 0 && (int) h <= maxVal);
  111. #endif
  112. return { x, y, w, h };
  113. }
  114. }
  115. //==============================================================================
  116. Graphics::Graphics (const Image& imageToDrawOnto)
  117. : contextHolder (imageToDrawOnto.createLowLevelContext()),
  118. context (*contextHolder)
  119. {
  120. jassert (imageToDrawOnto.isValid()); // Can't draw into a null image!
  121. }
  122. Graphics::Graphics (LowLevelGraphicsContext& internalContext) noexcept
  123. : context (internalContext)
  124. {
  125. }
  126. //==============================================================================
  127. void Graphics::resetToDefaultState()
  128. {
  129. saveStateIfPending();
  130. context.setFill (FillType());
  131. context.setFont (Font());
  132. context.setInterpolationQuality (Graphics::mediumResamplingQuality);
  133. }
  134. bool Graphics::isVectorDevice() const
  135. {
  136. return context.isVectorDevice();
  137. }
  138. bool Graphics::reduceClipRegion (Rectangle<int> area)
  139. {
  140. saveStateIfPending();
  141. return context.clipToRectangle (area);
  142. }
  143. bool Graphics::reduceClipRegion (int x, int y, int w, int h)
  144. {
  145. return reduceClipRegion (coordsToRectangle (x, y, w, h));
  146. }
  147. bool Graphics::reduceClipRegion (const RectangleList<int>& clipRegion)
  148. {
  149. saveStateIfPending();
  150. return context.clipToRectangleList (clipRegion);
  151. }
  152. bool Graphics::reduceClipRegion (const Path& path, const AffineTransform& transform)
  153. {
  154. saveStateIfPending();
  155. context.clipToPath (path, transform);
  156. return ! context.isClipEmpty();
  157. }
  158. bool Graphics::reduceClipRegion (const Image& image, const AffineTransform& transform)
  159. {
  160. saveStateIfPending();
  161. context.clipToImageAlpha (image, transform);
  162. return ! context.isClipEmpty();
  163. }
  164. void Graphics::excludeClipRegion (Rectangle<int> rectangleToExclude)
  165. {
  166. saveStateIfPending();
  167. context.excludeClipRectangle (rectangleToExclude);
  168. }
  169. bool Graphics::isClipEmpty() const
  170. {
  171. return context.isClipEmpty();
  172. }
  173. Rectangle<int> Graphics::getClipBounds() const
  174. {
  175. return context.getClipBounds();
  176. }
  177. void Graphics::saveState()
  178. {
  179. saveStateIfPending();
  180. saveStatePending = true;
  181. }
  182. void Graphics::restoreState()
  183. {
  184. if (saveStatePending)
  185. saveStatePending = false;
  186. else
  187. context.restoreState();
  188. }
  189. void Graphics::saveStateIfPending()
  190. {
  191. if (saveStatePending)
  192. {
  193. saveStatePending = false;
  194. context.saveState();
  195. }
  196. }
  197. void Graphics::setOrigin (Point<int> newOrigin)
  198. {
  199. saveStateIfPending();
  200. context.setOrigin (newOrigin);
  201. }
  202. void Graphics::setOrigin (int x, int y)
  203. {
  204. setOrigin ({ x, y });
  205. }
  206. void Graphics::addTransform (const AffineTransform& transform)
  207. {
  208. saveStateIfPending();
  209. context.addTransform (transform);
  210. }
  211. bool Graphics::clipRegionIntersects (Rectangle<int> area) const
  212. {
  213. return context.clipRegionIntersects (area);
  214. }
  215. void Graphics::beginTransparencyLayer (float layerOpacity)
  216. {
  217. saveStateIfPending();
  218. context.beginTransparencyLayer (layerOpacity);
  219. }
  220. void Graphics::endTransparencyLayer()
  221. {
  222. context.endTransparencyLayer();
  223. }
  224. //==============================================================================
  225. void Graphics::setColour (Colour newColour)
  226. {
  227. saveStateIfPending();
  228. context.setFill (newColour);
  229. }
  230. void Graphics::setOpacity (float newOpacity)
  231. {
  232. saveStateIfPending();
  233. context.setOpacity (newOpacity);
  234. }
  235. void Graphics::setGradientFill (const ColourGradient& gradient)
  236. {
  237. setFillType (gradient);
  238. }
  239. void Graphics::setGradientFill (ColourGradient&& gradient)
  240. {
  241. setFillType (std::move (gradient));
  242. }
  243. void Graphics::setTiledImageFill (const Image& imageToUse, const int anchorX, const int anchorY, const float opacity)
  244. {
  245. saveStateIfPending();
  246. context.setFill (FillType (imageToUse, AffineTransform::translation ((float) anchorX, (float) anchorY)));
  247. context.setOpacity (opacity);
  248. }
  249. void Graphics::setFillType (const FillType& newFill)
  250. {
  251. saveStateIfPending();
  252. context.setFill (newFill);
  253. }
  254. //==============================================================================
  255. void Graphics::setFont (const Font& newFont)
  256. {
  257. saveStateIfPending();
  258. context.setFont (newFont);
  259. }
  260. void Graphics::setFont (const float newFontHeight)
  261. {
  262. setFont (context.getFont().withHeight (newFontHeight));
  263. }
  264. Font Graphics::getCurrentFont() const
  265. {
  266. return context.getFont();
  267. }
  268. //==============================================================================
  269. void Graphics::drawSingleLineText (const String& text, const int startX, const int baselineY,
  270. Justification justification) const
  271. {
  272. if (text.isEmpty())
  273. return;
  274. // Don't pass any vertical placement flags to this method - they'll be ignored.
  275. jassert (justification.getOnlyVerticalFlags() == 0);
  276. auto flags = justification.getOnlyHorizontalFlags();
  277. if (flags == Justification::right && startX < context.getClipBounds().getX())
  278. return;
  279. if (flags == Justification::left && startX > context.getClipBounds().getRight())
  280. return;
  281. struct ArrangementArgs
  282. {
  283. auto tie() const noexcept { return std::tie (font, text, startX, baselineY); }
  284. bool operator< (const ArrangementArgs& other) const { return tie() < other.tie(); }
  285. const Font font;
  286. const String text;
  287. const int startX, baselineY, flags;
  288. };
  289. auto configureArrangement = [] (const ArrangementArgs& args)
  290. {
  291. AffineTransform transform;
  292. GlyphArrangement arrangement;
  293. arrangement.addLineOfText (args.font, args.text, (float) args.startX, (float) args.baselineY);
  294. if (args.flags != Justification::left)
  295. {
  296. auto w = arrangement.getBoundingBox (0, -1, true).getWidth();
  297. if ((args.flags & (Justification::horizontallyCentred | Justification::horizontallyJustified)) != 0)
  298. w /= 2.0f;
  299. transform = AffineTransform::translation (-w, 0);
  300. }
  301. return ConfiguredArrangement { std::move (arrangement), std::move (transform) };
  302. };
  303. GlyphArrangementCache<ArrangementArgs>::getInstance()->draw (*this,
  304. { context.getFont(), text, startX, baselineY, flags },
  305. std::move (configureArrangement));
  306. }
  307. void Graphics::drawMultiLineText (const String& text, const int startX,
  308. const int baselineY, const int maximumLineWidth,
  309. Justification justification, const float leading) const
  310. {
  311. if (text.isEmpty() || startX >= context.getClipBounds().getRight())
  312. return;
  313. struct ArrangementArgs
  314. {
  315. auto tie() const noexcept { return std::tie (font, text, startX, baselineY, maximumLineWidth, justification, leading); }
  316. bool operator< (const ArrangementArgs& other) const { return tie() < other.tie(); }
  317. const Font font;
  318. const String text;
  319. const int startX, baselineY, maximumLineWidth;
  320. const Justification justification;
  321. const float leading;
  322. };
  323. auto configureArrangement = [] (const ArrangementArgs& args)
  324. {
  325. GlyphArrangement arrangement;
  326. arrangement.addJustifiedText (args.font, args.text,
  327. (float) args.startX, (float) args.baselineY, (float) args.maximumLineWidth,
  328. args.justification, args.leading);
  329. return ConfiguredArrangement { std::move (arrangement), {} };
  330. };
  331. GlyphArrangementCache<ArrangementArgs>::getInstance()->draw (*this,
  332. { context.getFont(), text, startX, baselineY, maximumLineWidth, justification, leading },
  333. std::move (configureArrangement));
  334. }
  335. void Graphics::drawText (const String& text, Rectangle<float> area,
  336. Justification justificationType, bool useEllipsesIfTooBig) const
  337. {
  338. if (text.isEmpty() || ! context.clipRegionIntersects (area.getSmallestIntegerContainer()))
  339. return;
  340. struct ArrangementArgs
  341. {
  342. auto tie() const noexcept { return std::tie (font, text, area, justificationType, useEllipsesIfTooBig); }
  343. bool operator< (const ArrangementArgs& other) const { return tie() < other.tie(); }
  344. const Font font;
  345. const String text;
  346. const Rectangle<float> area;
  347. const Justification justificationType;
  348. const bool useEllipsesIfTooBig;
  349. };
  350. auto configureArrangement = [] (const ArrangementArgs& args)
  351. {
  352. GlyphArrangement arrangement;
  353. arrangement.addCurtailedLineOfText (args.font, args.text, 0.0f, 0.0f,
  354. args.area.getWidth(), args.useEllipsesIfTooBig);
  355. arrangement.justifyGlyphs (0, arrangement.getNumGlyphs(),
  356. args.area.getX(), args.area.getY(), args.area.getWidth(), args.area.getHeight(),
  357. args.justificationType);
  358. return ConfiguredArrangement { std::move (arrangement), {} };
  359. };
  360. GlyphArrangementCache<ArrangementArgs>::getInstance()->draw (*this,
  361. { context.getFont(), text, area, justificationType, useEllipsesIfTooBig },
  362. std::move (configureArrangement));
  363. }
  364. void Graphics::drawText (const String& text, Rectangle<int> area,
  365. Justification justificationType, bool useEllipsesIfTooBig) const
  366. {
  367. drawText (text, area.toFloat(), justificationType, useEllipsesIfTooBig);
  368. }
  369. void Graphics::drawText (const String& text, int x, int y, int width, int height,
  370. Justification justificationType, const bool useEllipsesIfTooBig) const
  371. {
  372. drawText (text, coordsToRectangle (x, y, width, height), justificationType, useEllipsesIfTooBig);
  373. }
  374. void Graphics::drawFittedText (const String& text, Rectangle<int> area,
  375. Justification justification,
  376. const int maximumNumberOfLines,
  377. const float minimumHorizontalScale) const
  378. {
  379. if (text.isEmpty() || area.isEmpty() || ! context.clipRegionIntersects (area))
  380. return;
  381. struct ArrangementArgs
  382. {
  383. auto tie() const noexcept { return std::tie (font, text, area, justification, maximumNumberOfLines, minimumHorizontalScale); }
  384. bool operator< (const ArrangementArgs& other) const noexcept { return tie() < other.tie(); }
  385. const Font font;
  386. const String text;
  387. const Rectangle<float> area;
  388. const Justification justification;
  389. const int maximumNumberOfLines;
  390. const float minimumHorizontalScale;
  391. };
  392. auto configureArrangement = [] (const ArrangementArgs& args)
  393. {
  394. GlyphArrangement arrangement;
  395. arrangement.addFittedText (args.font, args.text,
  396. args.area.getX(), args.area.getY(),
  397. args.area.getWidth(), args.area.getHeight(),
  398. args.justification,
  399. args.maximumNumberOfLines,
  400. args.minimumHorizontalScale);
  401. return ConfiguredArrangement { std::move (arrangement), {} };
  402. };
  403. GlyphArrangementCache<ArrangementArgs>::getInstance()->draw (*this,
  404. { context.getFont(), text, area.toFloat(), justification, maximumNumberOfLines, minimumHorizontalScale },
  405. std::move (configureArrangement));
  406. }
  407. void Graphics::drawFittedText (const String& text, int x, int y, int width, int height,
  408. Justification justification,
  409. const int maximumNumberOfLines,
  410. const float minimumHorizontalScale) const
  411. {
  412. drawFittedText (text, coordsToRectangle (x, y, width, height),
  413. justification, maximumNumberOfLines, minimumHorizontalScale);
  414. }
  415. //==============================================================================
  416. void Graphics::fillRect (Rectangle<int> r) const
  417. {
  418. context.fillRect (r, false);
  419. }
  420. void Graphics::fillRect (Rectangle<float> r) const
  421. {
  422. context.fillRect (r);
  423. }
  424. void Graphics::fillRect (int x, int y, int width, int height) const
  425. {
  426. context.fillRect (coordsToRectangle (x, y, width, height), false);
  427. }
  428. void Graphics::fillRect (float x, float y, float width, float height) const
  429. {
  430. fillRect (coordsToRectangle (x, y, width, height));
  431. }
  432. void Graphics::fillRectList (const RectangleList<float>& rectangles) const
  433. {
  434. context.fillRectList (rectangles);
  435. }
  436. void Graphics::fillRectList (const RectangleList<int>& rects) const
  437. {
  438. for (auto& r : rects)
  439. context.fillRect (r, false);
  440. }
  441. void Graphics::fillAll() const
  442. {
  443. context.fillAll();
  444. }
  445. void Graphics::fillAll (Colour colourToUse) const
  446. {
  447. if (! colourToUse.isTransparent())
  448. {
  449. context.saveState();
  450. context.setFill (colourToUse);
  451. context.fillAll();
  452. context.restoreState();
  453. }
  454. }
  455. //==============================================================================
  456. void Graphics::fillPath (const Path& path) const
  457. {
  458. if (! (context.isClipEmpty() || path.isEmpty()))
  459. context.fillPath (path, AffineTransform());
  460. }
  461. void Graphics::fillPath (const Path& path, const AffineTransform& transform) const
  462. {
  463. if (! (context.isClipEmpty() || path.isEmpty()))
  464. context.fillPath (path, transform);
  465. }
  466. void Graphics::strokePath (const Path& path,
  467. const PathStrokeType& strokeType,
  468. const AffineTransform& transform) const
  469. {
  470. Path stroke;
  471. strokeType.createStrokedPath (stroke, path, transform, context.getPhysicalPixelScaleFactor());
  472. fillPath (stroke);
  473. }
  474. //==============================================================================
  475. void Graphics::drawRect (float x, float y, float width, float height, float lineThickness) const
  476. {
  477. drawRect (coordsToRectangle (x, y, width, height), lineThickness);
  478. }
  479. void Graphics::drawRect (int x, int y, int width, int height, int lineThickness) const
  480. {
  481. drawRect (coordsToRectangle (x, y, width, height), lineThickness);
  482. }
  483. void Graphics::drawRect (Rectangle<int> r, int lineThickness) const
  484. {
  485. drawRect (r.toFloat(), (float) lineThickness);
  486. }
  487. void Graphics::drawRect (Rectangle<float> r, const float lineThickness) const
  488. {
  489. jassert (r.getWidth() >= 0.0f && r.getHeight() >= 0.0f);
  490. RectangleList<float> rects;
  491. rects.addWithoutMerging (r.removeFromTop (lineThickness));
  492. rects.addWithoutMerging (r.removeFromBottom (lineThickness));
  493. rects.addWithoutMerging (r.removeFromLeft (lineThickness));
  494. rects.addWithoutMerging (r.removeFromRight (lineThickness));
  495. context.fillRectList (rects);
  496. }
  497. //==============================================================================
  498. void Graphics::fillEllipse (Rectangle<float> area) const
  499. {
  500. Path p;
  501. p.addEllipse (area);
  502. fillPath (p);
  503. }
  504. void Graphics::fillEllipse (float x, float y, float w, float h) const
  505. {
  506. fillEllipse (coordsToRectangle (x, y, w, h));
  507. }
  508. void Graphics::drawEllipse (float x, float y, float width, float height, float lineThickness) const
  509. {
  510. drawEllipse (coordsToRectangle (x, y, width, height), lineThickness);
  511. }
  512. void Graphics::drawEllipse (Rectangle<float> area, float lineThickness) const
  513. {
  514. Path p;
  515. if (area.getWidth() == area.getHeight())
  516. {
  517. // For a circle, we can avoid having to generate a stroke
  518. p.addEllipse (area.expanded (lineThickness * 0.5f));
  519. p.addEllipse (area.reduced (lineThickness * 0.5f));
  520. p.setUsingNonZeroWinding (false);
  521. fillPath (p);
  522. }
  523. else
  524. {
  525. p.addEllipse (area);
  526. strokePath (p, PathStrokeType (lineThickness));
  527. }
  528. }
  529. void Graphics::fillRoundedRectangle (float x, float y, float width, float height, float cornerSize) const
  530. {
  531. fillRoundedRectangle (coordsToRectangle (x, y, width, height), cornerSize);
  532. }
  533. void Graphics::fillRoundedRectangle (Rectangle<float> r, const float cornerSize) const
  534. {
  535. Path p;
  536. p.addRoundedRectangle (r, cornerSize);
  537. fillPath (p);
  538. }
  539. void Graphics::drawRoundedRectangle (float x, float y, float width, float height,
  540. float cornerSize, float lineThickness) const
  541. {
  542. drawRoundedRectangle (coordsToRectangle (x, y, width, height), cornerSize, lineThickness);
  543. }
  544. void Graphics::drawRoundedRectangle (Rectangle<float> r, float cornerSize, float lineThickness) const
  545. {
  546. Path p;
  547. p.addRoundedRectangle (r, cornerSize);
  548. strokePath (p, PathStrokeType (lineThickness));
  549. }
  550. void Graphics::drawArrow (Line<float> line, float lineThickness, float arrowheadWidth, float arrowheadLength) const
  551. {
  552. Path p;
  553. p.addArrow (line, lineThickness, arrowheadWidth, arrowheadLength);
  554. fillPath (p);
  555. }
  556. void Graphics::fillCheckerBoard (Rectangle<float> area, float checkWidth, float checkHeight,
  557. Colour colour1, Colour colour2) const
  558. {
  559. jassert (checkWidth > 0 && checkHeight > 0); // can't be zero or less!
  560. if (checkWidth > 0 && checkHeight > 0)
  561. {
  562. context.saveState();
  563. if (colour1 == colour2)
  564. {
  565. context.setFill (colour1);
  566. context.fillRect (area);
  567. }
  568. else
  569. {
  570. auto clipped = context.getClipBounds().getIntersection (area.getSmallestIntegerContainer());
  571. if (! clipped.isEmpty())
  572. {
  573. const int checkNumX = (int) (((float) clipped.getX() - area.getX()) / checkWidth);
  574. const int checkNumY = (int) (((float) clipped.getY() - area.getY()) / checkHeight);
  575. const float startX = area.getX() + (float) checkNumX * checkWidth;
  576. const float startY = area.getY() + (float) checkNumY * checkHeight;
  577. const float right = (float) clipped.getRight();
  578. const float bottom = (float) clipped.getBottom();
  579. for (int i = 0; i < 2; ++i)
  580. {
  581. int cy = i;
  582. RectangleList<float> checks;
  583. for (float y = startY; y < bottom; y += checkHeight)
  584. for (float x = startX + (cy++ & 1) * checkWidth; x < right; x += checkWidth * 2.0f)
  585. checks.addWithoutMerging ({ x, y, checkWidth, checkHeight });
  586. checks.clipTo (area);
  587. context.setFill (i == ((checkNumX ^ checkNumY) & 1) ? colour1 : colour2);
  588. context.fillRectList (checks);
  589. }
  590. }
  591. }
  592. context.restoreState();
  593. }
  594. }
  595. //==============================================================================
  596. void Graphics::drawVerticalLine (const int x, float top, float bottom) const
  597. {
  598. if (top < bottom)
  599. context.fillRect (Rectangle<float> ((float) x, top, 1.0f, bottom - top));
  600. }
  601. void Graphics::drawHorizontalLine (const int y, float left, float right) const
  602. {
  603. if (left < right)
  604. context.fillRect (Rectangle<float> (left, (float) y, right - left, 1.0f));
  605. }
  606. void Graphics::drawLine (Line<float> line) const
  607. {
  608. context.drawLine (line);
  609. }
  610. void Graphics::drawLine (float x1, float y1, float x2, float y2) const
  611. {
  612. context.drawLine (Line<float> (x1, y1, x2, y2));
  613. }
  614. void Graphics::drawLine (float x1, float y1, float x2, float y2, float lineThickness) const
  615. {
  616. drawLine (Line<float> (x1, y1, x2, y2), lineThickness);
  617. }
  618. void Graphics::drawLine (Line<float> line, const float lineThickness) const
  619. {
  620. Path p;
  621. p.addLineSegment (line, lineThickness);
  622. fillPath (p);
  623. }
  624. void Graphics::drawDashedLine (Line<float> line, const float* dashLengths,
  625. int numDashLengths, float lineThickness, int n) const
  626. {
  627. jassert (n >= 0 && n < numDashLengths); // your start index must be valid!
  628. const Point<double> delta ((line.getEnd() - line.getStart()).toDouble());
  629. const double totalLen = delta.getDistanceFromOrigin();
  630. if (totalLen >= 0.1)
  631. {
  632. const double onePixAlpha = 1.0 / totalLen;
  633. for (double alpha = 0.0; alpha < 1.0;)
  634. {
  635. jassert (dashLengths[n] > 0); // can't have zero-length dashes!
  636. const double lastAlpha = alpha;
  637. alpha += dashLengths [n] * onePixAlpha;
  638. n = (n + 1) % numDashLengths;
  639. if ((n & 1) != 0)
  640. {
  641. const Line<float> segment (line.getStart() + (delta * lastAlpha).toFloat(),
  642. line.getStart() + (delta * jmin (1.0, alpha)).toFloat());
  643. if (lineThickness != 1.0f)
  644. drawLine (segment, lineThickness);
  645. else
  646. context.drawLine (segment);
  647. }
  648. }
  649. }
  650. }
  651. //==============================================================================
  652. void Graphics::setImageResamplingQuality (const Graphics::ResamplingQuality newQuality)
  653. {
  654. saveStateIfPending();
  655. context.setInterpolationQuality (newQuality);
  656. }
  657. //==============================================================================
  658. void Graphics::drawImageAt (const Image& imageToDraw, int x, int y, bool fillAlphaChannel) const
  659. {
  660. drawImageTransformed (imageToDraw,
  661. AffineTransform::translation ((float) x, (float) y),
  662. fillAlphaChannel);
  663. }
  664. void Graphics::drawImage (const Image& imageToDraw, Rectangle<float> targetArea,
  665. RectanglePlacement placementWithinTarget, bool fillAlphaChannelWithCurrentBrush) const
  666. {
  667. if (imageToDraw.isValid())
  668. drawImageTransformed (imageToDraw,
  669. placementWithinTarget.getTransformToFit (imageToDraw.getBounds().toFloat(), targetArea),
  670. fillAlphaChannelWithCurrentBrush);
  671. }
  672. void Graphics::drawImageWithin (const Image& imageToDraw, int dx, int dy, int dw, int dh,
  673. RectanglePlacement placementWithinTarget, bool fillAlphaChannelWithCurrentBrush) const
  674. {
  675. drawImage (imageToDraw, coordsToRectangle (dx, dy, dw, dh).toFloat(),
  676. placementWithinTarget, fillAlphaChannelWithCurrentBrush);
  677. }
  678. void Graphics::drawImage (const Image& imageToDraw,
  679. int dx, int dy, int dw, int dh,
  680. int sx, int sy, int sw, int sh,
  681. const bool fillAlphaChannelWithCurrentBrush) const
  682. {
  683. if (imageToDraw.isValid() && context.clipRegionIntersects (coordsToRectangle (dx, dy, dw, dh)))
  684. drawImageTransformed (imageToDraw.getClippedImage (coordsToRectangle (sx, sy, sw, sh)),
  685. AffineTransform::scale ((float) dw / (float) sw, (float) dh / (float) sh)
  686. .translated ((float) dx, (float) dy),
  687. fillAlphaChannelWithCurrentBrush);
  688. }
  689. void Graphics::drawImageTransformed (const Image& imageToDraw,
  690. const AffineTransform& transform,
  691. const bool fillAlphaChannelWithCurrentBrush) const
  692. {
  693. if (imageToDraw.isValid() && ! context.isClipEmpty())
  694. {
  695. if (fillAlphaChannelWithCurrentBrush)
  696. {
  697. context.saveState();
  698. context.clipToImageAlpha (imageToDraw, transform);
  699. fillAll();
  700. context.restoreState();
  701. }
  702. else
  703. {
  704. context.drawImage (imageToDraw, transform);
  705. }
  706. }
  707. }
  708. //==============================================================================
  709. Graphics::ScopedSaveState::ScopedSaveState (Graphics& g) : context (g)
  710. {
  711. context.saveState();
  712. }
  713. Graphics::ScopedSaveState::~ScopedSaveState()
  714. {
  715. context.restoreState();
  716. }
  717. } // namespace juce