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     Copyright (C) 2013 Google, Inc. All rights reserved.
      8 
      9     This library is free software; you can redistribute it and/or
     10     modify it under the terms of the GNU Library General Public
     11     License as published by the Free Software Foundation; either
     12     version 2 of the License, or (at your option) any later version.
     13 
     14     This library is distributed in the hope that it will be useful,
     15     but WITHOUT ANY WARRANTY; without even the implied warranty of
     16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     17     Library General Public License for more details.
     18 
     19     You should have received a copy of the GNU Library General Public License
     20     along with this library; see the file COPYING.LIB.  If not, write to
     21     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     22     Boston, MA 02110-1301, USA.
     23 */
     24 
     25 #include "config.h"
     26 #include "core/css/CSSLengthFunctions.h"
     27 
     28 #include "core/rendering/RenderView.h"
     29 #include "platform/LayoutUnit.h"
     30 #include "platform/Length.h"
     31 #include "platform/LengthFunctions.h"
     32 
     33 namespace WebCore {
     34 
     35 int minimumIntValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
     36 {
     37     return static_cast<int>(minimumValueForLength(length, maximumValue, renderView, roundPercentages));
     38 }
     39 
     40 int intValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
     41 {
     42     return static_cast<int>(valueForLength(length, maximumValue, renderView, roundPercentages));
     43 }
     44 
     45 LayoutUnit minimumValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
     46 {
     47     switch (length.type()) {
     48     case Fixed:
     49         return length.value();
     50     case Percent:
     51         if (roundPercentages)
     52             return static_cast<LayoutUnit>(round(maximumValue * length.percent() / 100.0f));
     53         // Don't remove the extra cast to float. It is needed for rounding on 32-bit Intel machines that use the FPU stack.
     54         return static_cast<float>(maximumValue * length.percent() / 100.0f);
     55     case Calculated:
     56         return length.nonNanCalculatedValue(maximumValue);
     57     case ViewportPercentageWidth:
     58         return renderView ? renderView->viewportPercentageWidth(length.viewportPercentageLength()) : LayoutUnit(0);
     59     case ViewportPercentageHeight:
     60         return renderView ? renderView->viewportPercentageHeight(length.viewportPercentageLength()) : LayoutUnit(0);
     61     case ViewportPercentageMin:
     62         return renderView ? renderView->viewportPercentageMin(length.viewportPercentageLength()) : LayoutUnit(0);
     63     case ViewportPercentageMax:
     64         return renderView ? renderView->viewportPercentageMax(length.viewportPercentageLength()) : LayoutUnit(0);
     65     case FillAvailable:
     66     case Auto:
     67         return 0;
     68     case Intrinsic:
     69     case MinIntrinsic:
     70     case MinContent:
     71     case MaxContent:
     72     case FitContent:
     73     case ExtendToZoom:
     74     case Undefined:
     75         ASSERT_NOT_REACHED();
     76         return 0;
     77     }
     78     ASSERT_NOT_REACHED();
     79     return 0;
     80 }
     81 
     82 LayoutUnit valueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
     83 {
     84     switch (length.type()) {
     85     case Fixed:
     86     case Percent:
     87     case Calculated:
     88     case ViewportPercentageWidth:
     89     case ViewportPercentageHeight:
     90     case ViewportPercentageMin:
     91     case ViewportPercentageMax:
     92         return minimumValueForLength(length, maximumValue, renderView, roundPercentages);
     93     case FillAvailable:
     94     case Auto:
     95         return maximumValue;
     96     case Intrinsic:
     97     case MinIntrinsic:
     98     case MinContent:
     99     case MaxContent:
    100     case FitContent:
    101     case ExtendToZoom:
    102     case Undefined:
    103         ASSERT_NOT_REACHED();
    104         return 0;
    105     }
    106     ASSERT_NOT_REACHED();
    107     return 0;
    108 }
    109 
    110 // This method has code duplicated in platform/LengthFunctions.cpp.
    111 // Any changes here most likely also need to be applied there.
    112 float floatValueForLength(const Length& length, float maximumValue, RenderView* renderView)
    113 {
    114     if (!renderView)
    115         return floatValueForLength(length, maximumValue);
    116 
    117     switch (length.type()) {
    118     case Fixed:
    119         return length.getFloatValue();
    120     case Percent:
    121         return static_cast<float>(maximumValue * length.percent() / 100.0f);
    122     case FillAvailable:
    123     case Auto:
    124         return static_cast<float>(maximumValue);
    125     case Calculated:
    126         return length.nonNanCalculatedValue(maximumValue);
    127     case ViewportPercentageWidth:
    128         return static_cast<int>(renderView->viewportPercentageWidth(length.viewportPercentageLength()));
    129     case ViewportPercentageHeight:
    130         return static_cast<int>(renderView->viewportPercentageHeight(length.viewportPercentageLength()));
    131     case ViewportPercentageMin:
    132         return static_cast<int>(renderView->viewportPercentageMin(length.viewportPercentageLength()));
    133     case ViewportPercentageMax:
    134         return static_cast<int>(renderView->viewportPercentageMax(length.viewportPercentageLength()));
    135     case Intrinsic:
    136     case MinIntrinsic:
    137     case MinContent:
    138     case MaxContent:
    139     case FitContent:
    140     case ExtendToZoom:
    141     case Undefined:
    142         ASSERT_NOT_REACHED();
    143         return 0;
    144     }
    145     ASSERT_NOT_REACHED();
    146     return 0;
    147 }
    148 
    149 } // namespace WebCore
    150