// // 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 SixLabors.Fonts; /// /// Extension methods for the type. /// public static partial class ImageExtensions { private static readonly Vector2 DefaultTextDpi = new Vector2(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, TColor color, Vector2 location) where TColor : 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, TColor color, Vector2 location, TextGraphicsOptions options) where TColor : 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, Vector2 location) where TColor : 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, Vector2 location, TextGraphicsOptions options) where TColor : 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, Vector2 location) where TColor : 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, Vector2 location, TextGraphicsOptions options) where TColor : 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, Vector2 location) where TColor : 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, Vector2 location, TextGraphicsOptions options) where TColor : struct, IPixel { GlyphBuilder glyphBuilder = new GlyphBuilder(location); TextRenderer renderer = new TextRenderer(glyphBuilder); Vector2 dpi = DefaultTextDpi; if (options.UseImageResolution) { dpi = new Vector2((float)source.MetaData.HorizontalResolution, (float)source.MetaData.VerticalResolution); } FontSpan style = new FontSpan(font, dpi) { ApplyKerning = options.ApplyKerning, TabWidth = options.TabWidth, WrappingWidth = options.WrapTextWidth }; renderer.RenderText(text, style); System.Collections.Generic.IEnumerable shapesToDraw = glyphBuilder.Paths; GraphicsOptions pathOptions = (GraphicsOptions)options; if (brush != null) { foreach (SixLabors.Shapes.IPath s in shapesToDraw) { source.Fill(brush, s, pathOptions); } } if (pen != null) { foreach (SixLabors.Shapes.IPath s in shapesToDraw) { source.Draw(pen, s, pathOptions); } } return source; } } }