diff --git a/src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs b/src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs index 23d7aebf5..60a48a31f 100644 --- a/src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs +++ b/src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs @@ -12,51 +12,22 @@ namespace SixLabors.ImageSharp.Processing /// public class TextGraphicsOptions { - private static readonly Lazy Lazy = new Lazy(); - private int antialiasSubpixelDepth; - private float blendPercentage; - private float tabWidth; - private float dpiX; - private float dpiY; - - /// - /// Initializes a new instance of the class. - /// - public TextGraphicsOptions() - : this(true) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// If set to true [enable antialiasing]. - 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; /// /// Gets the default instance. /// - public static TextGraphicsOptions Default { get; } = Lazy.Value; + public static TextGraphicsOptions Default { get; } = new TextGraphicsOptions(); /// /// Gets or sets a value indicating whether antialiasing should be applied. + /// Defaults to true. /// - public bool Antialias { get; set; } + public bool Antialias { get; set; } = true; /// /// 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 directly on here - // or some forms of PixelBlender factory for each pixel type. Will need - // some API thought post V1. - /// - /// 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 . /// - public PixelColorBlendingMode ColorBlendingMode { get; set; } + public PixelColorBlendingMode ColorBlendingMode { get; set; } = PixelColorBlendingMode.Normal; /// /// Gets or sets a value indicating the color blending percentage to apply to the drawing operation + /// Defaults to . /// - public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } + public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } = PixelAlphaCompositionMode.SrcOver; /// /// Gets or sets a value indicating whether the text should be drawing with kerning enabled. + /// Defaults to true; /// - public bool ApplyKerning { get; set; } + public bool ApplyKerning { get; set; } = true; /// /// Gets or sets a value indicating the number of space widths a tab should lock to. + /// Defaults to 4. /// public float TabWidth { @@ -130,11 +101,13 @@ namespace SixLabors.ImageSharp.Processing /// /// Gets or sets a value, if greater than 0, indicating the width at which text should wrap. + /// Defaults to 0. /// public float WrapTextWidth { get; set; } /// - /// 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. /// public float DpiX { @@ -151,7 +124,8 @@ namespace SixLabors.ImageSharp.Processing } /// - /// 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. /// public float DpiY { @@ -172,13 +146,15 @@ namespace SixLabors.ImageSharp.Processing /// If is greater than zero it will align relative to the space /// defined by the location and width, if equals zero, and thus /// wrapping disabled, then the alignment is relative to the drawing location. + /// Defaults to . /// - public HorizontalAlignment HorizontalAlignment { get; set; } + public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Left; /// /// Gets or sets a value indicating how to align the text relative to the rendering space. + /// Defaults to . /// - public VerticalAlignment VerticalAlignment { get; set; } + public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Top; /// /// Performs an implicit conversion from to . @@ -189,8 +165,9 @@ namespace SixLabors.ImageSharp.Processing /// 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 /// 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 }; } + + /// + /// Creates a shallow copy of the . + /// + /// A new options instance. + 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 + }; + } } } diff --git a/tests/ImageSharp.Benchmarks/Drawing/DrawText.cs b/tests/ImageSharp.Benchmarks/Drawing/DrawText.cs index 0982db334..c19961390 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/DrawText.cs +++ b/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(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(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 } } } -} \ No newline at end of file +} diff --git a/tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs b/tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs index c5c1ba5ac..7d8b77659 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs +++ b/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(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 } } } -} \ No newline at end of file +} diff --git a/tests/ImageSharp.Tests/Drawing/DrawLinesTests.cs b/tests/ImageSharp.Tests/Drawing/DrawLinesTests.cs index 2836f8a38..b45fc620b 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawLinesTests.cs +++ b/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(TestImageProvider 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(TestImageProvider 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(TestImageProvider 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( TestImageProvider 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}"; diff --git a/tests/ImageSharp.Tests/Drawing/DrawPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/DrawPolygonTests.cs index 18fde6ad8..4a6cb430a 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawPolygonTests.cs +++ b/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}"; diff --git a/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs b/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs index 8aae342cb..031e732ea 100644 --- a/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs +++ b/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 }; diff --git a/tests/ImageSharp.Tests/Drawing/FillPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/FillPolygonTests.cs index 104237ec3..22294e76d 100644 --- a/tests/ImageSharp.Tests/Drawing/FillPolygonTests.cs +++ b/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}"; diff --git a/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs b/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs index c0388ea2d..6230d52a1 100644 --- a/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs +++ b/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(); 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(); - var options = new GraphicsOptions(true); + var options = new GraphicsOptions { Antialias = true }; var processor = new FillRegionProcessor(brush.Object, new MockRegion1(), options); var img = new Image(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(28, 28)) + using (var image = new Image(28, 28)) { var path = new Polygon( new LinearLineSegment(new PointF(17.11f, 13.99659f), new PointF(14.01433f, 27.06201f)), diff --git a/tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs b/tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs index a5e745083..1e3688fea 100644 --- a/tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs +++ b/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, diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawPathCollection.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawPathCollection.cs index 5d309d299..f6d9c7fe5 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawPathCollection.cs +++ b/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[] { diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs index cb247ac03..fa1949e67 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs +++ b/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[] { diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillPathCollection.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillPathCollection.cs index 0c4d6ca3c..39e2fc2f9 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillPathCollection.cs +++ b/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[] { diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs index 06a734842..03a827a6a 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs +++ b/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 = { diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs index c1ad059a8..cc108fb54 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs +++ b/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); diff --git a/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs b/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs index da7c865b9..f1a62cf29 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidFillBlendedShapesTests.cs +++ b/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 { - using(Image dstImg = provider.GetImage(), srcImg = provider.GetImage()) + using (Image 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( TestImageProvider 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); } } -} \ No newline at end of file +} diff --git a/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs b/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs index e6866c657..2a39e18cb 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs +++ b/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(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(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(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), diff --git a/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs b/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs index a767a686e..281a51650 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs +++ b/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(" ", ""); diff --git a/tests/ImageSharp.Tests/Drawing/Text/TextGraphicsOptionsTests.cs b/tests/ImageSharp.Tests/Drawing/Text/TextGraphicsOptionsTests.cs index 0885611c6..d15b717bb 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/TextGraphicsOptionsTests.cs +++ b/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); } } -} \ No newline at end of file +} diff --git a/tests/ImageSharp.Tests/GraphicsOptionsTests.cs b/tests/ImageSharp.Tests/GraphicsOptionsTests.cs index 6ff38626d..4e23b1766 100644 --- a/tests/ImageSharp.Tests/GraphicsOptionsTests.cs +++ b/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)); } } -} \ No newline at end of file +} diff --git a/tests/ImageSharp.Tests/Issues/Issue412.cs b/tests/ImageSharp.Tests/Issues/Issue412.cs index 5a590018e..53c65b643 100644 --- a/tests/ImageSharp.Tests/Issues/Issue412.cs +++ b/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), diff --git a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs index 140af9563..cfac8645f 100644 --- a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs +++ b/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(91 + 324, 123 + 56); this.rect = new Rectangle(91, 123, 324, 56); // make this random? this.internalOperations = new FakeImageOperationsProvider.FakeImageOperations(this.source, false);