Skip to content

Commit cd86e3c

Browse files
authored
Merge branch 'master' into sw/issue-28
2 parents 9dbf36e + a603226 commit cd86e3c

4 files changed

Lines changed: 135 additions & 28 deletions

File tree

src/ImageSharp.Drawing/Processing/Pen.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -39,6 +39,8 @@ public Pen(Color color, float width, float[] pattern)
3939
/// <param name="pattern">The pattern.</param>
4040
public Pen(IBrush brush, float width, float[] pattern)
4141
{
42+
Guard.MustBeGreaterThan(width, 0, nameof(width));
43+
4244
this.StrokeFill = brush;
4345
this.StrokeWidth = width;
4446
this.pattern = pattern;

src/ImageSharp.Drawing/Shapes/ComplexPolygon.cs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,52 @@ public ComplexPolygon(params IPath[] paths)
3636
{
3737
this.paths = paths ?? throw new ArgumentNullException(nameof(paths));
3838

39-
float minX = float.MaxValue;
40-
float maxX = float.MinValue;
41-
float minY = float.MaxValue;
42-
float maxY = float.MinValue;
43-
float length = 0;
44-
int intersections = 0;
45-
46-
foreach (IPath s in this.paths)
39+
if (paths.Length > 0)
4740
{
48-
length += s.Length;
49-
if (s.Bounds.Left < minX)
41+
float minX = float.MaxValue;
42+
float maxX = float.MinValue;
43+
float minY = float.MaxValue;
44+
float maxY = float.MinValue;
45+
float length = 0;
46+
int intersections = 0;
47+
48+
foreach (IPath s in this.paths)
5049
{
51-
minX = s.Bounds.Left;
52-
}
50+
length += s.Length;
51+
if (s.Bounds.Left < minX)
52+
{
53+
minX = s.Bounds.Left;
54+
}
5355

54-
if (s.Bounds.Right > maxX)
55-
{
56-
maxX = s.Bounds.Right;
57-
}
56+
if (s.Bounds.Right > maxX)
57+
{
58+
maxX = s.Bounds.Right;
59+
}
5860

59-
if (s.Bounds.Top < minY)
60-
{
61-
minY = s.Bounds.Top;
62-
}
61+
if (s.Bounds.Top < minY)
62+
{
63+
minY = s.Bounds.Top;
64+
}
6365

64-
if (s.Bounds.Bottom > maxY)
65-
{
66-
maxY = s.Bounds.Bottom;
66+
if (s.Bounds.Bottom > maxY)
67+
{
68+
maxY = s.Bounds.Bottom;
69+
}
70+
71+
intersections += s.MaxIntersections;
6772
}
6873

69-
intersections += s.MaxIntersections;
74+
this.MaxIntersections = intersections;
75+
this.Length = length;
76+
this.Bounds = new RectangleF(minX, minY, maxX - minX, maxY - minY);
77+
}
78+
else
79+
{
80+
this.MaxIntersections = 0;
81+
this.Length = 0;
82+
this.Bounds = RectangleF.Empty;
7083
}
7184

72-
this.MaxIntersections = intersections;
73-
this.Length = length;
74-
this.Bounds = new RectangleF(minX, minY, maxX - minX, maxY - minY);
7585
this.PathType = PathTypes.Mixed;
7686
}
7787

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using SixLabors.Fonts;
4+
using SixLabors.ImageSharp.Drawing.Processing;
5+
using SixLabors.ImageSharp.Drawing.Tests.TestUtilities.Attributes;
6+
using SixLabors.ImageSharp.PixelFormats;
7+
using SixLabors.ImageSharp.Processing;
8+
using Xunit;
9+
10+
namespace SixLabors.ImageSharp.Drawing.Tests.Issues
11+
{
12+
public class Issue_54
13+
{
14+
[WindowsFact]
15+
public void CanDrawWithoutMemoryException()
16+
{
17+
int width = 768;
18+
int height = 438;
19+
20+
// Creates a new image with empty pixel data.
21+
using (var image = new Image<Rgba32>(width, height))
22+
{
23+
FontFamily family = SystemFonts.Find("verdana");
24+
Font font = family.CreateFont(48, FontStyle.Bold);
25+
26+
// The options are optional
27+
TextGraphicsOptions options = new TextGraphicsOptions()
28+
{
29+
TextOptions = new TextOptions()
30+
{
31+
ApplyKerning = true,
32+
TabWidth = 8, // a tab renders as 8 spaces wide
33+
WrapTextWidth = width, // greater than zero so we will word wrap at 100 pixels wide
34+
HorizontalAlignment = HorizontalAlignment.Center // right align
35+
}
36+
};
37+
38+
IBrush brush = Brushes.Solid(Color.White);
39+
IPen pen = Pens.Solid(Color.White, 1);
40+
string text = "sample text";
41+
42+
// Draw the text
43+
image.Mutate(x => x.DrawText(options, text, font, brush, pen, new PointF(0, 100)));
44+
}
45+
}
46+
47+
[Fact]
48+
public void PenMustHaveAWidthGraterThanZero()
49+
{
50+
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() =>
51+
{
52+
IPen pen = new Pen(Color.White, 0);
53+
});
54+
55+
Assert.StartsWith("Parameter \"width\" (System.Single) must be greater than 0, was 0", ex.Message);
56+
}
57+
58+
59+
[Fact]
60+
public void ComplexPolygoWithZeroPathsCausesBoundsToBeNonSensicalValue()
61+
{
62+
var polygon = new ComplexPolygon(Array.Empty<IPath>());
63+
64+
Assert.NotEqual(float.NegativeInfinity, polygon.Bounds.Width);
65+
Assert.NotEqual(float.PositiveInfinity, polygon.Bounds.Width);
66+
Assert.NotEqual(float.NaN, polygon.Bounds.Width);
67+
}
68+
}
69+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace SixLabors.ImageSharp.Drawing.Tests.TestUtilities.Attributes
8+
{
9+
public class PlatformFactAttribute : FactAttribute
10+
{
11+
public PlatformFactAttribute(OSPlatform platform)
12+
{
13+
if (!RuntimeInformation.IsOSPlatform(platform))
14+
{
15+
this.Skip = $"Platform specific test, runs only on '{platform}'";
16+
}
17+
}
18+
}
19+
20+
public class WindowsFactAttribute : PlatformFactAttribute
21+
{
22+
public WindowsFactAttribute() : base(OSPlatform.Windows)
23+
{
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)