#include "config.h"
#include "SVGImage.h"
#include "Chrome.h"
#include "DocumentLoader.h"
#include "ElementIterator.h"
#include "FrameLoader.h"
#include "FrameView.h"
#include "ImageBuffer.h"
#include "ImageObserver.h"
#include "IntRect.h"
#include "MainFrame.h"
#include "PageConfiguration.h"
#include "RenderSVGRoot.h"
#include "RenderStyle.h"
#include "SVGDocument.h"
#include "SVGForeignObjectElement.h"
#include "SVGImageClients.h"
#include "SVGSVGElement.h"
#include "Settings.h"
namespace WebCore {
SVGImage::SVGImage(ImageObserver& observer, const URL& url)
: Image(&observer)
, m_url(url)
{
}
SVGImage::~SVGImage()
{
if (m_page) {
std::unique_ptr<Page> currentPage = WTF::move(m_page);
currentPage->mainFrame().loader().frameDetached(); }
ASSERT(!m_chromeClient || !m_chromeClient->image());
}
inline SVGSVGElement* SVGImage::rootElement() const
{
if (!m_page)
return nullptr;
return downcast<SVGDocument>(*m_page->mainFrame().document()).rootElement();
}
bool SVGImage::hasSingleSecurityOrigin() const
{
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return true;
if (descendantsOfType<SVGForeignObjectElement>(*rootElement).first())
return false;
return true;
}
void SVGImage::setContainerSize(const FloatSize& size)
{
if (!usesContainerSize())
return;
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return;
auto* renderer = downcast<RenderSVGRoot>(rootElement->renderer());
if (!renderer)
return;
FrameView* view = frameView();
view->resize(this->containerSize());
renderer->setContainerSize(IntSize(size));
}
IntSize SVGImage::containerSize() const
{
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return IntSize();
auto* renderer = downcast<RenderSVGRoot>(rootElement->renderer());
if (!renderer)
return IntSize();
IntSize containerSize = renderer->containerSize();
if (!containerSize.isEmpty())
return containerSize;
ASSERT(renderer->style().effectiveZoom() == 1);
FloatSize currentSize;
if (rootElement->hasIntrinsicWidth() && rootElement->hasIntrinsicHeight())
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, ColorSpace colorSpace, CompositeOperator compositeOp, BlendMode blendMode)
{
if (!m_page)
return;
ImageObserver* observer = imageObserver();
ASSERT(observer);
setImageObserver(nullptr);
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, colorSpace, compositeOp, blendMode, ImageOrientationDescription());
setImageObserver(observer);
}
#if USE(CAIRO)
PassNativeImagePtr SVGImage::nativeImageForCurrentFrame()
{
if (!m_page)
return 0;
std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(size(), 1);
if (!buffer) return 0;
draw(buffer->context(), rect(), rect(), ColorSpaceDeviceRGB, CompositeSourceOver, BlendModeNormal, ImageOrientationDescription());
return buffer->copyImage(CopyBackingStore)->nativeImageForCurrentFrame();
}
#endif
void SVGImage::drawPatternForContainer(GraphicsContext* context, const FloatSize& containerSize, float zoom, const FloatRect& srcRect,
const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, ColorSpace colorSpace, CompositeOperator compositeOp, const FloatRect& dstRect, BlendMode blendMode)
{
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());
FloatRect imageBufferSize = zoomedContainerRect;
imageBufferSize.scale(imageBufferScale.width(), imageBufferScale.height());
std::unique_ptr<ImageBuffer> buffer = ImageBuffer::createCompatibleBuffer(expandedIntSize(imageBufferSize.size()), 1, ColorSpaceDeviceRGB, context, true);
if (!buffer) return;
drawForContainer(buffer->context(), containerSize, zoom, imageBufferSize, zoomedContainerRect, ColorSpaceDeviceRGB, CompositeSourceOver, BlendModeNormal);
if (context->drawLuminanceMask())
buffer->convertToLuminanceMask();
RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore, Unscaled);
if (!image)
return;
FloatRect scaledSrcRect = srcRect;
scaledSrcRect.scale(imageBufferScale.width(), imageBufferScale.height());
AffineTransform unscaledPatternTransform(patternTransform);
unscaledPatternTransform.scale(1 / imageBufferScale.width(), 1 / imageBufferScale.height());
context->setDrawLuminanceMask(false);
image->drawPattern(context, scaledSrcRect, unscaledPatternTransform, phase, spacing, colorSpace, compositeOp, dstRect, blendMode);
}
void SVGImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace, CompositeOperator compositeOp, BlendMode blendMode, ImageOrientationDescription)
{
if (!m_page)
return;
FrameView* view = frameView();
ASSERT(view);
GraphicsContextStateSaver stateSaver(*context);
context->setCompositeOperation(compositeOp, blendMode);
context->clip(enclosingIntRect(dstRect));
float alpha = context->alpha();
bool compositingRequiresTransparencyLayer = compositeOp != CompositeSourceOver || blendMode != BlendModeNormal || alpha < 1;
if (compositingRequiresTransparencyLayer) {
context->beginTransparencyLayer(alpha);
context->setCompositeOperation(CompositeSourceOver, BlendModeNormal);
}
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);
view->resize(containerSize());
if (!m_url.isEmpty())
view->scrollToFragment(m_url);
if (view->needsLayout())
view->layout();
view->paint(context, intersection(context->clipBounds(), enclosingIntRect(srcRect)));
if (compositingRequiresTransparencyLayer)
context->endTransparencyLayer();
stateSaver.restore();
if (imageObserver())
imageObserver()->didDraw(this);
}
RenderBox* SVGImage::embeddedContentBox() const
{
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return nullptr;
return downcast<RenderBox>(rootElement->renderer());
}
FrameView* SVGImage::frameView() const
{
if (!m_page)
return nullptr;
return m_page->mainFrame().view();
}
bool SVGImage::hasRelativeWidth() const
{
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return false;
return rootElement->intrinsicWidth().isPercentOrCalculated();
}
bool SVGImage::hasRelativeHeight() const
{
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return false;
return rootElement->intrinsicHeight().isPercentOrCalculated();
}
void SVGImage::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
{
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return;
intrinsicWidth = rootElement->intrinsicWidth();
intrinsicHeight = rootElement->intrinsicHeight();
if (rootElement->preserveAspectRatio().align() == SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_NONE)
return;
intrinsicRatio = rootElement->viewBox().size();
if (intrinsicRatio.isEmpty() && intrinsicWidth.isFixed() && intrinsicHeight.isFixed())
intrinsicRatio = FloatSize(floatValueForLength(intrinsicWidth, 0), floatValueForLength(intrinsicHeight, 0));
}
void SVGImage::startAnimation(CatchUpAnimation)
{
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return;
rootElement->unpauseAnimations();
rootElement->setCurrentTime(0);
}
void SVGImage::stopAnimation()
{
SVGSVGElement* rootElement = this->rootElement();
if (!rootElement)
return;
rootElement->pauseAnimations();
}
void SVGImage::resetAnimation()
{
stopAnimation();
}
bool SVGImage::dataChanged(bool allDataReceived)
{
if (!data()->size())
return true;
if (allDataReceived) {
PageConfiguration pageConfiguration;
fillWithEmptyClients(pageConfiguration);
m_chromeClient = std::make_unique<SVGImageChromeClient>(this);
pageConfiguration.chromeClient = m_chromeClient.get();
m_loaderClient = std::make_unique<SVGFrameLoaderClient>(m_dataProtocolLoader);
pageConfiguration.loaderClientForMainFrame = m_loaderClient.get();
m_page = std::make_unique<Page>(pageConfiguration);
m_page->settings().setMediaEnabled(false);
m_page->settings().setScriptEnabled(false);
m_page->settings().setPluginsEnabled(false);
Frame& frame = m_page->mainFrame();
frame.setView(FrameView::create(frame));
frame.init();
FrameLoader& loader = frame.loader();
loader.forceSandboxFlags(SandboxAll);
frame.view()->setCanHaveScrollbars(false); frame.view()->setTransparent(true);
ASSERT(loader.activeDocumentLoader()); loader.activeDocumentLoader()->writer().setMIMEType("image/svg+xml");
loader.activeDocumentLoader()->writer().begin(URL()); loader.activeDocumentLoader()->writer().addData(data()->data(), data()->size());
loader.activeDocumentLoader()->writer().end();
m_intrinsicSize = containerSize();
}
return m_page != nullptr;
}
String SVGImage::filenameExtension() const
{
return "svg";
}
bool isInSVGImage(const Element* element)
{
ASSERT(element);
Page* page = element->document().page();
if (!page)
return false;
return page->chrome().client().isSVGImageChromeClient();
}
}