From: L. David Baron Bug 989802 - Round viewport units to appunits using floor rather than round so that repeated uses fit within a container. diff --git a/layout/style/nsRuleNode.cpp b/layout/style/nsRuleNode.cpp --- a/layout/style/nsRuleNode.cpp +++ b/layout/style/nsRuleNode.cpp @@ -218,19 +218,27 @@ struct CalcLengthCalcOps : public css::B result_type ComputeLeafValue(const nsCSSValue& aValue) { return CalcLengthWith(aValue, mFontSize, mStyleFont, mStyleContext, mPresContext, mUseProvidedRootEmSize, mUseUserFontSet, mCanStoreInRuleTree); } }; -static inline nscoord ScaleCoord(const nsCSSValue &aValue, float factor) -{ - return NSToCoordRoundWithClamp(aValue.GetFloatValue() * factor); +static inline nscoord ScaleCoord(const nsCSSValue& aValue, float aFactor) +{ + return NSToCoordRoundWithClamp(aValue.GetFloatValue() * aFactor); +} + +static inline nscoord ScaleCoordFloor(const nsCSSValue& aValue, float aFactor) +{ + // For units (like percentages and viewport units) where authors might + // repeatedly use a value and expect some multiple of the value to be + // smaller than a container, we need to use floor rather than round. + return NSToCoordFloorClamped(aValue.GetFloatValue() * aFactor); } already_AddRefed GetMetricsFor(nsPresContext* aPresContext, nsStyleContext* aStyleContext, const nsStyleFont* aStyleFont, nscoord aFontSize, // overrides value from aStyleFont bool aUseUserFontSet) @@ -346,28 +354,32 @@ static nscoord CalcLengthWith(const nsCS // throws away the style context and the rule tree. // Thus we do cache viewport units in the rule tree. This allows us // to benefit from the performance advantages of the rule tree // (e.g., faster dynamic changes on other things, like transforms) // and allows us not to need an additional code path, in exchange // for an increased cost to dynamic changes to the viewport size // when viewport units are in use. case eCSSUnit_ViewportWidth: { - return ScaleCoord(aValue, 0.01f * CalcViewportUnitsScale(aPresContext).width); + nscoord viewportWidth = CalcViewportUnitsScale(aPresContext).width; + return ScaleCoordFloor(aValue, 0.01f * viewportWidth); } case eCSSUnit_ViewportHeight: { - return ScaleCoord(aValue, 0.01f * CalcViewportUnitsScale(aPresContext).height); + nscoord viewportHeight = CalcViewportUnitsScale(aPresContext).height; + return ScaleCoordFloor(aValue, 0.01f * viewportHeight); } case eCSSUnit_ViewportMin: { nsSize vuScale(CalcViewportUnitsScale(aPresContext)); - return ScaleCoord(aValue, 0.01f * min(vuScale.width, vuScale.height)); + nscoord viewportMin = min(vuScale.width, vuScale.height); + return ScaleCoordFloor(aValue, 0.01f * viewportMin); } case eCSSUnit_ViewportMax: { nsSize vuScale(CalcViewportUnitsScale(aPresContext)); - return ScaleCoord(aValue, 0.01f * max(vuScale.width, vuScale.height)); + nscoord viewportMax = max(vuScale.width, vuScale.height); + return ScaleCoordFloor(aValue, 0.01f * viewportMax); } // While we could deal with 'rem' units correctly by simply not // caching any data that uses them in the rule tree, it's valuable // to store them in the rule tree (for faster dynamic changes of // other things). And since the font size of the root element // changes rarely, we instead handle dynamic changes to the root // element's font size by rebuilding all style data in // nsCSSFrameConstructor::RestyleElement.