SvgAttributeCollection.cs 8.92 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Svg
{
    /// <summary>
    /// A collection of Scalable Vector Attributes that can be inherited from the owner elements ancestors.
    /// </summary>
    public sealed class SvgAttributeCollection : Dictionary<string, object>
    {
        private SvgElement _owner;

        /// <summary>
        /// Initialises a new instance of a <see cref="SvgAttributeCollection"/> with the given <see cref="SvgElement"/> as the owner.
        /// </summary>
        /// <param name="owner">The <see cref="SvgElement"/> owner of the collection.</param>
        public SvgAttributeCollection(SvgElement owner)
        {
            this._owner = owner;
        }

        /// <summary>
        /// Gets the attribute with the specified name.
        /// </summary>
        /// <typeparam name="TAttributeType">The type of the attribute value.</typeparam>
        /// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
        /// <returns>The attribute value if available; otherwise the default value of <typeparamref name="TAttributeType"/>.</returns>
        public TAttributeType GetAttribute<TAttributeType>(string attributeName)
        {
            if (this.ContainsKey(attributeName) && base[attributeName] != null)
            {
                return (TAttributeType)base[attributeName];
            }

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
            return this.GetAttribute<TAttributeType>(attributeName, default(TAttributeType));
        }

        /// <summary>
        /// Gets the attribute with the specified name.
        /// </summary>
        /// <typeparam name="T">The type of the attribute value.</typeparam>
        /// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
        /// <param name="defaultValue">The value to return if a value hasn't already been specified.</param>
        /// <returns>The attribute value if available; otherwise the default value of <typeparamref name="T"/>.</returns>
        public T GetAttribute<T>(string attributeName, T defaultValue)
        {
            if (this.ContainsKey(attributeName) && base[attributeName] != null)
            {
                return (T)base[attributeName];
            }

            return defaultValue;
davescriven's avatar
davescriven committed
56
57
58
59
60
61
62
63
64
65
        }

        /// <summary>
        /// Gets the attribute with the specified name and inherits from ancestors if there is no attribute set.
        /// </summary>
        /// <typeparam name="TAttributeType">The type of the attribute value.</typeparam>
        /// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
        /// <returns>The attribute value if available; otherwise the ancestors value for the same attribute; otherwise the default value of <typeparamref name="TAttributeType"/>.</returns>
        public TAttributeType GetInheritedAttribute<TAttributeType>(string attributeName)
        {
66
            if (this.ContainsKey(attributeName) && !IsInheritValue(base[attributeName]))
davescriven's avatar
davescriven committed
67
            {
68
69
70
71
                var result = (TAttributeType)base[attributeName];
                var deferred = result as SvgDeferredPaintServer;
                if (deferred != null) deferred.EnsureServer(_owner);
                return result;
davescriven's avatar
davescriven committed
72
73
74
75
76
77
78
79
80
81
82
83
84
            }

            if (this._owner.Parent != null)
            {
                if (this._owner.Parent.Attributes[attributeName] != null)
                {
                    return (TAttributeType)this._owner.Parent.Attributes[attributeName];
                }
            }

            return default(TAttributeType);
        }

85
86
87
        private bool IsInheritValue(object value)
        {
            return (value == null ||
88
89
90
91
                    (value is SvgFontWeight && (SvgFontWeight)value == SvgFontWeight.Inherit) ||
                    (value is SvgTextAnchor && (SvgTextAnchor)value == SvgTextAnchor.Inherit) ||
                    (value is SvgFontVariant && (SvgFontVariant)value == SvgFontVariant.Inherit) || 
                    (value is SvgTextDecoration && (SvgTextDecoration)value == SvgTextDecoration.Inherit) ||
Eric Domke's avatar
Eric Domke committed
92
                    (value is XmlSpaceHandling && (XmlSpaceHandling)value == XmlSpaceHandling.inherit) ||
93
                    (value is SvgOverflow && (SvgOverflow)value == SvgOverflow.Inherit) ||
Eric Domke's avatar
Eric Domke committed
94
                    (value == SvgColourServer.Inherit) || 
Eric Domke's avatar
Eric Domke committed
95
                    (value is string && (string)value == "inherit")
96
97
98
                   );
        }

davescriven's avatar
davescriven committed
99
100
101
102
103
104
105
106
        /// <summary>
        /// Gets the attribute with the specified name.
        /// </summary>
        /// <param name="attributeName">A <see cref="string"/> containing the attribute name.</param>
        /// <returns>The attribute value associated with the specified name; If there is no attribute the parent's value will be inherited.</returns>
        public new object this[string attributeName]
        {
            get { return this.GetInheritedAttribute<object>(attributeName); }
107
108
109
110
            set 
            {
            	if(base.ContainsKey(attributeName))
            	{
111
	            	var oldVal = base[attributeName];	            	
112
	            	if(TryUnboxedCheck(oldVal, value))
113
114
115
116
	            	{
	            		base[attributeName] = value;
	            		OnAttributeChanged(attributeName, value);
	            	}
117
118
119
120
121
122
123
124
125
            	}
            	else
            	{
            		base[attributeName] = value;
	            	OnAttributeChanged(attributeName, value);
            	}
            }
        }
        
126
        private bool TryUnboxedCheck(object a, object b)
127
128
129
130
131
132
133
134
135
136
137
        {
        	if(IsValueType(a))
        	{
        		if(a is SvgUnit)
        			return UnboxAndCheck<SvgUnit>(a, b);
        		else if(a is bool)
        			return UnboxAndCheck<bool>(a, b);
        		else if(a is int)
        			return UnboxAndCheck<int>(a, b);
        		else if(a is float)
        			return UnboxAndCheck<float>(a, b);
tebjan's avatar
tebjan committed
138
139
        		else if(a is SvgViewBox)
        			return UnboxAndCheck<SvgViewBox>(a, b);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
        		else
        			return true;
        	}
        	else
        	{
        		return a != b;
        	}
        }
        
        private bool UnboxAndCheck<T>(object a, object b)
        {
        	return !((T)a).Equals((T)b);
        }
        
        private bool IsValueType(object obj) 
        {
        	return obj != null && obj.GetType().IsValueType;
        }
        
159
160
161
162
163
        /// <summary>
        /// Fired when an Atrribute has changed
        /// </summary>
        public event EventHandler<AttributeEventArgs> AttributeChanged;
        
164
        private void OnAttributeChanged(string attribute, object value)
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
        {
        	var handler = AttributeChanged;
        	if(handler != null)
        	{
        		handler(this._owner, new AttributeEventArgs { Attribute = attribute, Value = value });
        	}
        }
    }
    
    
    /// <summary>
    /// A collection of Custom Attributes
    /// </summary>
    public sealed class SvgCustomAttributeCollection : Dictionary<string, string>
    {
        private SvgElement _owner;

        /// <summary>
        /// Initialises a new instance of a <see cref="SvgAttributeCollection"/> with the given <see cref="SvgElement"/> as the owner.
        /// </summary>
        /// <param name="owner">The <see cref="SvgElement"/> owner of the collection.</param>
        public SvgCustomAttributeCollection(SvgElement owner)
        {
            this._owner = owner;
        }

        /// <summary>
        /// Gets the attribute with the specified name.
        /// </summary>
        /// <param name="attributeName">A <see cref="string"/> containing the attribute name.</param>
        /// <returns>The attribute value associated with the specified name; If there is no attribute the parent's value will be inherited.</returns>
        public new string this[string attributeName]
        {
        	get { return base[attributeName]; }
            set 
            {
            	if(base.ContainsKey(attributeName))
            	{
	            	var oldVal = base[attributeName];
	            	base[attributeName] = value;
	            	if(oldVal != value) OnAttributeChanged(attributeName, value);
            	}
            	else
            	{
            		base[attributeName] = value;
	            	OnAttributeChanged(attributeName, value);
            	}
            }
        }
        
        /// <summary>
        /// Fired when an Atrribute has changed
        /// </summary>
        public event EventHandler<AttributeEventArgs> AttributeChanged;
        
220
        private void OnAttributeChanged(string attribute, object value)
221
222
223
224
225
226
        {
        	var handler = AttributeChanged;
        	if(handler != null)
        	{
        		handler(this._owner, new AttributeEventArgs { Attribute = attribute, Value = value });
        	}
davescriven's avatar
davescriven committed
227
228
229
        }
    }
}