/*
    Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
                  2004, 2005, 2010 Rob Buis <buis@kde.org>
    Copyright (C) Research In Motion Limited 2010. All rights reserved.
    Copyright (C) 2015 Google Inc. All rights reserved.

    Based on khtml code by:
    Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
    Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
    Copyright (C) 2002-2003 Dirk Mueller (mueller@kde.org)
    Copyright (C) 2002-2023 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.
*/

#include "config.h"

#if ENABLE(SVG)
#include "SVGRenderStyle.h"

#include "CSSPrimitiveValue.h"
#include "CSSValueList.h"
#include "IntRect.h"
#include "NodeRenderStyle.h"
#include "StyleColor.h"
#include "SVGElement.h"
#include <wtf/NeverDestroyed.h>

namespace WebCore {

static const SVGRenderStyle& defaultSVGStyle()
{
    static NeverDestroyed<DataRef<SVGRenderStyle>> style(SVGRenderStyle::createDefaultStyle());
    return style.get().get();
}

Ref<SVGRenderStyle> SVGRenderStyle::createDefaultStyle()
{
    return adoptRef(*new SVGRenderStyle(CreateDefault));
}

inline SVGRenderStyle::SVGRenderStyle()
    : fill(defaultSVGStyle().fill)
    , stroke(defaultSVGStyle().stroke)
    , text(defaultSVGStyle().text)
    , inheritedResources(defaultSVGStyle().inheritedResources)
    , stops(defaultSVGStyle().stops)
    , misc(defaultSVGStyle().misc)
	, layout(defaultSVGStyle().layout)
{
    setBitDefaults();
}

inline SVGRenderStyle::SVGRenderStyle(CreateDefaultType)
    : fill(StyleFillData::create())
    , stroke(StyleStrokeData::create())
    , text(StyleTextData::create())
    , inheritedResources(StyleInheritedResourceData::create())
    , stops(StyleStopData::create())
    , misc(StyleMiscData::create())
	, layout(StyleLayoutData::create())
{
    setBitDefaults();
}

SVGRenderStyle::SVGRenderStyle(const SVGRenderStyle& other)
    : RefCounted<SVGRenderStyle>()
	, svg_inherited_flags(other.svg_inherited_flags)
	, svg_noninherited_flags(other.svg_noninherited_flags)
	, fill(other.fill)
	, stroke(other.stroke)
	, text(other.text)
	, inheritedResources(other.inheritedResources)
	, stops(other.stops)
	, misc(other.misc)
    , layout(other.layout)
{
}

SVGRenderStyle::~SVGRenderStyle() = default;

bool SVGRenderStyle::inheritedEqual(const SVGRenderStyle& other) const
{
    return fill == other.fill
        && stroke == other.stroke
		&& text == other.text
		&& inheritedResources == other.inheritedResources
		&& svg_inherited_flags == other.svg_inherited_flags;
}

void SVGRenderStyle::inheritFrom(const SVGRenderStyle& svgInheritParent)
{
    fill = svgInheritParent.fill;
    stroke = svgInheritParent.stroke;
    text = svgInheritParent.text;
    inheritedResources = svgInheritParent.inheritedResources;

    svg_inherited_flags = svgInheritParent.svg_inherited_flags;
}

void SVGRenderStyle::copyNonInheritedFrom(const SVGRenderStyle& other)
{
    svg_noninherited_flags = other.svg_noninherited_flags;
    stops = other.stops;
    misc = other.misc;
    layout = other.layout;
}

static bool colorChangeRequiresRepaint(const StyleColor& a, const StyleColor& b, bool currentColorDiffers)
{
    if (a != b)
        return true;

    if (a.isCurrentColor()) {
        ASSERT(b.isCurrentColor());
        return currentColorDiffers;
    }

    return false;
}

bool SVGRenderStyle::changeRequiresLayout(const SVGRenderStyle& other) const
{
    // If kerning changes, we need a relayout, to force SVGCharacterData to be recalculated in the SVGRootInlineBox.
    if (text != other.text)
        return true;

    // If markers change, we need a relayout, as marker boundaries are cached in RenderSVGPath.
    if (inheritedResources != other.inheritedResources)
        return true;

    // All text related properties influence layout.
    if (svg_inherited_flags._textAnchor != other.svg_inherited_flags._textAnchor
        || svg_inherited_flags._glyphOrientationHorizontal != other.svg_inherited_flags._glyphOrientationHorizontal
        || svg_inherited_flags._glyphOrientationVertical != other.svg_inherited_flags._glyphOrientationVertical
        || svg_inherited_flags._dominantBaseline != other.svg_inherited_flags._dominantBaseline
        || svg_noninherited_flags.f._alignmentBaseline != other.svg_noninherited_flags.f._alignmentBaseline
        || svg_noninherited_flags.f._baselineShift != other.svg_noninherited_flags.f._baselineShift)
        return true;

    // Text related properties influence layout.
    if (misc->baselineShiftValue != other.misc->baselineShiftValue)
		return true;

    // The x or y properties require relayout.
    if (layout != other.layout)
        return true;

    // Some stroke properties, requires relayouts, as the cached stroke boundaries need to be recalculated.
    if (stroke->paintType != other.stroke->paintType
        || stroke->paintUri != other.stroke->paintUri
        || stroke->dashArray != other.stroke->dashArray
        || stroke->dashOffset != other.stroke->dashOffset
        || stroke->visitedLinkPaintUri != other.stroke->visitedLinkPaintUri
        || stroke->visitedLinkPaintType != other.stroke->visitedLinkPaintType)
        return true;

    // vector-effect changes require a re-layout.
    if (svg_noninherited_flags.f._vectorEffect != other.svg_noninherited_flags.f._vectorEffect)
        return true;

    return false;
}

bool SVGRenderStyle::changeRequiresRepaint(const SVGRenderStyle& other, bool currentColorDiffers) const
{
	if (stroke->opacity != other.stroke->opacity
		|| colorChangeRequiresRepaint(stroke->paintColor, other.stroke->paintColor, currentColorDiffers)
		|| colorChangeRequiresRepaint(stroke->visitedLinkPaintColor, other.stroke->visitedLinkPaintColor, currentColorDiffers))
		return true;

	// Painting related properties only need repaints.
    if (colorChangeRequiresRepaint(misc->floodColor, other.misc->floodColor, currentColorDiffers)
        || misc->floodOpacity != other.misc->floodOpacity
        || colorChangeRequiresRepaint(misc->lightingColor, other.misc->lightingColor, currentColorDiffers))
        return true;

    // If fill changes, we just need to repaint. Fill boundaries are not influenced by this, only by the Path, that RenderSVGPath contains.
    if (fill->paintType != other.fill->paintType
        || colorChangeRequiresRepaint(fill->paintColor, other.fill->paintColor, currentColorDiffers)
        || fill->paintUri != other.fill->paintUri
        || fill->opacity != other.fill->opacity)
        return true;

    // If gradient stops change, we just need to repaint. Style updates are already handled through RenderSVGGradientSTop.
    if (stops != other.stops)
        return true;

    // Changes of these flags only cause repaints.
    if (svg_inherited_flags._shapeRendering != other.svg_inherited_flags._shapeRendering
        || svg_inherited_flags._clipRule != other.svg_inherited_flags._clipRule
        || svg_inherited_flags._fillRule != other.svg_inherited_flags._fillRule
        || svg_inherited_flags._colorInterpolation != other.svg_inherited_flags._colorInterpolation
        || svg_inherited_flags._colorInterpolationFilters != other.svg_inherited_flags._colorInterpolationFilters)
        return true;

    if (svg_noninherited_flags.f.bufferedRendering != other.svg_noninherited_flags.f.bufferedRendering)
        return true;

    if (svg_noninherited_flags.f.maskType != other.svg_noninherited_flags.f.maskType)
        return true;

    return false;
}

}

#endif // ENABLE(SVG)
