Bug 1645920 [wpt PR 24157] - [AspectRatio] Make intrinsic sizes respect aspect-ratio...
[gecko.git] / image / ImageRegion.h
bloba3471a942e2e234ab85b34efc2c046c826c766e6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_image_ImageRegion_h
7 #define mozilla_image_ImageRegion_h
9 #include "gfxRect.h"
10 #include "mozilla/gfx/Types.h"
12 namespace mozilla {
13 namespace image {
15 /**
16 * An axis-aligned rectangle in tiled image space, with an optional sampling
17 * restriction rect. The drawing code ensures that if a sampling restriction
18 * rect is present, any pixels sampled during the drawing process are found
19 * within that rect.
21 * The sampling restriction rect exists primarily for callers which perform
22 * pixel snapping. Other callers should generally use one of the Create()
23 * overloads.
25 class ImageRegion {
26 typedef mozilla::gfx::ExtendMode ExtendMode;
28 public:
29 static ImageRegion Empty() {
30 return ImageRegion(gfxRect(), ExtendMode::CLAMP);
33 static ImageRegion Create(const gfxRect& aRect,
34 ExtendMode aExtendMode = ExtendMode::CLAMP) {
35 return ImageRegion(aRect, aExtendMode);
38 static ImageRegion Create(const gfxSize& aSize,
39 ExtendMode aExtendMode = ExtendMode::CLAMP) {
40 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
43 static ImageRegion Create(const nsIntSize& aSize,
44 ExtendMode aExtendMode = ExtendMode::CLAMP) {
45 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
48 static ImageRegion CreateWithSamplingRestriction(
49 const gfxRect& aRect, const gfxRect& aRestriction,
50 ExtendMode aExtendMode = ExtendMode::CLAMP) {
51 return ImageRegion(aRect, aRestriction, aExtendMode);
54 bool IsRestricted() const { return mIsRestricted; }
55 const gfxRect& Rect() const { return mRect; }
57 const gfxRect& Restriction() const {
58 MOZ_ASSERT(mIsRestricted);
59 return mRestriction;
62 bool RestrictionContains(const gfxRect& aRect) const {
63 if (!mIsRestricted) {
64 return true;
66 return mRestriction.Contains(aRect);
69 ImageRegion Intersect(const gfxRect& aRect) const {
70 if (mIsRestricted) {
71 return CreateWithSamplingRestriction(aRect.Intersect(mRect),
72 aRect.Intersect(mRestriction));
74 return Create(aRect.Intersect(mRect));
77 gfxRect IntersectAndRestrict(const gfxRect& aRect) const {
78 gfxRect intersection = mRect.Intersect(aRect);
79 if (mIsRestricted) {
80 intersection = mRestriction.Intersect(intersection);
82 return intersection;
85 void MoveBy(gfxFloat dx, gfxFloat dy) {
86 mRect.MoveBy(dx, dy);
87 if (mIsRestricted) {
88 mRestriction.MoveBy(dx, dy);
92 void Scale(gfxFloat sx, gfxFloat sy) {
93 mRect.Scale(sx, sy);
94 if (mIsRestricted) {
95 mRestriction.Scale(sx, sy);
99 void TransformBy(const gfxMatrix& aMatrix) {
100 mRect = aMatrix.TransformRect(mRect);
101 if (mIsRestricted) {
102 mRestriction = aMatrix.TransformRect(mRestriction);
106 void TransformBoundsBy(const gfxMatrix& aMatrix) {
107 mRect = aMatrix.TransformBounds(mRect);
108 if (mIsRestricted) {
109 mRestriction = aMatrix.TransformBounds(mRestriction);
113 ImageRegion operator-(const gfxPoint& aPt) const {
114 if (mIsRestricted) {
115 return CreateWithSamplingRestriction(mRect - aPt, mRestriction - aPt);
117 return Create(mRect - aPt);
120 ImageRegion operator+(const gfxPoint& aPt) const {
121 if (mIsRestricted) {
122 return CreateWithSamplingRestriction(mRect + aPt, mRestriction + aPt);
124 return Create(mRect + aPt);
127 gfx::ExtendMode GetExtendMode() const { return mExtendMode; }
129 /* ImageRegion() : mIsRestricted(false) { } */
131 private:
132 explicit ImageRegion(const gfxRect& aRect, ExtendMode aExtendMode)
133 : mRect(aRect), mExtendMode(aExtendMode), mIsRestricted(false) {}
135 ImageRegion(const gfxRect& aRect, const gfxRect& aRestriction,
136 ExtendMode aExtendMode)
137 : mRect(aRect),
138 mRestriction(aRestriction),
139 mExtendMode(aExtendMode),
140 mIsRestricted(true) {}
142 gfxRect mRect;
143 gfxRect mRestriction;
144 ExtendMode mExtendMode;
145 bool mIsRestricted;
148 } // namespace image
149 } // namespace mozilla
151 #endif // mozilla_image_ImageRegion_h