// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using SixLabors.Fonts; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Processing { /// /// Options for influencing the drawing functions. /// public struct TextGraphicsOptions { /// /// Represents the default . /// public static readonly TextGraphicsOptions Default = new TextGraphicsOptions(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; /// /// Initializes a new instance of the struct. /// /// If set to true [enable antialiasing]. public TextGraphicsOptions(bool enableAntialiasing) { this.applyKerning = true; this.tabWidth = 4; 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 = DefaultTextDpi; this.dpiY = DefaultTextDpi; } /// /// Gets or sets a value indicating whether antialiasing should be applied. /// public bool Antialias { get => this.antialias ?? true; set => this.antialias = value; } /// /// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled. /// public int AntialiasSubpixelDepth { get => this.antialiasSubpixelDepth ?? 16; set => this.antialiasSubpixelDepth = value; } /// /// Gets or sets a value indicating the blending percentage to apply to the drawing operation /// public float BlendPercentage { get => this.blendPercentage ?? 1F; set => this.blendPercentage = NumberUtils.ClampFloat(value, 0, 1F); } // 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 /// public PixelColorBlendingMode ColorBlendingMode { get; set; } /// /// Gets or sets a value indicating the color blending percentage to apply to the drawing operation /// public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } /// /// Gets or sets a value indicating whether the text should be drawing with kerning enabled. /// public bool ApplyKerning { get => this.applyKerning ?? true; set => this.applyKerning = value; } /// /// Gets or sets a value indicating the number of space widths a tab should lock to. /// public float TabWidth { get => this.tabWidth ?? 4; set => this.tabWidth = value; } /// /// Gets or sets a value indicating if greater than zero determine the width at which text should wrap. /// public float WrapTextWidth { get; set; } /// /// Gets or sets a value indicating the DPI to render text along the X axis. /// public float DpiX { get => this.dpiX ?? DefaultTextDpi; set => this.dpiX = value; } /// /// Gets or sets a value indicating the DPI to render text along the Y axis. /// public float DpiY { get => this.dpiY ?? DefaultTextDpi; set => 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. /// public HorizontalAlignment HorizontalAlignment { get => this.horizontalAlignment ?? HorizontalAlignment.Left; set => this.horizontalAlignment = value; } /// /// Gets or sets a value indicating how to align the text relative to the rendering space. /// public VerticalAlignment VerticalAlignment { get => this.verticalAlignment ?? VerticalAlignment.Top; set => this.verticalAlignment = value; } /// /// Performs an implicit conversion from to . /// /// The options. /// /// The result of the conversion. /// public static implicit operator TextGraphicsOptions(GraphicsOptions options) { return new TextGraphicsOptions(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(options.Antialias) { AntialiasSubpixelDepth = options.AntialiasSubpixelDepth, ColorBlendingMode = options.ColorBlendingMode, AlphaCompositionMode = options.AlphaCompositionMode, BlendPercentage = options.BlendPercentage }; } } }