Skip to content

Commit 63e6a48

Browse files
committed
minor fixes and cleanup
1 parent a4b5b90 commit 63e6a48

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

SVGType/SvgType.cs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class SvgType : FileType
1414
{
1515
public SvgType()
1616
: base(
17-
"Scalable Vector Graphics",
17+
"Scalable Vector Graphics",
1818
FileTypeFlags.SupportsLoading,
1919
new[] {".svg", ".svgz"})
2020
{
@@ -35,6 +35,8 @@ private static Form GetMainForm()
3535

3636
private static bool IsCompressed(Stream input)
3737
{
38+
if (input.Length < 3)
39+
return false;
3840
var headerBytes = new byte[3];
3941
input.Read(headerBytes, 0, 3);
4042
input.Position = 0;
@@ -43,57 +45,67 @@ private static bool IsCompressed(Stream input)
4345

4446
protected override Document OnLoad(Stream input)
4547
{
46-
if(IsCompressed(input))
48+
if (IsCompressed(input))
4749
input = new GZipStream(input, CompressionMode.Decompress);
50+
51+
SvgDocument doc;
4852
using (input)
4953
{
50-
var doc = SvgDocument.Open<SvgDocument>(input);
51-
bool keepAspectRatio;
52-
int resolution;
53-
int canvasw;
54-
int canvash;
54+
doc = SvgDocument.Open<SvgDocument>(input);
55+
}
56+
57+
bool keepAspectRatio;
58+
int resolution;
59+
int canvasw;
60+
int canvash;
61+
var vpw = 0;
62+
var vph = 0;
63+
if (!doc.Width.IsNone && !doc.Width.IsEmpty &&
64+
doc.Width.Type != SvgUnitType.Percentage)
65+
vpw = (int) doc.Width.Value;
66+
if (!doc.Height.IsNone && !doc.Height.IsEmpty &&
67+
doc.Height.Type != SvgUnitType.Percentage)
68+
vph = (int) doc.Height.Value;
69+
var vbx = (int) doc.ViewBox.MinX;
70+
var vby = (int) doc.ViewBox.MinY;
71+
var vbw = (int) doc.ViewBox.Width;
72+
var vbh = (int) doc.ViewBox.Height;
73+
DialogResult dr = DialogResult.Cancel;
74+
using (var dialog = new UIDialog())
75+
{
5576
Form mainForm = GetMainForm();
56-
var dr = DialogResult.Cancel;
57-
using (var dialog = new UIDialog())
77+
if (mainForm != null)
5878
{
5979
mainForm.Invoke((MethodInvoker) (() =>
6080
{
61-
int vpw = 0;
62-
int vph = 0;
63-
if (!doc.Width.IsNone && !doc.Width.IsEmpty &&
64-
doc.Width.Type != SvgUnitType.Percentage)
65-
vpw = (int) doc.Width.Value;
66-
if (!doc.Height.IsNone && !doc.Height.IsEmpty &&
67-
doc.Height.Type != SvgUnitType.Percentage)
68-
vph = (int) doc.Height.Value;
69-
var vbx = (int) doc.ViewBox.MinX;
70-
var vby = (int) doc.ViewBox.MinY;
71-
var vbw = (int) doc.ViewBox.Width;
72-
var vbh = (int) doc.ViewBox.Height;
7381
dialog.SetSvgInfo(vpw, vph, vbx, vby, vbw, vbh);
7482
dr = dialog.ShowDialog(mainForm);
7583
}));
76-
if (dr != DialogResult.OK)
77-
throw new OperationCanceledException("Cancelled by user");
78-
canvasw = dialog.CanvasW;
79-
canvash = dialog.CanvasH;
80-
resolution = dialog.Dpi;
81-
keepAspectRatio = dialog.KeepAspectRatio;
8284
}
83-
var bmp = new Bitmap(canvasw, canvash);
84-
using (Graphics graph = Graphics.FromImage(bmp))
85-
using (ISvgRenderer renderer = SvgRenderer.FromGraphics(graph))
85+
else
8686
{
87-
doc.Ppi = resolution;
88-
doc.Width = new SvgUnit(SvgUnitType.Pixel, canvasw);
89-
doc.Height = new SvgUnit(SvgUnitType.Pixel, canvash);
90-
doc.AspectRatio = keepAspectRatio
91-
? new SvgAspectRatio(SvgPreserveAspectRatio.xMinYMin)
92-
: new SvgAspectRatio(SvgPreserveAspectRatio.none);
93-
doc.Draw(renderer);
87+
dialog.SetSvgInfo(vpw, vph, vbx, vby, vbw, vbh);
88+
dr = dialog.ShowDialog();
9489
}
95-
return Document.FromImage(bmp);
90+
if (dr != DialogResult.OK)
91+
throw new OperationCanceledException("Cancelled by user");
92+
canvasw = dialog.CanvasW;
93+
canvash = dialog.CanvasH;
94+
resolution = dialog.Dpi;
95+
keepAspectRatio = dialog.KeepAspectRatio;
96+
}
97+
var bmp = new Bitmap(canvasw, canvash);
98+
using (Graphics graph = Graphics.FromImage(bmp))
99+
{
100+
doc.Ppi = resolution;
101+
doc.Width = new SvgUnit(SvgUnitType.Pixel, canvasw);
102+
doc.Height = new SvgUnit(SvgUnitType.Pixel, canvash);
103+
doc.AspectRatio = keepAspectRatio
104+
? new SvgAspectRatio(SvgPreserveAspectRatio.xMinYMin)
105+
: new SvgAspectRatio(SvgPreserveAspectRatio.none);
106+
doc.Draw(graph);
96107
}
108+
return Document.FromImage(bmp);
97109
}
98110
}
99111
}

0 commit comments

Comments
 (0)