Skip to content

Commit 2646edb

Browse files
committed
Moving to C# 12
1 parent 98d7df6 commit 2646edb

File tree

17 files changed

+139
-169
lines changed

17 files changed

+139
-169
lines changed

SvgGdiTest/SvgGdiTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ProjectType>Local</ProjectType>
55
<ProductVersion>8.0.30319</ProductVersion>
66
<SchemaVersion>2.0</SchemaVersion>
7-
<LangVersion>10</LangVersion>
7+
<LangVersion>12</LangVersion>
88
<ProjectGuid>{0C963EB6-C1B4-453D-B694-E81EC26D3AB9}</ProjectGuid>
99
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
1010
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>

SvgNet/Elements/SvgElement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class SvgElement {
4646
/// <summary>
4747
/// An ArrayList containing this element's children
4848
/// </summary>
49-
public ArrayList Children { get; protected set; } = new ArrayList();
49+
public ArrayList Children { get; protected set; } = [];
5050

5151
public string Id {
5252
get => (string)_atts["id"];
@@ -175,7 +175,7 @@ public virtual void WriteXmlElements(XmlDocument doc, XmlElement parent) {
175175
_ = parent == null ? doc.AppendChild(me) : parent.AppendChild(me);
176176
}
177177

178-
protected Hashtable _atts = new();
178+
protected Hashtable _atts = [];
179179
protected object FirstChild => Children[0];
180180

181181
protected T GetTypedAttribute<T>(string attributeName, Func<object, T> fromString) where T : new() {

SvgNet/Extensions/StringExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace System;
1010

11-
1211
public static class StringExtensions {
1312
public static int ParseHex(this string s, int startIndex, int length = 1)
1413
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
@@ -17,7 +16,6 @@ public static int ParseHex(this string s, int startIndex, int length = 1)
1716
=> int.Parse(s.Substring(startIndex, length), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
1817
#endif
1918

20-
2119
public static IPB IsPrefixedBy(this string s, string prefix)
2220
=> !s.StartsWith(prefix, StringComparison.InvariantCulture)
2321
? new IPB(false, null)
@@ -36,8 +34,9 @@ public static string SkipFirst(this string s)
3634
=> s.Substring(1);
3735
#endif
3836

37+
private static readonly char[] digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
3938
public static bool TrySplitNumberAndSuffix(this string s, out string number, out string suffix) {
40-
int i = s.LastIndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }) + 1;
39+
int i = s.LastIndexOfAny(digits) + 1;
4140
suffix = number = null;
4241
if (i == 0)
4342
return false;

SvgNet/ImplementedGraphics/GDIGraphics.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ namespace SvgNet;
1010
/// <summary>
1111
/// An IGraphics implementation that simply passes every call through to a GDI+ <c>Graphics</c> object.
1212
/// </summary>
13-
public sealed class GdiGraphics : IGraphics {
14-
15-
public GdiGraphics(Graphics g) => _g = g ?? throw new ArgumentNullException(nameof(g));
16-
13+
public sealed class GdiGraphics(Graphics g) : IGraphics {
1714
public Region Clip { get => _g.Clip; set => _g.Clip = value; }
1815
public RectangleF ClipBounds => _g.ClipBounds;
1916
public CompositingMode CompositingMode { get => _g.CompositingMode; set => _g.CompositingMode = value; }
@@ -353,5 +350,5 @@ public SizeF MeasureString(string text, Font font, SizeF layoutArea, StringForma
353350

354351
public void TranslateTransform(float dx, float dy, MatrixOrder order) => _g.TranslateTransform(dx, dy, order);
355352

356-
private readonly Graphics _g;
353+
private readonly Graphics _g = g ?? throw new ArgumentNullException(nameof(g));
357354
}

SvgNet/ImplementedGraphics/SVGGraphics.BitmapDrawer.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,10 @@
1111
namespace SvgNet;
1212

1313
public sealed partial class SvgGraphics {
14-
private class BitmapDrawer {
15-
public BitmapDrawer(SvgGroupElement g, float x, float y, float scaleX, float scaleY) {
16-
_groupElement = g;
17-
_x = x;
18-
_y = y;
19-
_scaleX = scaleX;
20-
_scaleY = scaleY;
21-
}
22-
23-
private readonly SvgGroupElement _groupElement;
24-
private readonly float _x;
25-
private readonly float _y;
26-
private readonly float _scaleX;
27-
private readonly float _scaleY;
28-
14+
private class BitmapDrawer(SvgGroupElement g, float x, float y, float scaleX, float scaleY) {
2915
public SvgGroupElement DrawBitmapData(Bitmap b) {
3016
for (int line = 0; line < b.Height; ++line) {
31-
float scaledLine = _y + (line * _scaleY);
17+
float scaledLine = y + (line * scaleY);
3218
// Only draws the last 'set' of pixels when a new color is encountered or it's the last pixel in the line.
3319
Color currentColor = GetPixelColor(b, line, 0);
3420
int consecutive = 1;
@@ -48,19 +34,19 @@ public SvgGroupElement DrawBitmapData(Bitmap b) {
4834
} catch { }
4935
}
5036
}
51-
return _groupElement;
37+
return g;
5238
}
5339

5440
// This could be optimized in an unsafe version of the lib
5541
private static Color GetPixelColor(Bitmap b, int y, int x) => b.GetPixel(x, y);
5642

5743
private void DrawPixel(float scaledLine, int col, int consecutive, Color color) =>
58-
DrawImagePixel(_groupElement,
44+
DrawImagePixel(g,
5945
color,
60-
_x + ((col - consecutive - 1) * _scaleX),
46+
x + ((col - consecutive - 1) * scaleX),
6147
scaledLine,
62-
consecutive * _scaleX,
63-
_scaleY);
48+
consecutive * scaleX,
49+
scaleY);
6450
}
6551
}
6652

SvgNet/ImplementedGraphics/SVGGraphics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ public void DrawLine(Pen pen, float x1, float y1, float x2, float y2) {
618618
_cur.AddChild(lin);
619619

620620
DrawEndAnchors(pen, new PointF(x1, y1), new PointF(x2, y2));
621-
} else DrawLines(pen, new PointF[] { new PointF(x1, y1), new PointF(x2, y2) });
621+
} else DrawLines(pen, new PointF[] { new(x1, y1), new(x2, y2) });
622622
}
623623

624624
/// <summary>
@@ -2234,7 +2234,7 @@ private static SvgPath HandleGraphicsPath(GraphicsPath path) {
22342234
/// </summary>
22352235
private sealed class MatrixStack : IDisposable {
22362236
public MatrixStack() {
2237-
_mx = new();
2237+
_mx = [];
22382238

22392239
//we need 2 identity matrices on the stack. This is because we do a resettransform()
22402240
//by pop dup (to set current xform to xform of enclosing group).

SvgNet/MetafileTools/EmfTools.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,8 @@ protected EmfBinaryRecord() {
6262
/// <summary>
6363
/// Low-level EMF parser
6464
/// </summary>
65-
public class EmfReader : IDisposable {
66-
public EmfReader(Stream stream) {
67-
_stream = stream;
68-
_reader = new BinaryReader(stream);
69-
}
70-
71-
public bool IsEndOfFile => _stream.Length == _stream.Position;
65+
public class EmfReader(Stream stream) : IDisposable {
66+
public bool IsEndOfFile => stream.Length == stream.Position;
7267

7368
public void Dispose() {
7469
if (_reader is not null) {
@@ -84,7 +79,7 @@ public IBinaryRecord Read() {
8479
var rt = (EmfPlusRecordType)_reader.ReadUInt32();
8580
uint recordSize = _reader.ReadUInt32();
8681

87-
EmfBinaryRecord record = new EmfUnknownRecord {
82+
var record = new EmfUnknownRecord {
8883
RecordType = rt,
8984
RecordSize = recordSize
9085
};
@@ -101,8 +96,7 @@ public IBinaryRecord Read() {
10196
return record;
10297
}
10398

104-
private readonly Stream _stream;
105-
private BinaryReader _reader;
99+
private BinaryReader _reader = new(stream);
106100
}
107101

108102
public class EmfUnknownRecord : EmfBinaryRecord {
@@ -116,6 +110,6 @@ public override void Read(BinaryReader reader) {
116110
Data = length > 0 ? reader.ReadBytes(length) : _emptyData;
117111
}
118112

119-
private static readonly byte[] _emptyData = Array.Empty<byte>();
113+
private static readonly byte[] _emptyData = [];
120114
}
121115

SvgNet/MetafileTools/MetafileParser.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void EnumerateMetafile(Stream emf,
6666
_fillPolygon = fillPolygon;
6767
_zero = destination;
6868
_lineBuffer = new LineBuffer(unitSize);
69-
_objects = new Dictionary<uint, ObjectHandle>();
69+
_objects = [];
7070
_brush = null;
7171

7272
using (var reader = new EmfTools.EmfReader(emf)) {
@@ -451,9 +451,7 @@ private void ProcessPolygon16(byte[] recordData) {
451451

452452
uint totalNumberOfPoints = _br.ReadUInt32();
453453

454-
int[] numberOfPoints = new int[1];
455-
numberOfPoints[0] = (int)totalNumberOfPoints;
456-
454+
int[] numberOfPoints = [(int)totalNumberOfPoints];
457455
InternalProcessPolyline16(1, totalNumberOfPoints, numberOfPoints, _br);
458456

459457
System.Diagnostics.Debug.Assert(_ms.Position == _ms.Length);
@@ -601,14 +599,7 @@ public void Dispose() {
601599
_transform?.Dispose();
602600
}
603601

604-
private class LineBuffer {
605-
public LineBuffer(float unitSize) {
606-
_points = new List<NormalizedPoint>();
607-
_normalizedPoints = new List<NormalizedPoint>();
608-
_visualPoints = new List<VisualPoint>();
609-
_epsilonSquare = _unitSizeEpsilon * unitSize * _unitSizeEpsilon * unitSize;
610-
}
611-
602+
private class LineBuffer(float unitSize) {
612603
public bool IsEmpty => _points.Count == 0;
613604

614605
public void Add(PointF[] points, int offset, int count) {
@@ -668,14 +659,14 @@ public PointF[] GetPoints() {
668659
result.Add(visualPoint.Point);
669660
}
670661

671-
return result.ToArray();
662+
return [.. result];
672663
}
673664

674665
private const float _unitSizeEpsilon = 2.0f;
675-
private readonly float _epsilonSquare;
676-
private readonly List<NormalizedPoint> _normalizedPoints;
677-
private readonly List<NormalizedPoint> _points;
678-
private readonly List<VisualPoint> _visualPoints;
666+
private readonly float _epsilonSquare = _unitSizeEpsilon * unitSize * _unitSizeEpsilon * unitSize;
667+
private readonly List<NormalizedPoint> _normalizedPoints = [];
668+
private readonly List<NormalizedPoint> _points = [];
669+
private readonly List<VisualPoint> _visualPoints = [];
679670

680671
private static bool IsVisuallyIdentical(NormalizedPoint a, NormalizedPoint b) => a.VisualIndex == b.VisualIndex;
681672

SvgNet/SvgFactory.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ public static Hashtable BuildElementNameDictionary() {
2525
Type[] ta = asm.GetExportedTypes();
2626
foreach (Type t in ta) {
2727
if (t.IsSubclassOf(typeof(SvgElement)) && !t.IsAbstract) {
28-
ConstructorInfo ci = t.GetConstructor(Array.Empty<Type>());
29-
if (ci == null)
30-
throw new InvalidOperationException($"Type {t.Name} doesn't have the mandatory public parameterless constructor");
31-
var e = (SvgElement)ci.Invoke(Array.Empty<object>());
28+
ConstructorInfo ci = t.GetConstructor([]) ?? throw new InvalidOperationException($"Type {t.Name} doesn't have the mandatory public parameterless constructor");
29+
var e = (SvgElement)ci.Invoke([]);
3230
if (e.Name != "?" /* default name of abstract SvgElements */) {
3331
dict[e.Name] = e.GetType();
3432
}
@@ -45,7 +43,7 @@ public static Hashtable BuildElementNameDictionary() {
4543
/// <param name="el"></param>
4644
/// <returns></returns>
4745
public static SvgElement CloneElement(SvgElement el) {
48-
var clone = (SvgElement)el.GetType().GetConstructor(Array.Empty<Type>()).Invoke(Array.Empty<object>());
46+
var clone = (SvgElement)el.GetType().GetConstructor([]).Invoke([]);
4947

5048
foreach (string key in el.Attributes.Keys) {
5149
clone[key] = el[key].CloneIfPossible();
@@ -86,7 +84,7 @@ public static SvgElement LoadFromXML(XmlDocument doc, XmlElement el) {
8684

8785
var t = (Type)_elementNameDictionary[el.Name];
8886

89-
var e = (SvgElement)t.GetConstructor(Array.Empty<Type>()).Invoke(Array.Empty<object>());
87+
var e = (SvgElement)t.GetConstructor([]).Invoke([]);
9088

9189
RecLoadFromXML(e, doc, el);
9290

@@ -207,7 +205,7 @@ private static void RecLoadFromXML(SvgElement e, XmlDocument doc, XmlElement el)
207205

208206
SvgElement childSvg = t switch {
209207
null => new SvgGenericElement(childXml.Name),
210-
_ => (SvgElement)t.GetConstructor(Array.Empty<Type>()).Invoke(Array.Empty<object>()),
208+
_ => (SvgElement)t.GetConstructor([]).Invoke([]),
211209
};
212210
e.AddChild(childSvg);
213211

SvgNet/Types/PathSeg.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,10 @@ namespace SvgNet.Types;
1111
/// A segment in an Svg path. This is not a real SVG type; it is not in the SVG spec. It is provided for making paths
1212
/// easier to specify and parse.
1313
/// </summary>
14-
public class PathSeg : ICloneable {
15-
public float[] _data;
16-
public SvgPathSegType _type;
14+
public class PathSeg(SvgPathSegType type, bool abs, float[] data) : ICloneable {
15+
public bool Abs { get; } = abs;
1716

18-
public PathSeg(SvgPathSegType t, bool a, float[] arr) {
19-
_type = t;
20-
Abs = a;
21-
_data = arr;
22-
}
23-
24-
public bool Abs { get; }
25-
26-
public string Char => _type switch {
17+
public string Char => type switch {
2718
SvgPathSegType.SVG_SEGTYPE_MOVETO => Abs ? "M" : "m",
2819
SvgPathSegType.SVG_SEGTYPE_CLOSEPATH => "z",
2920
SvgPathSegType.SVG_SEGTYPE_LINETO => Abs ? "L" : "l",
@@ -35,12 +26,12 @@ public PathSeg(SvgPathSegType t, bool a, float[] arr) {
3526
SvgPathSegType.SVG_SEGTYPE_SMOOTHBEZIERTO => Abs ? "T" : "t",
3627
SvgPathSegType.SVG_SEGTYPE_ARCTO => Abs ? "A" : "a",
3728

38-
_ => throw new SvgException("Invalid PathSeg type", _type.ToString()),
29+
_ => throw new SvgException("Invalid PathSeg type", type.ToString()),
3930
};
4031

41-
public float[] Data => _data;
32+
public float[] Data => data;
4233

43-
public SvgPathSegType Type => _type;
34+
public SvgPathSegType Type => type;
4435

45-
public object Clone() => new PathSeg(_type, Abs, (float[])_data.Clone());
36+
public object Clone() => new PathSeg(type, Abs, (float[])data.Clone());
4637
};

0 commit comments

Comments
 (0)