Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2006 Apple Computer, Inc.
      3  * Copyright (C) 2009 Google, Inc.
      4  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Library General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Library General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Library General Public License
     17  * along with this library; see the file COPYING.LIB.  If not, write to
     18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  * Boston, MA 02110-1301, USA.
     20  */
     21 
     22 #include "config.h"
     23 
     24 #include "core/rendering/svg/RenderSVGForeignObject.h"
     25 
     26 #include "core/platform/graphics/GraphicsContextStateSaver.h"
     27 #include "core/rendering/HitTestResult.h"
     28 #include "core/rendering/LayoutRepainter.h"
     29 #include "core/rendering/RenderView.h"
     30 #include "core/rendering/svg/SVGRenderSupport.h"
     31 #include "core/rendering/svg/SVGRenderingContext.h"
     32 #include "core/rendering/svg/SVGResourcesCache.h"
     33 #include "core/svg/SVGForeignObjectElement.h"
     34 
     35 namespace WebCore {
     36 
     37 RenderSVGForeignObject::RenderSVGForeignObject(SVGForeignObjectElement* node)
     38     : RenderSVGBlock(node)
     39     , m_needsTransformUpdate(true)
     40 {
     41 }
     42 
     43 RenderSVGForeignObject::~RenderSVGForeignObject()
     44 {
     45 }
     46 
     47 void RenderSVGForeignObject::paint(PaintInfo& paintInfo, const LayoutPoint&)
     48 {
     49     if (paintInfo.context->paintingDisabled()
     50         || (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection))
     51         return;
     52 
     53     PaintInfo childPaintInfo(paintInfo);
     54     GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
     55     childPaintInfo.applyTransform(localTransform());
     56 
     57     if (SVGRenderSupport::isOverflowHidden(this))
     58         childPaintInfo.context->clip(m_viewport);
     59 
     60     SVGRenderingContext renderingContext;
     61     bool continueRendering = true;
     62     if (paintInfo.phase == PaintPhaseForeground) {
     63         renderingContext.prepareToRenderSVGContent(this, childPaintInfo);
     64         continueRendering = renderingContext.isRenderingPrepared();
     65     }
     66 
     67     if (continueRendering) {
     68         // Paint all phases of FO elements atomically, as though the FO element established its
     69         // own stacking context.
     70         bool preservePhase = paintInfo.phase == PaintPhaseSelection || paintInfo.phase == PaintPhaseTextClip;
     71         LayoutPoint childPoint = IntPoint();
     72         childPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
     73         RenderBlock::paint(childPaintInfo, IntPoint());
     74         if (!preservePhase) {
     75             childPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
     76             RenderBlock::paint(childPaintInfo, childPoint);
     77             childPaintInfo.phase = PaintPhaseFloat;
     78             RenderBlock::paint(childPaintInfo, childPoint);
     79             childPaintInfo.phase = PaintPhaseForeground;
     80             RenderBlock::paint(childPaintInfo, childPoint);
     81             childPaintInfo.phase = PaintPhaseOutline;
     82             RenderBlock::paint(childPaintInfo, childPoint);
     83         }
     84     }
     85 }
     86 
     87 LayoutRect RenderSVGForeignObject::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
     88 {
     89     return SVGRenderSupport::clippedOverflowRectForRepaint(this, repaintContainer);
     90 }
     91 
     92 void RenderSVGForeignObject::computeFloatRectForRepaint(const RenderLayerModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const
     93 {
     94     SVGRenderSupport::computeFloatRectForRepaint(this, repaintContainer, repaintRect, fixed);
     95 }
     96 
     97 const AffineTransform& RenderSVGForeignObject::localToParentTransform() const
     98 {
     99     m_localToParentTransform = localTransform();
    100     m_localToParentTransform.translate(m_viewport.x(), m_viewport.y());
    101     return m_localToParentTransform;
    102 }
    103 
    104 void RenderSVGForeignObject::updateLogicalWidth()
    105 {
    106     // FIXME: Investigate in size rounding issues
    107     // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
    108     setWidth(static_cast<int>(roundf(m_viewport.width())));
    109 }
    110 
    111 void RenderSVGForeignObject::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
    112 {
    113     // FIXME: Investigate in size rounding issues
    114     // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
    115     // FIXME: Is this correct for vertical writing mode?
    116     computedValues.m_extent = static_cast<int>(roundf(m_viewport.height()));
    117     computedValues.m_position = logicalTop;
    118 }
    119 
    120 void RenderSVGForeignObject::layout()
    121 {
    122     StackStats::LayoutCheckPoint layoutCheckPoint;
    123     ASSERT(needsLayout());
    124     ASSERT(!view()->layoutStateEnabled()); // RenderSVGRoot disables layoutState for the SVG rendering tree.
    125 
    126     LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this));
    127     SVGForeignObjectElement* foreign = static_cast<SVGForeignObjectElement*>(node());
    128 
    129     bool updateCachedBoundariesInParents = false;
    130     if (m_needsTransformUpdate) {
    131         m_localTransform = foreign->animatedLocalTransform();
    132         m_needsTransformUpdate = false;
    133         updateCachedBoundariesInParents = true;
    134     }
    135 
    136     FloatRect oldViewport = m_viewport;
    137 
    138     // Cache viewport boundaries
    139     SVGLengthContext lengthContext(foreign);
    140     FloatPoint viewportLocation(foreign->xCurrentValue().value(lengthContext), foreign->yCurrentValue().value(lengthContext));
    141     m_viewport = FloatRect(viewportLocation, FloatSize(foreign->widthCurrentValue().value(lengthContext), foreign->heightCurrentValue().value(lengthContext)));
    142     if (!updateCachedBoundariesInParents)
    143         updateCachedBoundariesInParents = oldViewport != m_viewport;
    144 
    145     // Set box origin to the foreignObject x/y translation, so positioned objects in XHTML content get correct
    146     // positions. A regular RenderBoxModelObject would pull this information from RenderStyle - in SVG those
    147     // properties are ignored for non <svg> elements, so we mimic what happens when specifying them through CSS.
    148 
    149     // FIXME: Investigate in location rounding issues - only affects RenderSVGForeignObject & RenderSVGText
    150     setLocation(roundedIntPoint(viewportLocation));
    151 
    152     bool layoutChanged = everHadLayout() && selfNeedsLayout();
    153     RenderBlock::layout();
    154     ASSERT(!needsLayout());
    155 
    156     // If our bounds changed, notify the parents.
    157     if (updateCachedBoundariesInParents)
    158         RenderSVGBlock::setNeedsBoundariesUpdate();
    159 
    160     // Invalidate all resources of this client if our layout changed.
    161     if (layoutChanged)
    162         SVGResourcesCache::clientLayoutChanged(this);
    163 
    164     repainter.repaintAfterLayout();
    165 }
    166 
    167 bool RenderSVGForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
    168 {
    169     // Embedded content is drawn in the foreground phase.
    170     if (hitTestAction != HitTestForeground)
    171         return false;
    172 
    173     FloatPoint localPoint = localTransform().inverse().mapPoint(pointInParent);
    174 
    175     // Early exit if local point is not contained in clipped viewport area
    176     if (SVGRenderSupport::isOverflowHidden(this) && !m_viewport.contains(localPoint))
    177         return false;
    178 
    179     // FOs establish a stacking context, so we need to hit-test all layers.
    180     HitTestLocation hitTestLocation(roundedLayoutPoint(localPoint));
    181     return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestForeground)
    182         || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestFloat)
    183         || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestChildBlockBackgrounds);
    184 }
    185 
    186 bool RenderSVGForeignObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction)
    187 {
    188     ASSERT_NOT_REACHED();
    189     return false;
    190 }
    191 
    192 void RenderSVGForeignObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags, bool* wasFixed) const
    193 {
    194     SVGRenderSupport::mapLocalToContainer(this, repaintContainer, transformState, wasFixed);
    195 }
    196 
    197 const RenderObject* RenderSVGForeignObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
    198 {
    199     return SVGRenderSupport::pushMappingToContainer(this, ancestorToStopAt, geometryMap);
    200 }
    201 
    202 }
    203