@@ -14,7 +14,7 @@ public class SvgType : FileType
14
14
{
15
15
public SvgType ( )
16
16
: base (
17
- "Scalable Vector Graphics" ,
17
+ "Scalable Vector Graphics" ,
18
18
FileTypeFlags . SupportsLoading ,
19
19
new [ ] { ".svg" , ".svgz" } )
20
20
{
@@ -35,6 +35,8 @@ private static Form GetMainForm()
35
35
36
36
private static bool IsCompressed ( Stream input )
37
37
{
38
+ if ( input . Length < 3 )
39
+ return false ;
38
40
var headerBytes = new byte [ 3 ] ;
39
41
input . Read ( headerBytes , 0 , 3 ) ;
40
42
input . Position = 0 ;
@@ -43,57 +45,67 @@ private static bool IsCompressed(Stream input)
43
45
44
46
protected override Document OnLoad ( Stream input )
45
47
{
46
- if ( IsCompressed ( input ) )
48
+ if ( IsCompressed ( input ) )
47
49
input = new GZipStream ( input , CompressionMode . Decompress ) ;
50
+
51
+ SvgDocument doc ;
48
52
using ( input )
49
53
{
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
+ {
55
76
Form mainForm = GetMainForm ( ) ;
56
- var dr = DialogResult . Cancel ;
57
- using ( var dialog = new UIDialog ( ) )
77
+ if ( mainForm != null )
58
78
{
59
79
mainForm . Invoke ( ( MethodInvoker ) ( ( ) =>
60
80
{
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 ;
73
81
dialog . SetSvgInfo ( vpw , vph , vbx , vby , vbw , vbh ) ;
74
82
dr = dialog . ShowDialog ( mainForm ) ;
75
83
} ) ) ;
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 ;
82
84
}
83
- var bmp = new Bitmap ( canvasw , canvash ) ;
84
- using ( Graphics graph = Graphics . FromImage ( bmp ) )
85
- using ( ISvgRenderer renderer = SvgRenderer . FromGraphics ( graph ) )
85
+ else
86
86
{
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 ( ) ;
94
89
}
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 ) ;
96
107
}
108
+ return Document . FromImage ( bmp ) ;
97
109
}
98
110
}
99
111
}
0 commit comments