Browse Source

expose TextAlignment for text drawing

af/merge-core
Scott Williams 9 years ago
parent
commit
c165c75ba5
  1. 4
      src/ImageSharp.Drawing/ImageSharp.Drawing.csproj
  2. 3
      src/ImageSharp.Drawing/Text/DrawText.cs
  3. 102
      src/ImageSharp.Drawing/Text/TextGraphicsOptions.cs

4
src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

@ -39,8 +39,8 @@
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta001">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="SixLabors.Fonts" Version="0.1.0-alpha0008" />
<PackageReference Include="SixLabors.Shapes" Version="0.1.0-alpha0011" />
<PackageReference Include="SixLabors.Fonts" Version="0.1.0-alpha0010" />
<PackageReference Include="SixLabors.Shapes" Version="0.1.0-alpha0012" />
</ItemGroup>
<PropertyGroup>
<CodeAnalysisRuleSet>..\..\ImageSharp.ruleset</CodeAnalysisRuleSet>

3
src/ImageSharp.Drawing/Text/DrawText.cs

@ -181,7 +181,8 @@ namespace ImageSharp
{
ApplyKerning = options.ApplyKerning,
TabWidth = options.TabWidth,
WrappingWidth = options.WrapTextWidth
WrappingWidth = options.WrapTextWidth,
Alignment = options.TextAlignment
};
renderer.RenderText(text, style);

102
src/ImageSharp.Drawing/Text/TextGraphicsOptions.cs

@ -2,9 +2,11 @@
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Drawing
{
using ImageSharp.PixelFormats;
using SixLabors.Fonts;
/// <summary>
/// Options for influencing the drawing functions.
/// </summary>
@ -15,50 +17,94 @@ namespace ImageSharp.Drawing
/// </summary>
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 bool? useImageResolution;
private float wrapTextWidth;
private SixLabors.Fonts.TextAlignment? textAlignment;
/// <summary>
/// Initializes a new instance of the <see cref="TextGraphicsOptions" /> struct.
/// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
public TextGraphicsOptions(bool enableAntialiasing)
{
this.applyKerning = true;
this.tabWidth = 4;
this.useImageResolution = false;
this.wrapTextWidth = 0;
this.textAlignment = SixLabors.Fonts.TextAlignment.Left;
this.antialiasSubpixelDepth = 16;
this.blenderMode = PixelBlenderMode.Normal;
this.blendPercentage = 1;
this.antialias = enableAntialiasing;
}
/// <summary>
/// Gets or sets a value indicating whether antialiasing should be applied.
/// </summary>
public bool Antialias { get => this.antialias ?? true; set => this.antialias = value; }
/// <summary>
/// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled.
/// </summary>
public int AntialiasSubpixelDepth { get => this.antialiasSubpixelDepth ?? 16; set => this.antialiasSubpixelDepth = value; }
/// <summary>
/// Whether antialiasing should be applied.
/// Gets or sets a value indicating the blending percentage to apply to the drawing operation
/// </summary>
public bool Antialias;
public float BlendPercentage { get => (this.blendPercentage ?? 1).Clamp(0, 1); set => this.blendPercentage = value; }
// In the future we could expose a PixelBlender<TPixel> directly on here
// or some forms of PixelBlender factory for each pixel type. Will need
// some API thought post V1.
/// <summary>
/// The number of subpixels to use while rendering with antialiasing enabled.
/// Gets or sets a value indicating the blending percentage to apply to the drawing operation
/// </summary>
public int AntialiasSubpixelDepth;
public PixelBlenderMode BlenderMode { get => this.blenderMode; set => this.blenderMode = value; }
/// <summary>
/// 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>
public bool ApplyKerning;
public bool ApplyKerning { get => this.applyKerning ?? true; set => this.applyKerning = value; }
/// <summary>
/// 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>
public float TabWidth;
public float TabWidth { get => this.tabWidth ?? 4; set => this.tabWidth = value; }
/// <summary>
/// Flag weather to use the current image resultion to for point size scaling.
/// Gets or sets a value indicating whether to use the current image resultion to for point size scaling.
/// If this is [false] the text renders at 72dpi otherwise it renders at Image resolution
/// </summary>
public bool UseImageResolution;
public bool UseImageResolution { get => this.useImageResolution ?? false; set => this.useImageResolution = value; }
/// <summary>
/// If greater than zero determine the width at which text should wrap.
/// Gets or sets a value indicating if greater than zero determine the width at which text should wrap.
/// </summary>
public float WrapTextWidth;
public float WrapTextWidth { get => this.wrapTextWidth; set => this.wrapTextWidth = value; }
/// <summary>
/// Initializes a new instance of the <see cref="TextGraphicsOptions" /> struct.
/// Gets or sets a value indicating how to align the text relative to the rendering space.
/// If <see cref="WrapTextWidth"/> is greater than zero it will align relative to the space
/// 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.
/// </summary>
/// <param name="enableAntialiasing">If set to <c>true</c> [enable antialiasing].</param>
public TextGraphicsOptions(bool enableAntialiasing)
{
this.Antialias = enableAntialiasing;
this.ApplyKerning = true;
this.TabWidth = 4;
this.AntialiasSubpixelDepth = 16;
this.UseImageResolution = false;
this.WrapTextWidth = 0;
}
public TextAlignment TextAlignment { get => this.textAlignment ?? TextAlignment.Left; set => this.textAlignment = value; }
/// <summary>
/// Performs an implicit conversion from <see cref="GraphicsOptions"/> to <see cref="TextGraphicsOptions"/>.
@ -71,7 +117,9 @@ namespace ImageSharp.Drawing
{
return new TextGraphicsOptions(options.Antialias)
{
AntialiasSubpixelDepth = options.AntialiasSubpixelDepth
AntialiasSubpixelDepth = options.AntialiasSubpixelDepth,
blendPercentage = options.BlendPercentage,
blenderMode = options.BlenderMode
};
}
@ -86,7 +134,9 @@ namespace ImageSharp.Drawing
{
return new GraphicsOptions(options.Antialias)
{
AntialiasSubpixelDepth = options.AntialiasSubpixelDepth
AntialiasSubpixelDepth = options.AntialiasSubpixelDepth,
BlenderMode = options.BlenderMode,
BlendPercentage = options.BlendPercentage
};
}
}

Loading…
Cancel
Save