public static void Draw(SKCanvas canvas, double screenWidth, double screenHeight, ScaleBarWidget scaleBar, float layerOpacity) { // If this widget belongs to no viewport, than stop drawing if (scaleBar.Map?.CRS == null) { return; } if (scaleBar.Map.Transformation == null) { return; } if (scaleBar.Map.Transformation.IsProjectionSupported(scaleBar.Map.CRS, "EPSG:4326") != true) { return; } // If this is the first time, we call this renderer, ... if (_paintScaleBar == null) { // ... than create the paints _paintScaleBar = CreateScaleBarPaint(scaleBar.TextColor.ToSkia(layerOpacity), StrokeInternal, SKPaintStyle.Fill, scaleBar.Scale); _paintScaleBarStroke = CreateScaleBarPaint(scaleBar.Halo.ToSkia(layerOpacity), StrokeExternal, SKPaintStyle.Stroke, scaleBar.Scale); _paintScaleText = CreateTextPaint(scaleBar.TextColor.ToSkia(layerOpacity), 2, SKPaintStyle.Fill, scaleBar.Scale); _paintScaleTextStroke = CreateTextPaint(scaleBar.Halo.ToSkia(layerOpacity), 2, SKPaintStyle.Stroke, scaleBar.Scale); } else { // Update paints with new values _paintScaleBar.Color = scaleBar.TextColor.ToSkia(layerOpacity); _paintScaleBar.StrokeWidth = StrokeInternal * scaleBar.Scale; _paintScaleBarStroke.Color = scaleBar.Halo.ToSkia(layerOpacity); _paintScaleBarStroke.StrokeWidth = StrokeExternal * scaleBar.Scale; _paintScaleText.Color = scaleBar.TextColor.ToSkia(layerOpacity); _paintScaleText.StrokeWidth = StrokeInternal * scaleBar.Scale; _paintScaleText.Typeface = SKTypeface.FromFamilyName(scaleBar.Font.FontFamily, SKTypefaceStyle.Bold); _paintScaleText.TextSize = (float)scaleBar.Font.Size * scaleBar.Scale; _paintScaleTextStroke.Color = scaleBar.Halo.ToSkia(layerOpacity); _paintScaleTextStroke.StrokeWidth = StrokeInternal * scaleBar.Scale; _paintScaleTextStroke.Typeface = SKTypeface.FromFamilyName(scaleBar.Font.FontFamily, SKTypefaceStyle.Bold); _paintScaleTextStroke.TextSize = (float)scaleBar.Font.Size * scaleBar.Scale; } float scaleBarLength1; string scaleBarText1; float scaleBarLength2; string scaleBarText2; (scaleBarLength1, scaleBarText1, scaleBarLength2, scaleBarText2) = scaleBar.GetScaleBarLengthAndText(); // Calc height of scale bar SKRect textSize = SKRect.Empty; // Do this, because height of text changes sometimes (e.g. from 2 m to 1 m) _paintScaleTextStroke.MeasureText("9999 m", ref textSize); var scaleBarHeight = textSize.Height + (scaleBar.TickLength + StrokeExternal * 0.5f + scaleBar.TextMargin) * scaleBar.Scale; if (scaleBar.ScaleBarMode == ScaleBarMode.Both && scaleBar.SecondaryUnitConverter != null) { scaleBarHeight *= 2; } else { scaleBarHeight += StrokeExternal * 0.5f * scaleBar.Scale; } scaleBar.Height = scaleBarHeight; // Draw lines // Get lines for scale bar var points = scaleBar.GetScaleBarLinePositions(scaleBarLength1, scaleBarLength2, StrokeExternal); // BoundingBox for scale bar BoundingBox envelop = new BoundingBox(); if (points != null) { // Draw outline of scale bar for (int i = 0; i < points.Length; i += 2) { canvas.DrawLine((float)points[i].X, (float)points[i].Y, (float)points[i + 1].X, (float)points[i + 1].Y, _paintScaleBarStroke); } // Draw scale bar for (int i = 0; i < points.Length; i += 2) { canvas.DrawLine((float)points[i].X, (float)points[i].Y, (float)points[i + 1].X, (float)points[i + 1].Y, _paintScaleBar); } envelop = points[0].GetBoundingBox(); for (int i = 1; i < points.Length; i++) { envelop = envelop.Join(points[i].GetBoundingBox()); } envelop = envelop.Grow(StrokeExternal * 0.5f * scaleBar.Scale); } // Draw text // Calc text height SKRect textSize1 = SKRect.Empty; SKRect textSize2 = SKRect.Empty; scaleBarText1 = scaleBarText1 ?? string.Empty; scaleBarText2 = scaleBarText2 ?? string.Empty; _paintScaleTextStroke.MeasureText(scaleBarText1, ref textSize1); _paintScaleTextStroke.MeasureText(scaleBarText2, ref textSize2); var(posX1, posY1, posX2, posY2) = scaleBar.GetScaleBarTextPositions(textSize.ToMapsui(), textSize1.ToMapsui(), textSize2.ToMapsui(), StrokeExternal); // Now draw text canvas.DrawText(scaleBarText1, posX1, posY1 - textSize1.Top, _paintScaleTextStroke); canvas.DrawText(scaleBarText1, posX1, posY1 - textSize1.Top, _paintScaleText); envelop = envelop.Join(new BoundingBox(posX1, posY1, posX1 + textSize1.Width, posY1 + textSize1.Height)); if (scaleBar.ScaleBarMode == ScaleBarMode.Both && scaleBar.SecondaryUnitConverter != null) { // Now draw second text canvas.DrawText(scaleBarText2, posX2, posY2 - textSize2.Top, _paintScaleTextStroke); canvas.DrawText(scaleBarText2, posX2, posY2 - textSize2.Top, _paintScaleText); envelop = envelop.Join(new BoundingBox(posX2, posY2, posX2 + textSize2.Width, posY2 + textSize2.Height)); } scaleBar.Envelope = envelop; if (scaleBar.ShowEnvelop) { // Draw a rect around the scale bar for testing var tempPaint = _paintScaleTextStroke; canvas.DrawRect(new SKRect((float)envelop.MinX, (float)envelop.MinY, (float)envelop.MaxX, (float)envelop.MaxY), tempPaint); } }
public static void Draw(Canvas canvas, ScaleBarWidget scaleBar) { // If this widget belongs to no viewport, than stop drawing if (scaleBar.Map?.CRS == null) { return; } if (scaleBar.Map.Transformation == null) { return; } if (scaleBar.Map.Transformation.IsProjectionSupported(scaleBar.Map.CRS, "EPSG:4326") != true) { return; } _brushScaleBar = new SolidColorBrush(scaleBar.TextColor.ToXaml()); _brushScaleBarStroke = new SolidColorBrush(scaleBar.Halo.ToXaml()); _brushScaleText = new SolidColorBrush(scaleBar.TextColor.ToXaml()); _brushScaleTextStroke = new SolidColorBrush(scaleBar.Halo.ToXaml()); var textBlock = new OutlinedTextBlock(); textBlock.Text = "9999 m"; textBlock.Fill = _brushScaleText; textBlock.Stroke = _brushScaleTextStroke; textBlock.StrokeThickness = StrokeExternal - StrokeInternal + 1; textBlock.FontFamily = new FontFamily(scaleBar.Font.FontFamily); textBlock.FontSize = scaleBar.Font.Size; textBlock.FontWeight = FontWeights.Bold; float scaleBarLength1; string scaleBarText1; float scaleBarLength2; string scaleBarText2; (scaleBarLength1, scaleBarText1, scaleBarLength2, scaleBarText2) = scaleBar.GetScaleBarLengthAndText(); // Calc height of scale bar Size textSize; // Do this, because height of text changes sometimes (e.g. from 2 m to 1 m) textSize = textBlock.MeasureText(); var scaleBarHeight = textSize.Height + (scaleBar.TickLength + StrokeExternal * 0.5f + scaleBar.TextMargin) * scaleBar.Scale; if (scaleBar.ScaleBarMode == ScaleBarMode.Both && scaleBar.SecondaryUnitConverter != null) { scaleBarHeight *= 2; } else { scaleBarHeight += StrokeExternal * 0.5f * scaleBar.Scale; } scaleBar.Height = (float)scaleBarHeight; // Draw lines // Get lines for scale bar var points = scaleBar.GetScaleBarLinePositions(scaleBarLength1, scaleBarLength2, StrokeExternal); // BoundingBox for scale bar BoundingBox envelop = new BoundingBox(); if (points != null) { // Draw outline of lines for (int i = 0; i < points.Length; i += 2) { var line = new Line(); line.X1 = points[i].X; line.Y1 = points[i].Y; line.X2 = points[i + 1].X; line.Y2 = points[i + 1].Y; line.Stroke = _brushScaleBarStroke; line.StrokeThickness = StrokeExternal; line.StrokeStartLineCap = PenLineCap.Square; line.StrokeEndLineCap = PenLineCap.Square; canvas.Children.Add(line); } // Draw lines for (int i = 0; i < points.Length; i += 2) { var line = new Line(); line.X1 = points[i].X; line.Y1 = points[i].Y; line.X2 = points[i + 1].X; line.Y2 = points[i + 1].Y; line.Stroke = _brushScaleBar; line.StrokeThickness = StrokeInternal; line.StrokeStartLineCap = PenLineCap.Square; line.StrokeEndLineCap = PenLineCap.Square; canvas.Children.Add(line); } envelop = points[0].GetBoundingBox(); for (int i = 1; i < points.Length; i++) { envelop = envelop.Join(points[i].GetBoundingBox()); } envelop = envelop.Grow(StrokeExternal * 0.5f * scaleBar.Scale); } // Draw text // Calc text height Size textSize1; Size textSize2; scaleBarText1 = scaleBarText1 ?? string.Empty; scaleBarText2 = scaleBarText2 ?? string.Empty; textBlock.Text = scaleBarText1; textSize1 = textBlock.MeasureText(); textBlock.Text = scaleBarText2; textSize2 = textBlock.MeasureText(); var boundingBoxText = new BoundingBox(0, 0, textSize.Width, textSize.Height); var boundingBoxText1 = new BoundingBox(0, 0, textSize1.Width, textSize1.Height); var boundingBoxText2 = new BoundingBox(0, 0, textSize2.Width, textSize2.Height); var(posX1, posY1, posX2, posY2) = scaleBar.GetScaleBarTextPositions(boundingBoxText, boundingBoxText1, boundingBoxText2, StrokeExternal); // Now draw text textBlock.Text = scaleBarText1; textBlock.Width = textSize1.Width; textBlock.Height = textSize1.Height; Canvas.SetLeft(textBlock, posX1); Canvas.SetTop(textBlock, posY1); canvas.Children.Add(textBlock); envelop = envelop.Join(new BoundingBox(posX1, posY1, posX1 + textSize1.Width, posY1 + textSize1.Height)); if (scaleBar.ScaleBarMode == ScaleBarMode.Both && scaleBar.SecondaryUnitConverter != null) { textBlock = new OutlinedTextBlock(); textBlock.Fill = _brushScaleText; textBlock.Stroke = _brushScaleTextStroke; textBlock.StrokeThickness = StrokeExternal - StrokeInternal + 1; textBlock.FontFamily = new FontFamily(scaleBar.Font.FontFamily); textBlock.FontSize = scaleBar.Font.Size; textBlock.FontWeight = FontWeights.Bold; textBlock.Text = scaleBarText2; textBlock.Width = textSize2.Width; textBlock.Height = textSize2.Height; Canvas.SetLeft(textBlock, posX2); Canvas.SetTop(textBlock, posY2); canvas.Children.Add(textBlock); envelop = envelop.Join(new BoundingBox(posX2, posY2, posX2 + textSize2.Width, posY2 + textSize2.Height)); } scaleBar.Envelope = envelop; if (scaleBar.ShowEnvelop) { // Draw a rect around the scale bar for testing var rect = new Rectangle(); rect.Width = envelop.MaxX - envelop.MinX; rect.Height = envelop.MaxY - envelop.MinY; rect.Stroke = _brushScaleTextStroke; rect.StrokeThickness = 1; Canvas.SetLeft(rect, envelop.MinX); Canvas.SetTop(rect, envelop.MinY); canvas.Children.Add(rect); } }