Browse Source

make processors immutable again

af/merge-core
Scott Williams 8 years ago
parent
commit
c13fb2cd1d
  1. 19
      src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs
  2. 35
      src/ImageSharp.Drawing/Processing/Text/Processors/DrawTextProcessor.cs
  3. 138
      tests/ImageSharp.Benchmarks/Drawing/OldProcessors/DrawTextProcessorV1.cs

19
src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs

@ -37,29 +37,22 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Processors
}
/// <summary>
/// Initializes a new instance of the <see cref="FillRegionProcessor{TPixel}" /> class.
/// </summary>
public FillRegionProcessor()
{
}
/// <summary>
/// Gets or sets the brush.
/// Gets the brush.
/// </summary>
public IBrush<TPixel> Brush { get; set; }
public IBrush<TPixel> Brush { get; }
/// <summary>
/// Gets or sets the region that this processor applies to.
/// Gets the region that this processor applies to.
/// </summary>
public Region Region { get; set; }
public Region Region { get; }
/// <summary>
/// Gets or sets the options.
/// Gets the options.
/// </summary>
/// <value>
/// The options.
/// </value>
public GraphicsOptions Options { get; set; }
public GraphicsOptions Options { get; }
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)

35
src/ImageSharp.Drawing/Processing/Text/Processors/DrawTextProcessor.cs

@ -39,43 +39,50 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors
/// <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;
Guard.NotNull(text, nameof(text));
Guard.NotNull(font, nameof(font));
if (brush == null && pen == null)
{
throw new ArgumentNullException($"at least one of {nameof(brush)} or {nameof(pen)} must not be null");
}
this.Options = options;
this.Text = text;
this.Pen = pen;
this.Font = font;
this.Location = location;
this.Brush = brush;
this.Pen = pen;
}
/// <summary>
/// Gets or sets the brush.
/// Gets the brush.
/// </summary>
public IBrush<TPixel> Brush { get; set; }
public IBrush<TPixel> Brush { get; }
/// <summary>
/// Gets or sets the options
/// Gets the options
/// </summary>
public TextGraphicsOptions Options { get; set; }
public TextGraphicsOptions Options { get; }
/// <summary>
/// Gets or sets the text
/// Gets the text
/// </summary>
public string Text { get; set; }
public string Text { get; }
/// <summary>
/// Gets or sets the pen used for outlining the text, if Null then we will not outline
/// Gets the pen used for outlining the text, if Null then we will not outline
/// </summary>
public IPen<TPixel> Pen { get; set; }
public IPen<TPixel> Pen { get; }
/// <summary>
/// Gets or sets the font used to render the text.
/// Gets the font used to render the text.
/// </summary>
public Font Font { get; set; }
public Font Font { get; }
/// <summary>
/// Gets or sets the location to draw the text at.
/// Gets the location to draw the text at.
/// </summary>
public PointF Location { get; set; }
public PointF Location { get; }
protected override void BeforeImageApply(Image<TPixel> source, Rectangle sourceRectangle)
{

138
tests/ImageSharp.Benchmarks/Drawing/OldProcessors/DrawTextProcessorV1.cs

@ -1,138 +0,0 @@
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
}
}
}
Loading…
Cancel
Save