2 * Copyright (C) 2006 Rob Buis <buis@kde.org>
3 * (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2008 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "core/css/CSSCursorImageValue.h"
26 #include "core/css/CSSImageSetValue.h"
27 #include "core/fetch/ImageResource.h"
28 #include "core/fetch/ResourceFetcher.h"
29 #include "core/rendering/style/StyleFetchedImage.h"
30 #include "core/rendering/style/StyleFetchedImageSet.h"
31 #include "core/rendering/style/StyleImage.h"
32 #include "core/rendering/style/StylePendingImage.h"
33 #include "core/svg/SVGCursorElement.h"
34 #include "core/svg/SVGLengthContext.h"
35 #include "core/svg/SVGURIReference.h"
36 #include "wtf/MathExtras.h"
37 #include "wtf/text/StringBuilder.h"
38 #include "wtf/text/WTFString.h"
42 static inline SVGCursorElement* resourceReferencedByCursorElement(const String& url, TreeScope& treeScope)
44 Element* element = SVGURIReference::targetElementFromIRIString(url, treeScope);
45 return isSVGCursorElement(element) ? toSVGCursorElement(element) : 0;
48 CSSCursorImageValue::CSSCursorImageValue(PassRefPtrWillBeRawPtr<CSSValue> imageValue, bool hasHotSpot, const IntPoint& hotSpot)
49 : CSSValue(CursorImageClass)
50 , m_imageValue(imageValue)
51 , m_hasHotSpot(hasHotSpot)
53 , m_accessedImage(false)
57 CSSCursorImageValue::~CSSCursorImageValue()
59 // The below teardown is all handled by weak pointer processing in oilpan.
64 HashSet<SVGElement*>::const_iterator it = m_referencedElements.begin();
65 HashSet<SVGElement*>::const_iterator end = m_referencedElements.end();
66 String url = toCSSImageValue(m_imageValue.get())->url();
68 for (; it != end; ++it) {
69 SVGElement* referencedElement = *it;
70 referencedElement->cursorImageValueRemoved();
71 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, referencedElement->treeScope()))
72 cursorElement->removeClient(referencedElement);
77 String CSSCursorImageValue::customCSSText() const
80 result.append(m_imageValue->cssText());
83 result.appendNumber(m_hotSpot.x());
85 result.appendNumber(m_hotSpot.y());
87 return result.toString();
90 bool CSSCursorImageValue::updateIfSVGCursorIsUsed(Element* element)
92 if (!element || !element->isSVGElement())
98 String url = toCSSImageValue(m_imageValue.get())->url();
99 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, element->treeScope())) {
100 // FIXME: This will override hot spot specified in CSS, which is probably incorrect.
101 SVGLengthContext lengthContext(0);
103 float x = roundf(cursorElement->x()->currentValue()->value(lengthContext));
104 m_hotSpot.setX(static_cast<int>(x));
106 float y = roundf(cursorElement->y()->currentValue()->value(lengthContext));
107 m_hotSpot.setY(static_cast<int>(y));
109 if (cachedImageURL() != element->document().completeURL(cursorElement->href()->currentValue()->value()))
110 clearImageResource();
112 SVGElement* svgElement = toSVGElement(element);
114 m_referencedElements.add(svgElement);
116 svgElement->setCursorImageValue(this);
117 cursorElement->addClient(svgElement);
124 StyleImage* CSSCursorImageValue::cachedImage(ResourceFetcher* loader, float deviceScaleFactor)
126 if (m_imageValue->isImageSetValue())
127 return toCSSImageSetValue(m_imageValue.get())->cachedImageSet(loader, deviceScaleFactor);
129 if (!m_accessedImage) {
130 m_accessedImage = true;
132 // For SVG images we need to lazily substitute in the correct URL. Rather than attempt
133 // to change the URL of the CSSImageValue (which would then change behavior like cssText),
134 // we create an alternate CSSImageValue to use.
135 if (isSVGCursor() && loader && loader->document()) {
136 RefPtrWillBeRawPtr<CSSImageValue> imageValue = toCSSImageValue(m_imageValue.get());
137 // FIXME: This will fail if the <cursor> element is in a shadow DOM (bug 59827)
138 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(imageValue->url(), *loader->document())) {
139 RefPtrWillBeRawPtr<CSSImageValue> svgImageValue = CSSImageValue::create(loader->document()->completeURL(cursorElement->href()->currentValue()->value()));
140 svgImageValue->setReferrer(imageValue->referrer());
141 StyleFetchedImage* cachedImage = svgImageValue->cachedImage(loader);
142 m_image = cachedImage;
147 if (m_imageValue->isImageValue())
148 m_image = toCSSImageValue(m_imageValue.get())->cachedImage(loader);
151 if (m_image && m_image->isImageResource())
152 return toStyleFetchedImage(m_image);
156 StyleImage* CSSCursorImageValue::cachedOrPendingImage(float deviceScaleFactor)
158 // Need to delegate completely so that changes in device scale factor can be handled appropriately.
159 if (m_imageValue->isImageSetValue())
160 return toCSSImageSetValue(m_imageValue.get())->cachedOrPendingImageSet(deviceScaleFactor);
163 m_image = StylePendingImage::create(this);
165 return m_image.get();
168 bool CSSCursorImageValue::isSVGCursor() const
170 if (m_imageValue->isImageValue()) {
171 RefPtrWillBeRawPtr<CSSImageValue> imageValue = toCSSImageValue(m_imageValue.get());
172 KURL kurl(ParsedURLString, imageValue->url());
173 return kurl.hasFragmentIdentifier();
178 String CSSCursorImageValue::cachedImageURL()
180 if (!m_image || !m_image->isImageResource())
182 return toStyleFetchedImage(m_image)->cachedImage()->url().string();
185 void CSSCursorImageValue::clearImageResource()
188 m_accessedImage = false;
192 void CSSCursorImageValue::removeReferencedElement(SVGElement* element)
194 m_referencedElements.remove(element);
198 bool CSSCursorImageValue::equals(const CSSCursorImageValue& other) const
200 return m_hasHotSpot ? other.m_hasHotSpot && m_hotSpot == other.m_hotSpot : !other.m_hasHotSpot
201 && compareCSSValuePtr(m_imageValue, other.m_imageValue);
204 void CSSCursorImageValue::traceAfterDispatch(Visitor* visitor)
206 visitor->trace(m_imageValue);
207 CSSValue::traceAfterDispatch(visitor);
210 } // namespace WebCore