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
|
/*
* Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2007 Rob Buis <buis@kde.org>
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
* Copyright (C) 2009 Google, Inc.
* Copyright (c) 2020, 2021, 2022 Igalia S.L.
*
* 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 "RenderSVGViewportContainer.h"
#include "RenderLayer.h"
#include "RenderSVGModelObjectInlines.h"
#include "RenderSVGRoot.h"
#include "SVGContainerLayout.h"
#include "SVGElementTypeHelpers.h"
#include "SVGSVGElement.h"
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(RenderSVGViewportContainer);
RenderSVGViewportContainer::RenderSVGViewportContainer(RenderSVGRoot& parent, RenderStyle&& style)
: RenderSVGContainer(Type::SVGViewportContainer, parent.document(), WTFMove(style))
, m_owningSVGRoot(parent)
{
ASSERT(isRenderSVGViewportContainer());
}
RenderSVGViewportContainer::RenderSVGViewportContainer(SVGSVGElement& element, RenderStyle&& style)
: RenderSVGContainer(Type::SVGViewportContainer, element, WTFMove(style))
{
ASSERT(isRenderSVGViewportContainer());
}
RenderSVGViewportContainer::~RenderSVGViewportContainer() = default;
SVGSVGElement& RenderSVGViewportContainer::svgSVGElement() const
{
if (isOutermostSVGViewportContainer()) {
ASSERT(m_owningSVGRoot);
return m_owningSVGRoot->svgSVGElement();
}
return downcast<SVGSVGElement>(RenderSVGContainer::element());
}
Ref<SVGSVGElement> RenderSVGViewportContainer::protectedSVGSVGElement() const
{
return svgSVGElement();
}
FloatPoint RenderSVGViewportContainer::computeViewportLocation() const
{
if (isOutermostSVGViewportContainer())
return { };
Ref useSVGSVGElement = svgSVGElement();
SVGLengthContext lengthContext(useSVGSVGElement.ptr());
return { useSVGSVGElement->x().value(lengthContext), useSVGSVGElement->y().value(lengthContext) };
}
FloatSize RenderSVGViewportContainer::computeViewportSize() const
{
if (isOutermostSVGViewportContainer())
return downcast<RenderSVGRoot>(*parent()).computeViewportSize();
Ref useSVGSVGElement = svgSVGElement();
SVGLengthContext lengthContext(useSVGSVGElement.ptr());
return { useSVGSVGElement->width().value(lengthContext), useSVGSVGElement->height().value(lengthContext) };
}
bool RenderSVGViewportContainer::updateLayoutSizeIfNeeded()
{
auto previousViewportSize = viewportSize();
m_viewport = { computeViewportLocation(), computeViewportSize() };
return selfNeedsLayout() || previousViewportSize != viewportSize();
}
bool RenderSVGViewportContainer::needsHasSVGTransformFlags() const
{
Ref useSVGSVGElement = svgSVGElement();
if (useSVGSVGElement->hasTransformRelatedAttributes())
return true;
if (isOutermostSVGViewportContainer())
return !useSVGSVGElement->currentTranslateValue().isZero() || useSVGSVGElement->renderer()->style().usedZoom() != 1;
return false;
}
void RenderSVGViewportContainer::updateFromStyle()
{
RenderSVGContainer::updateFromStyle();
if (SVGRenderSupport::isOverflowHidden(*this))
setHasNonVisibleOverflow();
}
inline AffineTransform viewBoxToViewTransform(const SVGSVGElement& svgSVGElement, const FloatSize& viewportSize)
{
return svgSVGElement.viewBoxToViewTransform(viewportSize.width(), viewportSize.height());
}
void RenderSVGViewportContainer::updateLayerTransform()
{
ASSERT(hasLayer());
// First update the supplemental layer transform.
Ref useSVGSVGElement = svgSVGElement();
auto viewportSize = this->viewportSize();
m_supplementalLayerTransform.makeIdentity();
if (isOutermostSVGViewportContainer()) {
// Handle pan - set on outermost <svg> element.
if (auto translation = useSVGSVGElement->currentTranslateValue(); !translation.isZero())
m_supplementalLayerTransform.translate(translation);
// Handle zoom - take effective zoom from outermost <svg> element.
if (auto scale = useSVGSVGElement->renderer()->style().usedZoom(); scale != 1) {
m_supplementalLayerTransform.scale(scale);
viewportSize.scale(1.0 / scale);
}
} else if (!m_viewport.location().isZero())
m_supplementalLayerTransform.translate(m_viewport.location());
if (useSVGSVGElement->hasAttribute(SVGNames::viewBoxAttr)) {
// An empty viewBox disables the rendering -- dirty the visible descendant status!
if (useSVGSVGElement->hasEmptyViewBox())
layer()->dirtyVisibleContentStatus();
else if (auto viewBoxTransform = viewBoxToViewTransform(useSVGSVGElement, viewportSize); !viewBoxTransform.isIdentity()) {
if (m_supplementalLayerTransform.isIdentity())
m_supplementalLayerTransform = viewBoxTransform;
else
m_supplementalLayerTransform.multiply(viewBoxTransform);
}
}
// After updating the supplemental layer transform we're able to use it in RenderLayerModelObjects::updateLayerTransform().
RenderSVGContainer::updateLayerTransform();
}
void RenderSVGViewportContainer::applyTransform(TransformationMatrix& transform, const RenderStyle& style, const FloatRect& boundingBox, OptionSet<RenderStyle::TransformOperationOption> options) const
{
applySVGTransform(transform, protectedSVGSVGElement(), style, boundingBox, m_supplementalLayerTransform.isIdentity() ? std::nullopt : std::make_optional(m_supplementalLayerTransform), std::nullopt, options);
}
LayoutRect RenderSVGViewportContainer::overflowClipRect(const LayoutPoint& location, OverlayScrollbarSizeRelevancy, PaintPhase) const
{
// Overflow for the outermost <svg> element is handled in RenderSVGRoot, not here.
ASSERT(!isOutermostSVGViewportContainer());
Ref useSVGSVGElement = svgSVGElement();
auto clipRect = enclosingLayoutRect(viewport());
if (useSVGSVGElement->hasAttribute(SVGNames::viewBoxAttr)) {
if (useSVGSVGElement->hasEmptyViewBox())
return { };
if (auto viewBoxTransform = viewBoxToViewTransform(useSVGSVGElement, viewportSize()); !viewBoxTransform.isIdentity())
clipRect = enclosingLayoutRect(viewBoxTransform.inverse().value_or(AffineTransform { }).mapRect(viewport()));
}
clipRect.moveBy(location);
return clipRect;
}
}
|