SvgVisualElementStyle.cs 14.5 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ComponentModel;
6
7
8
using Svg.DataTypes;
using System.Text.RegularExpressions;
using System.Linq;
davescriven's avatar
davescriven committed
9
10
11

namespace Svg
{
12
    public abstract partial class SvgVisualElement
davescriven's avatar
davescriven committed
13
14
15
16
17
18
19
20
21
22
23
    {
        private static float FixOpacityValue(float value)
        {
            const float max = 1.0f;
            const float min = 0.0f;
            return Math.Min(Math.Max(value, min), max);
        }

        /// <summary>
        /// Gets or sets a value to determine whether the element will be rendered.
        /// </summary>
Tebjan Halm's avatar
Tebjan Halm committed
24
        [TypeConverter(typeof(SvgBoolConverter))]
davescriven's avatar
davescriven committed
25
26
27
        [SvgAttribute("visibility")]
        public virtual bool Visible
        {
C Moore's avatar
C Moore committed
28
            get { return (this.Attributes["visibility"] == null) ? true : (bool)this.Attributes["visibility"]; }
Tebjan Halm's avatar
Tebjan Halm committed
29
            set { this.Attributes["visibility"] = value; }
davescriven's avatar
davescriven committed
30
31
        }

C Moore's avatar
C Moore committed
32
33
34
35
36
37
38
39
40
41
42
        /// <summary>
        /// Gets or sets a value to determine whether the element will be rendered.
        /// Needed to support SVG attribute display="none"
        /// </summary>
        [SvgAttribute("display")]
        public virtual string Display
        {
            get { return this.Attributes["display"] as string; }
            set { this.Attributes["display"] = value; }
        }

C Moore's avatar
C Moore committed
43
44
45
46
47
48
49
50
51
52
53
54
55
        // Displayable - false if attribute display="none", true otherwise
        protected virtual bool Displayable
        {
            get
            {
                string checkForDisplayNone = this.Attributes["display"] as string;
                if ((!string.IsNullOrEmpty(checkForDisplayNone)) && (checkForDisplayNone == "none"))
                    return false;
                else
                    return true;
            }
        }

56
57
58
        /// <summary>
        /// Gets or sets the fill <see cref="SvgPaintServer"/> of this element.
        /// </summary>
davescriven's avatar
davescriven committed
59
60
61
        [SvgAttribute("fill")]
        public virtual SvgPaintServer Fill
        {
62
63
            get { return (this.Attributes["fill"] == null) ? SvgColourServer.NotSet : (SvgPaintServer)this.Attributes["fill"]; }
            set { this.Attributes["fill"] = value; }
davescriven's avatar
davescriven committed
64
65
        }

66
67
68
        /// <summary>
        /// Gets or sets the <see cref="SvgPaintServer"/> to be used when rendering a stroke around this element.
        /// </summary>
davescriven's avatar
davescriven committed
69
70
71
        [SvgAttribute("stroke")]
        public virtual SvgPaintServer Stroke
        {
72
73
            get { return (this.Attributes["stroke"] == null) ? null : (SvgPaintServer)this.Attributes["stroke"]; }
            set { this.Attributes["stroke"] = value; }
davescriven's avatar
davescriven committed
74
75
76
77
78
        }

        [SvgAttribute("fill-rule")]
        public virtual SvgFillRule FillRule
        {
79
80
            get { return (this.Attributes["fill-rule"] == null) ? SvgFillRule.NonZero : (SvgFillRule)this.Attributes["fill-rule"]; }
            set { this.Attributes["fill-rule"] = value; }
davescriven's avatar
davescriven committed
81
82
        }

83
84
85
        /// <summary>
        /// Gets or sets the opacity of this element's <see cref="Fill"/>.
        /// </summary>
davescriven's avatar
davescriven committed
86
87
88
        [SvgAttribute("fill-opacity")]
        public virtual float FillOpacity
        {
89
90
            get { return (this.Attributes["fill-opacity"] == null) ? this.Opacity : (float)this.Attributes["fill-opacity"]; }
            set { this.Attributes["fill-opacity"] = FixOpacityValue(value); }
davescriven's avatar
davescriven committed
91
92
        }

93
94
95
        /// <summary>
        /// Gets or sets the width of the stroke (if the <see cref="Stroke"/> property has a valid value specified.
        /// </summary>
davescriven's avatar
davescriven committed
96
97
98
        [SvgAttribute("stroke-width")]
        public virtual SvgUnit StrokeWidth
        {
99
100
            get { return (this.Attributes["stroke-width"] == null) ? new SvgUnit(1.0f) : (SvgUnit)this.Attributes["stroke-width"]; }
            set { this.Attributes["stroke-width"] = value; }
davescriven's avatar
davescriven committed
101
102
103
104
105
        }

        [SvgAttribute("stroke-linecap")]
        public virtual SvgStrokeLineCap StrokeLineCap
        {
106
107
            get { return (this.Attributes["stroke-linecap"] == null) ? SvgStrokeLineCap.Butt : (SvgStrokeLineCap)this.Attributes["stroke-linecap"]; }
            set { this.Attributes["stroke-linecap"] = value; }
davescriven's avatar
davescriven committed
108
109
110
111
112
        }

        [SvgAttribute("stroke-linejoin")]
        public virtual SvgStrokeLineJoin StrokeLineJoin
        {
113
114
            get { return (this.Attributes["stroke-linejoin"] == null) ? SvgStrokeLineJoin.Miter : (SvgStrokeLineJoin)this.Attributes["stroke-linejoin"]; }
            set { this.Attributes["stroke-linejoin"] = value; }
davescriven's avatar
davescriven committed
115
116
117
118
119
        }

        [SvgAttribute("stroke-miterlimit")]
        public virtual float StrokeMiterLimit
        {
120
121
            get { return (this.Attributes["stroke-miterlimit"] == null) ? 4.0f : (float)this.Attributes["stroke-miterlimit"]; }
            set { this.Attributes["stroke-miterlimit"] = value; }
davescriven's avatar
davescriven committed
122
123
124
        }

        [SvgAttribute("stroke-dasharray")]
125
        public virtual SvgUnitCollection StrokeDashArray
davescriven's avatar
davescriven committed
126
        {
127
128
            get { return this.Attributes["stroke-dasharray"] as SvgUnitCollection; }
            set { this.Attributes["stroke-dasharray"] = value; }
davescriven's avatar
davescriven committed
129
130
131
132
133
        }

        [SvgAttribute("stroke-dashoffset")]
        public virtual SvgUnit StrokeDashOffset
        {
134
135
            get { return (this.Attributes["stroke-dashoffset"] == null) ? SvgUnit.Empty : (SvgUnit)this.Attributes["stroke-dashoffset"]; }
            set { this.Attributes["stroke-dashoffset"] = value; }
davescriven's avatar
davescriven committed
136
137
        }

138
139
140
        /// <summary>
        /// Gets or sets the opacity of the stroke, if the <see cref="Stroke"/> property has been specified. 1.0 is fully opaque; 0.0 is transparent.
        /// </summary>
davescriven's avatar
davescriven committed
141
142
143
        [SvgAttribute("stroke-opacity")]
        public virtual float StrokeOpacity
        {
144
145
            get { return (this.Attributes["stroke-opacity"] == null) ? this.Opacity : (float)this.Attributes["stroke-opacity"]; }
            set { this.Attributes["stroke-opacity"] = FixOpacityValue(value); }
davescriven's avatar
davescriven committed
146
147
148
149
150
151
152
153
        }

        /// <summary>
        /// Gets or sets the opacity of the element. 1.0 is fully opaque; 0.0 is transparent.
        /// </summary>
        [SvgAttribute("opacity")]
        public virtual float Opacity
        {
154
155
            get { return (this.Attributes["opacity"] == null) ? 1.0f : (float)this.Attributes["opacity"]; }
            set { this.Attributes["opacity"] = FixOpacityValue(value); }
davescriven's avatar
davescriven committed
156
        }
157
158
159
160
161
162
163
164

        /// <summary>
        /// Indicates which font family is to be used to render the text.
        /// </summary>
        [SvgAttribute("font-family")]
        public virtual string FontFamily
        {
            get { return this.Attributes["font-family"] as string; }
165
            set { this.Attributes["font-family"] = value; this.IsPathDirty = true; }
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
        }

        /// <summary>
        /// Refers to the size of the font from baseline to baseline when multiple lines of text are set solid in a multiline layout environment.
        /// </summary>
        [SvgAttribute("font-size")]
        public virtual SvgUnit FontSize
        {
            get { return (this.Attributes["font-size"] == null) ? SvgUnit.Empty : (SvgUnit)this.Attributes["font-size"]; }
            set { this.Attributes["font-size"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Refers to the boldness of the font.
        /// </summary>
        [SvgAttribute("font-style")]
        public virtual SvgFontStyle FontStyle
        {
            get { return (this.Attributes["font-style"] == null) ? SvgFontStyle.inherit : (SvgFontStyle)this.Attributes["font-style"]; }
            set { this.Attributes["font-style"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Refers to the boldness of the font.
        /// </summary>
        [SvgAttribute("font-variant")]
        public virtual SvgFontVariant FontVariant
        {
            get { return (this.Attributes["font-variant"] == null) ? SvgFontVariant.inherit : (SvgFontVariant)this.Attributes["font-variant"]; }
            set { this.Attributes["font-variant"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Refers to the boldness of the font.
        /// </summary>
        [SvgAttribute("font-weight")]
        public virtual SvgFontWeight FontWeight
        {
            get { return (this.Attributes["font-weight"] == null) ? SvgFontWeight.inherit : (SvgFontWeight)this.Attributes["font-weight"]; }
            set { this.Attributes["font-weight"] = value; this.IsPathDirty = true; }
        }

        private enum FontParseState
        {
            fontStyle,
            fontVariant,
            fontWeight,
            fontSize,
            fontFamilyNext,
            fontFamilyCurr
        }

        /// <summary>
        /// Set all font information.
        /// </summary>
        [SvgAttribute("font")]
        public virtual string Font
        {
            get { return (this.Attributes["font"] == null ? "" : this.Attributes["font"] as string); }
            set
            {
                var state = FontParseState.fontStyle;
                var parts = value.Split(' ');
                
                SvgFontStyle fontStyle;
                SvgFontVariant fontVariant;
                SvgFontWeight fontWeight;
                SvgUnit fontSize;

                bool success;
                string[] sizes;
                string part;

                for (int i = 0; i < parts.Length; i++)
                {
                    part = parts[i];
                    success = false;
                    while (!success)
                    {
                        switch (state)
                        {
                            case FontParseState.fontStyle:
                                success = Enums.TryParse<SvgFontStyle>(part, out fontStyle);
                                if (success) this.FontStyle = fontStyle;
                                state++;
                                break;
                            case FontParseState.fontVariant:
                                success = Enums.TryParse<SvgFontVariant>(part, out fontVariant);
                                if (success) this.FontVariant = fontVariant;
                                state++;
                                break;
                            case FontParseState.fontWeight:
                                success = Enums.TryParse<SvgFontWeight>(part, out fontWeight);
                                if (success) this.FontWeight = fontWeight;
                                state++;
                                break;
                            case FontParseState.fontSize:
                                sizes = part.Split('/');
                                try
                                {
                                    fontSize = (SvgUnit)(new SvgUnitConverter().ConvertFromInvariantString(sizes[0]));
                                    success = true;
                                    this.FontSize = fontSize;
                                }
                                catch { }
                                state++;
                                break;
                            case FontParseState.fontFamilyNext:
                                state++;
                                success = true;
                                break;
                        }
                    }

                    switch (state)
                    {
                        case FontParseState.fontFamilyNext:
                            this.FontFamily = string.Join(" ", parts, i + 1, parts.Length - (i + 1));
284
                            i = int.MaxValue - 2;
285
286
287
                            break;
                        case FontParseState.fontFamilyCurr:
                            this.FontFamily = string.Join(" ", parts, i, parts.Length - (i));
288
                            i = int.MaxValue - 2;
289
290
291
292
293
294
295
296
297
                            break;
                    }

                }

                this.Attributes["font"] = value;
                this.IsPathDirty = true;
            }
        }
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

        private const string DefaultFontFamily = "Times New Roman";

        /// <summary>
        /// Get the font information based on data stored with the text object or inherited from the parent.
        /// </summary>
        /// <returns></returns>
        internal System.Drawing.Font GetFont(SvgRenderer renderer)
        {
            // Get the font-size
            float fontSize;
            var fontSizeUnit = this.FontSize;
            if (fontSizeUnit == SvgUnit.None)
            {
                fontSize = 1.0f;
            }
            else
            {
                fontSize = fontSizeUnit.ToDeviceValue(renderer, UnitRenderingType.Vertical, this);
            }

            var fontStyle = System.Drawing.FontStyle.Regular;

            // Get the font-weight
            switch (this.FontWeight)
            {
                case SvgFontWeight.bold:
                case SvgFontWeight.bolder:
                case SvgFontWeight.w700:
                case SvgFontWeight.w800:
                case SvgFontWeight.w900:
                    fontStyle |= System.Drawing.FontStyle.Bold;
                    break;
            }

            // Get the font-style
            switch (this.FontStyle)
            {
                case SvgFontStyle.italic:
                case SvgFontStyle.oblique:
                    fontStyle |= System.Drawing.FontStyle.Italic;
                    break;
            }

            // Get the font-family
            string family = ValidateFontFamily(this.FontFamily) ?? DefaultFontFamily;
            return new System.Drawing.Font(family, fontSize, fontStyle, System.Drawing.GraphicsUnit.Pixel);
        }

        private static string ValidateFontFamily(string fontFamilyList)
        {
            // Split font family list on "," and then trim start and end spaces and quotes.
            var fontParts = (fontFamilyList ?? "").Split(new[] { ',' }).Select(fontName => fontName.Trim(new[] { '"', ' ', '\'' }));

            var families = System.Drawing.FontFamily.Families;

            // Find a the first font that exists in the list of installed font families.
            //styles from IE get sent through as lowercase.
            foreach (var f in fontParts.Where(f => families.Any(family => family.Name.ToLower() == f.ToLower())))
            {
                return f;
            }
            // No valid font family found from the list requested.
            return null;
        }

davescriven's avatar
davescriven committed
364
365
    }
}