-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathXTextFormatterTest.cs
More file actions
129 lines (103 loc) · 6.13 KB
/
XTextFormatterTest.cs
File metadata and controls
129 lines (103 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.IO;
using FluentAssertions;
using ImageMagick;
using PdfSharpCore.Drawing;
using PdfSharpCore.Drawing.Layout;
using PdfSharpCore.Drawing.Layout.enums;
using PdfSharpCore.Pdf;
using PdfSharpCore.Test.Helpers;
using Xunit;
namespace PdfSharpCore.Test.Drawing.Layout
{
public class XTextFormatterTest
{
private static readonly string _outDir = "TestResults/XTextFormatterTest";
private static readonly string _expectedImagesPath = Path.Combine("Drawing", "Layout");
private const double DiffTolerance = 80000;
private PdfDocument _document;
private XGraphics _renderer;
private XTextFormatter _textFormatter;
// Run before each test
public XTextFormatterTest()
{
_document = new PdfDocument();
var page = _document.AddPage();
page.Size = PageSize.A6; // 295 x 417 pts
_renderer = XGraphics.FromPdfPage(page);
_textFormatter = new XTextFormatter(_renderer);
}
[Fact]
public void DrawSingleLineString()
{
var layout = new XRect(12, 12, 200, 50);
_textFormatter.DrawString("This is a simple single line test", new XFont("Arial", 12), XBrushes.Black, layout);
var diffResult = DiffPage(_document, "DrawSingleLineString", 1);
diffResult.DiffValue.Should().BeLessOrEqualTo(DiffTolerance);
}
[Fact]
public void DrawMultilineStringWithTruncate()
{
var layout = new XRect(12, 12, 200, 40);
_renderer.DrawRectangle(XBrushes.LightGray, layout);
_textFormatter.DrawString("This is text\nspanning 3 lines\nbut only space for 2", new XFont("Arial", 12), XBrushes.Black, layout);
var diffResult = DiffPage(_document, "DrawMultilineStringWithTruncate", 1);
diffResult.DiffValue.Should().BeLessOrEqualTo(DiffTolerance);
}
[Fact]
public void DrawMultiLineStringWithOverflow()
{
var layout = new XRect(12, 12, 200, 40);
_renderer.DrawRectangle(XBrushes.LightGray, layout);
_textFormatter.AllowVerticalOverflow = true;
_textFormatter.DrawString("This is text\nspanning 3 lines\nand overflow shows all three", new XFont("Arial", 12), XBrushes.Black, layout);
var diffResult = DiffPage(_document, "DrawMultiLineStringWithOverflow", 1);
diffResult.DiffValue.Should().BeLessOrEqualTo(DiffTolerance);
}
[Fact]
public void DrawMultiLineStringsWithAlignment()
{
var layout1 = new XRect(12, 12, 200, 80);
_renderer.DrawRectangle(XBrushes.LightGray, layout1);
_textFormatter.DrawString("This is text\naligned to the top-left", new XFont("Arial", 12), XBrushes.Black, layout1);
var layout2 = new XRect(12, 100, 200, 80);
_renderer.DrawRectangle(XBrushes.LightGray, layout2);
_textFormatter.SetAlignment(new TextFormatAlignment { Horizontal = XParagraphAlignment.Center, Vertical = XVerticalAlignment.Middle});
_textFormatter.DrawString("This is text\naligned to the middle-center", new XFont("Arial", 12), XBrushes.Black, layout2);
var layout3 = new XRect(12, 200, 200, 80);
_renderer.DrawRectangle(XBrushes.LightGray, layout3);
_textFormatter.SetAlignment(new TextFormatAlignment { Horizontal = XParagraphAlignment.Right, Vertical = XVerticalAlignment.Bottom});
_textFormatter.DrawString("This is text\naligned to the bottom-right", new XFont("Arial", 12), XBrushes.Black, layout3);
var diffResult = DiffPage(_document, "DrawMultiLineStringsWithAlignment", 1);
diffResult.DiffValue.Should().BeLessOrEqualTo(DiffTolerance);
}
[Fact]
public void DrawMultiLineStringsWithLineHeight()
{
var font = new XFont("Arial", 12);
var layout1 = new XRect(10, 10, 200, 80);
_renderer.DrawRectangle(XBrushes.LightGray, layout1);
_textFormatter.DrawString("This is text\naligned to the top-left\nand a custom line height", font, XBrushes.Black, layout1, 16);
var layout2 = new XRect(10, 110, 200, 80);
_renderer.DrawRectangle(XBrushes.LightGray, layout2);
_textFormatter.SetAlignment(new TextFormatAlignment { Horizontal = XParagraphAlignment.Center, Vertical = XVerticalAlignment.Middle});
_textFormatter.DrawString("This is text\naligned to the middle-center\nand a custom line height", font, XBrushes.Black, layout2, 16);
var layout3 = new XRect(10, 210, 200, 80);
_renderer.DrawRectangle(XBrushes.LightGray, layout3);
_textFormatter.SetAlignment(new TextFormatAlignment { Horizontal = XParagraphAlignment.Right, Vertical = XVerticalAlignment.Bottom});
_textFormatter.DrawString("This is text\naligned to the bottom-right\nand a custom line height", font, XBrushes.Black, layout3, 16);
var layout4 = new XRect(10, 310, 200, 80);
_renderer.DrawRectangle(XBrushes.LightGray, layout4);
_textFormatter.SetAlignment(new TextFormatAlignment { Horizontal = XParagraphAlignment.Center, Vertical = XVerticalAlignment.Middle});
_textFormatter.DrawString("This is text\nwith a very small\nline height", font, XBrushes.Black, layout4, 6);
var diffResult = DiffPage(_document, "DrawMultiLineStringsWithLineHeight", 1);
diffResult.DiffValue.Should().BeLessOrEqualTo(DiffTolerance);
}
private static DiffOutput DiffPage(PdfDocument document, string filePrefix, int pageNum)
{
var rasterized = PdfHelper.Rasterize(document);
var rasterizedFiles = PdfHelper.WriteImageCollection(rasterized.ImageCollection, _outDir, filePrefix);
var expectedImagePath = PathHelper.GetInstance().GetAssetPath(_expectedImagesPath, $"{filePrefix}_{pageNum}.png");
return PdfHelper.Diff(rasterizedFiles[pageNum-1], expectedImagePath, _outDir, filePrefix);
}
}
}