LengthFunctions.cpp [plain text]
#include "config.h"
#include "LengthFunctions.h"
#include "Length.h"
#include "RenderView.h"
namespace WebCore {
int minimumIntValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
{
return static_cast<int>(minimumValueForLength(length, maximumValue, renderView, roundPercentages));
}
int intValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
{
return static_cast<int>(valueForLength(length, maximumValue, renderView, roundPercentages));
}
LayoutUnit minimumValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
{
switch (length.type()) {
case Fixed:
return length.value();
case Percent:
if (roundPercentages)
return static_cast<LayoutUnit>(round(maximumValue * length.percent() / 100.0f));
return static_cast<LayoutUnit>(static_cast<float>(maximumValue * length.percent() / 100.0f));
case Calculated:
return length.nonNanCalculatedValue(maximumValue);
case ViewportPercentageWidth:
if (renderView)
return static_cast<LayoutUnit>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
return ZERO_LAYOUT_UNIT;
case ViewportPercentageHeight:
if (renderView)
return static_cast<LayoutUnit>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
return ZERO_LAYOUT_UNIT;
case ViewportPercentageMin:
if (renderView) {
IntSize viewportSize = renderView->viewportSize();
return static_cast<LayoutUnit>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
}
return ZERO_LAYOUT_UNIT;
case Auto:
return ZERO_LAYOUT_UNIT;
case Relative:
case Intrinsic:
case MinIntrinsic:
case Undefined:
ASSERT_NOT_REACHED();
return ZERO_LAYOUT_UNIT;
}
ASSERT_NOT_REACHED();
return ZERO_LAYOUT_UNIT;
}
LayoutUnit valueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
{
switch (length.type()) {
case Fixed:
case Percent:
case Calculated:
case ViewportPercentageWidth:
case ViewportPercentageHeight:
case ViewportPercentageMin:
return minimumValueForLength(length, maximumValue, renderView, roundPercentages);
case Auto:
return maximumValue;
case Relative:
ASSERT_NOT_REACHED();
return ZERO_LAYOUT_UNIT;
case Intrinsic:
ASSERT_NOT_REACHED();
return ZERO_LAYOUT_UNIT;
case MinIntrinsic:
ASSERT_NOT_REACHED();
return ZERO_LAYOUT_UNIT;
case Undefined:
ASSERT_NOT_REACHED();
return ZERO_LAYOUT_UNIT;
}
ASSERT_NOT_REACHED();
return ZERO_LAYOUT_UNIT;
}
float floatValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView)
{
switch (length.type()) {
case Fixed:
return length.getFloatValue();
case Percent:
return static_cast<float>(maximumValue * length.percent() / 100.0f);
case Auto:
return static_cast<float>(maximumValue);
case Calculated:
return length.nonNanCalculatedValue(maximumValue);
case ViewportPercentageWidth:
if (renderView)
return static_cast<int>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
return 0;
case ViewportPercentageHeight:
if (renderView)
return static_cast<int>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
return 0;
case ViewportPercentageMin:
if (renderView) {
IntSize viewportSize = renderView->viewportSize();
return static_cast<int>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
}
return 0;
case Relative:
case Intrinsic:
case MinIntrinsic:
case Undefined:
ASSERT_NOT_REACHED();
return 0;
}
ASSERT_NOT_REACHED();
return 0;
}
float floatValueForLength(const Length& length, float maximumValue, RenderView* renderView)
{
switch (length.type()) {
case Fixed:
return length.getFloatValue();
case Percent:
return static_cast<float>(maximumValue * length.percent() / 100.0f);
case Auto:
return static_cast<float>(maximumValue);
case Calculated:
return length.nonNanCalculatedValue(maximumValue);
case ViewportPercentageWidth:
if (renderView)
return static_cast<int>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
return 0;
case ViewportPercentageHeight:
if (renderView)
return static_cast<int>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
return 0;
case ViewportPercentageMin:
if (renderView) {
IntSize viewportSize = renderView->viewportSize();
return static_cast<int>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
}
return 0;
case Relative:
case Intrinsic:
case MinIntrinsic:
case Undefined:
ASSERT_NOT_REACHED();
return 0;
}
ASSERT_NOT_REACHED();
return 0;
}
}