// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Drawing { using ImageSharp.PixelFormats; using SixLabors.Fonts; /// /// Options for influencing the drawing functions. /// public struct TextGraphicsOptions { /// /// Represents the default . /// public static readonly TextGraphicsOptions Default = new TextGraphicsOptions(true); private float? blendPercentage; private int? antialiasSubpixelDepth; private bool? antialias; private bool? applyKerning; private float? tabWidth; private PixelBlenderMode blenderMode; private float wrapTextWidth; private SixLabors.Fonts.HorizontalAlignment? horizontalAlignment; private SixLabors.Fonts.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.blenderMode = PixelBlenderMode.Normal; this.blendPercentage = 1; this.antialias = enableAntialiasing; } /// /// 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 ?? 1).Clamp(0, 1); set => this.blendPercentage = value; } // 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 blending percentage to apply to the drawing operation /// public PixelBlenderMode BlenderMode { get => this.blenderMode; set => this.blenderMode = value; } /// /// 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 => this.wrapTextWidth; set => this.wrapTextWidth = 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, blenderMode = options.BlenderMode }; } /// /// 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, BlenderMode = options.BlenderMode, BlendPercentage = options.BlendPercentage }; } } }