private static Label CreateLabel(IGeometry feature, string text, float rotation, int priority, LabelStyle style, IViewport viewport, Graphics g) { var gdiSize = g.MeasureString(text, style.Font.ToGdi()); var size = new Styles.Size { Width = gdiSize.Width, Height = gdiSize.Height }; var position = viewport.WorldToScreen(feature.GetBoundingBox().GetCentroid()); position.X = position.X - size.Width * (short)style.HorizontalAlignment * 0.5f; position.Y = position.Y - size.Height * (short)style.VerticalAlignment * 0.5f; if (position.X - size.Width > viewport.Width || position.X + size.Width < 0 || position.Y - size.Height > viewport.Height || position.Y + size.Height < 0) { return(null); } Label label; if (!style.CollisionDetection) { label = new Label(text, position, rotation, priority, null, style); } else { //Collision detection is enabled so we need to measure the size of the string label = new Label(text, position, rotation, priority, new LabelBox(position.X - size.Width * 0.5f - style.CollisionBuffer.Width, position.Y + size.Height * 0.5f + style.CollisionBuffer.Height, size.Width + 2f * style.CollisionBuffer.Width, size.Height + style.CollisionBuffer.Height * 2f), style); } if (!(feature is LineString)) { return(label); } var line = feature as LineString; if (line.Length / viewport.Resolution > size.Width) //Only label feature if it is long enough { CalculateLabelOnLinestring(line, ref label, viewport); } else { return(null); } return(label); }
private static Label CreateLabel(IGeometry feature, string text, float rotation, int priority, LabelStyle style, IViewport viewport, Graphics g) { var gdiSize = g.MeasureString(text, style.Font.ToGdi()); var size = new Styles.Size { Width = gdiSize.Width, Height = gdiSize.Height }; var position = viewport.WorldToScreen(feature.GetBoundingBox().GetCentroid()); position.X = position.X - size.Width * (short)style.HorizontalAlignment * 0.5f; position.Y = position.Y - size.Height * (short)style.VerticalAlignment * 0.5f; if (position.X - size.Width > viewport.Width || position.X + size.Width < 0 || position.Y - size.Height > viewport.Height || position.Y + size.Height < 0) return null; Label label; if (!style.CollisionDetection) label = new Label(text, position, rotation, priority, null, style); else { //Collision detection is enabled so we need to measure the size of the string label = new Label(text, position, rotation, priority, new LabelBox(position.X - size.Width * 0.5f - style.CollisionBuffer.Width, position.Y + size.Height * 0.5f + style.CollisionBuffer.Height, size.Width + 2f * style.CollisionBuffer.Width, size.Height + style.CollisionBuffer.Height * 2f), style); } if (!(feature is LineString)) return label; var line = feature as LineString; if (line.Length / viewport.Resolution > size.Width) //Only label feature if it is long enough CalculateLabelOnLinestring(line, ref label, viewport); else return null; return label; }