SvgElementStyle.cs 17.7 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
1
2
3
using System;
using System.Collections.Generic;
using System.Drawing;
4
5
6
using System.Drawing.Text;
using SystemFontStyle = System.Drawing.FontStyle;
using SystemFontFamily = System.Drawing.FontFamily;
Eric Domke's avatar
Eric Domke committed
7
8
9
10
11
12
13
14
using System.ComponentModel;
using System.Linq;

namespace Svg
{
    public partial class SvgElement
    {
        private bool _dirty;
Hayk's avatar
Hayk committed
15

Eric Domke's avatar
Eric Domke committed
16
        /// <summary>
17
        /// Gets or sets a value indicating whether this element's 'Path' is dirty.
Eric Domke's avatar
Eric Domke committed
18
19
20
21
22
23
24
25
26
27
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the path is dirty; otherwise, <c>false</c>.
        /// </value>
        protected virtual bool IsPathDirty
        {
            get { return this._dirty; }
            set { this._dirty = value; }
        }

28
29
30
31
32
33
34
35
36
37
38
39
        /// <summary>
        /// Force recreation of the paths for the element and it's children.
        /// </summary>
        public void InvalidateChildPaths()
        {
            this.IsPathDirty = true;
            foreach (SvgElement element in this.Children)
            {
                element.InvalidateChildPaths();
            }
        }

40
        protected static float FixOpacityValue(float value)
Eric Domke's avatar
Eric Domke committed
41
42
43
44
45
46
47
48
49
        {
            const float max = 1.0f;
            const float min = 0.0f;
            return Math.Min(Math.Max(value, min), max);
        }

        /// <summary>
        /// Gets or sets the fill <see cref="SvgPaintServer"/> of this element.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
50
        [SvgAttribute("fill", true)]
Eric Domke's avatar
Eric Domke committed
51
52
        public virtual SvgPaintServer Fill
        {
HeinrichAD's avatar
HeinrichAD committed
53
            get { return ((SvgPaintServer)this.Attributes["fill"] ?? SvgColourServer.NotSet); }
Eric Domke's avatar
Eric Domke committed
54
55
56
57
58
59
            set { this.Attributes["fill"] = value; }
        }

        /// <summary>
        /// Gets or sets the <see cref="SvgPaintServer"/> to be used when rendering a stroke around this element.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
60
        [SvgAttribute("stroke", true)]
Eric Domke's avatar
Eric Domke committed
61
62
        public virtual SvgPaintServer Stroke
        {
HeinrichAD's avatar
HeinrichAD committed
63
            get { return (SvgPaintServer)this.Attributes["stroke"]; }
Eric Domke's avatar
Eric Domke committed
64
65
66
            set { this.Attributes["stroke"] = value; }
        }

Eric Domke's avatar
Eric Domke committed
67
        [SvgAttribute("fill-rule", true)]
Eric Domke's avatar
Eric Domke committed
68
69
        public virtual SvgFillRule FillRule
        {
HeinrichAD's avatar
HeinrichAD committed
70
            get { return (SvgFillRule)(this.Attributes["fill-rule"] ?? SvgFillRule.NonZero); }
Eric Domke's avatar
Eric Domke committed
71
72
73
74
75
76
            set { this.Attributes["fill-rule"] = value; }
        }

        /// <summary>
        /// Gets or sets the opacity of this element's <see cref="Fill"/>.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
77
        [SvgAttribute("fill-opacity", true)]
Eric Domke's avatar
Eric Domke committed
78
79
        public virtual float FillOpacity
        {
80
            get { return (float)(this.Attributes["fill-opacity"] ?? 1.0f); }
Eric Domke's avatar
Eric Domke committed
81
82
83
84
85
86
            set { this.Attributes["fill-opacity"] = FixOpacityValue(value); }
        }

        /// <summary>
        /// Gets or sets the width of the stroke (if the <see cref="Stroke"/> property has a valid value specified.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
87
        [SvgAttribute("stroke-width", true)]
Eric Domke's avatar
Eric Domke committed
88
89
        public virtual SvgUnit StrokeWidth
        {
HeinrichAD's avatar
HeinrichAD committed
90
            get { return (SvgUnit)(this.Attributes["stroke-width"] ?? new SvgUnit(1.0f)); }
Eric Domke's avatar
Eric Domke committed
91
92
93
            set { this.Attributes["stroke-width"] = value; }
        }

Eric Domke's avatar
Eric Domke committed
94
        [SvgAttribute("stroke-linecap", true)]
Eric Domke's avatar
Eric Domke committed
95
96
        public virtual SvgStrokeLineCap StrokeLineCap
        {
HeinrichAD's avatar
HeinrichAD committed
97
            get { return (SvgStrokeLineCap)(this.Attributes["stroke-linecap"] ?? SvgStrokeLineCap.Butt); }
Eric Domke's avatar
Eric Domke committed
98
99
100
            set { this.Attributes["stroke-linecap"] = value; }
        }

Eric Domke's avatar
Eric Domke committed
101
        [SvgAttribute("stroke-linejoin", true)]
Eric Domke's avatar
Eric Domke committed
102
103
        public virtual SvgStrokeLineJoin StrokeLineJoin
        {
HeinrichAD's avatar
HeinrichAD committed
104
            get { return (SvgStrokeLineJoin)(this.Attributes["stroke-linejoin"] ?? SvgStrokeLineJoin.Miter); }
Eric Domke's avatar
Eric Domke committed
105
106
107
            set { this.Attributes["stroke-linejoin"] = value; }
        }

Eric Domke's avatar
Eric Domke committed
108
        [SvgAttribute("stroke-miterlimit", true)]
Eric Domke's avatar
Eric Domke committed
109
110
        public virtual float StrokeMiterLimit
        {
HeinrichAD's avatar
HeinrichAD committed
111
            get { return (float)(this.Attributes["stroke-miterlimit"] ?? 4f); }
Eric Domke's avatar
Eric Domke committed
112
113
114
            set { this.Attributes["stroke-miterlimit"] = value; }
        }

Eric Domke's avatar
Eric Domke committed
115
        [SvgAttribute("stroke-dasharray", true)]
Eric Domke's avatar
Eric Domke committed
116
117
118
119
120
121
        public virtual SvgUnitCollection StrokeDashArray
        {
            get { return this.Attributes["stroke-dasharray"] as SvgUnitCollection; }
            set { this.Attributes["stroke-dasharray"] = value; }
        }

Eric Domke's avatar
Eric Domke committed
122
        [SvgAttribute("stroke-dashoffset", true)]
Eric Domke's avatar
Eric Domke committed
123
124
        public virtual SvgUnit StrokeDashOffset
        {
HeinrichAD's avatar
HeinrichAD committed
125
            get { return (SvgUnit)(this.Attributes["stroke-dashoffset"] ?? SvgUnit.Empty); }
Eric Domke's avatar
Eric Domke committed
126
127
128
129
130
131
            set { this.Attributes["stroke-dashoffset"] = value; }
        }

        /// <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>
Eric Domke's avatar
Eric Domke committed
132
        [SvgAttribute("stroke-opacity", true)]
Eric Domke's avatar
Eric Domke committed
133
134
        public virtual float StrokeOpacity
        {
135
            get { return (float)(this.Attributes["stroke-opacity"] ?? 1.0f); }
Eric Domke's avatar
Eric Domke committed
136
137
138
139
140
141
142
            set { this.Attributes["stroke-opacity"] = FixOpacityValue(value); }
        }

        /// <summary>
        /// Gets or sets the colour of the gradient stop.
        /// </summary>
        /// <remarks>Apparently this can be set on non-sensical elements.  Don't ask; just check the tests.</remarks>
Eric Domke's avatar
Eric Domke committed
143
        [SvgAttribute("stop-color", true)]
Eric Domke's avatar
Eric Domke committed
144
        [TypeConverter(typeof(SvgPaintServerFactory))]
145
        public virtual SvgPaintServer StopColor
Eric Domke's avatar
Eric Domke committed
146
147
148
149
150
151
152
153
        {
            get { return this.Attributes["stop-color"] as SvgPaintServer; }
            set { this.Attributes["stop-color"] = value; }
        }

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

161
162
163
164
165
166
167
168
169
170
        /// <summary>
        /// Refers to the AnitAlias rendering of shapes.
        /// </summary>
        [SvgAttribute("shape-rendering")]
        public virtual SvgShapeRendering ShapeRendering
        {
            get { return this.Attributes.GetInheritedAttribute<SvgShapeRendering>("shape-rendering"); }
            set { this.Attributes["shape-rendering"] = value; }
        }

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
        /// <summary>
        /// Gets or sets the text anchor.
        /// </summary>
        [SvgAttribute("text-anchor", true)]
        public virtual SvgTextAnchor TextAnchor
        {
            get { return this.Attributes.GetInheritedAttribute<SvgTextAnchor>("text-anchor"); }
            set { this.Attributes["text-anchor"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Specifies dominant-baseline positioning of text.
        /// </summary>
        [SvgAttribute("baseline-shift", true)]
        public virtual string BaselineShift
        {
            get { return this.Attributes.GetInheritedAttribute<string>("baseline-shift"); }
            set { this.Attributes["baseline-shift"] = value; this.IsPathDirty = true; }
        }

Eric Domke's avatar
Eric Domke committed
191
192
193
        /// <summary>
        /// Indicates which font family is to be used to render the text.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
194
        [SvgAttribute("font-family", true)]
Eric Domke's avatar
Eric Domke committed
195
196
197
198
199
200
201
202
203
        public virtual string FontFamily
        {
            get { return this.Attributes["font-family"] as string; }
            set { this.Attributes["font-family"] = value; this.IsPathDirty = true; }
        }

        /// <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>
Eric Domke's avatar
Eric Domke committed
204
        [SvgAttribute("font-size", true)]
Eric Domke's avatar
Eric Domke committed
205
206
        public virtual SvgUnit FontSize
        {
HeinrichAD's avatar
HeinrichAD committed
207
            get { return (SvgUnit)(this.Attributes["font-size"] ?? SvgUnit.Empty); }
Eric Domke's avatar
Eric Domke committed
208
209
210
211
212
213
            set { this.Attributes["font-size"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Refers to the style of the font.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
214
        [SvgAttribute("font-style", true)]
Eric Domke's avatar
Eric Domke committed
215
216
        public virtual SvgFontStyle FontStyle
        {
HeinrichAD's avatar
HeinrichAD committed
217
            get { return (SvgFontStyle)(this.Attributes["font-style"] ?? SvgFontStyle.All); }
Eric Domke's avatar
Eric Domke committed
218
219
220
221
222
223
            set { this.Attributes["font-style"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Refers to the varient of the font.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
224
        [SvgAttribute("font-variant", true)]
Eric Domke's avatar
Eric Domke committed
225
226
        public virtual SvgFontVariant FontVariant
        {
HeinrichAD's avatar
HeinrichAD committed
227
            get { return (SvgFontVariant)(this.Attributes["font-variant"] ?? SvgFontVariant.Inherit); }
Eric Domke's avatar
Eric Domke committed
228
229
230
231
232
233
            set { this.Attributes["font-variant"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Refers to the boldness of the font.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
234
        [SvgAttribute("text-decoration", true)]
Eric Domke's avatar
Eric Domke committed
235
236
        public virtual SvgTextDecoration TextDecoration
        {
HeinrichAD's avatar
HeinrichAD committed
237
            get { return (SvgTextDecoration)(this.Attributes["text-decoration"] ?? SvgTextDecoration.Inherit); }
Eric Domke's avatar
Eric Domke committed
238
239
240
241
242
243
            set { this.Attributes["text-decoration"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Refers to the boldness of the font.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
244
        [SvgAttribute("font-weight", true)]
Eric Domke's avatar
Eric Domke committed
245
246
        public virtual SvgFontWeight FontWeight
        {
HeinrichAD's avatar
HeinrichAD committed
247
            get { return (SvgFontWeight)(this.Attributes["font-weight"] ?? SvgFontWeight.Inherit); }
Eric Domke's avatar
Eric Domke committed
248
249
250
            set { this.Attributes["font-weight"] = value; this.IsPathDirty = true; }
        }

251
252
253
254
255
256
257
258
259
260
        /// <summary>
        /// Refers to the text transformation.
        /// </summary>
        [SvgAttribute("text-transform", true)]
        public virtual SvgTextTransformation TextTransformation
        {
            get { return (SvgTextTransformation)(this.Attributes["text-transform"] ?? SvgTextTransformation.Inherit); }
            set { this.Attributes["text-transform"] = value; this.IsPathDirty = true; }
        }

Eric Domke's avatar
Eric Domke committed
261
262
263
264
265
266
267
268
269
270
271
272
273
        private enum FontParseState
        {
            fontStyle,
            fontVariant,
            fontWeight,
            fontSize,
            fontFamilyNext,
            fontFamilyCurr
        }

        /// <summary>
        /// Set all font information.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
274
        [SvgAttribute("font", true)]
Eric Domke's avatar
Eric Domke committed
275
276
        public virtual string Font
        {
HeinrichAD's avatar
HeinrichAD committed
277
            get { return ((this.Attributes["font"] ?? string.Empty) as string); }
Eric Domke's avatar
Eric Domke committed
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
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
            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));
                            i = int.MaxValue - 2;
                            break;
                        case FontParseState.fontFamilyCurr:
                            this.FontFamily = string.Join(" ", parts, i, parts.Length - (i));
                            i = int.MaxValue - 2;
                            break;
                    }

                }

                this.Attributes["font"] = value;
                this.IsPathDirty = true;
            }
        }
Hayk's avatar
Hayk committed
351

Eric Domke's avatar
Eric Domke committed
352
353
354
355
356
357
358
359
360
        /// <summary>
        /// Get the font information based on data stored with the text object or inherited from the parent.
        /// </summary>
        /// <returns></returns>
        internal IFontDefn GetFont(ISvgRenderer renderer)
        {
            // Get the font-size
            float fontSize;
            var fontSizeUnit = this.FontSize;
361
            if (fontSizeUnit == SvgUnit.None || fontSizeUnit == SvgUnit.Empty)
Eric Domke's avatar
Eric Domke committed
362
363
364
365
366
367
368
369
370
371
372
373
374
            {
                fontSize = 1.0f;
            }
            else
            {
                fontSize = fontSizeUnit.ToDeviceValue(renderer, UnitRenderingType.Vertical, this);
            }

            var family = ValidateFontFamily(this.FontFamily, this.OwnerDocument);
            var sFaces = family as IEnumerable<SvgFontFace>;

            if (sFaces == null)
            {
375
                var fontStyle = SystemFontStyle.Regular;
Eric Domke's avatar
Eric Domke committed
376
377
378
379

                // Get the font-weight
                switch (this.FontWeight)
                {
380
381
382
383
384
385
                    //Note: Bold is not listed because it is = W700.
                    case SvgFontWeight.Bolder:
                    case SvgFontWeight.W600:
                    case SvgFontWeight.W700:
                    case SvgFontWeight.W800:
                    case SvgFontWeight.W900:
386
                        fontStyle |= SystemFontStyle.Bold;
Eric Domke's avatar
Eric Domke committed
387
388
389
390
391
392
                        break;
                }

                // Get the font-style
                switch (this.FontStyle)
                {
393
394
                    case SvgFontStyle.Italic:
                    case SvgFontStyle.Oblique:
395
                        fontStyle |= SystemFontStyle.Italic;
Eric Domke's avatar
Eric Domke committed
396
397
398
399
400
401
                        break;
                }

                // Get the text-decoration
                switch (this.TextDecoration)
                {
402
                    case SvgTextDecoration.LineThrough:
403
                        fontStyle |= SystemFontStyle.Strikeout;
Eric Domke's avatar
Eric Domke committed
404
                        break;
405
                    case SvgTextDecoration.Underline:
406
                        fontStyle |= SystemFontStyle.Underline;
Eric Domke's avatar
Eric Domke committed
407
408
409
410
411
412
413
414
415
416
                        break;
                }

                var ff = family as FontFamily;
                if (!ff.IsStyleAvailable(fontStyle))
                {
                    // Do Something
                }

                // Get the font-family
417
                return new GdiFontDefn(new Font(ff, fontSize, fontStyle, GraphicsUnit.Pixel));
Eric Domke's avatar
Eric Domke committed
418
419
420
421
422
423
424
425
426
427
428
429
430
            }
            else
            {
                var font = sFaces.First().Parent as SvgFont;
                if (font == null)
                {
                    var uri = sFaces.First().Descendants().OfType<SvgFontFaceUri>().First().ReferencedElement;
                    font = OwnerDocument.IdManager.GetElementById(uri) as SvgFont;
                }
                return new SvgFontDefn(font, fontSize, OwnerDocument.Ppi);
            }
        }

431
        public static PrivateFontCollection PrivateFonts = new PrivateFontCollection();
Eric Domke's avatar
Eric Domke committed
432
433
434
        public static object ValidateFontFamily(string fontFamilyList, SvgDocument doc)
        {
            // Split font family list on "," and then trim start and end spaces and quotes.
435
            var fontParts = (fontFamilyList ?? string.Empty).Split(new[] { ',' }).Select(fontName => fontName.Trim(new[] { '"', ' ', '\'' }));
436
            var families = SystemFontFamily.Families;
437
            Func<FontFamily, bool> getFamily;
Eric Domke's avatar
Eric Domke committed
438
439
440
441
442
443
444
            FontFamily family;
            IEnumerable<SvgFontFace> sFaces;

            // 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)
            {
Hayk's avatar
Hayk committed
445
                if (doc != null && doc.FontDefns().TryGetValue(f, out sFaces)) return sFaces;
Eric Domke's avatar
Eric Domke committed
446

447
448
449
450
                getFamily = new Func<FontFamily, bool>(ff => string.Equals(ff.Name, f, StringComparison.OrdinalIgnoreCase));
                family = families.FirstOrDefault(getFamily);
                if (family != null) return family;
                family = PrivateFonts.Families.FirstOrDefault(getFamily);
Eric Domke's avatar
Eric Domke committed
451
452
                if (family != null) return family;

453
                switch (f.ToLower())
Eric Domke's avatar
Eric Domke committed
454
455
                {
                    case "serif":
456
                        return SystemFontFamily.GenericSerif;
Eric Domke's avatar
Eric Domke committed
457
                    case "sans-serif":
458
                        return SystemFontFamily.GenericSansSerif;
Eric Domke's avatar
Eric Domke committed
459
                    case "monospace":
460
                        return SystemFontFamily.GenericMonospace;
Eric Domke's avatar
Eric Domke committed
461
462
463
464
                }
            }

            // No valid font family found from the list requested.
465
            return SystemFontFamily.GenericSansSerif;
Eric Domke's avatar
Eric Domke committed
466
467
468
        }
    }
}