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
{
///
/// Using the brush as a source of pixels colors blends the brush color with source.
///
/// The pixel format.
internal class DrawTextProcessor : ImageProcessor
where TPixel : struct, IPixel
{
private FillRegionProcessor fillRegionProcessor = null;
///
/// Initializes a new instance of the class.
///
/// The options
/// The text we want to render
/// The font we want to render with
/// The brush to source pixel colors from.
/// The pen to outline text with.
/// The location on the image to start drawign the text from.
public DrawTextProcessor(TextGraphicsOptions options, string text, Font font, IBrush brush, IPen pen, PointF location)
{
this.Brush = brush;
this.Options = options;
this.Text = text;
this.Pen = pen;
this.Font = font;
this.Location = location;
}
///
/// Gets or sets the brush.
///
public IBrush Brush { get; set; }
///
/// Gets or sets the options
///
public TextGraphicsOptions Options { get; set; }
///
/// Gets or sets the text
///
public string Text { get; set; }
///
/// Gets or sets the pen used for outlining the text, if Null then we will not outline
///
public IPen Pen { get; set; }
///
/// Gets or sets the font used to render the text.
///
public Font Font { get; set; }
///
/// Gets or sets the location to draw the text at.
///
public PointF Location { get; set; }
protected override void BeforeImageApply(Image 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()
{
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()
{
Brush = this.Brush,
Options = pathOptions
};
}
foreach (IPath p in glyphs)
{
this.fillRegionProcessor.Region = new ShapePath(p, this.Pen);
this.fillRegionProcessor.Apply(source, sourceRectangle);
}
}
}
///
protected override void OnFrameApply(ImageFrame 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
}
}
}