/*
* Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
- * 2008 Eric Seidel <eric@webkit.org>
- * 2008 Dirk Schulze <krit@webkit.org>
+ * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
+ * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
#include "GradientAttributes.h"
#include "GraphicsContext.h"
+#include "RenderSVGText.h"
+#include "SVGImageBufferTools.h"
#include "SVGRenderSupport.h"
#include <wtf/UnusedParam.h>
RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
: RenderSVGResourceContainer(node)
+ , m_shouldCollectGradientAttributes(true)
#if PLATFORM(CG)
, m_savedContext(0)
#endif
RenderSVGResourceGradient::~RenderSVGResourceGradient()
{
- deleteAllValues(m_gradient);
- m_gradient.clear();
-}
-
-void RenderSVGResourceGradient::invalidateClients()
-{
- const HashMap<RenderObject*, GradientData*>::const_iterator end = m_gradient.end();
- for (HashMap<RenderObject*, GradientData*>::const_iterator it = m_gradient.begin(); it != end; ++it)
- markForLayoutAndResourceInvalidation(it->first);
+ if (m_gradient.isEmpty())
+ return;
deleteAllValues(m_gradient);
m_gradient.clear();
}
-void RenderSVGResourceGradient::invalidateClient(RenderObject* object)
+void RenderSVGResourceGradient::removeAllClientsFromCache(bool markForInvalidation)
{
- ASSERT(object);
-
- // FIXME: The HashMap should always contain the object on calling invalidateClient. A race condition
- // during the parsing can causes a call of invalidateClient right before the call of applyResource.
- // We return earlier for the moment. This bug should be fixed in:
- // https://bugs.webkit.org/show_bug.cgi?id=35181
- if (!m_gradient.contains(object))
- return;
+ if (!m_gradient.isEmpty()) {
+ deleteAllValues(m_gradient);
+ m_gradient.clear();
+ }
- delete m_gradient.take(object);
- markForLayoutAndResourceInvalidation(object);
+ m_shouldCollectGradientAttributes = true;
+ markAllClientsForInvalidation(markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
}
-#if PLATFORM(CG)
-static inline AffineTransform absoluteTransformForRenderer(const RenderObject* object)
+void RenderSVGResourceGradient::removeClientFromCache(RenderObject* client, bool markForInvalidation)
{
- AffineTransform absoluteTransform;
+ ASSERT(client);
- const RenderObject* currentObject = object;
- while (currentObject) {
- absoluteTransform = currentObject->localToParentTransform() * absoluteTransform;
- currentObject = currentObject->parent();
- }
+ if (m_gradient.contains(client))
+ delete m_gradient.take(client);
- return absoluteTransform;
+ markClientForInvalidation(client, markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
}
+#if PLATFORM(CG)
static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
GraphicsContext*& savedContext,
OwnPtr<ImageBuffer>& imageBuffer,
- const RenderObject* object)
+ RenderObject* object)
{
- const RenderObject* textRootBlock = findTextRootObject(object);
+ RenderObject* textRootBlock = RenderSVGText::locateRenderSVGTextAncestor(object);
+ ASSERT(textRootBlock);
- AffineTransform transform = absoluteTransformForRenderer(textRootBlock);
- FloatRect maskAbsoluteBoundingBox = transform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
+ AffineTransform absoluteTransform;
+ SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(textRootBlock, absoluteTransform);
- IntRect maskImageRect = enclosingIntRect(maskAbsoluteBoundingBox);
- if (maskImageRect.isEmpty())
+ FloatRect absoluteTargetRect = absoluteTransform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
+ FloatRect clampedAbsoluteTargetRect = SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(textRootBlock, absoluteTargetRect);
+ if (clampedAbsoluteTargetRect.isEmpty())
return false;
- // Allocate an image buffer as big as the absolute unclipped size of the object
- OwnPtr<ImageBuffer> maskImage = ImageBuffer::create(maskImageRect.size());
- if (!maskImage)
+ OwnPtr<ImageBuffer> maskImage;
+ if (!SVGImageBufferTools::createImageBuffer(absoluteTargetRect, clampedAbsoluteTargetRect, maskImage, ColorSpaceDeviceRGB))
return false;
GraphicsContext* maskImageContext = maskImage->context();
+ ASSERT(maskImageContext);
- // Transform the mask image coordinate system to absolute screen coordinates
- maskImageContext->translate(-maskAbsoluteBoundingBox.x(), -maskAbsoluteBoundingBox.y());
- maskImageContext->concatCTM(transform);
+ maskImageContext->translate(-clampedAbsoluteTargetRect.x(), -clampedAbsoluteTargetRect.y());
+ maskImageContext->concatCTM(absoluteTransform);
- imageBuffer.set(maskImage.release());
+ ASSERT(maskImage);
savedContext = context;
context = maskImageContext;
-
+ imageBuffer = maskImage.release();
return true;
}
static inline AffineTransform clipToTextMask(GraphicsContext* context,
OwnPtr<ImageBuffer>& imageBuffer,
- const RenderObject* object,
- GradientData* gradientData)
+ FloatRect& targetRect,
+ RenderObject* object,
+ bool boundingBoxMode,
+ const AffineTransform& gradientTransform)
{
- const RenderObject* textRootBlock = findTextRootObject(object);
- context->clipToImageBuffer(textRootBlock->repaintRectInLocalCoordinates(), imageBuffer.get());
+ RenderObject* textRootBlock = RenderSVGText::locateRenderSVGTextAncestor(object);
+ ASSERT(textRootBlock);
+
+ targetRect = textRootBlock->repaintRectInLocalCoordinates();
+
+ AffineTransform absoluteTransform;
+ SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(textRootBlock, absoluteTransform);
+
+ FloatRect absoluteTargetRect = absoluteTransform.mapRect(targetRect);
+ FloatRect clampedAbsoluteTargetRect = SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(textRootBlock, absoluteTargetRect);
+
+ SVGImageBufferTools::clipToImageBuffer(context, absoluteTransform, clampedAbsoluteTargetRect, imageBuffer);
AffineTransform matrix;
- if (gradientData->boundingBoxMode) {
+ if (boundingBoxMode) {
FloatRect maskBoundingBox = textRootBlock->objectBoundingBox();
matrix.translate(maskBoundingBox.x(), maskBoundingBox.y());
matrix.scaleNonUniform(maskBoundingBox.width(), maskBoundingBox.height());
}
- matrix.multiply(gradientData->transform);
+ matrix.multLeft(gradientTransform);
return matrix;
}
#endif
// Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
// Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
- // synchronization to kick in, which causes invalidateClients() to be called, which in turn deletes our
+ // synchronization to kick in, which causes removeAllClientsFromCache() to be called, which in turn deletes our
// GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
if (!gradientElement)
return false;
- gradientElement->updateAnimatedSVGAttribute(anyQName());
+ if (m_shouldCollectGradientAttributes) {
+ gradientElement->updateAnimatedSVGAttribute(anyQName());
+ collectGradientAttributes(gradientElement);
+ m_shouldCollectGradientAttributes = false;
+ }
+
+ // Spec: When the geometry of the applicable element has no width or height and objectBoundingBox is specified,
+ // then the given effect (e.g. a gradient or a filter) will be ignored.
+ FloatRect objectBoundingBox = object->objectBoundingBox();
+ if (boundingBoxMode() && objectBoundingBox.isEmpty())
+ return false;
if (!m_gradient.contains(object))
m_gradient.set(object, new GradientData);
GradientData* gradientData = m_gradient.get(object);
-
bool isPaintingText = resourceMode & ApplyToTextMode;
// Create gradient object
// resource, so don't apply it here. For non-CG platforms, we want the text bounding
// box applied to the gradient space transform now, so the gradient shader can use it.
#if PLATFORM(CG)
- if (gradientData->boundingBoxMode && !isPaintingText) {
+ if (boundingBoxMode() && !objectBoundingBox.isEmpty() && !isPaintingText) {
#else
- if (gradientData->boundingBoxMode) {
+ if (boundingBoxMode() && !objectBoundingBox.isEmpty()) {
#endif
- FloatRect objectBoundingBox = object->objectBoundingBox();
gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
}
- gradientData->userspaceTransform.multiply(gradientData->transform);
+ AffineTransform gradientTransform;
+ calculateGradientTransform(gradientTransform);
+
+ gradientData->userspaceTransform.multLeft(gradientTransform);
gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
}
gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
context->setAlpha(svgStyle->strokeOpacity());
context->setStrokeGradient(gradientData->gradient);
- applyStrokeStyleToContext(context, style, object);
+ SVGRenderSupport::applyStrokeStyleToContext(context, style, object);
}
return true;
}
-void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
+void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode, const Path* path)
{
ASSERT(context);
ASSERT(resourceMode != ApplyToDefaultMode);
context = m_savedContext;
m_savedContext = 0;
- gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, object, gradientData));
- context->setFillGradient(gradientData->gradient);
+ AffineTransform gradientTransform;
+ calculateGradientTransform(gradientTransform);
- const RenderObject* textRootBlock = findTextRootObject(object);
- context->fillRect(textRootBlock->repaintRectInLocalCoordinates());
+ FloatRect targetRect;
+ gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, targetRect, object, boundingBoxMode(), gradientTransform));
+ context->setFillGradient(gradientData->gradient);
+ context->fillRect(targetRect);
m_imageBuffer.clear();
}
#else
UNUSED_PARAM(object);
#endif
- } else {
+ } else if (path) {
if (resourceMode & ApplyToFillMode)
- context->fillPath();
+ context->fillPath(*path);
else if (resourceMode & ApplyToStrokeMode)
- context->strokePath();
+ context->strokePath(*path);
}
context->restore();