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