File indexing completed on 2024-04-28 11:39:10

0001 /*
0002     Copyright (C) 2006 Apple Computer, Inc.
0003                   2006 Nikolas Zimmermann <zimmermann@kde.org>
0004 
0005     This file is part of the WebKit project
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef SVGDocumentExtensions_h
0024 #define SVGDocumentExtensions_h
0025 
0026 #if ENABLE(SVG)
0027 
0028 #include <memory>
0029 #include <wtf/Forward.h>
0030 #include <wtf/HashSet.h>
0031 #include <wtf/HashMap.h>
0032 
0033 #include "FloatRect.h"
0034 #include "StringHash.h"
0035 //#include "StringImpl.h"
0036 #include "AtomicString.h"
0037 #include "xml/Document.h"
0038 
0039 namespace DOM
0040 {
0041 class EventListener;
0042 }
0043 
0044 namespace WebCore
0045 {
0046 
0047 //class AtomicString;
0048 //class Document;
0049 //class EventListener;
0050 //class Node;
0051 //class String;
0052 class SVGElement;
0053 class SVGElementInstance;
0054 class SVGStyledElement;
0055 class SVGSVGElement;
0056 class TimeScheduler;
0057 
0058 class DOMStringHash
0059 {
0060 public:
0061     static unsigned hash(DOMString key)
0062     {
0063         return qHash(key.implementation());
0064     }
0065     static bool equal(DOMString a, DOMString b)
0066     {
0067         return a == b;
0068     }
0069     static const bool safeToCompareToEmptyOrDeleted = false;
0070 };
0071 
0072 class SVGDocumentExtensions
0073 {
0074 public:
0075     SVGDocumentExtensions(Document *);
0076     ~SVGDocumentExtensions();
0077 
0078     DOM::EventListener *createSVGEventListener(const DOMString &functionName, const DOMString &code, DOM::NodeImpl *);
0079 
0080     void addTimeContainer(SVGSVGElement *);
0081     void removeTimeContainer(SVGSVGElement *);
0082 
0083     void startAnimations();
0084     void pauseAnimations();
0085     void unpauseAnimations();
0086 
0087     void reportWarning(const String &);
0088     void reportError(const String &);
0089 
0090 private:
0091     Document *m_doc; // weak reference
0092     HashSet<SVGSVGElement *> m_timeContainers; // For SVG 1.2 support this will need to be made more general.
0093     //HashMap<String, HashSet<SVGStyledElement*>*, DOMStringHash> m_pendingResources;
0094     HashMap<SVGElement *, HashSet<SVGElementInstance *>*> m_elementInstances;
0095 
0096     SVGDocumentExtensions(const SVGDocumentExtensions &);
0097     SVGDocumentExtensions &operator=(const SVGDocumentExtensions &);
0098 
0099     template<typename ValueType>
0100     HashMap<const SVGElement *, HashMap<StringImpl *, ValueType>*> *baseValueMap() const
0101     {
0102         static HashMap<const SVGElement *, HashMap<StringImpl *, ValueType>*> *s_baseValueMap = new HashMap<const SVGElement *, HashMap<StringImpl *, ValueType>*>();
0103         return s_baseValueMap;
0104     }
0105 
0106 public:
0107     // This HashMap contains a list of pending resources. Pending resources, are such
0108     // which are referenced by any object in the SVG document, but do NOT exist yet.
0109     // For instance, dynamically build gradients / patterns / clippers...
0110     void addPendingResource(const AtomicString &id, SVGStyledElement *);
0111     bool isPendingResource(const AtomicString &id) const;
0112     std::unique_ptr<HashSet<SVGStyledElement *> > removePendingResource(const AtomicString &id);
0113 
0114     // This HashMap maps elements to their instances, when they are used by <use> elements.
0115     // This is needed to synchronize the original element with the internally cloned one.
0116     void mapInstanceToElement(SVGElementInstance *, SVGElement *);
0117     void removeInstanceMapping(SVGElementInstance *, SVGElement *);
0118     HashSet<SVGElementInstance *> *instancesForElement(SVGElement *) const;
0119 
0120     // Used by the ANIMATED_PROPERTY_* macros
0121     template<typename ValueType>
0122     ValueType baseValue(const SVGElement *element, const AtomicString &propertyName) const
0123     {
0124         HashMap<StringImpl *, ValueType> *propertyMap = baseValueMap<ValueType>()->get(element);
0125         if (propertyMap) {
0126             return propertyMap->get(propertyName.impl());
0127         }
0128 
0129         return ValueType();
0130     }
0131 
0132     template<typename ValueType>
0133     void setBaseValue(const SVGElement *element, const AtomicString &propertyName, ValueType newValue)
0134     {
0135         HashMap<StringImpl *, ValueType> *propertyMap = baseValueMap<ValueType>()->get(element);
0136         if (!propertyMap) {
0137             propertyMap = new HashMap<StringImpl *, ValueType>();
0138             baseValueMap<ValueType>()->set(element, propertyMap);
0139         }
0140 
0141         propertyMap->set(propertyName.impl(), newValue);
0142     }
0143 
0144     template<typename ValueType>
0145     void removeBaseValue(const SVGElement *element, const AtomicString &propertyName)
0146     {
0147         HashMap<StringImpl *, ValueType> *propertyMap = baseValueMap<ValueType>()->get(element);
0148         if (!propertyMap) {
0149             return;
0150         }
0151 
0152         propertyMap->remove(propertyName.impl());
0153     }
0154 
0155     template<typename ValueType>
0156     bool hasBaseValue(const SVGElement *element, const AtomicString &propertyName) const
0157     {
0158         HashMap<StringImpl *, ValueType> *propertyMap = baseValueMap<ValueType>()->get(element);
0159         if (propertyMap) {
0160             return propertyMap->contains(propertyName.impl());
0161         }
0162 
0163         return false;
0164     }
0165 };
0166 
0167 // Special handling for WebCore::String
0168 template<>
0169 inline String SVGDocumentExtensions::baseValue<String>(const SVGElement *element, const AtomicString &propertyName) const
0170 {
0171     HashMap<StringImpl *, String> *propertyMap = baseValueMap<String>()->get(element);
0172     if (propertyMap) {
0173         return propertyMap->get(propertyName.impl());
0174     }
0175 
0176     return String();
0177 }
0178 
0179 // Special handling for WebCore::FloatRect
0180 template<>
0181 inline FloatRect SVGDocumentExtensions::baseValue<FloatRect>(const SVGElement *element, const AtomicString &propertyName) const
0182 {
0183     HashMap<StringImpl *, FloatRect> *propertyMap = baseValueMap<FloatRect>()->get(element);
0184     if (propertyMap) {
0185         return propertyMap->get(propertyName.impl());
0186     }
0187 
0188     return FloatRect();
0189 }
0190 
0191 // Special handling for booleans
0192 template<>
0193 inline bool SVGDocumentExtensions::baseValue<bool>(const SVGElement *element, const AtomicString &propertyName) const
0194 {
0195     HashMap<StringImpl *, bool> *propertyMap = baseValueMap<bool>()->get(element);
0196     if (propertyMap) {
0197         return propertyMap->get(propertyName.impl());
0198     }
0199 
0200     return false;
0201 }
0202 
0203 // Special handling for doubles
0204 template<>
0205 inline double SVGDocumentExtensions::baseValue<double>(const SVGElement *element, const AtomicString &propertyName) const
0206 {
0207     HashMap<StringImpl *, double> *propertyMap = baseValueMap<double>()->get(element);
0208     if (propertyMap) {
0209         return propertyMap->get(propertyName.impl());
0210     }
0211 
0212     return 0.0;
0213 }
0214 
0215 }
0216 
0217 #endif // ENABLE(SVG)
0218 
0219 #endif