Пример #1
0
        public static void RenderBitmap(SKCanvas canvas, SKImage bitmap, float x, float y, float orientation = 0,
                                        float offsetX = 0, float offsetY = 0,
                                        LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
                                        LabelStyle.VerticalAlignmentEnum verticalAlignment     = LabelStyle.VerticalAlignmentEnum.Center,
                                        float opacity = 1f,
                                        float scale   = 1f)
        {
            canvas.Save();

            canvas.Translate(x, y);
            canvas.RotateDegrees(orientation, 0, 0); // todo: degrees or radians?
            canvas.Scale(scale, scale);

            x = offsetX + DetermineHorizontalAlignmentCorrection(horizontalAlignment, bitmap.Width);
            y = -offsetY + DetermineVerticalAlignmentCorrection(verticalAlignment, bitmap.Height);

            var halfWidth  = bitmap.Width / 2;
            var halfHeight = bitmap.Height / 2;

            var rect = new SKRect(x - halfWidth, y - halfHeight, x + halfWidth, y + halfHeight);

            RenderBitmap(canvas, bitmap, rect, opacity);

            canvas.Restore();
        }
Пример #2
0
        public static void RenderSvg(SKCanvas canvas, SkiaSharp.Extended.Svg.SKSvg svg, float x, float y, float orientation = 0,
                                     float offsetX = 0, float offsetY = 0,
                                     LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left,
                                     LabelStyle.VerticalAlignmentEnum verticalAlignment     = LabelStyle.VerticalAlignmentEnum.Top,
                                     float opacity = 1f,
                                     float scale   = 1f)
        {
            canvas.Save();

            canvas.Translate(x, y);
            canvas.RotateDegrees(orientation, 0, 0); // todo: degrees or radians?
            canvas.Scale(scale, scale);

            var halfWidth  = svg.CanvasSize.Width / 2;
            var halfHeight = svg.CanvasSize.Height / 2;

            // 0/0 are assumed at center of image, but Svg has 0/0 at left top position
            canvas.Translate(-halfWidth + offsetX, -halfHeight - offsetY);

            var rect = new SKRect(-halfWidth, -halfHeight, +halfWidth, +halfHeight);

            //var color = new SKColor(255, 255, 255, (byte)(255 * opacity));
            //var paint = new SKPaint { Color = color, FilterQuality = SKFilterQuality.High };

            canvas.DrawPicture(svg.Picture, null);

            canvas.Restore();
        }
Пример #3
0
        public static void RenderTexture(TextureInfo textureInfo, float x, float y, float orientation = 0,
                                         float offsetX = 0, float offsetY = 0,
                                         LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
                                         LabelStyle.VerticalAlignmentEnum verticalAlignment     = LabelStyle.VerticalAlignmentEnum.Center,
                                         float opacity = 1f,
                                         float scale   = 1f)
        {
            GL.Enable(All.Texture2D);
            GL.BindTexture(All.Texture2D, textureInfo.TextureId);

            GL.PushMatrix();
            GL.Translate(x, y, 0f);
            GL.Rotate(orientation, 0, 0, 1);
            GL.Scale(scale, scale, 1);

            x = offsetX + DetermineHorizontalAlignmentCorrection(horizontalAlignment, textureInfo.Width);
            y = -offsetY + DetermineVerticalAlignmentCorrection(verticalAlignment, textureInfo.Height);

            var halfWidth  = textureInfo.Width / 2;
            var halfHeight = textureInfo.Height / 2;

            var vertextArray = new[]
            {
                x - halfWidth, y - halfHeight,
                x + halfWidth, y - halfHeight,
                x + halfWidth, y + halfHeight,
                x - halfWidth, y + halfHeight
            };

            RenderTextureWithoutBinding(textureInfo.TextureId, vertextArray, opacity);

            GL.PopMatrix();
            GL.BindTexture(All.Texture2D, 0);
            GL.Disable(All.Texture2D);
        }
Пример #4
0
        public static void Draw(SKCanvas canvas, SKImage bitmap, float x, float y, float rotation = 0,
                                float offsetX = 0, float offsetY = 0,
                                LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
                                LabelStyle.VerticalAlignmentEnum verticalAlignment     = LabelStyle.VerticalAlignmentEnum.Center,
                                float opacity = 1f,
                                float scale   = 1f)
        {
            canvas.Save();

            canvas.Translate(x, y);
            if (rotation != 0)
            {
                canvas.RotateDegrees(rotation, 0, 0);
            }
            canvas.Scale(scale, scale);

            var width  = bitmap.Width;
            var height = bitmap.Height;

            x = offsetX + DetermineHorizontalAlignmentCorrection(horizontalAlignment, width);
            y = -offsetY + DetermineVerticalAlignmentCorrection(verticalAlignment, height);

            var halfWidth  = width >> 1;
            var halfHeight = height >> 1;

            var rect = new SKRect(x - halfWidth, y - halfHeight, x + halfWidth, y + halfHeight);

            Draw(canvas, bitmap, rect, opacity);

            canvas.Restore();
        }
Пример #5
0
        public static void Draw(SKCanvas canvas, SkiaSharp.Extended.Svg.SKSvg svg, float x, float y, float orientation = 0,
                                float offsetX = 0, float offsetY = 0,
                                LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left,
                                LabelStyle.VerticalAlignmentEnum verticalAlignment     = LabelStyle.VerticalAlignmentEnum.Top,
                                float opacity = 1f,
                                float scale   = 1f)
        {
            // todo: I assume we also need to apply opacity.
            // todo: It seems horizontalAlignment and verticalAlignment would make sense too. Is this similar to Anchor?

            canvas.Save();

            canvas.Translate(x, y);
            canvas.RotateDegrees(orientation, 0, 0); // todo: degrees or radians?
            canvas.Scale(scale, scale);

            var halfWidth  = svg.CanvasSize.Width / 2;
            var halfHeight = svg.CanvasSize.Height / 2;

            // 0/0 are assumed at center of image, but Svg has 0/0 at left top position
            canvas.Translate(-halfWidth + offsetX, -halfHeight - offsetY);

            canvas.DrawPicture(svg.Picture, new SKPaint()
            {
                IsAntialias = true
            });

            canvas.Restore();
        }
Пример #6
0
 public static void DrawLabel(Graphics g, PointF labelPoint, PointF offset, Font font, Color foreColor,
                              Brush backColor, Pen halo, float rotation, string text, MapViewport map,
                              LabelStyle.HorizontalAlignmentEnum alignment = LabelStyle.HorizontalAlignmentEnum.Left,
                              PointF?rotationPoint = null)
 {
     DrawLabelEx(g, labelPoint, offset, font, foreColor, backColor, halo, rotation, text, map, alignment, rotationPoint);
 }
Пример #7
0
 private static int DetermineHorizontalAlignmentCorrection(
     LabelStyle.HorizontalAlignmentEnum horizontalAlignment, int width)
 {
     if (horizontalAlignment == LabelStyle.HorizontalAlignmentEnum.Left)
     {
         return(width / 2);
     }
     if (horizontalAlignment == LabelStyle.HorizontalAlignmentEnum.Right)
     {
         return(-width / 2);
     }
     return(0); // center
 }
Пример #8
0
 private static float CalcHorizontalAlignment(LabelStyle.HorizontalAlignmentEnum horizontalAligment)
 {
     if (horizontalAligment == LabelStyle.HorizontalAlignmentEnum.Center)
     {
         return(0.5f);
     }
     if (horizontalAligment == LabelStyle.HorizontalAlignmentEnum.Left)
     {
         return(0f);
     }
     if (horizontalAligment == LabelStyle.HorizontalAlignmentEnum.Right)
     {
         return(1f);
     }
     throw new ArgumentException();
 }
Пример #9
0
        public static void RenderSvg(SKCanvas canvas, SkiaSharp.Extended.Svg.SKSvg svg, float x, float y, float orientation = 0,
                                     float offsetX = 0, float offsetY = 0,
                                     LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left,
                                     LabelStyle.VerticalAlignmentEnum verticalAlignment     = LabelStyle.VerticalAlignmentEnum.Top,
                                     float opacity = 1f,
                                     float scale   = 1f)
        {
            canvas.Save();

            canvas.Translate(x, y);
            canvas.RotateDegrees(orientation, 0, 0); // todo: degrees or radians?
            canvas.Scale(scale, scale);

            var halfWidth  = svg.CanvasSize.Width / 2;
            var halfHeight = svg.CanvasSize.Height / 2;

            // 0/0 are assumed at center of image, but Svg has 0/0 at left top position
            canvas.Translate(-halfWidth + offsetX, -halfHeight - offsetY);

            canvas.DrawPicture(svg.Picture);

            canvas.Restore();
        }
Пример #10
0
        public static void Draw(SKCanvas canvas, SKSvg svg, float x, float y, float orientation = 0,
                                float offsetX = 0, float offsetY = 0,
                                LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left,
                                LabelStyle.VerticalAlignmentEnum verticalAlignment     = LabelStyle.VerticalAlignmentEnum.Top,
                                float opacity = 1f,
                                float scale   = 1f)
        {
            // todo: I assume we also need to apply opacity.
            // todo: It seems horizontalAlignment and verticalAlignment would make sense too. Is this similar to Anchor?

            canvas.Save();

            canvas.Translate(x, y);
            canvas.RotateDegrees(orientation, 0, 0); // todo: degrees or radians?
            canvas.Scale(scale, scale);

            var halfWidth  = svg.Picture.CullRect.Width / 2;
            var halfHeight = svg.Picture.CullRect.Height / 2;

            // 0/0 are assumed at center of image, but Svg has 0/0 at left top position
            canvas.Translate(-halfWidth + offsetX, -halfHeight - offsetY);

            var alpha        = Convert.ToByte(255 * opacity);
            var transparency = SKColors.White.WithAlpha(alpha);

            using (var cf = SKColorFilter.CreateBlendMode(transparency, SKBlendMode.DstIn))
            {
                canvas.DrawPicture(svg.Picture, new SKPaint()
                {
                    IsAntialias = true,
                    ColorFilter = cf,
                });
            }

            canvas.Restore();
        }
Пример #11
0
        public static void DrawLabel(Graphics g, PointF labelPoint, PointF offset, Font font, Color forecolor,
                                     Brush backcolor, Pen halo, float rotation, string text, MapViewport map,
                                     LabelStyle.HorizontalAlignmentEnum alignment = LabelStyle.HorizontalAlignmentEnum.Left,
                                     PointF?rotationPoint = null)

        {
            //Calculate the size of the text
            var labelSize = _sizeOfString(g, text, font);

            //Add label offset
            labelPoint.X += offset.X;
            labelPoint.Y += offset.Y;

            //Translate alignment to stringalignment
            StringAlignment salign;

            switch (alignment)
            {
            case LabelStyle.HorizontalAlignmentEnum.Left:
                salign = StringAlignment.Near;
                break;

            case LabelStyle.HorizontalAlignmentEnum.Center:
                salign = StringAlignment.Center;
                break;

            default:
                salign = StringAlignment.Far;
                break;
            }

            if (rotation != 0 && !float.IsNaN(rotation))
            {
                rotationPoint = rotationPoint ?? labelPoint;

                g.FillEllipse(Brushes.LawnGreen, rotationPoint.Value.X - 1, rotationPoint.Value.Y - 1, 2, 2);

                var t = g.Transform.Clone();
                g.TranslateTransform(rotationPoint.Value.X, rotationPoint.Value.Y);
                g.RotateTransform(rotation);
                //g.TranslateTransform(-labelSize.Width/2, -labelSize.Height/2);

                labelPoint = new PointF(labelPoint.X - rotationPoint.Value.X,
                                        labelPoint.Y - rotationPoint.Value.Y);

                //labelSize = new SizeF(labelSize.Width*0.74f + 1f, labelSize.Height*0.74f);
                if (backcolor != null && backcolor != Brushes.Transparent)
                {
                    g.FillRectangle(backcolor, labelPoint.X, labelPoint.Y, labelSize.Width, labelSize.Height);
                }

                var path = new GraphicsPath();
                path.AddString(text, font.FontFamily, (int)font.Style, font.Size,
                               new RectangleF(labelPoint, labelSize) /* labelPoint*/,
                               new StringFormat {
                    Alignment = salign
                } /*null*/);
                if (halo != null)
                {
                    g.DrawPath(halo, path);
                }

                g.FillPath(new SolidBrush(forecolor), path);
                //g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), 0, 0);
                g.Transform = t;
            }
            else
            {
                if (backcolor != null && backcolor != Brushes.Transparent)
                {
                    g.FillRectangle(backcolor, labelPoint.X, labelPoint.Y, labelSize.Width,
                                    labelSize.Height);
                }

                var path = new GraphicsPath();
                path.AddString(text, font.FontFamily, (int)font.Style, font.Size,
                               new RectangleF(labelPoint, labelSize) /* labelPoint*/,
                               new StringFormat {
                    Alignment = salign
                } /*null*/);
                if (halo != null)
                {
                    g.DrawPath(halo, path);
                }
                g.FillPath(new SolidBrush(forecolor), path);
                //g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), LabelPoint.X, LabelPoint.Y);
            }
        }
Пример #12
0
        public static RectangleF DrawLabelEx(Graphics g, PointF labelPoint, PointF offset, Font font, Color foreColor,
                                             Brush backColor, Pen halo, float rotation, string text, MapViewport map,
                                             LabelStyle.HorizontalAlignmentEnum alignment = LabelStyle.HorizontalAlignmentEnum.Left,
                                             PointF?rotationPoint = null)
        {
            //Calculate the size of the text
            var labelSize = _sizeOfString(g, text, font);

            //Add label offset
            labelPoint.X += offset.X;
            labelPoint.Y += offset.Y;

            //Translate alignment to stringalignment
            StringAlignment sAlign;

            switch (alignment)
            {
            case LabelStyle.HorizontalAlignmentEnum.Left:
                sAlign = StringAlignment.Near;
                break;

            case LabelStyle.HorizontalAlignmentEnum.Center:
                sAlign = StringAlignment.Center;
                break;

            default:
                sAlign = StringAlignment.Far;
                break;
            }

            Matrix origTrans = null;
            Matrix symTrans  = null;

            if (rotation != 0 && !float.IsNaN(rotation))
            {
                rotationPoint = rotationPoint ?? labelPoint;

                origTrans = g.Transform.Clone();

                g.TranslateTransform(rotationPoint.Value.X, rotationPoint.Value.Y);
                g.RotateTransform(rotation);

                labelPoint = new PointF(labelPoint.X - rotationPoint.Value.X, labelPoint.Y - rotationPoint.Value.Y);

                symTrans = new Matrix();
                symTrans.Translate(rotationPoint.Value.X, rotationPoint.Value.Y);
                symTrans.Rotate(rotation);
            }

            var background = new RectangleF(labelPoint.X, labelPoint.Y, labelSize.Width, labelSize.Height);

            if (backColor != null && backColor != Brushes.Transparent)
            {
                g.FillRectangle(backColor, background);
            }

            using (var path = new GraphicsPath())
            {
                path.AddString(text, font.FontFamily, (int)font.Style, font.Size,
                               new RectangleF(labelPoint, labelSize),
                               new StringFormat {
                    Alignment = sAlign
                });

                if (halo != null)
                {
                    g.DrawPath(halo, path);
                    // excessive halo can bleed outside of background
                    background.Inflate(halo.Width / 2f, halo.Width / 2f);
                }

                g.FillPath(new SolidBrush(foreColor), path);
            }

            if (origTrans != null)
            {
                g.Transform = origTrans;
                origTrans.Dispose();
            }

            if (symTrans == null)
            {
                return(background);
            }

            var pts = background.ToPointArray();

            symTrans.TransformPoints(pts);
            symTrans.Dispose();
            return(pts.ToRectangleF());
        }