📷 A modern, cross-platform, 2D Graphics library for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

138 lines
5.2 KiB

using System;
using System.Collections.Generic;
using System.Text;
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.ImageSharp.Processing.Drawing.Brushes;
using SixLabors.ImageSharp.Processing.Drawing.Pens;
using SixLabors.ImageSharp.Processing.Drawing.Processors;
using SixLabors.ImageSharp.Processing.Processors;
using SixLabors.ImageSharp.Processing.Text;
using SixLabors.Primitives;
using SixLabors.Shapes;
namespace SixLabors.ImageSharp.Benchmarks.Drawing.OldProcessors
{
/// <summary>
/// Using the brush as a source of pixels colors blends the brush color with source.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class DrawTextProcessor<TPixel> : ImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private FillRegionProcessor<TPixel> fillRegionProcessor = null;
/// <summary>
/// Initializes a new instance of the <see cref="DrawTextProcessor{TPixel}"/> class.
/// </summary>
/// <param name="options">The options</param>
/// <param name="text">The text we want to render</param>
/// <param name="font">The font we want to render with</param>
/// <param name="brush">The brush to source pixel colors from.</param>
/// <param name="pen">The pen to outline text with.</param>
/// <param name="location">The location on the image to start drawign the text from.</param>
public DrawTextProcessor(TextGraphicsOptions options, string text, Font font, IBrush<TPixel> brush, IPen<TPixel> pen, PointF location)
{
this.Brush = brush;
this.Options = options;
this.Text = text;
this.Pen = pen;
this.Font = font;
this.Location = location;
}
/// <summary>
/// Gets or sets the brush.
/// </summary>
public IBrush<TPixel> Brush { get; set; }
/// <summary>
/// Gets or sets the options
/// </summary>
public TextGraphicsOptions Options { get; set; }
/// <summary>
/// Gets or sets the text
/// </summary>
public string Text { get; set; }
/// <summary>
/// Gets or sets the pen used for outlining the text, if Null then we will not outline
/// </summary>
public IPen<TPixel> Pen { get; set; }
/// <summary>
/// Gets or sets the font used to render the text.
/// </summary>
public Font Font { get; set; }
/// <summary>
/// Gets or sets the location to draw the text at.
/// </summary>
public PointF Location { get; set; }
protected override void BeforeImageApply(Image<TPixel> source, Rectangle sourceRectangle)
{
base.BeforeImageApply(source, sourceRectangle);
// do everythign at the image level as we are deligating the processing down to other processors
var style = new RendererOptions(this.Font, this.Options.DpiX, this.Options.DpiY, this.Location)
{
ApplyKerning = this.Options.ApplyKerning,
TabWidth = this.Options.TabWidth,
WrappingWidth = this.Options.WrapTextWidth,
HorizontalAlignment = this.Options.HorizontalAlignment,
VerticalAlignment = this.Options.VerticalAlignment
};
IPathCollection glyphs = TextBuilder.GenerateGlyphs(this.Text, style);
var pathOptions = (GraphicsOptions)this.Options;
if (this.Brush != null)
{
// we will reuse the processor for all fill operations to reduce allocations
if (this.fillRegionProcessor == null)
{
this.fillRegionProcessor = new FillRegionProcessor<TPixel>()
{
Brush = this.Brush,
Options = pathOptions
};
}
foreach (IPath p in glyphs)
{
this.fillRegionProcessor.Region = new ShapeRegion(p);
this.fillRegionProcessor.Apply(source, sourceRectangle);
}
}
if (this.Pen != null)
{
// we will reuse the processor for all fill operations to reduce allocations
if (this.fillRegionProcessor == null)
{
this.fillRegionProcessor = new FillRegionProcessor<TPixel>()
{
Brush = this.Brush,
Options = pathOptions
};
}
foreach (IPath p in glyphs)
{
this.fillRegionProcessor.Region = new ShapePath(p, this.Pen);
this.fillRegionProcessor.Apply(source, sourceRectangle);
}
}
}
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
{
// this is a no-op as we have processes all as an image, we should be able to pass out of before email apply a skip frames outcome
}
}
}