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.

2600 lines
100KB

  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. #ifndef JUCE_RENDERINGHELPERS_H_INCLUDED
  18. #define JUCE_RENDERINGHELPERS_H_INCLUDED
  19. #if JUCE_MSVC
  20. #pragma warning (push)
  21. #pragma warning (disable: 4127) // "expression is constant" warning
  22. #endif
  23. namespace RenderingHelpers
  24. {
  25. //==============================================================================
  26. /** Holds either a simple integer translation, or an affine transform.
  27. */
  28. class TranslationOrTransform
  29. {
  30. public:
  31. TranslationOrTransform (int x, int y) noexcept
  32. : xOffset (x), yOffset (y), isOnlyTranslated (true), isRotated (false)
  33. {
  34. }
  35. TranslationOrTransform (const TranslationOrTransform& other) noexcept
  36. : complexTransform (other.complexTransform),
  37. xOffset (other.xOffset), yOffset (other.yOffset),
  38. isOnlyTranslated (other.isOnlyTranslated), isRotated (other.isRotated)
  39. {
  40. }
  41. AffineTransform getTransform() const noexcept
  42. {
  43. return isOnlyTranslated ? AffineTransform::translation ((float) xOffset, (float) yOffset)
  44. : complexTransform;
  45. }
  46. AffineTransform getTransformWith (const AffineTransform& userTransform) const noexcept
  47. {
  48. return isOnlyTranslated ? userTransform.translated ((float) xOffset, (float) yOffset)
  49. : userTransform.followedBy (complexTransform);
  50. }
  51. void setOrigin (const int x, const int y) noexcept
  52. {
  53. if (isOnlyTranslated)
  54. {
  55. xOffset += x;
  56. yOffset += y;
  57. }
  58. else
  59. {
  60. complexTransform = AffineTransform::translation ((float) x, (float) y)
  61. .followedBy (complexTransform);
  62. }
  63. }
  64. void addTransform (const AffineTransform& t) noexcept
  65. {
  66. if (isOnlyTranslated && t.isOnlyTranslation())
  67. {
  68. xOffset += (int) t.getTranslationX();
  69. yOffset += (int) t.getTranslationY();
  70. }
  71. else
  72. {
  73. complexTransform = getTransformWith (t);
  74. isOnlyTranslated = false;
  75. isRotated = (complexTransform.mat01 != 0 || complexTransform.mat10 != 0);
  76. }
  77. }
  78. float getScaleFactor() const noexcept
  79. {
  80. return isOnlyTranslated ? 1.0f
  81. : (isRotated ? complexTransform.getScaleFactor()
  82. : jmax (complexTransform.mat00,
  83. complexTransform.mat11));
  84. }
  85. void moveOriginInDeviceSpace (const int dx, const int dy) noexcept
  86. {
  87. if (isOnlyTranslated)
  88. {
  89. xOffset += dx;
  90. yOffset += dy;
  91. }
  92. else
  93. {
  94. complexTransform = complexTransform.translated ((float) dx, (float) dy);
  95. }
  96. }
  97. template <typename Type>
  98. Rectangle<Type> translated (const Rectangle<Type>& r) const noexcept
  99. {
  100. jassert (isOnlyTranslated);
  101. return r.translated (static_cast <Type> (xOffset),
  102. static_cast <Type> (yOffset));
  103. }
  104. template <typename Type>
  105. Rectangle<Type> transformed (const Rectangle<Type>& r) const noexcept
  106. {
  107. return r.transformedBy (complexTransform);
  108. }
  109. template <typename Type>
  110. Rectangle<Type> deviceSpaceToUserSpace (const Rectangle<Type>& r) const noexcept
  111. {
  112. return isOnlyTranslated ? r.translated (-xOffset, -yOffset)
  113. : r.transformedBy (complexTransform.inverted());
  114. }
  115. AffineTransform complexTransform;
  116. int xOffset, yOffset;
  117. bool isOnlyTranslated, isRotated;
  118. };
  119. //==============================================================================
  120. /** Holds a cache of recently-used glyph objects of some type. */
  121. template <class CachedGlyphType, class RenderTargetType>
  122. class GlyphCache : private DeletedAtShutdown
  123. {
  124. public:
  125. GlyphCache()
  126. {
  127. addNewGlyphSlots (120);
  128. }
  129. ~GlyphCache()
  130. {
  131. getSingletonPointer() = nullptr;
  132. }
  133. static GlyphCache& getInstance()
  134. {
  135. GlyphCache*& g = getSingletonPointer();
  136. if (g == nullptr)
  137. g = new GlyphCache();
  138. return *g;
  139. }
  140. //==============================================================================
  141. void drawGlyph (RenderTargetType& target, const Font& font, const int glyphNumber, float x, float y)
  142. {
  143. ++accessCounter;
  144. CachedGlyphType* glyph = nullptr;
  145. const ScopedReadLock srl (lock);
  146. for (int i = glyphs.size(); --i >= 0;)
  147. {
  148. CachedGlyphType* const g = glyphs.getUnchecked (i);
  149. if (g->glyph == glyphNumber && g->font == font)
  150. {
  151. glyph = g;
  152. ++hits;
  153. break;
  154. }
  155. }
  156. if (glyph == nullptr)
  157. {
  158. ++misses;
  159. const ScopedWriteLock swl (lock);
  160. if (hits.value + misses.value > glyphs.size() * 16)
  161. {
  162. if (misses.value * 2 > hits.value)
  163. addNewGlyphSlots (32);
  164. hits.set (0);
  165. misses.set (0);
  166. glyph = glyphs.getLast();
  167. }
  168. else
  169. {
  170. glyph = findLeastRecentlyUsedGlyph();
  171. }
  172. jassert (glyph != nullptr);
  173. glyph->generate (font, glyphNumber);
  174. }
  175. glyph->lastAccessCount = accessCounter.value;
  176. glyph->draw (target, x, y);
  177. }
  178. private:
  179. friend class OwnedArray <CachedGlyphType>;
  180. OwnedArray <CachedGlyphType> glyphs;
  181. Atomic<int> accessCounter, hits, misses;
  182. ReadWriteLock lock;
  183. void addNewGlyphSlots (int num)
  184. {
  185. while (--num >= 0)
  186. glyphs.add (new CachedGlyphType());
  187. }
  188. CachedGlyphType* findLeastRecentlyUsedGlyph() const noexcept
  189. {
  190. CachedGlyphType* oldest = glyphs.getLast();
  191. int oldestCounter = oldest->lastAccessCount;
  192. for (int i = glyphs.size() - 1; --i >= 0;)
  193. {
  194. CachedGlyphType* const glyph = glyphs.getUnchecked(i);
  195. if (glyph->lastAccessCount <= oldestCounter)
  196. {
  197. oldestCounter = glyph->lastAccessCount;
  198. oldest = glyph;
  199. }
  200. }
  201. return oldest;
  202. }
  203. static GlyphCache*& getSingletonPointer() noexcept
  204. {
  205. static GlyphCache* g = nullptr;
  206. return g;
  207. }
  208. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GlyphCache)
  209. };
  210. //==============================================================================
  211. /** Caches a glyph as an edge-table. */
  212. template <class RendererType>
  213. class CachedGlyphEdgeTable
  214. {
  215. public:
  216. CachedGlyphEdgeTable() : glyph (0), lastAccessCount (0) {}
  217. void draw (RendererType& state, float x, const float y) const
  218. {
  219. if (snapToIntegerCoordinate)
  220. x = std::floor (x + 0.5f);
  221. if (edgeTable != nullptr)
  222. state.fillEdgeTable (*edgeTable, x, roundToInt (y));
  223. }
  224. void generate (const Font& newFont, const int glyphNumber)
  225. {
  226. font = newFont;
  227. Typeface* const typeface = newFont.getTypeface();
  228. snapToIntegerCoordinate = typeface->isHinted();
  229. glyph = glyphNumber;
  230. const float fontHeight = font.getHeight();
  231. edgeTable = typeface->getEdgeTableForGlyph (glyphNumber,
  232. AffineTransform::scale (fontHeight * font.getHorizontalScale(), fontHeight)
  233. #if JUCE_MAC || JUCE_IOS
  234. .translated (0.0f, -0.5f)
  235. #endif
  236. );
  237. }
  238. Font font;
  239. int glyph, lastAccessCount;
  240. bool snapToIntegerCoordinate;
  241. private:
  242. ScopedPointer <EdgeTable> edgeTable;
  243. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedGlyphEdgeTable)
  244. };
  245. //==============================================================================
  246. /** Calculates the alpha values and positions for rendering the edges of a
  247. non-pixel-aligned rectangle.
  248. */
  249. struct FloatRectangleRasterisingInfo
  250. {
  251. FloatRectangleRasterisingInfo (const Rectangle<float>& area)
  252. : left (roundToInt (256.0f * area.getX())),
  253. top (roundToInt (256.0f * area.getY())),
  254. right (roundToInt (256.0f * area.getRight())),
  255. bottom (roundToInt (256.0f * area.getBottom()))
  256. {
  257. if ((top >> 8) == (bottom >> 8))
  258. {
  259. topAlpha = bottom - top;
  260. bottomAlpha = 0;
  261. totalTop = top >> 8;
  262. totalBottom = bottom = top = totalTop + 1;
  263. }
  264. else
  265. {
  266. if ((top & 255) == 0)
  267. {
  268. topAlpha = 0;
  269. top = totalTop = (top >> 8);
  270. }
  271. else
  272. {
  273. topAlpha = 255 - (top & 255);
  274. totalTop = (top >> 8);
  275. top = totalTop + 1;
  276. }
  277. bottomAlpha = bottom & 255;
  278. bottom >>= 8;
  279. totalBottom = bottom + (bottomAlpha != 0 ? 1 : 0);
  280. }
  281. if ((left >> 8) == (right >> 8))
  282. {
  283. leftAlpha = right - left;
  284. rightAlpha = 0;
  285. totalLeft = (left >> 8);
  286. totalRight = right = left = totalLeft + 1;
  287. }
  288. else
  289. {
  290. if ((left & 255) == 0)
  291. {
  292. leftAlpha = 0;
  293. left = totalLeft = (left >> 8);
  294. }
  295. else
  296. {
  297. leftAlpha = 255 - (left & 255);
  298. totalLeft = (left >> 8);
  299. left = totalLeft + 1;
  300. }
  301. rightAlpha = right & 255;
  302. right >>= 8;
  303. totalRight = right + (rightAlpha != 0 ? 1 : 0);
  304. }
  305. }
  306. template <class Callback>
  307. void iterate (Callback& callback) const
  308. {
  309. if (topAlpha != 0) callback (totalLeft, totalTop, totalRight - totalLeft, 1, topAlpha);
  310. if (bottomAlpha != 0) callback (totalLeft, bottom, totalRight - totalLeft, 1, bottomAlpha);
  311. if (leftAlpha != 0) callback (totalLeft, totalTop, 1, totalBottom - totalTop, leftAlpha);
  312. if (rightAlpha != 0) callback (right, totalTop, 1, totalBottom - totalTop, rightAlpha);
  313. callback (left, top, right - left, bottom - top, 255);
  314. }
  315. inline bool isOnePixelWide() const noexcept { return right - left == 1 && leftAlpha + rightAlpha == 0; }
  316. inline int getTopLeftCornerAlpha() const noexcept { return (topAlpha * leftAlpha) >> 8; }
  317. inline int getTopRightCornerAlpha() const noexcept { return (topAlpha * rightAlpha) >> 8; }
  318. inline int getBottomLeftCornerAlpha() const noexcept { return (bottomAlpha * leftAlpha) >> 8; }
  319. inline int getBottomRightCornerAlpha() const noexcept { return (bottomAlpha * rightAlpha) >> 8; }
  320. //==============================================================================
  321. int left, top, right, bottom; // bounds of the solid central area, excluding anti-aliased edges
  322. int totalTop, totalLeft, totalBottom, totalRight; // bounds of the total area, including edges
  323. int topAlpha, leftAlpha, bottomAlpha, rightAlpha; // alpha of each anti-aliased edge
  324. };
  325. //==============================================================================
  326. /** Contains classes for calculating the colour of pixels within various types of gradient. */
  327. namespace GradientPixelIterators
  328. {
  329. /** Iterates the colour of pixels in a linear gradient */
  330. class Linear
  331. {
  332. public:
  333. Linear (const ColourGradient& gradient, const AffineTransform& transform,
  334. const PixelARGB* const colours, const int numColours)
  335. : lookupTable (colours),
  336. numEntries (numColours)
  337. {
  338. jassert (numColours >= 0);
  339. Point<float> p1 (gradient.point1);
  340. Point<float> p2 (gradient.point2);
  341. if (! transform.isIdentity())
  342. {
  343. const Line<float> l (p2, p1);
  344. Point<float> p3 = l.getPointAlongLine (0.0f, 100.0f);
  345. p1.applyTransform (transform);
  346. p2.applyTransform (transform);
  347. p3.applyTransform (transform);
  348. p2 = Line<float> (p2, p3).findNearestPointTo (p1);
  349. }
  350. vertical = std::abs (p1.x - p2.x) < 0.001f;
  351. horizontal = std::abs (p1.y - p2.y) < 0.001f;
  352. if (vertical)
  353. {
  354. scale = roundToInt ((numEntries << (int) numScaleBits) / (double) (p2.y - p1.y));
  355. start = roundToInt (p1.y * scale);
  356. }
  357. else if (horizontal)
  358. {
  359. scale = roundToInt ((numEntries << (int) numScaleBits) / (double) (p2.x - p1.x));
  360. start = roundToInt (p1.x * scale);
  361. }
  362. else
  363. {
  364. grad = (p2.getY() - p1.y) / (double) (p1.x - p2.x);
  365. yTerm = p1.getY() - p1.x / grad;
  366. scale = roundToInt ((numEntries << (int) numScaleBits) / (yTerm * grad - (p2.y * grad - p2.x)));
  367. grad *= scale;
  368. }
  369. }
  370. forcedinline void setY (const int y) noexcept
  371. {
  372. if (vertical)
  373. linePix = lookupTable [jlimit (0, numEntries, (y * scale - start) >> (int) numScaleBits)];
  374. else if (! horizontal)
  375. start = roundToInt ((y - yTerm) * grad);
  376. }
  377. inline PixelARGB getPixel (const int x) const noexcept
  378. {
  379. return vertical ? linePix
  380. : lookupTable [jlimit (0, numEntries, (x * scale - start) >> (int) numScaleBits)];
  381. }
  382. private:
  383. const PixelARGB* const lookupTable;
  384. const int numEntries;
  385. PixelARGB linePix;
  386. int start, scale;
  387. double grad, yTerm;
  388. bool vertical, horizontal;
  389. enum { numScaleBits = 12 };
  390. JUCE_DECLARE_NON_COPYABLE (Linear)
  391. };
  392. //==============================================================================
  393. /** Iterates the colour of pixels in a circular radial gradient */
  394. class Radial
  395. {
  396. public:
  397. Radial (const ColourGradient& gradient, const AffineTransform&,
  398. const PixelARGB* const colours, const int numColours)
  399. : lookupTable (colours),
  400. numEntries (numColours),
  401. gx1 (gradient.point1.x),
  402. gy1 (gradient.point1.y)
  403. {
  404. jassert (numColours >= 0);
  405. const Point<float> diff (gradient.point1 - gradient.point2);
  406. maxDist = diff.x * diff.x + diff.y * diff.y;
  407. invScale = numEntries / std::sqrt (maxDist);
  408. jassert (roundToInt (std::sqrt (maxDist) * invScale) <= numEntries);
  409. }
  410. forcedinline void setY (const int y) noexcept
  411. {
  412. dy = y - gy1;
  413. dy *= dy;
  414. }
  415. inline PixelARGB getPixel (const int px) const noexcept
  416. {
  417. double x = px - gx1;
  418. x *= x;
  419. x += dy;
  420. return lookupTable [x >= maxDist ? numEntries : roundToInt (std::sqrt (x) * invScale)];
  421. }
  422. protected:
  423. const PixelARGB* const lookupTable;
  424. const int numEntries;
  425. const double gx1, gy1;
  426. double maxDist, invScale, dy;
  427. JUCE_DECLARE_NON_COPYABLE (Radial)
  428. };
  429. //==============================================================================
  430. /** Iterates the colour of pixels in a skewed radial gradient */
  431. class TransformedRadial : public Radial
  432. {
  433. public:
  434. TransformedRadial (const ColourGradient& gradient, const AffineTransform& transform,
  435. const PixelARGB* const colours, const int numColours)
  436. : Radial (gradient, transform, colours, numColours),
  437. inverseTransform (transform.inverted())
  438. {
  439. tM10 = inverseTransform.mat10;
  440. tM00 = inverseTransform.mat00;
  441. }
  442. forcedinline void setY (const int y) noexcept
  443. {
  444. lineYM01 = inverseTransform.mat01 * y + inverseTransform.mat02 - gx1;
  445. lineYM11 = inverseTransform.mat11 * y + inverseTransform.mat12 - gy1;
  446. }
  447. inline PixelARGB getPixel (const int px) const noexcept
  448. {
  449. double x = px;
  450. const double y = tM10 * x + lineYM11;
  451. x = tM00 * x + lineYM01;
  452. x *= x;
  453. x += y * y;
  454. if (x >= maxDist)
  455. return lookupTable [numEntries];
  456. return lookupTable [jmin (numEntries, roundToInt (std::sqrt (x) * invScale))];
  457. }
  458. private:
  459. double tM10, tM00, lineYM01, lineYM11;
  460. const AffineTransform inverseTransform;
  461. JUCE_DECLARE_NON_COPYABLE (TransformedRadial)
  462. };
  463. }
  464. #define JUCE_PERFORM_PIXEL_OP_LOOP(op) \
  465. { \
  466. const int destStride = destData.pixelStride; \
  467. do { dest->op; dest = addBytesToPointer (dest, destStride); } while (--width > 0); \
  468. }
  469. //==============================================================================
  470. /** Contains classes for filling edge tables with various fill types. */
  471. namespace EdgeTableFillers
  472. {
  473. /** Fills an edge-table with a solid colour. */
  474. template <class PixelType, bool replaceExisting = false>
  475. class SolidColour
  476. {
  477. public:
  478. SolidColour (const Image::BitmapData& image, const PixelARGB colour)
  479. : destData (image), sourceColour (colour)
  480. {
  481. if (sizeof (PixelType) == 3 && destData.pixelStride == sizeof (PixelType))
  482. {
  483. areRGBComponentsEqual = sourceColour.getRed() == sourceColour.getGreen()
  484. && sourceColour.getGreen() == sourceColour.getBlue();
  485. filler[0].set (sourceColour);
  486. filler[1].set (sourceColour);
  487. filler[2].set (sourceColour);
  488. filler[3].set (sourceColour);
  489. }
  490. }
  491. forcedinline void setEdgeTableYPos (const int y) noexcept
  492. {
  493. linePixels = (PixelType*) destData.getLinePointer (y);
  494. }
  495. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) const noexcept
  496. {
  497. if (replaceExisting)
  498. getPixel (x)->set (sourceColour);
  499. else
  500. getPixel (x)->blend (sourceColour, (uint32) alphaLevel);
  501. }
  502. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  503. {
  504. if (replaceExisting)
  505. getPixel (x)->set (sourceColour);
  506. else
  507. getPixel (x)->blend (sourceColour);
  508. }
  509. forcedinline void handleEdgeTableLine (const int x, const int width, const int alphaLevel) const noexcept
  510. {
  511. PixelARGB p (sourceColour);
  512. p.multiplyAlpha (alphaLevel);
  513. PixelType* dest = getPixel (x);
  514. if (replaceExisting || p.getAlpha() >= 0xff)
  515. replaceLine (dest, p, width);
  516. else
  517. blendLine (dest, p, width);
  518. }
  519. forcedinline void handleEdgeTableLineFull (const int x, const int width) const noexcept
  520. {
  521. PixelType* dest = getPixel (x);
  522. if (replaceExisting || sourceColour.getAlpha() >= 0xff)
  523. replaceLine (dest, sourceColour, width);
  524. else
  525. blendLine (dest, sourceColour, width);
  526. }
  527. private:
  528. const Image::BitmapData& destData;
  529. PixelType* linePixels;
  530. PixelARGB sourceColour;
  531. PixelRGB filler [4];
  532. bool areRGBComponentsEqual;
  533. forcedinline PixelType* getPixel (const int x) const noexcept
  534. {
  535. return addBytesToPointer (linePixels, x * destData.pixelStride);
  536. }
  537. inline void blendLine (PixelType* dest, const PixelARGB colour, int width) const noexcept
  538. {
  539. JUCE_PERFORM_PIXEL_OP_LOOP (blend (colour))
  540. }
  541. forcedinline void replaceLine (PixelRGB* dest, const PixelARGB colour, int width) const noexcept
  542. {
  543. if (destData.pixelStride == sizeof (*dest))
  544. {
  545. if (areRGBComponentsEqual) // if all the component values are the same, we can cheat..
  546. {
  547. memset (dest, colour.getRed(), (size_t) width * 3);
  548. }
  549. else
  550. {
  551. if (width >> 5)
  552. {
  553. const int* const intFiller = reinterpret_cast<const int*> (filler);
  554. while (width > 8 && (((pointer_sized_int) dest) & 7) != 0)
  555. {
  556. dest->set (colour);
  557. ++dest;
  558. --width;
  559. }
  560. while (width > 4)
  561. {
  562. int* d = reinterpret_cast<int*> (dest);
  563. *d++ = intFiller[0];
  564. *d++ = intFiller[1];
  565. *d++ = intFiller[2];
  566. dest = reinterpret_cast<PixelRGB*> (d);
  567. width -= 4;
  568. }
  569. }
  570. while (--width >= 0)
  571. {
  572. dest->set (colour);
  573. ++dest;
  574. }
  575. }
  576. }
  577. else
  578. {
  579. JUCE_PERFORM_PIXEL_OP_LOOP (set (colour))
  580. }
  581. }
  582. forcedinline void replaceLine (PixelAlpha* dest, const PixelARGB colour, int width) const noexcept
  583. {
  584. if (destData.pixelStride == sizeof (*dest))
  585. memset (dest, colour.getAlpha(), (size_t) width);
  586. else
  587. JUCE_PERFORM_PIXEL_OP_LOOP (setAlpha (colour.getAlpha()))
  588. }
  589. forcedinline void replaceLine (PixelARGB* dest, const PixelARGB colour, int width) const noexcept
  590. {
  591. JUCE_PERFORM_PIXEL_OP_LOOP (set (colour))
  592. }
  593. JUCE_DECLARE_NON_COPYABLE (SolidColour)
  594. };
  595. //==============================================================================
  596. /** Fills an edge-table with a gradient. */
  597. template <class PixelType, class GradientType>
  598. class Gradient : public GradientType
  599. {
  600. public:
  601. Gradient (const Image::BitmapData& dest, const ColourGradient& gradient, const AffineTransform& transform,
  602. const PixelARGB* const colours, const int numColours)
  603. : GradientType (gradient, transform, colours, numColours - 1),
  604. destData (dest)
  605. {
  606. }
  607. forcedinline void setEdgeTableYPos (const int y) noexcept
  608. {
  609. linePixels = (PixelType*) destData.getLinePointer (y);
  610. GradientType::setY (y);
  611. }
  612. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) const noexcept
  613. {
  614. getPixel (x)->blend (GradientType::getPixel (x), (uint32) alphaLevel);
  615. }
  616. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  617. {
  618. getPixel (x)->blend (GradientType::getPixel (x));
  619. }
  620. void handleEdgeTableLine (int x, int width, const int alphaLevel) const noexcept
  621. {
  622. PixelType* dest = getPixel (x);
  623. if (alphaLevel < 0xff)
  624. JUCE_PERFORM_PIXEL_OP_LOOP (blend (GradientType::getPixel (x++), (uint32) alphaLevel))
  625. else
  626. JUCE_PERFORM_PIXEL_OP_LOOP (blend (GradientType::getPixel (x++)))
  627. }
  628. void handleEdgeTableLineFull (int x, int width) const noexcept
  629. {
  630. PixelType* dest = getPixel (x);
  631. JUCE_PERFORM_PIXEL_OP_LOOP (blend (GradientType::getPixel (x++)))
  632. }
  633. private:
  634. const Image::BitmapData& destData;
  635. PixelType* linePixels;
  636. forcedinline PixelType* getPixel (const int x) const noexcept
  637. {
  638. return addBytesToPointer (linePixels, x * destData.pixelStride);
  639. }
  640. JUCE_DECLARE_NON_COPYABLE (Gradient)
  641. };
  642. //==============================================================================
  643. /** Fills an edge-table with a non-transformed image. */
  644. template <class DestPixelType, class SrcPixelType, bool repeatPattern>
  645. class ImageFill
  646. {
  647. public:
  648. ImageFill (const Image::BitmapData& dest, const Image::BitmapData& src,
  649. const int alpha, const int x, const int y)
  650. : destData (dest),
  651. srcData (src),
  652. extraAlpha (alpha + 1),
  653. xOffset (repeatPattern ? negativeAwareModulo (x, src.width) - src.width : x),
  654. yOffset (repeatPattern ? negativeAwareModulo (y, src.height) - src.height : y)
  655. {
  656. }
  657. forcedinline void setEdgeTableYPos (int y) noexcept
  658. {
  659. linePixels = (DestPixelType*) destData.getLinePointer (y);
  660. y -= yOffset;
  661. if (repeatPattern)
  662. {
  663. jassert (y >= 0);
  664. y %= srcData.height;
  665. }
  666. sourceLineStart = (SrcPixelType*) srcData.getLinePointer (y);
  667. }
  668. forcedinline void handleEdgeTablePixel (const int x, int alphaLevel) const noexcept
  669. {
  670. alphaLevel = (alphaLevel * extraAlpha) >> 8;
  671. getDestPixel (x)->blend (*getSrcPixel (repeatPattern ? ((x - xOffset) % srcData.width) : (x - xOffset)), (uint32) alphaLevel);
  672. }
  673. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  674. {
  675. getDestPixel (x)->blend (*getSrcPixel (repeatPattern ? ((x - xOffset) % srcData.width) : (x - xOffset)), (uint32) extraAlpha);
  676. }
  677. void handleEdgeTableLine (int x, int width, int alphaLevel) const noexcept
  678. {
  679. DestPixelType* dest = getDestPixel (x);
  680. alphaLevel = (alphaLevel * extraAlpha) >> 8;
  681. x -= xOffset;
  682. jassert (repeatPattern || (x >= 0 && x + width <= srcData.width));
  683. if (alphaLevel < 0xfe)
  684. {
  685. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*getSrcPixel (repeatPattern ? (x++ % srcData.width) : x++), (uint32) alphaLevel))
  686. }
  687. else
  688. {
  689. if (repeatPattern)
  690. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*getSrcPixel (x++ % srcData.width)))
  691. else
  692. copyRow (dest, getSrcPixel (x), width);
  693. }
  694. }
  695. void handleEdgeTableLineFull (int x, int width) const noexcept
  696. {
  697. DestPixelType* dest = getDestPixel (x);
  698. x -= xOffset;
  699. jassert (repeatPattern || (x >= 0 && x + width <= srcData.width));
  700. if (extraAlpha < 0xfe)
  701. {
  702. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*getSrcPixel (repeatPattern ? (x++ % srcData.width) : x++), (uint32) extraAlpha))
  703. }
  704. else
  705. {
  706. if (repeatPattern)
  707. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*getSrcPixel (x++ % srcData.width)))
  708. else
  709. copyRow (dest, getSrcPixel (x), width);
  710. }
  711. }
  712. void clipEdgeTableLine (EdgeTable& et, int x, int y, int width)
  713. {
  714. jassert (x - xOffset >= 0 && x + width - xOffset <= srcData.width);
  715. SrcPixelType* s = (SrcPixelType*) srcData.getLinePointer (y - yOffset);
  716. uint8* mask = (uint8*) (s + x - xOffset);
  717. if (sizeof (SrcPixelType) == sizeof (PixelARGB))
  718. mask += PixelARGB::indexA;
  719. et.clipLineToMask (x, y, mask, sizeof (SrcPixelType), width);
  720. }
  721. private:
  722. const Image::BitmapData& destData;
  723. const Image::BitmapData& srcData;
  724. const int extraAlpha, xOffset, yOffset;
  725. DestPixelType* linePixels;
  726. SrcPixelType* sourceLineStart;
  727. forcedinline DestPixelType* getDestPixel (int const x) const noexcept
  728. {
  729. return addBytesToPointer (linePixels, x * destData.pixelStride);
  730. }
  731. forcedinline SrcPixelType const* getSrcPixel (int const x) const noexcept
  732. {
  733. return addBytesToPointer (sourceLineStart, x * srcData.pixelStride);
  734. }
  735. forcedinline void copyRow (DestPixelType* dest, SrcPixelType const* src, int width) const noexcept
  736. {
  737. if (srcData.pixelStride == 3 && destData.pixelStride == 3)
  738. {
  739. memcpy (dest, src, sizeof (PixelRGB) * (size_t) width);
  740. }
  741. else
  742. {
  743. const int destStride = destData.pixelStride;
  744. const int srcStride = srcData.pixelStride;
  745. do
  746. {
  747. dest->blend (*src);
  748. dest = addBytesToPointer (dest, destStride);
  749. src = addBytesToPointer (src, srcStride);
  750. } while (--width > 0);
  751. }
  752. }
  753. JUCE_DECLARE_NON_COPYABLE (ImageFill)
  754. };
  755. //==============================================================================
  756. /** Fills an edge-table with a transformed image. */
  757. template <class DestPixelType, class SrcPixelType, bool repeatPattern>
  758. class TransformedImageFill
  759. {
  760. public:
  761. TransformedImageFill (const Image::BitmapData& dest, const Image::BitmapData& src,
  762. const AffineTransform& transform, const int alpha, const Graphics::ResamplingQuality q)
  763. : interpolator (transform,
  764. q != Graphics::lowResamplingQuality ? 0.5f : 0.0f,
  765. q != Graphics::lowResamplingQuality ? -128 : 0),
  766. destData (dest),
  767. srcData (src),
  768. extraAlpha (alpha + 1),
  769. quality (q),
  770. maxX (src.width - 1),
  771. maxY (src.height - 1),
  772. scratchSize (2048)
  773. {
  774. scratchBuffer.malloc (scratchSize);
  775. }
  776. forcedinline void setEdgeTableYPos (const int newY) noexcept
  777. {
  778. y = newY;
  779. linePixels = (DestPixelType*) destData.getLinePointer (newY);
  780. }
  781. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) noexcept
  782. {
  783. SrcPixelType p;
  784. generate (&p, x, 1);
  785. getDestPixel (x)->blend (p, (uint32) (alphaLevel * extraAlpha) >> 8);
  786. }
  787. forcedinline void handleEdgeTablePixelFull (const int x) noexcept
  788. {
  789. SrcPixelType p;
  790. generate (&p, x, 1);
  791. getDestPixel (x)->blend (p, (uint32) extraAlpha);
  792. }
  793. void handleEdgeTableLine (const int x, int width, int alphaLevel) noexcept
  794. {
  795. if (width > (int) scratchSize)
  796. {
  797. scratchSize = (size_t) width;
  798. scratchBuffer.malloc (scratchSize);
  799. }
  800. SrcPixelType* span = scratchBuffer;
  801. generate (span, x, width);
  802. DestPixelType* dest = getDestPixel (x);
  803. alphaLevel *= extraAlpha;
  804. alphaLevel >>= 8;
  805. if (alphaLevel < 0xfe)
  806. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*span++, (uint32) alphaLevel))
  807. else
  808. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*span++))
  809. }
  810. forcedinline void handleEdgeTableLineFull (const int x, int width) noexcept
  811. {
  812. handleEdgeTableLine (x, width, 255);
  813. }
  814. void clipEdgeTableLine (EdgeTable& et, int x, int y_, int width)
  815. {
  816. if (width > (int) scratchSize)
  817. {
  818. scratchSize = (size_t) width;
  819. scratchBuffer.malloc (scratchSize);
  820. }
  821. y = y_;
  822. generate (scratchBuffer.getData(), x, width);
  823. et.clipLineToMask (x, y_,
  824. reinterpret_cast<uint8*> (scratchBuffer.getData()) + SrcPixelType::indexA,
  825. sizeof (SrcPixelType), width);
  826. }
  827. private:
  828. forcedinline DestPixelType* getDestPixel (const int x) const noexcept
  829. {
  830. return addBytesToPointer (linePixels, x * destData.pixelStride);
  831. }
  832. //==============================================================================
  833. template <class PixelType>
  834. void generate (PixelType* dest, const int x, int numPixels) noexcept
  835. {
  836. this->interpolator.setStartOfLine ((float) x, (float) y, numPixels);
  837. do
  838. {
  839. int hiResX, hiResY;
  840. this->interpolator.next (hiResX, hiResY);
  841. int loResX = hiResX >> 8;
  842. int loResY = hiResY >> 8;
  843. if (repeatPattern)
  844. {
  845. loResX = negativeAwareModulo (loResX, srcData.width);
  846. loResY = negativeAwareModulo (loResY, srcData.height);
  847. }
  848. if (quality != Graphics::lowResamplingQuality)
  849. {
  850. if (isPositiveAndBelow (loResX, maxX))
  851. {
  852. if (isPositiveAndBelow (loResY, maxY))
  853. {
  854. // In the centre of the image..
  855. render4PixelAverage (dest, this->srcData.getPixelPointer (loResX, loResY),
  856. hiResX & 255, hiResY & 255);
  857. ++dest;
  858. continue;
  859. }
  860. else if (! repeatPattern)
  861. {
  862. // At a top or bottom edge..
  863. if (loResY < 0)
  864. render2PixelAverageX (dest, this->srcData.getPixelPointer (loResX, 0), hiResX & 255);
  865. else
  866. render2PixelAverageX (dest, this->srcData.getPixelPointer (loResX, maxY), hiResX & 255);
  867. ++dest;
  868. continue;
  869. }
  870. }
  871. else
  872. {
  873. if (isPositiveAndBelow (loResY, maxY) && ! repeatPattern)
  874. {
  875. // At a left or right hand edge..
  876. if (loResX < 0)
  877. render2PixelAverageY (dest, this->srcData.getPixelPointer (0, loResY), hiResY & 255);
  878. else
  879. render2PixelAverageY (dest, this->srcData.getPixelPointer (maxX, loResY), hiResY & 255);
  880. ++dest;
  881. continue;
  882. }
  883. }
  884. }
  885. if (! repeatPattern)
  886. {
  887. if (loResX < 0) loResX = 0;
  888. if (loResY < 0) loResY = 0;
  889. if (loResX > maxX) loResX = maxX;
  890. if (loResY > maxY) loResY = maxY;
  891. }
  892. dest->set (*(const PixelType*) this->srcData.getPixelPointer (loResX, loResY));
  893. ++dest;
  894. } while (--numPixels > 0);
  895. }
  896. //==============================================================================
  897. void render4PixelAverage (PixelARGB* const dest, const uint8* src, const int subPixelX, const int subPixelY) noexcept
  898. {
  899. uint32 c[4] = { 256 * 128, 256 * 128, 256 * 128, 256 * 128 };
  900. uint32 weight = (uint32) ((256 - subPixelX) * (256 - subPixelY));
  901. c[0] += weight * src[0];
  902. c[1] += weight * src[1];
  903. c[2] += weight * src[2];
  904. c[3] += weight * src[3];
  905. src += this->srcData.pixelStride;
  906. weight = (uint32) (subPixelX * (256 - subPixelY));
  907. c[0] += weight * src[0];
  908. c[1] += weight * src[1];
  909. c[2] += weight * src[2];
  910. c[3] += weight * src[3];
  911. src += this->srcData.lineStride;
  912. weight = (uint32) (subPixelX * subPixelY);
  913. c[0] += weight * src[0];
  914. c[1] += weight * src[1];
  915. c[2] += weight * src[2];
  916. c[3] += weight * src[3];
  917. src -= this->srcData.pixelStride;
  918. weight = (uint32) ((256 - subPixelX) * subPixelY);
  919. c[0] += weight * src[0];
  920. c[1] += weight * src[1];
  921. c[2] += weight * src[2];
  922. c[3] += weight * src[3];
  923. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 16),
  924. (uint8) (c[PixelARGB::indexR] >> 16),
  925. (uint8) (c[PixelARGB::indexG] >> 16),
  926. (uint8) (c[PixelARGB::indexB] >> 16));
  927. }
  928. void render2PixelAverageX (PixelARGB* const dest, const uint8* src, const uint32 subPixelX) noexcept
  929. {
  930. uint32 c[4] = { 128, 128, 128, 128 };
  931. uint32 weight = 256 - subPixelX;
  932. c[0] += weight * src[0];
  933. c[1] += weight * src[1];
  934. c[2] += weight * src[2];
  935. c[3] += weight * src[3];
  936. src += this->srcData.pixelStride;
  937. weight = subPixelX;
  938. c[0] += weight * src[0];
  939. c[1] += weight * src[1];
  940. c[2] += weight * src[2];
  941. c[3] += weight * src[3];
  942. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 8),
  943. (uint8) (c[PixelARGB::indexR] >> 8),
  944. (uint8) (c[PixelARGB::indexG] >> 8),
  945. (uint8) (c[PixelARGB::indexB] >> 8));
  946. }
  947. void render2PixelAverageY (PixelARGB* const dest, const uint8* src, const uint32 subPixelY) noexcept
  948. {
  949. uint32 c[4] = { 128, 128, 128, 128 };
  950. uint32 weight = 256 - subPixelY;
  951. c[0] += weight * src[0];
  952. c[1] += weight * src[1];
  953. c[2] += weight * src[2];
  954. c[3] += weight * src[3];
  955. src += this->srcData.lineStride;
  956. weight = subPixelY;
  957. c[0] += weight * src[0];
  958. c[1] += weight * src[1];
  959. c[2] += weight * src[2];
  960. c[3] += weight * src[3];
  961. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 8),
  962. (uint8) (c[PixelARGB::indexR] >> 8),
  963. (uint8) (c[PixelARGB::indexG] >> 8),
  964. (uint8) (c[PixelARGB::indexB] >> 8));
  965. }
  966. //==============================================================================
  967. void render4PixelAverage (PixelRGB* const dest, const uint8* src, const uint32 subPixelX, const uint32 subPixelY) noexcept
  968. {
  969. uint32 c[3] = { 256 * 128, 256 * 128, 256 * 128 };
  970. uint32 weight = (256 - subPixelX) * (256 - subPixelY);
  971. c[0] += weight * src[0];
  972. c[1] += weight * src[1];
  973. c[2] += weight * src[2];
  974. src += this->srcData.pixelStride;
  975. weight = subPixelX * (256 - subPixelY);
  976. c[0] += weight * src[0];
  977. c[1] += weight * src[1];
  978. c[2] += weight * src[2];
  979. src += this->srcData.lineStride;
  980. weight = subPixelX * subPixelY;
  981. c[0] += weight * src[0];
  982. c[1] += weight * src[1];
  983. c[2] += weight * src[2];
  984. src -= this->srcData.pixelStride;
  985. weight = (256 - subPixelX) * subPixelY;
  986. c[0] += weight * src[0];
  987. c[1] += weight * src[1];
  988. c[2] += weight * src[2];
  989. dest->setARGB ((uint8) 255,
  990. (uint8) (c[PixelRGB::indexR] >> 16),
  991. (uint8) (c[PixelRGB::indexG] >> 16),
  992. (uint8) (c[PixelRGB::indexB] >> 16));
  993. }
  994. void render2PixelAverageX (PixelRGB* const dest, const uint8* src, const uint32 subPixelX) noexcept
  995. {
  996. uint32 c[3] = { 128, 128, 128 };
  997. const uint32 weight = 256 - subPixelX;
  998. c[0] += weight * src[0];
  999. c[1] += weight * src[1];
  1000. c[2] += weight * src[2];
  1001. src += this->srcData.pixelStride;
  1002. c[0] += subPixelX * src[0];
  1003. c[1] += subPixelX * src[1];
  1004. c[2] += subPixelX * src[2];
  1005. dest->setARGB ((uint8) 255,
  1006. (uint8) (c[PixelRGB::indexR] >> 8),
  1007. (uint8) (c[PixelRGB::indexG] >> 8),
  1008. (uint8) (c[PixelRGB::indexB] >> 8));
  1009. }
  1010. void render2PixelAverageY (PixelRGB* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1011. {
  1012. uint32 c[3] = { 128, 128, 128 };
  1013. const uint32 weight = 256 - subPixelY;
  1014. c[0] += weight * src[0];
  1015. c[1] += weight * src[1];
  1016. c[2] += weight * src[2];
  1017. src += this->srcData.lineStride;
  1018. c[0] += subPixelY * src[0];
  1019. c[1] += subPixelY * src[1];
  1020. c[2] += subPixelY * src[2];
  1021. dest->setARGB ((uint8) 255,
  1022. (uint8) (c[PixelRGB::indexR] >> 8),
  1023. (uint8) (c[PixelRGB::indexG] >> 8),
  1024. (uint8) (c[PixelRGB::indexB] >> 8));
  1025. }
  1026. //==============================================================================
  1027. void render4PixelAverage (PixelAlpha* const dest, const uint8* src, const uint32 subPixelX, const uint32 subPixelY) noexcept
  1028. {
  1029. uint32 c = 256 * 128;
  1030. c += src[0] * ((256 - subPixelX) * (256 - subPixelY));
  1031. src += this->srcData.pixelStride;
  1032. c += src[1] * (subPixelX * (256 - subPixelY));
  1033. src += this->srcData.lineStride;
  1034. c += src[1] * (subPixelX * subPixelY);
  1035. src -= this->srcData.pixelStride;
  1036. c += src[0] * ((256 - subPixelX) * subPixelY);
  1037. *((uint8*) dest) = (uint8) (c >> 16);
  1038. }
  1039. void render2PixelAverageX (PixelAlpha* const dest, const uint8* src, const uint32 subPixelX) noexcept
  1040. {
  1041. uint32 c = 128;
  1042. c += src[0] * (256 - subPixelX);
  1043. src += this->srcData.pixelStride;
  1044. c += src[0] * subPixelX;
  1045. *((uint8*) dest) = (uint8) (c >> 8);
  1046. }
  1047. void render2PixelAverageY (PixelAlpha* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1048. {
  1049. uint32 c = 128;
  1050. c += src[0] * (256 - subPixelY);
  1051. src += this->srcData.lineStride;
  1052. c += src[0] * subPixelY;
  1053. *((uint8*) dest) = (uint8) (c >> 8);
  1054. }
  1055. //==============================================================================
  1056. class TransformedImageSpanInterpolator
  1057. {
  1058. public:
  1059. TransformedImageSpanInterpolator (const AffineTransform& transform,
  1060. const float offsetFloat, const int offsetInt) noexcept
  1061. : inverseTransform (transform.inverted()),
  1062. pixelOffset (offsetFloat), pixelOffsetInt (offsetInt)
  1063. {}
  1064. void setStartOfLine (float sx, float sy, const int numPixels) noexcept
  1065. {
  1066. jassert (numPixels > 0);
  1067. sx += pixelOffset;
  1068. sy += pixelOffset;
  1069. float x1 = sx, y1 = sy;
  1070. sx += numPixels;
  1071. inverseTransform.transformPoints (x1, y1, sx, sy);
  1072. xBresenham.set ((int) (x1 * 256.0f), (int) (sx * 256.0f), numPixels, pixelOffsetInt);
  1073. yBresenham.set ((int) (y1 * 256.0f), (int) (sy * 256.0f), numPixels, pixelOffsetInt);
  1074. }
  1075. void next (int& px, int& py) noexcept
  1076. {
  1077. px = xBresenham.n; xBresenham.stepToNext();
  1078. py = yBresenham.n; yBresenham.stepToNext();
  1079. }
  1080. private:
  1081. class BresenhamInterpolator
  1082. {
  1083. public:
  1084. BresenhamInterpolator() noexcept {}
  1085. void set (const int n1, const int n2, const int numSteps_, const int offsetInt) noexcept
  1086. {
  1087. numSteps = numSteps_;
  1088. step = (n2 - n1) / numSteps;
  1089. remainder = modulo = (n2 - n1) % numSteps;
  1090. n = n1 + offsetInt;
  1091. if (modulo <= 0)
  1092. {
  1093. modulo += numSteps;
  1094. remainder += numSteps;
  1095. --step;
  1096. }
  1097. modulo -= numSteps;
  1098. }
  1099. forcedinline void stepToNext() noexcept
  1100. {
  1101. modulo += remainder;
  1102. n += step;
  1103. if (modulo > 0)
  1104. {
  1105. modulo -= numSteps;
  1106. ++n;
  1107. }
  1108. }
  1109. int n;
  1110. private:
  1111. int numSteps, step, modulo, remainder;
  1112. };
  1113. const AffineTransform inverseTransform;
  1114. BresenhamInterpolator xBresenham, yBresenham;
  1115. const float pixelOffset;
  1116. const int pixelOffsetInt;
  1117. JUCE_DECLARE_NON_COPYABLE (TransformedImageSpanInterpolator)
  1118. };
  1119. //==============================================================================
  1120. TransformedImageSpanInterpolator interpolator;
  1121. const Image::BitmapData& destData;
  1122. const Image::BitmapData& srcData;
  1123. const int extraAlpha;
  1124. const Graphics::ResamplingQuality quality;
  1125. const int maxX, maxY;
  1126. int y;
  1127. DestPixelType* linePixels;
  1128. HeapBlock <SrcPixelType> scratchBuffer;
  1129. size_t scratchSize;
  1130. JUCE_DECLARE_NON_COPYABLE (TransformedImageFill)
  1131. };
  1132. //==============================================================================
  1133. template <class Iterator>
  1134. void renderImageTransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData,
  1135. const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill)
  1136. {
  1137. switch (destData.pixelFormat)
  1138. {
  1139. case Image::ARGB:
  1140. switch (srcData.pixelFormat)
  1141. {
  1142. case Image::ARGB:
  1143. if (tiledFill) { TransformedImageFill <PixelARGB, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1144. else { TransformedImageFill <PixelARGB, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1145. break;
  1146. case Image::RGB:
  1147. if (tiledFill) { TransformedImageFill <PixelARGB, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1148. else { TransformedImageFill <PixelARGB, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1149. break;
  1150. default:
  1151. if (tiledFill) { TransformedImageFill <PixelARGB, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1152. else { TransformedImageFill <PixelARGB, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1153. break;
  1154. }
  1155. break;
  1156. case Image::RGB:
  1157. switch (srcData.pixelFormat)
  1158. {
  1159. case Image::ARGB:
  1160. if (tiledFill) { TransformedImageFill <PixelRGB, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1161. else { TransformedImageFill <PixelRGB, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1162. break;
  1163. case Image::RGB:
  1164. if (tiledFill) { TransformedImageFill <PixelRGB, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1165. else { TransformedImageFill <PixelRGB, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1166. break;
  1167. default:
  1168. if (tiledFill) { TransformedImageFill <PixelRGB, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1169. else { TransformedImageFill <PixelRGB, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1170. break;
  1171. }
  1172. break;
  1173. default:
  1174. switch (srcData.pixelFormat)
  1175. {
  1176. case Image::ARGB:
  1177. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1178. else { TransformedImageFill <PixelAlpha, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1179. break;
  1180. case Image::RGB:
  1181. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1182. else { TransformedImageFill <PixelAlpha, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1183. break;
  1184. default:
  1185. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1186. else { TransformedImageFill <PixelAlpha, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1187. break;
  1188. }
  1189. break;
  1190. }
  1191. }
  1192. template <class Iterator>
  1193. void renderImageUntransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, int x, int y, bool tiledFill)
  1194. {
  1195. switch (destData.pixelFormat)
  1196. {
  1197. case Image::ARGB:
  1198. switch (srcData.pixelFormat)
  1199. {
  1200. case Image::ARGB:
  1201. if (tiledFill) { ImageFill <PixelARGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1202. else { ImageFill <PixelARGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1203. break;
  1204. case Image::RGB:
  1205. if (tiledFill) { ImageFill <PixelARGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1206. else { ImageFill <PixelARGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1207. break;
  1208. default:
  1209. if (tiledFill) { ImageFill <PixelARGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1210. else { ImageFill <PixelARGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1211. break;
  1212. }
  1213. break;
  1214. case Image::RGB:
  1215. switch (srcData.pixelFormat)
  1216. {
  1217. case Image::ARGB:
  1218. if (tiledFill) { ImageFill <PixelRGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1219. else { ImageFill <PixelRGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1220. break;
  1221. case Image::RGB:
  1222. if (tiledFill) { ImageFill <PixelRGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1223. else { ImageFill <PixelRGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1224. break;
  1225. default:
  1226. if (tiledFill) { ImageFill <PixelRGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1227. else { ImageFill <PixelRGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1228. break;
  1229. }
  1230. break;
  1231. default:
  1232. switch (srcData.pixelFormat)
  1233. {
  1234. case Image::ARGB:
  1235. if (tiledFill) { ImageFill <PixelAlpha, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1236. else { ImageFill <PixelAlpha, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1237. break;
  1238. case Image::RGB:
  1239. if (tiledFill) { ImageFill <PixelAlpha, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1240. else { ImageFill <PixelAlpha, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1241. break;
  1242. default:
  1243. if (tiledFill) { ImageFill <PixelAlpha, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1244. else { ImageFill <PixelAlpha, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1245. break;
  1246. }
  1247. break;
  1248. }
  1249. }
  1250. template <class Iterator, class DestPixelType>
  1251. void renderSolidFill (Iterator& iter, const Image::BitmapData& destData, const PixelARGB fillColour, const bool replaceContents, DestPixelType*)
  1252. {
  1253. if (replaceContents)
  1254. {
  1255. EdgeTableFillers::SolidColour <DestPixelType, true> r (destData, fillColour);
  1256. iter.iterate (r);
  1257. }
  1258. else
  1259. {
  1260. EdgeTableFillers::SolidColour <DestPixelType, false> r (destData, fillColour);
  1261. iter.iterate (r);
  1262. }
  1263. }
  1264. template <class Iterator, class DestPixelType>
  1265. void renderGradient (Iterator& iter, const Image::BitmapData& destData, const ColourGradient& g, const AffineTransform& transform,
  1266. const PixelARGB* const lookupTable, const int numLookupEntries, const bool isIdentity, DestPixelType*)
  1267. {
  1268. if (g.isRadial)
  1269. {
  1270. if (isIdentity)
  1271. {
  1272. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::Radial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1273. iter.iterate (renderer);
  1274. }
  1275. else
  1276. {
  1277. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::TransformedRadial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1278. iter.iterate (renderer);
  1279. }
  1280. }
  1281. else
  1282. {
  1283. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::Linear> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1284. iter.iterate (renderer);
  1285. }
  1286. }
  1287. }
  1288. //==============================================================================
  1289. template <class SavedStateType>
  1290. struct ClipRegions
  1291. {
  1292. class Base : public SingleThreadedReferenceCountedObject
  1293. {
  1294. public:
  1295. Base() {}
  1296. virtual ~Base() {}
  1297. typedef ReferenceCountedObjectPtr<Base> Ptr;
  1298. virtual Ptr clone() const = 0;
  1299. virtual Ptr applyClipTo (const Ptr& target) const = 0;
  1300. virtual Ptr clipToRectangle (const Rectangle<int>&) = 0;
  1301. virtual Ptr clipToRectangleList (const RectangleList<int>&) = 0;
  1302. virtual Ptr excludeClipRectangle (const Rectangle<int>&) = 0;
  1303. virtual Ptr clipToPath (const Path&, const AffineTransform&) = 0;
  1304. virtual Ptr clipToEdgeTable (const EdgeTable& et) = 0;
  1305. virtual Ptr clipToImageAlpha (const Image&, const AffineTransform&, const Graphics::ResamplingQuality) = 0;
  1306. virtual void translate (Point<int> delta) = 0;
  1307. virtual bool clipRegionIntersects (const Rectangle<int>&) const = 0;
  1308. virtual Rectangle<int> getClipBounds() const = 0;
  1309. virtual void fillRectWithColour (SavedStateType&, const Rectangle<int>&, const PixelARGB colour, bool replaceContents) const = 0;
  1310. virtual void fillRectWithColour (SavedStateType&, const Rectangle<float>&, const PixelARGB colour) const = 0;
  1311. virtual void fillAllWithColour (SavedStateType&, const PixelARGB colour, bool replaceContents) const = 0;
  1312. virtual void fillAllWithGradient (SavedStateType&, ColourGradient&, const AffineTransform&, bool isIdentity) const = 0;
  1313. virtual void renderImageTransformed (SavedStateType&, const Image&, const int alpha, const AffineTransform&, Graphics::ResamplingQuality, bool tiledFill) const = 0;
  1314. virtual void renderImageUntransformed (SavedStateType&, const Image&, const int alpha, int x, int y, bool tiledFill) const = 0;
  1315. };
  1316. //==============================================================================
  1317. class EdgeTableRegion : public Base
  1318. {
  1319. public:
  1320. EdgeTableRegion (const EdgeTable& e) : edgeTable (e) {}
  1321. EdgeTableRegion (const Rectangle<int>& r) : edgeTable (r) {}
  1322. EdgeTableRegion (const Rectangle<float>& r) : edgeTable (r) {}
  1323. EdgeTableRegion (const RectangleList<int>& r) : edgeTable (r) {}
  1324. EdgeTableRegion (const Rectangle<int>& bounds, const Path& p, const AffineTransform& t) : edgeTable (bounds, p, t) {}
  1325. EdgeTableRegion (const EdgeTableRegion& other) : Base(), edgeTable (other.edgeTable) {}
  1326. typedef typename Base::Ptr Ptr;
  1327. Ptr clone() const { return new EdgeTableRegion (*this); }
  1328. Ptr applyClipTo (const Ptr& target) const { return target->clipToEdgeTable (edgeTable); }
  1329. Ptr clipToRectangle (const Rectangle<int>& r)
  1330. {
  1331. edgeTable.clipToRectangle (r);
  1332. return edgeTable.isEmpty() ? nullptr : this;
  1333. }
  1334. Ptr clipToRectangleList (const RectangleList<int>& r)
  1335. {
  1336. RectangleList<int> inverse (edgeTable.getMaximumBounds());
  1337. if (inverse.subtract (r))
  1338. for (const Rectangle<int>* i = inverse.begin(), * const e = inverse.end(); i != e; ++i)
  1339. edgeTable.excludeRectangle (*i);
  1340. return edgeTable.isEmpty() ? nullptr : this;
  1341. }
  1342. Ptr excludeClipRectangle (const Rectangle<int>& r)
  1343. {
  1344. edgeTable.excludeRectangle (r);
  1345. return edgeTable.isEmpty() ? nullptr : this;
  1346. }
  1347. Ptr clipToPath (const Path& p, const AffineTransform& transform)
  1348. {
  1349. EdgeTable et (edgeTable.getMaximumBounds(), p, transform);
  1350. edgeTable.clipToEdgeTable (et);
  1351. return edgeTable.isEmpty() ? nullptr : this;
  1352. }
  1353. Ptr clipToEdgeTable (const EdgeTable& et)
  1354. {
  1355. edgeTable.clipToEdgeTable (et);
  1356. return edgeTable.isEmpty() ? nullptr : this;
  1357. }
  1358. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, const Graphics::ResamplingQuality quality)
  1359. {
  1360. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  1361. if (transform.isOnlyTranslation())
  1362. {
  1363. // If our translation doesn't involve any distortion, just use a simple blit..
  1364. const int tx = (int) (transform.getTranslationX() * 256.0f);
  1365. const int ty = (int) (transform.getTranslationY() * 256.0f);
  1366. if (quality == Graphics::lowResamplingQuality || ((tx | ty) & 224) == 0)
  1367. {
  1368. const int imageX = ((tx + 128) >> 8);
  1369. const int imageY = ((ty + 128) >> 8);
  1370. if (image.getFormat() == Image::ARGB)
  1371. straightClipImage (srcData, imageX, imageY, (PixelARGB*) 0);
  1372. else
  1373. straightClipImage (srcData, imageX, imageY, (PixelAlpha*) 0);
  1374. return edgeTable.isEmpty() ? nullptr : this;
  1375. }
  1376. }
  1377. if (transform.isSingularity())
  1378. return Ptr();
  1379. {
  1380. Path p;
  1381. p.addRectangle (0, 0, (float) srcData.width, (float) srcData.height);
  1382. EdgeTable et2 (edgeTable.getMaximumBounds(), p, transform);
  1383. edgeTable.clipToEdgeTable (et2);
  1384. }
  1385. if (! edgeTable.isEmpty())
  1386. {
  1387. if (image.getFormat() == Image::ARGB)
  1388. transformedClipImage (srcData, transform, quality, (PixelARGB*) 0);
  1389. else
  1390. transformedClipImage (srcData, transform, quality, (PixelAlpha*) 0);
  1391. }
  1392. return edgeTable.isEmpty() ? nullptr : this;
  1393. }
  1394. void translate (Point<int> delta)
  1395. {
  1396. edgeTable.translate ((float) delta.x, delta.y);
  1397. }
  1398. bool clipRegionIntersects (const Rectangle<int>& r) const
  1399. {
  1400. return edgeTable.getMaximumBounds().intersects (r);
  1401. }
  1402. Rectangle<int> getClipBounds() const
  1403. {
  1404. return edgeTable.getMaximumBounds();
  1405. }
  1406. void fillRectWithColour (SavedStateType& state, const Rectangle<int>& area, const PixelARGB colour, bool replaceContents) const
  1407. {
  1408. const Rectangle<int> totalClip (edgeTable.getMaximumBounds());
  1409. const Rectangle<int> clipped (totalClip.getIntersection (area));
  1410. if (! clipped.isEmpty())
  1411. {
  1412. EdgeTableRegion et (clipped);
  1413. et.edgeTable.clipToEdgeTable (edgeTable);
  1414. state.fillWithSolidColour (et.edgeTable, colour, replaceContents);
  1415. }
  1416. }
  1417. void fillRectWithColour (SavedStateType& state, const Rectangle<float>& area, const PixelARGB colour) const
  1418. {
  1419. const Rectangle<float> totalClip (edgeTable.getMaximumBounds().toFloat());
  1420. const Rectangle<float> clipped (totalClip.getIntersection (area));
  1421. if (! clipped.isEmpty())
  1422. {
  1423. EdgeTableRegion et (clipped);
  1424. et.edgeTable.clipToEdgeTable (edgeTable);
  1425. state.fillWithSolidColour (et.edgeTable, colour, false);
  1426. }
  1427. }
  1428. void fillAllWithColour (SavedStateType& state, const PixelARGB colour, bool replaceContents) const
  1429. {
  1430. state.fillWithSolidColour (edgeTable, colour, replaceContents);
  1431. }
  1432. void fillAllWithGradient (SavedStateType& state, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const
  1433. {
  1434. state.fillWithGradient (edgeTable, gradient, transform, isIdentity);
  1435. }
  1436. void renderImageTransformed (SavedStateType& state, const Image& src, const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill) const
  1437. {
  1438. state.renderImageTransformed (edgeTable, src, alpha, transform, quality, tiledFill);
  1439. }
  1440. void renderImageUntransformed (SavedStateType& state, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  1441. {
  1442. state.renderImageUntransformed (edgeTable, src, alpha, x, y, tiledFill);
  1443. }
  1444. EdgeTable edgeTable;
  1445. private:
  1446. template <class SrcPixelType>
  1447. void transformedClipImage (const Image::BitmapData& srcData, const AffineTransform& transform, const Graphics::ResamplingQuality quality, const SrcPixelType*)
  1448. {
  1449. EdgeTableFillers::TransformedImageFill <SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, transform, 255, quality);
  1450. for (int y = 0; y < edgeTable.getMaximumBounds().getHeight(); ++y)
  1451. renderer.clipEdgeTableLine (edgeTable, edgeTable.getMaximumBounds().getX(), y + edgeTable.getMaximumBounds().getY(),
  1452. edgeTable.getMaximumBounds().getWidth());
  1453. }
  1454. template <class SrcPixelType>
  1455. void straightClipImage (const Image::BitmapData& srcData, int imageX, int imageY, const SrcPixelType*)
  1456. {
  1457. Rectangle<int> r (imageX, imageY, srcData.width, srcData.height);
  1458. edgeTable.clipToRectangle (r);
  1459. EdgeTableFillers::ImageFill <SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, 255, imageX, imageY);
  1460. for (int y = 0; y < r.getHeight(); ++y)
  1461. renderer.clipEdgeTableLine (edgeTable, r.getX(), y + r.getY(), r.getWidth());
  1462. }
  1463. EdgeTableRegion& operator= (const EdgeTableRegion&);
  1464. };
  1465. //==============================================================================
  1466. class RectangleListRegion : public Base
  1467. {
  1468. public:
  1469. RectangleListRegion (const Rectangle<int>& r) : clip (r) {}
  1470. RectangleListRegion (const RectangleList<int>& r) : clip (r) {}
  1471. RectangleListRegion (const RectangleListRegion& other) : Base(), clip (other.clip) {}
  1472. typedef typename Base::Ptr Ptr;
  1473. Ptr clone() const { return new RectangleListRegion (*this); }
  1474. Ptr applyClipTo (const Ptr& target) const { return target->clipToRectangleList (clip); }
  1475. Ptr clipToRectangle (const Rectangle<int>& r)
  1476. {
  1477. clip.clipTo (r);
  1478. return clip.isEmpty() ? nullptr : this;
  1479. }
  1480. Ptr clipToRectangleList (const RectangleList<int>& r)
  1481. {
  1482. clip.clipTo (r);
  1483. return clip.isEmpty() ? nullptr : this;
  1484. }
  1485. Ptr excludeClipRectangle (const Rectangle<int>& r)
  1486. {
  1487. clip.subtract (r);
  1488. return clip.isEmpty() ? nullptr : this;
  1489. }
  1490. Ptr clipToPath (const Path& p, const AffineTransform& transform) { return toEdgeTable()->clipToPath (p, transform); }
  1491. Ptr clipToEdgeTable (const EdgeTable& et) { return toEdgeTable()->clipToEdgeTable (et); }
  1492. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, const Graphics::ResamplingQuality quality)
  1493. {
  1494. return toEdgeTable()->clipToImageAlpha (image, transform, quality);
  1495. }
  1496. void translate (Point<int> delta)
  1497. {
  1498. clip.offsetAll (delta.x, delta.y);
  1499. }
  1500. bool clipRegionIntersects (const Rectangle<int>& r) const { return clip.intersects (r); }
  1501. Rectangle<int> getClipBounds() const { return clip.getBounds(); }
  1502. void fillRectWithColour (SavedStateType& state, const Rectangle<int>& area, const PixelARGB colour, bool replaceContents) const
  1503. {
  1504. SubRectangleIterator iter (clip, area);
  1505. state.fillWithSolidColour (iter, colour, replaceContents);
  1506. }
  1507. void fillRectWithColour (SavedStateType& state, const Rectangle<float>& area, const PixelARGB colour) const
  1508. {
  1509. SubRectangleIteratorFloat iter (clip, area);
  1510. state.fillWithSolidColour (iter, colour, false);
  1511. }
  1512. void fillAllWithColour (SavedStateType& state, const PixelARGB colour, bool replaceContents) const
  1513. {
  1514. state.fillWithSolidColour (*this, colour, replaceContents);
  1515. }
  1516. void fillAllWithGradient (SavedStateType& state, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const
  1517. {
  1518. state.fillWithGradient (*this, gradient, transform, isIdentity);
  1519. }
  1520. void renderImageTransformed (SavedStateType& state, const Image& src, const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill) const
  1521. {
  1522. state.renderImageTransformed (*this, src, alpha, transform, quality, tiledFill);
  1523. }
  1524. void renderImageUntransformed (SavedStateType& state, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  1525. {
  1526. state.renderImageUntransformed (*this, src, alpha, x, y, tiledFill);
  1527. }
  1528. RectangleList<int> clip;
  1529. //==============================================================================
  1530. template <class Renderer>
  1531. void iterate (Renderer& r) const noexcept
  1532. {
  1533. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1534. {
  1535. const int x = i->getX();
  1536. const int w = i->getWidth();
  1537. jassert (w > 0);
  1538. const int bottom = i->getBottom();
  1539. for (int y = i->getY(); y < bottom; ++y)
  1540. {
  1541. r.setEdgeTableYPos (y);
  1542. r.handleEdgeTableLineFull (x, w);
  1543. }
  1544. }
  1545. }
  1546. private:
  1547. //==============================================================================
  1548. class SubRectangleIterator
  1549. {
  1550. public:
  1551. SubRectangleIterator (const RectangleList<int>& clipList, const Rectangle<int>& clipBounds)
  1552. : clip (clipList), area (clipBounds)
  1553. {}
  1554. template <class Renderer>
  1555. void iterate (Renderer& r) const noexcept
  1556. {
  1557. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1558. {
  1559. const Rectangle<int> rect (i->getIntersection (area));
  1560. if (! rect.isEmpty())
  1561. {
  1562. const int x = rect.getX();
  1563. const int w = rect.getWidth();
  1564. const int bottom = rect.getBottom();
  1565. for (int y = rect.getY(); y < bottom; ++y)
  1566. {
  1567. r.setEdgeTableYPos (y);
  1568. r.handleEdgeTableLineFull (x, w);
  1569. }
  1570. }
  1571. }
  1572. }
  1573. private:
  1574. const RectangleList<int>& clip;
  1575. const Rectangle<int> area;
  1576. JUCE_DECLARE_NON_COPYABLE (SubRectangleIterator)
  1577. };
  1578. //==============================================================================
  1579. class SubRectangleIteratorFloat
  1580. {
  1581. public:
  1582. SubRectangleIteratorFloat (const RectangleList<int>& clipList, const Rectangle<float>& clipBounds) noexcept
  1583. : clip (clipList), area (clipBounds)
  1584. {
  1585. }
  1586. template <class Renderer>
  1587. void iterate (Renderer& r) const noexcept
  1588. {
  1589. const RenderingHelpers::FloatRectangleRasterisingInfo f (area);
  1590. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1591. {
  1592. const int clipLeft = i->getX();
  1593. const int clipRight = i->getRight();
  1594. const int clipTop = i->getY();
  1595. const int clipBottom = i->getBottom();
  1596. if (f.totalBottom > clipTop && f.totalTop < clipBottom
  1597. && f.totalRight > clipLeft && f.totalLeft < clipRight)
  1598. {
  1599. if (f.isOnePixelWide())
  1600. {
  1601. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1602. {
  1603. r.setEdgeTableYPos (f.totalTop);
  1604. r.handleEdgeTablePixel (f.left, f.topAlpha);
  1605. }
  1606. const int endY = jmin (f.bottom, clipBottom);
  1607. for (int y = jmax (clipTop, f.top); y < endY; ++y)
  1608. {
  1609. r.setEdgeTableYPos (y);
  1610. r.handleEdgeTablePixelFull (f.left);
  1611. }
  1612. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1613. {
  1614. r.setEdgeTableYPos (f.bottom);
  1615. r.handleEdgeTablePixel (f.left, f.bottomAlpha);
  1616. }
  1617. }
  1618. else
  1619. {
  1620. const int clippedLeft = jmax (f.left, clipLeft);
  1621. const int clippedWidth = jmin (f.right, clipRight) - clippedLeft;
  1622. const bool doLeftAlpha = f.leftAlpha != 0 && f.totalLeft >= clipLeft;
  1623. const bool doRightAlpha = f.rightAlpha != 0 && f.right < clipRight;
  1624. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1625. {
  1626. r.setEdgeTableYPos (f.totalTop);
  1627. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getTopLeftCornerAlpha());
  1628. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.topAlpha);
  1629. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getTopRightCornerAlpha());
  1630. }
  1631. const int endY = jmin (f.bottom, clipBottom);
  1632. for (int y = jmax (clipTop, f.top); y < endY; ++y)
  1633. {
  1634. r.setEdgeTableYPos (y);
  1635. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.leftAlpha);
  1636. if (clippedWidth > 0) r.handleEdgeTableLineFull (clippedLeft, clippedWidth);
  1637. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.rightAlpha);
  1638. }
  1639. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1640. {
  1641. r.setEdgeTableYPos (f.bottom);
  1642. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getBottomLeftCornerAlpha());
  1643. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.bottomAlpha);
  1644. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getBottomRightCornerAlpha());
  1645. }
  1646. }
  1647. }
  1648. }
  1649. }
  1650. private:
  1651. const RectangleList<int>& clip;
  1652. const Rectangle<float>& area;
  1653. JUCE_DECLARE_NON_COPYABLE (SubRectangleIteratorFloat)
  1654. };
  1655. Ptr toEdgeTable() const { return new EdgeTableRegion (clip); }
  1656. RectangleListRegion& operator= (const RectangleListRegion&);
  1657. };
  1658. };
  1659. //==============================================================================
  1660. template <class SavedStateType>
  1661. class SavedStateBase
  1662. {
  1663. public:
  1664. typedef typename ClipRegions<SavedStateType>::Base BaseRegionType;
  1665. typedef typename ClipRegions<SavedStateType>::EdgeTableRegion EdgeTableRegionType;
  1666. typedef typename ClipRegions<SavedStateType>::RectangleListRegion RectangleListRegionType;
  1667. SavedStateBase (const Rectangle<int>& initialClip)
  1668. : clip (new RectangleListRegionType (initialClip)), transform (0, 0),
  1669. interpolationQuality (Graphics::mediumResamplingQuality), transparencyLayerAlpha (1.0f)
  1670. {
  1671. }
  1672. SavedStateBase (const RectangleList<int>& clipList, int x, int y)
  1673. : clip (new RectangleListRegionType (clipList)), transform (x, y),
  1674. interpolationQuality (Graphics::mediumResamplingQuality), transparencyLayerAlpha (1.0f)
  1675. {
  1676. }
  1677. SavedStateBase (const SavedStateBase& other)
  1678. : clip (other.clip), transform (other.transform), fillType (other.fillType),
  1679. interpolationQuality (other.interpolationQuality),
  1680. transparencyLayerAlpha (other.transparencyLayerAlpha)
  1681. {
  1682. }
  1683. SavedStateType& getThis() noexcept { return *static_cast <SavedStateType*> (this); }
  1684. bool clipToRectangle (const Rectangle<int>& r)
  1685. {
  1686. if (clip != nullptr)
  1687. {
  1688. if (transform.isOnlyTranslated)
  1689. {
  1690. cloneClipIfMultiplyReferenced();
  1691. clip = clip->clipToRectangle (transform.translated (r));
  1692. }
  1693. else if (! transform.isRotated)
  1694. {
  1695. cloneClipIfMultiplyReferenced();
  1696. clip = clip->clipToRectangle (transform.transformed (r));
  1697. }
  1698. else
  1699. {
  1700. Path p;
  1701. p.addRectangle (r);
  1702. clipToPath (p, AffineTransform::identity);
  1703. }
  1704. }
  1705. return clip != nullptr;
  1706. }
  1707. bool clipToRectangleList (const RectangleList<int>& r)
  1708. {
  1709. if (clip != nullptr)
  1710. {
  1711. if (transform.isOnlyTranslated)
  1712. {
  1713. cloneClipIfMultiplyReferenced();
  1714. RectangleList<int> offsetList (r);
  1715. offsetList.offsetAll (transform.xOffset, transform.yOffset);
  1716. clip = clip->clipToRectangleList (offsetList);
  1717. }
  1718. else if (! transform.isRotated)
  1719. {
  1720. cloneClipIfMultiplyReferenced();
  1721. RectangleList<int> scaledList;
  1722. for (const Rectangle<int>* i = r.begin(), * const e = r.end(); i != e; ++i)
  1723. scaledList.add (transform.transformed (*i));
  1724. clip = clip->clipToRectangleList (scaledList);
  1725. }
  1726. else
  1727. {
  1728. clipToPath (r.toPath(), AffineTransform::identity);
  1729. }
  1730. }
  1731. return clip != nullptr;
  1732. }
  1733. static Rectangle<int> getLargestIntegerWithin (Rectangle<float> r)
  1734. {
  1735. const int x1 = (int) std::ceil (r.getX());
  1736. const int y1 = (int) std::ceil (r.getY());
  1737. const int x2 = (int) std::floor (r.getRight());
  1738. const int y2 = (int) std::floor (r.getBottom());
  1739. return Rectangle<int> (x1, y1, x2 - x1, y2 - y1);
  1740. }
  1741. bool excludeClipRectangle (const Rectangle<int>& r)
  1742. {
  1743. if (clip != nullptr)
  1744. {
  1745. cloneClipIfMultiplyReferenced();
  1746. if (transform.isOnlyTranslated)
  1747. {
  1748. clip = clip->excludeClipRectangle (getLargestIntegerWithin (transform.translated (r.toFloat())));
  1749. }
  1750. else if (! transform.isRotated)
  1751. {
  1752. clip = clip->excludeClipRectangle (getLargestIntegerWithin (transform.transformed (r.toFloat())));
  1753. }
  1754. else
  1755. {
  1756. Path p;
  1757. p.addRectangle (r.toFloat());
  1758. p.applyTransform (transform.complexTransform);
  1759. p.addRectangle (clip->getClipBounds().toFloat());
  1760. p.setUsingNonZeroWinding (false);
  1761. clip = clip->clipToPath (p, AffineTransform::identity);
  1762. }
  1763. }
  1764. return clip != nullptr;
  1765. }
  1766. void clipToPath (const Path& p, const AffineTransform& t)
  1767. {
  1768. if (clip != nullptr)
  1769. {
  1770. cloneClipIfMultiplyReferenced();
  1771. clip = clip->clipToPath (p, transform.getTransformWith (t));
  1772. }
  1773. }
  1774. void clipToImageAlpha (const Image& sourceImage, const AffineTransform& t)
  1775. {
  1776. if (clip != nullptr)
  1777. {
  1778. if (sourceImage.hasAlphaChannel())
  1779. {
  1780. cloneClipIfMultiplyReferenced();
  1781. clip = clip->clipToImageAlpha (sourceImage, transform.getTransformWith (t), interpolationQuality);
  1782. }
  1783. else
  1784. {
  1785. Path p;
  1786. p.addRectangle (sourceImage.getBounds());
  1787. clipToPath (p, t);
  1788. }
  1789. }
  1790. }
  1791. bool clipRegionIntersects (const Rectangle<int>& r) const
  1792. {
  1793. if (clip != nullptr)
  1794. {
  1795. if (transform.isOnlyTranslated)
  1796. return clip->clipRegionIntersects (transform.translated (r));
  1797. return getClipBounds().intersects (r);
  1798. }
  1799. return false;
  1800. }
  1801. Rectangle<int> getClipBounds() const
  1802. {
  1803. return clip != nullptr ? transform.deviceSpaceToUserSpace (clip->getClipBounds())
  1804. : Rectangle<int>();
  1805. }
  1806. void setFillType (const FillType& newFill)
  1807. {
  1808. fillType = newFill;
  1809. }
  1810. void fillTargetRect (const Rectangle<int>& r, const bool replaceContents)
  1811. {
  1812. if (fillType.isColour())
  1813. {
  1814. clip->fillRectWithColour (getThis(), r, fillType.colour.getPixelARGB(), replaceContents);
  1815. }
  1816. else
  1817. {
  1818. const Rectangle<int> clipped (clip->getClipBounds().getIntersection (r));
  1819. if (! clipped.isEmpty())
  1820. fillShape (new RectangleListRegionType (clipped), false);
  1821. }
  1822. }
  1823. void fillTargetRect (const Rectangle<float>& r)
  1824. {
  1825. if (fillType.isColour())
  1826. {
  1827. clip->fillRectWithColour (getThis(), r, fillType.colour.getPixelARGB());
  1828. }
  1829. else
  1830. {
  1831. const Rectangle<float> clipped (clip->getClipBounds().toFloat().getIntersection (r));
  1832. if (! clipped.isEmpty())
  1833. fillShape (new EdgeTableRegionType (clipped), false);
  1834. }
  1835. }
  1836. template <typename CoordType>
  1837. void fillRectAsPath (const Rectangle<CoordType>& r)
  1838. {
  1839. Path p;
  1840. p.addRectangle (r);
  1841. fillPath (p, AffineTransform::identity);
  1842. }
  1843. void fillRect (const Rectangle<int>& r, const bool replaceContents)
  1844. {
  1845. if (clip != nullptr)
  1846. {
  1847. if (transform.isOnlyTranslated)
  1848. fillTargetRect (transform.translated (r), replaceContents);
  1849. else
  1850. {
  1851. jassert (! replaceContents); // not implemented..
  1852. if (! transform.isRotated)
  1853. fillTargetRect (transform.transformed (r.toFloat()));
  1854. else
  1855. fillRectAsPath (r);
  1856. }
  1857. }
  1858. }
  1859. void fillRect (const Rectangle<float>& r)
  1860. {
  1861. if (clip != nullptr)
  1862. {
  1863. if (transform.isOnlyTranslated)
  1864. fillTargetRect (transform.translated (r));
  1865. else if (! transform.isRotated)
  1866. fillTargetRect (transform.transformed (r));
  1867. else
  1868. fillRectAsPath (r);
  1869. }
  1870. }
  1871. void fillPath (const Path& path, const AffineTransform& t)
  1872. {
  1873. if (clip != nullptr)
  1874. fillShape (new EdgeTableRegionType (clip->getClipBounds(), path, transform.getTransformWith (t)), false);
  1875. }
  1876. void fillEdgeTable (const EdgeTable& edgeTable, const float x, const int y)
  1877. {
  1878. jassert (transform.isOnlyTranslated);
  1879. if (clip != nullptr)
  1880. {
  1881. EdgeTableRegionType* edgeTableClip = new EdgeTableRegionType (edgeTable);
  1882. edgeTableClip->edgeTable.translate (x + transform.xOffset,
  1883. y + transform.yOffset);
  1884. fillShape (edgeTableClip, false);
  1885. }
  1886. }
  1887. void drawLine (const Line <float>& line)
  1888. {
  1889. Path p;
  1890. p.addLineSegment (line, 1.0f);
  1891. fillPath (p, AffineTransform::identity);
  1892. }
  1893. void drawImage (const Image& sourceImage, const AffineTransform& trans)
  1894. {
  1895. if (clip != nullptr && ! fillType.colour.isTransparent())
  1896. renderImage (sourceImage, trans, nullptr);
  1897. }
  1898. void renderImage (const Image& sourceImage, const AffineTransform& trans,
  1899. const BaseRegionType* const tiledFillClipRegion)
  1900. {
  1901. const AffineTransform t (transform.getTransformWith (trans));
  1902. const int alpha = fillType.colour.getAlpha();
  1903. if (t.isOnlyTranslation())
  1904. {
  1905. // If our translation doesn't involve any distortion, just use a simple blit..
  1906. int tx = (int) (t.getTranslationX() * 256.0f);
  1907. int ty = (int) (t.getTranslationY() * 256.0f);
  1908. if (interpolationQuality == Graphics::lowResamplingQuality || ((tx | ty) & 224) == 0)
  1909. {
  1910. tx = ((tx + 128) >> 8);
  1911. ty = ((ty + 128) >> 8);
  1912. if (tiledFillClipRegion != nullptr)
  1913. {
  1914. tiledFillClipRegion->renderImageUntransformed (getThis(), sourceImage, alpha, tx, ty, true);
  1915. }
  1916. else
  1917. {
  1918. Rectangle<int> area (tx, ty, sourceImage.getWidth(), sourceImage.getHeight());
  1919. area = area.getIntersection (getThis().getMaximumBounds());
  1920. if (! area.isEmpty())
  1921. if (typename BaseRegionType::Ptr c = clip->applyClipTo (new EdgeTableRegionType (area)))
  1922. c->renderImageUntransformed (getThis(), sourceImage, alpha, tx, ty, false);
  1923. }
  1924. return;
  1925. }
  1926. }
  1927. if (! t.isSingularity())
  1928. {
  1929. if (tiledFillClipRegion != nullptr)
  1930. {
  1931. tiledFillClipRegion->renderImageTransformed (getThis(), sourceImage, alpha, t, interpolationQuality, true);
  1932. }
  1933. else
  1934. {
  1935. Path p;
  1936. p.addRectangle (sourceImage.getBounds());
  1937. typename BaseRegionType::Ptr c (clip->clone());
  1938. c = c->clipToPath (p, t);
  1939. if (c != nullptr)
  1940. c->renderImageTransformed (getThis(), sourceImage, alpha, t, interpolationQuality, false);
  1941. }
  1942. }
  1943. }
  1944. void fillShape (typename BaseRegionType::Ptr shapeToFill, const bool replaceContents)
  1945. {
  1946. jassert (clip != nullptr);
  1947. shapeToFill = clip->applyClipTo (shapeToFill);
  1948. if (shapeToFill != nullptr)
  1949. {
  1950. if (fillType.isGradient())
  1951. {
  1952. jassert (! replaceContents); // that option is just for solid colours
  1953. ColourGradient g2 (*(fillType.gradient));
  1954. g2.multiplyOpacity (fillType.getOpacity());
  1955. AffineTransform t (transform.getTransformWith (fillType.transform).translated (-0.5f, -0.5f));
  1956. const bool isIdentity = t.isOnlyTranslation();
  1957. if (isIdentity)
  1958. {
  1959. // If our translation doesn't involve any distortion, we can speed it up..
  1960. g2.point1.applyTransform (t);
  1961. g2.point2.applyTransform (t);
  1962. t = AffineTransform::identity;
  1963. }
  1964. shapeToFill->fillAllWithGradient (getThis(), g2, t, isIdentity);
  1965. }
  1966. else if (fillType.isTiledImage())
  1967. {
  1968. renderImage (fillType.image, fillType.transform, shapeToFill);
  1969. }
  1970. else
  1971. {
  1972. shapeToFill->fillAllWithColour (getThis(), fillType.colour.getPixelARGB(), replaceContents);
  1973. }
  1974. }
  1975. }
  1976. void cloneClipIfMultiplyReferenced()
  1977. {
  1978. if (clip->getReferenceCount() > 1)
  1979. clip = clip->clone();
  1980. }
  1981. typename BaseRegionType::Ptr clip;
  1982. RenderingHelpers::TranslationOrTransform transform;
  1983. FillType fillType;
  1984. Graphics::ResamplingQuality interpolationQuality;
  1985. float transparencyLayerAlpha;
  1986. };
  1987. //==============================================================================
  1988. class SoftwareRendererSavedState : public SavedStateBase<SoftwareRendererSavedState>
  1989. {
  1990. typedef SavedStateBase<SoftwareRendererSavedState> BaseClass;
  1991. public:
  1992. SoftwareRendererSavedState (const Image& im, const Rectangle<int>& clipBounds)
  1993. : BaseClass (clipBounds), image (im)
  1994. {
  1995. }
  1996. SoftwareRendererSavedState (const Image& im, const RectangleList<int>& clipList, int x, int y)
  1997. : BaseClass (clipList, x, y), image (im)
  1998. {
  1999. }
  2000. SoftwareRendererSavedState (const SoftwareRendererSavedState& other)
  2001. : BaseClass (other), image (other.image), font (other.font)
  2002. {
  2003. }
  2004. SoftwareRendererSavedState* beginTransparencyLayer (float opacity)
  2005. {
  2006. SoftwareRendererSavedState* s = new SoftwareRendererSavedState (*this);
  2007. if (clip != nullptr)
  2008. {
  2009. const Rectangle<int> layerBounds (clip->getClipBounds());
  2010. s->image = Image (Image::ARGB, layerBounds.getWidth(), layerBounds.getHeight(), true);
  2011. s->transparencyLayerAlpha = opacity;
  2012. s->transform.moveOriginInDeviceSpace (-layerBounds.getX(), -layerBounds.getY());
  2013. s->cloneClipIfMultiplyReferenced();
  2014. s->clip->translate (-layerBounds.getPosition());
  2015. }
  2016. return s;
  2017. }
  2018. void endTransparencyLayer (SoftwareRendererSavedState& finishedLayerState)
  2019. {
  2020. if (clip != nullptr)
  2021. {
  2022. const Rectangle<int> layerBounds (clip->getClipBounds());
  2023. const ScopedPointer<LowLevelGraphicsContext> g (image.createLowLevelContext());
  2024. g->setOpacity (finishedLayerState.transparencyLayerAlpha);
  2025. g->drawImage (finishedLayerState.image, AffineTransform::translation ((float) layerBounds.getX(),
  2026. (float) layerBounds.getY()));
  2027. }
  2028. }
  2029. //==============================================================================
  2030. void drawGlyph (int glyphNumber, const AffineTransform& trans)
  2031. {
  2032. if (clip != nullptr)
  2033. {
  2034. if (trans.isOnlyTranslation() && transform.isOnlyTranslated)
  2035. {
  2036. GlyphCache <CachedGlyphEdgeTable <SoftwareRendererSavedState>, SoftwareRendererSavedState>::getInstance()
  2037. .drawGlyph (*this, font, glyphNumber,
  2038. trans.getTranslationX(),
  2039. trans.getTranslationY());
  2040. }
  2041. else
  2042. {
  2043. const float fontHeight = font.getHeight();
  2044. AffineTransform t (transform.getTransformWith (AffineTransform::scale (fontHeight * font.getHorizontalScale(), fontHeight)
  2045. .followedBy (trans)));
  2046. const ScopedPointer<EdgeTable> et (font.getTypeface()->getEdgeTableForGlyph (glyphNumber, t));
  2047. if (et != nullptr)
  2048. fillShape (new EdgeTableRegionType (*et), false);
  2049. }
  2050. }
  2051. }
  2052. Rectangle<int> getMaximumBounds() const { return image.getBounds(); }
  2053. //==============================================================================
  2054. template <typename IteratorType>
  2055. void renderImageTransformed (IteratorType& iter, const Image& src, const int alpha, const AffineTransform& trans, Graphics::ResamplingQuality quality, bool tiledFill) const
  2056. {
  2057. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2058. const Image::BitmapData srcData (src, Image::BitmapData::readOnly);
  2059. EdgeTableFillers::renderImageTransformed (iter, destData, srcData, alpha, trans, quality, tiledFill);
  2060. }
  2061. template <typename IteratorType>
  2062. void renderImageUntransformed (IteratorType& iter, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  2063. {
  2064. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2065. const Image::BitmapData srcData (src, Image::BitmapData::readOnly);
  2066. EdgeTableFillers::renderImageUntransformed (iter, destData, srcData, alpha, x, y, tiledFill);
  2067. }
  2068. template <typename IteratorType>
  2069. void fillWithSolidColour (IteratorType& iter, const PixelARGB colour, bool replaceContents) const
  2070. {
  2071. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2072. switch (destData.pixelFormat)
  2073. {
  2074. case Image::ARGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelARGB*) 0); break;
  2075. case Image::RGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelRGB*) 0); break;
  2076. default: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelAlpha*) 0); break;
  2077. }
  2078. }
  2079. template <typename IteratorType>
  2080. void fillWithGradient (IteratorType& iter, ColourGradient& gradient, const AffineTransform& trans, bool isIdentity) const
  2081. {
  2082. HeapBlock <PixelARGB> lookupTable;
  2083. const int numLookupEntries = gradient.createLookupTable (trans, lookupTable);
  2084. jassert (numLookupEntries > 0);
  2085. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2086. switch (destData.pixelFormat)
  2087. {
  2088. case Image::ARGB: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelARGB*) 0); break;
  2089. case Image::RGB: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelRGB*) 0); break;
  2090. default: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelAlpha*) 0); break;
  2091. }
  2092. }
  2093. //==============================================================================
  2094. Image image;
  2095. Font font;
  2096. private:
  2097. SoftwareRendererSavedState& operator= (const SoftwareRendererSavedState&);
  2098. };
  2099. //==============================================================================
  2100. template <class StateObjectType>
  2101. class SavedStateStack
  2102. {
  2103. public:
  2104. SavedStateStack (StateObjectType* const initialState) noexcept
  2105. : currentState (initialState)
  2106. {}
  2107. SavedStateStack() noexcept {}
  2108. void initialise (StateObjectType* state)
  2109. {
  2110. currentState = state;
  2111. }
  2112. inline StateObjectType* operator->() const noexcept { return currentState; }
  2113. inline StateObjectType& operator*() const noexcept { return *currentState; }
  2114. void save()
  2115. {
  2116. stack.add (new StateObjectType (*currentState));
  2117. }
  2118. void restore()
  2119. {
  2120. if (StateObjectType* const top = stack.getLast())
  2121. {
  2122. currentState = top;
  2123. stack.removeLast (1, false);
  2124. }
  2125. else
  2126. {
  2127. jassertfalse; // trying to pop with an empty stack!
  2128. }
  2129. }
  2130. void beginTransparencyLayer (float opacity)
  2131. {
  2132. save();
  2133. currentState = currentState->beginTransparencyLayer (opacity);
  2134. }
  2135. void endTransparencyLayer()
  2136. {
  2137. const ScopedPointer<StateObjectType> finishedTransparencyLayer (currentState);
  2138. restore();
  2139. currentState->endTransparencyLayer (*finishedTransparencyLayer);
  2140. }
  2141. private:
  2142. ScopedPointer<StateObjectType> currentState;
  2143. OwnedArray<StateObjectType> stack;
  2144. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedStateStack)
  2145. };
  2146. //==============================================================================
  2147. template <class SavedStateType>
  2148. class StackBasedLowLevelGraphicsContext : public LowLevelGraphicsContext
  2149. {
  2150. public:
  2151. bool isVectorDevice() const override { return false; }
  2152. void setOrigin (int x, int y) override { stack->transform.setOrigin (x, y); }
  2153. void addTransform (const AffineTransform& t) override { stack->transform.addTransform (t); }
  2154. float getScaleFactor() override { return stack->transform.getScaleFactor(); }
  2155. float getTargetDeviceScaleFactor() override { return stack->transform.getScaleFactor(); }
  2156. Rectangle<int> getClipBounds() const override { return stack->getClipBounds(); }
  2157. bool isClipEmpty() const override { return stack->clip == nullptr; }
  2158. bool clipRegionIntersects (const Rectangle<int>& r) override { return stack->clipRegionIntersects (r); }
  2159. bool clipToRectangle (const Rectangle<int>& r) override { return stack->clipToRectangle (r); }
  2160. bool clipToRectangleList (const RectangleList<int>& r) override { return stack->clipToRectangleList (r); }
  2161. void excludeClipRectangle (const Rectangle<int>& r) override { stack->excludeClipRectangle (r); }
  2162. void clipToPath (const Path& path, const AffineTransform& t) override { stack->clipToPath (path, t); }
  2163. void clipToImageAlpha (const Image& im, const AffineTransform& t) override { stack->clipToImageAlpha (im, t); }
  2164. void saveState() override { stack.save(); }
  2165. void restoreState() override { stack.restore(); }
  2166. void beginTransparencyLayer (float opacity) override { stack.beginTransparencyLayer (opacity); }
  2167. void endTransparencyLayer() override { stack.endTransparencyLayer(); }
  2168. void setFill (const FillType& fillType) override { stack->setFillType (fillType); }
  2169. void setOpacity (float newOpacity) override { stack->fillType.setOpacity (newOpacity); }
  2170. void setInterpolationQuality (Graphics::ResamplingQuality quality) override { stack->interpolationQuality = quality; }
  2171. void fillRect (const Rectangle<int>& r, bool replace) override { stack->fillRect (r, replace); }
  2172. void fillPath (const Path& path, const AffineTransform& t) override { stack->fillPath (path, t); }
  2173. void drawImage (const Image& im, const AffineTransform& t) override { stack->drawImage (im, t); }
  2174. void drawVerticalLine (int x, float top, float bottom) override { if (top < bottom) stack->fillRect (Rectangle<float> ((float) x, top, 1.0f, bottom - top)); }
  2175. void drawHorizontalLine (int y, float left, float right) override { if (left < right) stack->fillRect (Rectangle<float> (left, (float) y, right - left, 1.0f)); }
  2176. void drawGlyph (int glyphNumber, const AffineTransform& t) override { stack->drawGlyph (glyphNumber, t); }
  2177. void drawLine (const Line <float>& line) override { stack->drawLine (line); }
  2178. void setFont (const Font& newFont) override { stack->font = newFont; }
  2179. const Font& getFont() override { return stack->font; }
  2180. protected:
  2181. StackBasedLowLevelGraphicsContext (SavedStateType* initialState) : stack (initialState) {}
  2182. StackBasedLowLevelGraphicsContext() {}
  2183. RenderingHelpers::SavedStateStack<SavedStateType> stack;
  2184. };
  2185. }
  2186. #if JUCE_MSVC
  2187. #pragma warning (pop)
  2188. #endif
  2189. #endif // JUCE_RENDERINGHELPERS_H_INCLUDED