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