This source file includes following definitions.
- m_shouldCollectPatternAttributes
- removeAllClientsFromCache
- removeClientFromCache
- buildPattern
- applyResource
- postApplyResource
- calculatePatternBoundaries
- buildTileImageTransform
- createTileImage
#include "config.h"
#include "core/rendering/svg/RenderSVGResourcePattern.h"
#include "core/rendering/svg/SVGRenderSupport.h"
#include "core/rendering/svg/SVGRenderingContext.h"
#include "core/svg/SVGFitToViewBox.h"
#include "platform/graphics/GraphicsContext.h"
namespace WebCore {
const RenderSVGResourceType RenderSVGResourcePattern::s_resourceType = PatternResourceType;
RenderSVGResourcePattern::RenderSVGResourcePattern(SVGPatternElement* node)
: RenderSVGResourceContainer(node)
, m_shouldCollectPatternAttributes(true)
{
}
void RenderSVGResourcePattern::removeAllClientsFromCache(bool markForInvalidation)
{
m_patternMap.clear();
m_shouldCollectPatternAttributes = true;
markAllClientsForInvalidation(markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
}
void RenderSVGResourcePattern::removeClientFromCache(RenderObject* client, bool markForInvalidation)
{
ASSERT(client);
m_patternMap.remove(client);
markClientForInvalidation(client, markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
}
PatternData* RenderSVGResourcePattern::buildPattern(RenderObject* object, unsigned short resourceMode)
{
ASSERT(object);
PatternData* currentData = m_patternMap.get(object);
if (currentData && currentData->pattern)
return currentData;
SVGPatternElement* patternElement = toSVGPatternElement(element());
if (!patternElement)
return 0;
if (m_shouldCollectPatternAttributes) {
patternElement->synchronizeAnimatedSVGAttribute(anyQName());
m_attributes = PatternAttributes();
patternElement->collectPatternAttributes(m_attributes);
m_shouldCollectPatternAttributes = false;
}
if (!m_attributes.patternContentElement())
return 0;
if (m_attributes.hasViewBox() && m_attributes.viewBox().isEmpty())
return 0;
FloatRect tileBoundaries;
AffineTransform tileImageTransform;
if (!buildTileImageTransform(object, m_attributes, patternElement, tileBoundaries, tileImageTransform))
return 0;
AffineTransform absoluteTransformIgnoringRotation;
SVGRenderingContext::calculateDeviceSpaceTransformation(object, absoluteTransformIgnoringRotation);
SVGRenderingContext::clear2DRotation(absoluteTransformIgnoringRotation);
FloatRect absoluteTileBoundaries = absoluteTransformIgnoringRotation.mapRect(tileBoundaries);
FloatRect clampedAbsoluteTileBoundaries;
absoluteTileBoundaries.scale(static_cast<float>(m_attributes.patternTransform().xScale()),
static_cast<float>(m_attributes.patternTransform().yScale()));
OwnPtr<ImageBuffer> tileImage = createTileImage(m_attributes, tileBoundaries, absoluteTileBoundaries, tileImageTransform, clampedAbsoluteTileBoundaries);
if (!tileImage)
return 0;
RefPtr<Image> copiedImage = tileImage->copyImage(CopyBackingStore);
if (!copiedImage)
return 0;
OwnPtr<PatternData> patternData = adoptPtr(new PatternData);
patternData->pattern = Pattern::create(copiedImage, true, true);
const IntSize tileImageSize = tileImage->size();
patternData->transform.translate(tileBoundaries.x(), tileBoundaries.y());
patternData->transform.scale(tileBoundaries.width() / tileImageSize.width(), tileBoundaries.height() / tileImageSize.height());
AffineTransform patternTransform = m_attributes.patternTransform();
if (!patternTransform.isIdentity())
patternData->transform = patternTransform * patternData->transform;
if (resourceMode & ApplyToTextMode) {
AffineTransform additionalTextTransformation;
if (shouldTransformOnTextPainting(object, additionalTextTransformation))
patternData->transform *= additionalTextTransformation;
}
patternData->pattern->setPatternSpaceTransform(patternData->transform);
return m_patternMap.set(object, patternData.release()).storedValue->value.get();
}
bool RenderSVGResourcePattern::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
{
ASSERT(object);
ASSERT(style);
ASSERT(context);
ASSERT(resourceMode != ApplyToDefaultMode);
clearInvalidationMask();
FloatRect objectBoundingBox = object->objectBoundingBox();
if (m_attributes.patternUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX && objectBoundingBox.isEmpty())
return false;
PatternData* patternData = buildPattern(object, resourceMode);
if (!patternData)
return false;
context->save();
const SVGRenderStyle* svgStyle = style->svgStyle();
ASSERT(svgStyle);
if (resourceMode & ApplyToFillMode) {
context->setAlphaAsFloat(svgStyle->fillOpacity());
context->setFillPattern(patternData->pattern);
context->setFillRule(svgStyle->fillRule());
} else if (resourceMode & ApplyToStrokeMode) {
if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
patternData->pattern->setPatternSpaceTransform(transformOnNonScalingStroke(object, patternData->transform));
context->setAlphaAsFloat(svgStyle->strokeOpacity());
context->setStrokePattern(patternData->pattern);
SVGRenderSupport::applyStrokeStyleToContext(context, style, object);
}
if (resourceMode & ApplyToTextMode) {
if (resourceMode & ApplyToFillMode)
context->setTextDrawingMode(TextModeFill);
else if (resourceMode & ApplyToStrokeMode)
context->setTextDrawingMode(TextModeStroke);
}
return true;
}
void RenderSVGResourcePattern::postApplyResource(RenderObject*, GraphicsContext*& context, unsigned short resourceMode, const Path* path, const RenderSVGShape* shape)
{
ASSERT(context);
ASSERT(resourceMode != ApplyToDefaultMode);
if (resourceMode & ApplyToFillMode) {
if (path)
context->fillPath(*path);
else if (shape)
shape->fillShape(context);
}
if (resourceMode & ApplyToStrokeMode) {
if (path)
context->strokePath(*path);
else if (shape)
shape->strokeShape(context);
}
context->restore();
}
static inline FloatRect calculatePatternBoundaries(const PatternAttributes& attributes,
const FloatRect& objectBoundingBox,
const SVGPatternElement* patternElement)
{
ASSERT(patternElement);
return SVGLengthContext::resolveRectangle(patternElement, attributes.patternUnits(), objectBoundingBox, attributes.x(), attributes.y(), attributes.width(), attributes.height());
}
bool RenderSVGResourcePattern::buildTileImageTransform(RenderObject* renderer,
const PatternAttributes& attributes,
const SVGPatternElement* patternElement,
FloatRect& patternBoundaries,
AffineTransform& tileImageTransform) const
{
ASSERT(renderer);
ASSERT(patternElement);
FloatRect objectBoundingBox = renderer->objectBoundingBox();
patternBoundaries = calculatePatternBoundaries(attributes, objectBoundingBox, patternElement);
if (patternBoundaries.width() <= 0 || patternBoundaries.height() <= 0)
return false;
AffineTransform viewBoxCTM = SVGFitToViewBox::viewBoxToViewTransform(attributes.viewBox(), attributes.preserveAspectRatio(), patternBoundaries.width(), patternBoundaries.height());
if (!viewBoxCTM.isIdentity())
tileImageTransform = viewBoxCTM;
else if (attributes.patternContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
tileImageTransform.scale(objectBoundingBox.width(), objectBoundingBox.height());
return true;
}
PassOwnPtr<ImageBuffer> RenderSVGResourcePattern::createTileImage(const PatternAttributes& attributes,
const FloatRect& tileBoundaries,
const FloatRect& absoluteTileBoundaries,
const AffineTransform& tileImageTransform,
FloatRect& clampedAbsoluteTileBoundaries) const
{
clampedAbsoluteTileBoundaries = SVGRenderingContext::clampedAbsoluteTargetRect(absoluteTileBoundaries);
IntSize imageSize(roundedIntSize(clampedAbsoluteTileBoundaries.size()));
if (imageSize.isEmpty())
return nullptr;
OwnPtr<ImageBuffer> tileImage = ImageBuffer::create(imageSize);
if (!tileImage)
return nullptr;
GraphicsContext* tileImageContext = tileImage->context();
ASSERT(tileImageContext);
IntSize unclampedImageSize(roundedIntSize(absoluteTileBoundaries.size()));
tileImageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTileBoundaries.width(), unclampedImageSize.height() / absoluteTileBoundaries.height()));
tileImageContext->scale(FloatSize(clampedAbsoluteTileBoundaries.width() / tileBoundaries.width(),
clampedAbsoluteTileBoundaries.height() / tileBoundaries.height()));
if (!tileImageTransform.isIdentity())
tileImageContext->concatCTM(tileImageTransform);
AffineTransform contentTransformation;
if (attributes.patternContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
contentTransformation = tileImageTransform;
for (Element* element = ElementTraversal::firstWithin(*attributes.patternContentElement()); element; element = ElementTraversal::nextSibling(*element)) {
if (!element->isSVGElement() || !element->renderer())
continue;
if (element->renderer()->needsLayout())
return nullptr;
SVGRenderingContext::renderSubtree(tileImage->context(), element->renderer(), contentTransformation);
}
return tileImage.release();
}
}