diff --git a/src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs index a77c6728b..6a8028770 100644 --- a/src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs @@ -89,7 +89,17 @@ namespace SixLabors.ImageSharp.Processing /// internal override void Apply(Span scanline, int x, int y) { - Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length); + Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x); + + // constrain the spans to eachother + if (destinationRow.Length > scanline.Length) + { + destinationRow = destinationRow.Slice(0, scanline.Length); + } + else + { + scanline = scanline.Slice(0, destinationRow.Length); + } MemoryAllocator memoryAllocator = this.Target.MemoryAllocator; Configuration configuration = this.Target.Configuration; diff --git a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj index 0ca3cffa1..bb559b70d 100644 --- a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj +++ b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj @@ -19,7 +19,7 @@ - + diff --git a/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs b/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs index 44bb160ce..b05ada522 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs @@ -25,6 +25,41 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text public static ImageComparer TextDrawingComparer = ImageComparer.TolerantPercentage(0.01f); public static ImageComparer OutlinedTextDrawingComparer = ImageComparer.TolerantPercentage(0.5f, 3); + [Theory] + [WithSolidFilledImages(276, 336, "White", PixelTypes.Rgba32)] + public void DoesntThrowExceptionWhenOverlappingRightEdge_Issue688(TestImageProvider provider) + where TPixel : struct, IPixel + { + Font font = CreateFont("OpenSans-Regular.ttf", 36); + TPixel color = NamedColors.Black; + float padding = 5; + var text = "A short piece of text"; + + using (var img = provider.GetImage()) + { + float targetWidth = img.Width - (padding * 2); + float targetHeight = img.Height - (padding * 2); + + // measure the text size + SizeF size = TextMeasurer.Measure(text, new RendererOptions(font)); + + //find out how much we need to scale the text to fill the space (up or down) + float scalingFactor = Math.Min(img.Width / size.Width, img.Height / size.Height); + + //create a new font + Font scaledFont = new Font(font, scalingFactor * font.Size); + + var center = new PointF(img.Width / 2, img.Height / 2); + var textGraphicOptions = new TextGraphicsOptions(true) + { + HorizontalAlignment = HorizontalAlignment.Center, + VerticalAlignment = VerticalAlignment.Center + }; + + img.Mutate(i => i.DrawText(textGraphicOptions, text, scaledFont, color, center)); + } + } + [Theory] [WithSolidFilledImages(200, 100, "White", PixelTypes.Rgba32, 50, 0, 0, "SixLaborsSampleAB.woff", AB)] [WithSolidFilledImages(900, 100, "White", PixelTypes.Rgba32, 50, 0, 0, "OpenSans-Regular.ttf", TestText)] diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 04a680200..86c1a7a25 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -27,7 +27,7 @@ - +