1/*
2 * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "SVGImageCache.h"
23
24#include "FrameView.h"
25#include "GraphicsContext.h"
26#include "HTMLImageElement.h"
27#include "ImageBuffer.h"
28#include "LayoutSize.h"
29#include "Page.h"
30#include "RenderSVGRoot.h"
31#include "SVGImage.h"
32#include "SVGImageForContainer.h"
33
34namespace WebCore {
35
36SVGImageCache::SVGImageCache(SVGImage* svgImage)
37    : m_svgImage(svgImage)
38{
39    ASSERT(m_svgImage);
40}
41
42SVGImageCache::~SVGImageCache()
43{
44    m_imageForContainerMap.clear();
45}
46
47void SVGImageCache::removeClientFromCache(const CachedImageClient* client)
48{
49    ASSERT(client);
50
51    m_imageForContainerMap.remove(client);
52}
53
54void SVGImageCache::setContainerSizeForRenderer(const CachedImageClient* client, const LayoutSize& containerSize, float containerZoom)
55{
56    ASSERT(client);
57    ASSERT(!containerSize.isEmpty());
58    ASSERT(containerZoom);
59
60    FloatSize containerSizeWithoutZoom(containerSize);
61    containerSizeWithoutZoom.scale(1 / containerZoom);
62
63    m_imageForContainerMap.set(client, SVGImageForContainer::create(m_svgImage, containerSizeWithoutZoom, containerZoom));
64}
65
66FloatSize SVGImageCache::imageSizeForRenderer(const RenderObject* renderer) const
67{
68    FloatSize imageSize = m_svgImage->size();
69    if (!renderer)
70        return imageSize;
71
72    ImageForContainerMap::const_iterator it = m_imageForContainerMap.find(renderer);
73    if (it == m_imageForContainerMap.end())
74        return imageSize;
75
76    RefPtr<SVGImageForContainer> imageForContainer = it->value;
77    ASSERT(!imageForContainer->size().isEmpty());
78    return imageForContainer->size();
79}
80
81// FIXME: This doesn't take into account the animation timeline so animations will not
82// restart on page load, nor will two animations in different pages have different timelines.
83Image* SVGImageCache::imageForRenderer(const RenderObject* renderer)
84{
85    if (!renderer)
86        return Image::nullImage();
87
88    ImageForContainerMap::iterator it = m_imageForContainerMap.find(renderer);
89    if (it == m_imageForContainerMap.end())
90        return Image::nullImage();
91
92    RefPtr<SVGImageForContainer> imageForContainer = it->value;
93
94    Node* node = renderer->node();
95    if (node && isHTMLImageElement(node)) {
96        const AtomicString& urlString = toHTMLImageElement(node)->imageSourceURL();
97        URL url = node->document().completeURL(urlString);
98        imageForContainer->setURL(url);
99    }
100
101    ASSERT(!imageForContainer->size().isEmpty());
102    return imageForContainer.get();
103}
104
105} // namespace WebCore
106