| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More tests => 19; |
| 7 |
use IO::String; |
| 8 |
use File::Temp qw(tempfile); |
| 9 |
use File::Spec; |
| 10 |
|
| 11 |
BEGIN { |
| 12 |
use_ok('PDF::Reuse') or BAIL_OUT "Can't load PDF::Reuse"; |
| 13 |
} |
| 14 |
|
| 15 |
# RT #130152 / GitHub #12 |
| 16 |
# sprintf with undefined objekt values should not warn |
| 17 |
{ |
| 18 |
my @warnings; |
| 19 |
local $SIG{__WARN__} = sub { push @warnings, @_ }; |
| 20 |
|
| 21 |
my ($fh, $tmpfile) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 22 |
close $fh; |
| 23 |
|
| 24 |
prFile($tmpfile); |
| 25 |
prText(100, 700, 'Test RT 130152'); |
| 26 |
prEnd(); |
| 27 |
|
| 28 |
my @undef_warnings = grep { /uninitialized value/ } @warnings; |
| 29 |
is(scalar @undef_warnings, 0, |
| 30 |
'RT #130152: No uninitialized value warnings from sprintf'); |
| 31 |
} |
| 32 |
|
| 33 |
# RT #171691 / GitHub #13 |
| 34 |
# IO::String untie: writing to IO::String then calling prFile() again |
| 35 |
# should not produce "Can't locate object method OPEN" error |
| 36 |
{ |
| 37 |
my $pdf_data = ''; |
| 38 |
my $io = IO::String->new($pdf_data); |
| 39 |
|
| 40 |
my $ok = eval { |
| 41 |
prFile($io); |
| 42 |
prText(100, 700, 'IO::String test'); |
| 43 |
prEnd(); |
| 44 |
|
| 45 |
# This second prFile call would fail without the untie fix |
| 46 |
my ($fh, $tmpfile) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 47 |
close $fh; |
| 48 |
prFile($tmpfile); |
| 49 |
prText(100, 700, 'After IO::String'); |
| 50 |
prEnd(); |
| 51 |
1; |
| 52 |
}; |
| 53 |
my $err = $@; |
| 54 |
|
| 55 |
ok($ok, 'RT #171691: prFile() works after IO::String write') |
| 56 |
or diag("Error: $err"); |
| 57 |
|
| 58 |
ok(length($pdf_data) > 0, |
| 59 |
'RT #171691: IO::String received PDF data'); |
| 60 |
} |
| 61 |
|
| 62 |
# RT #120459 / GitHub #19 |
| 63 |
# defined(%hash) - module loads and prDocForm compiles without error |
| 64 |
# (This was a compile-time failure on Perl 5.24+) |
| 65 |
{ |
| 66 |
my $ok = eval { |
| 67 |
# Force loading of the prDocForm code path |
| 68 |
# (previously hidden behind AutoLoader) |
| 69 |
my $exists = defined &PDF::Reuse::prDocForm; |
| 70 |
1; |
| 71 |
}; |
| 72 |
ok($ok, 'RT #120459: prDocForm compiles without defined(%hash) error'); |
| 73 |
} |
| 74 |
|
| 75 |
# GitHub #8 |
| 76 |
# prDocForm should not crash with "undefined value as ARRAY reference" |
| 77 |
# when %links has entries but not for the current page |
| 78 |
{ |
| 79 |
my @warnings; |
| 80 |
local $SIG{__WARN__} = sub { push @warnings, @_ }; |
| 81 |
|
| 82 |
my ($fh, $tmpfile) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 83 |
close $fh; |
| 84 |
|
| 85 |
# Generate a simple PDF, then try to use it with prDocForm |
| 86 |
prFile($tmpfile); |
| 87 |
prText(100, 700, 'Source PDF for prDocForm test'); |
| 88 |
prEnd(); |
| 89 |
|
| 90 |
my ($fh2, $outfile) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 91 |
close $fh2; |
| 92 |
|
| 93 |
my $ok = eval { |
| 94 |
prFile($outfile); |
| 95 |
prDocForm($tmpfile); |
| 96 |
prEnd(); |
| 97 |
1; |
| 98 |
}; |
| 99 |
my $err = $@; |
| 100 |
|
| 101 |
ok($ok, 'GitHub #8: prDocForm does not crash on undefined links') |
| 102 |
or diag("Error: $err"); |
| 103 |
} |
| 104 |
|
| 105 |
# RT #83185 / GitHub #20 |
| 106 |
# crossrefObj should not warn about non-numeric arguments |
| 107 |
{ |
| 108 |
my @warnings; |
| 109 |
local $SIG{__WARN__} = sub { push @warnings, @_ }; |
| 110 |
|
| 111 |
my ($fh, $tmpfile) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 112 |
close $fh; |
| 113 |
|
| 114 |
prFile($tmpfile); |
| 115 |
prText(100, 700, 'Test RT 83185'); |
| 116 |
prEnd(); |
| 117 |
|
| 118 |
my @numeric_warnings = grep { /isn't numeric/ } @warnings; |
| 119 |
is(scalar @numeric_warnings, 0, |
| 120 |
"RT #83185: No 'isn't numeric' warnings from crossrefObj"); |
| 121 |
} |
| 122 |
|
| 123 |
# RT #168975 / GitHub #11 |
| 124 |
# prForm() should accept IO::String objects as file input |
| 125 |
{ |
| 126 |
my ($fh, $tmpfile) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 127 |
close $fh; |
| 128 |
|
| 129 |
# First, generate a source PDF |
| 130 |
my ($sfh, $srcfile) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 131 |
close $sfh; |
| 132 |
prFile($srcfile); |
| 133 |
prText(100, 700, 'Source for prForm IO::String test'); |
| 134 |
prEnd(); |
| 135 |
|
| 136 |
# Read it into an IO::String |
| 137 |
open(my $in, '<', $srcfile) or die "Can't open $srcfile: $!"; |
| 138 |
binmode $in; |
| 139 |
my $pdf_data = do { local $/; <$in> }; |
| 140 |
close $in; |
| 141 |
my $io = IO::String->new($pdf_data); |
| 142 |
|
| 143 |
# Use the IO::String as input to prForm |
| 144 |
my $ok = eval { |
| 145 |
prFile($tmpfile); |
| 146 |
prForm({ file => $io }); |
| 147 |
prEnd(); |
| 148 |
1; |
| 149 |
}; |
| 150 |
my $err = $@; |
| 151 |
|
| 152 |
ok($ok, 'RT #168975: prForm() accepts IO::String input') |
| 153 |
or diag("Error: $err"); |
| 154 |
ok(-s $tmpfile > 0, 'RT #168975: Output PDF has content'); |
| 155 |
} |
| 156 |
|
| 157 |
# GitHub #24 |
| 158 |
# prTTFont leaves orphaned $docProxy causing prEnd() crash. |
| 159 |
# The DESTROY method on the TTFont wrapper was calling release() on the |
| 160 |
# TTFont0 object (wiping its uid), while the DocProxy still held a reference. |
| 161 |
SKIP: { |
| 162 |
eval { require Text::PDF::TTFont0; require Font::TTF::Font; }; |
| 163 |
skip 'Text::PDF::TTFont0 or Font::TTF not available', 3 if $@; |
| 164 |
|
| 165 |
my @fonts = glob('/usr/share/fonts/truetype/*/*.ttf'); |
| 166 |
skip 'No TTF fonts found on system', 3 unless @fonts; |
| 167 |
my $font = $fonts[0]; |
| 168 |
|
| 169 |
# Test 1: Normal prTTFont session works |
| 170 |
my ($fh1, $file1) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 171 |
close $fh1; |
| 172 |
my $ok1 = eval { |
| 173 |
prFile($file1); |
| 174 |
prPage(); |
| 175 |
prTTFont($font); |
| 176 |
prEnd(); |
| 177 |
1; |
| 178 |
}; |
| 179 |
ok($ok1, 'GitHub #24: prTTFont in normal session does not crash') |
| 180 |
or diag("Error: $@"); |
| 181 |
|
| 182 |
# Test 2: prInitVars clears docProxy |
| 183 |
my ($fh2, $file2) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 184 |
close $fh2; |
| 185 |
prFile($file2); |
| 186 |
prPage(); |
| 187 |
prTTFont($font); |
| 188 |
prInitVars(); |
| 189 |
{ |
| 190 |
no strict 'refs'; |
| 191 |
is(${"PDF::Reuse::docProxy"}, undef, |
| 192 |
'GitHub #24: prInitVars clears docProxy'); |
| 193 |
} |
| 194 |
|
| 195 |
# Test 3: Multiple sessions with prTTFont do not crash |
| 196 |
my ($fh3, $file3) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 197 |
close $fh3; |
| 198 |
my ($fh4, $file4) = tempfile(SUFFIX => '.pdf', UNLINK => 1); |
| 199 |
close $fh4; |
| 200 |
my $ok3 = eval { |
| 201 |
prFile($file3); |
| 202 |
prPage(); |
| 203 |
prTTFont($font); |
| 204 |
prEnd(); |
| 205 |
prFile($file4); |
| 206 |
prPage(); |
| 207 |
prTTFont($font); |
| 208 |
prEnd(); |
| 209 |
1; |
| 210 |
}; |
| 211 |
ok($ok3, 'GitHub #24: Multiple prTTFont sessions do not crash') |
| 212 |
or diag("Error: $@"); |
| 213 |
} |
| 214 |
|
| 215 |
# GitHub #21 / #22 (RT #103758, RT #103768) |
| 216 |
# %processed caches byte offsets keyed by filename. Reusing a filename for |
| 217 |
# fresh content within one session must not apply the old file's offsets to |
| 218 |
# the new bytes -- that failed with "Didn't find pages" on the second pass. |
| 219 |
{ |
| 220 |
my $dir = File::Temp->newdir(); |
| 221 |
my $tpl = File::Spec->catfile($dir, 'tpl.pdf'); |
| 222 |
|
| 223 |
my $build = sub { |
| 224 |
my ($pages, $label) = @_; |
| 225 |
prFile($tpl); |
| 226 |
for my $p (1 .. $pages) { |
| 227 |
prPage() if $p > 1; |
| 228 |
prFontSize(24); |
| 229 |
prText(72, 700, "$label page $p"); |
| 230 |
} |
| 231 |
prEnd(); |
| 232 |
}; |
| 233 |
|
| 234 |
my $ok = eval { |
| 235 |
for my $i (1 .. 3) { |
| 236 |
$build->($i * 3, "ITER$i"); |
| 237 |
prFile(File::Spec->catfile($dir, "out_$i.pdf")); |
| 238 |
prDoc($tpl); |
| 239 |
prEnd(); |
| 240 |
} |
| 241 |
1; |
| 242 |
}; |
| 243 |
ok($ok, 'GitHub #21/#22: reusing a filename for new content does not die') |
| 244 |
or diag("Error: $@"); |
| 245 |
|
| 246 |
# The cache must survive for a file that has NOT changed -- that is what it |
| 247 |
# exists for, and clearing it wholesale would be a performance regression. |
| 248 |
my $fixed = File::Spec->catfile($dir, 'fixed.pdf'); |
| 249 |
prFile($fixed); |
| 250 |
prPage(); |
| 251 |
prText(72, 700, 'unchanged template'); |
| 252 |
prEnd(); |
| 253 |
|
| 254 |
prFile(File::Spec->catfile($dir, 'r1.pdf')); |
| 255 |
prDoc($fixed); |
| 256 |
prEnd(); |
| 257 |
my $first = do { no strict 'refs'; ${"PDF::Reuse::processed"}{$fixed}{root} }; |
| 258 |
|
| 259 |
prFile(File::Spec->catfile($dir, 'r2.pdf')); |
| 260 |
prDoc($fixed); |
| 261 |
prEnd(); |
| 262 |
my $second = do { no strict 'refs'; ${"PDF::Reuse::processed"}{$fixed}{root} }; |
| 263 |
|
| 264 |
ok(defined $first && defined $second, |
| 265 |
'GitHub #21/#22: cache retained across reuse of an unchanged file'); |
| 266 |
is($second, $first, |
| 267 |
'GitHub #21/#22: cached root is stable for an unchanged file'); |
| 268 |
} |
| 269 |
|
| 270 |
# RT #123564 / GitHub #15 |
| 271 |
# Embedded TrueType text must be extractable, which requires a /ToUnicode CMap. |
| 272 |
# Text::PDF::TTFont0's own ToUnicode support emits a malformed CMap (RT #123562, |
| 273 |
# unfixed upstream), so PDF::Reuse builds the mapping itself. |
| 274 |
SKIP: { |
| 275 |
# Runners differ: Linux images carry DejaVu/Liberation, macOS keeps fonts |
| 276 |
# under /System or /Library, Windows under the system root. Glob rather |
| 277 |
# than list, so this exercises the CMap on every platform that has any |
| 278 |
# TrueType font rather than silently skipping. |
| 279 |
my @globs = ( |
| 280 |
'/usr/share/fonts/truetype/*/*.ttf', |
| 281 |
'/usr/share/fonts/*/*.ttf', |
| 282 |
'/Library/Fonts/*.ttf', |
| 283 |
'/System/Library/Fonts/*.ttf', |
| 284 |
'/System/Library/Fonts/Supplemental/*.ttf', |
| 285 |
($ENV{SystemRoot} ? "$ENV{SystemRoot}\\Fonts\\*.ttf" : ()), |
| 286 |
'C:/Windows/Fonts/*.ttf', |
| 287 |
); |
| 288 |
my ($ttf) = grep { -r $_ } map { glob $_ } @globs; |
| 289 |
skip 'no TrueType font available on this platform', 4 unless $ttf; |
| 290 |
diag("using font: $ttf"); |
| 291 |
|
| 292 |
my $dir = File::Temp->newdir(); |
| 293 |
my $out = File::Spec->catfile($dir, 'tt.pdf'); |
| 294 |
|
| 295 |
prInitVars(); |
| 296 |
prFile($out); |
| 297 |
prTTFont($ttf); |
| 298 |
prFontSize(14); |
| 299 |
prText(60, 700, 'Hello ToUnicode'); |
| 300 |
prEnd(); |
| 301 |
|
| 302 |
open my $fh, '<', $out or BAIL_OUT "can't read $out: $!"; |
| 303 |
binmode $fh; |
| 304 |
my $pdf = do { local $/; <$fh> }; |
| 305 |
close $fh; |
| 306 |
|
| 307 |
like($pdf, qr{/ToUnicode}, 'GitHub #15: /ToUnicode key present for TrueType font'); |
| 308 |
like($pdf, qr{beginbfchar}, 'GitHub #15: CMap uses bfchar mappings'); |
| 309 |
like($pdf, qr{currentdict}, 'GitHub #15: CMap trailer is well formed'); |
| 310 |
|
| 311 |
# Every section must declare exactly as many entries as it contains -- |
| 312 |
# a miscount produces a CMap some readers silently reject. |
| 313 |
my @declared = $pdf =~ /(\d+) beginbfchar/g; |
| 314 |
my @bodies = $pdf =~ /beginbfchar\n(.*?)endbfchar/gs; |
| 315 |
my $consistent = scalar(@declared) == scalar(@bodies); |
| 316 |
if ($consistent) { |
| 317 |
for my $i (0 .. $#declared) { |
| 318 |
my $actual = grep { /\S/ } split /\n/, $bodies[$i]; |
| 319 |
$consistent = 0 if $actual != $declared[$i]; |
| 320 |
} |
| 321 |
} |
| 322 |
ok($consistent, 'GitHub #15: each bfchar section declares its true entry count'); |
| 323 |
} |