From f0be59ccfa7046c57199fe35c49e1e8963fcb1a9 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Mon, 23 Nov 2020 03:01:13 +0000 Subject: CTAN sync 202011230301 --- macros/latex/contrib/hyperxmp/add_byteCount.pl | 193 ------------------ .../contrib/hyperxmp/hyperxmp-add-bytecount.1 | 36 ++++ .../contrib/hyperxmp/hyperxmp-add-bytecount.pl | 219 +++++++++++++++++++++ macros/latex/contrib/hyperxmp/hyperxmp.dtx | 30 +-- macros/latex/contrib/hyperxmp/hyperxmp.pdf | Bin 1551868 -> 1555235 bytes 5 files changed, 273 insertions(+), 205 deletions(-) delete mode 100644 macros/latex/contrib/hyperxmp/add_byteCount.pl create mode 100644 macros/latex/contrib/hyperxmp/hyperxmp-add-bytecount.1 create mode 100755 macros/latex/contrib/hyperxmp/hyperxmp-add-bytecount.pl (limited to 'macros/latex/contrib/hyperxmp') diff --git a/macros/latex/contrib/hyperxmp/add_byteCount.pl b/macros/latex/contrib/hyperxmp/add_byteCount.pl deleted file mode 100644 index 0cfa3e094f..0000000000 --- a/macros/latex/contrib/hyperxmp/add_byteCount.pl +++ /dev/null @@ -1,193 +0,0 @@ -#!/usr/bin/env perl -use warnings; - -$name = 'add_byteCount.pl'; -$version = '1.1 2020/11/15'; -$maintainer - = 'John Collins, jcc8@psu.edu; Scott Pakin, scott+hyperxmp@pakin.org'; - -my $exit_code = 0; - -if ( (! @ARGV) || ($ARGV[0] =~ /^(-|--)(h|help)$/) ) { - print "$name, ver. $version.\n", - "Usage '$name pdf_filename(s)'\n", - "Adds byteCount specification to XMP packet in pdf file(s) from hyperxmp,\n", - "with byteCount = file size.\n", - "Bug reports to:\n $maintainer.\n"; - exit; -} elsif ( $ARGV[0] =~ /^(-|--)(v|version)$/ ) { - print "$name, ver. $version.\n", - "Bug reports to:\n $maintainer.\n"; - exit; -} - -foreach (@ARGV) { - if ( ! fix_pdf($_) ) { $exit_code = 1; } -} - -exit $exit_code; - -#====================================================== - -sub fix_pdf { - # Change/insert byteCount field with correct file length, while preserving - # the file size and the length of the stream containing xmp metadata. - # Return 1 on success, else 0. - - local $pdf_name = shift; - local $tmp_name = "$pdf_name.new.pdf"; - local $pdf_size = (stat($pdf_name))[7]; - warn "Inserting/correcting byteCount field in '$pdf_name' ...\n"; - - # Strings surrounding (and identifying) the byteCount field, and other - # parts of the xmp packet: - local $xmp_start = ''; - local $decl_bC = 'byteCount'; - local $pre_bC = ''; - local $post_bC = ''; - local $pC = ''; - local $rd_end = ''; - local $xmp_end = ''; - - local *PDF; - local *TMP; - - if (! open PDF, "<", $pdf_name ) { - warn " Cannot read '$pdf_name'\n"; - return 0; - } - if ( ! open TMP, ">", $tmp_name ) { - warn " Cannot write temporary file '$tmp_name'\n"; - close PDF; - return 0; - } - local $status = 0; # 0 = no XMP packet, 1 = success, >= errors - while ( ) { - # Only examine first XMP packet: - if ( ($status == 0) && /^\s*\Q$xmp_start\E/ ) { - local @xmp = $_; - local $len_padding = 0; - local $xmp_after_line = ''; - &xmp_get_mod; - print TMP @xmp; - # Insert correct padding to leave file size unchanged: - while ( $len_padding > 0 ) { - my $len_line = 64; - if ( $len_line > $len_padding ) { $len_line = $len_padding; } - $len_padding -= $len_line; - print TMP (' ' x ($len_line - 1) ), "\n"; - } - print TMP $xmp_after_line; - $xmp_after_line = ''; - } - else { - print TMP "$_"; - } - } - close PDF; - close TMP; - - if ($status == 0) { - warn " Could not insert/modify byteCount, since no XMP packet was found.\n"; - warn " So '$pdf_name' is unchanged,\n", - " and I will delete temporary file '$tmp_name'.\n"; - unlink $tmp_name; - } elsif ($status == 1) { - rename $tmp_name, $pdf_name - or die " Cannot move temporary file '$tmp_name' to '$pdf_name'.\n", - " Error is '$!'\n"; - } else { - warn " Could not insert correct byteCount. See above for reason.\n"; - warn " So '$pdf_name' is unchanged,\n", - " and I will delete temporary file '$tmp_name'.\n"; - unlink $tmp_name; - } - return ($status == 1); -} - -#====================================================== - -sub xmp_get_mod { - # Get xmp packet, given that @xmp contains its first line. - # Get amount of trailing padding, and line after that. - # If possible, insert a byteCount field: - # Either replace existing specification, if it exists, - # or insert one in expected place for hyperxmp, if the XMP packet - # matches what hyperxmp would produce. - # Return xmp packet in @xmp, amount of padding needed in $len_padding, - # line after that in $xmp_after_line, and error code in $error. - # Set $status appropriately: 1 for success; >=1 for failure. - - $len_padding = 0; - $xmp_after_line = ''; - - my $bC_index = -1; - my $xmp_end_found = 0; - my $decl_bC_found = 0; - while ( ) { - push @xmp, $_; - if ( /^\s*\Q$xmp_end\E/ ) { - $xmp_end_found = 1; - # Get amount of padding; - while () { - if ( /^\s*$/ ) { - $len_padding += length($_); - } else { - $xmp_after_line = $_; - last; - } - } - last; - } - elsif ( $bC_index >= 0 ){ - next; - } - # Rest of conditions only apply if no place yet found for byteCount - # specification. - elsif ( /^(\s*)\Q$pre_bC\E.*?\Q$post_bC\E\s*$/ ) { - $bC_index = $#xmp; - } - elsif ( /^\s*\Q$decl_bC\E/ ) { - $decl_bC_found = 1; - } - elsif ( /^(\s*)\Q$rd_end\E/ ){ - # End of rdf:Description block. - # So having previous declaration of byteCount is irrelevant. - $decl_bC_found = 0; - } - elsif ( $decl_bC_found && /^(\s*)\Q$pC\E/ ){ - $bC_index = $#xmp; - pop @xmp; - push @xmp, '', $_; - } - - } # End reading of XMP - - if ($bC_index < 0) { - if ( ! $xmp_end_found ) { - warn " End of XMP packet not found.\n"; - $status = 2; - } - elsif ( ! $decl_bC_found ) { - warn " XMP packet not in appropriate hyperxmp-compatible format.\n"; - $status = 3; - } - return; - } - my $new_line = ' ' . $pre_bC . $pdf_size . $post_bC . "\n"; - my $old_line = $xmp[$bC_index]; - my $delta_len = length($new_line) - length($old_line); - if ($delta_len > $len_padding) { - warn " Cannot get padding correct for '$pdf_name'.\n", - " Length change of bC line = $delta_len; ", - " Padding bytes available = $len_padding.\n"; - $status = 4; - return; - } else { - $len_padding -= $delta_len; - $xmp[$bC_index] = $new_line; - $status = 1; - } -} - -#====================================================== diff --git a/macros/latex/contrib/hyperxmp/hyperxmp-add-bytecount.1 b/macros/latex/contrib/hyperxmp/hyperxmp-add-bytecount.1 new file mode 100644 index 0000000000..e1aa4c8b90 --- /dev/null +++ b/macros/latex/contrib/hyperxmp/hyperxmp-add-bytecount.1 @@ -0,0 +1,36 @@ +.TH HYPERXMP-ADD-BYTECOUNT 1 "22 November 2020" "v1.1" +.SH NAME +hyperxmp-add-bytecount \- adds/updates byteCount specification in XMP +packet in PDF file. +.SH SYNOPSIS +.B hyperxmp-add-bytecount +.RI [ options ] +.IR filename.pdf ... +.SH DESCRIPTION +\fBhyperxmp-add-bytecount\fP adds/updates the \fCbyteCount\fP +specification in the XMP packet in a PDF file(s), with \fCbyteCount\fP += file size. The XMP packet needs to be of, or compatible with, the +format produced from a document that uses +.UR https://ctan.org/pkg/hyperxmp +the +.ie t L\h'-0.36'\v'-0.15'\s-2A\s+2\v'0.15'\h'-0.15'T\h'-0.1667'\v'0.2'E\v'-0.2'\h'-0.125'X +.el LaTeX +hyperxmp package +.UE . +.PP +If no XMP packet of a compatible form is found in the PDF file, no +change is made to the file. +.SH OPTIONS +.IP \fB-help\fP +Write usage summary and exit. +.IP \fB-version\fP +Output version information and exit. +.SH BUGS +Email bug reports to +.MT jcc8@psu.edu +John Collins +.ME +and/or +.MT scott+hyxmp@pakin.org +Scott Pakin +.ME . diff --git a/macros/latex/contrib/hyperxmp/hyperxmp-add-bytecount.pl b/macros/latex/contrib/hyperxmp/hyperxmp-add-bytecount.pl new file mode 100755 index 0000000000..8d965a63f5 --- /dev/null +++ b/macros/latex/contrib/hyperxmp/hyperxmp-add-bytecount.pl @@ -0,0 +1,219 @@ +#!/usr/bin/env perl +use warnings; + +############################################################# +############################################################# + +######################################################################## +# hyperxmp-add-bytecount # +# Adds/updates byteCount specification in XMP packet in pdf file(s) # +# made by hyperxmp, with byteCount = file size. # +# Copyright (C) 2020 John Collins # +# and Scott Pakin, # +# # +# This program may be distributed and/or modified under the conditions # +# of the LaTeX Project Public License, either version 1.3c of this # +# license or (at your option) any later version. # +# # +# The latest version of this license is in: # +# # +# http://www.latex-project.org/lppl.txt # +# # +# and version 1.3c or later is part of all distributions of LaTeX # +# version 2008/05/04 or later. # +######################################################################## + +$name = 'hyperref-add-bytecount'; +$version = '1.1-2020-11-20'; +$maintainer + = 'John Collins, jcc8@psu.edu; Scott Pakin, scott+hyxmp@pakin.org'; + +my $exit_code = 0; + +if ( (! @ARGV) || ($ARGV[0] =~ /^(-|--)(h|help)$/) ) { + print "$name $version.\n", + "Usage: $name [options] pdf_filename(s)\n", + " Adds/updates byteCount specification in XMP packet in pdf file(s) from\n", + " hyperxmp, with byteCount = file size.\n", + " No change if there's no XMP packet of the form produced by hyperxmp.\n", + "Options:\n", + " -help or -h Output usage information.\n", + " -version or -v Output version information.\n", + "Bug reports to:\n $maintainer.\n"; + exit; +} elsif ( $ARGV[0] =~ /^(-|--)(v|version)$/ ) { + print "$name $version.\n", + "Bug reports to:\n $maintainer.\n"; + exit; +} + +foreach (@ARGV) { + if ( ! fix_pdf($_) ) { $exit_code = 1; } +} + +exit $exit_code; + +#====================================================== + +sub fix_pdf { + # Change/insert byteCount field with correct file length, while preserving + # the file size and the length of the stream containing xmp metadata. + # Return 1 on success, else 0. + + local $pdf_name = shift; + local $tmp_name = "$pdf_name.new.pdf"; + local $pdf_size = (stat($pdf_name))[7]; + warn "Inserting/correcting byteCount field in '$pdf_name' ...\n"; + + # Strings surrounding (and identifying) the byteCount field, and other + # parts of the xmp packet: + local $xmp_start = ''; + local $decl_bC = 'byteCount'; + local $pre_bC = ''; + local $post_bC = ''; + local $pC = ''; + local $rd_end = ''; + local $xmp_end = ''; + + local *PDF; + local *TMP; + + if (! open PDF, "<", $pdf_name ) { + warn " Cannot read '$pdf_name'\n"; + return 0; + } + if ( ! open TMP, ">", $tmp_name ) { + warn " Cannot write temporary file '$tmp_name'\n"; + close PDF; + return 0; + } + local $status = 0; # 0 = no XMP packet, 1 = success, >= errors + while ( ) { + # Only examine first XMP packet: + if ( ($status == 0) && /^\s*\Q$xmp_start\E/ ) { + local @xmp = $_; + local $len_padding = 0; + local $xmp_after_line = ''; + &xmp_get_mod; + print TMP @xmp; + # Insert correct padding to leave file size unchanged: + while ( $len_padding > 0 ) { + my $len_line = 64; + if ( $len_line > $len_padding ) { $len_line = $len_padding; } + $len_padding -= $len_line; + print TMP (' ' x ($len_line - 1) ), "\n"; + } + print TMP $xmp_after_line; + $xmp_after_line = ''; + } + else { + print TMP "$_"; + } + } + close PDF; + close TMP; + + if ($status == 0) { + warn " Could not insert/modify byteCount, since no XMP packet was found.\n"; + warn " So '$pdf_name' is unchanged,\n", + " and I will delete temporary file '$tmp_name'.\n"; + unlink $tmp_name; + } elsif ($status == 1) { + rename $tmp_name, $pdf_name + or die " Cannot move temporary file '$tmp_name' to '$pdf_name'.\n", + " Error is '$!'\n"; + } else { + warn " Could not insert correct byteCount. See above for reason.\n"; + warn " So '$pdf_name' is unchanged,\n", + " and I will delete temporary file '$tmp_name'.\n"; + unlink $tmp_name; + } + return ($status == 1); +} + +#====================================================== + +sub xmp_get_mod { + # Get xmp packet, given that @xmp contains its first line. + # Get amount of trailing padding, and line after that. + # If possible, insert a byteCount field: + # Either replace existing specification, if it exists, + # or insert one in expected place for hyperxmp, if the XMP packet + # matches what hyperxmp would produce. + # Return xmp packet in @xmp, amount of padding needed in $len_padding, + # line after that in $xmp_after_line, and error code in $error. + # Set $status appropriately: 1 for success; >=1 for failure. + + $len_padding = 0; + $xmp_after_line = ''; + + my $bC_index = -1; + my $xmp_end_found = 0; + my $decl_bC_found = 0; + while ( ) { + push @xmp, $_; + if ( /^\s*\Q$xmp_end\E/ ) { + $xmp_end_found = 1; + # Get amount of padding; + while () { + if ( /^\s*$/ ) { + $len_padding += length($_); + } else { + $xmp_after_line = $_; + last; + } + } + last; + } + elsif ( $bC_index >= 0 ){ + next; + } + # Rest of conditions only apply if no place yet found for byteCount + # specification. + elsif ( /^(\s*)\Q$pre_bC\E.*?\Q$post_bC\E\s*$/ ) { + $bC_index = $#xmp; + } + elsif ( /^\s*\Q$decl_bC\E/ ) { + $decl_bC_found = 1; + } + elsif ( /^(\s*)\Q$rd_end\E/ ){ + # End of rdf:Description block. + # So having previous declaration of byteCount is irrelevant. + $decl_bC_found = 0; + } + elsif ( $decl_bC_found && /^(\s*)\Q$pC\E/ ){ + $bC_index = $#xmp; + pop @xmp; + push @xmp, '', $_; + } + + } # End reading of XMP + + if ($bC_index < 0) { + if ( ! $xmp_end_found ) { + warn " End of XMP packet not found.\n"; + $status = 2; + } + elsif ( ! $decl_bC_found ) { + warn " XMP packet not in appropriate hyperxmp-compatible format.\n"; + $status = 3; + } + return; + } + my $new_line = ' ' . $pre_bC . $pdf_size . $post_bC . "\n"; + my $old_line = $xmp[$bC_index]; + my $delta_len = length($new_line) - length($old_line); + if ($delta_len > $len_padding) { + warn " Cannot get padding correct for '$pdf_name'.\n", + " Length change of bC line = $delta_len; ", + " Padding bytes available = $len_padding.\n"; + $status = 4; + return; + } else { + $len_padding -= $delta_len; + $xmp[$bC_index] = $new_line; + $status = 1; + } +} + +#====================================================== diff --git a/macros/latex/contrib/hyperxmp/hyperxmp.dtx b/macros/latex/contrib/hyperxmp/hyperxmp.dtx index 47968aed38..4fad6b29d0 100644 --- a/macros/latex/contrib/hyperxmp/hyperxmp.dtx +++ b/macros/latex/contrib/hyperxmp/hyperxmp.dtx @@ -22,7 +22,7 @@ %\NeedsTeXFormat{LaTeX2e}[1999/12/01] %\ProvidesPackage{hyperxmp} %<*package> - [2020/11/18 v5.8 Store hyperref metadata in XMP format] + [2020/11/22 v5.9 Store hyperref metadata in XMP format] % % %<*driver> @@ -1495,15 +1495,17 @@ % the byte count to converge relative to the the number of compilations % that would otherwise be required. % -% Starting with \pkgname{hyperxmp}~v5.8, the \pkgname{hyperxmp} +% Starting with \pkgname{hyperxmp}~v5.9, the \pkgname{hyperxmp} % distribution includes a Perl\index{Perl} script called -% \progname{add\_byteCount} that edits a \acro{PDF} file in place, -% adding or replacing the \xmpprop{prism:byteCount} property with one -% that specifies the final file size. Run the script as -% ``\progname{add\_byteCount} \meta{filename.pdf}''. +% \progname{hyperxmp-add-bytecount} that edits a \acro{PDF} file in +% place, adding or replacing the \xmpprop{prism:byteCount} property with +% one that specifies the final file size.\footnote{The script was in +% fact introduced with \progname{hyperxmp}~v5.8 and was then called +% \texttt{add\_byteCount}.} Run the script as +% ``\progname{hyperxmp-add-bytecount} \meta{filename.pdf}''. % % The \progname{latexmk} build tool can be configured to run -% \progname{add\_byteCount} automatically every time a \acro{PDF} file +% \progname{hyperxmp-add-bytecount} automatically every time a \acro{PDF} file % is generated. Simply add the code shown in Figure~\ref{fig:latexmkrc} % to your \progname{latexmk} configuration file. See % \href{http://mirrors.ctan.org/support/latexmk/latexmk.pdf}{the @@ -1515,6 +1517,9 @@ % \protect\progname{add\_byteCount} script and document some sample % \protect\progname{latexmk} configuration code that invokes it. % Thanks to John Collins for providing both of those} +% \changes{v5.9}{2020/11/22}{At Karl Berry's request, rename +% \protect\progname{add\_byteCount} to the less generic-sounding +% \protect\progname{hyperxmp-add-bytecount}} % % \iffalse %<*listings> @@ -1528,17 +1533,18 @@ frame=single, basicstyle=\footnotesize, showstringspaces=false, - upquote=true + upquote=true, + literate={-}{-}{1} ] foreach my $cmd ( "latex", "lualatex", "pdflatex", "xelatex", "dvipdf", "xdvipdfmx", "ps2pdf" ) { - ${$cmd} = "internal mycmd4 ${$cmd}"; + ${$cmd} = "internal mycmd ${$cmd}"; } -sub mycmd4 { +sub mycmd { my $retval = system @_; if ( $$Pdest =~ /\.pdf$/ ) { - system 'add_byteCount', $$Pdest; + system 'hyperxmp-add-bytecount', $$Pdest; } return $retval; } @@ -1552,7 +1558,7 @@ sub mycmd4 { % \centering % \usebox{\latexmkrcbox} % \caption{ \progname{latexmk} configuration-file code for -% automatically invoking \progname{add\_byteCount} every time a +% automatically invoking \progname{hyperxmp-add-bytecount} every time a % \acro{PDF} file is generated} % \label{fig:latexmkrc} % \end{figure} diff --git a/macros/latex/contrib/hyperxmp/hyperxmp.pdf b/macros/latex/contrib/hyperxmp/hyperxmp.pdf index 80892a532a..f11adaee15 100644 Binary files a/macros/latex/contrib/hyperxmp/hyperxmp.pdf and b/macros/latex/contrib/hyperxmp/hyperxmp.pdf differ -- cgit v1.2.3