Browse Source

test drawing along path

af/merge-core
Scott Williams 8 years ago
parent
commit
77bf952bbd
  1. 2
      src/ImageSharp.Drawing/Primitives/ShapePath.cs
  2. 25
      src/ImageSharp.Drawing/Processing/Text/Processors/DrawTextOnPathProcessor.cs
  3. 41
      tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs
  4. 2
      tests/Images/External

2
src/ImageSharp.Drawing/Primitives/ShapePath.cs

@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.Primitives
/// <param name="pen">The pen to apply to the shape.</param>
// TODO: SixLabors.shape will be moving to a Span/ReadOnlySpan based API shortly use ToArray for now.
public ShapePath(IPath shape, IPen pen)
: base(shape.GenerateOutline(pen.StrokeWidth, pen.StrokePattern.ToArray()))
: base(shape.GenerateOutline(pen.StrokeWidth, pen.StrokePattern))
{
}
}

25
src/ImageSharp.Drawing/Processing/Text/Processors/DrawTextOnPathProcessor.cs

@ -82,25 +82,18 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors
{
ApplyKerning = this.Options.ApplyKerning,
TabWidth = this.Options.TabWidth,
WrappingWidth = this.Options.WrapTextWidth,
WrappingWidth = this.Path.Length,
HorizontalAlignment = this.Options.HorizontalAlignment,
VerticalAlignment = this.Options.VerticalAlignment
};
IPathCollection glyphs = TextBuilder.GenerateGlyphs(this.Text, this.Path, style);
this.fillRegionProcessor = new FillRegionProcessor<TPixel>();
this.fillRegionProcessor.Options = (GraphicsOptions)this.Options;
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
};
}
this.fillRegionProcessor.Brush = this.Brush;
foreach (IPath p in glyphs)
{
@ -111,15 +104,7 @@ namespace SixLabors.ImageSharp.Processing.Text.Processors
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
};
}
this.fillRegionProcessor.Brush = this.Pen.StrokeFill;
foreach (IPath p in glyphs)
{

41
tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs

@ -17,10 +17,12 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
using System;
using System.Linq;
using System.Text;
using SixLabors.ImageSharp.Processing.Drawing.Brushes;
using SixLabors.ImageSharp.Processing.Drawing.Brushes.GradientBrushes;
using SixLabors.ImageSharp.Processing.Drawing.Pens;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.Primitives;
using SixLabors.Shapes;
[GroupOutput("Drawing/Text")]
public class DrawTextOnImageTests
@ -163,6 +165,45 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Text
appendSourceFileOrDescription: true);
}
[Theory]
[WithSolidFilledImages(200, 100, "White", PixelTypes.Rgba32, 50, "SixLaborsSampleAB.woff", AB)]
[WithSolidFilledImages(900, 100, "White", PixelTypes.Rgba32, 50, "OpenSans-Regular.ttf", TestText)]
public void FontShapesAreRenderedCorrectlyAlongAPath<TPixel>(
TestImageProvider<TPixel> provider,
int fontSize,
string fontName,
string text)
where TPixel : struct, IPixel<TPixel>
{
Font font = CreateFont(fontName, fontSize);
string fnDisplayText = text.Replace("\n", "");
fnDisplayText = fnDisplayText.Substring(0, Math.Min(fnDisplayText.Length, 4));
TPixel colorOutline = NamedColors<TPixel>.Black;
TPixel colorFill = NamedColors<TPixel>.Gray;
provider.VerifyOperation(
ImageComparer.Tolerant(imageThreshold: 0.1f, perPixelManhattanThreshold: 20),
img =>
{
IPath path = new Path(new LinearLineSegment(new Point(0, img.Height), new Point(img.Width, 0)));
img.Mutate(c => c.DrawText(
new TextGraphicsOptions {
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Top
} ,
text, new Font(font, fontSize),
Brushes.Solid(colorFill)
, Pens.DashDot(colorOutline, 3),
path));
},
$"pen_{fontName}-{fontSize}-{fnDisplayText}",
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: true);
}
private static string Repeat(string str, int times) => string.Concat(Enumerable.Repeat(str, times));
private static Font CreateFont(string fontName, int size)

2
tests/Images/External

@ -1 +1 @@
Subproject commit 09059fae2d8bea3a4c9288ab10fb184f1b648f40
Subproject commit 83f6cbab9a08b550c84bf931c95188341140516a
Loading…
Cancel
Save