//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp
{
using System.Numerics;
using Drawing;
using Drawing.Brushes;
using Drawing.Pens;
using ImageSharp.PixelFormats;
using SixLabors.Fonts;
using SixLabors.Primitives;
using SixLabors.Shapes;
///
/// Extension methods for the type.
///
public static partial class ImageExtensions
{
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 Image DrawText(this Image source, string text, Font font, TPixel color, PointF location)
where TPixel : struct, IPixel
{
return 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 Image DrawText(this Image source, string text, Font font, TPixel color, PointF location, TextGraphicsOptions options)
where TPixel : struct, IPixel
{
return 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 Image DrawText(this Image source, string text, Font font, IBrush brush, PointF location)
where TPixel : struct, IPixel
{
return 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 Image DrawText(this Image source, string text, Font font, IBrush brush, PointF location, TextGraphicsOptions options)
where TPixel : struct, IPixel
{
return 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 Image DrawText(this Image source, string text, Font font, IPen pen, PointF location)
where TPixel : struct, IPixel
{
return 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 Image DrawText(this Image source, string text, Font font, IPen pen, PointF location, TextGraphicsOptions options)
where TPixel : struct, IPixel
{
return 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 Image DrawText(this Image source, string text, Font font, IBrush brush, IPen pen, PointF location)
where TPixel : struct, IPixel
{
return source.DrawText(text, font, brush, pen, location, TextGraphicsOptions.Default);
}
///
/// 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 options.
///
/// The .
///
public static Image DrawText(this Image source, string text, Font font, IBrush brush, IPen pen, PointF location, TextGraphicsOptions options)
where TPixel : struct, IPixel
{
float dpiX = DefaultTextDpi;
float dpiY = DefaultTextDpi;
if (options.UseImageResolution)
{
dpiX = (float)source.MetaData.HorizontalResolution;
dpiY = (float)source.MetaData.VerticalResolution;
}
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;
}
}
}