Add snap to css3-images image-resolution
https://bugs.webkit.org/show_bug.cgi?id=89745

Patch by David Barr <davidbarr@chromium.org> on 2012-06-24
Reviewed by Tony Chang.

Source/WebCore:

Due to floating point imprecision, it is difficult to be precise in dpcm.
So use PrimitiveValue::roundForImpreciseConversion rather than just floor.

No new tests; extended fast/css/image-resolution/image-resolution.html

* css/CSSParser.cpp: Accept snap identifier in image-resolution property.
(WebCore::CSSParser::parseImageResolution): Map CSSValueSnap to identifier value from cssValuePool.
* css/CSSValueKeywords.in: Add snap.
* css/StyleBuilder.cpp: Extend ApplyPropertyImageResolution to apply RenderStyle::imageResolutionSnap.
(WebCore::ApplyPropertyImageResolution::applyInheritValue): Apply RenderStyle::imageResolutionSnap.
(WebCore::ApplyPropertyImageResolution::applyInitialValue): Apply RenderStyle::imageResolutionSnap.
(WebCore::ApplyPropertyImageResolution::applyValue): Map CSSValueSnap to ImageResolutionSnapPixels.
* rendering/RenderImage.cpp: Extend conditions for recalculation of intrinsic size.
(WebCore::RenderImage::styleDidChange): Update intrinsic size if RenderStyle::imageResolutionSnap() has changed.
(WebCore::RenderImage::imageDimensionsChanged): Floor scale factor to int, round up if less than 0.01 away from ceiling.
* rendering/style/RenderStyle.cpp: Include StyleRareInheritedData::m_imageResolutionSnap in style diff.
(WebCore::RenderStyle::diff): Map change in StyleRareInheritedData::m_imageResolutionSnap to StyleDifferenceLayout.
* rendering/style/RenderStyle.h: Add RenderStyle::imageResolutionSnap, RenderStyle::setImageResolutionSnap, RenderStyle::initialImageResolutionSnap.
* rendering/style/RenderStyleConstants.h: Add enum ImageResolutionSnap.
* rendering/style/StyleRareInheritedData.cpp: Add StyleRareInheritedData::m_imageResolutionSnap.
(WebCore::StyleRareInheritedData::StyleRareInheritedData): Add m_imageResolutionSnap to default and copy constructor.
(WebCore::StyleRareInheritedData::operator==): Include m_imageResolutionSnap in comparison.
* rendering/style/StyleRareInheritedData.h: Add StyleRareInheritedData::m_imageResolutionSnap.
(StyleRareInheritedData): Add 1-bit field StyleRareInheritedData::m_imageResolutionSnap.

LayoutTests:

* fast/css/image-resolution/image-resolution-expected.txt:
* fast/css/image-resolution/image-resolution.html:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@121127 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/rendering/RenderImage.cpp b/Source/WebCore/rendering/RenderImage.cpp
index e10f908..e3aea91 100644
--- a/Source/WebCore/rendering/RenderImage.cpp
+++ b/Source/WebCore/rendering/RenderImage.cpp
@@ -140,6 +140,7 @@
 #if ENABLE(CSS_IMAGE_RESOLUTION)
     if (diff == StyleDifferenceLayout
         && (oldStyle->imageResolution() != style()->imageResolution()
+            || oldStyle->imageResolutionSnap() != style()->imageResolutionSnap()
             || oldStyle->imageResolutionSource() != style()->imageResolutionSource()))
         imageDimensionsChanged(true /* imageSizeChanged */);
 #endif
@@ -198,7 +199,12 @@
 void RenderImage::imageDimensionsChanged(bool imageSizeChanged, const IntRect* rect)
 {
 #if ENABLE(CSS_IMAGE_RESOLUTION)
-    bool intrinsicSizeChanged = updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom() / style()->imageResolution()), imageSizeChanged);
+    double scale = style()->imageResolution();
+    if (style()->imageResolutionSnap() == ImageResolutionSnapPixels)
+        scale = roundForImpreciseConversion<int>(scale);
+    if (scale <= 0)
+        scale = 1;
+    bool intrinsicSizeChanged = updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom() / scale), imageSizeChanged);
 #else
     bool intrinsicSizeChanged = updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom()), imageSizeChanged);
 #endif