mirror of https://github.com/SixLabors/ImageSharp
3 changed files with 114 additions and 39 deletions
@ -0,0 +1,114 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Numerics; |
||||
|
|
||||
|
using SixLabors.Fonts; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.ImageSharp.Processing; |
||||
|
using SixLabors.ImageSharp.Processing.Drawing; |
||||
|
using SixLabors.ImageSharp.Processing.Text; |
||||
|
|
||||
|
using Xunit; |
||||
|
// ReSharper disable InconsistentNaming
|
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Drawing.Text |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
|
||||
|
using SixLabors.Primitives; |
||||
|
|
||||
|
[GroupOutput("Drawing/Text")] |
||||
|
public class DrawTextOnImageTests |
||||
|
{ |
||||
|
private const string AB = "AB\nAB"; |
||||
|
|
||||
|
private const string TestText = "Sphinx of black quartz, judge my vow\n0123456789"; |
||||
|
|
||||
|
private const string TestText2 = |
||||
|
"THISISTESTWORDS "; |
||||
|
|
||||
|
[Theory] |
||||
|
[WithSolidFilledImages(200, 100, "White", PixelTypes.Rgba32, 50, 0, 0, "SixLaborsSampleAB.woff", AB)] |
||||
|
[WithSolidFilledImages(900, 100, "White", PixelTypes.Rgba32, 50, 0, 0, "OpenSans-Regular.ttf", TestText)] |
||||
|
[WithSolidFilledImages(400, 40, "White", PixelTypes.Rgba32, 20, 0, 0, "OpenSans-Regular.ttf", TestText)] |
||||
|
[WithSolidFilledImages(1100, 200, "White", PixelTypes.Rgba32, 50, 150, 100, "OpenSans-Regular.ttf", TestText)] |
||||
|
public void FontShapesAreRenderedCorrectly<TPixel>( |
||||
|
TestImageProvider<TPixel> provider, |
||||
|
int fontSize, |
||||
|
int x, |
||||
|
int y, |
||||
|
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 color = NamedColors<TPixel>.Black; |
||||
|
|
||||
|
provider.VerifyOperation( |
||||
|
img => |
||||
|
{ |
||||
|
img.Mutate(c => c.DrawText(text, new Font(font, fontSize), color, new PointF(x, y))); |
||||
|
}, |
||||
|
$"{fontName}-{fontSize}-{fnDisplayText}-({x},{y})", |
||||
|
appendPixelTypeToFileName: false, |
||||
|
appendSourceFileOrDescription: true); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Based on:
|
||||
|
/// https://github.com/SixLabors/ImageSharp/issues/572
|
||||
|
/// </summary>
|
||||
|
[Theory] |
||||
|
[WithSolidFilledImages(2480, 3508, "White", PixelTypes.Rgba32)] |
||||
|
public void FontShapesAreRenderedCorrectly_LargeText<TPixel>( |
||||
|
TestImageProvider<TPixel> provider) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
Font font = CreateFont("OpenSans-Regular.ttf", 36); |
||||
|
|
||||
|
var sb = new StringBuilder(); |
||||
|
string str = Repeat(" ", 78) + "THISISTESTWORDSTHISISTESTWORDSTHISISTESTWORDSTHISISTESTWORDSTHISISTESTWORDS"; |
||||
|
sb.Append(str); |
||||
|
|
||||
|
string newLines = Repeat(Environment.NewLine, 80); |
||||
|
sb.Append(newLines); |
||||
|
|
||||
|
for (int i = 0; i < 10; i++) |
||||
|
{ |
||||
|
sb.AppendLine(str); |
||||
|
} |
||||
|
|
||||
|
var textOptions = new TextGraphicsOptions |
||||
|
{ |
||||
|
Antialias = true, |
||||
|
ApplyKerning = true, |
||||
|
VerticalAlignment = VerticalAlignment.Top, |
||||
|
HorizontalAlignment = HorizontalAlignment.Left, |
||||
|
}; |
||||
|
TPixel color = NamedColors<TPixel>.Black; |
||||
|
|
||||
|
provider.VerifyOperation( |
||||
|
img => |
||||
|
{ |
||||
|
img.Mutate(c => c.DrawText(textOptions, sb.ToString(), font, color, new PointF(10, 5))); |
||||
|
}, |
||||
|
false, |
||||
|
false); |
||||
|
} |
||||
|
|
||||
|
private static string Repeat(string str, int times) => string.Concat(Enumerable.Repeat(str, times)); |
||||
|
|
||||
|
private static Font CreateFont(string fontName, int size) |
||||
|
{ |
||||
|
var fontCollection = new FontCollection(); |
||||
|
string fontPath = TestFontUtilities.GetPath(fontName); |
||||
|
Font font = fontCollection.Install(fontPath).CreateFont(size); |
||||
|
return font; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,39 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System.Numerics; |
|
||||
|
|
||||
using SixLabors.Fonts; |
|
||||
using SixLabors.ImageSharp.PixelFormats; |
|
||||
using SixLabors.ImageSharp.Processing; |
|
||||
using SixLabors.ImageSharp.Processing.Drawing; |
|
||||
using SixLabors.ImageSharp.Processing.Text; |
|
||||
|
|
||||
using Xunit; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Tests.Drawing.Text |
|
||||
{ |
|
||||
public class OutputText : FileTestBase |
|
||||
{ |
|
||||
private readonly FontCollection FontCollection; |
|
||||
private readonly Font Font; |
|
||||
|
|
||||
public OutputText() |
|
||||
{ |
|
||||
this.FontCollection = new FontCollection(); |
|
||||
this.Font = this.FontCollection.Install(TestFontUtilities.GetPath("SixLaborsSampleAB.woff")).CreateFont(12); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void DrawAB() |
|
||||
{ |
|
||||
//draws 2 overlapping triangle glyphs twice 1 set on each line
|
|
||||
using (var img = new Image<Rgba32>(100, 200)) |
|
||||
{ |
|
||||
img.Mutate(x => x.Fill(Rgba32.DarkBlue) |
|
||||
.DrawText("AB\nAB", new Font(this.Font, 50), Rgba32.Red, new Vector2(0, 0))); |
|
||||
img.Save($"{TestEnvironment.CreateOutputDirectory("Drawing", "Text")}/AB.png"); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Binary file not shown.
Loading…
Reference in new issue