File: text.t

package info (click to toggle)
libpdf-api2-perl 2.048-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,228 kB
  • sloc: perl: 42,239; makefile: 11
file content (220 lines) | stat: -rw-r--r-- 6,385 bytes parent folder | download | duplicates (3)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use Test::More tests => 26;

use warnings;
use strict;

use PDF::API2;

# advancewidth

my $pdf = PDF::API2->new();
my $page = $pdf->page();
my $text = $page->text();
my $font = $pdf->corefont('Helvetica');

$text->font($font, 12);
$text->text('Test Text');

my $width = $text->advancewidth('Test Text');
is($width, '50.016', 'Advance Width Check');

$text->charspace(2);
is($text->charspace(), 2, 'Charspace is set');
$width = $text->advancewidth('Test Text');
is($width, '66.016', 'Advance width check with charspace added');

$width = $text->advancewidth('Test Text', charspace => 0);
is($width, '50.016', 'Advance width check with charspace overridden to 0');

$text->wordspace(4);
is($text->wordspace(), 4, 'Wordspace is set');
$width = $text->advancewidth('Test Text');
is($width, '70.016', 'Advance width check with wordspace added');

$width = $text->advancewidth('Test Text', wordspace => 0);
is($width, '66.016', 'Advance width check with wordspace overridden to 0');

# Check for death if text() is called without font()
$text = $page->text();
{
    local $@;
    eval { $text->text('This should die because no font has been set') };
    like($@,
         qr{Can't add text without first setting a font and font size},
         q{Call to text without a set font returns an informative error});

    # Call font(), but without setting a font size
    eval { $text->font($font); };
    like($@,
         qr{A font size is required},
         q{Call to text without a set font size returns an informative error});
}

# text

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);

$width = $text->text('Test Text');
like($pdf->to_string(), qr/\(Test Text\) Tj/,
     q{Basic text call});
is($width, '50.016',
   q{Basic text call has expected width});

# text with indent

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);

$width = $text->text('Test Text', -indent => 72);
like($pdf->to_string(), qr/\[ -6000 \(Test Text\) \] TJ/,
     q{text with indent});
is($width, '50.016',
   q{text with indent has expected width});

# text_right

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);

$width = $text->text_right('Test Text');
like($pdf->to_string(), qr/\[ 4168 \(Test Text\) \] TJ/,
     q{text_right});

# text_center

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);

$width = $text->text_center('Test Text');
like($pdf->to_string(), qr/\[ 2084 \(Test Text\) \] TJ/,
     q{text_center});

# text_justified

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);

$width = $text->text_justified('Test Text', 72);
like($pdf->to_string(), qr/21.984 Tw \(Test Text\) Tj 0 Tw/,
     q{text_justified});

# paragraph

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);

my $leftover = $text->paragraph(('aaa ' x 30), 144, 72);
like($pdf->to_string(), qr/15 TL (\((aaa ){5}aaa\) Tj T\* ){4}\s*ET/,
     q{paragraph});
is($leftover, 'aaa aaa aaa aaa aaa aaa',
   q{paragraph has expected leftover});

# paragraph, align right

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);

$text->paragraph(('aaa ' x 10), 144, 72, -align => 'right');
like($pdf->to_string(),
     qr/\[ 11398 \((aaa ){5}aaa\) \] TJ T\* \[ 7506 \(aaa aaa aaa aaa\) \] TJ T\*/,
     q{paragraph, align right});

# paragraph, align center

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);

$text->paragraph(('aaa ' x 10), 144, 72, -align => 'center');
like($pdf->to_string(),
     qr/\[ 5699 \((aaa ){5}aaa\) \] TJ T\* \[ 3753 \(aaa aaa aaa aaa\) \] TJ T\*/,
     q{paragraph, align center});

# paragraph, justified left

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);

$text->paragraph(('aaa ' x 10), 144, 72, -align => 'justified');
like($pdf->to_string(),
     qr/1.4448 Tw \((aaa ){5}aaa\) Tj 0 Tw T\* \(aaa aaa aaa aaa\) Tj T\*/,
     q{paragraph, justified, last line left});

# paragraph, justified right

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);

$text->paragraph(('aaa ' x 10), 144, 72, -align => 'justified',
                 '-align-last' => 'right');
like($pdf->to_string(),
     qr/1.4448 Tw \((aaa ){5}aaa\) Tj 0 Tw T\* \[ 7506 \((aaa ){3}aaa\) \] TJ T\*/,
     q{paragraph, justified, last line right});

# paragraph, justified center

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);

$text->paragraph(('aaa ' x 10), 144, 72, -align => 'justified',
                 '-align-last' => 'center');
like($pdf->to_string(),
     qr/1.4448 Tw \((aaa ){5}aaa\) Tj 0 Tw T\* \[ 3753 \((aaa ){3}aaa\) \] TJ T\*/,
     q{paragraph, justified, last line center});

# paragraph, justified, last line justified

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);

$text->paragraph(('aaa ' x 10), 144, 72, -align => 'justified',
                 '-align-last' => 'justified');
like($pdf->to_string(),
     qr/1.4448 Tw \((aaa ){5}aaa\) Tj 0 Tw T\* 17.976 Tw \((aaa ){3}aaa\) Tj 0 Tw/,
     q{paragraph, justified, last line justified});

# paragraphs (formerly "section")

$pdf = PDF::API2->new(-compress => 0);
$text = $pdf->page->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);

my $input = 'aaa ' x 10 . "\n\n" . 'bbb ' x 10;
$leftover = $text->paragraphs($input, 144, 72);
like($pdf->to_string(), qr/15 TL \((aaa ){5}aaa\) Tj T\* \((aaa ){3}aaa\) Tj T\* T\* \((bbb ){5}bbb\) Tj T\*\s+ET/,
     q{paragraphs});
is($leftover, 'bbb bbb bbb bbb',
   q{paragraphs has expected leftover});