|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Windows.Forms; |
| 4 | +using SvgNet; |
| 5 | +using SvgNet.SvgElements; |
| 6 | +using SvgNet.SvgTypes; |
| 7 | + |
| 8 | +namespace SvgDocTest { |
| 9 | + public partial class DocForm : Form { |
| 10 | + public DocForm() => InitializeComponent(); |
| 11 | + |
| 12 | + private static readonly string _tempFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "foo.svg"); |
| 13 | + |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// The main entry point for the application. |
| 17 | + /// </summary> |
| 18 | + [STAThread] |
| 19 | + private static void Main() => Application.Run(new DocForm()); |
| 20 | + |
| 21 | + private static void RefreshBrowserFrom(WebBrowser browser, string filename) { |
| 22 | + browser.Navigate(new Uri(filename)); |
| 23 | + browser.Refresh(WebBrowserRefreshOption.Completely); |
| 24 | + } |
| 25 | + |
| 26 | + private void Button1_Click(object sender, EventArgs e) { |
| 27 | + using (var dlg = new OpenFileDialog { |
| 28 | + AutoUpgradeEnabled = true, |
| 29 | + CheckFileExists = true, |
| 30 | + DefaultExt = ".svg", |
| 31 | + Filter = "Scalable Vector Graphics|*.svg", |
| 32 | + Multiselect = false, |
| 33 | + Title = "Choose one Scalable Vector Graphics file" |
| 34 | + }) { |
| 35 | + if (dlg.ShowDialog() == DialogResult.OK) { |
| 36 | + ProcessSvgFile(dlg.FileName); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private void Button2_Click(object sender, EventArgs e) { |
| 42 | + var root = new SvgSvgElement("4in", "4in", "-10,-10 250,250"); |
| 43 | + |
| 44 | + //adding multiple children |
| 45 | + |
| 46 | + root.AddChildren( |
| 47 | + new SvgRectElement(5, 5, 5, 5), |
| 48 | + new SvgEllipseElement(20, 20, 8, 12) { |
| 49 | + Style = "fill:yellow;stroke:red" |
| 50 | + }, |
| 51 | + |
| 52 | + new SvgAElement("https://github.com/managed-commons/SvgNet").AddChildren( |
| 53 | + new SvgTextElement("Textastic!", 30, 20) { |
| 54 | + Style = "fill:midnightblue;stroke:navy;stroke-width:1px;font-size:30px;font-family:Calibri" |
| 55 | + }) |
| 56 | + ); |
| 57 | + |
| 58 | + //group and path |
| 59 | + |
| 60 | + var grp = new SvgGroupElement("green_group") { |
| 61 | + Style = "fill:green;stroke:black;" |
| 62 | + }; |
| 63 | + |
| 64 | + grp.AddChild(new SvgRectElement(30, 30, 5, 20)); |
| 65 | + |
| 66 | + var ell = new SvgEllipseElement { |
| 67 | + CX = 50, |
| 68 | + CY = 50, |
| 69 | + RX = 10, |
| 70 | + RY = 20 |
| 71 | + }; |
| 72 | + |
| 73 | + var pathy = new SvgPathElement { |
| 74 | + D = "M 20,80 C 20,90 30,80 70,100 C 70,100 40,60 50,60 z", |
| 75 | + Style = ell.Style |
| 76 | + }; |
| 77 | + |
| 78 | + root.AddChild(grp); |
| 79 | + |
| 80 | + //cloning and style arithmetic |
| 81 | + |
| 82 | + grp.AddChildren(ell, pathy); |
| 83 | + |
| 84 | + grp.Style.Set("fill", "blue"); |
| 85 | + |
| 86 | + var grp2 = (SvgGroupElement)SvgFactory.CloneElement(grp); |
| 87 | + |
| 88 | + grp2.Id = "cloned_red_group"; |
| 89 | + |
| 90 | + grp2.Style.Set("fill", "red"); |
| 91 | + |
| 92 | + grp2.Style += "opacity:0.5"; |
| 93 | + |
| 94 | + grp2.Transform = "scale (1.2, 1.2) translate(10)"; |
| 95 | + |
| 96 | + root.AddChild(grp2); |
| 97 | + |
| 98 | + //output |
| 99 | + |
| 100 | + string s = root.WriteSVGString(true); |
| 101 | + |
| 102 | + tbOut.Text = s; |
| 103 | + |
| 104 | + string tempFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "foo.svg"); |
| 105 | + |
| 106 | + using (var tw = new StreamWriter(tempFile, false)) |
| 107 | + tw.Write(s); |
| 108 | + |
| 109 | + svgOut.Navigate(new Uri(tempFile)); |
| 110 | + svgOut.Refresh(WebBrowserRefreshOption.Completely); |
| 111 | + } |
| 112 | + |
| 113 | + private void Button3_Click(object sender, EventArgs e) { |
| 114 | + SvgNumList a = "3, 5.6 901 -7 "; |
| 115 | + Assert.Equals(a[3], -7f); |
| 116 | + |
| 117 | + SvgTransformList b = "rotate ( 45 ), translate (11, 10)skewX(3)"; |
| 118 | + Assert.Equals(b[1].Matrix.OffsetX, 11f); |
| 119 | + |
| 120 | + SvgColor c = "rgb( 100%, 100%, 50%)"; |
| 121 | + Assert.Equals(c.Color.B, 0x7f); |
| 122 | + |
| 123 | + SvgColor d = "#abc"; |
| 124 | + Assert.Equals(d.Color.G, 0xbb); |
| 125 | + |
| 126 | + SvgPath f = "M 5,5 L 1.1 -6 , Q 1,3 9,10 z"; |
| 127 | + Assert.Equals(f.Count, 4f); |
| 128 | + Assert.Equals(f[1].Abs, true); |
| 129 | + Assert.Equals(f[2].Data[3], 10f); |
| 130 | + |
| 131 | + MessageBox.Show("Tests completed Ok"); |
| 132 | + } |
| 133 | + private void ProcessSvgFile(string svgFileName) { |
| 134 | + tbIn.Text = svgFileName.LoadText(); |
| 135 | + RefreshBrowserFrom(svgIn, svgFileName); |
| 136 | + tbOut.Text = SvgFactory.LoadFromXML(svgFileName.LoadXml(), null).WriteSVGString(true); |
| 137 | + File.WriteAllText(_tempFileName, tbOut.Text); |
| 138 | + RefreshBrowserFrom(svgOut, _tempFileName); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | +} |
0 commit comments