193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)/*
293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * modification, are permitted provided that the following conditions are
693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * met:
793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
1093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
1193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
1293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * in the documentation and/or other materials provided with the
1393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * distribution.
1493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
1593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
1693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * this software without specific prior written permission.
1793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
1893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) */
3093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#include "config.h"
328abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#include "core/page/PageScaleConstraints.h"
338abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
348abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#include <algorithm>
3593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
36c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
3793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)PageScaleConstraints::PageScaleConstraints()
3993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    : initialScale(-1), minimumScale(-1), maximumScale(-1) { }
4093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
4193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)PageScaleConstraints::PageScaleConstraints(float initial, float minimum, float maximum)
4293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    : initialScale(initial), minimumScale(minimum), maximumScale(maximum) { }
4393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
4493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)void PageScaleConstraints::overrideWith(const PageScaleConstraints& other)
4593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
4693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (other.initialScale != -1) {
4793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        initialScale = other.initialScale;
4893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        if (minimumScale != -1)
4993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            minimumScale = std::min(minimumScale, other.initialScale);
5093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
5193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (other.minimumScale != -1)
5293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        minimumScale = other.minimumScale;
5393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (other.maximumScale != -1)
5493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        maximumScale = other.maximumScale;
5593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (!other.layoutSize.isEmpty())
5693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        layoutSize = other.layoutSize;
5793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    clampAll();
5893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
5993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
6093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)float PageScaleConstraints::clampToConstraints(float pageScaleFactor) const
6193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
6293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (pageScaleFactor == -1)
6393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return pageScaleFactor;
6493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (minimumScale != -1)
6593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        pageScaleFactor = std::max(pageScaleFactor, minimumScale);
6693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (maximumScale != -1)
6793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        pageScaleFactor = std::min(pageScaleFactor, maximumScale);
6893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    return pageScaleFactor;
6993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
7093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
7193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)void PageScaleConstraints::clampAll()
7293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
7393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (minimumScale != -1 && maximumScale != -1)
7493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        maximumScale = std::max(minimumScale, maximumScale);
7593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    initialScale = clampToConstraints(initialScale);
7693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
7793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
7893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)void PageScaleConstraints::fitToContentsWidth(float contentsWidth, int viewWidthNotIncludingScrollbars)
7993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
8093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (!contentsWidth || !viewWidthNotIncludingScrollbars)
8193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return;
8293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
8393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // Clamp the minimum scale so that the viewport can't exceed the document
8493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // width.
8593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    minimumScale = std::max(minimumScale, viewWidthNotIncludingScrollbars / contentsWidth);
8693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
8793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // If the initial scale wasn't defined, set it to minimum scale now that we
8893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // know the real value.
8993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    if (initialScale == -1)
9093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        initialScale = minimumScale;
9193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
9293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    clampAll();
9393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
9493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
9593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)bool PageScaleConstraints::operator==(const PageScaleConstraints& other) const
9693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
9793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    return layoutSize == other.layoutSize
9893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        && initialScale == other.initialScale
9993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        && minimumScale == other.minimumScale
10093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        && maximumScale == other.maximumScale;
10193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
10293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
103c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
104