Browse Source

Simplify options and add clone + tests

af/merge-core
James Jackson-South 6 years ago
parent
commit
4a4379ef80
  1. 103
      src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs
  2. 10
      tests/ImageSharp.Benchmarks/Drawing/DrawText.cs
  3. 14
      tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs
  4. 22
      tests/ImageSharp.Tests/Drawing/DrawLinesTests.cs
  5. 2
      tests/ImageSharp.Tests/Drawing/DrawPolygonTests.cs
  6. 3
      tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs
  7. 2
      tests/ImageSharp.Tests/Drawing/FillPolygonTests.cs
  8. 16
      tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs
  9. 24
      tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs
  10. 2
      tests/ImageSharp.Tests/Drawing/Paths/DrawPathCollection.cs
  11. 2
      tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs
  12. 2
      tests/ImageSharp.Tests/Drawing/Paths/FillPathCollection.cs
  13. 2
      tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs
  14. 2
      tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs
  15. 29
      tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs
  16. 12
      tests/ImageSharp.Tests/Drawing/Text/DrawText.cs
  17. 9
      tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs
  18. 128
      tests/ImageSharp.Tests/Drawing/Text/TextGraphicsOptionsTests.cs
  19. 65
      tests/ImageSharp.Tests/GraphicsOptionsTests.cs
  20. 4
      tests/ImageSharp.Tests/Issues/Issue412.cs
  21. 2
      tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs

103
src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs

@ -12,51 +12,22 @@ namespace SixLabors.ImageSharp.Processing
/// </summary>
public class TextGraphicsOptions
{
private static readonly Lazy<TextGraphicsOptions> Lazy = new Lazy<TextGraphicsOptions>();
private int antialiasSubpixelDepth;
private float blendPercentage;
private float tabWidth;
private float dpiX;
private float dpiY;
/// <summary>
/// Initializes a new instance of the <see cref="TextGraphicsOptions"/> class.
/// </summary>
public TextGraphicsOptions()
: this(true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TextGraphicsOptions" /> class.
/// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
public TextGraphicsOptions(bool enableAntialiasing)
{
this.ApplyKerning = true;
this.TabWidth = 4F;
this.WrapTextWidth = 0;
this.HorizontalAlignment = HorizontalAlignment.Left;
this.VerticalAlignment = VerticalAlignment.Top;
this.antialiasSubpixelDepth = 16;
this.ColorBlendingMode = PixelColorBlendingMode.Normal;
this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver;
this.blendPercentage = 1F;
this.Antialias = enableAntialiasing;
this.dpiX = 72F;
this.dpiY = 72F;
}
private int antialiasSubpixelDepth = 16;
private float blendPercentage = 1F;
private float tabWidth = 4F;
private float dpiX = 72F;
private float dpiY = 72F;
/// <summary>
/// Gets the default <see cref="TextGraphicsOptions"/> instance.
/// </summary>
public static TextGraphicsOptions Default { get; } = Lazy.Value;
public static TextGraphicsOptions Default { get; } = new TextGraphicsOptions();
/// <summary>
/// Gets or sets a value indicating whether antialiasing should be applied.
/// Defaults to true.
/// </summary>
public bool Antialias { get; set; }
public bool Antialias { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled.
@ -92,27 +63,27 @@ namespace SixLabors.ImageSharp.Processing
}
}
// In the future we could expose a PixelBlender<TPixel> directly on here
// or some forms of PixelBlender factory for each pixel type. Will need
// some API thought post V1.
/// <summary>
/// Gets or sets a value indicating the color blending percentage to apply to the drawing operation
/// Gets or sets a value indicating the color blending percentage to apply to the drawing operation.
/// Defaults to <see cref= "PixelColorBlendingMode.Normal" />.
/// </summary>
public PixelColorBlendingMode ColorBlendingMode { get; set; }
public PixelColorBlendingMode ColorBlendingMode { get; set; } = PixelColorBlendingMode.Normal;
/// <summary>
/// Gets or sets a value indicating the color blending percentage to apply to the drawing operation
/// Defaults to <see cref= "PixelAlphaCompositionMode.SrcOver" />.
/// </summary>
public PixelAlphaCompositionMode AlphaCompositionMode { get; set; }
public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } = PixelAlphaCompositionMode.SrcOver;
/// <summary>
/// Gets or sets a value indicating whether the text should be drawing with kerning enabled.
/// Defaults to true;
/// </summary>
public bool ApplyKerning { get; set; }
public bool ApplyKerning { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating the number of space widths a tab should lock to.
/// Defaults to 4.
/// </summary>
public float TabWidth
{
@ -130,11 +101,13 @@ namespace SixLabors.ImageSharp.Processing
/// <summary>
/// Gets or sets a value, if greater than 0, indicating the width at which text should wrap.
/// Defaults to 0.
/// </summary>
public float WrapTextWidth { get; set; }
/// <summary>
/// Gets or sets a value indicating the DPI to render text along the X axis.
/// Gets or sets a value indicating the DPI (Dots Per Inch) to render text along the X axis.
/// Defaults to 72.
/// </summary>
public float DpiX
{
@ -151,7 +124,8 @@ namespace SixLabors.ImageSharp.Processing
}
/// <summary>
/// Gets or sets a value indicating the DPI to render text along the Y axis.
/// Gets or sets a value indicating the DPI (Dots Per Inch) to render text along the Y axis.
/// Defaults to 72.
/// </summary>
public float DpiY
{
@ -172,13 +146,15 @@ namespace SixLabors.ImageSharp.Processing
/// If <see cref="WrapTextWidth"/> is greater than zero it will align relative to the space
/// defined by the location and width, if <see cref="WrapTextWidth"/> equals zero, and thus
/// wrapping disabled, then the alignment is relative to the drawing location.
/// Defaults to <see cref="HorizontalAlignment.Left"/>.
/// </summary>
public HorizontalAlignment HorizontalAlignment { get; set; }
public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Left;
/// <summary>
/// Gets or sets a value indicating how to align the text relative to the rendering space.
/// Defaults to <see cref="VerticalAlignment.Top"/>.
/// </summary>
public VerticalAlignment VerticalAlignment { get; set; }
public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Top;
/// <summary>
/// Performs an implicit conversion from <see cref="GraphicsOptions"/> to <see cref="TextGraphicsOptions"/>.
@ -189,8 +165,9 @@ namespace SixLabors.ImageSharp.Processing
/// </returns>
public static implicit operator TextGraphicsOptions(GraphicsOptions options)
{
return new TextGraphicsOptions(options.Antialias)
return new TextGraphicsOptions()
{
Antialias = options.Antialias,
AntialiasSubpixelDepth = options.AntialiasSubpixelDepth,
blendPercentage = options.BlendPercentage,
ColorBlendingMode = options.ColorBlendingMode,
@ -207,13 +184,37 @@ namespace SixLabors.ImageSharp.Processing
/// </returns>
public static explicit operator GraphicsOptions(TextGraphicsOptions options)
{
return new GraphicsOptions(options.Antialias)
return new GraphicsOptions()
{
Antialias = options.Antialias,
AntialiasSubpixelDepth = options.AntialiasSubpixelDepth,
ColorBlendingMode = options.ColorBlendingMode,
AlphaCompositionMode = options.AlphaCompositionMode,
BlendPercentage = options.BlendPercentage
};
}
/// <summary>
/// Creates a shallow copy of the <see cref="TextGraphicsOptions"/>.
/// </summary>
/// <returns>A new options instance.</returns>
public TextGraphicsOptions Clone()
{
return new TextGraphicsOptions
{
AlphaCompositionMode = this.AlphaCompositionMode,
Antialias = this.Antialias,
AntialiasSubpixelDepth = this.AntialiasSubpixelDepth,
ApplyKerning = this.ApplyKerning,
BlendPercentage = this.BlendPercentage,
ColorBlendingMode = this.ColorBlendingMode,
DpiX = this.DpiX,
DpiY = this.DpiY,
HorizontalAlignment = this.HorizontalAlignment,
TabWidth = this.TabWidth,
WrapTextWidth = this.WrapTextWidth,
VerticalAlignment = this.VerticalAlignment
};
}
}
}

10
tests/ImageSharp.Benchmarks/Drawing/DrawText.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Drawing;
@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Benchmarks
graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var font = new Font("Arial", 12, GraphicsUnit.Point))
{
graphics.DrawString(TextToRender, font, System.Drawing.Brushes.HotPink, new RectangleF(10, 10, 780, 780));
graphics.DrawString(this.TextToRender, font, System.Drawing.Brushes.HotPink, new RectangleF(10, 10, 780, 780));
}
}
}
@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Benchmarks
using (var image = new Image<Rgba32>(800, 800))
{
var font = SixLabors.Fonts.SystemFonts.CreateFont("Arial", 12);
image.Mutate(x => x.ApplyProcessor(new DrawTextProcessor(new TextGraphicsOptions(true) { WrapTextWidth = 780 }, TextToRender, font, Processing.Brushes.Solid(Rgba32.HotPink), null, new SixLabors.Primitives.PointF(10, 10))));
image.Mutate(x => x.ApplyProcessor(new DrawTextProcessor(new TextGraphicsOptions { Antialias = true, WrapTextWidth = 780 }, this.TextToRender, font, Processing.Brushes.Solid(Rgba32.HotPink), null, new SixLabors.Primitives.PointF(10, 10))));
}
}
@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Benchmarks
using (var image = new Image<Rgba32>(800, 800))
{
var font = SixLabors.Fonts.SystemFonts.CreateFont("Arial", 12);
image.Mutate(x => DrawTextOldVersion(x, new TextGraphicsOptions(true) { WrapTextWidth = 780 }, TextToRender, font, Processing.Brushes.Solid(Rgba32.HotPink), null, new SixLabors.Primitives.PointF(10, 10)));
image.Mutate(x => DrawTextOldVersion(x, new TextGraphicsOptions { Antialias = true, WrapTextWidth = 780 }, this.TextToRender, font, Processing.Brushes.Solid(Rgba32.HotPink), null, new SixLabors.Primitives.PointF(10, 10)));
}
IImageProcessingContext DrawTextOldVersion(
@ -93,4 +93,4 @@ namespace SixLabors.ImageSharp.Benchmarks
}
}
}
}
}

14
tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Drawing;
@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Benchmarks
[Params(10, 100)]
public int TextIterations { get; set; }
public string TextPhrase { get; set; } = "Hello World";
public string TextToRender => string.Join(" ", Enumerable.Repeat(TextPhrase, TextIterations));
public string TextToRender => string.Join(" ", Enumerable.Repeat(this.TextPhrase, this.TextIterations));
[Benchmark(Baseline = true, Description = "System.Drawing Draw Text Outline")]
public void DrawTextSystemDrawing()
@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Benchmarks
using (var font = new Font("Arial", 12, GraphicsUnit.Point))
using (var gp = new GraphicsPath())
{
gp.AddString(TextToRender, font.FontFamily, (int)font.Style, font.Size, new RectangleF(10, 10, 780, 780), new StringFormat());
gp.AddString(this.TextToRender, font.FontFamily, (int)font.Style, font.Size, new RectangleF(10, 10, 780, 780), new StringFormat());
graphics.DrawPath(pen, gp);
}
}
@ -43,7 +43,7 @@ namespace SixLabors.ImageSharp.Benchmarks
using (var image = new Image<Rgba32>(800, 800))
{
var font = SixLabors.Fonts.SystemFonts.CreateFont("Arial", 12);
image.Mutate(x => x.ApplyProcessor(new DrawTextProcessor(new TextGraphicsOptions(true) { WrapTextWidth = 780 }, TextToRender, font, null, Processing.Pens.Solid(Rgba32.HotPink, 10), new SixLabors.Primitives.PointF(10, 10))));
image.Mutate(x => x.ApplyProcessor(new DrawTextProcessor(new TextGraphicsOptions { Antialias = true, WrapTextWidth = 780 }, this.TextToRender, font, null, Processing.Pens.Solid(Rgba32.HotPink, 10), new SixLabors.Primitives.PointF(10, 10))));
}
}
@ -56,8 +56,8 @@ namespace SixLabors.ImageSharp.Benchmarks
image.Mutate(
x => DrawTextOldVersion(
x,
new TextGraphicsOptions(true) { WrapTextWidth = 780 },
TextToRender,
new TextGraphicsOptions { Antialias = true, WrapTextWidth = 780 },
this.TextToRender,
font,
null,
Processing.Pens.Solid(Rgba32.HotPink, 10),
@ -99,4 +99,4 @@ namespace SixLabors.ImageSharp.Benchmarks
}
}
}
}
}

22
tests/ImageSharp.Tests/Drawing/DrawLinesTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -24,10 +24,10 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen pen = new Pen(color, thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "White", 1f, 5, false)]
public void DrawLines_Dash<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
@ -35,10 +35,10 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen pen = Pens.Dash(color, thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "LightGreen", 1f, 5, false)]
public void DrawLines_Dot<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
@ -46,10 +46,10 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen pen = Pens.Dot(color, thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "Yellow", 1f, 5, false)]
public void DrawLines_DashDot<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
@ -57,7 +57,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen pen = Pens.DashDot(color, thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
@ -68,11 +68,11 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen pen = Pens.DashDotDot(color, thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
private static void DrawLinesImpl<TPixel>(
TestImageProvider<TPixel> provider,
string colorName,
@ -84,7 +84,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
SixLabors.Primitives.PointF[] simplePath = { new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300) };
GraphicsOptions options = new GraphicsOptions(antialias);
GraphicsOptions options = new GraphicsOptions { Antialias = antialias };
string aa = antialias ? "" : "_NoAntialias";
FormattableString outputDetails = $"{colorName}_A({alpha})_T({thickness}){aa}";

2
tests/ImageSharp.Tests/Drawing/DrawPolygonTests.cs

@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
};
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
GraphicsOptions options = new GraphicsOptions(antialias);
GraphicsOptions options = new GraphicsOptions { Antialias = antialias };
string aa = antialias ? "" : "_NoAntialias";
FormattableString outputDetails = $"{colorName}_A({alpha})_T({thickness}){aa}";

3
tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs

@ -412,8 +412,9 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
Size size = ctx.GetCurrentSize();
IPathCollection glossPath = BuildGloss(size.Width, size.Height);
var graphicsOptions = new GraphicsOptions(true)
var graphicsOptions = new GraphicsOptions
{
Antialias = true,
ColorBlendingMode = PixelColorBlendingMode.Normal,
AlphaCompositionMode = PixelAlphaCompositionMode.SrcAtop
};

2
tests/ImageSharp.Tests/Drawing/FillPolygonTests.cs

@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
};
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
var options = new GraphicsOptions(antialias);
var options = new GraphicsOptions { Antialias = antialias };
string aa = antialias ? "" : "_NoAntialias";
FormattableString outputDetails = $"{colorName}_A{alpha}{aa}";

16
tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Numerics;
@ -16,8 +16,6 @@ using SixLabors.Shapes;
namespace SixLabors.ImageSharp.Tests.Drawing
{
public class FillRegionProcessorTests
{
@ -35,8 +33,9 @@ namespace SixLabors.ImageSharp.Tests.Drawing
var brush = new Mock<IBrush>();
var region = new MockRegion2(bounds);
var options = new GraphicsOptions(antialias)
var options = new GraphicsOptions
{
Antialias = antialias,
AntialiasSubpixelDepth = 1
};
var processor = new FillRegionProcessor(brush.Object, region, options);
@ -51,7 +50,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
var bounds = new Rectangle(-100, -10, 10, 10);
var brush = new Mock<IBrush>();
var options = new GraphicsOptions(true);
var options = new GraphicsOptions { Antialias = true };
var processor = new FillRegionProcessor(brush.Object, new MockRegion1(), options);
var img = new Image<Rgba32>(10, 10);
processor.Execute(img, bounds);
@ -77,7 +76,8 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
img.Mutate(x => x.Fill(Rgba32.Transparent));
img.Mutate(ctx => {
img.Mutate(ctx =>
{
ctx.DrawLines(
Rgba32.Red,
0.984252f,
@ -90,7 +90,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
new PointF(104.782608f, 1075.13245f),
new PointF(104.782608f, 1075.13245f)
);
}
}
);
}
}
@ -98,7 +98,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
[Fact]
public void DoesNotThrowFillingTriangle()
{
using(var image = new Image<Rgba32>(28, 28))
using (var image = new Image<Rgba32>(28, 28))
{
var path = new Polygon(
new LinearLineSegment(new PointF(17.11f, 13.99659f), new PointF(14.01433f, 27.06201f)),

24
tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs

@ -156,10 +156,12 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
TPixel bgColor = image[0, 0];
var options = new GraphicsOptions(false)
{
ColorBlendingMode = blenderMode, BlendPercentage = blendPercentage
};
var options = new GraphicsOptions
{
Antialias = false,
ColorBlendingMode = blenderMode,
BlendPercentage = blendPercentage
};
if (triggerFillRegion)
{
@ -173,13 +175,13 @@ namespace SixLabors.ImageSharp.Tests.Drawing
}
var testOutputDetails = new
{
triggerFillRegion = triggerFillRegion,
newColorName = newColorName,
alpha = alpha,
blenderMode = blenderMode,
blendPercentage = blendPercentage
};
{
triggerFillRegion = triggerFillRegion,
newColorName = newColorName,
alpha = alpha,
blenderMode = blenderMode,
blendPercentage = blendPercentage
};
image.DebugSave(
provider,

2
tests/ImageSharp.Tests/Drawing/Paths/DrawPathCollection.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths
{
public class DrawPathCollection : BaseImageOperationsExtensionTest
{
GraphicsOptions noneDefault = new GraphicsOptions(false);
GraphicsOptions noneDefault = new GraphicsOptions { Antialias = false };
Color color = Color.HotPink;
Pen pen = Pens.Solid(Rgba32.HotPink, 1);
IPath path1 = new Path(new LinearLineSegment(new SixLabors.Primitives.PointF[] {

2
tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths
{
public class FillPath : BaseImageOperationsExtensionTest
{
GraphicsOptions noneDefault = new GraphicsOptions(false);
GraphicsOptions noneDefault = new GraphicsOptions { Antialias = false };
Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Rgba32.HotPink);
IPath path = new Path(new LinearLineSegment(new SixLabors.Primitives.PointF[] {

2
tests/ImageSharp.Tests/Drawing/Paths/FillPathCollection.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths
{
public class FillPathCollection : BaseImageOperationsExtensionTest
{
GraphicsOptions noneDefault = new GraphicsOptions(false);
GraphicsOptions noneDefault = new GraphicsOptions { Antialias = false };
Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Rgba32.HotPink);
IPath path1 = new Path(new LinearLineSegment(new SixLabors.Primitives.PointF[] {

2
tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths
{
public class FillPolygon : BaseImageOperationsExtensionTest
{
GraphicsOptions noneDefault = new GraphicsOptions(false);
GraphicsOptions noneDefault = new GraphicsOptions { Antialias = false };
Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Rgba32.HotPink);
SixLabors.Primitives.PointF[] path = {

2
tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths
{
public class FillRectangle : BaseImageOperationsExtensionTest
{
GraphicsOptions noneDefault = new GraphicsOptions(false);
GraphicsOptions noneDefault = new GraphicsOptions { Antialias = false };
Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Rgba32.HotPink);
SixLabors.Primitives.Rectangle rectangle = new SixLabors.Primitives.Rectangle(10, 10, 77, 76);

29
tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
}
}
}
[Theory]
[WithBlankImages(nameof(modes), 250, 250, PixelTypes.Rgba32)]
@ -46,7 +46,8 @@ namespace SixLabors.ImageSharp.Tests.Drawing
Color.DarkBlue,
new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY)
)
.Fill(new GraphicsOptions(true) { ColorBlendingMode = blending, AlphaCompositionMode=composition },
.Fill(
new GraphicsOptions { Antialias = true, ColorBlendingMode = blending, AlphaCompositionMode = composition },
Color.HotPink,
new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY))
);
@ -73,12 +74,12 @@ namespace SixLabors.ImageSharp.Tests.Drawing
new Rectangle(0 * scaleX, 40 * scaleY, 100 * scaleX, 20 * scaleY)));
img.Mutate(
x => x.Fill(
new GraphicsOptions(true) { ColorBlendingMode = blending, AlphaCompositionMode = composition },
new GraphicsOptions { Antialias = true, ColorBlendingMode = blending, AlphaCompositionMode = composition },
Color.HotPink,
new Rectangle(20 * scaleX, 0 * scaleY, 30 * scaleX, 100 * scaleY)));
img.Mutate(
x => x.Fill(
new GraphicsOptions(true) { ColorBlendingMode = blending, AlphaCompositionMode = composition },
new GraphicsOptions { Antialias = true, ColorBlendingMode = blending, AlphaCompositionMode = composition },
Color.Transparent,
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))
);
@ -105,7 +106,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
new Rectangle(0 * scaleX, 40, 100 * scaleX, 20 * scaleY)));
img.Mutate(
x => x.Fill(
new GraphicsOptions(true) { ColorBlendingMode = blending, AlphaCompositionMode = composition },
new GraphicsOptions { Antialias = true, ColorBlendingMode = blending, AlphaCompositionMode = composition },
Color.HotPink,
new Rectangle(20 * scaleX, 0, 30 * scaleX, 100 * scaleY)));
@ -113,7 +114,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
img.Mutate(
x => x.Fill(
new GraphicsOptions(true) { ColorBlendingMode = blending, AlphaCompositionMode = composition },
new GraphicsOptions { Antialias = true, ColorBlendingMode = blending, AlphaCompositionMode = composition },
transparentRed,
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))
);
@ -130,7 +131,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
PixelAlphaCompositionMode composition)
where TPixel : struct, IPixel<TPixel>
{
using(Image<TPixel> dstImg = provider.GetImage(), srcImg = provider.GetImage())
using (Image<TPixel> dstImg = provider.GetImage(), srcImg = provider.GetImage())
{
int scaleX = (dstImg.Width / 100);
int scaleY = (dstImg.Height / 100);
@ -146,13 +147,13 @@ namespace SixLabors.ImageSharp.Tests.Drawing
new Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY)));
dstImg.Mutate(
x => x.DrawImage(srcImg, new GraphicsOptions(true) { ColorBlendingMode = blending, AlphaCompositionMode = composition })
);
x => x.DrawImage(srcImg, new GraphicsOptions { Antialias = true, ColorBlendingMode = blending, AlphaCompositionMode = composition })
);
VerifyImage(provider, blending, composition, dstImg);
}
}
private static void VerifyImage<TPixel>(
TestImageProvider<TPixel> provider,
PixelColorBlendingMode blending,
@ -165,13 +166,13 @@ namespace SixLabors.ImageSharp.Tests.Drawing
new { composition, blending },
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
var comparer = ImageComparer.TolerantPercentage(0.01f, 3);
img.CompareFirstFrameToReferenceOutput(comparer,
provider,
new { composition, blending },
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
appendSourceFileOrDescription: false);
}
}
}
}

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

@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
public void FillsForEachACharacterWhenBrushSetAndNotPen()
{
this.operations.DrawText(
new TextGraphicsOptions(true),
new TextGraphicsOptions { Antialias = true },
"123",
this.Font,
Brushes.Solid(Color.Red),
@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
[Fact]
public void FillsForEachACharacterWhenBrushSet()
{
this.operations.DrawText(new TextGraphicsOptions(true), "123", this.Font, Brushes.Solid(Color.Red), Vector2.Zero);
this.operations.DrawText(new TextGraphicsOptions { Antialias = true }, "123", this.Font, Brushes.Solid(Color.Red), Vector2.Zero);
this.Verify<DrawTextProcessor>(0);
}
@ -64,7 +64,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
[Fact]
public void FillsForEachACharacterWhenColorSet()
{
this.operations.DrawText(new TextGraphicsOptions(true), "123", this.Font, Color.Red, Vector2.Zero);
this.operations.DrawText(new TextGraphicsOptions { Antialias = true }, "123", this.Font, Color.Red, Vector2.Zero);
var processor = this.Verify<DrawTextProcessor>(0);
@ -87,7 +87,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
public void DrawForEachACharacterWhenPenSetAndNotBrush()
{
this.operations.DrawText(
new TextGraphicsOptions(true),
new TextGraphicsOptions { Antialias = true },
"123",
this.Font,
null,
@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
[Fact]
public void DrawForEachACharacterWhenPenSet()
{
this.operations.DrawText(new TextGraphicsOptions(true), "123", this.Font, Pens.Dash(Color.Red, 1), Vector2.Zero);
this.operations.DrawText(new TextGraphicsOptions { Antialias = true }, "123", this.Font, Pens.Dash(Color.Red, 1), Vector2.Zero);
this.Verify<DrawTextProcessor>(0);
}
@ -132,7 +132,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
public void DrawForEachACharacterWhenPenSetAndFillFroEachWhenBrushSet()
{
this.operations.DrawText(
new TextGraphicsOptions(true),
new TextGraphicsOptions { Antialias = true },
"123",
this.Font,
Brushes.Solid(Color.Red),

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

@ -55,8 +55,9 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
var scaledFont = new Font(font, scalingFactor * font.Size);
var center = new PointF(img.Width / 2, img.Height / 2);
var textGraphicOptions = new TextGraphicsOptions(true)
var textGraphicOptions = new TextGraphicsOptions
{
Antialias = true,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
@ -222,7 +223,11 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
string text = Repeat("Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!\n",
20);
var textOptions = new TextGraphicsOptions(true) { WrapTextWidth = 1000 };
var textOptions = new TextGraphicsOptions
{
Antialias = true,
WrapTextWidth = 1000
};
string details = fontName.Replace(" ", "");

128
tests/ImageSharp.Tests/Drawing/Text/TextGraphicsOptionsTests.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Xunit;
@ -9,16 +11,129 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
{
public class TextGraphicsOptionsTests
{
private readonly TextGraphicsOptions newTextGraphicsOptions = new TextGraphicsOptions();
private readonly TextGraphicsOptions defaultTextGraphicsOptions = TextGraphicsOptions.Default;
private readonly TextGraphicsOptions cloneTextGraphicsOptions = TextGraphicsOptions.Default.Clone();
[Fact]
public void DefaultTextGraphicsOptionsIsNotNull() => Assert.True(this.defaultTextGraphicsOptions != null);
[Fact]
public void DefaultTextGraphicsOptionsAntialias()
{
Assert.True(this.newTextGraphicsOptions.Antialias);
Assert.True(this.defaultTextGraphicsOptions.Antialias);
Assert.True(this.cloneTextGraphicsOptions.Antialias);
}
[Fact]
public void DefaultTextGraphicsOptionsAntialiasSuppixelDepth()
{
const int Expected = 16;
Assert.Equal(Expected, this.newTextGraphicsOptions.AntialiasSubpixelDepth);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.AntialiasSubpixelDepth);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.AntialiasSubpixelDepth);
}
[Fact]
public void DefaultTextGraphicsOptionsBlendPercentage()
{
const float Expected = 1F;
Assert.Equal(Expected, this.newTextGraphicsOptions.BlendPercentage);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.BlendPercentage);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.BlendPercentage);
}
[Fact]
public void DefaultTextGraphicsOptionsColorBlendingMode()
{
const PixelColorBlendingMode Expected = PixelColorBlendingMode.Normal;
Assert.Equal(Expected, this.newTextGraphicsOptions.ColorBlendingMode);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.ColorBlendingMode);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.ColorBlendingMode);
}
[Fact]
public void DefaultTextGraphicsOptionsAlphaCompositionMode()
{
const PixelAlphaCompositionMode Expected = PixelAlphaCompositionMode.SrcOver;
Assert.Equal(Expected, this.newTextGraphicsOptions.AlphaCompositionMode);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.AlphaCompositionMode);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.AlphaCompositionMode);
}
[Fact]
public void DefaultTextGraphicsOptionsApplyKerning()
{
const bool Expected = true;
Assert.Equal(Expected, this.newTextGraphicsOptions.ApplyKerning);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.ApplyKerning);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.ApplyKerning);
}
[Fact]
public void DefaultTextGraphicsOptionsHorizontalAlignment()
{
const HorizontalAlignment Expected = HorizontalAlignment.Left;
Assert.Equal(Expected, this.newTextGraphicsOptions.HorizontalAlignment);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.HorizontalAlignment);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.HorizontalAlignment);
}
[Fact]
public void DefaultTextGraphicsOptionsVerticalAlignment()
{
const VerticalAlignment Expected = VerticalAlignment.Top;
Assert.Equal(Expected, this.newTextGraphicsOptions.VerticalAlignment);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.VerticalAlignment);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.VerticalAlignment);
}
[Fact]
public void DefaultTextGraphicsOptionsDpiX()
{
const float Expected = 72F;
Assert.Equal(Expected, this.newTextGraphicsOptions.DpiX);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.DpiX);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.DpiX);
}
[Fact]
public void DefaultTextGraphicsOptionsDpiY()
{
const float Expected = 72F;
Assert.Equal(Expected, this.newTextGraphicsOptions.DpiY);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.DpiY);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.DpiY);
}
[Fact]
public void DefaultTextGraphicsOptionsTabWidth()
{
const float Expected = 4F;
Assert.Equal(Expected, this.newTextGraphicsOptions.TabWidth);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.TabWidth);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.TabWidth);
}
[Fact]
public void DefaultTextGraphicsOptionsWrapTextWidth()
{
const float Expected = 0F;
Assert.Equal(Expected, this.newTextGraphicsOptions.WrapTextWidth);
Assert.Equal(Expected, this.defaultTextGraphicsOptions.WrapTextWidth);
Assert.Equal(Expected, this.cloneTextGraphicsOptions.WrapTextWidth);
}
[Fact]
public void ExplicitCastOfGraphicsOptions()
{
var opt = new GraphicsOptions(false)
TextGraphicsOptions textOptions = new GraphicsOptions
{
Antialias = false,
AntialiasSubpixelDepth = 99
};
TextGraphicsOptions textOptions = opt;
Assert.False(textOptions.Antialias);
Assert.Equal(99, textOptions.AntialiasSubpixelDepth);
}
@ -26,8 +141,9 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
[Fact]
public void ImplicitCastToGraphicsOptions()
{
var textOptions = new TextGraphicsOptions(false)
var textOptions = new TextGraphicsOptions
{
Antialias = false,
AntialiasSubpixelDepth = 99
};
@ -37,4 +153,4 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
Assert.Equal(99, opt.AntialiasSubpixelDepth);
}
}
}
}

65
tests/ImageSharp.Tests/GraphicsOptionsTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats;
@ -8,14 +8,65 @@ namespace SixLabors.ImageSharp.Tests
{
public class GraphicsOptionsTests
{
private readonly GraphicsOptions newGraphicsOptions = new GraphicsOptions();
private readonly GraphicsOptions defaultGraphicsOptions = GraphicsOptions.Default;
private readonly GraphicsOptions cloneGraphicsOptions = GraphicsOptions.Default.Clone();
[Fact]
public void DefaultGraphicsOptionsIsNotNull() => Assert.True(this.defaultGraphicsOptions != null);
[Fact]
public void DefaultGraphicsOptionsAntialias()
{
Assert.True(this.newGraphicsOptions.Antialias);
Assert.True(this.defaultGraphicsOptions.Antialias);
Assert.True(this.cloneGraphicsOptions.Antialias);
}
[Fact]
public void DefaultGraphicsOptionsAntialiasSuppixelDepth()
{
const int Expected = 16;
Assert.Equal(Expected, this.newGraphicsOptions.AntialiasSubpixelDepth);
Assert.Equal(Expected, this.defaultGraphicsOptions.AntialiasSubpixelDepth);
Assert.Equal(Expected, this.cloneGraphicsOptions.AntialiasSubpixelDepth);
}
[Fact]
public void DefaultGraphicsOptionsBlendPercentage()
{
const float Expected = 1F;
Assert.Equal(Expected, this.newGraphicsOptions.BlendPercentage);
Assert.Equal(Expected, this.defaultGraphicsOptions.BlendPercentage);
Assert.Equal(Expected, this.cloneGraphicsOptions.BlendPercentage);
}
[Fact]
public void DefaultGraphicsOptionsColorBlendingMode()
{
const PixelColorBlendingMode Expected = PixelColorBlendingMode.Normal;
Assert.Equal(Expected, this.newGraphicsOptions.ColorBlendingMode);
Assert.Equal(Expected, this.defaultGraphicsOptions.ColorBlendingMode);
Assert.Equal(Expected, this.cloneGraphicsOptions.ColorBlendingMode);
}
[Fact]
public void DefaultGraphicsOptionsAlphaCompositionMode()
{
const PixelAlphaCompositionMode Expected = PixelAlphaCompositionMode.SrcOver;
Assert.Equal(Expected, this.newGraphicsOptions.AlphaCompositionMode);
Assert.Equal(Expected, this.defaultGraphicsOptions.AlphaCompositionMode);
Assert.Equal(Expected, this.cloneGraphicsOptions.AlphaCompositionMode);
}
[Fact]
public void IsOpaqueColor()
{
Assert.True(new GraphicsOptions(true).IsOpaqueColorWithoutBlending(Rgba32.Red));
Assert.False(new GraphicsOptions(true, 0.5f).IsOpaqueColorWithoutBlending(Rgba32.Red));
Assert.False(new GraphicsOptions(true).IsOpaqueColorWithoutBlending(Rgba32.Transparent));
Assert.False(new GraphicsOptions(true, PixelColorBlendingMode.Lighten, 1).IsOpaqueColorWithoutBlending(Rgba32.Red));
Assert.False(new GraphicsOptions(true, PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.DestOver, 1).IsOpaqueColorWithoutBlending(Rgba32.Red));
Assert.True(new GraphicsOptions().IsOpaqueColorWithoutBlending(Rgba32.Red));
Assert.False(new GraphicsOptions { BlendPercentage = .5F }.IsOpaqueColorWithoutBlending(Rgba32.Red));
Assert.False(new GraphicsOptions().IsOpaqueColorWithoutBlending(Rgba32.Transparent));
Assert.False(new GraphicsOptions { ColorBlendingMode = PixelColorBlendingMode.Lighten, BlendPercentage = 1F }.IsOpaqueColorWithoutBlending(Rgba32.Red));
Assert.False(new GraphicsOptions { ColorBlendingMode = PixelColorBlendingMode.Normal, AlphaCompositionMode = PixelAlphaCompositionMode.DestOver, BlendPercentage = 1f }.IsOpaqueColorWithoutBlending(Rgba32.Red));
}
}
}
}

4
tests/ImageSharp.Tests/Issues/Issue412.cs

@ -20,14 +20,14 @@ namespace SixLabors.ImageSharp.Tests.Issues
for (var i = 0; i < 40; ++i)
{
context.DrawLines(
new GraphicsOptions(false),
new GraphicsOptions { Antialias = false },
Color.Black,
1,
new PointF(i, 0.1066f),
new PointF(i, 10.1066f));
context.DrawLines(
new GraphicsOptions(false),
new GraphicsOptions { Antialias = false },
Color.Red,
1,
new PointF(i, 15.1066f),

2
tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs

@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Tests.Processing
public BaseImageOperationsExtensionTest()
{
this.options = new GraphicsOptions(false);
this.options = new GraphicsOptions { Antialias = false };
this.source = new Image<Rgba32>(91 + 324, 123 + 56);
this.rect = new Rectangle(91, 123, 324, 56); // make this random?
this.internalOperations = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(this.source, false);

Loading…
Cancel
Save