Skip to content

Commit 83c342f

Browse files
committed
v3.2.0 - Adding 'public string WriteSVGString(float width, float height)' overload to SvgGraphics to simplify usage
v3.1.0 - Adding 'public string WriteSVGString(SizeF bounds)' overload to SvgGraphics to simplify usage
1 parent a4d02ab commit 83c342f

8 files changed

Lines changed: 90 additions & 21 deletions

File tree

SvgDotNetCoreTest/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void Main(string[] args) {
1111
using var ig = new SvgGraphics(Color.WhiteSmoke);
1212
Console.WriteLine($"=== Renderer {pair.Key}");
1313
pair.Value(ig);
14-
Console.WriteLine(ig.WriteSVGString());
14+
Console.WriteLine(ig.WriteSVGString(640, 480));
1515
}
1616
}
1717
}

SvgNet/Elements/SvgElement.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,17 @@ public virtual void ReadXmlElement(XmlDocument doc, XmlElement el) {
121121
/// <returns></returns>
122122
/// <param name="compressAttributes">Should usually be set true. Causes the XML output to be optimized so that
123123
/// long attributes like styles and transformations are represented with entities.</param>
124-
public string WriteSVGString(bool compressAttributes) {
124+
public string WriteSVGString(bool compressAttributes) =>
125+
WriteSVGString(compressAttributes, null);
126+
127+
/// <summary>
128+
/// Get a string that contains a complete SVG document. XML version, DOCTYPE etc are included.
129+
/// </summary>
130+
/// <returns></returns>
131+
/// <param name="compressAttributes">Should usually be set true. Causes the XML output to be optimized so that
132+
/// long attributes like styles and transformations are represented with entities.</param>
133+
/// <param name="bounds">Width/Height values to add as attributes to the svg element</param>
134+
public string WriteSVGString(bool compressAttributes, SizeF? bounds) {
125135
var doc = new XmlDocument();
126136

127137
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, "yes");
@@ -132,17 +142,18 @@ public string WriteSVGString(bool compressAttributes) {
132142

133143
doc.DocumentElement.SetAttribute("xmlns", svgNamespaceURI);
134144
doc.DocumentElement.SetAttribute("xmlns:xlink", xlinkNamespaceURI);
135-
136145
string ents = string.Empty;
137146
if (compressAttributes)
138147
ents = SvgFactory.CompressXML(doc, doc.DocumentElement);
139-
148+
if (bounds != null) {
149+
doc.DocumentElement.SetAttribute("width", bounds.Value.Width.ToInvariantString());
150+
doc.DocumentElement.SetAttribute("height", bounds.Value.Height.ToInvariantString());
151+
}
140152
doc.XmlResolver = new DummyXmlResolver();
141153
_ = doc.InsertAfter(
142154
doc.CreateDocumentType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd", ents),
143155
declaration
144156
);
145-
146157
return ToXmlString(doc);
147158
}
148159

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Copyright © 2003 RiskCare Ltd. All rights reserved.
3+
Copyright © 2010 SvgNet & SvgGdi Bridge Project. All rights reserved.
4+
Copyright © 2015-2022 Rafael Teixeira, Mojmír Němeček, Benjamin Peterson and Other Contributors
5+
6+
Original source code licensed with BSD-2-Clause spirit, treat it thus, see accompanied LICENSE for more
7+
*/
8+
9+
namespace System;
10+
11+
public static class FloatExtensions {
12+
public static string ToInvariantString(this float value) =>
13+
value.ToString(CultureInfo.InvariantCulture);
14+
15+
}
16+

SvgNet/Extensions/StringExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ public void Deconstruct(out bool isPrefixed, out string tail) {
8787
tail = Tail;
8888
}
8989
}
90+

SvgNet/ImplementedGraphics/SVGGraphics.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ public SvgGraphics(Color backgroundColor) {
5353
_transforms = new MatrixStack();
5454
}
5555

56+
/// <summary>
57+
/// Get a string containing an SVG document. The very heart of SvgGdi. It calls <c>WriteSVGString</c> on the <see cref="SvgElement"/>
58+
/// at the root of this <c>SvgGraphics</c> and returns the resulting string.
59+
/// </summary>
60+
public string WriteSVGString() => _root.WriteSVGString(true);
61+
62+
/// <summary>
63+
/// Get a string containing an SVG document. The very heart of SvgGdi. It calls <c>WriteSVGString</c> on the <see cref="SvgElement"/>
64+
/// at the root of this <c>SvgGraphics</c> and returns the resulting string.
65+
/// </summary>
66+
/// <param name="bounds">Width/Height values to add as attributes to the svg element</param>
67+
public string WriteSVGString(SizeF bounds) => _root.WriteSVGString(true, bounds);
68+
69+
/// <summary>
70+
/// Get a string containing an SVG document. The very heart of SvgGdi. It calls <c>WriteSVGString</c> on the <see cref="SvgElement"/>
71+
/// at the root of this <c>SvgGraphics</c> and returns the resulting string.
72+
/// </summary>
73+
/// <param name="width">Width value to add as attribute to the svg element</param>
74+
/// <param name="height">Height value to add as attribute to the svg element</param>
75+
public string WriteSVGString(float width, float height) => _root.WriteSVGString(true, new SizeF(width, height));
76+
5677
/// <summary>
5778
/// Not implemented.
5879
/// </summary>
@@ -1365,12 +1386,6 @@ public void SetClip(RectangleF rect) {
13651386
/// </summary>
13661387
public void TranslateTransform(float dx, float dy, MatrixOrder order) => _transforms.Top.Translate(dx, dy, order);
13671388

1368-
/// <summary>
1369-
/// Get a string containing an SVG document. The very heart of SvgGdi. It calls <c>WriteSVGString</c> on the <see cref="SvgElement"/>
1370-
/// at the root of this <c>SvgGraphics</c> and returns the resulting string.
1371-
/// </summary>
1372-
public string WriteSVGString() => _root.WriteSVGString(true);
1373-
13741389
//a default graphics so that we can make a guess as to functions like MeasureString
13751390
private static Graphics _g;
13761391
private readonly SvgRectElement _bg;
@@ -1753,11 +1768,13 @@ private static string GDIArc2SVGPath(float x, float y, float width, float height
17531768

17541769
sweepAngle += startAngle;
17551770

1771+
#pragma warning disable IDE0180
17561772
if (sweepAngle > startAngle) {
1757-
var temp = startAngle;
1773+
float temp = startAngle;
17581774
startAngle = sweepAngle;
17591775
sweepAngle = temp;
17601776
}
1777+
#pragma warning restore IDE0180
17611778

17621779
if (sweepAngle - startAngle > Math.PI || startAngle - sweepAngle > Math.PI) longArc = 1;
17631780

SvgNet/SvgNet.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<LangVersion>10.0</LangVersion>
88
<AssemblyName>SVG</AssemblyName>
99
<PackageId>SvgNet</PackageId>
10-
<Version>3.0.1</Version>
10+
<Version>3.2.0</Version>
1111
<RootNamespace>SvgNet</RootNamespace>
1212
<DocumentationFile>svgnetdoc.xml</DocumentationFile>
1313
<NoWarn>CS1591</NoWarn>
@@ -23,7 +23,7 @@
2323
<Product>SvgNet</Product>
2424
<RepositoryType>git</RepositoryType>
2525
<PackageReleaseNotes>
26-
Dropping tuples dependency
26+
Adding 'public string WriteSVGString(float width, float height)' overload to SvgGraphics to simplify usage
2727
</PackageReleaseNotes>
2828
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2929
<ImplicitUsings>true</ImplicitUsings>

SvgNet/svgnetdoc.xml

Lines changed: 30 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SvgNetUnitTests/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void TestCases(string key) {
3232
System.Action<IGraphics> value = TestShared.Renderers[key];
3333
using var ig = new SvgGraphics(Color.WhiteSmoke);
3434
value(ig);
35-
string svgBody = ig.WriteSVGString();
35+
string svgBody = ig.WriteSVGString(640, 480);
3636
key = key.Replace("/", "."); // Arcs/Pies is not file friendly
3737
string dstPath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, $"{TestContext.CurrentContext.Test.ID}.{key}.svg");
3838
System.IO.File.WriteAllText(dstPath, svgBody);

0 commit comments

Comments
 (0)