Ejemplo n.º 1
0
        public SVSObjectStyle GetStyle()
        {
            string style;

            if (Attributes.TryGetValue("style", out style))
            {
                return(SVSObjectStyle.FromString(style));
            }
            return(null);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public byte[,] GetMap()
        {
            Bitmap   bmp = new Bitmap(size.Width, size.Height);
            Graphics g   = Graphics.FromImage(bmp);

            g.Clear(Color.White);
            for (int n = 0; n < PaintObjectList.Count; n++)
            {
                if (PaintObjectList[n].GetName() == "path")
                {
                    string data;
                    if (!PaintObjectList[n].TryGetAttributeValue("data", out data))
                    {
                        continue;
                    }
                    string[]       val          = data.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    PointF         currentPoint = new PointF();
                    SVSObjectStyle style        = PaintObjectList[n].GetStyle();
                    GraphicsPath   path         = new GraphicsPath();
                    for (int i = 0; i < val.Length; i++)
                    {
                        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]);
                                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")
                        {
                            break;
                        }
                    }
                    path.Transform(PaintObjectList[n].GetTransformMatrix());
                    g.FillPath(style.GetBrush(), path);
                    g.DrawPath(style.GetPen(), path);
                }
                if (PaintObjectList[n].GetName() == "rect")
                {
                    float          x     = PaintObject.StringToFloatConvertor(PaintObjectList[n].GetAttributeValue("x"));
                    float          y     = PaintObject.StringToFloatConvertor(PaintObjectList[n].GetAttributeValue("y"));
                    float          w     = PaintObject.StringToFloatConvertor(PaintObjectList[n].GetAttributeValue("width"));
                    float          h     = PaintObject.StringToFloatConvertor(PaintObjectList[n].GetAttributeValue("height"));
                    SVSObjectStyle style = PaintObjectList[n].GetStyle();
                    g.Transform = PaintObjectList[n].GetTransformMatrix();
                    g.FillRectangle(style.GetBrush(), x, y, w, h);
                    g.DrawRectangle(style.GetPen(), x, y, w, h);
                    g.ResetTransform();
                }
            }
            MemoryStream ms = new MemoryStream();

            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ms.Position = 0;
            image       = new System.Windows.Media.Imaging.BitmapImage();
            image.BeginInit();
            image.StreamSource = ms;
            image.EndInit();
            byte[,] map = new byte[size.Width, size.Height];
            for (int i = 0; i < size.Width; i++)
            {
                for (int j = 0; j < size.Height; j++)
                {
                    Color color = bmp.GetPixel(i, j);

                    if (color != Color.FromArgb(255, 255, 255, 255))
                    {
                        map[i, j] |= 0x80;
                    }
                }
            }
            return(map);
        }
Ejemplo n.º 4
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;
 }