- Timestamp:
- 2011-06-01T12:33:42+12:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/perllib/cpan/Image/ExifTool/WritePostScript.pl
r16842 r24107 74 74 75 75 #------------------------------------------------------------------------------ 76 # Check PS tag value77 # Inputs: 0) ExifTool object ref, 1) tag info ref, 2) value ref78 # Returns: undef on success, or error string79 sub CheckPS($$$)80 {81 my ($exifTool, $tagInfo, $valPt) = @_;82 # parentheses must be balanced (or escaped)83 my $n = 0;84 pos($$valPt) = 0;85 while ($$valPt =~ /(\(|\))/g) {86 $n += ($1 eq '(') ? 1 : -1;87 last if $n < 0;88 }89 return 'Unmatched parentheses' unless $n == 0;90 return undef; # success91 }92 93 #------------------------------------------------------------------------------94 76 # Write XMP directory to file, with begin/end tokens if necessary 95 77 # Inputs: 0) outfile ref, 1) flags hash ref, 2-N) data to write … … 160 142 $dirInfo{DataPt} = \$xmp; 161 143 } 162 my $tagTablePtr = GetTagTable("Image::ExifTool::${dirName}::Main");144 my $tagTablePtr = Image::ExifTool::GetTagTable("Image::ExifTool::${dirName}::Main"); 163 145 my $val = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr); 164 146 if (defined $val) { … … 264 246 265 247 #------------------------------------------------------------------------------ 248 # Encode postscript tag/value 249 # Inputs: 0) tag ID, 1) value 250 # Returns: postscript comment 251 # - adds brackets, escapes special characters, and limits line length 252 sub EncodeTag($$) 253 { 254 my ($tag, $val) = @_; 255 unless ($val =~ /^\d+$/) { 256 $val =~ s/([()\\])/\\$1/g; # escape brackets and backslashes 257 $val =~ s/\n/\\n/g; # escape newlines 258 $val =~ s/\r/\\r/g; # escape carriage returns 259 $val =~ s/\t/\\t/g; # escape tabs 260 # use octal escape codes for other control characters 261 $val =~ s/([\x00-\x1f\x7f\xff])/sprintf("\\%.3o",ord($1))/ge; 262 $val = "($val)"; 263 } 264 my $line = "%%$tag: $val"; 265 # postscript line limit is 255 characters 266 # --> split if necessary using continuation comment "%%+" 267 my $n; 268 for ($n=255; length($line)>$n; $n+=255+length($/)) { 269 substr($line, $n, 0) = "$/%%+"; 270 } 271 return $line . $/; 272 } 273 274 #------------------------------------------------------------------------------ 266 275 # Write new tags information in comments section 267 276 # Inputs: 0) ExifTool object ref, 1) output file ref, 2) reference to new tag hash … … 279 288 foreach $tag (sort keys %$newTags) { 280 289 my $tagInfo = $$newTags{$tag}; 281 my $newValueHash = $exifTool->GetNewValueHash($tagInfo); 282 next unless Image::ExifTool::IsCreating($newValueHash); 283 my $val = Image::ExifTool::GetNewValues($newValueHash); 284 if ($exifTool->Options('Verbose') > 1) { 285 my $out = $exifTool->Options('TextOut'); 286 print $out " + PostScript:$$tagInfo{Name} = '$val'\n"; 287 } 288 $val =~ /^\d+$/ or $val = "($val)"; # add brackets around strings 289 my $buff = "%%$tag: $val$/"; 290 if (length $buff > 255) { 291 $exifTool->Warn("Value for too long for $tag"); 292 } else { 293 Write($outfile, $buff) or $success = 0; 294 ++$exifTool->{CHANGED}; 295 } 290 my $nvHash = $exifTool->GetNewValueHash($tagInfo); 291 next unless Image::ExifTool::IsCreating($nvHash); 292 my $val = Image::ExifTool::GetNewValues($nvHash); 293 $exifTool->VerboseValue("+ PostScript:$$tagInfo{Name}", $val); 294 Write($outfile, EncodeTag($tag, $val)) or $success = 0; 295 ++$exifTool->{CHANGED}; 296 296 } 297 297 # write XMP hint if necessary … … 436 436 # rewrite PostScript data 437 437 # 438 my $oldsep = SetInputRecordSeparator($raf);439 unless ($ oldsepand $raf->ReadLine($buff)) {438 local $/ = GetInputRecordSeparator($raf); 439 unless ($/ and $raf->ReadLine($buff)) { 440 440 $exifTool->Error('Invalid PostScript data'); 441 441 return 1; 442 442 } 443 443 $data .= $buff; 444 unless ($data =~ /^%!PS-Adobe-3 .(0|1)/) {444 unless ($data =~ /^%!PS-Adobe-3\.(\d+)\b/ and $1 < 2) { 445 445 if ($exifTool->Error("Document does not conform to DSC spec. Metadata may be unreadable by other apps", 1)) { 446 446 return 1; 447 447 } 448 448 } 449 my $psRev = $1; # save PS revision number (3.x) 449 450 Write($outfile, $data) or $err = 1; 450 451 $flags{EPS} = 1 if $data =~ /EPSF/; … … 461 462 # set XMP hint flag (1 for adding, 0 for deleting, undef for no change) 462 463 $xmpHint = 1 if $$addDirs{XMP}; 463 $xmpHint = 0 if $ exifTool->{DEL_GROUP}->{XMP};464 $xmpHint = 0 if $$exifTool{DEL_GROUP}{XMP}; 464 465 $$newTags{XMP_HINT} = $xmpHint if $xmpHint; # add special tag to newTags list 465 466 466 my @lines;467 my (@lines, $changedNL); 467 468 my $altnl = ($/ eq "\x0d") ? "\x0a" : "\x0d"; 468 469 … … 474 475 $dos and CheckPSEnd($raf, $psEnd, $data); 475 476 # split line if it contains other newline sequences 476 SplitLine(\$data, \@lines) if $data =~ /$altnl/; 477 } 477 if ($data =~ /$altnl/) { 478 if (length($data) > 500000 and IsPC()) { 479 # patch for Windows memory problem 480 unless ($changedNL) { 481 $changedNL = 1; 482 my $t = $/; 483 $/ = $altnl; 484 $altnl = $t; 485 $raf->Seek(-length($data), 1); 486 next; 487 } 488 } else { 489 # split into separate lines 490 SplitLine(\$data, \@lines); 491 } 492 } 493 } 494 undef $changedNL; 478 495 if ($endToken) { 479 496 # look for end token … … 537 554 # rewrite information from PostScript tags in comments 538 555 my ($tag, $val) = ($1, $2); 556 # handle Adobe Illustrator files specially 557 # - EVENTUALLY IT WOULD BE BETTER TO FIND ANOTHER IDENTIFICATION METHOD 558 # (because Illustrator doesn't care if the Creator is changed) 559 if ($tag eq 'Creator' and $val =~ /^Adobe Illustrator/) { 560 # disable writing XMP to PS-format Adobe Illustrator files and 561 # older Illustrator EPS files becaues it confuses Illustrator 562 # (Illustrator 8 and older write PS-Adobe-3.0, newer write PS-Adobe-3.1) 563 if ($$editDirs{XMP} and $psRev == 0) { 564 if ($flags{EPS}) { 565 $exifTool->Warn("Can't write XMP to Illustrator 8 or older EPS files"); 566 } else { 567 $exifTool->Warn("Can't write XMP to PS-format AI files"); 568 } 569 # pretend like we wrote it already so we won't try to add it later 570 $doneDir{XMP} = 1; 571 } 572 # don't allow "Creator" to be changed in Illustrator files 573 # (we need it to be able to recognize these files) 574 # --> find a better way to do this! 575 if ($$newTags{$tag}) { 576 $exifTool->Warn("Can't change Postscript:Creator of Illustrator files"); 577 delete $$newTags{$tag}; 578 } 579 } 539 580 if ($$newTags{$tag}) { 540 581 my $tagInfo = $$newTags{$tag}; 582 delete $$newTags{$tag}; # write it then forget it 541 583 next unless ref $tagInfo; 542 delete $$newTags{$tag}; # write it then forget it 543 $val =~ s/\x0d*\x0a*$//; # remove trailing CR, LF or CR/LF 544 if ($val =~ s/^\((.*)\)$/$1/) { # remove brackets if necessary 545 $val =~ s/\) \(/, /g; # convert contained brackets too 546 } 547 my $newValueHash = $exifTool->GetNewValueHash($tagInfo); 548 if (Image::ExifTool::IsOverwriting($newValueHash, $val)) { 549 $verbose > 1 and print $out " - PostScript:$$tagInfo{Name} = '$val'\n"; 550 $val = Image::ExifTool::GetNewValues($newValueHash); 584 # decode comment string (reading continuation lines if necessary) 585 $val = DecodeComment($val, $raf, \@lines, \$data); 586 $val = join $exifTool->Options('ListSep'), @$val if ref $val eq 'ARRAY'; 587 my $nvHash = $exifTool->GetNewValueHash($tagInfo); 588 if (Image::ExifTool::IsOverwriting($nvHash, $val)) { 589 $exifTool->VerboseValue("- PostScript:$$tagInfo{Name}", $val); 590 $val = Image::ExifTool::GetNewValues($nvHash); 551 591 ++$exifTool->{CHANGED}; 552 592 next unless defined $val; # next if tag is being deleted 553 $verbose > 1 and print $out " + PostScript:$$tagInfo{Name} = '$val'\n"; 554 $val =~ /^\d+$/ or $val = "($val)"; # add brackets around strings 555 $buff = "%%$tag: $val$/"; 556 if (length $buff > 255) { 557 # lines in PS documents must be less than 256 characters 558 # (don't yet support continuation with %%+ comment) 559 $exifTool->Warn("Value for too long for $tag"); 560 } else { 561 $data = $buff; # write the new value 562 } 593 $exifTool->VerboseValue("+ PostScript:$$tagInfo{Name}", $val); 594 $data = EncodeTag($tag, $val); 563 595 } 564 596 } … … 663 695 push @notDone, 'PostScript' if %$newTags; 664 696 foreach $dir (qw{Photoshop ICC_Profile XMP}) { 665 push @notDone, $dir if $$editDirs{$dir} and not $doneDir{$dir}; 697 push @notDone, $dir if $$editDirs{$dir} and not $doneDir{$dir} and 698 not $$exifTool{DEL_GROUP}{$dir}; 666 699 } 667 700 @notDone and $exifTool->Warn("Couldn't write ".join('/',@notDone).' information'); … … 698 731 =head1 NOTES 699 732 700 Currently, information is written only in the out ter-level document.733 Currently, information is written only in the outer-level document. 701 734 702 735 Photoshop will discard meta information in a PostScript document if it has … … 731 764 =head1 AUTHOR 732 765 733 Copyright 2003-20 07, Phil Harvey (phil at owl.phy.queensu.ca)766 Copyright 2003-2011, Phil Harvey (phil at owl.phy.queensu.ca) 734 767 735 768 This library is free software; you can redistribute it and/or modify it
Note:
See TracChangeset
for help on using the changeset viewer.