1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "nsViewportInfo.h"
8 #include "mozilla/Assertions.h"
9 #include <algorithm>
10
11 using namespace mozilla;
12
ConstrainViewportValues()13 void nsViewportInfo::ConstrainViewportValues() {
14 // Non-positive zoom factors can produce NaN or negative viewport sizes,
15 // so we better be sure our constraints will produce positive zoom factors.
16 MOZ_ASSERT(mMinZoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
17 MOZ_ASSERT(mMaxZoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
18
19 if (mDefaultZoom > mMaxZoom) {
20 mDefaultZoomValid = false;
21 mDefaultZoom = mMaxZoom;
22 }
23 if (mDefaultZoom < mMinZoom) {
24 mDefaultZoomValid = false;
25 mDefaultZoom = mMinZoom;
26 }
27 }
28
MinOrMax(const float & aA,const float & aB,const float & (* aMinOrMax)(const float &,const float &))29 static const float& MinOrMax(const float& aA, const float& aB,
30 const float& (*aMinOrMax)(const float&,
31 const float&)) {
32 MOZ_ASSERT(
33 aA != nsViewportInfo::ExtendToZoom && aA != nsViewportInfo::DeviceSize &&
34 aB != nsViewportInfo::ExtendToZoom && aB != nsViewportInfo::DeviceSize);
35 if (aA == nsViewportInfo::Auto) {
36 return aB;
37 }
38 if (aB == nsViewportInfo::Auto) {
39 return aA;
40 }
41 return aMinOrMax(aA, aB);
42 }
43
44 // static
Min(const float & aA,const float & aB)45 const float& nsViewportInfo::Min(const float& aA, const float& aB) {
46 return MinOrMax(aA, aB, std::min);
47 }
48
49 // static
Max(const float & aA,const float & aB)50 const float& nsViewportInfo::Max(const float& aA, const float& aB) {
51 return MinOrMax(aA, aB, std::max);
52 }
53