Browse Source

Convert options into classes.

pull/1055/head
James Jackson-South 7 years ago
parent
commit
7d028001bf
  1. 3
      src/ImageSharp.Drawing/Processing/GradientBrush.cs
  2. 140
      src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs
  3. 120
      src/ImageSharp/GraphicsOptions.cs
  4. 2
      tests/ImageSharp.Tests/Drawing/Paths/DrawPathCollection.cs
  5. 2
      tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs
  6. 2
      tests/ImageSharp.Tests/Drawing/Paths/FillPathCollection.cs
  7. 2
      tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs
  8. 4
      tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs
  9. 11
      tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffCompositorTests.cs

3
src/ImageSharp.Drawing/Processing/GradientBrush.cs

@ -118,9 +118,6 @@ namespace SixLabors.ImageSharp.Processing
else else
{ {
float onLocalGradient = (positionOnCompleteGradient - from.Ratio) / (to.Ratio - from.Ratio); float onLocalGradient = (positionOnCompleteGradient - from.Ratio) / (to.Ratio - from.Ratio);
// TODO: this should be changeble for different gradienting functions.
// TODO: Is the comment above still relevent?
return new Color(Vector4.Lerp((Vector4)from.Color, (Vector4)to.Color, onLocalGradient)).ToPixel<TPixel>(); return new Color(Vector4.Lerp((Vector4)from.Color, (Vector4)to.Color, onLocalGradient)).ToPixel<TPixel>();
} }
} }

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

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.Fonts; using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -9,68 +10,87 @@ namespace SixLabors.ImageSharp.Processing
/// <summary> /// <summary>
/// Options for influencing the drawing functions. /// Options for influencing the drawing functions.
/// </summary> /// </summary>
public struct TextGraphicsOptions 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> /// <summary>
/// Represents the default <see cref="TextGraphicsOptions"/>. /// Initializes a new instance of the <see cref="TextGraphicsOptions"/> class.
/// </summary> /// </summary>
public static readonly TextGraphicsOptions Default = new TextGraphicsOptions(true); public TextGraphicsOptions()
: this(true)
private const int DefaultTextDpi = 72; {
}
private float? blendPercentage;
private int? antialiasSubpixelDepth;
private bool? antialias;
private bool? applyKerning;
private float? tabWidth;
private float? dpiX;
private float? dpiY;
private HorizontalAlignment? horizontalAlignment;
private VerticalAlignment? verticalAlignment;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TextGraphicsOptions" /> struct. /// Initializes a new instance of the <see cref="TextGraphicsOptions" /> class.
/// </summary> /// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param> /// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
public TextGraphicsOptions(bool enableAntialiasing) public TextGraphicsOptions(bool enableAntialiasing)
{ {
this.applyKerning = true; this.ApplyKerning = true;
this.tabWidth = 4; this.TabWidth = 4F;
this.WrapTextWidth = 0; this.WrapTextWidth = 0;
this.horizontalAlignment = HorizontalAlignment.Left; this.HorizontalAlignment = HorizontalAlignment.Left;
this.verticalAlignment = VerticalAlignment.Top; this.VerticalAlignment = VerticalAlignment.Top;
this.antialiasSubpixelDepth = 16; this.antialiasSubpixelDepth = 16;
this.ColorBlendingMode = PixelColorBlendingMode.Normal; this.ColorBlendingMode = PixelColorBlendingMode.Normal;
this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver; this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver;
this.blendPercentage = 1F; this.blendPercentage = 1F;
this.antialias = enableAntialiasing; this.Antialias = enableAntialiasing;
this.dpiX = DefaultTextDpi; this.dpiX = 72F;
this.dpiY = DefaultTextDpi; this.dpiY = 72F;
} }
/// <summary>
/// Gets the default <see cref="TextGraphicsOptions"/> instance.
/// </summary>
public static TextGraphicsOptions Default { get; } = Lazy.Value;
/// <summary> /// <summary>
/// Gets or sets a value indicating whether antialiasing should be applied. /// Gets or sets a value indicating whether antialiasing should be applied.
/// </summary> /// </summary>
public bool Antialias { get => this.antialias ?? true; set => this.antialias = value; } public bool Antialias { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled. /// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled.
/// </summary> /// </summary>
public int AntialiasSubpixelDepth { get => this.antialiasSubpixelDepth ?? 16; set => this.antialiasSubpixelDepth = value; } public int AntialiasSubpixelDepth
{
get
{
return this.antialiasSubpixelDepth;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.AntialiasSubpixelDepth));
this.antialiasSubpixelDepth = value;
}
}
/// <summary> /// <summary>
/// Gets or sets a value indicating the blending percentage to apply to the drawing operation /// Gets or sets a value indicating the blending percentage to apply to the drawing operation.
/// </summary> /// </summary>
public float BlendPercentage { get => this.blendPercentage ?? 1F; set => this.blendPercentage = NumberUtils.ClampFloat(value, 0, 1F); } public float BlendPercentage
{
get
{
return this.blendPercentage;
}
set
{
Guard.MustBeBetweenOrEqualTo(value, 0, 1F, nameof(this.BlendPercentage));
this.blendPercentage = value;
}
}
// In the future we could expose a PixelBlender<TPixel> directly on here // In the future we could expose a PixelBlender<TPixel> directly on here
// or some forms of PixelBlender factory for each pixel type. Will need // or some forms of PixelBlender factory for each pixel type. Will need
@ -89,27 +109,63 @@ namespace SixLabors.ImageSharp.Processing
/// <summary> /// <summary>
/// Gets or sets a value indicating whether the text should be drawing with kerning enabled. /// Gets or sets a value indicating whether the text should be drawing with kerning enabled.
/// </summary> /// </summary>
public bool ApplyKerning { get => this.applyKerning ?? true; set => this.applyKerning = value; } public bool ApplyKerning { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating the number of space widths a tab should lock to. /// Gets or sets a value indicating the number of space widths a tab should lock to.
/// </summary> /// </summary>
public float TabWidth { get => this.tabWidth ?? 4; set => this.tabWidth = value; } public float TabWidth
{
get
{
return this.tabWidth;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.TabWidth));
this.tabWidth = value;
}
}
/// <summary> /// <summary>
/// Gets or sets a value indicating if greater than zero determine the width at which text should wrap. /// Gets or sets a value, if greater than 0, indicating the width at which text should wrap.
/// </summary> /// </summary>
public float WrapTextWidth { get; set; } public float WrapTextWidth { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating the DPI to render text along the X axis. /// Gets or sets a value indicating the DPI to render text along the X axis.
/// </summary> /// </summary>
public float DpiX { get => this.dpiX ?? DefaultTextDpi; set => this.dpiX = value; } public float DpiX
{
get
{
return this.dpiX;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.DpiX));
this.dpiX = value;
}
}
/// <summary> /// <summary>
/// Gets or sets a value indicating the DPI to render text along the Y axis. /// Gets or sets a value indicating the DPI to render text along the Y axis.
/// </summary> /// </summary>
public float DpiY { get => this.dpiY ?? DefaultTextDpi; set => this.dpiY = value; } public float DpiY
{
get
{
return this.dpiY;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.DpiY));
this.dpiY = value;
}
}
/// <summary> /// <summary>
/// Gets or sets a value indicating how to align the text relative to the rendering space. /// Gets or sets a value indicating how to align the text relative to the rendering space.
@ -117,12 +173,12 @@ namespace SixLabors.ImageSharp.Processing
/// defined by the location and width, if <see cref="WrapTextWidth"/> equals zero, and thus /// 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. /// wrapping disabled, then the alignment is relative to the drawing location.
/// </summary> /// </summary>
public HorizontalAlignment HorizontalAlignment { get => this.horizontalAlignment ?? HorizontalAlignment.Left; set => this.horizontalAlignment = value; } public HorizontalAlignment HorizontalAlignment { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating how to align the text relative to the rendering space. /// Gets or sets a value indicating how to align the text relative to the rendering space.
/// </summary> /// </summary>
public VerticalAlignment VerticalAlignment { get => this.verticalAlignment ?? VerticalAlignment.Top; set => this.verticalAlignment = value; } public VerticalAlignment VerticalAlignment { get; set; }
/// <summary> /// <summary>
/// Performs an implicit conversion from <see cref="GraphicsOptions"/> to <see cref="TextGraphicsOptions"/>. /// Performs an implicit conversion from <see cref="GraphicsOptions"/> to <see cref="TextGraphicsOptions"/>.

120
src/ImageSharp/GraphicsOptions.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp namespace SixLabors.ImageSharp
@ -8,108 +9,115 @@ namespace SixLabors.ImageSharp
/// <summary> /// <summary>
/// Options for influencing the drawing functions. /// Options for influencing the drawing functions.
/// </summary> /// </summary>
public struct GraphicsOptions public class GraphicsOptions
{ {
private static readonly Lazy<GraphicsOptions> Lazy = new Lazy<GraphicsOptions>();
private int antialiasSubpixelDepth;
private float blendPercentage;
/// <summary> /// <summary>
/// Represents the default <see cref="GraphicsOptions"/>. /// Initializes a new instance of the <see cref="GraphicsOptions"/> class.
/// </summary> /// </summary>
public static readonly GraphicsOptions Default = new GraphicsOptions(true); public GraphicsOptions()
: this(true)
private float? blendPercentage; {
}
private int? antialiasSubpixelDepth;
private bool? antialias;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="GraphicsOptions"/> struct. /// Initializes a new instance of the <see cref="GraphicsOptions"/> class.
/// </summary> /// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param> /// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
public GraphicsOptions(bool enableAntialiasing) public GraphicsOptions(bool enableAntialiasing)
: this(enableAntialiasing, 1F)
{ {
this.ColorBlendingMode = PixelColorBlendingMode.Normal;
this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver;
this.blendPercentage = 1;
this.antialiasSubpixelDepth = 16;
this.antialias = enableAntialiasing;
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="GraphicsOptions"/> struct. /// Initializes a new instance of the <see cref="GraphicsOptions"/> class.
/// </summary> /// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param> /// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
/// <param name="opacity">blending percentage to apply to the drawing operation</param> /// <param name="blendPercentage">The blending percentage to apply to the drawing operation</param>
public GraphicsOptions(bool enableAntialiasing, float opacity) public GraphicsOptions(bool enableAntialiasing, float blendPercentage)
: this(enableAntialiasing, PixelColorBlendingMode.Normal, blendPercentage)
{ {
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity));
this.ColorBlendingMode = PixelColorBlendingMode.Normal;
this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver;
this.blendPercentage = opacity;
this.antialiasSubpixelDepth = 16;
this.antialias = enableAntialiasing;
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="GraphicsOptions"/> struct. /// Initializes a new instance of the <see cref="GraphicsOptions"/> class.
/// </summary> /// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param> /// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
/// <param name="opacity">blending percentage to apply to the drawing operation</param> /// <param name="blending">The color blending mode to apply to the drawing operation</param>
/// <param name="blending">color blending mode to apply to the drawing operation</param> /// <param name="blendPercentage">The blending percentage to apply to the drawing operation</param>
public GraphicsOptions(bool enableAntialiasing, PixelColorBlendingMode blending, float opacity) public GraphicsOptions(
bool enableAntialiasing,
PixelColorBlendingMode blending,
float blendPercentage)
: this(enableAntialiasing, blending, PixelAlphaCompositionMode.SrcOver, blendPercentage)
{ {
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity));
this.ColorBlendingMode = blending;
this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver;
this.blendPercentage = opacity;
this.antialiasSubpixelDepth = 16;
this.antialias = enableAntialiasing;
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="GraphicsOptions"/> struct. /// Initializes a new instance of the <see cref="GraphicsOptions"/> class.
/// </summary> /// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param> /// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
/// <param name="opacity">blending percentage to apply to the drawing operation</param> /// <param name="blending">The color blending mode to apply to the drawing operation</param>
/// <param name="blending">color blending mode to apply to the drawing operation</param> /// <param name="composition">The alpha composition mode to apply to the drawing operation</param>
/// <param name="composition">alpha composition mode to apply to the drawing operation</param> /// <param name="blendPercentage">The blending percentage to apply to the drawing operation</param>
public GraphicsOptions(bool enableAntialiasing, PixelColorBlendingMode blending, PixelAlphaCompositionMode composition, float opacity) public GraphicsOptions(
bool enableAntialiasing,
PixelColorBlendingMode blending,
PixelAlphaCompositionMode composition,
float blendPercentage)
{ {
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity));
this.ColorBlendingMode = blending; this.ColorBlendingMode = blending;
this.AlphaCompositionMode = composition; this.AlphaCompositionMode = composition;
this.blendPercentage = opacity; this.BlendPercentage = blendPercentage;
this.antialiasSubpixelDepth = 16; this.AntialiasSubpixelDepth = 16;
this.antialias = enableAntialiasing; this.Antialias = enableAntialiasing;
} }
/// <summary>
/// Gets the default <see cref="GraphicsOptions"/> instance.
/// </summary>
public static GraphicsOptions Default { get; } = Lazy.Value;
/// <summary> /// <summary>
/// Gets or sets a value indicating whether antialiasing should be applied. /// Gets or sets a value indicating whether antialiasing should be applied.
/// </summary> /// </summary>
public bool Antialias public bool Antialias { get; set; }
{
get => this.antialias ?? true;
set => this.antialias = value;
}
/// <summary> /// <summary>
/// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled. /// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled.
/// </summary> /// </summary>
public int AntialiasSubpixelDepth public int AntialiasSubpixelDepth
{ {
get => this.antialiasSubpixelDepth ?? 16; get
set => this.antialiasSubpixelDepth = value; {
return this.antialiasSubpixelDepth;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.AntialiasSubpixelDepth));
this.antialiasSubpixelDepth = value;
}
} }
/// <summary> /// <summary>
/// Gets or sets a value indicating the blending percentage to apply to the drawing operation /// Gets or sets a value indicating the blending percentage to apply to the drawing operation.
/// </summary> /// </summary>
public float BlendPercentage public float BlendPercentage
{ {
get => (this.blendPercentage ?? 1).Clamp(0, 1); get
set => this.blendPercentage = value; {
return this.blendPercentage;
}
set
{
Guard.MustBeBetweenOrEqualTo(value, 0, 1F, nameof(this.BlendPercentage));
this.blendPercentage = value;
}
} }
// In the future we could expose a PixelBlender<TPixel> directly on here // In the future we could expose a PixelBlender<TPixel> directly on here

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

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths
{ {
public class DrawPathCollection : BaseImageOperationsExtensionTest public class DrawPathCollection : BaseImageOperationsExtensionTest
{ {
GraphicsOptions noneDefault = new GraphicsOptions(); GraphicsOptions noneDefault = new GraphicsOptions(false);
Color color = Color.HotPink; Color color = Color.HotPink;
Pen pen = Pens.Solid(Rgba32.HotPink, 1); Pen pen = Pens.Solid(Rgba32.HotPink, 1);
IPath path1 = new Path(new LinearLineSegment(new SixLabors.Primitives.PointF[] { 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 public class FillPath : BaseImageOperationsExtensionTest
{ {
GraphicsOptions noneDefault = new GraphicsOptions(); GraphicsOptions noneDefault = new GraphicsOptions(false);
Color color = Color.HotPink; Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Rgba32.HotPink); SolidBrush brush = Brushes.Solid(Rgba32.HotPink);
IPath path = new Path(new LinearLineSegment(new SixLabors.Primitives.PointF[] { 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 public class FillPathCollection : BaseImageOperationsExtensionTest
{ {
GraphicsOptions noneDefault = new GraphicsOptions(); GraphicsOptions noneDefault = new GraphicsOptions(false);
Color color = Color.HotPink; Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Rgba32.HotPink); SolidBrush brush = Brushes.Solid(Rgba32.HotPink);
IPath path1 = new Path(new LinearLineSegment(new SixLabors.Primitives.PointF[] { 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 public class FillPolygon : BaseImageOperationsExtensionTest
{ {
GraphicsOptions noneDefault = new GraphicsOptions(); GraphicsOptions noneDefault = new GraphicsOptions(false);
Color color = Color.HotPink; Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Rgba32.HotPink); SolidBrush brush = Brushes.Solid(Rgba32.HotPink);
SixLabors.Primitives.PointF[] path = { SixLabors.Primitives.PointF[] path = {

4
tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.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. // Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths
{ {
public class FillRectangle : BaseImageOperationsExtensionTest public class FillRectangle : BaseImageOperationsExtensionTest
{ {
GraphicsOptions noneDefault = new GraphicsOptions(); GraphicsOptions noneDefault = new GraphicsOptions(false);
Color color = Color.HotPink; Color color = Color.HotPink;
SolidBrush brush = Brushes.Solid(Rgba32.HotPink); SolidBrush brush = Brushes.Solid(Rgba32.HotPink);
SixLabors.Primitives.Rectangle rectangle = new SixLabors.Primitives.Rectangle(10, 10, 77, 76); SixLabors.Primitives.Rectangle rectangle = new SixLabors.Primitives.Rectangle(10, 10, 77, 76);

11
tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffCompositorTests.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. // Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelBlenders namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelBlenders
@ -36,9 +36,10 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelBlenders
using (Image<Rgba32> src = srcFile.CreateRgba32Image()) using (Image<Rgba32> src = srcFile.CreateRgba32Image())
using (Image<Rgba32> dest = provider.GetImage()) using (Image<Rgba32> dest = provider.GetImage())
{ {
GraphicsOptions options = new GraphicsOptions var options = new GraphicsOptions
{ {
AlphaCompositionMode = mode Antialias = false,
AlphaCompositionMode = mode
}; };
using (Image<Rgba32> res = dest.Clone(x => x.DrawImage(src, options))) using (Image<Rgba32> res = dest.Clone(x => x.DrawImage(src, options)))
@ -53,4 +54,4 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelBlenders
} }
} }
} }
} }

Loading…
Cancel
Save