forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabelStyle.cs
More file actions
373 lines (336 loc) · 11.9 KB
/
LabelStyle.cs
File metadata and controls
373 lines (336 loc) · 11.9 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
// 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.Drawing;
using System.Drawing.Drawing2D;
using System.Net.Mime;
using System.Runtime.CompilerServices;
using Common.Logging;
using SharpMap.Rendering.Thematics;
namespace SharpMap.Styles
{
/// <summary>
/// Defines a style used for rendering labels
/// </summary>
[Serializable]
public class LabelStyle : Style, ICloneable
{
private static readonly ILog _logger = LogManager.GetLogger(typeof (LabelStyle));
#region HorizontalAlignmentEnum enum
/// <summary>
/// Label text alignment
/// </summary>
public enum HorizontalAlignmentEnum : short
{
/// <summary>
/// Left oriented
/// </summary>
Left = 0,
/// <summary>
/// Right oriented
/// </summary>
Right = 2,
/// <summary>
/// Centered
/// </summary>
Center = 1
}
#endregion
#region VerticalAlignmentEnum enum
/// <summary>
/// Label text alignment
/// </summary>
public enum VerticalAlignmentEnum : short
{
/// <summary>
/// Left oriented
/// </summary>
Bottom = 0,
/// <summary>
/// Right oriented
/// </summary>
Top = 2,
/// <summary>
/// Centered
/// </summary>
Middle = 1
}
#endregion
private Brush _BackColor;
private SizeF _CollisionBuffer;
private bool _CollisionDetection;
private Font _Font;
private Color _ForeColor;
private Pen _Halo;
private HorizontalAlignmentEnum _HorizontalAlignment;
private PointF _Offset;
private VerticalAlignmentEnum _VerticalAlignment;
private float _rotation;
private bool _ignoreLength;
private bool _isTextOnPath = false;
/// <summary>
/// get or set label on path
/// </summary>
[Obsolete("TextOnPath has been deprecated in favor of more efficient rendering techniques")]
public bool IsTextOnPath
{
get { return _isTextOnPath; }
set { _isTextOnPath = value; }
}
/// <summary>
/// Initializes a new LabelStyle
/// </summary>
public LabelStyle()
{
_Font = new Font(FontFamily.GenericSerif, 12f);
_Offset = new PointF(0, 0);
_CollisionDetection = false;
_CollisionBuffer = new Size(0, 0);
_ForeColor = Color.Black;
_HorizontalAlignment = HorizontalAlignmentEnum.Center;
_VerticalAlignment = VerticalAlignmentEnum.Middle;
}
/// <summary>
/// Releases managed resources
/// </summary>
protected override void ReleaseManagedResources()
{
if (IsDisposed)
return;
if (_Font != null) _Font.Dispose();
if (_Halo != null) _Halo.Dispose();
if (_BackColor != null) _BackColor.Dispose();
base.ReleaseManagedResources();
}
/// <summary>
/// Method to create a deep copy of this <see cref="LabelStyle"/>
/// </summary>
/// <returns>A LabelStyle resembling this instance.</returns>
public LabelStyle Clone()
{
var res = (LabelStyle) MemberwiseClone();
lock (this)
{
if (_Font != null)
res.Font = (Font)_Font.Clone();
if (_Halo != null)
res._Halo = (Pen)_Halo.Clone();
if (_BackColor != null)
res._BackColor = (Brush) _BackColor.Clone();
}
return res;
}
object ICloneable.Clone()
{
return Clone();
}
/// <summary>
/// Function to get a <see cref="StringFormat"/> with <see cref="StringFormat.Alignment"/> and <see cref="StringFormat.LineAlignment"/> properties according to
/// <see cref="HorizontalAlignment"/> and <see cref="VerticalAlignment"/>
/// </summary>
/// <returns>A <see cref="StringFormat"/></returns>
internal StringFormat GetStringFormat()
{
var r = (StringFormat)StringFormat.GenericTypographic.Clone();
switch (HorizontalAlignment)
{
case HorizontalAlignmentEnum.Center:
r.Alignment = StringAlignment.Center;
break;
case HorizontalAlignmentEnum.Left:
r.Alignment = StringAlignment.Near;
break;
case HorizontalAlignmentEnum.Right:
r.Alignment = StringAlignment.Far;
break;
}
switch (VerticalAlignment)
{
case VerticalAlignmentEnum.Middle:
r.LineAlignment = StringAlignment.Center;
break;
case VerticalAlignmentEnum.Top:
r.LineAlignment = StringAlignment.Near;
break;
case VerticalAlignmentEnum.Bottom:
r.LineAlignment = StringAlignment.Far;
break;
}
return r;
}
/// <summary>
/// Label Font
/// </summary>
public Font Font
{
get
{
return _Font;
}
set
{
if (value.Unit != GraphicsUnit.Point)
_logger.Error( fmh => fmh("Only assign fonts with size in Points"));
_Font = value;
// we can't dispose the previous font
// because it could still be used by
// the rendering engine, so we just let it be GC'ed.
_cachedFontForGraphics = null;
}
}
[NonSerialized]
private float _cachedDpiY = 0f;
[NonSerialized]
private Font _cachedFontForGraphics = null;
/// <summary>
/// Method to create a font that ca
/// </summary>
/// <param name="g"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public Font GetFontForGraphics(Graphics g)
{
if (_cachedFontForGraphics == null || g.DpiY != _cachedDpiY)
{
_cachedDpiY = g.DpiY;
if (_cachedFontForGraphics != null) _cachedFontForGraphics.Dispose();
_cachedFontForGraphics = new Font(_Font.FontFamily, _Font.GetHeight(g), _Font.Style, GraphicsUnit.Pixel);
/*
var textHeight = _Font.Size;
switch (_Font.Unit)
{
case GraphicsUnit.Display:
textHeight *= g.DpiY / 75;
break;
case GraphicsUnit.Document:
textHeight *= g.DpiY / 300;
break;
case GraphicsUnit.Inch:
textHeight *= g.DpiY;
break;
case GraphicsUnit.Millimeter:
textHeight /= 25.4f * g.DpiY;
break;
case GraphicsUnit.Pixel:
//do nothing
break;
case GraphicsUnit.Point:
textHeight *= g.DpiY / 72;
break;
}
_cachedFontForGraphics = new Font(_Font.FontFamily, textHeight, _Font.Style, GraphicsUnit.Pixel);
*/
}
return _cachedFontForGraphics;
}
/// <summary>
/// Font color
/// </summary>
public Color ForeColor
{
get { return _ForeColor; }
set { _ForeColor = value; }
}
/// <summary>
/// The background color of the label. Set to transparent brush or null if background isn't needed
/// </summary>
public Brush BackColor
{
get { return _BackColor; }
set { _BackColor = value; }
}
/// <summary>
/// Creates a halo around the text
/// </summary>
[System.ComponentModel.Category("Uneditable")]
[System.ComponentModel.Editor()]
public Pen Halo
{
get { return _Halo; }
set
{
_Halo = value;
//if (_Halo != null)
// _Halo.LineJoin = LineJoin.Round;
}
}
/// <summary>
/// Specifies relative position of labels with respect to objects label point
/// </summary>
[System.ComponentModel.Category("Uneditable")]
public PointF Offset
{
get { return _Offset; }
set { _Offset = value; }
}
/// <summary>
/// Gets or sets whether Collision Detection is enabled for the labels.
/// If set to true, label collision will be tested.
/// </summary>
/// <remarks>Just setting this property in a <see cref="ITheme.GetStyle"/> method does not lead to the desired result. You must set it to for the whole layer using the default Style.</remarks>
[System.ComponentModel.Category("Collision Detection")]
public bool CollisionDetection
{
get { return _CollisionDetection; }
set { _CollisionDetection = value; }
}
/// <summary>
/// Distance around label where collision buffer is active
/// </summary>
[System.ComponentModel.Category("Collision Detection")]
public SizeF CollisionBuffer
{
get { return _CollisionBuffer; }
set { _CollisionBuffer = value; }
}
/// <summary>
/// The horizontal alignment of the text in relation to the labelpoint
/// </summary>
[System.ComponentModel.Category("Alignment")]
public HorizontalAlignmentEnum HorizontalAlignment
{
get { return _HorizontalAlignment; }
set { _HorizontalAlignment = value; }
}
/// <summary>
/// The horizontal alignment of the text in relation to the labelpoint
/// </summary>
[System.ComponentModel.Category("Alignment")]
public VerticalAlignmentEnum VerticalAlignment
{
get { return _VerticalAlignment; }
set { _VerticalAlignment = value; }
}
/// <summary>
/// The Rotation of the text
/// </summary>
[System.ComponentModel.Category("Alignment")]
public float Rotation
{
get { return _rotation; }
set { _rotation = value % 360f; }
}
/// <summary>
/// Gets or sets if length of linestring should be ignored
/// </summary>
[System.ComponentModel.Category("Alignment")]
public bool IgnoreLength
{
get { return _ignoreLength; }
set { _ignoreLength = value; }
}
}
}