Home | History | Annotate | Download | only in css
      1 /*
      2     Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3     Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
      4     Copyright (C) 2011 Rik Cabanier (cabanier (at) adobe.com)
      5     Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
      6     Copyright (C) 2012 Motorola Mobility, Inc. All rights reserved.
      7 
      8     This library is free software; you can redistribute it and/or
      9     modify it under the terms of the GNU Library General Public
     10     License as published by the Free Software Foundation; either
     11     version 2 of the License, or (at your option) any later version.
     12 
     13     This library is distributed in the hope that it will be useful,
     14     but WITHOUT ANY WARRANTY; without even the implied warranty of
     15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16     Library General Public License for more details.
     17 
     18     You should have received a copy of the GNU Library General Public License
     19     along with this library; see the file COPYING.LIB.  If not, write to
     20     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     21     Boston, MA 02110-1301, USA.
     22 */
     23 
     24 #include "config.h"
     25 #include "core/css/LengthFunctions.h"
     26 
     27 #include "core/platform/LayoutUnit.h"
     28 #include "core/platform/Length.h"
     29 #include "core/rendering/RenderView.h"
     30 
     31 namespace WebCore {
     32 
     33 int minimumIntValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
     34 {
     35     return static_cast<int>(minimumValueForLength(length, maximumValue, renderView, roundPercentages));
     36 }
     37 
     38 int intValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
     39 {
     40     return static_cast<int>(valueForLength(length, maximumValue, renderView, roundPercentages));
     41 }
     42 
     43 LayoutUnit minimumValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
     44 {
     45     switch (length.type()) {
     46     case Fixed:
     47         return length.value();
     48     case Percent:
     49         if (roundPercentages)
     50             return static_cast<LayoutUnit>(round(maximumValue * length.percent() / 100.0f));
     51         // Don't remove the extra cast to float. It is needed for rounding on 32-bit Intel machines that use the FPU stack.
     52         return static_cast<float>(maximumValue * length.percent() / 100.0f);
     53     case Calculated:
     54         return length.nonNanCalculatedValue(maximumValue);
     55     case ViewportPercentageWidth:
     56         if (renderView)
     57             return static_cast<LayoutUnit>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
     58         return 0;
     59     case ViewportPercentageHeight:
     60         if (renderView)
     61             return static_cast<LayoutUnit>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
     62         return 0;
     63     case ViewportPercentageMin:
     64         if (renderView) {
     65             IntSize viewportSize = renderView->viewportSize();
     66             return static_cast<LayoutUnit>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
     67         }
     68         return 0;
     69     case ViewportPercentageMax:
     70         if (renderView) {
     71             IntSize viewportSize = renderView->viewportSize();
     72             return static_cast<LayoutUnit>(std::max(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
     73         }
     74         return 0;
     75     case FillAvailable:
     76     case Auto:
     77         return 0;
     78     case Relative:
     79     case Intrinsic:
     80     case MinIntrinsic:
     81     case MinContent:
     82     case MaxContent:
     83     case FitContent:
     84     case ExtendToZoom:
     85     case Undefined:
     86         ASSERT_NOT_REACHED();
     87         return 0;
     88     }
     89     ASSERT_NOT_REACHED();
     90     return 0;
     91 }
     92 
     93 LayoutUnit valueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
     94 {
     95     switch (length.type()) {
     96     case Fixed:
     97     case Percent:
     98     case Calculated:
     99     case ViewportPercentageWidth:
    100     case ViewportPercentageHeight:
    101     case ViewportPercentageMin:
    102     case ViewportPercentageMax:
    103         return minimumValueForLength(length, maximumValue, renderView, roundPercentages);
    104     case FillAvailable:
    105     case Auto:
    106         return maximumValue;
    107     case Relative:
    108     case Intrinsic:
    109     case MinIntrinsic:
    110     case MinContent:
    111     case MaxContent:
    112     case FitContent:
    113     case ExtendToZoom:
    114     case Undefined:
    115         ASSERT_NOT_REACHED();
    116         return 0;
    117     }
    118     ASSERT_NOT_REACHED();
    119     return 0;
    120 }
    121 
    122 float floatValueForLength(const Length& length, float maximumValue, RenderView* renderView)
    123 {
    124     switch (length.type()) {
    125     case Fixed:
    126         return length.getFloatValue();
    127     case Percent:
    128         return static_cast<float>(maximumValue * length.percent() / 100.0f);
    129     case FillAvailable:
    130     case Auto:
    131         return static_cast<float>(maximumValue);
    132     case Calculated:
    133         return length.nonNanCalculatedValue(maximumValue);
    134     case ViewportPercentageWidth:
    135         if (renderView)
    136             return static_cast<int>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
    137         return 0;
    138     case ViewportPercentageHeight:
    139         if (renderView)
    140             return static_cast<int>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
    141         return 0;
    142     case ViewportPercentageMin:
    143         if (renderView) {
    144             IntSize viewportSize = renderView->viewportSize();
    145             return static_cast<int>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
    146         }
    147         return 0;
    148     case ViewportPercentageMax:
    149         if (renderView) {
    150             IntSize viewportSize = renderView->viewportSize();
    151             return static_cast<int>(std::max(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
    152         }
    153         return 0;
    154     case Relative:
    155     case Intrinsic:
    156     case MinIntrinsic:
    157     case MinContent:
    158     case MaxContent:
    159     case FitContent:
    160     case ExtendToZoom:
    161     case Undefined:
    162         ASSERT_NOT_REACHED();
    163         return 0;
    164     }
    165     ASSERT_NOT_REACHED();
    166     return 0;
    167 }
    168 
    169 } // namespace WebCore
    170