/*
 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005, 2007 Rob Buis <buis@kde.org>
 * Copyright (C) 2009-2016 Google, Inc.  All rights reserved.
 * Copyright (C) 2009 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#pragma once

#include "FloatRect.h"
#include "RenderReplaced.h"
#include "SVGRenderSupport.h"
#include <wtf/WeakHashSet.h>

namespace WebCore {

class AffineTransform;
class SVGElement;
class SVGSVGElement;

// LegacyRenderSVGRoot
class RenderSVGRoot FINAL : public RenderReplaced {
public:
    RenderSVGRoot(SVGElement&, PassOwnPtr<RenderStyle>);
    virtual ~RenderSVGRoot();

    SVGSVGElement& svgSVGElement() const;

    bool isEmbeddedThroughSVGImage() const;
    bool isEmbeddedThroughFrameContainingSVGDocument() const;

    virtual void computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const FINAL;

    bool isLayoutSizeChanged() const { return m_isLayoutSizeChanged; }
    bool isInLayout() const { return m_inLayout; }
    virtual void setNeedsBoundariesUpdate() FINAL { m_needsBoundariesOrTransformUpdate = true; }
    virtual bool needsBoundariesUpdate() FINAL { return m_needsBoundariesOrTransformUpdate; }
	virtual void setNeedsTransformUpdate() FINAL { m_needsBoundariesOrTransformUpdate = true; }

    IntSize containerSize() const { return m_containerSize; }
    void setContainerSize(const IntSize& containerSize) { m_containerSize = containerSize; }

    virtual bool hasRelativeDimensions() const FINAL;

    // localToBorderBoxTransform maps local SVG viewport coordinates to local CSS box coordinates.  
    const AffineTransform& localToBorderBoxTransform() const { return m_localToBorderBoxTransform; }

    // The flag is cleared at the beginning of each layout() pass. Elements then call this
    // method during layout when they are invalidated by a filter.
    static void addResourceForClientInvalidation(RenderSVGResourceContainer&);

private:
    virtual const char* renderName() const FINAL { return "RenderSVGRoot"; }

    virtual LayoutUnit computeReplacedLogicalWidth(ShouldComputePreferred = ComputeActual) const FINAL;
    virtual LayoutUnit computeReplacedLogicalHeight(Optional<LayoutUnit> estimatedUsedWidth = Nullopt) const FINAL;
    virtual void layout() FINAL;
    virtual void paintReplaced(PaintInfo&, const LayoutPoint&) FINAL;

    virtual void willBeDestroyed() FINAL;

    virtual void insertedIntoTree(IsInternalMove = IsInternalMove::No) FINAL;
    virtual void willBeRemovedFromTree(IsInternalMove = IsInternalMove::No) FINAL;

    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) FINAL;

    virtual const AffineTransform& localToParentTransform() const FINAL;

    bool fillContains(const FloatPoint&) const;
    bool strokeContains(const FloatPoint&) const;

    virtual FloatRect objectBoundingBox() const FINAL { return m_objectBoundingBox; }
    virtual FloatRect strokeBoundingBox() const FINAL { return m_strokeBoundingBox; }
    virtual FloatRect repaintRectInLocalCoordinates() const FINAL { return m_repaintBoundingBox; }

    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) FINAL;

    virtual LayoutRect clippedOverflowRectForRepaint(RenderLayerModelObject* repaintContainer) const FINAL;
    virtual Optional<FloatRect> computeFloatVisibleRectInContainer(const FloatRect&, const RenderLayerModelObject* container, VisibleRectContext) const override;

    virtual void mapLocalToContainer(RenderLayerModelObject* repaintContainer, TransformState&, OptionSet<MapCoordinatesMode>, bool* wasFixed) const FINAL;
	virtual const RenderElement* pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap&) const FINAL;

    virtual bool canBeSelectionLeaf() const FINAL { return false; }
    virtual bool canHaveChildren() const FINAL { return true; }

    bool shouldApplyViewportClip() const;
    void updateCachedBoundaries();
    void buildLocalToBorderBoxTransform();

    FloatSize calculateIntrinsicSize() const;

    IntSize m_containerSize;
    FloatRect m_objectBoundingBox;
    bool m_objectBoundingBoxValid;
    bool m_inLayout;
    FloatRect m_strokeBoundingBox;
    FloatRect m_repaintBoundingBox;
    mutable AffineTransform m_localToParentTransform;
    AffineTransform m_localToBorderBoxTransform;
    WeakHashSet<RenderSVGResourceContainer> m_resourcesNeedingToInvalidateClients;
    bool m_isLayoutSizeChanged;
    bool m_needsBoundariesOrTransformUpdate;
    bool m_hasBoxDecorations;
};

template<> inline bool isRendererOfType<const RenderSVGRoot>(const RenderObject& renderer) { return renderer.isSVGRoot(); }
RENDER_OBJECT_TYPE_CASTS(RenderSVGRoot, isSVGRoot())

} // namespace WebCore
