Exemplo n.º 1
0
        public override async Task <ImageResult> BuildResultAsync()
        {
            //apply filters
            if (Filters.Any())
            {
                var defs = Image.Children.GetSvgElementOf <SvgDefinitionList>();

                if (defs == null)
                {
                    defs = new SvgDefinitionList();
                    Image.Children.Add(defs);
                }

                var filterElement = new Svg.FilterEffects.SvgFilter();
                filterElement.ID = "filter01";

                defs.Children.Add(filterElement);

                foreach (var f in Filters)
                {
                    filterElement.Children.Add(f);
                }

                Image.CustomAttributes.Add("filter", $"url(#{filterElement.ID})");
            }

            MemoryStream mem = new MemoryStream();

            Image.Write(mem);

            return(new ImageResult(mem.ToArray(), MimeTypes.Svg));
        }
Exemplo n.º 2
0
        private static SvgGroup GetDataGroup(float left, float top, IReadOnlyList <DataPoint> data)
        {
            SvgGroup group       = Group(left, top);
            var      definitions = new SvgDefinitionList();
            var      clip        = new SvgClipPath()
            {
                ID = "data-clip"
            };

            clip.Children.Add(Rect(0, 0, ChartWidth, ChartHeight));
            definitions.Children.Add(clip);
            group.Children.Add(definitions);

            group.Children.Add(GetChartGroup(0, 0));
            group.Children.Add(GetDayGroup(0, ChartHeight));
            group.Children.Add(GetDateGroup(0, ChartHeight + DayRow));

            for (int i = 0; i <= TotalDays * SegmentsPerDay; i++)
            {
                float x         = i * SegmentWidth;
                bool  endOfWeek = (i % (SegmentsPerDay * DaysPerWeek)) == 0;
                bool  endOfDay  = (i % SegmentsPerDay) == 0;
                group.Children.Add(Line(x, 0, x, endOfDay ? DataHeight : ChartHeight, endOfWeek ? 2.5f : 1f));
            }

            group.Children.Add(Line(0, ChartHeight + DayRow, ChartWidth, ChartHeight + DayRow));

            var chartDataGroup = GetChartDataGroup(0, 0, data);

            chartDataGroup.ClipPath = new Uri(string.Format("url(#{0})", clip.ID), UriKind.Relative);
            group.Children.Add(chartDataGroup);
            group.Children.Add(GetDaysDataGroup(0, ChartHeight, data.First().Time.Date));

            return(group);
        }
Exemplo n.º 3
0
        private SvgDefinitionList GetDefinitions()
        {
            var defs  = new SvgDefinitionList();
            var group = new SvgGroup();

            group.ID = "element";
            defs.Children.Add(group);
            AddElementDefinition(group);
            return(defs);
        }
Exemplo n.º 4
0
        public SvgDocument RectangleToSVG(FigureSettings settings, int width, int height, string filepath)
        {
            SvgDocument svg = new SvgDocument();

            svg.Width  = width;
            svg.Height = height;

            SvgLinearGradientServer gradientFill = new SvgLinearGradientServer
            {
                ID = "lgradient"
            };
            SvgGradientStop from = new SvgGradientStop();

            from.Offset    = new SvgUnit(SvgUnitType.Percentage, 0.0f);
            from.StopColor = new SvgColourServer(settings.ColorFrom);

            SvgGradientStop to = new SvgGradientStop();

            to.Offset    = new SvgUnit(SvgUnitType.Percentage, 100.0f);
            to.StopColor = new SvgColourServer(settings.ColorTo);

            gradientFill.Children.Add(from);
            gradientFill.Children.Add(to);


            SvgRectangle rectangle = new SvgRectangle();

            rectangle.SetRectangle(new RectangleF(getTopLeft(settings, width, height), settings.Dimensions));
            rectangle.Fill = gradientFill;


            SvgDefinitionList defs = new SvgDefinitionList();

            defs.Children.Add(gradientFill);

            svg.Children.Add(defs);
            svg.Children.Add(rectangle);
            File.WriteAllText(filepath, svg.GetXML());
            return(svg);
        }
Exemplo n.º 5
0
        public void TestArrowCodeCreation()
        {
            // Sample code from Issue 212. Thanks to podostro.
            const int width  = 50;
            const int height = 50;

            var document = new SvgDocument()
            {
                ID      = "svgMap",
                ViewBox = new SvgViewBox(0, 0, width, height)
            };

            var defsElement = new SvgDefinitionList()
            {
                ID = "defsMap"
            };

            document.Children.Add(defsElement);

            var groupElement = new SvgGroup()
            {
                ID = "gMap"
            };

            document.Children.Add(groupElement);

            var arrowPath = new SvgPath()
            {
                ID       = "pathMarkerArrow",
                Fill     = new SvgColourServer(Color.Black),
                PathData = SvgPathBuilder.Parse(@"M0,0 L4,2 L0,4 L1,2 z")
            };

            var arrowMarker = new SvgMarker()
            {
                ID           = "markerArrow",
                MarkerUnits  = SvgMarkerUnits.StrokeWidth,
                MarkerWidth  = 5,
                MarkerHeight = 5,
                RefX         = 3,
                RefY         = 2,
                Orient       = new SvgOrient()
                {
                    IsAuto = true
                },
                Children = { arrowPath }
            };

            defsElement.Children.Add(arrowMarker);

            var line = new SvgLine()
            {
                ID          = "lineLinkedPoint",
                StartX      = 0,
                StartY      = 15,
                EndX        = 35,
                EndY        = 35,
                Stroke      = new SvgColourServer(Color.Black),
                StrokeWidth = 3,
                MarkerEnd   = new Uri(string.Format("url(#{0})", arrowMarker.ID), UriKind.Relative)
            };

            groupElement.Children.Add(line);

            var svgXml = document.GetXML();
            var img    = document.Draw();

            var file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            File.WriteAllText(file + ".svg", svgXml);
            img.Save(file + ".png");
            Debug.WriteLine(string.Format("Svg saved to '{0}'", file));

            Debugger.Break();

            // Remove
            var svg = new FileInfo(file + ".svg");

            if (svg.Exists)
            {
                svg.Delete();
            }
            var png = new FileInfo(file + ".png");

            if (png.Exists)
            {
                png.Delete();
            }
        }
        /// <summary>
        /// Creates an svg of the AOP network graph and writes it to the specified file.
        /// </summary>
        /// <param name="aopNetwork"></param>
        /// <param name="fileName"></param>
        public void Create(AopNetwork aopNetwork, string fileName)
        {
            var layers = aopNetwork.GetAopNetworkLayers();

            var title     = !string.IsNullOrEmpty(aopNetwork.Name) ? aopNetwork.Name : null;
            var showTitle = title != null;
            var offsetX   = 10D;
            var offsetY   = showTitle ? 40D : 20D;

            var width  = offsetX + layers.Count * (BlockWidth + HorizontalMargin) - HorizontalMargin + 1;
            var height = offsetY + layers.Max(r => r.KeyEvents.Count) * (BlockHeight + VerticalMargin) - VerticalMargin + 1;
            var doc    = new SvgDocument()
            {
                Width      = (float)(width),
                Height     = (float)(height),
                FontSize   = 10,
                FontFamily = "Arial",
            };
            var defsElement = new SvgDefinitionList()
            {
                ID = "defsMap"
            };

            doc.Children.Add(defsElement);
            defsElement.Children.Add(arrowMarker);

            if (showTitle)
            {
                var text = new SvgText()
                {
                    FontSize   = 14,
                    FontWeight = SvgFontWeight.Bold,
                    Nodes      = { new SvgContentNode()
                                   {
                                       Content = title
                                   } },
                    TextAnchor = SvgTextAnchor.Middle,
                    X          = new SvgUnitCollection()
                    {
                        0f
                    },
                    Y = new SvgUnitCollection()
                    {
                        0f
                    },
                    Dx = new SvgUnitCollection()
                    {
                        (float)width / 2f
                    },
                    Fill = new SvgColourServer(Color.Black),
                };
                text.Dy = new SvgUnitCollection()
                {
                    2 * text.Bounds.Height
                };
                doc.Children.Add(text);
            }

            var keyEventNodes = DrawKeyEvents(
                doc,
                layers,
                offsetX,
                offsetY
                );
            var indirectKers = aopNetwork.GetIndirectKeyEventRelationships();
            var cyclicKers   = aopNetwork.FindFeedbackRelationships();
            var kers         = aopNetwork.KeyEventRelationships
                               .Except(indirectKers)
                               .Except(cyclicKers)
                               .ToList();

            DrawKeyEventRelationships(doc, kers, keyEventNodes);
            doc.FlushStyles(true);
            doc.Write(fileName);
        }
Exemplo n.º 7
0
        private void button1_Click(object sender, EventArgs e)
        {
            /*
             * L 最低
             * M 低
             * Q 中等
             * H 高
             */
            FolderBrowserDialog fileDialog = new FolderBrowserDialog
            {
                Description         = @"请选择保存输出图件的文件夹",
                ShowNewFolderButton = true,
                SelectedPath        = Environment.CurrentDirectory,
            };

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                List <QRCodeUrlInfo> qRCodeUrlInfos = new List <QRCodeUrlInfo>();
                try
                {
                    qRCodeUrlInfos = JsonConvert.DeserializeObject <List <QRCodeUrlInfo> >(textBox1.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                try
                {
                    string elStr = comboBox1.SelectedItem.ToString().Split(' ')[0];
                    foreach (QRCodeUrlInfo qRCodeUrlInfo in qRCodeUrlInfos)
                    {
                        WebClient wc  = new WebClient();
                        string    xxx = System.Web.HttpUtility.UrlEncode(qRCodeUrlInfo.Url, Encoding.UTF8);
                        string    url = "http://www.liantu.com/savevector.php?text=" + xxx + "&el=" + elStr +
                                        "&m=15&vt=svg";
                        string svgFileFullName     = fileDialog.SelectedPath + "\\" + qRCodeUrlInfo.Name + ".svg";
                        string svgTempFileFullName = fileDialog.SelectedPath + "\\" + qRCodeUrlInfo.Name + ".Temp.svg";
                        wc.DownloadFile(url, svgTempFileFullName);

                        SvgDocument svgDoc   = SvgDocument.Open(svgTempFileFullName);
                        SvgUnit     minX     = SvgUnit.None;
                        SvgUnit     minY     = SvgUnit.None;
                        SvgUnit     maxX     = SvgUnit.None;
                        SvgUnit     maxY     = SvgUnit.None;
                        SvgUnit     recWidth = SvgUnit.None;

                        #region 获取真实边框大小
                        foreach (SvgElement element in svgDoc.Children)
                        {
                            if (element is SvgDefinitionList)
                            {
                                if (element.Children.Count > 0)
                                {
                                    if (element.Children[0] is SvgRectangle)
                                    {
                                        SvgRectangle svgRectangle = element.Children[0] as SvgRectangle;
                                        recWidth = svgRectangle.Width;
                                    }
                                }
                                SvgDefinitionList svgDefinitionList = element as SvgDefinitionList;
                            }
                            if (element is SvgGroup)
                            {
                                if (element.Children.Count > 0)
                                {
                                    if (element.Children[0] is SvgUse)
                                    {
                                        SvgUse svgUse = element.Children[0] as SvgUse;

                                        if (minX.IsNone || minY.IsNone)
                                        {
                                            minX = svgUse.X;
                                            minY = svgUse.Y;
                                        }
                                        else if (minX.Value > svgUse.X)
                                        {
                                            minX = svgUse.X;
                                        }
                                        else if (minY.Value > svgUse.Y)
                                        {
                                            minY = svgUse.Y;
                                        }
                                    }


                                    if (element.Children[element.Children.Count - 1] is SvgUse)
                                    {
                                        SvgUse svgUse = element.Children[element.Children.Count - 1] as SvgUse;
                                        if (maxX.IsNone || maxY.IsNone)
                                        {
                                            maxX = svgUse.X;
                                            maxY = svgUse.Y;
                                        }
                                        else if (maxX.Value < svgUse.X)
                                        {
                                            maxX = svgUse.X;
                                        }
                                        else if (maxY.Value < svgUse.Y)
                                        {
                                            maxY = svgUse.Y;
                                        }
                                    }
                                }
                            }
                        }
                        maxX.Value = maxX.Value - minX.Value + recWidth.Value;
                        maxY.Value = maxY.Value - minY.Value + recWidth.Value;

                        SvgUnitType svgUnitType = minX.Type;
                        #endregion

                        int rowNum = Convert.ToInt32(Math.Floor(maxX.Value / recWidth.Value));


                        #region 进行位置计算
                        foreach (SvgElement element in svgDoc.Children)
                        {
                            if (element is SvgRectangle)
                            {
                                SvgRectangle svgRectangle = element as SvgRectangle;
                                svgRectangle.Width  = maxX;
                                svgRectangle.Height = maxY;
                            }

                            if (element is SvgGroup)
                            {
                                foreach (SvgElement element2 in element.Children)
                                {
                                    SvgUse svgUse = element2 as SvgUse;
                                    if (svgUse != null)
                                    {
                                        svgUse.X = new SvgUnit(svgUnitType, svgUse.X.Value - minX.Value);
                                        svgUse.Y = new SvgUnit(svgUnitType, svgUse.Y.Value - minY.Value);
                                    }
                                }
                            }
                        }
                        #endregion


                        for (int i = 0; i < svgDoc.Children.Count; i++)
                        {
                            if (svgDoc.Children[i] is SvgRectangle)
                            {
                                SvgRectangle svgRectangle = svgDoc.Children[i] as SvgRectangle;
                                svgRectangle.Width  = new SvgUnit(svgUnitType, maxX.Value - recWidth.Value - recWidth.Value);
                                svgRectangle.Height = new SvgUnit(svgUnitType, maxY.Value - recWidth.Value - recWidth.Value);
                            }

                            if (svgDoc.Children[i] is SvgGroup)
                            {
                                SvgElement element   = svgDoc.Children[i] as SvgElement;
                                string     attribute = string.Empty;
                                element.TryGetAttribute("fill", out attribute);

                                for (int j = element.Children.Count; j > 0; j--)
                                {
                                    SvgUse svgUse = element.Children[j - 1] as SvgUse;

                                    if (((int)svgUse.Y.Value) + ((int)recWidth.Value) == ((int)maxY.Value) ||// 最后一行
                                        ((int)svgUse.Y.Value) == 0 ||          // 第一行
                                        ((int)svgUse.X.Value) + ((int)recWidth.Value) == ((int)maxX.Value) || // 最后一列
                                        ((int)svgUse.X.Value) == 0)            // 第一列
                                    {
                                        // 最后一行
                                        element.Children.RemoveAt(j - 1);
                                    }
                                    else
                                    {
                                        if (svgUse != null)
                                        {
                                            svgUse.X = new SvgUnit(svgUnitType, svgUse.X.Value - recWidth.Value);
                                            svgUse.Y = new SvgUnit(svgUnitType, svgUse.Y.Value - recWidth.Value);
                                        }
                                    }
                                }
                            }
                        }
                        svgDoc.Width   = new SvgUnit(svgUnitType, maxX.Value - recWidth.Value - recWidth.Value);
                        svgDoc.Height  = new SvgUnit(svgUnitType, maxY.Value - recWidth.Value - recWidth.Value);
                        svgDoc.ViewBox = new SvgViewBox(0, 0, svgDoc.Width.Value, svgDoc.Height.Value);

                        var svgXml = svgDoc.GetXML();
                        File.WriteAllText(svgFileFullName, svgXml);
                        File.Delete(svgTempFileFullName);
                    }
                    MessageBox.Show(@"导出完成");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Exemplo n.º 8
0
        public static void MakeSVG(List <CapturePacket> packets, string fileName)
        {
            const int baselinestep       = 250;  // distance between vertical lines
            const int horizontalBase     = 130;  // upper range for the vertical lines
            const int unitBoxWidth       = 120;  // width of device box
            const int unitBoxHeight      = 40;   // height of device box
            const int firstarrowdistance = 50;   // vertical distance before the first sequence arrow
            const int maxboxwidth        = 1000; // maximum width of description textboxes

            var font = new Font("Calibri",
                                9); // SVG and C# have different ideas of what a fontsize is apparently, so we set this lower and hope for the best

            int baselinegen =
                -100;                                           // iterator for making the vertical lines, the first line does one step from this value
            int arrowgen = horizontalBase + firstarrowdistance; // iterator for making sequence arrows

            // iterate through the data and gather what we need into a little struct for convenience
            List <Sequence> list = GetSequences(packets, baselinestep, out var devices, ref baselinegen);

            // after all vertical lines have been placed, make one for the description boxes
            int descriptionBaseline = baselinegen + 50;

            var svg          = new SvgDocument();
            var groupDevices = new SvgGroup
            {
                FontFamily = "Calibri",
                FontSize   = 16
            };
            var groupArrows = new SvgGroup
            {
                ID          = "arrows",
                Stroke      = new SvgColourServer(Color.Black),
                StrokeWidth = 2
            };
            var groupArrowTexts = new SvgGroup
            {
                ID         = "arrowtexts",
                FontFamily = "Calibri",
                FontSize   = 11
            };
            var groupBaselines = new SvgGroup
            {
                ID              = "baselines",
                Stroke          = new SvgColourServer(Color.Black),
                StrokeWidth     = 2,
                StrokeDashArray = new SvgUnitCollection {
                    10, 10
                }
            };
            var groupDescriptionBoxes = new SvgGroup
            {
                ID          = "descriptionboxes",
                Fill        = new SvgColourServer(Color.Transparent),
                Stroke      = new SvgColourServer(Color.Black),
                StrokeWidth = 2
            };
            var groupDescriptionTexts = new SvgGroup
            {
                ID         = "descriptiontexts",
                FontFamily = "Calibri",
                FontSize   = 12
            };

            svg.Children.Add(groupDevices);
            svg.Children.Add(groupArrows);
            svg.Children.Add(groupArrowTexts);
            svg.Children.Add(groupBaselines);
            svg.Children.Add(groupDescriptionBoxes);
            svg.Children.Add(groupDescriptionTexts);

            var svgDefinitionList = new SvgDefinitionList();
            var svgMarker         = new SvgMarker
            {
                ID           = "arrow",
                MarkerWidth  = 10,
                MarkerHeight = 10,
                RefX         = 9,
                RefY         = 3,
                Orient       = new SvgOrient {
                    IsAuto = true
                },
                MarkerUnits = SvgMarkerUnits.StrokeWidth
            };
            var svgPath = new SvgPath
            {
                PathData = new SvgPathSegmentList
                {
                    new SvgMoveToSegment(new PointF(0, 0)),
                    new SvgLineSegment(new PointF(0, 0), new PointF(0, 6)),
                    new SvgLineSegment(new PointF(0, 0), new PointF(9, 3)),
                    new SvgClosePathSegment()
                },
                Fill = new SvgColourServer(Color.Black)
            };

            svgMarker.Children.Add(svgPath);
            svgDefinitionList.Children.Add(svgMarker);
            svg.Children.Add(svgDefinitionList);

            // make the device boxes
            foreach (KeyValuePair <IPAddress, int> device in devices)
            {
                groupDevices.Children.Add(new SvgRectangle
                {
                    Fill        = new SvgColourServer(Color.White),
                    Stroke      = new SvgColourServer(Color.Black),
                    StrokeWidth = 2,
                    X           = device.Value - unitBoxWidth / 2,
                    Y           = horizontalBase - unitBoxHeight,
                    Width       = unitBoxWidth,
                    Height      = unitBoxHeight
                });
                groupDevices.Children.Add(new SvgText
                {
                    Text   = device.Key.ToString(),
                    Stroke = new SvgColourServer(Color.Black),
                    X      = new SvgUnitCollection {
                        new SvgUnit(device.Value)
                    },
                    Y = new SvgUnitCollection {
                        new SvgUnit(horizontalBase - unitBoxHeight / 2)
                    },
                    TextAnchor = SvgTextAnchor.Middle
                });
            }


            foreach (Sequence sequence in list)
            {
                // get the x axis of the two devices we are relating
                int one = devices[sequence.From];
                int two = devices[sequence.To];


                groupArrows.Children.Add(new SvgLine
                {
                    StartX    = one,
                    StartY    = arrowgen,
                    EndX      = two,
                    EndY      = arrowgen,
                    MarkerEnd = new Uri("url(#arrow)", UriKind.Relative)
                });

                // get middle of arrow
                int right  = Math.Max(one, two);
                int left   = Math.Min(one, two);
                int middle = (right - left) / 2;
                middle = left + middle;
                groupArrowTexts.Children.Add(new SvgText
                {
                    Text = sequence.Name,
                    X    = new SvgUnitCollection {
                        new SvgUnit(middle)
                    },
                    Y = new SvgUnitCollection {
                        new SvgUnit(arrowgen - 2)
                    },
                    TextAnchor = SvgTextAnchor.Middle
                });


                var svgBoxText = new SvgText
                {
                    X = new SvgUnitCollection {
                        new SvgUnit(descriptionBaseline + 5)
                    },
                    Y = new SvgUnitCollection {
                        new SvgUnit(arrowgen - 12)
                    }
                };
                svgBoxText.Children.Add(new SvgTextSpan
                {
                    Text           = sequence.Time,
                    TextDecoration = SvgTextDecoration.Underline,
                    X = new SvgUnitCollection {
                        new SvgUnit(descriptionBaseline + 5)
                    },
                    Dy = new SvgUnitCollection {
                        new SvgUnit(SvgUnitType.Em, 1.2f)
                    }
                });

                int maxstrlen = TextRenderer.MeasureText(sequence.Time, font).Width;
                var lines     = 1;
                foreach (KeyValuePair <string, string> pair in sequence.Dic)
                {
                    var both = new SvgTextSpan
                    {
                        X = new SvgUnitCollection {
                            new SvgUnit(descriptionBaseline + 5)
                        },
                        Dy = new SvgUnitCollection {
                            new SvgUnit(SvgUnitType.Em, 1.2f)
                        }
                    };

                    string textKey   = pair.Key + ": ";
                    string textValue = Functions.RemoveInvalidXMLChars(pair.Value);

                    int strlen = TextRenderer.MeasureText(textKey + textValue, font).Width;

                    if (strlen > maxboxwidth)
                    {
                        // ooof
                        string tv1 = textValue;
                        var    tv2 = "";
                        while (TextRenderer.MeasureText(textKey + tv1, font).Width > maxboxwidth)
                        {
                            tv2 = tv1[tv1.Length - 1] + tv2;
                            tv1 = tv1.Substring(0, tv1.Length - 1);
                        }

                        var keySpan = new SvgTextSpan {
                            Text = textKey, FontWeight = SvgFontWeight.Bold
                        };
                        var valueSpan1 = new SvgTextSpan {
                            Text = tv1
                        };
                        var valueSpan2 = new SvgTextSpan
                        {
                            Text = tv2,
                            X    = new SvgUnitCollection {
                                new SvgUnit(descriptionBaseline + 5)
                            },
                            Dy = new SvgUnitCollection {
                                new SvgUnit(SvgUnitType.Em, 1.2f)
                            }
                        };
                        both.Children.Add(keySpan);
                        both.Children.Add(valueSpan1);
                        svgBoxText.Children.Add(both);

                        svgBoxText.Children.Add(valueSpan2);
                        lines += 2;
                    }
                    else
                    {
                        var keySpan = new SvgTextSpan {
                            Text = textKey, FontWeight = SvgFontWeight.Bold
                        };
                        var valueSpan = new SvgTextSpan {
                            Text = textValue
                        };
                        both.Children.Add(keySpan);
                        both.Children.Add(valueSpan);
                        svgBoxText.Children.Add(both);
                        lines++;
                    }


                    if (strlen > maxstrlen)
                    {
                        maxstrlen = strlen;
                    }
                }

                // box height calc, 30 margin + 14 per line. Then have 20 between box bottom and next arrow (at least)

                var boxheight = (int)(lines * font.Height * 1.02);


                // draw the box
                groupDescriptionBoxes.Children.Add(new SvgRectangle
                {
                    X      = descriptionBaseline,
                    Y      = arrowgen - 10,
                    Width  = maxstrlen,
                    Height = boxheight
                });

                // add text after box
                groupDescriptionTexts.Children.Add(svgBoxText);

                // setup next arrow
                arrowgen += boxheight + 30;
            }


            // finally, draw the baselines
            foreach (KeyValuePair <IPAddress, int> device in devices)
            {
                groupBaselines.Children.Add(new SvgLine
                {
                    StartX = device.Value,
                    StartY = horizontalBase,
                    EndX   = device.Value,
                    EndY   = arrowgen
                });
            }

            // now lets make SVG!!!
            svg.Width   = descriptionBaseline + maxboxwidth + 5;
            svg.Height  = arrowgen + 5;
            svg.ViewBox = new SvgViewBox(0, 0, descriptionBaseline + maxboxwidth + 5, arrowgen + 5);


            using (FileStream fileStream = File.Create(fileName))
            {
                svg.Write(fileStream);
            }
        }
Exemplo n.º 9
0
        public static SvgDocument Generate(ISvgRenderableColumn[] columns, ILegendGroup[] legend)
        {
            //generating headers
            SvgGroup       headerGroup      = new SvgGroup();
            SvgPaintServer blackPaint       = new SvgColourServer(System.Drawing.Color.Black);
            double         horizontalOffset = 0.0;
            double         headerHeight     = 0;

            for (int i = 0; i < columns.Length; i++)
            {
                RenderedSvg  heading = columns[i].RenderHeader();
                SvgRectangle rect    = new SvgRectangle();
                headerHeight = heading.RenderedSize.Height;
                rect.Width   = Helpers.dtos(heading.RenderedSize.Width);
                rect.Height  = Helpers.dtos(heading.RenderedSize.Height);
                rect.X       = Helpers.dtos(horizontalOffset);
                rect.Y       = Helpers.dtos(0.0);
                rect.Stroke  = blackPaint;

                heading.SVG.Transforms.Add(new SvgTranslate((float)(horizontalOffset + heading.RenderedSize.Width * 0.5), (float)heading.RenderedSize.Height * 0.9f));
                heading.SVG.Transforms.Add(new SvgRotate((float)-90.0));

                headerGroup.Children.Add(rect);
                headerGroup.Children.Add(heading.SVG);
                horizontalOffset += heading.RenderedSize.Width;
            }
            //generating columns
            SvgGroup columnsGroup = new SvgGroup();
            double   columnHeight = 0.0;

            horizontalOffset = 0.0;
            columnsGroup.Transforms.Add(new SvgTranslate(0.0f, (float)headerHeight));
            for (int i = 0; i < columns.Length; i++)
            {
                RenderedSvg  column = columns[i].RenderColumn();
                SvgRectangle rect   = new SvgRectangle();
                columnHeight = column.RenderedSize.Height;
                rect.Width   = Helpers.dtos(column.RenderedSize.Width);
                rect.Height  = Helpers.dtos(column.RenderedSize.Height);
                rect.X       = Helpers.dtos(horizontalOffset);
                rect.Y       = Helpers.dtos(0.0);
                rect.Stroke  = blackPaint;

                column.SVG.Transforms.Add(new SvgTranslate((float)(horizontalOffset)));

                columnsGroup.Children.Add(rect);
                columnsGroup.Children.Add(column.SVG);

                horizontalOffset += column.RenderedSize.Width;
            }

            //generating legend group
            SvgGroup legendGroup = new SvgGroup();

            const float legendYGap    = 30.0f;
            const float legendXOffset = 10.0f;


            legendGroup.Transforms.Add(new SvgTranslate(legendXOffset, Helpers.dtos(headerHeight + columnHeight + legendYGap)));

            const float titleYoffset    = 60.0f;
            const float itemsYoffset    = 20.0f;
            const float itemYgap        = 20.0f;
            const float interGroupYgap  = 15.0f;
            const float itemImageWidth  = 64.0f;
            const float itemImageHeight = 32.0f;
            const float descrXoffset    = 150.0f;

            SvgText legendTitle = new SvgText("Условные обозначения");

            legendTitle.FontSize = 22;
            legendTitle.Fill     = new SvgColourServer(System.Drawing.Color.Black);
            legendTitle.Transforms.Add(new SvgTranslate(30.0f, Helpers.dtos(titleYoffset * 0.25f)));
            legendGroup.Children.Add(legendTitle);

            float currGroupOffset = 0.0f;
            int   k = 0;

            foreach (ILegendGroup group in legend)
            {
                //title
                SvgText groupTitle = new SvgText(group.GroupName);
                groupTitle.FontSize = new SvgUnit((float)18);
                groupTitle.Fill     = new SvgColourServer(System.Drawing.Color.Black);
                groupTitle.Transforms.Add(new SvgTranslate(0.0f, currGroupOffset + titleYoffset));
                legendGroup.Children.Add(groupTitle);

                //items
                var items = group.Items;

                int j = 0;

                SvgElement[] fragments = items.Select(item => item.GetPresentation(itemImageWidth, itemImageHeight)).ToArray();

                foreach (var item in items)
                {
                    if (fragments[j] == null)
                    {
                        continue;
                    }

                    float yOffset = currGroupOffset + titleYoffset + itemsYoffset + j * (itemYgap + itemImageHeight);

                    if (fragments[j] is SvgFragment)
                    {
                        SvgFragment fragment = (SvgFragment)fragments[j];
                        fragment.X = 0;
                        fragment.Y = yOffset;
                    }
                    else
                    {
                        fragments[j].Transforms.Add(new SvgTranslate(0, yOffset));
                    }

                    legendGroup.Children.Add(fragments[j]);

                    SvgText text = new SvgText(item.Description);
                    text.FontSize = new SvgUnit((float)14);
                    text.Fill     = new SvgColourServer(System.Drawing.Color.Black);
                    text.Transforms.Add(new SvgTranslate(descrXoffset, yOffset + itemImageHeight * 0.5f));
                    legendGroup.Children.Add(text);
                    j++;
                }
                currGroupOffset += titleYoffset + itemsYoffset + (itemYgap + itemImageHeight) * items.Length + interGroupYgap;
                k++;
            }


            //gathering definitions
            SvgDefinitionList allDefs = new SvgDefinitionList();

            for (int i = 0; i < columns.Length; i++)
            {
                SvgDefinitionList defs = columns[i].Definitions;
                foreach (SvgPatternServer def in defs.Children)
                {
                    //overridings tile size
                    allDefs.Children.Add(def);
                }
            }

            SvgDocument result = new SvgDocument();

            result.Children.Add(allDefs);
            result.Width  = Helpers.dtos(horizontalOffset);
            result.Height = Helpers.dtos((headerHeight + columnHeight + legendYGap + currGroupOffset));
            result.Fill   = new SvgColourServer(System.Drawing.Color.White);
            result.Children.Add(headerGroup);
            result.Children.Add(columnsGroup);
            result.Children.Add(legendGroup);

            return(result);
        }
Exemplo n.º 10
0
        public void TestArrowCodeCreation()
        {
            // Sample code from Issue 212. Thanks to podostro.
            const int width = 50;
            const int height = 50;

            var document = new SvgDocument()
            {
                ID = "svgMap",
                ViewBox = new SvgViewBox(0, 0, width, height)
            };

            var defsElement = new SvgDefinitionList() { ID = "defsMap" };
            document.Children.Add(defsElement);

            var groupElement = new SvgGroup() { ID = "gMap" };
            document.Children.Add(groupElement);

            var arrowPath = new SvgPath()
            {
                ID = "pathMarkerArrow",
                Fill = new SvgColourServer(Color.Black),
                PathData = SvgPathBuilder.Parse(@"M0,0 L4,2 L0,4 L1,2 z")
            };

            var arrowMarker = new SvgMarker()
            {
                ID = "markerArrow",
                MarkerUnits = SvgMarkerUnits.StrokeWidth,
                MarkerWidth = 5,
                MarkerHeight = 5,
                RefX = 3,
                RefY = 2,
                Orient = new SvgOrient() { IsAuto = true },
                Children = { arrowPath }
            };

            defsElement.Children.Add(arrowMarker);

            var line = new SvgLine()
            {
                ID = "lineLinkedPoint",
                StartX = 0,
                StartY = 15,
                EndX = 35,
                EndY = 35,
                Stroke = new SvgColourServer(Color.Black),
                StrokeWidth = 3,
                MarkerEnd = new Uri(string.Format("url(#{0})", arrowMarker.ID), UriKind.Relative)
            };

            groupElement.Children.Add(line);

            var svgXml = document.GetXML();
            var img = document.Draw();

            var file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            File.WriteAllText(file + ".svg", svgXml);
            img.Save(file + ".png");
            Debug.WriteLine(string.Format("Svg saved to '{0}'", file));

            Debugger.Break();

            // Remove
            var svg = new FileInfo(file + ".svg");
            if (svg.Exists) svg.Delete();
            var png = new FileInfo(file + ".png");
            if (png.Exists) png.Delete();
        }