Skip to content

Commit ae72e22

Browse files
committed
fixed the zero division error when calculating target dimensions (#11)
1 parent 6a6f4a9 commit ae72e22

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

SvgFileType/SvgFileType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SvgFileTypePlugin
1313
{
1414
public class SvgFileType : FileType
1515
{
16-
public SvgFileType() : base(FileTypeName,
16+
public SvgFileType() : base("Scalable Vector Graphics",
1717
new FileTypeOptions
1818
{
1919
LoadExtensions = new []{".svg", ".svgz"},
@@ -23,7 +23,6 @@ public SvgFileType() : base(FileTypeName,
2323
{
2424
}
2525

26-
private const string FileTypeName = "Scalable Vector Graphics";
2726
private const string WindowTitle = "SVG Import Plug-in v1.0.1";
2827

2928
// Don't change this text! It's used by a PSD import plugin to keep Photoshop's folder structure.

SvgFileType/SvgImportDialog.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public string Title
140140
private const string NotAvailable = "n/a";
141141
private const int CanvasSizeWarningThreshold = 1280;
142142
private Size sizeHint;
143+
private object lastChangedCtrl;
143144

144145
#endregion
145146

@@ -153,7 +154,7 @@ public SvgImportDialog()
153154
canvash.KeyUp += Canvas_KeyUp;
154155
canvasw.ValueChanged += CanvasW_ValueChanged;
155156
canvash.ValueChanged += CanvasH_ValueChanged;
156-
cbKeepAR.CheckedChanged += CanvasW_ValueChanged;
157+
cbKeepAR.CheckedChanged += KeepAR_CheckedChanged;
157158
linkLabel1.Click += BtnUseOriginal_Click;
158159
linkGitHub.LinkClicked += LinkGitHub_LinkClicked;
159160
btnOk.Click += BtnOk_Click;
@@ -177,21 +178,43 @@ private void SetDefaults()
177178

178179
private void UpdateCanvasH()
179180
{
181+
decimal newHeight;
180182
if (KeepAspectRatio)
181183
{
182-
canvash.Value = canvasw.Value * sizeHint.Height / sizeHint.Width;
184+
newHeight = canvasw.Value * sizeHint.Height / sizeHint.Width;
185+
}
186+
else
187+
{
188+
newHeight = canvash.Value;
189+
}
190+
191+
if (newHeight < 1)
192+
{
193+
newHeight = canvasw.Minimum;
183194
}
184195

185-
warningBox.Visible = canvash.Value > CanvasSizeWarningThreshold;
196+
canvash.Value = newHeight;
197+
warningBox.Visible = newHeight > CanvasSizeWarningThreshold;
186198
}
187199

188200
private void UpdateCanvasW()
189201
{
202+
decimal newWidth;
190203
if (KeepAspectRatio)
191204
{
192-
canvasw.Value = canvash.Value * sizeHint.Width / sizeHint.Height;
205+
newWidth = canvash.Value * sizeHint.Width / sizeHint.Height;
206+
}
207+
else
208+
{
209+
newWidth = canvasw.Value;
210+
}
211+
212+
if (newWidth < 1)
213+
{
214+
newWidth = canvash.Minimum;
193215
}
194216

217+
canvasw.Value = newWidth;
195218
warningBox.Visible = canvasw.Value > CanvasSizeWarningThreshold;
196219
}
197220

@@ -265,16 +288,32 @@ public void InitSizeHint()
265288

266289
private void CanvasW_ValueChanged(object sender, EventArgs e)
267290
{
291+
lastChangedCtrl = sender;
268292
UpdateCanvasH();
269293
}
270294

271295
private void CanvasH_ValueChanged(object sender, EventArgs e)
272296
{
297+
lastChangedCtrl = sender;
273298
UpdateCanvasW();
274299
}
275300

301+
private void KeepAR_CheckedChanged(object sender, EventArgs e)
302+
{
303+
if (ReferenceEquals(lastChangedCtrl, canvasw))
304+
{
305+
UpdateCanvasH();
306+
}
307+
else
308+
{
309+
UpdateCanvasW();
310+
}
311+
}
312+
276313
private void Canvas_KeyUp(object sender, KeyEventArgs e)
277314
{
315+
lastChangedCtrl = sender;
316+
278317
if (e.KeyValue >= '0' || e.KeyValue <= '9' ||
279318
e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
280319
{

0 commit comments

Comments
 (0)