2222using System . Reflection ;
2323using GeoAPI . Geometries ;
2424using SharpMap . Rendering . Symbolizer ;
25+ using SharpMap . Styles ;
2526using SharpMap . Utilities ;
2627using Point = GeoAPI . Geometries . Coordinate ;
2728using System . Runtime . CompilerServices ;
@@ -38,7 +39,7 @@ public static class VectorRenderer
3839
3940 static VectorRenderer ( )
4041 {
41- SizeOfString = SizeOfStringCeiling ;
42+ SizeOfString = SizeOfString74 ;
4243 }
4344
4445 private static readonly Bitmap Defaultsymbol =
@@ -271,7 +272,7 @@ private static PointF[] LimitValues(PointF[] vertices, float limit)
271272 /// </summary>
272273 public static SizeOfStringDelegate SizeOfString
273274 {
274- get { return _sizeOfString ; }
275+ get { return _sizeOfString ?? ( _sizeOfString = SizeOfString74 ) ; }
275276 set
276277 {
277278 if ( value != null )
@@ -291,6 +292,18 @@ public static SizeF SizeOfStringBase(Graphics g, string text, Font font)
291292 return g . MeasureString ( text , font ) ;
292293 }
293294
295+ /// <summary>
296+ /// Function to get the <see cref="SizeF"/> of a string when rendered with the given font.
297+ /// </summary>
298+ /// <param name="g"><see cref="Graphics"/> object</param>
299+ /// <param name="text">the text to render</param>
300+ /// <param name="font">the font to use</param>
301+ /// <returns>the size</returns>
302+ public static SizeF SizeOfString74 ( Graphics g , string text , Font font )
303+ {
304+ var s = g . MeasureString ( text , font ) ;
305+ return new SizeF ( s . Width * 0.74f + 1f , s . Height * 0.74f ) ;
306+ }
294307 /// <summary>
295308 /// Function to get the <see cref="SizeF"/> of a string when rendered with the given font.
296309 /// </summary>
@@ -305,7 +318,6 @@ public static SizeF SizeOfStringCeiling(Graphics g, string text, Font font)
305318 }
306319
307320
308-
309321 /// <summary>
310322 /// Renders a label to the map.
311323 /// </summary>
@@ -319,37 +331,76 @@ public static SizeF SizeOfStringCeiling(Graphics g, string text, Font font)
319331 /// <param name="rotation">Text rotation in degrees</param>
320332 /// <param name="text">Text to render</param>
321333 /// <param name="map">Map reference</param>
334+ /// <param name="alignment">Horizontal alignment for multi line labels. If not set <see cref="StringAlignment.Near"/> is used</param>
335+ /// <param name="rotationPoint">Point where the rotation should take place</param>
322336 [ MethodImpl ( MethodImplOptions . Synchronized ) ]
323337 public static void DrawLabel ( Graphics g , PointF labelPoint , PointF offset , Font font , Color forecolor ,
324- Brush backcolor , Pen halo , float rotation , string text , Map map )
338+ Brush backcolor , Pen halo , float rotation , string text , Map map ,
339+ LabelStyle . HorizontalAlignmentEnum alignment = LabelStyle . HorizontalAlignmentEnum . Left ,
340+ PointF ? rotationPoint = null )
341+
325342 {
326- SizeF fontSize = _sizeOfString ( g , text , font ) ; //Calculate the size of the text
343+ //Calculate the size of the text
344+ var labelSize = _sizeOfString ( g , text , font ) ;
345+
346+ //Add label offset
327347 labelPoint . X += offset . X ;
328- labelPoint . Y += offset . Y ; //add label offset
348+ labelPoint . Y += offset . Y ;
349+
350+ //Translate alignment to stringalignment
351+ StringAlignment salign ;
352+ switch ( alignment )
353+ {
354+ case LabelStyle . HorizontalAlignmentEnum . Left :
355+ salign = StringAlignment . Near ;
356+ break ;
357+ case LabelStyle . HorizontalAlignmentEnum . Center :
358+ salign = StringAlignment . Center ;
359+ break ;
360+ default :
361+ salign = StringAlignment . Far ;
362+ break ;
363+ }
364+
329365 if ( rotation != 0 && ! float . IsNaN ( rotation ) )
330366 {
331- g . TranslateTransform ( labelPoint . X , labelPoint . Y ) ;
367+ rotationPoint = rotationPoint ?? labelPoint ;
368+
369+ g . FillEllipse ( Brushes . LawnGreen , rotationPoint . Value . X - 1 , rotationPoint . Value . Y - 1 , 2 , 2 ) ;
370+
371+ var t = g . Transform . Clone ( ) ;
372+ g . TranslateTransform ( rotationPoint . Value . X , rotationPoint . Value . Y ) ;
332373 g . RotateTransform ( rotation ) ;
333- g . TranslateTransform ( - fontSize . Width / 2 , - fontSize . Height / 2 ) ;
374+ //g.TranslateTransform(-labelSize.Width/2, -labelSize.Height/2);
375+
376+ labelPoint = new PointF ( labelPoint . X - rotationPoint . Value . X ,
377+ labelPoint . Y - rotationPoint . Value . Y ) ;
378+
379+ //labelSize = new SizeF(labelSize.Width*0.74f + 1f, labelSize.Height*0.74f);
334380 if ( backcolor != null && backcolor != Brushes . Transparent )
335- g . FillRectangle ( backcolor , 0 , 0 , fontSize . Width * 0.74f + 1f , fontSize . Height * 0.74f ) ;
381+ g . FillRectangle ( backcolor , labelPoint . X , labelPoint . Y , labelSize . Width , labelSize . Height ) ;
382+
336383 var path = new GraphicsPath ( ) ;
337- path . AddString ( text , font . FontFamily , ( int ) font . Style , font . Size , new System . Drawing . Point ( 0 , 0 ) , null ) ;
384+ path . AddString ( text , font . FontFamily , ( int ) font . Style , font . Size ,
385+ new RectangleF ( labelPoint , labelSize ) /* labelPoint*/ ,
386+ new StringFormat { Alignment = salign } /*null*/ ) ;
338387 if ( halo != null )
339388 g . DrawPath ( halo , path ) ;
389+
340390 g . FillPath ( new SolidBrush ( forecolor ) , path ) ;
341391 //g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), 0, 0);
342- g . Transform = map . MapTransform ;
392+ g . Transform = t ;
343393 }
344394 else
345395 {
346396 if ( backcolor != null && backcolor != Brushes . Transparent )
347- g . FillRectangle ( backcolor , labelPoint . X , labelPoint . Y , fontSize . Width * 0.74f + 1 ,
348- fontSize . Height * 0.74f ) ;
397+ g . FillRectangle ( backcolor , labelPoint . X , labelPoint . Y , labelSize . Width ,
398+ labelSize . Height ) ;
349399
350400 var path = new GraphicsPath ( ) ;
351-
352- path . AddString ( text , font . FontFamily , ( int ) font . Style , font . Size , labelPoint , null ) ;
401+ path . AddString ( text , font . FontFamily , ( int ) font . Style , font . Size ,
402+ new RectangleF ( labelPoint , labelSize ) /* labelPoint*/ ,
403+ new StringFormat { Alignment = salign } /*null*/ ) ;
353404 if ( halo != null )
354405 g . DrawPath ( halo , path ) ;
355406 g . FillPath ( new SolidBrush ( forecolor ) , path ) ;
0 commit comments