2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
30 #include "juce_RectanglePlacement.h"
33 //==============================================================================
34 RectanglePlacement::RectanglePlacement (const RectanglePlacement
& other
) noexcept
39 RectanglePlacement
& RectanglePlacement::operator= (const RectanglePlacement
& other
) noexcept
45 bool RectanglePlacement::operator== (const RectanglePlacement
& other
) const noexcept
47 return flags
== other
.flags
;
50 bool RectanglePlacement::operator!= (const RectanglePlacement
& other
) const noexcept
52 return flags
!= other
.flags
;
55 void RectanglePlacement::applyTo (double& x
, double& y
, double& w
, double& h
,
56 const double dx
, const double dy
, const double dw
, const double dh
) const noexcept
61 if ((flags
& stretchToFit
) != 0)
70 double scale
= (flags
& fillDestination
) != 0 ? jmax (dw
/ w
, dh
/ h
)
71 : jmin (dw
/ w
, dh
/ h
);
73 if ((flags
& onlyReduceInSize
) != 0)
74 scale
= jmin (scale
, 1.0);
76 if ((flags
& onlyIncreaseInSize
) != 0)
77 scale
= jmax (scale
, 1.0);
82 if ((flags
& xLeft
) != 0)
84 else if ((flags
& xRight
) != 0)
87 x
= dx
+ (dw
- w
) * 0.5;
89 if ((flags
& yTop
) != 0)
91 else if ((flags
& yBottom
) != 0)
94 y
= dy
+ (dh
- h
) * 0.5;
98 const AffineTransform
RectanglePlacement::getTransformToFit (const Rectangle
<float>& source
, const Rectangle
<float>& destination
) const noexcept
100 if (source
.isEmpty())
101 return AffineTransform::identity
;
103 float newX
= destination
.getX();
104 float newY
= destination
.getY();
106 float scaleX
= destination
.getWidth() / source
.getWidth();
107 float scaleY
= destination
.getHeight() / source
.getHeight();
109 if ((flags
& stretchToFit
) == 0)
111 scaleX
= (flags
& fillDestination
) != 0 ? jmax (scaleX
, scaleY
)
112 : jmin (scaleX
, scaleY
);
114 if ((flags
& onlyReduceInSize
) != 0)
115 scaleX
= jmin (scaleX
, 1.0f
);
117 if ((flags
& onlyIncreaseInSize
) != 0)
118 scaleX
= jmax (scaleX
, 1.0f
);
122 if ((flags
& xRight
) != 0)
123 newX
+= destination
.getWidth() - source
.getWidth() * scaleX
; // right
124 else if ((flags
& xLeft
) == 0)
125 newX
+= (destination
.getWidth() - source
.getWidth() * scaleX
) / 2.0f
; // centre
127 if ((flags
& yBottom
) != 0)
128 newY
+= destination
.getHeight() - source
.getHeight() * scaleX
; // bottom
129 else if ((flags
& yTop
) == 0)
130 newY
+= (destination
.getHeight() - source
.getHeight() * scaleX
) / 2.0f
; // centre
133 return AffineTransform::translation (-source
.getX(), -source
.getY())
134 .scaled (scaleX
, scaleY
)
135 .translated (newX
, newY
);