SvgGlyph.cs 3.38 KB
Newer Older
1
using System.Linq;
Eric Domke's avatar
Eric Domke committed
2
3
4
5
6
7
using Svg.Pathing;
using System.Drawing.Drawing2D;

namespace Svg
{
    [SvgElement("glyph")]
8
    public class SvgGlyph : SvgPathBasedElement
Eric Domke's avatar
Eric Domke committed
9
10
11
12
13
14
    {
        private GraphicsPath _path;

        /// <summary>
        /// Gets or sets a <see cref="SvgPathSegmentList"/> of path data.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
15
        [SvgAttribute("d", true)]
Eric Domke's avatar
Eric Domke committed
16
17
18
19
20
21
        public SvgPathSegmentList PathData
        {
            get { return this.Attributes.GetAttribute<SvgPathSegmentList>("d"); }
            set { this.Attributes["d"] = value; }
        }

Eric Domke's avatar
Eric Domke committed
22
        [SvgAttribute("glyph-name", true)]
Eric Domke's avatar
Eric Domke committed
23
24
25
26
27
        public virtual string GlyphName
        {
            get { return this.Attributes["glyph-name"] as string; }
            set { this.Attributes["glyph-name"] = value; }
        }
Eric Domke's avatar
Eric Domke committed
28
        [SvgAttribute("horiz-adv-x", true)]
Eric Domke's avatar
Eric Domke committed
29
30
31
32
33
        public float HorizAdvX
        {
            get { return (this.Attributes["horiz-adv-x"] == null ? this.Parents.OfType<SvgFont>().First().HorizAdvX : (float)this.Attributes["horiz-adv-x"]); }
            set { this.Attributes["horiz-adv-x"] = value; }
        }
Eric Domke's avatar
Eric Domke committed
34
        [SvgAttribute("unicode", true)]
Eric Domke's avatar
Eric Domke committed
35
36
37
38
39
        public string Unicode
        {
            get { return this.Attributes["unicode"] as string; }
            set { this.Attributes["unicode"] = value; }
        }
Eric Domke's avatar
Eric Domke committed
40
        [SvgAttribute("vert-adv-y", true)]
Eric Domke's avatar
Eric Domke committed
41
42
43
44
45
        public float VertAdvY
        {
            get { return (this.Attributes["vert-adv-y"] == null ? this.Parents.OfType<SvgFont>().First().VertAdvY : (float)this.Attributes["vert-adv-y"]); }
            set { this.Attributes["vert-adv-y"] = value; }
        }
Eric Domke's avatar
Eric Domke committed
46
        [SvgAttribute("vert-origin-x", true)]
Eric Domke's avatar
Eric Domke committed
47
48
49
50
51
        public float VertOriginX
        {
            get { return (this.Attributes["vert-origin-x"] == null ? this.Parents.OfType<SvgFont>().First().VertOriginX : (float)this.Attributes["vert-origin-x"]); }
            set { this.Attributes["vert-origin-x"] = value; }
        }
Eric Domke's avatar
Eric Domke committed
52
        [SvgAttribute("vert-origin-y", true)]
Eric Domke's avatar
Eric Domke committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        public float VertOriginY
        {
            get { return (this.Attributes["vert-origin-y"] == null ? this.Parents.OfType<SvgFont>().First().VertOriginY : (float)this.Attributes["vert-origin-y"]); }
            set { this.Attributes["vert-origin-y"] = value; }
        }


        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        public override GraphicsPath Path(ISvgRenderer renderer)
        {
            if (this._path == null || this.IsPathDirty)
            {
                _path = new GraphicsPath();

                foreach (SvgPathSegment segment in this.PathData)
                {
                    segment.AddToPath(_path);
                }

                this.IsPathDirty = false;
            }
            return _path;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgGlyph"/> class.
        /// </summary>
        public SvgGlyph()
        {
            var pathData = new SvgPathSegmentList();
            this.Attributes["d"] = pathData;
        }

        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgGlyph>();
        }

        public override SvgElement DeepCopy<T>()
        {
            var newObj = base.DeepCopy<T>() as SvgGlyph;
            foreach (var pathData in this.PathData)
                newObj.PathData.Add(pathData.Clone());
            return newObj;

        }
    }
}