Browse Source

Merge pull request #756 from SixLabors/sw/DrawTextException

constrain destination span and scanline to same length
af/merge-core
Anton Firsov 8 years ago
committed by GitHub
parent
commit
450447beac
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs
  2. 35
      tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs

12
src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs

@ -89,7 +89,17 @@ namespace SixLabors.ImageSharp.Processing
/// <inheritdoc />
internal override void Apply(Span<float> scanline, int x, int y)
{
Span<TPixel> destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
Span<TPixel> 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;

35
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<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
Font font = CreateFont("OpenSans-Regular.ttf", 36);
TPixel color = NamedColors<TPixel>.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)]

Loading…
Cancel
Save