Browse Source

Merge pull request #1008 from Turnerj/fix-issue-688

Fix horizontally out-of-bounds error when drawing text
pull/1009/head
Scott Williams 7 years ago
committed by GitHub
parent
commit
fbfc452dd4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor{TPixel}.cs
  2. 18
      tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs

17
src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor{TPixel}.cs

@ -90,26 +90,31 @@ namespace SixLabors.ImageSharp.Processing.Processors.Text
Buffer2D<float> buffer = operation.Map; Buffer2D<float> buffer = operation.Map;
int startY = operation.Location.Y; int startY = operation.Location.Y;
int startX = operation.Location.X; int startX = operation.Location.X;
int offSetSpan = 0; int offsetSpan = 0;
if (startX < 0) if (startX < 0)
{ {
offSetSpan = -startX; offsetSpan = -startX;
startX = 0; startX = 0;
} }
int fistRow = 0; if (startX >= source.Width)
{
continue;
}
int firstRow = 0;
if (startY < 0) if (startY < 0)
{ {
fistRow = -startY; firstRow = -startY;
} }
int maxHeight = source.Height - startY; int maxHeight = source.Height - startY;
int end = Math.Min(operation.Map.Height, maxHeight); int end = Math.Min(operation.Map.Height, maxHeight);
for (int row = fistRow; row < end; row++) for (int row = firstRow; row < end; row++)
{ {
int y = startY + row; int y = startY + row;
Span<float> span = buffer.GetRowSpan(row).Slice(offSetSpan); Span<float> span = buffer.GetRowSpan(row).Slice(offsetSpan);
app.Apply(span, startX, y); app.Apply(span, startX, y);
} }
} }

18
tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs

@ -65,6 +65,24 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
} }
} }
[Theory]
[WithSolidFilledImages(1500, 500, "White", PixelTypes.Rgba32)]
public void DoesntThrowExceptionWhenOverlappingRightEdge_Issue688_2<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> img = provider.GetImage())
{
Font font = CreateFont("OpenSans-Regular.ttf", 39);
string text = new string('a', 10000); // exception
// string text = "Hello"; // no exception
Rgba32 color = Rgba32.Black;
var point = new PointF(100, 100);
img.Mutate(ctx => ctx.DrawText(text, font, color, point));
}
}
[Theory] [Theory]
[WithSolidFilledImages(200, 100, "White", PixelTypes.Rgba32, 50, 0, 0, "SixLaborsSampleAB.woff", AB)] [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)] [WithSolidFilledImages(900, 100, "White", PixelTypes.Rgba32, 50, 0, 0, "OpenSans-Regular.ttf", TestText)]

Loading…
Cancel
Save