1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Rob Buis <buis@kde.org>
* Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
* Copyright (C) 2018 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"
#include "SVGClipPathElement.h"
#include "Document.h"
#include "ImageBuffer.h"
#include "LegacyRenderSVGResourceClipper.h"
#include "RenderElementInlines.h"
#include "RenderSVGResourceClipper.h"
#include "RenderSVGText.h"
#include "RenderStyleInlines.h"
#include "SVGElementInlines.h"
#include "SVGElementTypeHelpers.h"
#include "SVGLayerTransformComputation.h"
#include "SVGNames.h"
#include "SVGUseElement.h"
#include "StyleResolver.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(SVGClipPathElement);
inline SVGClipPathElement::SVGClipPathElement(const QualifiedName& tagName, Document& document)
: SVGGraphicsElement(tagName, document, makeUniqueRef<PropertyRegistry>(*this))
{
ASSERT(hasTagName(SVGNames::clipPathTag));
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
PropertyRegistry::registerProperty<SVGNames::clipPathUnitsAttr, SVGUnitTypes::SVGUnitType, &SVGClipPathElement::m_clipPathUnits>();
});}
Ref<SVGClipPathElement> SVGClipPathElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new SVGClipPathElement(tagName, document));
}
void SVGClipPathElement::attributeChanged(const QualifiedName& name, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason attributeModificationReason)
{
if (name == SVGNames::clipPathUnitsAttr) {
auto propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(newValue);
if (propertyValue > 0)
m_clipPathUnits->setBaseValInternal<SVGUnitTypes::SVGUnitType>(propertyValue);
}
SVGGraphicsElement::attributeChanged(name, oldValue, newValue, attributeModificationReason);
}
void SVGClipPathElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (PropertyRegistry::isKnownAttribute(attrName)) {
InstanceInvalidationGuard guard(*this);
if (document().settings().layerBasedSVGEngineEnabled()) {
if (CheckedPtr renderer = this->renderer())
renderer->repaintClientsOfReferencedSVGResources();
return;
}
updateSVGRendererForElementChange();
return;
}
SVGGraphicsElement::svgAttributeChanged(attrName);
}
void SVGClipPathElement::childrenChanged(const ChildChange& change)
{
SVGGraphicsElement::childrenChanged(change);
if (change.source == ChildChange::Source::Parser)
return;
if (document().settings().layerBasedSVGEngineEnabled()) {
if (CheckedPtr renderer = this->renderer())
renderer->repaintClientsOfReferencedSVGResources();
return;
}
updateSVGRendererForElementChange();
}
RenderPtr<RenderElement> SVGClipPathElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
if (document().settings().layerBasedSVGEngineEnabled())
return createRenderer<RenderSVGResourceClipper>(*this, WTFMove(style));
return createRenderer<LegacyRenderSVGResourceClipper>(*this, WTFMove(style));
}
RefPtr<SVGGraphicsElement> SVGClipPathElement::shouldApplyPathClipping() const
{
// If the current clip-path gets clipped itself, we have to fall back to masking.
if (renderer() && renderer()->style().clipPath())
return nullptr;
auto rendererRequiresMaskClipping = [](const RenderObject& renderer) -> bool {
// Only shapes or paths are supported for direct clipping. We need to fall back to masking for texts.
if (is<RenderSVGText>(renderer))
return true;
auto& style = renderer.style();
if (style.display() == DisplayType::None || style.usedVisibility() != Visibility::Visible)
return false;
// Current shape in clip-path gets clipped too. Fall back to masking.
return style.clipPath();
};
RefPtr<SVGGraphicsElement> useGraphicsElement;
// If clip-path only contains one visible shape or path, we can use path-based clipping. Invisible
// shapes don't affect the clipping and can be ignored. If clip-path contains more than one
// visible shape, the additive clipping may not work, caused by the clipRule. EvenOdd
// as well as NonZero can cause self-clipping of the elements.
// See also http://www.w3.org/TR/SVG/painting.html#FillRuleProperty
for (auto* childNode = firstChild(); childNode; childNode = childNode->nextSibling()) {
RefPtr graphicsElement = dynamicDowncast<SVGGraphicsElement>(*childNode);
if (!graphicsElement)
continue;
CheckedPtr renderer = graphicsElement->renderer();
if (!renderer)
continue;
if (rendererRequiresMaskClipping(*renderer))
return nullptr;
// Fallback to masking, if there is more than one clipping path.
if (useGraphicsElement)
return nullptr;
// For <use> elements, delegate the decision whether to use mask clipping or not to the referenced element.
if (auto* useElement = dynamicDowncast<SVGUseElement>(*graphicsElement)) {
CheckedPtr clipChildRenderer = useElement->rendererClipChild();
if (clipChildRenderer && rendererRequiresMaskClipping(*clipChildRenderer))
return nullptr;
}
useGraphicsElement = WTFMove(graphicsElement);
}
return useGraphicsElement;
}
FloatRect SVGClipPathElement::calculateClipContentRepaintRect(RepaintRectCalculation repaintRectCalculation)
{
ASSERT(renderer());
auto transformationMatrixFromChild = [&](const RenderLayerModelObject& child) -> std::optional<AffineTransform> {
if (!document().settings().layerBasedSVGEngineEnabled())
return std::nullopt;
if (!(renderer()->isTransformed() || child.isTransformed()) || !child.hasLayer())
return std::nullopt;
ASSERT(child.isSVGLayerAwareRenderer());
ASSERT(!child.isRenderSVGRoot());
auto transform = SVGLayerTransformComputation(child).computeAccumulatedTransform(downcast<RenderLayerModelObject>(renderer()), TransformState::TrackSVGCTMMatrix);
return transform.isIdentity() ? std::nullopt : std::make_optional(WTFMove(transform));
};
FloatRect clipContentRepaintRect;
// This is a rough heuristic to appraise the clip size and doesn't consider clip on clip.
for (auto* childNode = firstChild(); childNode; childNode = childNode->nextSibling()) {
CheckedPtr renderer = childNode->renderer();
if (!childNode->isSVGElement() || !renderer)
continue;
if (!renderer->isRenderSVGShape() && !renderer->isRenderSVGText() && !childNode->hasTagName(SVGNames::useTag))
continue;
auto& style = renderer->style();
if (style.display() == DisplayType::None || style.usedVisibility() != Visibility::Visible)
continue;
auto r = renderer->repaintRectInLocalCoordinates(repaintRectCalculation);
if (auto transform = transformationMatrixFromChild(downcast<RenderLayerModelObject>(*renderer)))
r = transform->mapRect(r);
clipContentRepaintRect.unite(r);
}
return clipContentRepaintRect;
}
}
|