OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "ViewportAttributesManager.h" |
| 33 |
| 34 using namespace WebCore; |
| 35 |
| 36 namespace WebKit { |
| 37 |
| 38 static const float defaultMinimumScale = 0.25f; |
| 39 static const float defaultMaximumScale = 5.0f; |
| 40 |
| 41 ViewportAttributesManager::ViewportAttributesManager() |
| 42 : m_lastContentsWidth(0) |
| 43 , m_needsReset(false) |
| 44 , m_constraintsDirty(false) |
| 45 { |
| 46 m_finalConstraints = defaultConstraints(); |
| 47 } |
| 48 |
| 49 ViewportAttributes ViewportAttributesManager::defaultConstraints() const |
| 50 { |
| 51 return ViewportAttributes(-1, defaultMinimumScale, defaultMaximumScale); |
| 52 } |
| 53 |
| 54 void ViewportAttributesManager::updatePageDefinedConstraints(const ViewportArgum
ents& arguments, IntSize viewSize, int layoutFallbackWidth) |
| 55 { |
| 56 m_pageDefinedConstraints = arguments.resolve(viewSize, viewSize, layoutFallb
ackWidth); |
| 57 |
| 58 // Only set initialScale to a value if it was explicitly set. |
| 59 if (arguments.zoom == ViewportArguments::ValueAuto) |
| 60 m_pageDefinedConstraints.initialScale = -1; |
| 61 |
| 62 m_constraintsDirty = true; |
| 63 } |
| 64 |
| 65 void ViewportAttributesManager::setUserAgentConstraints(ViewportAttributes userA
gentConstraints) |
| 66 { |
| 67 m_userAgentConstraints = userAgentConstraints; |
| 68 m_constraintsDirty = true; |
| 69 } |
| 70 |
| 71 void ViewportAttributesManager::computeFinalConstraints() |
| 72 { |
| 73 m_finalConstraints = defaultConstraints(); |
| 74 m_finalConstraints.overrideWith(m_pageDefinedConstraints); |
| 75 m_finalConstraints.overrideWith(m_userAgentConstraints); |
| 76 |
| 77 m_constraintsDirty = false; |
| 78 } |
| 79 |
| 80 void ViewportAttributesManager::adjustFinalConstraintsToContentsSize(IntSize vie
wSize, IntSize contentsSize, int nonOverlayScrollbarWidth) |
| 81 { |
| 82 if (!viewSize.width() || !contentsSize.width()) |
| 83 return; |
| 84 |
| 85 // Clamp the minimum scale so that the viewport can't exceed the document |
| 86 // width. |
| 87 float viewWidthNotIncludingScrollbars = viewSize.width() - nonOverlayScrollb
arWidth; |
| 88 m_finalConstraints.minimumScale = std::max(m_finalConstraints.minimumScale,
viewWidthNotIncludingScrollbars / contentsSize.width()); |
| 89 |
| 90 // If the initial scale wasn't defined by any of the attributes, set |
| 91 // it to minimum scale now that we know the real value. |
| 92 if (m_finalConstraints.initialScale == -1) |
| 93 m_finalConstraints.initialScale = m_finalConstraints.minimumScale; |
| 94 } |
| 95 |
| 96 void ViewportAttributesManager::setNeedsReset(bool needsReset) |
| 97 { |
| 98 m_needsReset = needsReset; |
| 99 if (needsReset) |
| 100 m_constraintsDirty = true; |
| 101 } |
| 102 |
| 103 void ViewportAttributesManager::didChangeContentsSize(IntSize contentsSize, floa
t pageScaleFactor) |
| 104 { |
| 105 // If a large fixed-width element expanded the size of the document |
| 106 // late in loading and our initial scale is not constrained, reset the |
| 107 // page scale factor to the new minimum scale. |
| 108 if (contentsSize.width() > m_lastContentsWidth |
| 109 && pageScaleFactor == finalConstraints().minimumScale |
| 110 && userAgentConstraints().initialScale == -1 && pageDefinedConstraints()
.initialScale == -1) |
| 111 setNeedsReset(true); |
| 112 |
| 113 m_constraintsDirty = true; |
| 114 m_lastContentsWidth = contentsSize.width(); |
| 115 } |
| 116 |
| 117 static float computeDeprecatedTargetDensityDPIFactor(const ViewportArguments& ar
guments, float deviceScaleFactor) |
| 118 { |
| 119 if (arguments.deprecatedTargetDensityDPI == ViewportArguments::ValueDeviceDP
I) |
| 120 return 1.0f / deviceScaleFactor; |
| 121 |
| 122 float targetDPI = -1.0f; |
| 123 if (arguments.deprecatedTargetDensityDPI == ViewportArguments::ValueLowDPI) |
| 124 targetDPI = 120.0f; |
| 125 else if (arguments.deprecatedTargetDensityDPI == ViewportArguments::ValueMed
iumDPI) |
| 126 targetDPI = 160.0f; |
| 127 else if (arguments.deprecatedTargetDensityDPI == ViewportArguments::ValueHig
hDPI) |
| 128 targetDPI = 240.0f; |
| 129 else if (arguments.deprecatedTargetDensityDPI != ViewportArguments::ValueAut
o) |
| 130 targetDPI = arguments.deprecatedTargetDensityDPI; |
| 131 return targetDPI > 0 ? (deviceScaleFactor * 120.0f) / targetDPI : 1.0f; |
| 132 } |
| 133 |
| 134 static float getLayoutWidthForNonWideViewport(const FloatSize& deviceSize, float
initialScale) |
| 135 { |
| 136 return initialScale == -1 ? deviceSize.width() : deviceSize.width() / initia
lScale; |
| 137 } |
| 138 |
| 139 void ViewportAttributesManager::adjustPageDefinedConstraintsForAndroidWebView(co
nst ViewportArguments& arguments, IntSize viewSize, int layoutFallbackWidth, flo
at deviceScaleFactor, bool useWideViewport, bool initializeAtMinimumPageScale) |
| 140 { |
| 141 float initialScale = m_pageDefinedConstraints.initialScale; |
| 142 if (arguments.zoom == -1 && !initializeAtMinimumPageScale) { |
| 143 if (arguments.width == -1 || (useWideViewport && arguments.width != View
portArguments::ValueDeviceWidth)) |
| 144 m_pageDefinedConstraints.initialScale = 1.0f; |
| 145 } |
| 146 |
| 147 float targetDensityDPIFactor = computeDeprecatedTargetDensityDPIFactor(argum
ents, deviceScaleFactor); |
| 148 if (m_pageDefinedConstraints.initialScale != -1) |
| 149 m_pageDefinedConstraints.initialScale *= targetDensityDPIFactor; |
| 150 m_pageDefinedConstraints.minimumScale *= targetDensityDPIFactor; |
| 151 m_pageDefinedConstraints.maximumScale *= targetDensityDPIFactor; |
| 152 |
| 153 if (useWideViewport && arguments.width == -1 && arguments.zoom != 1.0f) |
| 154 m_pageDefinedConstraints.layoutSize.setWidth(layoutFallbackWidth); |
| 155 else { |
| 156 if (!useWideViewport) |
| 157 m_pageDefinedConstraints.layoutSize.setWidth(getLayoutWidthForNonWid
eViewport(viewSize, initialScale)); |
| 158 if (!useWideViewport || arguments.width == -1 || arguments.width == View
portArguments::ValueDeviceWidth) |
| 159 m_pageDefinedConstraints.layoutSize.scale(1.0f / targetDensityDPIFac
tor); |
| 160 } |
| 161 } |
| 162 |
| 163 } // namespace WebKit |
OLD | NEW |