Example #1
0
        public SVSObjectStyle GetStyle()
        {
            string style;

            if (_attributes.TryGetValue("style", out style))
            {
                return(SVSObjectStyle.FromString(style));
            }
            return(null);
        }
Example #2
0
        public static SVSObjectStyle FromString(string str)
        {
            SVSObjectStyle style = new SVSObjectStyle();

            if (string.IsNullOrEmpty(str))
            {
                return(style);
            }
            string[] values = str.Split(';');
            for (int i = 0; i < values.Length; i++)
            {
                string[] pair = values[i].Split(':');
                switch (pair[0])
                {
                case "fill":
                    if (pair[1].Contains("#") && !pair[1].Contains("url"))
                    {
                        style._fillColor = ColorTranslator.FromHtml(pair[1]);
                    }
                    else
                    {
                        style._fillColor = Color.Transparent;
                    }
                    break;

                case "fill-opacity":
                    style._fillColor = Color.FromArgb((int)(Convert.ToSingle(pair[1].Replace(".", ",")) * 255), Convert.ToInt32(style._fillColor.R), Convert.ToInt32(style._fillColor.G), Convert.ToInt32(style._fillColor.B));
                    break;

                case "stroke":
                    if (pair[1].Contains("#") && !pair[1].Contains("url"))
                    {
                        style._borderColor = ColorTranslator.FromHtml(pair[1]);
                    }
                    else
                    {
                        style._borderColor = Color.Transparent;
                    }
                    break;

                case "stroke-opacity":
                    style._borderColor = Color.FromArgb((int)(Convert.ToSingle(pair[1].Replace(".", ",")) * 255), Convert.ToInt32(style._borderColor.R), Convert.ToInt32(style._borderColor.G), Convert.ToInt32(style._borderColor.B));
                    break;

                case "stroke-width":
                    style._borderSize = Convert.ToSingle(pair[1].Replace(".", ",").Replace("px", ""));
                    break;
                }
            }
            return(style);
        }
Example #3
0
        public Bitmap CreateBitmapFromSvg(float scale = 1F)
        {
            Size     bitmapSize = new SizeF((float)MapSize.Width / scale, (float)MapSize.Height / scale).ToSize();
            Bitmap   bitmap     = new Bitmap(bitmapSize.Width, bitmapSize.Height);
            Graphics g          = Graphics.FromImage(bitmap);

            g.Clear(Color.White);

            foreach (var obj in PaintObjectList)
            {
                Matrix matrix = obj.GetTransformMatrix();
                matrix.Scale(1.0F / scale, 1.0F / scale, MatrixOrder.Append);
                g.Transform = matrix;

                switch (obj.Name)
                {
                case "path":
                {
                    string data;
                    if (!obj.TryGetAttributeValue("data", out data))
                    {
                        continue;
                    }
                    string[]       val          = data.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    PointF         currentPoint = new PointF(0, 0);
                    SVSObjectStyle style        = obj.GetStyle();
                    GraphicsPath   path         = new GraphicsPath();

                    for (int i = 0; i < val.Length; i++)
                    {
                        if (val[i] == "m")
                        {
                            i++;
                            var p = StringToPoint(val[i]);
                            currentPoint = PointF.Add(currentPoint, new SizeF(p.X, p.Y));
                            while (i < val.Length - 1)
                            {
                                if (val[i + 1].Length == 1)
                                {
                                    break;
                                }
                                i++;
                                PointF nextPoint = StringToPoint(val[i]);
                                nextPoint.X += currentPoint.X;
                                nextPoint.Y += currentPoint.Y;
                                path.AddLine(currentPoint, nextPoint);
                                currentPoint = nextPoint;
                            }
                            continue;
                        }
                        if (val[i] == "M")
                        {
                            i++;
                            currentPoint = StringToPoint(val[i]);
                            while (i < val.Length - 1)
                            {
                                if (val[i + 1].Length == 1)
                                {
                                    break;
                                }
                                i++;
                                PointF nextPoint = StringToPoint(val[i]);
                                path.AddLine(currentPoint, nextPoint);
                                currentPoint = nextPoint;
                            }
                            continue;
                        }
                        if (val[i] == "l")
                        {
                            while (i < val.Length - 1)
                            {
                                if (val[i + 1].Length == 1)
                                {
                                    break;
                                }
                                i++;
                                PointF nextPoint = StringToPoint(val[i]);
                                nextPoint.X += currentPoint.X;
                                nextPoint.Y += currentPoint.Y;
                                path.AddLine(currentPoint, nextPoint);
                                currentPoint = nextPoint;
                            }
                        }
                        if (val[i] == "L")
                        {
                            while (i < val.Length - 1)
                            {
                                if (val[i + 1].Length == 1)
                                {
                                    break;
                                }
                                i++;
                                PointF nextPoint = StringToPoint(val[i]);
                                path.AddLine(currentPoint, nextPoint);
                                currentPoint = nextPoint;
                            }
                        }
                        if (val[i] == "c")
                        {
                            while (i < val.Length - 1)
                            {
                                PointF bez1   = new PointF(currentPoint.X + StringToPoint(val[i + 1]).X, currentPoint.Y + StringToPoint(val[i + 1]).Y);
                                PointF bez2   = new PointF(currentPoint.X + StringToPoint(val[i + 2]).X, currentPoint.Y + StringToPoint(val[i + 2]).Y);
                                PointF finish = new PointF(currentPoint.X + StringToPoint(val[i + 3]).X, currentPoint.Y + StringToPoint(val[i + 3]).Y);
                                path.AddBezier(currentPoint, bez1, bez2, finish);
                                currentPoint = finish;
                                i           += 3;
                                if (i < val.Length - 1)
                                {
                                    if (val[i + 1].Length == 1)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        if (val[i] == "C")
                        {
                            while (i < val.Length - 1)
                            {
                                PointF bez1   = new PointF(StringToPoint(val[i + 1]).X, StringToPoint(val[i + 1]).Y);
                                PointF bez2   = new PointF(StringToPoint(val[i + 2]).X, StringToPoint(val[i + 2]).Y);
                                PointF finish = new PointF(StringToPoint(val[i + 3]).X, StringToPoint(val[i + 3]).Y);
                                path.AddBezier(currentPoint, bez1, bez2, finish);
                                currentPoint = finish;
                                i           += 3;
                                if (i < val.Length - 1)
                                {
                                    if (val[i + 1].Length == 1)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        if (val[i].ToLower() == "a")
                        {
                            i++;
                            PointF point = StringToPoint(val[i]);
                            float  w     = point.X * 2;
                            float  h     = point.Y * 2;
                            float  x     = currentPoint.X - w;
                            float  y     = currentPoint.Y - h / 2;
                            path.AddEllipse(x, y, w, h);
                        }
                        if (val[i].ToLower() == "z")
                        {
                            if (val[i - 1].Length == 1 && val[1].Length == 1)
                            {
                                break;
                            }
                            path.CloseFigure();
                            break;
                        }
                    }

                    g.FillPath(style.GetBrush(), path);
                    g.DrawPath(style.GetPen(scale), path);

                    break;
                }

                case "rect":
                {
                    float          x     = PaintObject.ParseToFloat(obj.GetAttributeValue("x"));
                    float          y     = PaintObject.ParseToFloat(obj.GetAttributeValue("y"));
                    float          w     = PaintObject.ParseToFloat(obj.GetAttributeValue("width"));
                    float          h     = PaintObject.ParseToFloat(obj.GetAttributeValue("height"));
                    SVSObjectStyle style = obj.GetStyle();

                    g.FillRectangle(style.GetBrush(), x, y, w, h);
                    g.DrawRectangle(style.GetPen(scale), x, y, w, h);

                    break;
                }
                }
                g.ResetTransform();
            }
            return(bitmap);
        }
 public static SVSObjectStyle FromString(string str)
 {
     SVSObjectStyle style = new SVSObjectStyle();
     if (string.IsNullOrEmpty(str))
     {
         return style;
     }
     string[] values = str.Split(';');
     for (int i = 0; i < values.Length; i++)
     {
         string[] pair = values[i].Split(':');
         switch (pair[0])
         {
             case "fill":
                 if (pair[1].Contains("#") && !pair[1].Contains("url"))
                 {
                     style._fillColor = ColorTranslator.FromHtml(pair[1]);
                 }
                 else
                 {
                     style._fillColor = Color.Transparent;
                 }
                 break;
             case "fill-opacity":
                 style._fillColor = Color.FromArgb((int)(Convert.ToSingle(pair[1].Replace(".", ",")) * 255), Convert.ToInt32(style._fillColor.R), Convert.ToInt32(style._fillColor.G), Convert.ToInt32(style._fillColor.B));
                 break;
             case "stroke":
                 if (pair[1].Contains("#") && !pair[1].Contains("url"))
                 {
                     style._borderColor = ColorTranslator.FromHtml(pair[1]);
                 }
                 else
                 {
                     style._borderColor = Color.Transparent;
                 }
                 break;
             case "stroke-opacity":
                 style._borderColor = Color.FromArgb((int)(Convert.ToSingle(pair[1].Replace(".", ",")) * 255), Convert.ToInt32(style._borderColor.R), Convert.ToInt32(style._borderColor.G), Convert.ToInt32(style._borderColor.B));
                 break;
             case "stroke-width":
                 style._borderSize = Convert.ToSingle(pair[1].Replace(".", ",").Replace("px", ""));
                 break;
         }
     }
     return style;
 }