Changeset 28637 in webkit


Ignore:
Timestamp:
Dec 11, 2007 7:23:35 PM (16 years ago)
Author:
Beth Dakin
Message:

WebCore:

Reviewed by Darin.

Fix for <rdar://problem/5641255> SVGs with width and height 100%
fail to render when used as background images

CachedImage now has a setImageContainerSize function. It is only
needed for SVG right now.

  • loader/CachedImage.cpp: (WebCore::CachedImage::setImageContainerSize):
  • loader/CachedImage.h:

Relatively sized SVGs have no intrinsic size. Because this call is
low-level enough that we cannot pass in the container size and get
the real size of the SVG, we need to know if it has relative
dimensions, and if so, we need to get the size from scaledTileSize.

  • platform/graphics/Image.cpp: (WebCore::Image::drawTiled):
  • platform/graphics/Image.h: (WebCore::Image::setContainerSize): (WebCore::Image::hasRelativeWidth): (WebCore::Image::hasRelativeHeight):

Re-factored calculateBackgroundSize a bit so that it returns an
IntSize. Made it a member function so that it can send m_width and
m_height along to setImageContainerSize.

  • rendering/RenderBox.cpp: (WebCore::RenderBox::calculateBackgroundSize): (WebCore::RenderBox::calculateBackgroundImageGeometry):
  • rendering/RenderBox.h:

Need to account for relatively sized SVGs in calcViewport()

  • rendering/RenderSVGRoot.cpp: (WebCore::RenderSVGRoot::calcViewport):


Added new member variables to store the size of the SVG container.
relativeWidthValue() and relativeHeightValue() use the stored SVG
container size to correctly calculate the width and height of a
relatively sized SVG.

  • svg/SVGSVGElement.cpp: (WebCore::SVGSVGElement::SVGSVGElement): (WebCore::SVGSVGElement::setContainerSize): (WebCore::SVGSVGElement::relativeWidthValue): (WebCore::SVGSVGElement::relativeHeightValue):
  • svg/SVGSVGElement.h: (WebCore::SVGSVGElement::containerSize):


Re-factored size calculations to use the size of the container.

  • svg/graphics/SVGImage.cpp: (WebCore::SVGImage::setContainerSize): (WebCore::SVGImage::size): (WebCore::SVGImage::hasRelativeWidth): (WebCore::SVGImage::hasRelativeHeight):
  • svg/graphics/SVGImage.h:

LayoutTests:

Reviewed by Darin.

Tests for <rdar://problem/5641255> SVGs with width and height 100%
fail to render when used as background images

  • fast/backgrounds/resources/balloon.svg: Added.
  • fast/backgrounds/resources/eyes.svg: Added.
  • fast/backgrounds/resources/hearts.svg: Added.
  • fast/backgrounds/resources/linguistics.svg: Added.
  • fast/backgrounds/resources/map.svg: Added.
  • fast/backgrounds/resources/mouse-repel.svg: Added.
  • fast/backgrounds/svg-as-background-1.html: Added.
  • fast/backgrounds/svg-as-background-2.html: Added.
  • fast/backgrounds/svg-as-background-3.html: Added.
  • fast/backgrounds/svg-as-background-4.html: Added.
  • fast/backgrounds/svg-as-background-5.html: Added.
  • fast/backgrounds/svg-as-background-6.html: Added.
  • platform/mac/fast/backgrounds/svg-as-background-1-expected.checksum: Added.
  • platform/mac/fast/backgrounds/svg-as-background-1-expected.png: Added.
  • platform/mac/fast/backgrounds/svg-as-background-1-expected.txt: Added.
  • platform/mac/fast/backgrounds/svg-as-background-2-expected.checksum: Added.
  • platform/mac/fast/backgrounds/svg-as-background-2-expected.png: Added.
  • platform/mac/fast/backgrounds/svg-as-background-2-expected.txt: Added.
  • platform/mac/fast/backgrounds/svg-as-background-3-expected.checksum: Added.
  • platform/mac/fast/backgrounds/svg-as-background-3-expected.png: Added.
  • platform/mac/fast/backgrounds/svg-as-background-3-expected.txt: Added.
  • platform/mac/fast/backgrounds/svg-as-background-4-expected.checksum: Added.
  • platform/mac/fast/backgrounds/svg-as-background-4-expected.png: Added.
  • platform/mac/fast/backgrounds/svg-as-background-4-expected.txt: Added.
  • platform/mac/fast/backgrounds/svg-as-background-5-expected.checksum: Added.
  • platform/mac/fast/backgrounds/svg-as-background-5-expected.png: Added.
  • platform/mac/fast/backgrounds/svg-as-background-5-expected.txt: Added.
  • platform/mac/fast/backgrounds/svg-as-background-6-expected.checksum: Added.
  • platform/mac/fast/backgrounds/svg-as-background-6-expected.png: Added.
  • platform/mac/fast/backgrounds/svg-as-background-6-expected.txt: Added.
Location:
trunk
Files:
30 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r28629 r28637  
     12007-12-11  Beth Dakin  <bdakin@apple.com>
     2
     3        Reviewed by Darin.
     4
     5        Tests for <rdar://problem/5641255> SVGs with width and height 100%
     6        fail to render when used as background images
     7
     8        * fast/backgrounds/resources/balloon.svg: Added.
     9        * fast/backgrounds/resources/eyes.svg: Added.
     10        * fast/backgrounds/resources/hearts.svg: Added.
     11        * fast/backgrounds/resources/linguistics.svg: Added.
     12        * fast/backgrounds/resources/map.svg: Added.
     13        * fast/backgrounds/resources/mouse-repel.svg: Added.
     14        * fast/backgrounds/svg-as-background-1.html: Added.
     15        * fast/backgrounds/svg-as-background-2.html: Added.
     16        * fast/backgrounds/svg-as-background-3.html: Added.
     17        * fast/backgrounds/svg-as-background-4.html: Added.
     18        * fast/backgrounds/svg-as-background-5.html: Added.
     19        * fast/backgrounds/svg-as-background-6.html: Added.
     20        * platform/mac/fast/backgrounds/svg-as-background-1-expected.checksum: Added.
     21        * platform/mac/fast/backgrounds/svg-as-background-1-expected.png: Added.
     22        * platform/mac/fast/backgrounds/svg-as-background-1-expected.txt: Added.
     23        * platform/mac/fast/backgrounds/svg-as-background-2-expected.checksum: Added.
     24        * platform/mac/fast/backgrounds/svg-as-background-2-expected.png: Added.
     25        * platform/mac/fast/backgrounds/svg-as-background-2-expected.txt: Added.
     26        * platform/mac/fast/backgrounds/svg-as-background-3-expected.checksum: Added.
     27        * platform/mac/fast/backgrounds/svg-as-background-3-expected.png: Added.
     28        * platform/mac/fast/backgrounds/svg-as-background-3-expected.txt: Added.
     29        * platform/mac/fast/backgrounds/svg-as-background-4-expected.checksum: Added.
     30        * platform/mac/fast/backgrounds/svg-as-background-4-expected.png: Added.
     31        * platform/mac/fast/backgrounds/svg-as-background-4-expected.txt: Added.
     32        * platform/mac/fast/backgrounds/svg-as-background-5-expected.checksum: Added.
     33        * platform/mac/fast/backgrounds/svg-as-background-5-expected.png: Added.
     34        * platform/mac/fast/backgrounds/svg-as-background-5-expected.txt: Added.
     35        * platform/mac/fast/backgrounds/svg-as-background-6-expected.checksum: Added.
     36        * platform/mac/fast/backgrounds/svg-as-background-6-expected.png: Added.
     37        * platform/mac/fast/backgrounds/svg-as-background-6-expected.txt: Added.
     38
    1392007-12-11  Nikolas Zimmermann  <zimmermann@kde.org>
    240
  • trunk/WebCore/ChangeLog

    r28631 r28637  
     12007-12-11  Beth Dakin  <bdakin@apple.com>
     2
     3        Reviewed by Darin.
     4
     5        Fix for <rdar://problem/5641255> SVGs with width and height 100%
     6        fail to render when used as background images
     7
     8        CachedImage now has a setImageContainerSize function. It is only
     9        needed for SVG right now.
     10        * loader/CachedImage.cpp:
     11        (WebCore::CachedImage::setImageContainerSize):
     12        * loader/CachedImage.h:
     13
     14        Relatively sized SVGs have no intrinsic size. Because this call is
     15        low-level enough that we cannot pass in the container size and get
     16        the real size of the SVG, we need to know if it has relative
     17        dimensions, and if so, we need to get the size from scaledTileSize.
     18        * platform/graphics/Image.cpp:
     19        (WebCore::Image::drawTiled):
     20        * platform/graphics/Image.h:
     21        (WebCore::Image::setContainerSize):
     22        (WebCore::Image::hasRelativeWidth):
     23        (WebCore::Image::hasRelativeHeight):
     24
     25        Re-factored calculateBackgroundSize a bit so that it returns an
     26        IntSize. Made it a member function so that it can send m_width and
     27        m_height along to setImageContainerSize.
     28        * rendering/RenderBox.cpp:
     29        (WebCore::RenderBox::calculateBackgroundSize):
     30        (WebCore::RenderBox::calculateBackgroundImageGeometry):
     31        * rendering/RenderBox.h:
     32
     33        Need to account for relatively sized SVGs in calcViewport()
     34        * rendering/RenderSVGRoot.cpp:
     35        (WebCore::RenderSVGRoot::calcViewport):
     36       
     37        Added new member variables to store the size of the SVG container.
     38        relativeWidthValue() and relativeHeightValue() use the stored SVG
     39        container size to correctly calculate the width and height of a
     40        relatively sized SVG.
     41        * svg/SVGSVGElement.cpp:
     42        (WebCore::SVGSVGElement::SVGSVGElement):
     43        (WebCore::SVGSVGElement::setContainerSize):
     44        (WebCore::SVGSVGElement::relativeWidthValue):
     45        (WebCore::SVGSVGElement::relativeHeightValue):
     46        * svg/SVGSVGElement.h:
     47        (WebCore::SVGSVGElement::containerSize):
     48       
     49        Re-factored size calculations to use the size of the container.
     50        * svg/graphics/SVGImage.cpp:
     51        (WebCore::SVGImage::setContainerSize):
     52        (WebCore::SVGImage::size):
     53        (WebCore::SVGImage::hasRelativeWidth):
     54        (WebCore::SVGImage::hasRelativeHeight):
     55        * svg/graphics/SVGImage.h:
     56
    1572007-12-11  Darin Adler  <darin@apple.com>
    258
  • trunk/WebCore/loader/CachedImage.cpp

    r28568 r28637  
    112112
    113113    return nullImage();
     114}
     115
     116void CachedImage::setImageContainerSize(const IntSize& containerSize)
     117{
     118    if (m_image)
     119        m_image->setContainerSize(containerSize);
    114120}
    115121
  • trunk/WebCore/loader/CachedImage.h

    r25754 r28637  
    4747    bool canRender() const { return !errorOccurred() && imageSize().width() > 0 && imageSize().height() > 0; }
    4848
     49    void setImageContainerSize(const IntSize&);
     50   
    4951    IntSize imageSize() const;  // returns the size of the complete image
    5052    IntRect imageRect() const;  // The size of the image.
  • trunk/WebCore/platform/graphics/Image.cpp

    r27060 r28637  
    128128
    129129    FloatSize intrinsicTileSize = size();
     130    if (hasRelativeWidth())
     131        intrinsicTileSize.setWidth(scaledTileSize.width());
     132    if (hasRelativeHeight())
     133        intrinsicTileSize.setHeight(scaledTileSize.height());
     134
    130135    FloatSize scale(scaledTileSize.width() / intrinsicTileSize.width(),
    131136                    scaledTileSize.height() / intrinsicTileSize.height());
  • trunk/WebCore/platform/graphics/Image.h

    r25674 r28637  
    8181    bool isNull() const;
    8282
     83    // These are ony used for SVGImage right now
     84    virtual void setContainerSize(const IntSize&) { }
     85    virtual bool hasRelativeWidth() const { return false; }
     86    virtual bool hasRelativeHeight() const { return false; }
     87
    8388    virtual IntSize size() const = 0;
    8489    IntRect rect() const;
  • trunk/WebCore/rendering/RenderBox.cpp

    r28299 r28637  
    418418}
    419419
    420 static void cacluateBackgroundSize(const BackgroundLayer* bgLayer, int& scaledWidth, int& scaledHeight)
     420IntSize RenderBox::calculateBackgroundSize(const BackgroundLayer* bgLayer, int scaledWidth, int scaledHeight) const
    421421{
    422422    CachedImage* bg = bgLayer->backgroundImage();
    423423
    424424    if (bgLayer->isBackgroundSizeSet()) {
     425        int w = scaledWidth;
     426        int h = scaledHeight;
    425427        Length bgWidth = bgLayer->backgroundSize().width;
    426428        Length bgHeight = bgLayer->backgroundSize().height;
    427429
    428430        if (bgWidth.isPercent())
    429             scaledWidth = bgWidth.calcValue(scaledWidth);
     431            w = bgWidth.calcValue(scaledWidth);
    430432        else if (bgWidth.isFixed())
    431             scaledWidth = bgWidth.value();
     433            w = bgWidth.value();
    432434        else if (bgWidth.isAuto()) {
    433435            // If the width is auto and the height is not, we have to use the appropriate
     
    435437            if (bgHeight.isPercent()) {
    436438                int scaledH = bgHeight.calcValue(scaledHeight);
    437                 scaledWidth = bg->imageSize().width() * scaledH / bg->imageSize().height();
     439                w = bg->imageSize().width() * scaledH / bg->imageSize().height();
    438440            } else if (bgHeight.isFixed())
    439                 scaledWidth = bg->imageSize().width() * bgHeight.value() / bg->imageSize().height();
     441                w = bg->imageSize().width() * bgHeight.value() / bg->imageSize().height();
    440442        }
    441443
    442444        if (bgHeight.isPercent())
    443             scaledHeight = bgHeight.calcValue(scaledHeight);
     445            h = bgHeight.calcValue(scaledHeight);
    444446        else if (bgHeight.isFixed())
    445             scaledHeight = bgHeight.value();
     447            h = bgHeight.value();
    446448        else if (bgHeight.isAuto()) {
    447449            // If the height is auto and the width is not, we have to use the appropriate
    448450            // scale to maintain our aspect ratio.
    449451            if (bgWidth.isPercent())
    450                 scaledHeight = bg->imageSize().height() * scaledWidth / bg->imageSize().width();
     452                h = bg->imageSize().height() * scaledWidth / bg->imageSize().width();
    451453            else if (bgWidth.isFixed())
    452                 scaledHeight = bg->imageSize().height() * bgWidth.value() / bg->imageSize().width();
     454                h = bg->imageSize().height() * bgWidth.value() / bg->imageSize().width();
    453455            else if (bgWidth.isAuto()) {
    454456                // If both width and height are auto, we just want to use the image's
    455457                // intrinsic size.
    456                 scaledWidth = bg->imageSize().width();
    457                 scaledHeight = bg->imageSize().height();
     458                w = bg->imageSize().width();
     459                h = bg->imageSize().height();
    458460            }
    459461        }
    460         scaledWidth = max(1, scaledWidth);
    461         scaledHeight = max(1, scaledHeight);
     462        return IntSize(max(1, w), max(1, h));
    462463    } else {
    463         scaledWidth = bg->imageSize().width();
    464         scaledHeight = bg->imageSize().height();
     464        bg->setImageContainerSize(IntSize(m_width, m_height));
     465        return bg->imageSize();
    465466    }
    466467}
     
    562563    int cw;
    563564    int ch;
    564     int scaledImageWidth = pw;
    565     int scaledImageHeight = ph;
    566 
    567     cacluateBackgroundSize(bgLayer, scaledImageWidth, scaledImageHeight);
     565
     566    IntSize scaledImageSize = calculateBackgroundSize(bgLayer, pw, ph);
     567    int scaledImageWidth = scaledImageSize.width();
     568    int scaledImageHeight = scaledImageSize.height();
    568569
    569570    EBackgroundRepeat backgroundRepeat = bgLayer->backgroundRepeat();
  • trunk/WebCore/rendering/RenderBox.h

    r28226 r28637  
    144144    virtual void paintBackgroundExtended(GraphicsContext*, const Color&, const BackgroundLayer*, int clipY, int clipHeight,
    145145                                         int tx, int ty, int width, int height, bool includeLeftEdge = true, bool includeRightEdge = true);
     146    IntSize calculateBackgroundSize(const BackgroundLayer*, int scaledWidth, int scaledHeight) const;
    146147
    147148    virtual int staticX() const;
  • trunk/WebCore/rendering/RenderSVGRoot.cpp

    r28568 r28637  
    136136        return;
    137137
    138     // Be sure that our viewport size, didn't depend on ourselves!
    139     // If that is true, we have a null viewport() here, and need to recalculate.
    140     if (viewport().isEmpty())
    141         calcViewport();
     138    calcViewport();
    142139
    143140    SVGSVGElement* svg = static_cast<SVGSVGElement*>(element());
     
    198195            return;
    199196
    200         float w = svg->width().value();
    201         float h = svg->height().value();
     197        float w, h;
     198        SVGLength width = svg->width();
     199        if (width.unitType() == LengthTypePercentage && svg->relativeWidthValue() > 0)
     200            w = svg->relativeWidthValue();
     201        else
     202            w = width.value();
     203       
     204        SVGLength height = svg->height();
     205        if (height.unitType() == LengthTypePercentage && svg->relativeHeightValue() > 0)
     206            h = svg->relativeHeightValue();
     207        else
     208            h = height.value();
     209
    202210        m_viewport = FloatRect(0, 0, w, h);
    203211    }
  • trunk/WebCore/svg/SVGSVGElement.cpp

    r26571 r28637  
    7272    , m_timeScheduler(new TimeScheduler(doc))
    7373    , m_viewSpec(0)
     74    , m_containerSize(300, 150)
    7475{
    7576    setWidthBaseValue(SVGLength(this, LengthModeWidth, "100%"));
     
    132133    viewBox.map(w, h, &wDouble, &hDouble);
    133134    return FloatRect::narrowPrecision(_x, _y, wDouble, hDouble);
     135}
     136
     137int SVGSVGElement::relativeWidthValue() const
     138{
     139    SVGLength w = width();
     140    if (w.unitType() != LengthTypePercentage)
     141        return 0;
     142
     143    return static_cast<int>(w.valueAsPercentage() * m_containerSize.width());
     144}
     145
     146int SVGSVGElement::relativeHeightValue() const
     147{
     148    SVGLength h = height();
     149    if (h.unitType() != LengthTypePercentage)
     150        return 0;
     151
     152    return static_cast<int>(h.valueAsPercentage() * m_containerSize.height());
    134153}
    135154
  • trunk/WebCore/svg/SVGSVGElement.h

    r26571 r28637  
    2626#if ENABLE(SVG)
    2727
     28#include "IntSize.h"
    2829#include "SVGExternalResourcesRequired.h"
    2930#include "SVGFitToViewBox.h"
     
    6465
    6566        FloatRect viewport() const;
     67
     68        void setContainerSize(const IntSize& containerSize) { m_containerSize = containerSize; }
     69        IntSize containerSize() const { return m_containerSize; }
     70        int relativeWidthValue() const;
     71        int relativeHeightValue() const;
    6672
    6773        float pixelUnitToMillimeterX() const;
     
    154160        FloatPoint m_translation;
    155161        mutable OwnPtr<SVGViewSpec> m_viewSpec;
     162        IntSize m_containerSize;
    156163    };
    157164
  • trunk/WebCore/svg/graphics/SVGImage.cpp

    r28319 r28637  
    6565}
    6666
     67void SVGImage::setContainerSize(const IntSize& containerSize)
     68{
     69    if (containerSize.width() <= 0 || containerSize.height() <= 0)
     70        return;
     71
     72    SVGSVGElement* rootElement = static_cast<SVGDocument*>(m_frame->document())->rootElement();
     73    if (!rootElement)
     74        return;
     75
     76    rootElement->setContainerSize(containerSize);
     77}
     78
    6779IntSize SVGImage::size() const
    6880{
    69     IntSize defaultSize(300, 150);
    70     // FIXME: Eventually we'll be passed in the dest size and can scale against that
    71     IntSize destSize = defaultSize;
    72    
    7381    if (!m_frame || !m_frame->document())
    7482        return IntSize();
     
    7684    SVGSVGElement* rootElement = static_cast<SVGDocument*>(m_frame->document())->rootElement();
    7785    if (!rootElement)
    78         return defaultSize;
     86        return IntSize();
    7987   
    8088    SVGLength width = rootElement->width();
     
    8290   
    8391    IntSize svgSize;
    84     if (width.unitType() == LengthTypePercentage)
    85         svgSize.setWidth(static_cast<int>(width.valueAsPercentage() * destSize.width()));
     92    if (width.unitType() == LengthTypePercentage) 
     93        svgSize.setWidth(rootElement->relativeWidthValue());
    8694    else
    8795        svgSize.setWidth(static_cast<int>(width.value()));
    88     if (height.unitType() == LengthTypePercentage)
    89         svgSize.setHeight(static_cast<int>(height.valueAsPercentage() * destSize.height()));
     96
     97    if (height.unitType() == LengthTypePercentage)
     98        svgSize.setHeight(rootElement->relativeHeightValue());
    9099    else
    91100        svgSize.setHeight(static_cast<int>(height.value()));
    92    
     101
    93102    return svgSize;
     103}
     104
     105bool SVGImage::hasRelativeWidth() const
     106{
     107    SVGSVGElement* rootElement = static_cast<SVGDocument*>(m_frame->document())->rootElement();
     108    if (!rootElement)
     109        return false;
     110
     111    return rootElement->width().unitType() == LengthTypePercentage;
     112}
     113
     114bool SVGImage::hasRelativeHeight() const
     115{
     116    SVGSVGElement* rootElement = static_cast<SVGDocument*>(m_frame->document())->rootElement();
     117    if (!rootElement)
     118        return false;
     119
     120    return rootElement->height().unitType() == LengthTypePercentage;
    94121}
    95122
  • trunk/WebCore/svg/graphics/SVGImage.h

    r28319 r28637  
    4545        SVGImage(ImageObserver*);
    4646        ~SVGImage();
    47        
     47
     48        virtual void setContainerSize(const IntSize&);
     49        virtual bool hasRelativeWidth() const;
     50        virtual bool hasRelativeHeight() const;
     51
    4852        virtual IntSize size() const;
    4953       
Note: See TracChangeset for help on using the changeset viewer.