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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
#!/usr/bin/perl
use warnings;
use strict;
use Test::More tests => 27;
use PDF::Builder;
my $pdf = PDF::Builder->new();
my $page = $pdf->page();
my $text = $page->text();
my $font = $pdf->corefont('Helvetica');
# advancewidth
$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, '68.016', 'Advance width check with charspace added');
$width = $text->advancewidth('Test Text', charspace => 0);
is($width, '50.016', 'Advance width check with charspace 2 overridden to 0');
$text->wordspace(4);
is($text->wordspace(), 4, 'Wordspace is set');
$width = $text->advancewidth('Test Text');
is($width, '72.016', 'Advance width check with wordspace added');
$width = $text->advancewidth('Test Text', wordspace => 0);
is($width, '68.016', 'Advance width check with wordspace 4 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::Builder->new(-compress => 'none');
$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::Builder->new(-compress => 'none');
$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::Builder->new(-compress => 'none');
$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::Builder->new(-compress => 'none');
$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::Builder->new(-compress => 'none');
$text = $pdf->page()->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$width = $text->text_justified('Test Text', 72);
like($pdf->to_string(), qr/3.336 Tw 2.331 Tc \(Test Text\) Tj 100 Tz 0 Tw/,
q{text_justified});
# paragraph
# note that it spills over past the right margin without the -spillover flag
# note that this is by default, left-justified (ragged right)
# paragraph originally 72 high, but this allows full fit with no leftover
$pdf = PDF::Builder->new(-compress => 'none');
$text = $pdf->page()->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);
my $leftover = $text->paragraph(('aaa ' x 30), 144, 57, 1,-spillover=>0);
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::Builder->new(-compress => 'none');
$text = $pdf->page()->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);
$text->paragraph(('aaa ' x 10), 144, 72, 1,-align => 'right', -spillover=>0);
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::Builder->new(-compress => 'none');
$text = $pdf->page()->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);
$text->paragraph(('aaa ' x 10), 144, 72, 1,-align => 'center', -spillover=>0);
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::Builder->new(-compress => 'none');
$text = $pdf->page()->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);
$text->paragraph(('aaa ' x 10), 144, 72, 1,-align => 'justified', -spillover=>0);
like($pdf->to_string(),
qr/1.4448 Tw \((aaa ){5}aaa\) Tj 100 Tz 0 Tw 0 Tc T\* \(aaa aaa aaa aaa\) Tj T\*/,
q{paragraph, justified, last line left});
# paragraph, justified right
$pdf = PDF::Builder->new(-compress => 'none');
$text = $pdf->page()->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);
$text->paragraph(('aaa ' x 10), 144, 72, 1,-align => 'j',
-spillover=>0, '-last_align' => 'r');
like($pdf->to_string(),
qr/1.4448 Tw \((aaa ){5}aaa\) Tj 100 Tz 0 Tw 0 Tc T\* \[ -4494 \((aaa ){3}aaa\) \] TJ T\*/,
q{paragraph, justified, last line right});
# paragraph, justified center
$pdf = PDF::Builder->new(-compress => 'none');
$text = $pdf->page()->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);
$text->paragraph(('aaa ' x 10), 144, 72, 1,-align => 'justified',
-spillover=>0, '-last_align' => 'center');
like($pdf->to_string(),
qr/1.4448 Tw \((aaa ){5}aaa\) Tj 100 Tz 0 Tw 0 Tc T\* \[ -2247 \((aaa ){3}aaa\) \] TJ T\*/,
q{paragraph, justified, last line center});
# paragraph, justified, last line justified
# PDF::Builder does not support justification for short last lines
#$pdf = PDF::Builder->new(-compress => 'none');
#$text = $pdf->page()->text();
#$font = $pdf->corefont('Helvetica');
#$text->font($font, 12);
#$text->leading(15);
#$text->paragraph(('aaa ' x 10), 144, 72, 1,-align => 'justified',
#-spillover=>0, '-align-last' => 'justified');
#like($pdf->to_string(),
#qr/1.204 Tw \((aaa ){5}aaa\) Tj 0 Tw T\* 13.482 Tw \((aaa ){3}aaa\) Tj 0 Tw/,
#q{paragraph, justified, last line justified});
# paragraphs (formerly "section")
$pdf = PDF::Builder->new(-compress => 'none');
$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, 40, 1,-spillover=>0);
like($pdf->to_string(), qr/15 TL \((aaa ){5}aaa\) Tj T\* \((aaa ){3}aaa\) Tj T\* \((bbb ){5}bbb\) Tj T\*\s+ET/,
q{paragraphs});
is($leftover, 'bbb bbb bbb bbb',
q{paragraphs has expected leftover});
# section (same as "paragraphs")
$pdf = PDF::Builder->new(-compress => 'none');
$text = $pdf->page()->text();
$font = $pdf->corefont('Helvetica');
$text->font($font, 12);
$text->leading(15);
$input = 'aaa ' x 10 . "\n\n" . 'bbb ' x 10;
$leftover = $text->section($input, 144, 40, 1,-spillover=>0);
like($pdf->to_string(), qr/15 TL \((aaa ){5}aaa\) Tj T\* \((aaa ){3}aaa\) Tj T\* \((bbb ){5}bbb\) Tj T\*\s+ET/,
q{section});
is($leftover, 'bbb bbb bbb bbb',
q{section has expected leftover});
1;
|