-
Notifications
You must be signed in to change notification settings - Fork 3
/
30-demo.t
148 lines (111 loc) · 3.41 KB
/
30-demo.t
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
BEGIN {
my $rc;
$rc = eval {
require PDF::API2;
1;
};
if (!defined $rc) { $rc = 0; }
unless($rc) {
print qq{1..0 # SKIP these tests; PDF::API2 is not installed\n};
exit;
}
}
use Test::More tests => 19;
# Bold every other word under various alignments.
use PDF::API2;
use PDF::TextBlock;
use constant mm => 25.4 / 72;
use constant in => 1 / 72;
use constant pt => 1;
ok(my $pdf = PDF::API2->new( -file => "30-demo.pdf" ), "PDF::API2->new()");
my $fonts = {
b => PDF::TextBlock::Font->new({
pdf => $pdf,
font => $pdf->corefont( 'Helvetica-Bold', -encoding => 'latin1' ),
}),
};
my ($endw, $ypos);
ok(my $tb = PDF::TextBlock->new({
pdf => $pdf,
y => 270/mm,
fonts => $fonts,
text => "align => 'justify'",
}), "new()");
ok(($endw, $ypos) = $tb->apply(), "apply()");
# Tag every other word with <b>.
my $text = $tb->garbledy_gook(30) . ".";
my $text_with_bold = $text;
$text_with_bold =~ s/(\w+) (\w+)/$1 <b>$2<\/b>/g;
$tb->y($ypos);
$tb->text($text);
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text_with_bold);
ok(($endw, $ypos) = $tb->apply(), "apply()");
# ---
red_line($ypos);
$tb->y($ypos);
$tb->align('right');
$tb->text("align => 'right'");
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text);
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text_with_bold);
ok(($endw, $ypos) = $tb->apply(), "apply()");
# ---
red_line($ypos);
$tb->y($ypos);
$tb->align('center');
$tb->text("align => 'center'");
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text);
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text_with_bold);
ok(($endw, $ypos) = $tb->apply(), "apply()");
# ---
red_line($ypos);
$tb->y($ypos);
$tb->align('left');
$tb->text("align => 'left'");
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text);
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text_with_bold);
ok(($endw, $ypos) = $tb->apply(), "apply()");
# ---
red_line($ypos);
$tb->y($ypos);
$tb->align('fulljustify');
$tb->text("align => 'fulljustify'");
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text);
ok(($endw, $ypos) = $tb->apply(), "apply()");
$tb->y($ypos);
$tb->text($text_with_bold);
ok(($endw, $ypos) = $tb->apply(), "apply()");
# ---
red_line($ypos);
$tb->y($ypos);
$tb->align('left');
$tb->text("Generated by t/30-demo.t");
$tb->fonts->{default}->fillcolor('darkblue');
ok(($endw, $ypos) = $tb->apply(), "apply()");
$pdf->save; # Doesn't return true, even when it succeeds. -sigh-
$pdf->end; # Doesn't return true, even when it succeeds. -sigh-
ok(-r "30-demo.pdf", "30-demo.pdf created");
diag( "Testing PDF::TextBlock $PDF::TextBlock::VERSION, Perl $], $^X" );
sub red_line {
my ($ypos) = @_;
my $red_line = $tb->page->gfx;
$red_line->strokecolor('red');
$red_line->move( 20/mm, $ypos + 4/mm );
$red_line->line( 195/mm, $ypos + 4/mm );
$red_line->stroke;
}