-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathLabel.cs
More file actions
473 lines (431 loc) · 15.3 KB
/
Label.cs
File metadata and controls
473 lines (431 loc) · 15.3 KB
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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using GeoAPI.Geometries;
using SharpMap.Styles;
namespace SharpMap.Rendering
{
/// <summary>
/// Defines an axis-aligned box around a label, used for collision detection
/// </summary>
public class LabelBox : IComparable<LabelBox>
{
private float _height;
private float _left;
private float _top;
private float _width;
/// <summary>
/// Initializes a new LabelBox instance
/// </summary>
/// <param name="left">Left side of box</param>
/// <param name="top">Top of box</param>
/// <param name="width">Width of the box</param>
/// <param name="height">Height of the box</param>
public LabelBox(float left, float top, float width, float height)
{
_left = left;
_top = top;
_width = width;
_height = height;
}
/// <summary>
/// Initializes a new LabelBox instance based on a rectangle
/// </summary>
/// <param name="rectangle"></param>
public LabelBox(RectangleF rectangle)
{
_left = rectangle.X;
_top = rectangle.Y;
_width = rectangle.Width;
_height = rectangle.Height;
}
/// <summary>
/// The Left tie-point for the Label
/// </summary>
public float Left
{
get { return _left; }
set { _left = value; }
}
/// <summary>
/// The Top tie-point for the label
/// </summary>
public float Top
{
get { return _top; }
set { _top = value; }
}
/// <summary>
/// Width of the box
/// </summary>
public float Width
{
get { return _width; }
set { _width = value; }
}
/// <summary>
/// Height of the box
/// </summary>
public float Height
{
get { return _height; }
set { _height = value; }
}
/// <summary>
/// Right side of the box
/// </summary>
public float Right
{
get { return _left + _width; }
}
/// <summary>
/// Bottom of the box
/// </summary>
public float Bottom
{
get { return _top - _height; }
}
#region IComparable<LabelBox> Members
/// <summary>
/// Returns 0 if the boxes intersects each other
/// </summary>
/// <param name="other">labelbox to perform intersectiontest with</param>
/// <returns>0 if the intersect</returns>
public int CompareTo(LabelBox other)
{
if (Intersects(other))
return 0;
else if (other.Left > Right ||
other.Bottom > Top)
return 1;
else
return -1;
}
#endregion
/// <summary>
/// Determines whether the boundingbox intersects another boundingbox
/// </summary>
/// <param name="box"></param>
/// <returns></returns>
public bool Intersects(LabelBox box)
{
return !(box.Left > Right ||
box.Right < Left ||
box.Bottom > Top ||
box.Top < Bottom);
}
}
/// <summary>
/// Class for storing a label instance
/// </summary>
public abstract class BaseLabel : IComparable<BaseLabel>, IComparer<BaseLabel>
{
private LabelBox _box;
private Font _Font;
//private PointF _LabelPoint;
private int _Priority;
private float _Rotation;
private bool _show;
private LabelStyle _Style;
private string _Text;
[Obsolete]
private TextOnPath _textOnPath;
/// <summary>
/// Render text on path
/// </summary>
[Obsolete("Paths are now labelled based upon LengthIndexedLine")]
public TextOnPath TextOnPathLabel
{
get { return _textOnPath; }
set { _textOnPath = value; }
}
/// <summary>
/// Initializes a new Label instance
/// </summary>
/// <param name="text">Text to write</param>
/// <param name="rotation">Rotation</param>
/// <param name="priority">Label priority used for collision detection</param>
/// <param name="collisionbox">Box around label for collision detection</param>
/// <param name="style">The style of the label</param>
protected BaseLabel(string text, float rotation, int priority, LabelBox collisionbox,
LabelStyle style)
{
_Text = text;
//_LabelPoint = labelpoint;
_Rotation = rotation;
_Priority = priority;
_box = collisionbox;
_Style = style;
_show = true;
}
/// <summary>
/// Initializes a new Label instance
/// </summary>
/// <param name="text"></param>
/// <param name="rotation"></param>
/// <param name="priority"></param>
/// <param name="style"></param>
protected BaseLabel(string text, float rotation, int priority,
LabelStyle style)
{
_Text = text;
//_LabelPoint = labelpoint;
_Rotation = rotation;
_Priority = priority;
_Style = style;
_show = true;
}
/// <summary>
/// Show this label or don't
/// </summary>
public bool Show
{
get { return _show; }
set { _show = value; }
}
/// <summary>
/// The text of the label
/// </summary>
public string Text
{
get { return _Text; }
set { _Text = value; }
}
/// <summary>
/// Label font
/// </summary>
public Font Font
{
get { return _Font; }
set { _Font = value; }
}
/// <summary>
/// Label rotation
/// </summary>
public float Rotation
{
get { return _Rotation; }
set { _Rotation = value; }
}
/// <summary>
/// Value indicating rendering priority
/// </summary>
public int Priority
{
get { return _Priority; }
set { _Priority = value; }
}
/// <summary>
/// Label box
/// </summary>
public LabelBox Box
{
get { return _box; }
set { _box = value; }
}
/// <summary>
/// Gets or sets the <see cref="SharpMap.Styles.LabelStyle"/> of this label
/// </summary>
public LabelStyle Style
{
get { return _Style; }
set { _Style = value; }
}
#region IComparable<Label> Members
/// <summary>
/// Tests if two label boxes intersects
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public virtual int CompareTo(BaseLabel other)
{
// if (this.TextOnPathLabel != null)
// {
// return CompareToTextOnPath(other);
// }
if (this == other)
return 0;
if (_box == null)
return -1;
if (other.Box == null)
return 1;
return _box.CompareTo(other.Box);
}
// private int CompareToTextOnPath(BaseLabel other)
// {
// if (this == other)
// return 0;
// if (TextOnPathLabel == null)
// return -1;
// if (other.TextOnPathLabel == null)
// return 1;
//
// for (int i = 0; i < TextOnPathLabel.RegionList.Count; i++)
// {
// for (int j = 0; j < other.TextOnPathLabel.RegionList.Count; j++)
// {
// if (TextOnPathLabel.RegionList[i].IntersectsWith(other.TextOnPathLabel.RegionList[j]))
// return 0;
// }
// }
// if (_box == null)
// return -1;
// if (other.Box == null)
// return 1;
// if (other.Box.Left > this.Box.Right ||
// other.Box.Bottom > this.Box.Top)
// return 1;
// else
// return -1;
// }
#endregion
#region IComparer<BaseLabel> Members
/// <summary>
/// Checks if two labels intersect
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(BaseLabel x, BaseLabel y)
{
return x?.CompareTo(y) ?? -1;
}
#endregion
}
/// <summary>
/// Type specific base label class
/// </summary>
/// <typeparam name="T">The type of the location</typeparam>
public abstract class BaseLabel<T> : BaseLabel
{
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="text">The label text</param>
/// <param name="location">The position of label</param>
/// <param name="rotation">The rotation of the label (in degrees)</param>
/// <param name="priority">A priority value. Labels with lower priority are less likely to be rendered</param>
/// <param name="collisionbox">A bounding box for collision detection</param>
/// <param name="style">The label style to apply upon rendering</param>
protected BaseLabel(string text, T location, float rotation, int priority, LabelBox collisionbox, LabelStyle style)
: base(text, rotation, priority, collisionbox, style)
{
//if (typeof(T) is ValueType)
if (location==null)
return;
if (!(location is PointF || location is GraphicsPath))
throw new ArgumentException("Invalid location type", "location");
Location = location;
}
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="text">The label text</param>
/// <param name="location">The position of label</param>
/// <param name="rotation">The rotation of the label (in degrees)</param>
/// <param name="priority">A priority value. Labels with lower priority are less likely to be rendered</param>
/// <param name="style">The label style to apply upon rendering</param>
protected BaseLabel(string text, T location, float rotation, int priority, LabelStyle style)
: base(text, rotation, priority, style)
{
if (location == null)
return;
if (!(location is PointF || location is GraphicsPath))
throw new ArgumentException("Invalid location type", "location");
Location = location;
}
/// <summary>
/// Gets or sets the location of the label
/// </summary>
public T Location { get; set; }
}
/// <summary>
/// A label that is to be rendered on a <see cref="GraphicsPath"/>
/// </summary>
public class PathLabel : BaseLabel<GraphicsPath>
{
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="text">The label text</param>
/// <param name="location">The position of label</param>
/// <param name="rotation">The rotation of the label (in degrees)</param>
/// <param name="priority">A priority value. Labels with lower priority are less likely to be rendered</param>
/// <param name="collisionbox">A bounding box used for collision detection</param>
/// <param name="style">The label style to apply upon rendering</param>
public PathLabel(string text, GraphicsPath location, float rotation, int priority, LabelBox collisionbox, LabelStyle style)
: base(text, location, rotation, priority, collisionbox, style)
{
}
/// <summary>
/// Bounding polygon in world coordinates
/// </summary>
public IPolygon AffectedArea { get; set; }
/// <inheritdoc cref="BaseLabel.CompareTo"/>
public override int CompareTo(BaseLabel other)
{
var lbThis = new LabelBox(Location.GetBounds());
var lbOther = other is PathLabel otherPl
? new LabelBox(otherPl.Location.GetBounds())
: other.Box;
return lbThis.CompareTo(lbOther);
}
}
/// <summary>
/// A label that is to be rendered at or around a <see cref="PointF"/>
/// </summary>
public class Label : BaseLabel<PointF>
{
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="text">The label text</param>
/// <param name="location">The position of label</param>
/// <param name="rotation">The rotation of the label (in degrees)</param>
/// <param name="priority">A priority value. Labels with lower priority are less likely to be rendered</param>
/// <param name="collisionbox">A bounding box used for collision detection</param>
/// <param name="style">The label style to apply upon rendering</param>
public Label(string text, PointF location, float rotation, int priority, LabelBox collisionbox, LabelStyle style)
: base(text, location, rotation, priority, collisionbox, style)
{
LabelPoint = location;
}
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="text">The label text</param>
/// <param name="location">The position of label</param>
/// <param name="rotation">The rotation of the label (in degrees)</param>
/// <param name="priority">A priority value. Labels with lower priority are less likely to be rendered</param>
/// <param name="style">The label style to apply upon rendering</param>
public Label(string text, PointF location, float rotation, int priority, LabelStyle style)
: base(text, location, rotation, priority, style)
{
LabelPoint = location;
}
/// <summary>
/// Label position
/// </summary>
public PointF LabelPoint
{
get;
set;
}
}
}