diff options
author | Norbert Preining <norbert@preining.info> | 2020-11-20 03:01:07 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2020-11-20 03:01:07 +0000 |
commit | 9fd41d05ec3aeb960a242af4f054e5c2d21de4e1 (patch) | |
tree | c5b4d8f677677e0a172270edf93e0600e8814f1f /macros/latex/contrib/hyperxmp | |
parent | 760d89b8378840fd2799ef792b5300b760723bff (diff) |
CTAN sync 202011200301
Diffstat (limited to 'macros/latex/contrib/hyperxmp')
-rw-r--r-- | macros/latex/contrib/hyperxmp/README | 10 | ||||
-rw-r--r-- | macros/latex/contrib/hyperxmp/add_byteCount.pl | 193 | ||||
-rw-r--r-- | macros/latex/contrib/hyperxmp/hyperxmp.dtx | 210 | ||||
-rw-r--r-- | macros/latex/contrib/hyperxmp/hyperxmp.pdf | bin | 1549965 -> 1551868 bytes |
4 files changed, 359 insertions, 54 deletions
diff --git a/macros/latex/contrib/hyperxmp/README b/macros/latex/contrib/hyperxmp/README index 4fb504485d..93a2733315 100644 --- a/macros/latex/contrib/hyperxmp/README +++ b/macros/latex/contrib/hyperxmp/README @@ -36,13 +36,13 @@ installing packages. Building hyperxmp.pdf is a little more involved: - pdflatex hyperxmp.dtx - pdflatex hyperxmp.dtx + lualatex hyperxmp.dtx + lualatex hyperxmp.dtx makeindex -s gind-hyxmp.ist hyperxmp.idx -o hyperxmp.ind makeindex -s gglo-hyxmp.ist hyperxmp.glo -o hyperxmp.gls - pdflatex hyperxmp.dtx - pdflatex hyperxmp.dtx - + lualatex hyperxmp.dtx + lualatex hyperxmp.dtx + perl add_byteCount.pl hyperxmp.pdf Copyright and license ===================== diff --git a/macros/latex/contrib/hyperxmp/add_byteCount.pl b/macros/latex/contrib/hyperxmp/add_byteCount.pl new file mode 100644 index 0000000000..0cfa3e094f --- /dev/null +++ b/macros/latex/contrib/hyperxmp/add_byteCount.pl @@ -0,0 +1,193 @@ +#!/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 = '<x:xmpmeta xmlns:x="adobe:ns:meta/">'; + local $decl_bC = '<pdfaProperty:name>byteCount</pdfaProperty:name>'; + local $pre_bC = '<prism:byteCount>'; + local $post_bC = '</prism:byteCount>'; + local $pC = '<prism:pageCount>'; + local $rd_end = '</rdf:Description>'; + local $xmp_end = '</x:xmpmeta>'; + + 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 ( <PDF> ) { + # 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 ( <PDF> ) { + push @xmp, $_; + if ( /^\s*\Q$xmp_end\E/ ) { + $xmp_end_found = 1; + # Get amount of padding; + while (<PDF>) { + 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 a6dd612e77..47968aed38 100644 --- a/macros/latex/contrib/hyperxmp/hyperxmp.dtx +++ b/macros/latex/contrib/hyperxmp/hyperxmp.dtx @@ -22,7 +22,7 @@ %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01] %<package>\ProvidesPackage{hyperxmp} %<*package> - [2020/11/01 v5.7 Store hyperref metadata in XMP format] + [2020/11/18 v5.8 Store hyperref metadata in XMP format] %</package> % %<*driver> @@ -116,7 +116,7 @@ %</driver> % \fi % -% \CheckSum{2826} +% \CheckSum{2836} % % \CharacterTable % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z @@ -305,7 +305,7 @@ % \texorpdfstring{Dvips\SortIndex{dvips}{\texttt{dvips}}}{Dvips}^^A % } % \DeclareRobustCommand{\progname}[1]{^^A -% \texttt{#1}\SortIndex{#1}{\texttt{#1}}^^A +% \mbox{\texttt{#1}}\SortIndex{#1}{\texttt{#1}}^^A % } % \DeclareRobustCommand{\Lua}{^^A % Lua\index{Lua}^^A @@ -1489,68 +1489,82 @@ % property that indicates the \acro{PDF} file size in bytes. % \pkgname{hyperxmp} computes this value automatically when the document % is built using \LuaLaTeX\ but not when using any other \TeX\ engine. -% (Note that \pkgname{hyperxmp} uses the file size from the +% Note that \pkgname{hyperxmp} uses the file size from the % \emph{previous} run of \LuaLaTeX\ because the new \acro{PDF} file is -% not yet complete.) -% -% The \progname{latexmk} build tool can be configured to automatically -% append a line of the form ``|\hypersetup{pdfbytes=|\meta{number}|}|'' -% to the generated |.aux| file. The subsequent build of the document -% will read the |.aux| file at the end of the preamble and set -% \optname{pdfbytes} accordingly. The Perl code shown in -% Figure~\ref{fig:latexmkrc}, derived from code posted by Nelson Posse -% Lago and John Collins on -% \url{https://github.com/borisveytsman/acmart/issues/413}, can be -% included in a \progname{latexmk} configuration file to set -% \optname{pdfbytes} automatically whenever \progname{latexmk} builds a -% \pkgname{hyperxmp}-based document using -% \pdfLaTeX\ (\progname{latexmk}'s |--pdf| option). The code can be -% adapted, \textit{mutatis mutandis}, to work with \XeLaTeX\ builds -% (\progname{latexmk}'s |--pdfxe| or |--xelatex| option). +% not yet complete. Consequently, one extra compilation is needed for +% 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} +% 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}''. +% +% The \progname{latexmk} build tool can be configured to run +% \progname{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 +% \progname{latexmk} manual} for information on configuration-file +% naming on different operating systems and explanations of the hook +% functions used in Figure~\ref{fig:latexmkrc}. +% +% \changes{v5.8}{2020/11/18}{Distribute an +% \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} % % \iffalse %<*listings> % \fi -\begin{figure} +\newsavebox{\latexmkrcbox} +\begin{lrbox}{\latexmkrcbox} \begin{lstlisting}[ language=perl, columns=fullflexible, + linewidth=19pc, frame=single, basicstyle=\footnotesize, - showstringspaces=false + showstringspaces=false, + upquote=true ] -$pdflatex = 'internal mylatex %O %S'; - -sub mylatex { - my @args = @_; - $retval = system 'pdflatex', @args; - get_pdf_size(); - return $retval; +foreach my $cmd ( "latex", "lualatex", "pdflatex", "xelatex", + "dvipdf", "xdvipdfmx", "ps2pdf" ) { + ${$cmd} = "internal mycmd4 ${$cmd}"; } -sub get_pdf_size { - my $size = (stat($out_dir1 . $root_filename . ".pdf"))[7]; - open(AUX, '>>', $aux_dir1 . $root_filename . ".aux") - || die "Can't open file " . $aux_dir1 . $root_filename . ".aux: $!"; - if (defined($size)) { - print AUX "\\\@ifpackageloaded{hyperxmp}{\\hypersetup{pdfbytes=$size}}{}\n"; +sub mycmd4 { + my $retval = system @_; + if ( $$Pdest =~ /\.pdf$/ ) { + system 'add_byteCount', $$Pdest; } - close(AUX); + return $retval; } \end{lstlisting} - \caption{Code to include in a \progname{latexmk} configuration file - to automate the assignment of \pkgname{hyperxmp}'s - \optname{pdfbytes} option when building with \pdfLaTeX} - \label{fig:latexmkrc} -\end{figure} +\end{lrbox} % \iffalse %</listings> % \fi % -% See \href{http://mirrors.ctan.org/support/latexmk/latexmk.pdf}{the -% \progname{latexmk} manual} for information on configuration-file -% naming on different operating systems and explanations of the hooks -% used in the Figure~\ref{fig:latexmkrc} code. +% \begin{figure} +% \centering +% \usebox{\latexmkrcbox} +% \caption{ \progname{latexmk} configuration-file code for +% automatically invoking \progname{add\_byteCount} every time a +% \acro{PDF} file is generated} +% \label{fig:latexmkrc} +% \end{figure} +% +% Even though \pkgname{hyperxmp} can compute the byte count +% automatically when run from \LuaLaTeX, users of \progname{latexmk} +% need to use configuration-file code like that shown in +% Figure~\ref{fig:latexmkrc}. Otherwise, \progname{latexmk} would +% compile the document one time too few for the byte count to +% converge. It is recommended that those who use both \progname{latexmk} +% and \pkgname{hyperxmp} configure \progname{latexmk} to be +% \pkgname{hyperxmp}-aware. % % % \StopEventually{^^A @@ -3194,13 +3208,111 @@ sub get_pdf_size { % Derek Dreyer, and the other contributors to % \protect\href{https://github.com/borisveytsman/acmart/issues/413}{\protect\pkgname{acmart} issue \#413}, % ``Latexmk goes into an infinite loop even on sample files from ACM''} +% \changes{v5.8}{2020/11/05}{Take +% \protect\mbox{\protect\texttt{-\kern0pt-output-directory}} into +% consideration when querying the output file size. Thanks to John +% Collins for pointing out that the user can change the output +% directory} % \begin{macrocode} \ifLuaTeX - \edef\hyxmp@prev@pdf@size{% - \luadirect{% -local nbytes = lfs.attributes("\hyxmp@jobname.pdf", "size") +% \end{macrocode} +% Now that we know we're running \LuaLaTeX\ we define a Lua function, +% |get_pdf_size|, that takes the base name of the output file and +% returns the number of bytes in the corresponding \acro{PDF} file. One +% difficulty is that, at the time of this writing, \LuaTeX\ lacks a +% mechanism for querying the full name of the output file. Our +% workaround is a tad kludgy but seems to work. We walk the list of +% command-line arguments for +% \mbox{``|-\kern0pt-output-directory=|\meta{dir}''}. (We in fact +% accept either one or two initial dashes and abbreviations as terse as +% \mbox{``|-output-d|''}.) Then, we concatenate the output directory +% (or ``|.|'' if unspecified), a path separator, the given base name of +% the job, and a ``|.pdf|'' extension. Alas, different operating +% systems use different path separators so we have to query the +% operating-system type to select an appropriate separator: ``|\|'' on +% Windows/\acro{DOS} and ``|/|'' on everything else. +% +% |get_pdf_size| is called regardless of whether we're producing +% \acro{PDF} or \acro{DVI} output. We assume that even if the user +% specified |--output-format=dvi|, the user's intention is eventually to +% convert the document to \acro{PDF}. +% \begin{macrocode} + \begin{luacode*} +function get_pdf_size (bname) +% \end{macrocode} +% Search the list of command-line arguments for the output directory. +% \begin{macrocode} + local odir = "" + for _, opt in ipairs(arg) do + local m = string.match(opt, "%-output%-d.-=(.*)") + if m then + odir = m + end + end +% \end{macrocode} +% Set the path separator to either ``|/|'' or ``|\|'', depending on the +% operating system. +% \begin{macrocode} + local sep = "/" + if os.type == "windows" or os.type == "msdos" then + sep = "\\\\" + end +% \end{macrocode} +% Concatenate the output directory, path separator, base name, and +% \texttt{.pdf} extension. Do not insert a path separator if either +% (1)~no output directory was specified, (2)~the output directory +% already ends with the path separator, or (3)~the output directory ends +% in a colon (and is therefore a relative directory) on +% Windows/\acro{DOS}. As a few examples, +% +% \bigskip +% \noindent +% \begingroup +% \let\bs=\textbackslash +% \begin{tabular}{@{\quad\textbullet~}l@{~+~}l@{~+~}l@{~+~``\texttt{.pdf}''~=~}l@{}} +% ``'' & ``\texttt{/}'' & ``\texttt{myfile}'' +% & ``\texttt{myfile.pdf}'' \\ +% ``\texttt{/docs}'' & ``\texttt{/}'' & ``\texttt{myfile}'' +% & ``\texttt{/docs/myfile.pdf}'' \\ +% ``\texttt{/docs/}'' & ``\texttt{/}'' & ``\texttt{myfile}'' +% & ``\texttt{/docs/myfile.pdf}'' \\ +% ``\texttt{C:\bs docs}'' & ``\texttt{\bs}'' & ``\texttt{myfile}'' +% & ``\texttt{C:\bs docs\bs myfile.pdf}'' \\ +% ``\texttt{C:\bs docs\bs}'' & ``\texttt{\bs}'' & ``\texttt{myfile}'' +% & ``\texttt{C:\bs docs\bs myfile.pdf}'' \\ +% ``\texttt{C:\bs}'' & ``\texttt{\bs}'' & ``\texttt{myfile}'' +% & ``\texttt{C:\bs myfile.pdf}'' \\ +% ``\texttt{C:}'' & ``\texttt{\bs}'' & ``\texttt{myfile}'' +% & ``\texttt{C:myfile.pdf}'' \\ +% \end{tabular} +% \endgroup +% \bigskip +% \begin{macrocode} + local dlast = string.sub(odir, -1) + if odir == "" or dlast == sep or (dlast == ":" and sep == "\\\\") then + sep = "" + end + local fname = odir .. sep .. bname .. ".pdf" +% \end{macrocode} +% Query the file size and return it. +% \begin{macrocode} + local nbytes = lfs.attributes(fname, "size") + return nbytes +end + \end{luacode*} +% \end{macrocode} +% Now that we've defined |get_pdf_size| we invoke it, passing it +% \cs{hyxmp@jobname} as the base name of the job. (Recall that +% \cs{hyxmp@jobname} is the same as \cs{jobname} but with any +% surrounding double quotes removed.) We store |get_pdf_size|'s +% output---which will be empty if the \acro{PDF} file doesn't yet +% exist---in \cs{hyxmp@prev@pdf@size}. +% \begin{macrocode} + \xdef\hyxmp@prev@pdf@size{% + \luadirect{ +nbytes = get_pdf_size("\hyxmp@jobname") if nbytes then - tex.write(nbytes) + tex.write(nbytes) end }% }% diff --git a/macros/latex/contrib/hyperxmp/hyperxmp.pdf b/macros/latex/contrib/hyperxmp/hyperxmp.pdf Binary files differindex d62a5dc555..80892a532a 100644 --- a/macros/latex/contrib/hyperxmp/hyperxmp.pdf +++ b/macros/latex/contrib/hyperxmp/hyperxmp.pdf |