aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GraphicObjects/TextRun.cs
blob: 6b6f7999240c5566a8c3f5cf7a57c9c145cacd6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
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
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Cairo;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.ComponentModel;

namespace Crow
{
	public class TextRun : GraphicObject
	{
		#region CTOR
		public TextRun ()
		{

		}
		public TextRun (string _text)
			: base ()
		{
			Text = _text;
		}
		#endregion

		//TODO:change protected to private

		#region private and protected fields
		protected string _text = "label";
		Alignment _textAlignment = Alignment.Left;
		bool horizontalStretch = false;
		bool verticalStretch = false;
		bool _multiline;
		bool wordWrap;
		protected Rectangle rText;
		protected float widthRatio = 1f;
		protected float heightRatio = 1f;
		protected FontExtents fe;
		protected TextExtents te;
		#endregion


		[XmlAttributeAttribute ()]
		[DefaultValue (Alignment.Left)]
		public Alignment TextAlignment {
			get { return _textAlignment; }
			set { _textAlignment = value; }
		}
		[XmlAttributeAttribute ()]
		[DefaultValue (false)]
		public virtual bool HorizontalStretch {
			get { return horizontalStretch; }
			set {
				if (horizontalStretch == value)
					return;
				horizontalStretch = value;
				RegisterForRedraw ();
				NotifyValueChanged ("HorizontalStretch", horizontalStretch);
			}
		}
		[XmlAttributeAttribute ()]
		[DefaultValue (false)]
		public virtual bool VerticalStretch {
			get { return verticalStretch; }
			set {
				if (verticalStretch == value)
					return;
				verticalStretch = value;
				RegisterForRedraw ();
				NotifyValueChanged ("VerticalStretch", verticalStretch);
			}
		}
		[XmlAttributeAttribute ()]
		[DefaultValue ("label")]
		public string Text {
			get {
				return lines == null ?
					_text : lines.Aggregate ((i, j) => i + Interface.LineBreak + j);
			}
			set {
				if (_text == value)
					return;

				RegisterForGraphicUpdate ();

				_text = value;

				if (string.IsNullOrEmpty (_text))
					_text = "";

				lines = getLines;
			}
		}
		[XmlAttributeAttribute ()]
		[DefaultValue (false)]
		public bool Multiline {
			get { return _multiline; }
			set {
				_multiline = value;
				RegisterForGraphicUpdate ();
			}
		}
		[XmlAttributeAttribute ()]
		[DefaultValue (false)]
		public bool WordWrap {
			get {
				return wordWrap;
			}
			set {
				if (wordWrap == value)
					return;
				wordWrap = value;
				RegisterForGraphicUpdate ();
			}
		}

		List<string> lines;
		List<string> getLines {
			get {
				return _multiline ?
					Regex.Split (_text, "\r\n|\r|\n").ToList () :
					new List<string> (new string [] { _text });
			}
		}

		#region GraphicObject overrides
		protected override int measureRawSize (LayoutingType lt)
		{
			if (lines == null)
				lines = getLines;

			using (ImageSurface img = new ImageSurface (Format.Argb32, 10, 10)) {
				using (Context gr = new Context (img)) {
					//Cairo.FontFace cf = gr.GetContextFontFace ();

					gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
					gr.SetFontSize (Font.Size);


					fe = gr.FontExtents;
					te = new TextExtents ();

					if (lt == LayoutingType.Height) {
						int lc = lines.Count;
						//ensure minimal height = text line height
						if (lc == 0)
							lc = 1;

						return (int)(fe.Height * lc) + Margin * 2;
					}

					foreach (string s in lines) {
						string l = s.Replace("\t", new String (' ', Interface.TabSize));
						TextExtents tmp = gr.TextExtents (l);
						if (tmp.XAdvance > te.XAdvance)
							te = tmp;
					}
					return (int)Math.Ceiling (te.XAdvance) + Margin * 2;
				}
			}
		}
		protected override void onDraw (Context gr)
		{
			base.onDraw (gr);

			gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
			gr.SetFontSize (Font.Size);
			gr.FontOptions = Interface.FontRenderingOptions;

			gr.Antialias = Antialias.Subpixel;

			rText = new Rectangle (new Size (
				measureRawSize (LayoutingType.Width), measureRawSize (LayoutingType.Height)));
			rText.Width -= 2 * Margin;
			rText.Height -= 2 * Margin;

			widthRatio = 1f;
			heightRatio = 1f;

			Rectangle cb = ClientRectangle;

			//ignore text alignment if size to content = true
			//or if text size is larger than client bounds
			if (Width < 0 || Height < 0 || rText.Width > cb.Width) {
				rText.X = cb.X;
				rText.Y = cb.Y;
			} else {
				if (horizontalStretch) {
					widthRatio = (float)cb.Width / rText.Width;
					if (!verticalStretch)
						heightRatio = widthRatio;
				}
				if (verticalStretch) {
					heightRatio = (float)cb.Height / rText.Height;
					if (!horizontalStretch)
						widthRatio = heightRatio;
				}

				rText.Width = (int)(widthRatio * cb.Width);
				rText.Height = (int)(heightRatio * cb.Height);

				switch (TextAlignment) {
				case Alignment.TopLeft:     //ok
					rText.X = cb.X;
					rText.Y = cb.Y;
					break;
				case Alignment.Top:   //ok						
					rText.Y = cb.Y;
					rText.X = cb.X + cb.Width / 2 - rText.Width / 2;
					break;
				case Alignment.TopRight:    //ok
					rText.Y = cb.Y;
					rText.X = cb.Right - rText.Width;
					break;
				case Alignment.Left://ok
					rText.X = cb.X;
					rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
					break;
				case Alignment.Right://ok
					rText.X = cb.X + cb.Width - rText.Width;
					rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
					break;
				case Alignment.Bottom://ok
					rText.X = cb.Width / 2 - rText.Width / 2;
					rText.Y = cb.Height - rText.Height;
					break;
				case Alignment.BottomLeft://ok
					rText.X = cb.X;
					rText.Y = cb.Bottom - rText.Height;
					break;
				case Alignment.BottomRight://ok
					rText.Y = cb.Bottom - rText.Height;
					rText.X = cb.Right - rText.Width;
					break;
				case Alignment.Center://ok
					rText.X = cb.X + cb.Width / 2 - rText.Width / 2;
					rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
					break;
				}
			}

			gr.FontMatrix = new Matrix (widthRatio * Font.Size, 0, 0, heightRatio * Font.Size, 0, 0);


			int curLineCount = 0;
			for (int i = 0; i < lines.Count; i++) {
				string l = lines [i].Replace ("\t", new String (' ', Interface.TabSize));
				List<string> wl = new List<string> ();
				int lineLength = (int)gr.TextExtents (l).XAdvance;

				if (wordWrap && lineLength > cb.Width) {
					string tmpLine = "";
					int curChar = 0;
					while (curChar < l.Length) {
						tmpLine += l [curChar];
						if ((int)gr.TextExtents (tmpLine).XAdvance > cb.Width) {
							tmpLine = tmpLine.Remove (tmpLine.Length - 1);
							wl.Add (tmpLine);
							tmpLine = "";
							continue;
						}
						curChar++;
					}
					wl.Add (tmpLine);
				} else
					wl.Add (l);

				foreach (string ll in wl) {
					lineLength = (int)gr.TextExtents (ll).XAdvance;


					if (string.IsNullOrWhiteSpace (ll)) {
						curLineCount++;
						continue;
					}

					Foreground.SetAsSource (gr);
					gr.MoveTo (rText.X, rText.Y + fe.Ascent + fe.Height * curLineCount);

					gr.ShowText (ll);
					gr.Fill ();

					curLineCount++;
				}
			}
		}
		#endregion
	}
}