SvgText.cs 10.8 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace Svg
{
    /// <summary>
    /// The <see cref="SvgText"/> element defines a graphics element consisting of text.
    /// </summary>
14
    [SvgElement("text")]
15
    public class SvgText : SvgVisualElement
davescriven's avatar
davescriven committed
16
17
18
19
20
21
    {
        private SvgUnit _x;
        private SvgUnit _y;
        private SvgUnit _letterSpacing;
        private SvgUnit _wordSpacing;
        private SvgUnit _fontSize;
Tebjan Halm's avatar
Tebjan Halm committed
22
23
        private string _font;
        private string _fontFamily;
davescriven's avatar
davescriven committed
24
25
        private GraphicsPath _path;
        private SvgTextAnchor _textAnchor = SvgTextAnchor.Start;
26
        private static readonly SvgRenderer _stringMeasure;
davescriven's avatar
davescriven committed
27
28
29
30
31
32
33

        /// <summary>
        /// Initializes the <see cref="SvgText"/> class.
        /// </summary>
        static SvgText()
        {
            Bitmap bitmap = new Bitmap(1, 1);
34
            _stringMeasure = SvgRenderer.FromImage(bitmap);
35
            _stringMeasure.TextRenderingHint = TextRenderingHint.AntiAlias;
davescriven's avatar
davescriven committed
36
37
38
39
40
41
42
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgText"/> class.
        /// </summary>
        public SvgText()
        {
Tebjan Halm's avatar
Tebjan Halm committed
43
            this._fontFamily = "Times New Roman";
davescriven's avatar
davescriven committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
            this._fontSize = new SvgUnit(0.0f);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgText"/> class.
        /// </summary>
        /// <param name="text">The text.</param>
        public SvgText(string text) : this()
        {
            this.Text = text;
        }

        /// <summary>
        /// Gets or sets the text to be rendered.
        /// </summary>
        public virtual string Text
        {
            get { return base.Content; }
Tebjan Halm's avatar
Tebjan Halm committed
62
            set { base.Content = value; this.IsPathDirty = true; this.Content = value; }
davescriven's avatar
davescriven committed
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        }

        /// <summary>
        /// Gets or sets the text anchor.
        /// </summary>
        /// <value>The text anchor.</value>
        [SvgAttribute("text-anchor")]
        public virtual SvgTextAnchor TextAnchor
        {
            get { return this._textAnchor; }
            set { this._textAnchor = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Gets or sets the X.
        /// </summary>
        /// <value>The X.</value>
        [SvgAttribute("x")]
        public virtual SvgUnit X
        {
            get { return this._x; }
            set { this._x = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Gets or sets the Y.
        /// </summary>
        /// <value>The Y.</value>
        [SvgAttribute("y")]
        public virtual SvgUnit Y
        {
            get { return this._y; }
            set { this._y = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Specifies spacing behavior between text characters.
        /// </summary>
        [SvgAttribute("letter-spacing")]
        public virtual SvgUnit LetterSpacing
        {
            get { return this._letterSpacing; }
            set { this._letterSpacing = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Specifies spacing behavior between words.
        /// </summary>
        [SvgAttribute("word-spacing")]
        public virtual SvgUnit WordSpacing
        {
            get { return this._wordSpacing; }
            set { this._wordSpacing = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Indicates which font family is to be used to render the text.
        /// </summary>
        [SvgAttribute("font-family")]
Tebjan Halm's avatar
Tebjan Halm committed
122
        public virtual string FontFamily
davescriven's avatar
davescriven committed
123
        {
Tebjan Halm's avatar
Tebjan Halm committed
124
125
            get { return this._fontFamily; }
            set { this._fontFamily = value; this.IsPathDirty = true; }
davescriven's avatar
davescriven committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        }

        /// <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._fontSize; }
            set { this._fontSize = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Set all font information.
        /// </summary>
        [SvgAttribute("font")]
Tebjan Halm's avatar
Tebjan Halm committed
142
        public virtual string Font
davescriven's avatar
davescriven committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
        {
            get { return this._font; }
            set { this._font = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Gets or sets the fill.
        /// </summary>
        /// <remarks>
        /// <para>Unlike other <see cref="SvgGraphicsElement"/>s, <see cref="SvgText"/> has a default fill of black rather than transparent.</para>
        /// </remarks>
        /// <value>The fill.</value>
        public override SvgPaintServer Fill
        {
            get { return (this.Attributes["Fill"] == null) ? new SvgColourServer(Color.Black) : (SvgPaintServer)this.Attributes["Fill"]; }
            set { this.Attributes["Fill"] = value; }
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.Text;
        }

        /// <summary>
        /// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered.
        /// </summary>
        /// <value></value>
        protected override bool RequiresSmoothRendering
        {
            get { return true; }
        }

        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
        public override System.Drawing.RectangleF Bounds
        {
            get { return this.Path.GetBounds(); }
        }

Tebjan Halm's avatar
Tebjan Halm committed
190
        static private RectangleF MeasureString(SvgRenderer renderer, string text, Font font)
191
192
193
        {
            GraphicsPath p = new GraphicsPath();
            p.AddString(text, font.FontFamily, 0, font.Size, new PointF(0.0f, 0.0f), StringFormat.GenericTypographic);
194
            p.Transform(renderer.Transform);
Tebjan Halm's avatar
Tebjan Halm committed
195
            return p.GetBounds();
196
197
        }

davescriven's avatar
davescriven committed
198
199
200
201
202
203
204
205
206
207
208
209
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
        public override System.Drawing.Drawing2D.GraphicsPath Path
        {
            get
            {
                // Make sure the path is always null if there is no text
                if (_path == null || this.IsPathDirty && !string.IsNullOrEmpty(this.Text))
                {
                    float fontSize = this.FontSize.ToDeviceValue(this);
210
211
212
213
                    if (fontSize == 0.0f)
                    {
                        fontSize = 1.0f;
                    }
Tebjan Halm's avatar
Tebjan Halm committed
214
                    RectangleF stringBounds;
davescriven's avatar
davescriven committed
215
                    PointF location = PointF.Empty;
Tebjan Halm's avatar
Tebjan Halm committed
216
                    Font font = new Font(this._fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
davescriven's avatar
davescriven committed
217
218
219
220
221

                    // Minus FontSize because the x/y coords mark the bottom left, not bottom top.
                    switch (this.TextAnchor)
                    {
                        case SvgTextAnchor.Start:
Tebjan Halm's avatar
Tebjan Halm committed
222
                            location = new PointF(this.X.ToDeviceValue(this), this.Y.ToDeviceValue(this, true) - this._fontSize);
davescriven's avatar
davescriven committed
223
224
                            break;
                        case SvgTextAnchor.Middle:
Tebjan Halm's avatar
Tebjan Halm committed
225
226
                            stringBounds = SvgText.MeasureString(_stringMeasure, this.Text, font);
                            location = new PointF(this.X.ToDeviceValue(this) - (stringBounds.Width / 2), this.Y.ToDeviceValue(this, true) - this._fontSize);
davescriven's avatar
davescriven committed
227
228
                            break;
                        case SvgTextAnchor.End:
Tebjan Halm's avatar
Tebjan Halm committed
229
230
                            stringBounds = SvgText.MeasureString(_stringMeasure, this.Text, font);
                            location = new PointF(this.X.ToDeviceValue(this) - stringBounds.Width, this.Y.ToDeviceValue(this, true) - this._fontSize);
davescriven's avatar
davescriven committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
                            break;
                    }

                    _path = new GraphicsPath();
                    _path.StartFigure();

                    // No way to do letter-spacing or word-spacing, so do manually
                    if (this.LetterSpacing.Value > 0.0f || this.WordSpacing.Value > 0.0f)
                    {
                        // Cut up into words, or just leave as required
                        string[] words = (this.WordSpacing.Value > 0.0f) ? this.Text.Split(' ') : new string[] { this.Text };
                        float wordSpacing = this.WordSpacing.ToDeviceValue(this);
                        float letterSpacing = this.LetterSpacing.ToDeviceValue(this);
                        float start = this.X.ToDeviceValue(this);

                        foreach (string word in words)
                        {
                            // Only do if there is line spacing, just write the word otherwise
                            if (this.LetterSpacing.Value > 0.0f)
                            {
                                char[] characters = word.ToCharArray();
                                foreach (char currentCharacter in characters)
                                {
Tebjan Halm's avatar
Tebjan Halm committed
254
                                    _path.AddString(currentCharacter.ToString(), new FontFamily(this._fontFamily), 0, fontSize, location, StringFormat.GenericTypographic);
davescriven's avatar
davescriven committed
255
256
257
258
                                    location = new PointF(_path.GetBounds().Width + start + letterSpacing, location.Y);
                                }
                            }
                            else
259
                            {
Tebjan Halm's avatar
Tebjan Halm committed
260
                                _path.AddString(word, new FontFamily(this._fontFamily), 0, fontSize, location, StringFormat.GenericTypographic);
261
                            }
davescriven's avatar
davescriven committed
262
263
264
265
266
267
268

                            // Move the location of the word to be written along
                            location = new PointF(_path.GetBounds().Width + start + wordSpacing, location.Y);
                        }
                    }
                    else
                    {
269
270
                        if (!string.IsNullOrEmpty(this.Text))
                        {
Tebjan Halm's avatar
Tebjan Halm committed
271
                            _path.AddString(this.Text, new FontFamily(this._fontFamily), 0, fontSize, location, StringFormat.GenericTypographic);
272
                        }
davescriven's avatar
davescriven committed
273
274
275
276
277
278
279
280
281
282
                    }

                    _path.CloseFigure();
                    this.IsPathDirty = false;
                }
                return _path;
            }
        }
    }
}