// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Processing
{
///
/// Options for influencing the drawing functions.
///
public class TextGraphicsOptions
{
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; } = new TextGraphicsOptions();
///
/// Gets or sets a value indicating whether antialiasing should be applied.
/// Defaults to true.
///
public bool Antialias { get; set; } = true;
///
/// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled.
///
public int AntialiasSubpixelDepth
{
get
{
return this.antialiasSubpixelDepth;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.AntialiasSubpixelDepth));
this.antialiasSubpixelDepth = value;
}
}
///
/// Gets or sets a value indicating the blending percentage to apply to the drawing operation.
///
public float BlendPercentage
{
get
{
return this.blendPercentage;
}
set
{
Guard.MustBeBetweenOrEqualTo(value, 0, 1F, nameof(this.BlendPercentage));
this.blendPercentage = value;
}
}
///
/// Gets or sets a value indicating the color blending percentage to apply to the drawing operation.
/// Defaults to .
///
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; } = 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; } = true;
///
/// Gets or sets a value indicating the number of space widths a tab should lock to.
/// Defaults to 4.
///
public float TabWidth
{
get
{
return this.tabWidth;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.TabWidth));
this.tabWidth = value;
}
}
///
/// 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 (Dots Per Inch) to render text along the X axis.
/// Defaults to 72.
///
public float DpiX
{
get
{
return this.dpiX;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.DpiX));
this.dpiX = value;
}
}
///
/// Gets or sets a value indicating the DPI (Dots Per Inch) to render text along the Y axis.
/// Defaults to 72.
///
public float DpiY
{
get
{
return this.dpiY;
}
set
{
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.DpiY));
this.dpiY = value;
}
}
///
/// Gets or sets a value indicating how to align the text relative to the rendering space.
/// 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; } = 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; } = VerticalAlignment.Top;
///
/// Performs an implicit conversion from to .
///
/// The options.
///
/// The result of the conversion.
///
public static implicit operator TextGraphicsOptions(GraphicsOptions options)
{
return new TextGraphicsOptions()
{
Antialias = options.Antialias,
AntialiasSubpixelDepth = options.AntialiasSubpixelDepth,
blendPercentage = options.BlendPercentage,
ColorBlendingMode = options.ColorBlendingMode,
AlphaCompositionMode = options.AlphaCompositionMode
};
}
///
/// Performs an explicit conversion from to .
///
/// The options.
///
/// The result of the conversion.
///
public static explicit operator GraphicsOptions(TextGraphicsOptions options)
{
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
};
}
}
}