// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.Fonts;
using SixLabors.ImageSharp.Drawing.Brushes;
using SixLabors.ImageSharp.Drawing.Pens;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Overlays;
using SixLabors.Primitives;
using SixLabors.Shapes;
namespace SixLabors.ImageSharp.Processing.Text
{
///
/// Adds extensions that allow the drawing of text to the type.
///
public static partial class DrawTextExtensions
{
private static readonly int DefaultTextDpi = 72;
///
/// 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 location.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, TPixel color, PointF location)
where TPixel : struct, IPixel
=> source.DrawText(text, font, color, location, TextGraphicsOptions.Default);
///
/// 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 location.
/// The options.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, TPixel color, PointF location, TextGraphicsOptions options)
where TPixel : struct, IPixel
=> source.DrawText(text, font, Brushes.Solid(color), null, location, options);
///
/// 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, PointF location)
where TPixel : struct, IPixel
=> source.DrawText(text, font, brush, location, TextGraphicsOptions.Default);
///
/// 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 options.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, IBrush brush, PointF location, TextGraphicsOptions options)
where TPixel : struct, IPixel
=> source.DrawText(text, font, brush, null, location, options);
///
/// 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 location.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, IPen pen, PointF location)
where TPixel : struct, IPixel
=> source.DrawText(text, font, pen, location, TextGraphicsOptions.Default);
///
/// 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 location.
/// The options.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, IPen pen, PointF location, TextGraphicsOptions options)
where TPixel : struct, IPixel
=> source.DrawText(text, font, null, pen, location, options);
///
/// 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 location.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, IBrush brush, IPen pen, PointF location)
where TPixel : struct, IPixel
=> source.DrawText(text, font, brush, pen, location, TextGraphicsOptions.Default);
///
/// Draws the text using the default resolution of 72dpi 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 location.
/// The options.
///
/// The .
///
public static IImageProcessingContext DrawText(this IImageProcessingContext source, string text, Font font, IBrush brush, IPen pen, PointF location, TextGraphicsOptions options)
where TPixel : struct, IPixel
{
float dpiX = DefaultTextDpi;
float dpiY = DefaultTextDpi;
var style = new RendererOptions(font, dpiX, dpiY, location)
{
ApplyKerning = options.ApplyKerning,
TabWidth = options.TabWidth,
WrappingWidth = options.WrapTextWidth,
HorizontalAlignment = options.HorizontalAlignment,
VerticalAlignment = options.VerticalAlignment
};
IPathCollection glyphs = TextBuilder.GenerateGlyphs(text, style);
var pathOptions = (GraphicsOptions)options;
if (brush != null)
{
source.Fill(brush, glyphs, pathOptions);
}
if (pen != null)
{
source.Draw(pen, glyphs, pathOptions);
}
return source;
}
}
}