Ignore:
Timestamp:
2011-06-01T12:33:42+12:00 (13 years ago)
Author:
sjm84
Message:

Updating the ExifTool perl modules

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/cpan/Image/ExifTool/WritePostScript.pl

    r16842 r24107  
    7474
    7575#------------------------------------------------------------------------------
    76 # Check PS tag value
    77 # Inputs: 0) ExifTool object ref, 1) tag info ref, 2) value ref
    78 # Returns: undef on success, or error string
    79 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;   # success
    91 }
    92 
    93 #------------------------------------------------------------------------------
    9476# Write XMP directory to file, with begin/end tokens if necessary
    9577# Inputs: 0) outfile ref, 1) flags hash ref, 2-N) data to write
     
    160142        $dirInfo{DataPt} = \$xmp;
    161143    }
    162     my $tagTablePtr = GetTagTable("Image::ExifTool::${dirName}::Main");
     144    my $tagTablePtr = Image::ExifTool::GetTagTable("Image::ExifTool::${dirName}::Main");
    163145    my $val = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr);
    164146    if (defined $val) {
     
    264246
    265247#------------------------------------------------------------------------------
     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
     252sub 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#------------------------------------------------------------------------------
    266275# Write new tags information in comments section
    267276# Inputs: 0) ExifTool object ref, 1) output file ref, 2) reference to new tag hash
     
    279288    foreach $tag (sort keys %$newTags) {
    280289        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};
    296296    }
    297297    # write XMP hint if necessary
     
    436436# rewrite PostScript data
    437437#
    438     my $oldsep = SetInputRecordSeparator($raf);
    439     unless ($oldsep and $raf->ReadLine($buff)) {
     438    local $/ = GetInputRecordSeparator($raf);
     439    unless ($/ and $raf->ReadLine($buff)) {
    440440        $exifTool->Error('Invalid PostScript data');
    441441        return 1;
    442442    }
    443443    $data .= $buff;
    444     unless ($data =~ /^%!PS-Adobe-3.(0|1)/) {
     444    unless ($data =~ /^%!PS-Adobe-3\.(\d+)\b/ and $1 < 2) {
    445445        if ($exifTool->Error("Document does not conform to DSC spec. Metadata may be unreadable by other apps", 1)) {
    446446            return 1;
    447447        }
    448448    }
     449    my $psRev = $1; # save PS revision number (3.x)
    449450    Write($outfile, $data) or $err = 1;
    450451    $flags{EPS} = 1 if $data =~ /EPSF/;
     
    461462    # set XMP hint flag (1 for adding, 0 for deleting, undef for no change)
    462463    $xmpHint = 1 if $$addDirs{XMP};
    463     $xmpHint = 0 if $exifTool->{DEL_GROUP}->{XMP};
     464    $xmpHint = 0 if $$exifTool{DEL_GROUP}{XMP};
    464465    $$newTags{XMP_HINT} = $xmpHint if $xmpHint;  # add special tag to newTags list
    465466
    466     my @lines;
     467    my (@lines, $changedNL);
    467468    my $altnl = ($/ eq "\x0d") ? "\x0a" : "\x0d";
    468469
     
    474475            $dos and CheckPSEnd($raf, $psEnd, $data);
    475476            # 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;
    478495        if ($endToken) {
    479496            # look for end token
     
    537554            # rewrite information from PostScript tags in comments
    538555            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            }
    539580            if ($$newTags{$tag}) {
    540581                my $tagInfo = $$newTags{$tag};
     582                delete $$newTags{$tag}; # write it then forget it
    541583                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);
    551591                    ++$exifTool->{CHANGED};
    552592                    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);
    563595                }
    564596            }
     
    663695        push @notDone, 'PostScript' if %$newTags;
    664696        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};
    666699        }
    667700        @notDone and $exifTool->Warn("Couldn't write ".join('/',@notDone).' information');
     
    698731=head1 NOTES
    699732
    700 Currently, information is written only in the outter-level document.
     733Currently, information is written only in the outer-level document.
    701734
    702735Photoshop will discard meta information in a PostScript document if it has
     
    731764=head1 AUTHOR
    732765
    733 Copyright 2003-2007, Phil Harvey (phil at owl.phy.queensu.ca)
     766Copyright 2003-2011, Phil Harvey (phil at owl.phy.queensu.ca)
    734767
    735768This library is free software; you can redistribute it and/or modify it
Note: See TracChangeset for help on using the changeset viewer.