SvgUnit.cs 10.2 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
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace Svg
{
    /// <summary>
    /// Represents a unit in an Scalable Vector Graphics document.
    /// </summary>
    [TypeConverter(typeof(SvgUnitConverter))]
    public struct SvgUnit
    {
        private SvgUnitType _type;
        private float _value;
        private bool _isEmpty;
        private float? _deviceValue;

20
21
22
        /// <summary>
        /// Gets and empty <see cref="SvgUnit"/>.
        /// </summary>
23
        public static readonly SvgUnit Empty = new SvgUnit();
davescriven's avatar
davescriven committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

        /// <summary>
        /// Gets a value to determine whether the unit is empty.
        /// </summary>
        public bool IsEmpty
        {
            get { return this._isEmpty; }
        }

        /// <summary>
        /// Gets the value of the unit.
        /// </summary>
        public float Value
        {
            get { return this._value; }
        }

41
42
43
        /// <summary>
        /// Gets the <see cref="SvgUnitType"/> of unit.
        /// </summary>
davescriven's avatar
davescriven committed
44
45
46
47
48
        public SvgUnitType Type
        {
            get { return this._type; }
        }

49
50
51
52
        /// <summary>
        /// Converts the current unit to one that can be used at render time.
        /// </summary>
        /// <returns>The representation of the current unit in a device value (usually pixels).</returns>
davescriven's avatar
davescriven committed
53
54
55
56
57
        public float ToDeviceValue()
        {
            return this.ToDeviceValue(null);
        }

58
59
60
61
        /// <summary>
        /// Converts the current unit to one that can be used at render time.
        /// </summary>
        /// <returns>The representation of the current unit in a device value (usually pixels).</returns>
davescriven's avatar
davescriven committed
62
63
64
65
66
        public float ToDeviceValue(ISvgStylable styleOwner)
        {
            return this.ToDeviceValue(styleOwner, false);
        }

67
68
69
70
        /// <summary>
        /// Converts the current unit to one that can be used at render time.
        /// </summary>
        /// <returns>The representation of the current unit in a device value (usually pixels).</returns>
davescriven's avatar
davescriven committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        public float ToDeviceValue(ISvgStylable styleOwner, bool vertical)
        {
            // If it's already been calculated
            if (this._deviceValue.HasValue)
            {
                return this._deviceValue.Value;
            }

            if (this._value == 0.0f)
            {
                this._deviceValue = 0.0f;
                return this._deviceValue.Value;
            }

            // http://www.w3.org/TR/CSS21/syndata.html#values
            // http://www.w3.org/TR/SVG11/coords.html#Units

            const float cmInInch = 2.54f;
            int ppi = SvgDocument.PPI;

            switch (this.Type)
            {
93
94
95
96
                case SvgUnitType.Em:
                    float points = (float)(this.Value * 9);
                    _deviceValue = (points / 72) * ppi;
                    break;
davescriven's avatar
davescriven committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
                case SvgUnitType.Centimeter:
                    _deviceValue = (float)((this.Value / cmInInch) * ppi);
                    break;
                case SvgUnitType.Inch:
                    _deviceValue = this.Value * ppi;
                    break;
                case SvgUnitType.Millimeter:
                    _deviceValue = (float)((this.Value / 10) / cmInInch) * ppi;
                    break;
                case SvgUnitType.Pica:
                    _deviceValue = ((this.Value * 12) / 72) * ppi;
                    break;
                case SvgUnitType.Point:
                    _deviceValue = (this.Value / 72) * ppi;
                    break;
                case SvgUnitType.Pixel:
                    _deviceValue = this.Value;
                    break;
                case SvgUnitType.User:
                    _deviceValue = this.Value;
                    break;
                case SvgUnitType.Percentage:
                    // Can't calculate if there is no style owner
                    if (styleOwner == null)
                    {
                        _deviceValue = this.Value;
123
                        break;
davescriven's avatar
davescriven committed
124
125
126
127
128
129
130
131
132
133
134
135
136
                    }

                    // TODO : Support height percentages
                    System.Drawing.RectangleF size = styleOwner.Bounds;
                    _deviceValue = (((vertical) ? size.Height : size.Width) / 100) * this.Value;
                    break;
                default:
                    _deviceValue = this.Value;
                    break;
            }
            return this._deviceValue.Value;
        }

137
138
139
140
        /// <summary>
        /// Converts the current unit to a percentage, if applicable.
        /// </summary>
        /// <returns>An <see cref="SvgUnit"/> of type <see cref="SvgUnitType.Perscentage"/>.</returns>
davescriven's avatar
davescriven committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
        public SvgUnit ToPercentage()
        {
            switch (this.Type)
            {
                case SvgUnitType.Percentage:
                    return this;
                case SvgUnitType.User:
                    return new SvgUnit(SvgUnitType.Percentage, this.Value * 100);
                default:
                    throw new NotImplementedException();
                    break;
            }

            return this;
        }

157
158
159
160
161
162
163
        /// <summary>
        /// Indicates whether this instance and a specified object are equal.
        /// </summary>
        /// <param name="obj">Another object to compare to.</param>
        /// <returns>
        /// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
        /// </returns>
davescriven's avatar
davescriven committed
164
165
166
        public override bool Equals(object obj)
        {
            if (obj == null)
167
            {
davescriven's avatar
davescriven committed
168
                return false;
169
            }
davescriven's avatar
davescriven committed
170
171

            if (!(obj.GetType() == typeof(SvgUnit)))
172
            {
davescriven's avatar
davescriven committed
173
                return false;
174
            }
davescriven's avatar
davescriven committed
175
176
177
178
179

            SvgUnit unit = (SvgUnit)obj;
            return (unit.Value == this.Value && unit.Type == this.Type);
        }

180
181
182
183
184
185
        /// <summary>
        /// Returns the fully qualified type name of this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> containing a fully qualified type name.
        /// </returns>
davescriven's avatar
davescriven committed
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
        public override string ToString()
        {
            string type = string.Empty;

            switch (this.Type)
            {
                case SvgUnitType.Pixel:
                    type = "px";
                    break;
                case SvgUnitType.Point:
                    type = "pt";
                    break;
                case SvgUnitType.Inch:
                    type = "in";
                    break;
                case SvgUnitType.Centimeter:
                    type = "cm";
                    break;
                case SvgUnitType.Millimeter:
                    type = "mm";
                    break;
                case SvgUnitType.Percentage:
                    type = "%";
                    break;
                case SvgUnitType.Em:
                    type = "em";
                    break;
            }

            return string.Concat(this.Value.ToString(), type);
        }

218
219
220
221
222
        /// <summary>
        /// Performs an implicit conversion from <see cref="Svg.SvgUnit"/> to <see cref="System.Single"/>.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The result of the conversion.</returns>
davescriven's avatar
davescriven committed
223
224
225
226
227
        public static implicit operator float(SvgUnit value)
        {
            return value.ToDeviceValue();
        }

228
229
230
231
232
        /// <summary>
        /// Performs an implicit conversion from <see cref="System.Single"/> to <see cref="Svg.SvgUnit"/>.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The result of the conversion.</returns>
davescriven's avatar
davescriven committed
233
234
235
236
237
        public static implicit operator SvgUnit(float value)
        {
            return new SvgUnit(value);
        }

238
239
240
241
242
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgUnit"/> struct.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="value">The value.</param>
davescriven's avatar
davescriven committed
243
244
245
246
247
248
249
250
        public SvgUnit(SvgUnitType type, float value)
        {
            this._type = type;
            this._value = value;
            this._isEmpty = (this._value == 0.0f);
            this._deviceValue = null;
        }

251
252
253
254
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgUnit"/> struct.
        /// </summary>
        /// <param name="value">The value.</param>
davescriven's avatar
davescriven committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
        public SvgUnit(float value)
        {
            this._value = value;
            this._type = SvgUnitType.User;
            this._isEmpty = (this._value == 0.0f);
            this._deviceValue = null;
        }
    }

    /// <summary>
    /// Defines the various types of unit an <see cref="SvgUnit"/> can be.
    /// </summary>
    public enum SvgUnitType
    {
        /// <summary>
        /// Indicates that the unit is in pixels.
        /// </summary>
        Pixel,
        /// <summary>
        /// Indicates that the unit is equal to the pt size of the current font.
        /// </summary>
        Em,
        /// <summary>
        /// Indicates that the unit is a percentage.
        /// </summary>
        Percentage,
        /// <summary>
        /// Indicates that the unit has no unit identifier and is a value in the current user coordinate system.
        /// </summary>
        User,
        /// <summary>
        /// Indicates the the unit is in inches.
        /// </summary>
        Inch,
        /// <summary>
        /// Indicates that the unit is in centimeters.
        /// </summary>
        Centimeter,
        /// <summary>
        /// Indicates that the unit is in millimeters.
        /// </summary>
        Millimeter,
        /// <summary>
        /// Indicates that the unit is in picas.
        /// </summary>
        Pica,
        /// <summary>
        /// Indicates that the unit is in points, the smallest unit of measure, being a subdivision of the larger <see cref="Pica"/>. There are 12 points in the <see cref="Pica"/>.
        /// </summary>
        Point
    }
}