Browse Source

Implemented PinnedBuffer<T>.CreateClean() + format DrawText.cs

pull/137/head
Anton Firszov 9 years ago
parent
commit
02db976b95
  1. 12
      src/ImageSharp/Common/Memory/PinnedBuffer{T}.cs
  2. 18
      tests/ImageSharp.Tests/Common/PinnedBufferTests.cs
  3. 185
      tests/ImageSharp.Tests/Drawing/Text/DrawText.cs

12
src/ImageSharp/Common/Memory/PinnedBuffer{T}.cs

@ -109,6 +109,18 @@ namespace ImageSharp
return buffer.Slice(); return buffer.Slice();
} }
/// <summary>
/// Creates a clean instance of <see cref="PinnedBuffer{T}"/> initializing it's elements with 'default(T)'.
/// </summary>
/// <param name="count">The desired count of elements. (Minimum size for <see cref="Array"/>)</param>
/// <returns>The <see cref="PinnedBuffer{T}"/> instance</returns>
public static PinnedBuffer<T> CreateClean(int count)
{
PinnedBuffer<T> buffer = new PinnedBuffer<T>(count);
buffer.Clear();
return buffer;
}
/// <summary> /// <summary>
/// Gets a <see cref="BufferPointer{T}"/> to the beginning of the raw data of the buffer. /// Gets a <see cref="BufferPointer{T}"/> to the beginning of the raw data of the buffer.
/// </summary> /// </summary>

18
tests/ImageSharp.Tests/Common/PinnedBufferTests.cs

@ -3,6 +3,7 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit; using Xunit;
@ -62,6 +63,23 @@
} }
} }
[Fact]
public void CreateClean()
{
Parallel.For(0, 100,
i =>
{
using (PinnedBuffer<int> buffer = PinnedBuffer<int>.CreateClean(42))
{
for (int j = 0; j < buffer.Count; j++)
{
Assert.Equal(0, buffer.Array[j]);
buffer.Array[j] = 666;
}
}
});
}
[Fact] [Fact]
public void Dispose() public void Dispose()
{ {

185
tests/ImageSharp.Tests/Drawing/Text/DrawText.cs

@ -1,96 +1,107 @@
 // <copyright file="DrawText.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.Drawing.Text namespace ImageSharp.Tests.Drawing.Text
{ {
using System; using System;
using System.IO;
using ImageSharp;
using ImageSharp.Drawing.Brushes;
using Processing;
using System.Collections.Generic;
using Xunit;
using ImageSharp.Drawing;
using System.Numerics; using System.Numerics;
using SixLabors.Shapes;
using ImageSharp.Drawing.Processors; using ImageSharp.Drawing;
using ImageSharp.Drawing.Brushes;
using ImageSharp.Drawing.Pens; using ImageSharp.Drawing.Pens;
using ImageSharp.Drawing.Processors;
using ImageSharp.Tests.Drawing.Paths;
using SixLabors.Fonts; using SixLabors.Fonts;
using Paths; using SixLabors.Shapes;
using Xunit;
public class DrawText : IDisposable public class DrawText : IDisposable
{ {
Color color = Color.HotPink; Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Color.HotPink); SolidBrush brush = Brushes.Solid(Color.HotPink);
IPath path = new SixLabors.Shapes.Path(new LinearLineSegment(new Vector2[] {
new Vector2(10,10), IPath path = new SixLabors.Shapes.Path(
new Vector2(20,10), new LinearLineSegment(
new Vector2(20,10), new Vector2[] { new Vector2(10, 10), new Vector2(20, 10), new Vector2(20, 10), new Vector2(30, 10), }));
new Vector2(30,10),
}));
private ProcessorWatchingImage img; private ProcessorWatchingImage img;
private readonly FontCollection FontCollection; private readonly FontCollection FontCollection;
private readonly Font Font; private readonly Font Font;
public DrawText() public DrawText()
{ {
this.FontCollection = new FontCollection(); this.FontCollection = new FontCollection();
this.Font = FontCollection.Install(TestFontUtilities.GetPath("SixLaborsSampleAB.woff")); this.Font = this.FontCollection.Install(TestFontUtilities.GetPath("SixLaborsSampleAB.woff"));
this.img = new ProcessorWatchingImage(10, 10); this.img = new ProcessorWatchingImage(10, 10);
} }
public void Dispose() public void Dispose()
{ {
img.Dispose(); this.img.Dispose();
} }
[Fact] [Fact]
public void FillsForEachACharachterWhenBrushSetAndNotPen() public void FillsForEachACharachterWhenBrushSetAndNotPen()
{ {
img.DrawText("123", this.Font, Brushes.Solid(Color.Red), null, Vector2.Zero, new TextGraphicsOptions(true)); this.img.DrawText(
"123",
this.Font,
Brushes.Solid(Color.Red),
null,
Vector2.Zero,
new TextGraphicsOptions(true));
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); // 3 fills where applied Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied
Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
} }
[Fact] [Fact]
public void FillsForEachACharachterWhenBrushSetAndNotPenDefaultOptions() public void FillsForEachACharachterWhenBrushSetAndNotPenDefaultOptions()
{ {
img.DrawText("123", this.Font, Brushes.Solid(Color.Red), null, Vector2.Zero); this.img.DrawText("123", this.Font, Brushes.Solid(Color.Red), null, Vector2.Zero);
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); // 3 fills where applied Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied
Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
} }
[Fact] [Fact]
public void FillsForEachACharachterWhenBrushSet() public void FillsForEachACharachterWhenBrushSet()
{ {
img.DrawText("123", this.Font, Brushes.Solid(Color.Red), Vector2.Zero, new TextGraphicsOptions(true)); this.img.DrawText("123", this.Font, Brushes.Solid(Color.Red), Vector2.Zero, new TextGraphicsOptions(true));
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); // 3 fills where applied Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied
Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
} }
[Fact] [Fact]
public void FillsForEachACharachterWhenBrushSetDefaultOptions() public void FillsForEachACharachterWhenBrushSetDefaultOptions()
{ {
img.DrawText("123", this.Font, Brushes.Solid(Color.Red), Vector2.Zero); this.img.DrawText("123", this.Font, Brushes.Solid(Color.Red), Vector2.Zero);
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); // 3 fills where applied Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied
Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
} }
[Fact] [Fact]
public void FillsForEachACharachterWhenColorSet() public void FillsForEachACharachterWhenColorSet()
{ {
img.DrawText("123", this.Font, Color.Red, Vector2.Zero, new TextGraphicsOptions(true)); this.img.DrawText("123", this.Font, Color.Red, Vector2.Zero, new TextGraphicsOptions(true));
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); Assert.Equal(3, this.img.ProcessorApplications.Count);
FillRegionProcessor<Color> processor = Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); FillRegionProcessor<Color> processor =
Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
SolidBrush<Color> brush = Assert.IsType<SolidBrush<Color>>(processor.Brush); SolidBrush<Color> brush = Assert.IsType<SolidBrush<Color>>(processor.Brush);
Assert.Equal(Color.Red, brush.Color); Assert.Equal(Color.Red, brush.Color);
@ -99,12 +110,13 @@ namespace ImageSharp.Tests.Drawing.Text
[Fact] [Fact]
public void FillsForEachACharachterWhenColorSetDefaultOptions() public void FillsForEachACharachterWhenColorSetDefaultOptions()
{ {
img.DrawText("123", this.Font, Color.Red, Vector2.Zero); this.img.DrawText("123", this.Font, Color.Red, Vector2.Zero);
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); Assert.Equal(3, this.img.ProcessorApplications.Count);
Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
FillRegionProcessor<Color> processor = Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); FillRegionProcessor<Color> processor =
Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
SolidBrush<Color> brush = Assert.IsType<SolidBrush<Color>>(processor.Brush); SolidBrush<Color> brush = Assert.IsType<SolidBrush<Color>>(processor.Brush);
Assert.Equal(Color.Red, brush.Color); Assert.Equal(Color.Red, brush.Color);
@ -113,82 +125,99 @@ namespace ImageSharp.Tests.Drawing.Text
[Fact] [Fact]
public void DrawForEachACharachterWhenPenSetAndNotBrush() public void DrawForEachACharachterWhenPenSetAndNotBrush()
{ {
img.DrawText("123", this.Font, null, Pens.Dash(Color.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); this.img.DrawText(
"123",
this.Font,
null,
Pens.Dash(Color.Red, 1),
Vector2.Zero,
new TextGraphicsOptions(true));
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); // 3 fills where applied Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied
Assert.IsType<DrawPathProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<DrawPathProcessor<Color>>(this.img.ProcessorApplications[0].processor);
} }
[Fact] [Fact]
public void DrawForEachACharachterWhenPenSetAndNotBrushDefaultOptions() public void DrawForEachACharachterWhenPenSetAndNotBrushDefaultOptions()
{ {
img.DrawText("123", this.Font, null, Pens.Dash(Color.Red, 1), Vector2.Zero); this.img.DrawText("123", this.Font, null, Pens.Dash(Color.Red, 1), Vector2.Zero);
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); // 3 fills where applied Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied
Assert.IsType<DrawPathProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<DrawPathProcessor<Color>>(this.img.ProcessorApplications[0].processor);
} }
[Fact] [Fact]
public void DrawForEachACharachterWhenPenSet() public void DrawForEachACharachterWhenPenSet()
{ {
img.DrawText("123", this.Font, Pens.Dash(Color.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); this.img.DrawText("123", this.Font, Pens.Dash(Color.Red, 1), Vector2.Zero, new TextGraphicsOptions(true));
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); // 3 fills where applied Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied
Assert.IsType<DrawPathProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<DrawPathProcessor<Color>>(this.img.ProcessorApplications[0].processor);
} }
[Fact] [Fact]
public void DrawForEachACharachterWhenPenSetDefaultOptions() public void DrawForEachACharachterWhenPenSetDefaultOptions()
{ {
img.DrawText("123", this.Font, Pens.Dash(Color.Red, 1), Vector2.Zero); this.img.DrawText("123", this.Font, Pens.Dash(Color.Red, 1), Vector2.Zero);
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(3, img.ProcessorApplications.Count); // 3 fills where applied Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied
Assert.IsType<DrawPathProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<DrawPathProcessor<Color>>(this.img.ProcessorApplications[0].processor);
} }
[Fact] [Fact]
public void DrawForEachACharachterWhenPenSetAndFillFroEachWhenBrushSet() public void DrawForEachACharachterWhenPenSetAndFillFroEachWhenBrushSet()
{ {
img.DrawText("123", this.Font, Brushes.Solid(Color.Red), Pens.Dash(Color.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); this.img.DrawText(
"123",
this.Font,
Brushes.Solid(Color.Red),
Pens.Dash(Color.Red, 1),
Vector2.Zero,
new TextGraphicsOptions(true));
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(6, img.ProcessorApplications.Count); Assert.Equal(6, this.img.ProcessorApplications.Count);
} }
[Fact] [Fact]
public void DrawForEachACharachterWhenPenSetAndFillFroEachWhenBrushSetDefaultOptions() public void DrawForEachACharachterWhenPenSetAndFillFroEachWhenBrushSetDefaultOptions()
{ {
img.DrawText("123", this.Font, Brushes.Solid(Color.Red), Pens.Dash(Color.Red, 1), Vector2.Zero); this.img.DrawText("123", this.Font, Brushes.Solid(Color.Red), Pens.Dash(Color.Red, 1), Vector2.Zero);
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(6, img.ProcessorApplications.Count); Assert.Equal(6, this.img.ProcessorApplications.Count);
} }
[Fact] [Fact]
public void BrushAppliesBeforPen() public void BrushAppliesBeforPen()
{ {
img.DrawText("1", this.Font, Brushes.Solid(Color.Red), Pens.Dash(Color.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); this.img.DrawText(
"1",
this.Font,
Brushes.Solid(Color.Red),
Pens.Dash(Color.Red, 1),
Vector2.Zero,
new TextGraphicsOptions(true));
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(2, img.ProcessorApplications.Count); Assert.Equal(2, this.img.ProcessorApplications.Count);
Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
Assert.IsType<DrawPathProcessor<Color>>(img.ProcessorApplications[1].processor); Assert.IsType<DrawPathProcessor<Color>>(this.img.ProcessorApplications[1].processor);
} }
[Fact] [Fact]
public void BrushAppliesBeforPenDefaultOptions() public void BrushAppliesBeforPenDefaultOptions()
{ {
img.DrawText("1", this.Font, Brushes.Solid(Color.Red), Pens.Dash(Color.Red, 1), Vector2.Zero); this.img.DrawText("1", this.Font, Brushes.Solid(Color.Red), Pens.Dash(Color.Red, 1), Vector2.Zero);
Assert.NotEmpty(img.ProcessorApplications); Assert.NotEmpty(this.img.ProcessorApplications);
Assert.Equal(2, img.ProcessorApplications.Count); Assert.Equal(2, this.img.ProcessorApplications.Count);
Assert.IsType<FillRegionProcessor<Color>>(img.ProcessorApplications[0].processor); Assert.IsType<FillRegionProcessor<Color>>(this.img.ProcessorApplications[0].processor);
Assert.IsType<DrawPathProcessor<Color>>(img.ProcessorApplications[1].processor); Assert.IsType<DrawPathProcessor<Color>>(this.img.ProcessorApplications[1].processor);
} }
} }
} }

Loading…
Cancel
Save