This source file includes following definitions.
- isInSVGImage
- currentFrameHasSingleSecurityOrigin
- svgRootElement
- setContainerSize
- containerSize
- drawForContainer
- nativeImageForCurrentFrame
- drawPatternForContainer
- draw
- embeddedContentBox
- frameView
- hasRelativeWidth
- hasRelativeHeight
- computeIntrinsicDimensions
- startAnimation
- stopAnimation
- resetAnimation
- hasAnimations
- dataChanged
- filenameExtension
#include "config.h"
#include "core/svg/graphics/SVGImage.h"
#include "core/animation/DocumentTimeline.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/shadow/ComposedTreeWalker.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "core/loader/FrameLoadRequest.h"
#include "core/page/Chrome.h"
#include "core/rendering/style/RenderStyle.h"
#include "core/rendering/svg/RenderSVGRoot.h"
#include "core/svg/SVGDocument.h"
#include "core/svg/SVGFEImageElement.h"
#include "core/svg/SVGImageElement.h"
#include "core/svg/SVGSVGElement.h"
#include "core/svg/animation/SMILTimeContainer.h"
#include "core/svg/graphics/SVGImageChromeClient.h"
#include "platform/LengthFunctions.h"
#include "platform/geometry/IntRect.h"
#include "platform/graphics/GraphicsContextStateSaver.h"
#include "platform/graphics/ImageBuffer.h"
#include "platform/graphics/ImageObserver.h"
#include "wtf/PassRefPtr.h"
namespace WebCore {
SVGImage::SVGImage(ImageObserver* observer)
: Image(observer)
{
}
SVGImage::~SVGImage()
{
if (m_page) {
OwnPtr<Page> currentPage = m_page.release();
currentPage->mainFrame()->loader().frameDetached();
}
ASSERT(!m_chromeClient || !m_chromeClient->image());
}
bool SVGImage::isInSVGImage(const Element* element)
{
ASSERT(element);
Page* page = element->document().page();
if (!page)
return false;
return page->chrome().client().isSVGImageChromeClient();
}
bool SVGImage::currentFrameHasSingleSecurityOrigin() const
{
if (!m_page)
return true;
LocalFrame* frame = m_page->mainFrame();
RELEASE_ASSERT(frame->document()->loadEventFinished());
SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
if (!rootElement)
return true;
ComposedTreeWalker walker(rootElement);
while (Node* node = walker.get()) {
if (isSVGForeignObjectElement(*node))
return false;
if (isSVGImageElement(*node)) {
if (!toSVGImageElement(*node).currentFrameHasSingleSecurityOrigin())
return false;
} else if (isSVGFEImageElement(*node)) {
if (!toSVGFEImageElement(*node).currentFrameHasSingleSecurityOrigin())
return false;
}
walker.next();
}
return true;
}
static SVGSVGElement* svgRootElement(Page* page)
{
if (!page)
return 0;
LocalFrame* frame = page->mainFrame();
return toSVGDocument(frame->document())->rootElement();
}
void SVGImage::setContainerSize(const IntSize& size)
{
if (!usesContainerSize())
return;
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return;
FrameView* view = frameView();
view->resize(this->containerSize());
RenderSVGRoot* renderer = toRenderSVGRoot(rootElement->renderer());
if (!renderer)
return;
renderer->setContainerSize(size);
}
IntSize SVGImage::containerSize() const
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return IntSize();
RenderSVGRoot* renderer = toRenderSVGRoot(rootElement->renderer());
if (!renderer)
return IntSize();
IntSize containerSize = renderer->containerSize();
if (!containerSize.isEmpty())
return containerSize;
ASSERT(renderer->style()->effectiveZoom() == 1);
FloatSize currentSize;
if (rootElement->intrinsicWidth().isFixed() && rootElement->intrinsicHeight().isFixed())
currentSize = rootElement->currentViewportSize();
else
currentSize = rootElement->currentViewBoxRect().size();
if (!currentSize.isEmpty())
return IntSize(static_cast<int>(ceilf(currentSize.width())), static_cast<int>(ceilf(currentSize.height())));
return IntSize(300, 150);
}
void SVGImage::drawForContainer(GraphicsContext* context, const FloatSize containerSize, float zoom, const FloatRect& dstRect,
const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
{
if (!m_page)
return;
ImageObserverDisabler imageObserverDisabler(this);
IntSize roundedContainerSize = roundedIntSize(containerSize);
setContainerSize(roundedContainerSize);
FloatRect scaledSrc = srcRect;
scaledSrc.scale(1 / zoom);
FloatSize adjustedSrcSize = scaledSrc.size();
adjustedSrcSize.scale(roundedContainerSize.width() / containerSize.width(), roundedContainerSize.height() / containerSize.height());
scaledSrc.setSize(adjustedSrcSize);
draw(context, dstRect, scaledSrc, compositeOp, blendMode);
}
PassRefPtr<NativeImageSkia> SVGImage::nativeImageForCurrentFrame()
{
if (!m_page)
return nullptr;
OwnPtr<ImageBuffer> buffer = ImageBuffer::create(size());
if (!buffer)
return nullptr;
drawForContainer(buffer->context(), size(), 1, rect(), rect(), CompositeSourceOver, blink::WebBlendModeNormal);
return buffer->copyImage(CopyBackingStore)->nativeImageForCurrentFrame();
}
void SVGImage::drawPatternForContainer(GraphicsContext* context, const FloatSize containerSize, float zoom, const FloatRect& srcRect,
const FloatSize& scale, const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& dstRect, blink::WebBlendMode blendMode, const IntSize& repeatSpacing)
{
FloatRect zoomedContainerRect = FloatRect(FloatPoint(), containerSize);
zoomedContainerRect.scale(zoom);
AffineTransform transform = context->getCTM();
FloatSize imageBufferScale = FloatSize(transform.xScale(), transform.yScale());
ASSERT(imageBufferScale.width());
ASSERT(imageBufferScale.height());
FloatSize scaleWithoutCTM(scale.width() / imageBufferScale.width(), scale.height() / imageBufferScale.height());
FloatRect imageBufferSize = zoomedContainerRect;
imageBufferSize.scale(imageBufferScale.width(), imageBufferScale.height());
OwnPtr<ImageBuffer> buffer = ImageBuffer::create(expandedIntSize(imageBufferSize.size()));
if (!buffer)
return;
drawForContainer(buffer->context(), containerSize, zoom, imageBufferSize, zoomedContainerRect, CompositeSourceOver, blink::WebBlendModeNormal);
RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore, Unscaled);
FloatRect scaledSrcRect = srcRect;
scaledSrcRect.scale(imageBufferScale.width(), imageBufferScale.height());
image->drawPattern(context, scaledSrcRect, scaleWithoutCTM, phase, compositeOp, dstRect, blendMode, repeatSpacing);
}
void SVGImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
{
if (!m_page)
return;
GraphicsContextStateSaver stateSaver(*context);
context->setCompositeOperation(compositeOp, blendMode);
context->clip(enclosingIntRect(dstRect));
bool compositingRequiresTransparencyLayer = compositeOp != CompositeSourceOver || blendMode != blink::WebBlendModeNormal;
float opacity = context->getNormalizedAlpha() / 255.f;
bool requiresTransparencyLayer = compositingRequiresTransparencyLayer || opacity < 1;
if (requiresTransparencyLayer) {
context->beginTransparencyLayer(opacity);
if (compositingRequiresTransparencyLayer)
context->setCompositeOperation(CompositeSourceOver, blink::WebBlendModeNormal);
}
FloatSize scale(dstRect.width() / srcRect.width(), dstRect.height() / srcRect.height());
FloatSize topLeftOffset(srcRect.location().x() * scale.width(), srcRect.location().y() * scale.height());
FloatPoint destOffset = dstRect.location() - topLeftOffset;
context->translate(destOffset.x(), destOffset.y());
context->scale(scale);
FrameView* view = frameView();
view->resize(containerSize());
if (view->needsLayout())
view->layout();
view->paint(context, enclosingIntRect(srcRect));
ASSERT(!view->needsLayout());
if (requiresTransparencyLayer)
context->endLayer();
stateSaver.restore();
if (imageObserver())
imageObserver()->didDraw(this);
startAnimation();
}
RenderBox* SVGImage::embeddedContentBox() const
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return 0;
return toRenderBox(rootElement->renderer());
}
FrameView* SVGImage::frameView() const
{
if (!m_page)
return 0;
return m_page->mainFrame()->view();
}
bool SVGImage::hasRelativeWidth() const
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return false;
return rootElement->intrinsicWidth().isPercent();
}
bool SVGImage::hasRelativeHeight() const
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return false;
return rootElement->intrinsicHeight().isPercent();
}
void SVGImage::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return;
intrinsicWidth = rootElement->intrinsicWidth();
intrinsicHeight = rootElement->intrinsicHeight();
if (rootElement->preserveAspectRatio()->currentValue()->align() == SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_NONE)
return;
intrinsicRatio = rootElement->viewBox()->currentValue()->value().size();
if (intrinsicRatio.isEmpty() && intrinsicWidth.isFixed() && intrinsicHeight.isFixed())
intrinsicRatio = FloatSize(floatValueForLength(intrinsicWidth, 0), floatValueForLength(intrinsicHeight, 0));
}
void SVGImage::startAnimation(bool )
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement || !rootElement->animationsPaused())
return;
rootElement->unpauseAnimations();
}
void SVGImage::stopAnimation()
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return;
rootElement->pauseAnimations();
}
void SVGImage::resetAnimation()
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return;
rootElement->pauseAnimations();
rootElement->setCurrentTime(0);
}
bool SVGImage::hasAnimations() const
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return false;
return rootElement->timeContainer()->hasAnimations() || m_page->mainFrame()->document()->timeline().hasPendingUpdates();
}
bool SVGImage::dataChanged(bool allDataReceived)
{
TRACE_EVENT0("webkit", "SVGImage::dataChanged");
if (!data()->size())
return true;
if (allDataReceived) {
static FrameLoaderClient* dummyFrameLoaderClient = new EmptyFrameLoaderClient;
Page::PageClients pageClients;
fillWithEmptyClients(pageClients);
m_chromeClient = adoptPtr(new SVGImageChromeClient(this));
pageClients.chromeClient = m_chromeClient.get();
OwnPtr<Page> page = adoptPtr(new Page(pageClients));
page->settings().setScriptEnabled(false);
page->settings().setPluginsEnabled(false);
page->settings().setAcceleratedCompositingEnabled(false);
RefPtr<LocalFrame> frame = LocalFrame::create(dummyFrameLoaderClient, &page->frameHost(), 0);
frame->setView(FrameView::create(frame.get()));
frame->init();
FrameLoader& loader = frame->loader();
loader.forceSandboxFlags(SandboxAll);
frame->view()->setScrollbarsSuppressed(true);
frame->view()->setCanHaveScrollbars(false);
frame->view()->setTransparent(true);
m_page = page.release();
loader.load(FrameLoadRequest(0, blankURL(), SubstituteData(data(), "image/svg+xml", "UTF-8", KURL(), ForceSynchronousLoad)));
m_intrinsicSize = containerSize();
}
return m_page;
}
String SVGImage::filenameExtension() const
{
return "svg";
}
}