\RequirePackage{jbw-prog-util}
\RequirePackage{ifthen}

\AtBeginDocument{\jbwpp@SetPDFPaperSize}

\MakeOtherCharTokens{pt}
  {% #1 is "pt" except the character tokens are of "other" category
   \newcommand{\ConvertToBigPoints}[1]
     {% ##1 is DIMENSION
      % The result is \usetemplate{{CONVERTED_DIMENSION}}
      % where DIMENSION = CONVERTED_DIMENSION bp.
      \begingroup
      \dimen0=##1\relax
      % 800bp = 803pt, so multiply by 800/803
      \dimen0=0.99626400996264\dimen0\relax
      \expandafter
      \ParseArgs\expandafter{\the\dimen0 }
        {####1#1}
        {% ####1 is DIMENSION converted to bp
         \endgroup
         \usetemplate{{####1}}}}}

\newcommand{\RoundToNearestInteger}[1]
  {% #1 is REAL_NUMBER (possible leading - followed by decimal number)
   % The result is \usetemplate{{ROUNDED_NUMBER}} where ROUNDED_NUMBER
   % is the result of rounding REAL_NUMBER to the nearest integer.
   \ParseArgs{#1x#1x}
     {##1.##2x##3##4.##5x}
     {% ##1 is SIGNED_INTEGER_OR_ABS_INTEGER
      % ##2 is FRACTION
      % ##3 is DIGIT_OR_MINUS
      % ##4 is ABS_INTEGER_OR_JUNK
      % ##5 is JUNK
      \IfChar{##3-}
        {\usetemplate{{-}{##4}{##2}}}
        {\usetemplate{{}{##1}{##2}}}}%
           % indented because this is the 2nd arg of \usetemplate
           {% ##1 is SIGN
            % ##2 is ABS_INTEGER
            % ##3 is FRACTION
            \begingroup
            \count0=##2\relax
            \dimen0=0.##3pt\relax
            \IfDim{\dimen0<0.5pt\relax}
              {}
              {\advance\count0 by 1\relax}%
            \Expand{\the\count0 }
              {% ####1 is ROUNDED_ABS_INTEGER
               \endgroup
               \usetemplate{{##1####1}}}}}

\newcommand{\RoundAwayFromZero}[1]
  {% #1 is REAL_NUMBER (possible leading - followed by decimal number)
   % The result is \usetemplate{{ROUNDED_NUMBER}} where ROUNDED_NUMBER
   % is the result of rounding REAL_NUMBER to an integer.
   \ParseArgs{#1x#1x}
     {##1.##2x##3##4.##5x}
     {% ##1 is SIGNED_INTEGER_OR_ABS_INTEGER
      % ##2 is FRACTION
      % ##3 is DIGIT_OR_MINUS
      % ##4 is ABS_INTEGER_OR_JUNK
      % ##5 is JUNK
      \IfChar{##3-}
        {\usetemplate{{-}{##4}{##2}}}
        {\usetemplate{{}{##1}{##2}}}}%
           % indented because this is the 2nd arg of \usetemplate
           {% ##1 is SIGN
            % ##2 is ABS_INTEGER
            % ##3 is FRACTION
            \begingroup
            \count0=##2\relax
            \dimen0=0.##3pt\relax
            \IfDim{\dimen0=0pt\relax}
              {}
              {\advance\count0 by 1\relax}%
            \Expand{\the\count0 }
              {% ####1 is ROUNDED_ABS_INTEGER
               \endgroup
               \usetemplate{{##1####1}}}}}

\newcommand{\RoundTowardsZero}[1]
  {% #1 is REAL_NUMBER (possible leading - followed by decimal number)
   % The result is \usetemplate{{ROUNDED_NUMBER}} where ROUNDED_NUMBER
   % is the result of rounding REAL_NUMBER to an integer.
   \ParseArgs{#1x}
     {##1.##2x}
     {% ##1 is SIGNED_INTEGER_OR_ABS_INTEGER
      % ##2 is FRACTION
      \usetemplate{{##1}}}}

\newcommand{\ComposeTwo}[3]
  {#1{#3}{#2{##1}}}

\newcommand{\ConvertToBigPointsAndRound}
  {\ComposeTwo{\ConvertToBigPoints}{\RoundToNearestInteger}}
  %[1]
  %{% #1 is DIMENSION
  % % Result is \usetemplate{{CONVERTED_ROUNDED_DIMENSION}}.
  % \ConvertToBigPoints{#1}
  %   {% ##1 is CONVERTED_DIMENSION
  %    \RoundToNearestInteger{##1}
  %      {% ####1 is CONVERTED_ROUNDED_DIMENSION
  %       \usetemplate{{####1}}}}}

% This is all a crock.  The \special{papersize=HDIMEN,VDIMEN} should
% be sufficient.  But dvips doesn't put anything in the PostScript
% output that ps2pdf knows how to grok.  So we have to include an
% explicit PostScript setting of the /PageSize attribute.

% ps2pdf (or perhaps the PostScript definition) rounds the page size
% dimensions down to an integral number of PostScript points.  Since
% rounding down is perhaps not the best, we round here to the
% _nearest_ integral number of PostScript points.

\DeclareRobustCommand{\jbwpp@SetPDFPaperSize}
  {\ConvertToBigPointsAndRound{\paperheight}
     {% ##1 is CONVERTED_ROUNDED_HEIGHT
      \ConvertToBigPointsAndRound{\paperwidth}
        {% ####1 is CONVERTED_ROUNDED_WIDTH
         \special{! << /PageSize [####1 ##1] >> setpagedevice}}}}

\DeclareRobustCommand{\SetPDFCrop}[4]
  {\begingroup
   \ComposeTwo{\ConvertToBigPoints}{\RoundTowardsZero}{#1}
     {\def\jbwpp@Left{##1}}%
   \ComposeTwo{\ConvertToBigPoints}{\RoundTowardsZero}{#2}
     {\def\jbwpp@Bottom{##1}}%
   \ComposeTwo{\ConvertToBigPoints}{\RoundAwayFromZero}{#3}
     {\def\jbwpp@Right{##1}}%
   \ComposeTwo{\ConvertToBigPoints}{\RoundAwayFromZero}{#4}
     {\def\jbwpp@Top{##1}}%
   \edef\jbwpp@Edges{{\jbwpp@Left}{\jbwpp@Bottom}{\jbwpp@Right}{\jbwpp@Top}}%
   \expandafter
   \usetemplate\expandafter{\jbwpp@Edges}
     {\endgroup
      \special{! [ /CropBox [##1 ##2 ##3 ##4] /PAGES pdfmark}%
      \iffalse]\fi% brace matching hack
      %\hypersetup{pdfpagescrop=##1 ##2 ##3 ##4}
     }}
