Browse Source

Fix horizontally out-of-bounds error when drawing text

af/merge-core
Turnerj 7 years ago
parent
commit
ad56a756ce
  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;
int startY = operation.Location.Y;
int startX = operation.Location.X;
int offSetSpan = 0;
int offsetSpan = 0;
if (startX < 0)
{
offSetSpan = -startX;
offsetSpan = -startX;
startX = 0;
}
int fistRow = 0;
if (startX >= source.Width)
{
continue;
}
int firstRow = 0;
if (startY < 0)
{
fistRow = -startY;
firstRow = -startY;
}
int maxHeight = source.Height - startY;
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;
Span<float> span = buffer.GetRowSpan(row).Slice(offSetSpan);
Span<float> span = buffer.GetRowSpan(row).Slice(offsetSpan);
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 = SystemFonts.CreateFont("Arial", 39, FontStyle.Regular);
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]
[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)]

Loading…
Cancel
Save