1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Copyright (C) 2013 Google Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "core/css/resolver/ElementStyleResources.h"
25
26#include "core/css/CSSGradientValue.h"
27#include "core/css/CSSSVGDocumentValue.h"
28#include "core/rendering/style/StyleGeneratedImage.h"
29#include "core/rendering/style/StyleImage.h"
30#include "core/rendering/style/StylePendingImage.h"
31#include "platform/graphics/filters/FilterOperation.h"
32
33namespace blink {
34
35ElementStyleResources::ElementStyleResources()
36    : m_deviceScaleFactor(1)
37{
38}
39
40PassRefPtr<StyleImage> ElementStyleResources::styleImage(Document& document, const TextLinkColors& textLinkColors, Color currentColor, CSSPropertyID property, CSSValue* value)
41{
42    if (value->isImageValue())
43        return cachedOrPendingFromValue(document, property, toCSSImageValue(value));
44
45    if (value->isImageGeneratorValue()) {
46        if (value->isGradientValue())
47            return generatedOrPendingFromValue(property, toCSSGradientValue(value)->gradientWithStylesResolved(textLinkColors, currentColor).get());
48        return generatedOrPendingFromValue(property, toCSSImageGeneratorValue(value));
49    }
50
51    if (value->isImageSetValue())
52        return setOrPendingFromValue(property, toCSSImageSetValue(value));
53
54    if (value->isCursorImageValue())
55        return cursorOrPendingFromValue(property, toCSSCursorImageValue(value));
56
57    return nullptr;
58}
59
60PassRefPtr<StyleImage> ElementStyleResources::generatedOrPendingFromValue(CSSPropertyID property, CSSImageGeneratorValue* value)
61{
62    if (value->isPending()) {
63        m_pendingImageProperties.set(property, value);
64        return StylePendingImage::create(value);
65    }
66    return StyleGeneratedImage::create(value);
67}
68
69PassRefPtr<StyleImage> ElementStyleResources::setOrPendingFromValue(CSSPropertyID property, CSSImageSetValue* value)
70{
71    RefPtr<StyleImage> image = value->cachedOrPendingImageSet(m_deviceScaleFactor);
72    if (image && image->isPendingImage())
73        m_pendingImageProperties.set(property, value);
74    return image.release();
75}
76
77PassRefPtr<StyleImage> ElementStyleResources::cachedOrPendingFromValue(Document& document, CSSPropertyID property, CSSImageValue* value)
78{
79    RefPtr<StyleImage> image = value->cachedOrPendingImage();
80    if (image) {
81        if (image->isPendingImage())
82            m_pendingImageProperties.set(property, value);
83        else
84            value->restoreCachedResourceIfNeeded(document);
85    }
86    return image.release();
87}
88
89PassRefPtr<StyleImage> ElementStyleResources::cursorOrPendingFromValue(CSSPropertyID property, CSSCursorImageValue* value)
90{
91    RefPtr<StyleImage> image = value->cachedOrPendingImage(m_deviceScaleFactor);
92    if (image && image->isPendingImage())
93        m_pendingImageProperties.set(property, value);
94    return image.release();
95}
96
97void ElementStyleResources::clearPendingImageProperties()
98{
99    m_pendingImageProperties.clear();
100}
101
102void ElementStyleResources::clearPendingSVGDocuments()
103{
104    m_pendingSVGDocuments.clear();
105}
106
107void ElementStyleResources::addPendingSVGDocument(FilterOperation* filterOperation, CSSSVGDocumentValue* cssSVGDocumentValue)
108{
109    m_pendingSVGDocuments.set(filterOperation, cssSVGDocumentValue);
110}
111
112}
113