SvgGlyph.cs 3.7 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Svg.Pathing;
using System.Drawing.Drawing2D;

namespace Svg
{
    [SvgElement("glyph")]
    public class SvgGlyph : SvgVisualElement
    {
        private GraphicsPath _path;

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

Eric Domke's avatar
Eric Domke committed
25
        [SvgAttribute("glyph-name", true)]
Eric Domke's avatar
Eric Domke committed
26
27
28
29
30
        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
31
        [SvgAttribute("horiz-adv-x", true)]
Eric Domke's avatar
Eric Domke committed
32
33
34
35
36
        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
37
        [SvgAttribute("unicode", true)]
Eric Domke's avatar
Eric Domke committed
38
39
40
41
42
        public string Unicode
        {
            get { return this.Attributes["unicode"] as string; }
            set { this.Attributes["unicode"] = value; }
        }
Eric Domke's avatar
Eric Domke committed
43
        [SvgAttribute("vert-adv-y", true)]
Eric Domke's avatar
Eric Domke committed
44
45
46
47
48
        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
49
        [SvgAttribute("vert-origin-x", true)]
Eric Domke's avatar
Eric Domke committed
50
51
52
53
54
        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
55
        [SvgAttribute("vert-origin-y", true)]
Eric Domke's avatar
Eric Domke committed
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
        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>
        /// Gets the bounds of the element.
        /// </summary>
Tebjan Halm's avatar
Tebjan Halm committed
85
86
        /// <value>The bounds.</value>
        public override System.Drawing.RectangleF Bounds
Eric Domke's avatar
Eric Domke committed
87
        {
Tebjan Halm's avatar
Tebjan Halm committed
88
            get { return this.Path(null).GetBounds(); }
Eric Domke's avatar
Eric Domke committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        }

        /// <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;

        }
    }
}