Ejemplo n.º 1
0
        public bool Read()
        {
            using (XmlTextReader reader = new XmlTextReader(_sourceStream))
            {
                PaintObjectList = new List <PaintObject>();
                Stack <string> transforms = new Stack <string>();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                        case "Map":
                            MapSize = new Size(int.Parse(reader.GetAttribute("width").Split('.')[0]), int.Parse(reader.GetAttribute("height").Split('.')[0]));
                            break;

                        case "svg":
                            MapSize = new Size(int.Parse(reader.GetAttribute("width").Split('.')[0]), int.Parse(reader.GetAttribute("height").Split('.')[0]));
                            break;

                        case "g":
                            transforms.Push(reader.GetAttribute("transform"));
                            break;

                        case "path":
                            PaintObject path = new PaintObject(reader.Name);
                            path.AddAttribute("data", reader.GetAttribute("d"));
                            path.AddAttribute("style", reader.GetAttribute("style"));
                            transforms.Push(reader.GetAttribute("transform"));
                            path.Transforms = transforms.ToList();
                            transforms.Pop();
                            //path.AddAttribute("transform", reader.GetAttribute("transform"));
                            //if (!string.IsNullOrEmpty(transforms))
                            //{
                            //    AddAttribute("maptransform", transforms);
                            //}
                            PaintObjectList.Add(path);
                            break;

                        case "rect":
                            PaintObject rect = new PaintObject(reader.Name);
                            rect.AddAttribute("x", reader.GetAttribute("x"));
                            rect.AddAttribute("y", reader.GetAttribute("y"));
                            rect.AddAttribute("width", reader.GetAttribute("width"));
                            rect.AddAttribute("height", reader.GetAttribute("height"));
                            rect.AddAttribute("style", reader.GetAttribute("style"));
                            transforms.Push(reader.GetAttribute("transform"));
                            rect.Transforms = transforms.ToList();
                            transforms.Pop();
                            PaintObjectList.Add(rect);
                            break;
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name == "g")
                        {
                            transforms.Pop();
                        }
                    }
                }
            }
            Colors = PaintObjectList.Select(po => po.GetStyle().FillColor).Concat(PaintObjectList.Select(po => po.GetStyle().BorderColor)).Distinct();
#if DEBUG
            foreach (var c in Colors)
            {
                Console.WriteLine(c.ToString());
            }
#endif
            return(true);
        }
Ejemplo n.º 2
0
 private Matrix ParseToMatrix(string tranform)
 {
     if (!string.IsNullOrEmpty(tranform))
     {
         if (tranform.Contains("translate"))
         {
             int      begin = tranform.IndexOf("(") + 1, end = tranform.IndexOf(")");
             string[] values = tranform.Substring(begin, end - begin).Split(',');
             return(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, PaintObject.ParseToFloat(values[0]), PaintObject.ParseToFloat(values[1])));
         }
         if (tranform.Contains("matrix"))
         {
             int      begin = tranform.IndexOf("(") + 1, end = tranform.IndexOf(")");
             string[] values = tranform.Substring(begin, end - begin).Split(',');
             return(new System.Drawing.Drawing2D.Matrix(
                        PaintObject.ParseToFloat(values[0]),
                        PaintObject.ParseToFloat(values[1]),
                        PaintObject.ParseToFloat(values[2]),
                        PaintObject.ParseToFloat(values[3]),
                        PaintObject.ParseToFloat(values[4]),
                        PaintObject.ParseToFloat(values[5])));
         }
     }
     return(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, 0, 0));
 }
Ejemplo n.º 3
0
 private System.Windows.Media.Matrix GetMatrixFromString(string transform)
 {
     if (!string.IsNullOrEmpty(transform))
     {
         if (transform.Contains("translate"))
         {
             int      begin = transform.IndexOf("(") + 1, end = transform.IndexOf(")");
             string[] values = transform.Substring(begin, end - begin).Split(',');
             return(new System.Windows.Media.Matrix(1, 0, 0, 1, PaintObject.ParseToDouble(values[0]), PaintObject.ParseToDouble(values[1])));
         }
         if (transform.Contains("matrix"))
         {
             int      begin = transform.IndexOf("(") + 1, end = transform.IndexOf(")");
             string[] values = transform.Substring(begin, end - begin).Split(',');
             return(new System.Windows.Media.Matrix(
                        PaintObject.ParseToDouble(values[0]),
                        PaintObject.ParseToDouble(values[1]),
                        PaintObject.ParseToDouble(values[2]),
                        PaintObject.ParseToDouble(values[3]),
                        PaintObject.ParseToDouble(values[4]),
                        PaintObject.ParseToDouble(values[5])));
         }
     }
     return(new System.Windows.Media.Matrix(1, 0, 0, 1, 0, 0));
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 private PointF StringToPoint(string value)
 {
     string[] point = value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
     return(new PointF(Convert.ToSingle(PaintObject.ParseToDouble(point[0])), Convert.ToSingle(PaintObject.ParseToDouble(point[1]))));
 }
Ejemplo n.º 6
0
        public bool Read()
        {
            using (XmlTextReader reader = new XmlTextReader(_sourceStream))
            {
                PaintObjectList = new List<PaintObject>();
                Stack<string> transforms = new Stack<string>();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "Map":
                                MapSize = new Size(int.Parse(reader.GetAttribute("width").Split('.')[0]), int.Parse(reader.GetAttribute("height").Split('.')[0]));
                                break;

                            case "svg":
                                MapSize = new Size(int.Parse(reader.GetAttribute("width").Split('.')[0]), int.Parse(reader.GetAttribute("height").Split('.')[0]));
                                break;

                            case "g":
                                transforms.Push(reader.GetAttribute("transform"));
                                break;

                            case "path":
                                PaintObject path = new PaintObject(reader.Name);
                                path.AddAttribute("data", reader.GetAttribute("d"));
                                path.AddAttribute("style", reader.GetAttribute("style"));
                                transforms.Push(reader.GetAttribute("transform"));
                                path.Transforms = transforms.ToList();
                                transforms.Pop();
                                //path.AddAttribute("transform", reader.GetAttribute("transform"));
                                //if (!string.IsNullOrEmpty(transforms))
                                //{
                                //    AddAttribute("maptransform", transforms);
                                //}
                                PaintObjectList.Add(path);
                                break;

                            case "rect":
                                PaintObject rect = new PaintObject(reader.Name);
                                rect.AddAttribute("x", reader.GetAttribute("x"));
                                rect.AddAttribute("y", reader.GetAttribute("y"));
                                rect.AddAttribute("width", reader.GetAttribute("width"));
                                rect.AddAttribute("height", reader.GetAttribute("height"));
                                rect.AddAttribute("style", reader.GetAttribute("style"));
                                transforms.Push(reader.GetAttribute("transform"));
                                rect.Transforms = transforms.ToList();
                                transforms.Pop();
                                PaintObjectList.Add(rect);
                                break;
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name == "g")
                        {
                            transforms.Pop();
                        }
                    }
                }
            }
            Colors = PaintObjectList.Select(po => po.GetStyle().FillColor).Concat(PaintObjectList.Select(po => po.GetStyle().BorderColor)).Distinct();
            #if DEBUG
            foreach (var c in Colors)
            {
                Console.WriteLine(c.ToString());
            }
            #endif
            return true;
        }