WebCore:
authorbdakin@apple.com <bdakin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 19 Dec 2007 07:53:52 +0000 (07:53 +0000)
committerbdakin@apple.com <bdakin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 19 Dec 2007 07:53:52 +0000 (07:53 +0000)
        Reviewed by Oliver.

        Fix for <rdar://problem/5616982> SVGs with width and height 100%
        fail to render when used as <img> or CSS image (16167)

        This final part of the work fixes the <img> tag.

        This is the real fix.
        * rendering/RenderImage.cpp:
        (WebCore::RenderImage::calcReplacedWidth): Set the container size
        on the image. Setting the container size only actually sticks if
        the values are non-zero, so if the container size really was set,
        use the imageSize that is calculated using the container size. If
        it did not stick but the image does have relative width (meaning
        that the container size is 0), set the width to 0 by hand. We want
        to avoid setting the width before we have a container size or we
        will end up incorrectly using the default size of 300x150.
        (WebCore::RenderImage::calcReplacedHeight): Same as above, but for
        height.

        A few more pieces of information have to be exposed through cached
        image to make this happen.
        * loader/CachedImage.cpp:
        (WebCore::CachedImage::usesImageContainerSize): As mentioned above,
        when setContainerSize() is called, the container size is only
        actually set if the values are non-zero. This call tells you if it
        was set.
        (WebCore::CachedImage::imageHasRelativeWidth):
        (WebCore::CachedImage::imageHasRelativeHeight):
        * loader/CachedImage.h:
        * platform/graphics/Image.h:
        (WebCore::Image::usesContainerSize):
        * svg/graphics/SVGImage.cpp:
        (WebCore::SVGImage::usesContainerSize):
        * svg/graphics/SVGImage.h:

LayoutTests:

        Reviewed by Oliver.

        Test for <rdar://problem/5616982> SVGs with width and height 100%
        fail to render when used as <img> or CSS image (16167)

        * fast/images/resources/green-relative-size-rect.svg: Added.
        * fast/images/svg-as-relative-image.html: Added.
        * platform/mac/fast/images/svg-as-relative-image-expected.checksum: Added.
        * platform/mac/fast/images/svg-as-relative-image-expected.png: Added.
        * platform/mac/fast/images/svg-as-relative-image-expected.txt: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@28856 268f45cc-cd09-0410-ab3c-d52691b4dbfc

13 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/images/resources/green-relative-size-rect.svg [new file with mode: 0644]
LayoutTests/fast/images/svg-as-relative-image.html [new file with mode: 0644]
LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.checksum [new file with mode: 0644]
LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.png [new file with mode: 0644]
LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.txt [new file with mode: 0644]
WebCore/ChangeLog
WebCore/loader/CachedImage.cpp
WebCore/loader/CachedImage.h
WebCore/platform/graphics/Image.h
WebCore/rendering/RenderImage.cpp
WebCore/svg/graphics/SVGImage.cpp
WebCore/svg/graphics/SVGImage.h

index 0b799b6fda7944a3e577e53239cd85cafbb90069..8f584e9ea3aa6c084095bfb20213ea35b8a884af 100644 (file)
@@ -1,3 +1,16 @@
+2007-12-18  Beth Dakin  <bdakin@apple.com>
+
+        Reviewed by Oliver.
+
+        Test for <rdar://problem/5616982> SVGs with width and height 100% 
+        fail to render when used as <img> or CSS image (16167)
+
+        * fast/images/resources/green-relative-size-rect.svg: Added.
+        * fast/images/svg-as-relative-image.html: Added.
+        * platform/mac/fast/images/svg-as-relative-image-expected.checksum: Added.
+        * platform/mac/fast/images/svg-as-relative-image-expected.png: Added.
+        * platform/mac/fast/images/svg-as-relative-image-expected.txt: Added.
+
 2007-12-18  Dan Bernstein  <mitz@apple.com>
 
         Reviewed by John Sullivan.
diff --git a/LayoutTests/fast/images/resources/green-relative-size-rect.svg b/LayoutTests/fast/images/resources/green-relative-size-rect.svg
new file mode 100644 (file)
index 0000000..9eec19d
--- /dev/null
@@ -0,0 +1,3 @@
+<svg xmlns="http://www.w3.org/2000/svg" width='100%' height='100%'>
+    <rect fill='green' width='100px' height='100px' />
+</svg>
diff --git a/LayoutTests/fast/images/svg-as-relative-image.html b/LayoutTests/fast/images/svg-as-relative-image.html
new file mode 100644 (file)
index 0000000..219ede4
--- /dev/null
@@ -0,0 +1,16 @@
+<html>
+<head>
+<style>
+div {
+  background: red;
+  height: 100px;
+  width: 100px;
+}
+</style>
+</head>
+<body style='margin: 0px'>
+<div>
+  <img src='resources/green-relative-size-rect.svg'>
+</div>
+</body>
+</html>
diff --git a/LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.checksum b/LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.checksum
new file mode 100644 (file)
index 0000000..3a4f1f7
--- /dev/null
@@ -0,0 +1 @@
+16310588467cfc20d551635abc59b784
\ No newline at end of file
diff --git a/LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.png b/LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.png
new file mode 100644 (file)
index 0000000..3db2634
Binary files /dev/null and b/LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.png differ
diff --git a/LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.txt b/LayoutTests/platform/mac/fast/images/svg-as-relative-image-expected.txt
new file mode 100644 (file)
index 0000000..1be7b54
--- /dev/null
@@ -0,0 +1,8 @@
+layer at (0,0) size 800x600
+  RenderView at (0,0) size 800x600
+layer at (0,0) size 800x600
+  RenderBlock {HTML} at (0,0) size 800x600
+    RenderBody {BODY} at (0,0) size 800x600
+      RenderBlock {DIV} at (0,0) size 100x100 [bgcolor=#FF0000]
+        RenderImage {IMG} at (0,0) size 100x100
+        RenderText {#text} at (0,0) size 0x0
index 336ea9e1620018fff2d81dfe6b177dafaecac265..ea874062708e577933fb834403cf3ee26bdaccfe 100644 (file)
@@ -1,3 +1,41 @@
+2007-12-18  Beth Dakin  <bdakin@apple.com>
+
+        Reviewed by Oliver.
+
+        Fix for <rdar://problem/5616982> SVGs with width and height 100% 
+        fail to render when used as <img> or CSS image (16167)
+
+        This final part of the work fixes the <img> tag.
+
+        This is the real fix. 
+        * rendering/RenderImage.cpp:
+        (WebCore::RenderImage::calcReplacedWidth): Set the container size 
+        on the image. Setting the container size only actually sticks if 
+        the values are non-zero, so if the container size really was set, 
+        use the imageSize that is calculated using the container size. If 
+        it did not stick but the image does have relative width (meaning 
+        that the container size is 0), set the width to 0 by hand. We want 
+        to avoid setting the width before we have a container size or we 
+        will end up incorrectly using the default size of 300x150.
+        (WebCore::RenderImage::calcReplacedHeight): Same as above, but for 
+        height.
+
+        A few more pieces of information have to be exposed through cached 
+        image to make this happen.
+        * loader/CachedImage.cpp:
+        (WebCore::CachedImage::usesImageContainerSize): As mentioned above, 
+        when setContainerSize() is called, the container size is only 
+        actually set if the values are non-zero. This call tells you if it 
+        was set.
+        (WebCore::CachedImage::imageHasRelativeWidth):
+        (WebCore::CachedImage::imageHasRelativeHeight):
+        * loader/CachedImage.h:
+        * platform/graphics/Image.h:
+        (WebCore::Image::usesContainerSize):
+        * svg/graphics/SVGImage.cpp:
+        (WebCore::SVGImage::usesContainerSize):
+        * svg/graphics/SVGImage.h:
+
 2007-12-18  Mark Rowe  <mrowe@apple.com>
 
         Rubber-stamped by Maciej Stachowiak.
index f7e788eaa271902eebaf965850ab9547fa679f99..d8a43fd2b30bbeb5344556d022fe2bce18351979 100644 (file)
@@ -119,6 +119,30 @@ void CachedImage::setImageContainerSize(const IntSize& containerSize)
         m_image->setContainerSize(containerSize);
 }
 
+bool CachedImage::usesImageContainerSize() const
+{
+    if (m_image)
+        return m_image->usesContainerSize();
+
+    return false;
+}
+
+bool CachedImage::imageHasRelativeWidth() const
+{
+    if (m_image)
+        return m_image->hasRelativeWidth();
+
+    return false;
+}
+
+bool CachedImage::imageHasRelativeHeight() const
+{
+    if (m_image)
+        return m_image->hasRelativeHeight();
+
+    return false;
+}
+
 IntSize CachedImage::imageSize() const
 {
     return (m_image ? m_image->size() : IntSize());
index 2b0a916d08e7d9085e033d463ec16d6fd34e7a98..acf36508bbcf3eb9d7e4156f80483fc1be113181 100644 (file)
@@ -46,7 +46,11 @@ public:
 
     bool canRender() const { return !errorOccurred() && imageSize().width() > 0 && imageSize().height() > 0; }
 
+    // These are ony used for SVGImage right now
     void setImageContainerSize(const IntSize&);
+    bool usesImageContainerSize() const;
+    bool imageHasRelativeWidth() const;
+    bool imageHasRelativeHeight() const;
     
     IntSize imageSize() const;  // returns the size of the complete image
     IntRect imageRect() const;  // The size of the image.
index 35b7cbbad10dbb054d58a772237b4187aa699562..cbe8dcb8cd993e466a2aae3c9912d44b0fb0cb2c 100644 (file)
@@ -82,6 +82,7 @@ public:
 
     // These are ony used for SVGImage right now
     virtual void setContainerSize(const IntSize&) { }
+    virtual bool usesContainerSize() const { return false; }
     virtual bool hasRelativeWidth() const { return false; }
     virtual bool hasRelativeHeight() const { return false; }
 
index c4479b5d15f9d8ea7024911077aa7320f40641a3..d6899b53b222751a7b0280741ee3878c15c66a08 100644 (file)
@@ -324,9 +324,17 @@ bool RenderImage::isHeightSpecified() const
 
 int RenderImage::calcReplacedWidth() const
 {
+    if (m_cachedImage && m_cachedImage->imageHasRelativeWidth() && !m_cachedImage->usesImageContainerSize())
+        if (RenderObject* cb = isPositioned() ? container() : containingBlock())
+            m_cachedImage->setImageContainerSize(IntSize(cb->availableWidth(), cb->availableHeight()));
+    
     int width;
     if (isWidthSpecified())
         width = calcReplacedWidthUsing(style()->width());
+    else if (m_cachedImage && m_cachedImage->usesImageContainerSize())
+        width = m_cachedImage->imageSize().width();
+    else if (m_cachedImage && m_cachedImage->imageHasRelativeWidth())
+        width = 0; // If the image is relatively-sized, set the width to 0 until there is a set container size.
     else
         width = calcAspectRatioWidth();
 
@@ -341,6 +349,10 @@ int RenderImage::calcReplacedHeight() const
     int height;
     if (isHeightSpecified())
         height = calcReplacedHeightUsing(style()->height());
+    else if (m_cachedImage && m_cachedImage->usesImageContainerSize())
+        height = m_cachedImage->imageSize().height();
+    else if (m_cachedImage && m_cachedImage->imageHasRelativeHeight())
+        height = 0; // If the image is relatively-sized, set the height to 0 until there is a set container size.
     else
         height = calcAspectRatioHeight();
 
index 78bbdcf6abd9d5b2d05a8186fe0eef2186696ad7..c8d5cb1c34f4b565a751f3e8b93a28c1e5a4d3ec 100644 (file)
@@ -76,6 +76,15 @@ void SVGImage::setContainerSize(const IntSize& containerSize)
     rootElement->setContainerSize(containerSize);
 }
 
+bool SVGImage::usesContainerSize() const
+{
+    SVGSVGElement* rootElement = static_cast<SVGDocument*>(m_frame->document())->rootElement();
+    if (!rootElement)
+        return false;
+
+    return rootElement->hasSetContainerSize();
+}
+
 IntSize SVGImage::size() const
 {
     if (!m_frame || !m_frame->document())
index 86682e6f7acef672d0296aa38a8db37a3991124b..3e67a7c34e449e938af215238e33d4cdbc37f7c9 100644 (file)
@@ -46,6 +46,7 @@ namespace WebCore {
         ~SVGImage();
 
         virtual void setContainerSize(const IntSize&);
+        virtual bool usesContainerSize() const;
         virtual bool hasRelativeWidth() const;
         virtual bool hasRelativeHeight() const;