Skip to content

Commit b77474d

Browse files
committed
more refactoring. viewbox x, y values are now displaying correctly.
1 parent 0d53954 commit b77474d

File tree

3 files changed

+109
-111
lines changed

3 files changed

+109
-111
lines changed

SvgFileType/SvgFileType.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class SvgFileType : FileType
1616
public SvgFileType() : base(FileTypeName,
1717
new FileTypeOptions
1818
{
19-
LoadExtensions = SupportedExtensions,
19+
LoadExtensions = new []{".svg", ".svgz"},
2020
SupportsCancellation = true,
2121
SupportsLayers = true
2222
})
@@ -25,9 +25,9 @@ public SvgFileType() : base(FileTypeName,
2525

2626
private const string FileTypeName = "Scalable Vector Graphics";
2727
private const string WindowTitle = "SVG Import Plug-in v1.0.1";
28-
private static readonly string[] SupportedExtensions = {".svg", ".svgz"};
2928

3029
// Don't change this text! It's used by a PSD import plugin to keep Photoshop's folder structure.
30+
// https://forums.getpaint.net/topic/113742-photoshop-psd-file-plugin-with-layers-support/
3131
public const string LayerGroupBegin = "Layer Group: {0}";
3232
public const string LayerGroupEnd = "End Layer Group: {0}";
3333

@@ -54,9 +54,9 @@ protected override Document OnLoad(Stream input)
5454
using (dialog = new SvgImportDialog())
5555
{
5656
dialog.Title = WindowTitle;
57+
dialog.SourceDpi = ppi;
5758
dialog.ViewportW = viewportW;
5859
dialog.ViewportH = viewportH;
59-
dialog.SourceDpi = ppi;
6060
dialog.ViewBoxX = viewBoxX;
6161
dialog.ViewBoxY = viewBoxY;
6262
dialog.ViewBoxW = viewBoxW;
@@ -113,7 +113,6 @@ private void Dialog_FormClosing(object sender, FormClosingEventArgs e)
113113

114114
private void Dialog_FormClosed(object sender, FormClosedEventArgs e)
115115
{
116-
cts = null;
117116
svg = null;
118117
GC.Collect();
119118
}
@@ -150,8 +149,6 @@ private Document DoImport()
150149
: new SvgAspectRatio(SvgPreserveAspectRatio.none);
151150

152151
LayersMode layersMode = dialog.LayersMode;
153-
int canvasW = dialog.CanvasW;
154-
int canvasH = dialog.CanvasH;
155152
bool importGroupBoundariesAsLayers = dialog.ImportGroupBoundariesAsLayers;
156153
bool setOpacityForLayer = dialog.ImportOpacity;
157154
bool importHiddenLayers = dialog.ImportHiddenLayers;
@@ -160,7 +157,7 @@ private Document DoImport()
160157
// Render one flat image and quit.
161158
if (layersMode == LayersMode.Flat)
162159
{
163-
using (Bitmap bmp = RenderFlatImage(svg, canvasW, canvasH))
160+
using (Bitmap bmp = RenderSvgDocument())
164161
{
165162
return Document.FromImage(bmp);
166163
}
@@ -246,7 +243,8 @@ private Document DoImport()
246243
}
247244
}
248245

249-
if (ShowMemoryWarningDialog(groupsAndElementsWithoutGroup.Count) != DialogResult.Yes)
246+
if (groupsAndElementsWithoutGroup.Count > LayerCountWarningThreshold &&
247+
ShowMemoryWarningDialog(groupsAndElementsWithoutGroup.Count) != DialogResult.Yes)
250248
{
251249
dialog.DialogResult = DialogResult.Cancel;
252250
return null;
@@ -264,7 +262,7 @@ private Document DoImport()
264262
if (pdnDocument == null || pdnDocument.Layers.Count == 0)
265263
{
266264
pdnDocument?.Dispose();
267-
using (Bitmap bmp = RenderFlatImage(svg, canvasW, canvasH))
265+
using (Bitmap bmp = RenderSvgDocument())
268266
{
269267
return Document.FromImage(bmp);
270268
}
@@ -341,7 +339,7 @@ private Document RenderElements(IReadOnlyCollection<SvgVisualElement> elements,
341339

342340
pdnDocument = pdnDocument ?? new Document(width, height);
343341
// Render empty group boundary and continue
344-
var pdnLayer = new BitmapLayer(pdnDocument.Width, pdnDocument.Height)
342+
var pdnLayer = new BitmapLayer(width, height)
345343
{
346344
Name = boundaryNode.ID,
347345
Opacity = (byte) (boundaryNode.RelatedGroup.Opacity * 255),
@@ -400,7 +398,7 @@ private Document RenderElements(IReadOnlyCollection<SvgVisualElement> elements,
400398
private void RenderElement(SvgElement element, bool setOpacityForLayer,
401399
bool importHiddenLayers)
402400
{
403-
var opacity = element.Opacity;
401+
float opacity = element.Opacity;
404402
var visible = true;
405403
var visualElement = element as SvgVisualElement;
406404
if (visualElement != null)
@@ -431,7 +429,7 @@ private void RenderElement(SvgElement element, bool setOpacityForLayer,
431429

432430
BitmapLayer pdnLayer;
433431
pdnDocument = pdnDocument ?? new Document(width, height);
434-
using (Bitmap bmp = RenderFlatImage(element.OwnerDocument, pdnDocument.Width, pdnDocument.Height))
432+
using (Bitmap bmp = RenderSvgDocument())
435433
using (Surface surface = Surface.CopyFromBitmap(bmp))
436434
{
437435
pdnLayer = new BitmapLayer(surface);
@@ -464,12 +462,12 @@ private static bool IsVisibleOriginally(SvgElement element)
464462
return true;
465463
}
466464

467-
private static Bitmap RenderFlatImage(SvgDocument doc, int canvasw, int canvash)
465+
private Bitmap RenderSvgDocument()
468466
{
469-
var bmp = new Bitmap(canvasw, canvash);
467+
var bmp = new Bitmap(width, height);
470468
using (Graphics graph = Graphics.FromImage(bmp))
471469
{
472-
doc.Draw(graph);
470+
svg.Draw(graph);
473471
}
474472

475473
return bmp;
@@ -568,13 +566,16 @@ private static IEnumerable<SvgElement> PrepareFlatElements(SvgElementCollection
568566

569567
if (toRender is SvgVisualElement visual)
570568
{
569+
const string hidden = "hidden";
570+
const string none = "none";
571+
571572
// Fix problem that SVG visual element lib style "display:none" is not recognized as visible state.
572573
if (visual.Visible &&
573-
(visual.Display?.Trim().Equals("none", StringComparison.OrdinalIgnoreCase) == true ||
574-
visual.Display?.Trim().Equals("hidden", StringComparison.OrdinalIgnoreCase) == true))
574+
(visual.Display?.Trim().Equals(none, StringComparison.OrdinalIgnoreCase) == true ||
575+
visual.Display?.Trim().Equals(hidden, StringComparison.OrdinalIgnoreCase) == true))
575576
{
576-
visual.Visibility = "hidden";
577-
visual.Display = "none";
577+
visual.Visibility = hidden;
578+
visual.Display = none;
578579
}
579580

580581
// Store opacity

SvgFileType/SvgImportDialog.cs

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,40 @@ public int CanvasH
3737
}
3838
}
3939

40-
public int ViewportW
40+
public int? ViewportW
4141
{
42-
get => Int32.TryParse(vpw.Text, out int val) ? val : -1;
43-
set => vpw.Text = value <= 0 ? NotAvailable : value.ToString();
42+
get => Int32.TryParse(vpw.Text, out int val) ? (int?)val : null;
43+
set => vpw.Text = value > 0 ? value.ToString() : NotAvailable;
4444
}
4545

46-
public int ViewportH
46+
public int? ViewportH
4747
{
48-
get => Int32.TryParse(vph.Text, out int val) ? val : -1;
49-
set => vph.Text = value <= 0 ? NotAvailable : value.ToString();
48+
get => Int32.TryParse(vph.Text, out int val) ? (int?)val : null;
49+
set => vph.Text = value > 0 ? value.ToString() : NotAvailable;
5050
}
5151

52-
public int ViewBoxW
52+
public int? ViewBoxW
5353
{
54-
get => Int32.TryParse(vbw.Text, out int val) ? val : -1;
55-
set => vbw.Text = value <= 0 ? NotAvailable : value.ToString();
54+
get => Int32.TryParse(vbw.Text, out int val) ? (int?)val : null;
55+
set => vbw.Text = value > 0 ? value.ToString() : NotAvailable;
5656
}
5757

58-
public int ViewBoxH
58+
public int? ViewBoxH
5959
{
60-
get => Int32.TryParse(vbh.Text, out int val) ? val : -1;
61-
set => vbh.Text = value <= 0 ? NotAvailable : value.ToString();
60+
get => Int32.TryParse(vbh.Text, out int val) ? (int?)val : null;
61+
set => vbh.Text = value > 0 ? value.ToString() : NotAvailable;
6262
}
6363

64-
public int ViewBoxX
64+
public int? ViewBoxX
6565
{
66-
get => Int32.TryParse(vbx.Text, out int val) ? val : -1;
67-
set => vbx.Text = value < 0 ? NotAvailable : value.ToString();
66+
get => Int32.TryParse(vbx.Text, out int val) ? (int?) val : null;
67+
set => vbx.Text = value.HasValue ? value.ToString() : NotAvailable;
6868
}
6969

70-
public int ViewBoxY
70+
public int? ViewBoxY
7171
{
72-
get => Int32.TryParse(vby.Text, out int val) ? val : -1;
73-
set => vby.Text = value < 0 ? NotAvailable : value.ToString();
72+
get => Int32.TryParse(vby.Text, out int val) ? (int?) val : null;
73+
set => vby.Text = value.HasValue ? value.ToString() : NotAvailable;
7474
}
7575

7676
public bool KeepAspectRatio
@@ -150,7 +150,6 @@ public SvgImportDialog()
150150
rbFlat.CheckedChanged += Rb_CheckedChanged;
151151
rbGroups.CheckedChanged += Rb_CheckedChanged;
152152
canvasw.KeyUp += Canvas_KeyUp;
153-
;
154153
canvash.KeyUp += Canvas_KeyUp;
155154
canvasw.ValueChanged += CanvasW_ValueChanged;
156155
canvash.ValueChanged += CanvasH_ValueChanged;
@@ -170,54 +169,10 @@ private void SetDefaults()
170169
ImportOpacity = true;
171170
ImportHiddenLayers = true;
172171
ImportGroupBoundariesAsLayers = true;
173-
InitSizeHint();
174172
canvasw.Minimum = 1;
175173
canvash.Minimum = 1;
176174
cbKeepAR.Checked = true;
177-
}
178-
179-
public void InitSizeHint()
180-
{
181-
int w, h;
182-
183-
if (ViewportW > 0 && ViewportH > 0)
184-
{
185-
w = ViewportW;
186-
h = ViewportH;
187-
}
188-
else if (ViewBoxW > 0 && ViewBoxH > 0)
189-
{
190-
w = ViewBoxW;
191-
h = ViewBoxH;
192-
}
193-
else
194-
{
195-
w = h = 512;
196-
}
197-
198-
double ratio = w / (double) h;
199-
if (w > CanvasSizeWarningThreshold)
200-
{
201-
if (w > h)
202-
{
203-
w = CanvasSizeWarningThreshold;
204-
h = (int) Math.Ceiling(CanvasSizeWarningThreshold / ratio);
205-
}
206-
else
207-
{
208-
h = CanvasSizeWarningThreshold;
209-
w = (int) Math.Ceiling(CanvasSizeWarningThreshold * ratio);
210-
}
211-
}
212-
else if (h > CanvasSizeWarningThreshold)
213-
{
214-
h = CanvasSizeWarningThreshold;
215-
w = (int) Math.Ceiling(CanvasSizeWarningThreshold * ratio);
216-
}
217-
218-
sizeHint = new Size(w, h);
219-
UpdateCanvasW();
220-
UpdateCanvasH();
175+
InitSizeHint();
221176
}
222177

223178
private void UpdateCanvasH()
@@ -246,7 +201,7 @@ private void UpdateOptionsAvail()
246201
cbPSDSupport.Enabled = rbAll.Checked;
247202
}
248203

249-
public void SetValuesGivenInSource()
204+
private void SetValuesGivenInSource()
250205
{
251206
// Keep original image size and show warning
252207
CanvasW = sizeHint.Width;
@@ -258,6 +213,54 @@ public void SetValuesGivenInSource()
258213

259214
#endregion
260215

216+
#region Public
217+
218+
public void InitSizeHint()
219+
{
220+
int w = 512;
221+
int h = 512;
222+
223+
if (ViewBoxW > 0 && ViewBoxH > 0)
224+
{
225+
w = ViewBoxW.Value;
226+
h = ViewBoxH.Value;
227+
}
228+
else if (ViewportW > 0 && ViewportH > 0)
229+
{
230+
w = ViewportW.Value;
231+
h = ViewportH.Value;
232+
}
233+
234+
sizeHint = new Size(w, h);
235+
Size sizeHintOriginal = sizeHint;
236+
double ratio = w / (double) h;
237+
if (w > CanvasSizeWarningThreshold)
238+
{
239+
if (w > h)
240+
{
241+
w = CanvasSizeWarningThreshold;
242+
h = (int) Math.Ceiling(CanvasSizeWarningThreshold / ratio);
243+
}
244+
else
245+
{
246+
h = CanvasSizeWarningThreshold;
247+
w = (int) Math.Ceiling(CanvasSizeWarningThreshold * ratio);
248+
}
249+
}
250+
else if (h > CanvasSizeWarningThreshold)
251+
{
252+
h = CanvasSizeWarningThreshold;
253+
w = (int) Math.Ceiling(CanvasSizeWarningThreshold * ratio);
254+
}
255+
256+
sizeHint = new Size(w, h);
257+
UpdateCanvasW();
258+
UpdateCanvasH();
259+
sizeHint = sizeHintOriginal;
260+
}
261+
262+
#endregion
263+
261264
#region Events
262265

263266
private void CanvasW_ValueChanged(object sender, EventArgs e)

SvgFileType/Utils.cs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,32 @@ public static Form GetMainForm()
2020
}
2121
}
2222

23-
public static int ConvertToPixels(SvgUnitType type, float value, float ppi)
23+
public static int ConvertToPixels(SvgUnitType unit, float value, float ppi)
2424
{
25-
const double defaultRatioFor96 = 3.78;
25+
const float defaultRatioFor96 = 3.78f;
2626
var convertationRatio = ppi / 96 * defaultRatioFor96;
27-
28-
if (type == SvgUnitType.Millimeter)
29-
{
30-
return (int)Math.Ceiling(value * convertationRatio);
31-
}
32-
33-
if (type == SvgUnitType.Centimeter)
34-
{
35-
return (int)Math.Ceiling(value * 10 * convertationRatio);
36-
}
37-
38-
if (type == SvgUnitType.Inch)
39-
{
40-
return (int)Math.Ceiling(value * 25.4 * convertationRatio);
41-
}
42-
43-
if (type == SvgUnitType.Em || type == SvgUnitType.Pica)
44-
{
45-
// Default 1 em for 16 pixels.
46-
return (int)Math.Ceiling(value * 16);
47-
}
48-
49-
if (type != SvgUnitType.Percentage)
27+
float pixels;
28+
switch (unit)
5029
{
51-
return (int)Math.Ceiling(value);
30+
case SvgUnitType.Millimeter:
31+
pixels = value * convertationRatio;
32+
break;
33+
case SvgUnitType.Centimeter:
34+
pixels = value * convertationRatio * 10;
35+
break;
36+
case SvgUnitType.Inch:
37+
pixels = value * convertationRatio * 25.4f;
38+
break;
39+
case SvgUnitType.Em:
40+
case SvgUnitType.Pica:
41+
pixels = value * 16;
42+
break;
43+
default:
44+
pixels = 0;
45+
break;
5246
}
5347

54-
return 0;
48+
return (int) Math.Ceiling(pixels);
5549
}
5650
}
5751
}

0 commit comments

Comments
 (0)