Skip to content

Commit 929e29a

Browse files
committed
code cleanup & bug fixes
1 parent 181178d commit 929e29a

26 files changed

+584
-410
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,5 @@ ModelManifest.xml
249249
/SvgFileTypePlugin.zip
250250
/setup.nsi
251251
/CreateZippedRelease.bat
252-
/header.bmp
252+
/header.bmp
253+
/SvgFileType/Properties
Binary file not shown.
-730 KB
Binary file not shown.

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
This is a Paint.NET filetype plugin for loading SVG (Scalable Vector Graphics) and its compressed variant SVGZ files.
77
SVG elements can be rendered as a flat image file or each on a separate layer.
88

9-
The plugin is a tiny wrapper around the [SVG.NET Library](https://github.com/vvvv/SVG) which does the actual SVG reading.
9+
The plugin is a wrapper around the [SVG.NET Library](https://github.com/vvvv/SVG) which does the actual SVG reading.
1010

11-
Tested on Paint.NET 4.2.16 & 4.3 Beta Build 7929.
11+
Tested on Paint.NET v4.2.16 & Paint.NET v4.3.2.
1212

1313
### Download links
1414

@@ -20,8 +20,8 @@ Here are the download links for latest release:
2020
<th>Manual Installation</th>
2121
</tr>
2222
<tr>
23-
<td><a href="https://github.com/otuncelli/Scalable-Vector-Graphics-Plugin-for-Paint.NET/releases/latest/download/SvgFileTypePlugin_setup.exe">SvgFileTypePlugin_setup.exe</a> (399 KiB)</td>
24-
<td><a href="https://github.com/otuncelli/Scalable-Vector-Graphics-Plugin-for-Paint.NET/releases/latest/download/SvgFileTypePlugin.zip">SvgFileTypePlugin.zip</a> (274 KiB)</td>
23+
<td><a href="https://github.com/otuncelli/Scalable-Vector-Graphics-Plugin-for-Paint.NET/releases/latest/download/SvgFileTypePlugin_setup.exe">SvgFileTypePlugin_setup.exe</a> (374 KiB)</td>
24+
<td><a href="https://github.com/otuncelli/Scalable-Vector-Graphics-Plugin-for-Paint.NET/releases/latest/download/SvgFileTypePlugin.zip">SvgFileTypePlugin.zip</a> (267 KiB)</td>
2525
</tr>
2626
</table>
2727

@@ -37,13 +37,12 @@ To manually install the plugin perform the following steps:
3737
* Download and extract `SvgFileTypePlugin.zip`
3838
* If you're using Paint.NET 4.3 or later:
3939
* If you're using Classic version of Paint.NET:
40-
* Create a new folder named `SvgFileTypePlugin` in the `<Paint.NET>\FileTypes` directory (default location is `C:\Program Files\paint.net\FileTypes`).
40+
* Copy _the `SvgFileTypePlugin` folder with its contents_ to the `<Paint.NET>\FileTypes` directory (default location is `C:\Program Files\paint.net\FileTypes`).
4141
* If you're using [Microsoft Store version of Paint.NET](https://www.microsoft.com/store/apps/9NBHCS1LX4R0):
42-
* Create a new folder named `SvgFileTypePlugin` in the `<Documents>\paint.net App Files\FileTypes` directory.
43-
* Put the extracted files in this newly created folder.
42+
* Copy _the `SvgFileTypePlugin` folder with its contents_ to the `<Documents>\paint.net App Files\FileTypes` directory.
4443
* If you're using Paint.NET 4.2:
4544
* If you're using Classic version of Paint.NET:
46-
* Put the extracted files in the `<Paint.NET>\FileTypes` directory (default location is `C:\Program Files\paint.net\FileTypes`).
45+
* Copy _the contents of `SvgFileTypePlugin` folder_ to the `<Paint.NET>\FileTypes` directory (default location is `C:\Program Files\paint.net\FileTypes`).
4746
* If you're using [Microsoft Store version of Paint.NET](https://www.microsoft.com/store/apps/9NBHCS1LX4R0):
48-
* Put the extracted files in the `<Documents>\paint.net App Files\FileTypes` directory.
47+
* Copy _the contents of `SvgFileTypePlugin` folder_ to the `<Documents>\paint.net App Files\FileTypes` directory.
4948
* Restart Paint.NET.

SvgFileType/SvgUnitExtensions.cs renamed to SvgFileType/Extensions/SvgUnitExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@
44

55
using Svg;
66
using System;
7+
using System.Drawing;
78

89
namespace SvgFileTypePlugin
910
{
1011
internal static class SvgUnitExtensions
1112
{
12-
public static int ToPixels(this SvgUnit unit, SvgElement owner)
13+
public static int ToDeviceValue(this SvgUnit unit, SvgElement owner)
1314
{
14-
if (owner == null) throw new ArgumentNullException(nameof(owner));
15+
if (owner == null)
16+
{
17+
throw new ArgumentNullException(nameof(owner));
18+
}
19+
1520
using (var renderer = SvgRenderer.FromNull())
1621
{
1722
return (int)Math.Round(unit.ToDeviceValue(renderer, UnitRenderingType.Other, owner));
1823
}
1924
}
25+
26+
//public static SizeF GetDeviceDimensions(this SvgDocument svg)
27+
//{
28+
// return new SizeF(svg.Width.ToDeviceValue(svg), svg.Height.ToDeviceValue(svg));
29+
//}
2030
}
2131
}

SvgFileType/FodyWeavers.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
3-
<ILMerge IncludeAssemblies="Svg|Fizzler" NamespacePrefix="SvgFileTypePlugin$" FullImport="true" />
3+
<ILMerge IncludeAssemblies="Fizzler|Svg" NamespacePrefix="SvgFileTypePlugin$" FullImport="false" />
44
</Weavers>

SvgFileType/Import/ButtonHelper.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2021 Osman Tunçelli. All rights reserved.
2+
// Use of this source code is governed by a LGPL license that can be
3+
// found in the COPYING file.
4+
5+
using System.Drawing;
6+
7+
namespace SvgFileTypePlugin.Import
8+
{
9+
internal static class ButtonHelper
10+
{
11+
public static void DrawText(Graphics g, Rectangle rectangle, Font font, string text, bool enabled, Color color)
12+
{
13+
//Create string format
14+
using (var sf = new StringFormat())
15+
{
16+
sf.Alignment = StringAlignment.Near;
17+
sf.LineAlignment = StringAlignment.Near;
18+
19+
if (enabled)
20+
{
21+
using (var solidBrush = new SolidBrush(color))
22+
{
23+
g.DrawString(text, font, solidBrush, rectangle, sf);
24+
}
25+
}
26+
else
27+
{
28+
g.DrawString(text, font, SystemBrushes.GrayText, rectangle, sf);
29+
}
30+
}
31+
}
32+
}
33+
}

SvgFileType/Import/MyCheckBox.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 Osman Tunçelli. All rights reserved.
2+
// Use of this source code is governed by a LGPL license that can be
3+
// found in the COPYING file.
4+
5+
using System.Drawing;
6+
using System.Windows.Forms;
7+
using System.Windows.Forms.VisualStyles;
8+
9+
namespace SvgFileTypePlugin.Import
10+
{
11+
internal sealed class MyCheckBox : CheckBox
12+
{
13+
public MyCheckBox()
14+
{
15+
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
16+
SetStyle(ControlStyles.UserPaint, true);
17+
}
18+
19+
protected override void OnPaint(PaintEventArgs e)
20+
{
21+
using (SolidBrush b = new SolidBrush(BackColor))
22+
{
23+
e.Graphics.FillRectangle(b, ClientRectangle);
24+
}
25+
26+
//Draw the checkbox
27+
CheckBoxState state;
28+
switch(CheckState)
29+
{
30+
case CheckState.Checked when Enabled:
31+
state = CheckBoxState.CheckedNormal;
32+
break;
33+
case CheckState.Checked when !Enabled:
34+
state = CheckBoxState.CheckedDisabled;
35+
break;
36+
case CheckState.Unchecked when Enabled:
37+
state = CheckBoxState.UncheckedNormal;
38+
break;
39+
case CheckState.Unchecked when !Enabled:
40+
state = CheckBoxState.UncheckedDisabled;
41+
break;
42+
case CheckState.Indeterminate when Enabled:
43+
state = CheckBoxState.MixedNormal;
44+
break;
45+
default:
46+
case CheckState.Indeterminate when !Enabled:
47+
state = CheckBoxState.MixedDisabled;
48+
break;
49+
}
50+
51+
Size glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state);
52+
Point glyphLocation = new Point(0, (Bounds.Height - glyphSize.Height) / 2);
53+
CheckBoxRenderer.DrawCheckBox(e.Graphics, glyphLocation, state);
54+
int left = glyphSize.Width + 2;
55+
Rectangle textArea = new Rectangle(left, 1, Bounds.Width - left, Bounds.Height);
56+
ButtonHelper.DrawText(e.Graphics, textArea, Font, Text, Enabled, ForeColor);
57+
}
58+
}
59+
}

SvgFileType/Import/MyRadioButton.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2021 Osman Tunçelli. All rights reserved.
2+
// Use of this source code is governed by a LGPL license that can be
3+
// found in the COPYING file.
4+
5+
using System.Drawing;
6+
using System.Windows.Forms;
7+
using System.Windows.Forms.VisualStyles;
8+
9+
namespace SvgFileTypePlugin.Import
10+
{
11+
internal sealed class MyRadioButton : RadioButton
12+
{
13+
public MyRadioButton()
14+
{
15+
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
16+
SetStyle(ControlStyles.UserPaint, true);
17+
}
18+
19+
protected override void OnPaint(PaintEventArgs e)
20+
{
21+
using (SolidBrush b = new SolidBrush(BackColor))
22+
{
23+
e.Graphics.FillRectangle(b, ClientRectangle);
24+
}
25+
26+
//Draw the checkbox
27+
28+
RadioButtonState state;
29+
30+
if (Checked)
31+
{
32+
state = Enabled ? RadioButtonState.CheckedNormal : RadioButtonState.CheckedDisabled;
33+
}
34+
else
35+
{
36+
state = Enabled ? RadioButtonState.UncheckedNormal : RadioButtonState.UncheckedDisabled;
37+
}
38+
39+
Size glyphSize = RadioButtonRenderer.GetGlyphSize(e.Graphics, state);
40+
Point glyphLocation = new Point(0, (Bounds.Height - glyphSize.Height) / 2);
41+
RadioButtonRenderer.DrawRadioButton(e.Graphics, glyphLocation, state);
42+
int left = glyphSize.Width + 2;
43+
Rectangle textArea = new Rectangle(left, 1, Bounds.Width - left, Bounds.Height);
44+
ButtonHelper.DrawText(e.Graphics, textArea, Font, Text, Enabled, ForeColor);
45+
}
46+
}
47+
}

SvgFileType/PaintGroupBoundaries.cs renamed to SvgFileType/Import/PaintGroupBoundaries.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Drawing;
88
using System.Drawing.Drawing2D;
99

10-
namespace SvgFileTypePlugin
10+
namespace SvgFileTypePlugin.Import
1111
{
1212
/// <summary>
1313
/// Used to determine boundaries of a group.
@@ -25,15 +25,7 @@ public PaintGroupBoundaries(SvgGroup relatedGroup, bool isStart)
2525
public bool IsStart { get; }
2626

2727
public override RectangleF Bounds => throw new NotImplementedException();
28-
29-
public override SvgElement DeepCopy()
30-
{
31-
throw new NotImplementedException();
32-
}
33-
34-
public override GraphicsPath Path(ISvgRenderer renderer)
35-
{
36-
throw new NotImplementedException();
37-
}
28+
public override SvgElement DeepCopy() => throw new NotImplementedException();
29+
public override GraphicsPath Path(ISvgRenderer renderer) => throw new NotImplementedException();
3830
}
39-
}
31+
}

SvgFileType/PdnPluginForm.cs renamed to SvgFileType/Import/PdnPluginForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Security;
1010
using System.Windows.Forms;
1111

12-
namespace SvgFileTypePlugin
12+
namespace SvgFileTypePlugin.Import
1313
{
1414
internal class PdnPluginForm : Form
1515
{

SvgFileType/SvgDocumentOpener.cs renamed to SvgFileType/Import/SvgDocumentOpener.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,35 @@
88
using System.IO.Compression;
99
using System.Linq;
1010

11-
namespace SvgFileTypePlugin
11+
namespace SvgFileTypePlugin.Import
1212
{
1313
internal static class SvgDocumentOpener
1414
{
1515
public static SvgDocument Open(Stream stream)
1616
{
17-
if (stream == null) throw new ArgumentNullException(nameof(stream), "Input stream can not be null.");
18-
if (!stream.CanRead) throw new IOException("Input stream is not readable.");
17+
if (stream == null)
18+
{
19+
throw new ArgumentNullException(nameof(stream), "Input stream can not be null.");
20+
}
21+
22+
if (!stream.CanRead)
23+
{
24+
throw new IOException("Input stream is not readable.");
25+
}
1926

2027
byte[] first3Bytes = new byte[3];
2128
byte[] gzipHeaderBytes = { 0x1f, 0x8b, 0x8 };
2229
int read = stream.Read(first3Bytes, 0, 3);
23-
if (read < 3) throw new IOException("Input stream does not contain valid svg data.");
30+
if (read < 3)
31+
{
32+
throw new IOException("Input stream does not contain valid svg data.");
33+
}
2434

2535
bool svgz = first3Bytes.SequenceEqual(gzipHeaderBytes);
26-
Stream svgStream;
2736

2837
if (stream.CanSeek)
2938
{
30-
stream.Seek(0, SeekOrigin.Begin);
31-
svgStream = svgz ? new GZipStream(stream, CompressionMode.Decompress, true) : stream;
32-
return FromStream(svgStream, svgz);
39+
return FromStream(stream, svgz, true);
3340
}
3441
else
3542
{
@@ -47,26 +54,31 @@ public static SvgDocument Open(Stream stream)
4754
{
4855
memory.Write(first3Bytes, 0, 3);
4956
stream.CopyTo(memory);
50-
memory.Seek(0, SeekOrigin.Begin);
51-
svgStream = svgz ? (Stream)new GZipStream(memory, CompressionMode.Decompress, false) : memory;
52-
return FromStream(svgStream, svgz);
57+
return FromStream(memory, svgz, false);
5358
}
5459
}
5560
}
5661

57-
private static SvgDocument FromStream(Stream stream, bool svgz)
62+
private static SvgDocument FromStream(Stream stream, bool gzip, bool leaveOpen)
5863
{
64+
stream.Seek(0, SeekOrigin.Begin);
65+
66+
if (gzip)
67+
{
68+
stream = new GZipStream(stream, CompressionMode.Decompress, leaveOpen);
69+
}
70+
5971
try
6072
{
6173
return SvgDocument.Open<SvgDocument>(stream);
6274
}
6375
finally
6476
{
65-
if (svgz)
77+
if (!leaveOpen)
6678
{
6779
stream.Dispose();
6880
}
6981
}
7082
}
7183
}
72-
}
84+
}

SvgFileType/SvgElementExtensions.cs renamed to SvgFileType/Import/SvgElementExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Svg;
66
using System.Reflection;
77

8-
namespace SvgFileTypePlugin
8+
namespace SvgFileTypePlugin.Import
99
{
1010
internal static class SvgElementExtensions
1111
{

SvgFileType/SvgImportConfig.cs renamed to SvgFileType/Import/SvgImportConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a LGPL license that can be
33
// found in the COPYING file.
44

5-
namespace SvgFileTypePlugin
5+
namespace SvgFileTypePlugin.Import
66
{
77
internal sealed class SvgImportConfig
88
{

0 commit comments

Comments
 (0)