// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Drawing;
using SixLabors.ImageSharp.Processing.Drawing.Brushes;
using SixLabors.ImageSharp.Processing.Drawing.Pens;
using SixLabors.Shapes;
namespace SixLabors.ImageSharp.Processing.Text
{
///
/// Adds extensions that allow the drawing of text along given paths to the type.
///
public static partial class DrawTextExtensions
{
///
/// Draws the text onto the the image filled via the brush.
///
/// The type of the color.
/// The image this method extends.
/// The text.
/// The font.
/// The color.
/// The path.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, TPixel color, IPath path)
where TPixel : struct, IPixel
=> source.DrawText(TextGraphicsOptions.Default, text, font, color, path);
///
/// Draws the text onto the the image filled via the brush.
///
/// The type of the color.
/// The image this method extends.
/// The options.
/// The text.
/// The font.
/// The color.
/// The path.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, TextGraphicsOptions options, string text, Font font, TPixel color, IPath path)
where TPixel : struct, IPixel
=> source.DrawText(options, text, font, Brushes.Solid(color), null, path);
///
/// Draws the text onto the the image filled via the brush.
///
/// The type of the color.
/// The image this method extends.
/// The text.
/// The font.
/// The brush.
/// The location.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, IBrush brush, IPath path)
where TPixel : struct, IPixel
=> source.DrawText(TextGraphicsOptions.Default, text, font, brush, path);
///
/// Draws the text onto the the image filled via the brush.
///
/// The type of the color.
/// The image this method extends.
/// The options.
/// The text.
/// The font.
/// The brush.
/// The path.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, TextGraphicsOptions options, string text, Font font, IBrush brush, IPath path)
where TPixel : struct, IPixel
=> source.DrawText(options, text, font, brush, null, path);
///
/// Draws the text onto the the image outlined via the pen.
///
/// The type of the color.
/// The image this method extends.
/// The text.
/// The font.
/// The pen.
/// The path.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, IPen pen, IPath path)
where TPixel : struct, IPixel
=> source.DrawText(TextGraphicsOptions.Default, text, font, pen, path);
///
/// Draws the text onto the the image outlined via the pen.
///
/// The type of the color.
/// The image this method extends.
/// The options.
/// The text.
/// The font.
/// The pen.
/// The path.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, TextGraphicsOptions options, string text, Font font, IPen pen, IPath path)
where TPixel : struct, IPixel
=> source.DrawText(options, text, font, null, pen, path);
///
/// Draws the text onto the the image filled via the brush then outlined via the pen.
///
/// The type of the color.
/// The image this method extends.
/// The text.
/// The font.
/// The brush.
/// The pen.
/// The path.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, IBrush brush, IPen pen, IPath path)
where TPixel : struct, IPixel
=> source.DrawText(TextGraphicsOptions.Default, text, font, brush, pen, path);
///
/// Draws the text onto the the image filled via the brush then outlined via the pen.
///
/// The type of the color.
/// The image this method extends.
/// The options.
/// The text.
/// The font.
/// The brush.
/// The pen.
/// The path.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, TextGraphicsOptions options, string text, Font font, IBrush brush, IPen pen, IPath path)
where TPixel : struct, IPixel
{
float dpiX = DefaultTextDpi;
float dpiY = DefaultTextDpi;
var style = new RendererOptions(font, dpiX, dpiY)
{
ApplyKerning = options.ApplyKerning,
TabWidth = options.TabWidth,
WrappingWidth = options.WrapTextWidth,
HorizontalAlignment = options.HorizontalAlignment,
VerticalAlignment = options.VerticalAlignment
};
IPathCollection glyphs = TextBuilder.GenerateGlyphs(text, path, style);
var pathOptions = (GraphicsOptions)options;
if (brush != null)
{
source.Fill(pathOptions, brush, glyphs);
}
if (pen != null)
{
source.Draw(pathOptions, pen, glyphs);
}
return source;
}
}
}