\ProvidesPackage{zwpagelayout}[2011/11/22 ZW Page Layout]
\PackageInfo{zwpagelayout}{$Id: zwpagelayout.sty 419 2011-11-22 16:57:09Z zw $\@gobble}

%% Copyright 2008--2011 Z. Wagner, http://icebearsoft.euweb.cz
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% 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.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Z. Wagner.
%
% This work consists of the files: zwpagelayout.sty and
% the documentation files zwpagelayout.tex, zwpagelayout.pdf together with
% adjustfoot.tex, adjustfoot.pdf, adjusthead.tex, adjusthead.pdf,
% coversample.tex, coversample.pdf, LoremIpsumDolor.tex.

\@ifpackageloaded{ifpdf}{}{\IfFileExists{ifpdf.sty}{\RequirePackage{ifpdf}}{\newif\ifpdf}}
\@ifpackageloaded{ifxetex}{}{\IfFileExists{ifxetex.sty}{\RequirePackage{ifxetex}}{\newif\ifxetex}}
\RequirePackage{kvoptions}[2010/02/22]
\SetupKeyvalOptions{family=zwpl,prefix=zwpl@}

% Fix a problem with xkeyval

\expandafter\ifx\csname kvsetkeys\endcsname\relax
  \let\ZWsetkeys\setkeys
\else
  \let\ZWsetkeys\kvsetkeys
\fi

% Backslash character with catcode=12

{\lccode`\!`\\ \lowercase{\gdef\PDFbkslash{!}}}

% Percent character with catcode=12

{\lccode`\!`\% \lowercase{\gdef\ZWpercent{!}}}

% Driver selection macros and options

\def\zwpl@useunknown{0}
\def\zwpl@usepdftex{1}
\def\zwpl@usexetex{2}
\def\zwpl@usedvips{3}
\let\zwpl@usedvipdfm\zwpl@usexetex
\let\zwpl@usexdvipdfmx\zwpl@usexetex
\let\zwpl@useother\zwpl@useunknown
\newcount\zwpl@Driver
\zwpl@Driver\ifpdf \zwpl@usepdftex \else \ifxetex \zwpl@usexetex \else \zwpl@usedvips \fi \fi
\relax

\DeclareStringOption{driver}

% Landscape/Portrait

\DeclareBoolOption{Landscape}
\DeclareComplementaryOption{Portrait}{Landscape}
\DeclareBoolOption{AllowWidthHeightSwitching}

% Generic paper size, default=A4

\DeclareStringOption[210mm,297mm]{papersize}[] % empty means to calculate

% Normalized paper sizes

\DeclareVoidOption{a0}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={841mm,1189mm}}}
\DeclareVoidOption{a1}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={594mm,841mm}}}
\DeclareVoidOption{a2}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={420mm,594mm}}}
\DeclareVoidOption{a3}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={297mm,420mm}}}
\DeclareVoidOption{a4}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={210mm,297mm}}}
\DeclareVoidOption{a5}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={148mm,210mm}}}
\DeclareVoidOption{a6}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={105mm,148mm}}}
\DeclareVoidOption{a7}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={74mm,105mm}}}
\DeclareVoidOption{a8}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={52mm,74mm}}}
\DeclareVoidOption{a9}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={37mm,52mm}}}
\DeclareVoidOption{a10}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={26mm,37mm}}}
\DeclareVoidOption{b0}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={1000mm,1414mm}}}
\DeclareVoidOption{b1}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={707mm,1000mm}}}
\DeclareVoidOption{b2}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={500mm,707mm}}}
\DeclareVoidOption{b3}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={353mm,500mm}}}
\DeclareVoidOption{b4}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={250mm,353mm}}}
\DeclareVoidOption{b5}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={176mm,250mm}}}
\DeclareVoidOption{b6}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={125mm,176mm}}}
\DeclareVoidOption{b7}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={88mm,125mm}}}
\DeclareVoidOption{b8}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={62mm,88mm}}}
\DeclareVoidOption{b9}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={44mm,62mm}}}
\DeclareVoidOption{b10}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={31mm,44mm}}}
\DeclareVoidOption{c0}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={917mm,1279mm}}}
\DeclareVoidOption{c1}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={648mm,917mm}}}
\DeclareVoidOption{c2}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={458mm,648mm}}}
\DeclareVoidOption{c3}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={324mm,458mm}}}
\DeclareVoidOption{c4}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={229mm,324mm}}}
\DeclareVoidOption{c5}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={162mm,229mm}}}
\DeclareVoidOption{c6}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={114mm,162mm}}}
\DeclareVoidOption{c7}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={81mm,114mm}}}
\DeclareVoidOption{c8}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={57mm,81mm}}}
\DeclareVoidOption{c9}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={40mm,57mm}}}
\DeclareVoidOption{c10}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={28mm,40mm}}}
\DeclareVoidOption{letter}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={8.5in,11in}}}
\DeclareVoidOption{legal}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={8.5in,14in}}}
\DeclareVoidOption{executive}{\ZWsetkeys{zwpl}{AllowWidthHeightSwitching,papersize={7.25in,10.5in}}}

% Layout (for the odd page, even page is mirrored)

\DeclareStringOption{margins}[0mm]
\DeclareStringOption[1in]{topmargin}
\DeclareStringOption[-1in]{leftmargin}
\DeclareStringOption[-1in]{rightmargin}
\DeclareStringOption[-1in]{textwidth}
\DeclareStringOption[-1in]{textheight} % height including a header, headsep, and footskip
\DeclareStringOption[0mm]{headheight}
\DeclareStringOption[0mm]{headsep}
\DeclareStringOption[0mm]{footskip}
\DeclareStringOption{topskip}
\DeclareStringOption{botmargin}[\zwpl@topmargin]
\DeclareBoolOption{strictheight}
\DeclareBoolOption[true]{adjustfootskip}
\DeclareComplementaryOption{adjustheadsep}{adjustfootskip}

% Options for cropmarks

\DeclareBoolOption{onlycropmarks}% Page layout is already set in a compatible way
\DeclareBoolOption{cropmarks}
\DeclareComplementaryOption{nocropmarks}{cropmarks}
\DeclareStringOption[5mm]{croplength}
\DeclareStringOption[5mm]{cropgap}
\DeclareStringOption{spine}
\DeclareStringOption{xspine}
\DeclareStringOption{flap}
\DeclareStringOption{xtrim}
\DeclareStringOption{ytrim}
\DeclareStringOption{trim}
\DeclareBoolOption{cropframe}
\DeclareComplementaryOption{nocropframe}{cropframe}
\DeclareStringOption[default]{cropmarkstyle}
\DeclareStringOption{croptitle}
\DeclareStringOption[:\quad]{cropseparator}
\DeclareBoolOption{pagenumberfirst}
\DeclareComplementaryOption{pagenumberlast}{pagenumberfirst}
\DeclareBoolOption[true]{usepagenumbers}
\DeclareComplementaryOption{nopagenumbers}{usepagenumbers}
%%%\DeclareStringOption[\fontsize{10}{10}]{cropfontsize}
%%%\DeclareStringOption[\normalfont]{cropfont}
\DeclareBoolOption{nobleedclip}
\DeclareBoolOption{noBboxes}

% Leaflet cropmarks (cropmarkstyle=leaflet)
\DeclareStringOption[0mm]{foldcorr}% Ignored when fold=4 and fold=Z
\DeclareStringOption[2]{fold}% Valid options: 2, 3left, 3right, 4, Z
\DeclareStringOption[4]{leafcount}% For fold=Z only

% Color support for cropmarks and black overprint
\DeclareBoolOption{color}
\DeclareStringOption[cmyk]{colormodel}
\DeclareStringOption[1,1,1,1]{cropcolor}
\DeclareStringOption[{CYAN:1,0,0,0},{MAGENTA:0,1,0,0},{YELLOW:0,0,1,0},{BLACK:0,0,0,1}]{colors}
\DeclareBoolOption{redefineblack}
\DeclareBoolOption{redefinetocmyk}

% Overprint support, it will automatically request color
\DeclareBoolOption{overprint}

% PDF information
\DeclareStringOption[\zwpl@default@title]{title}[]
\DeclareStringOption{author}
\DeclareStringOption{subject}
\DeclareStringOption{keywords}
\DeclareVoidOption{nopdfinfo}{\ZWsetkeys{zwpl}{title}}

\def\zwpl@default@title{\ifcat$\zwpl@croptitle$\jobname\else\zwpl@croptitle\fi}

% PDF/X-1a support etc.
\DeclareStringOption{pdfminorversion}
\DeclareBoolOption{pdfx}
\DeclareStringOption[Euroscale Coated v2]{OutputCondition}
\DeclareStringOption[Euroscale Coated v2]{OutputConditionIdentifier}
\DeclareStringOption{ICCfile}

% Reflect, works with pdftex, (x)dvipdfm(x) aa well as dvips

\def\zwpl@makeHreflect{\AtBeginDocument{\ifcaseZWdriver
    \or
         \let\zwpl@Hship\shipout
         \def\shipout{\zwpl@Hreflect\zwpl@Hship}\zwpl@Hreflect
    \or
         \zwpl@XHreflect
    \else\zwpl@psHreflect\fi}}

\def\zwpl@makeVreflect{\AtBeginDocument{\ifcaseZWdriver
    \or
          \let\zwpl@Vship\shipout
          \def\shipout{\zwpl@Vreflect\zwpl@Vship}\zwpl@Vreflect
     \or
          \zwpl@XVreflect
     \else\zwpl@psVreflect\fi}}

\DeclareVoidOption{ReflectHorizontally}{\zwpl@makeHreflect}
\DeclareVoidOption{ReflectVertically}{\zwpl@makeVreflect}

\ProcessKeyvalOptions*

% Define the driver

\ifcat$\zwpl@driver$ \else
  \expandafter\ifx\csname zwpl@use\zwpl@driver\endcsname\relax
    \PackageErrorNoLine{zwpagelayout}{Unsupported driver \zwpl@driver}
  \else
    \zwpl@Driver\csname zwpl@use\zwpl@driver\endcsname\relax
  \fi
\fi
\def\ifcaseZWdriver{\ifcase\zwpl@Driver}
\DeclareRobustCommand\ZWifdriver[2]{\expandafter\ifx\csname zwpl@use#1\endcsname\relax
    \PackageWarningNoLine{zwpagelayout}{Driver #1 not recognized, code ignored}%
  \else
  \ifnum\csname zwpl@use#1\endcsname=\zwpl@Driver\relax #2\fi\fi}

\PackageInfo{zwpagelayout}{Driver mode used:
            \ifcaseZWdriver unknown\or pdftex\or xetex\else dvips\fi.\MessageBreak}

% Define pdfmark if the RIP does not know it

\ifnum\zwpl@Driver=\zwpl@usedvips
  \special{!/pdfmark where {pop}{userdict /pdfmark /cleartomark load put} ifelse}
\fi

% Other packages

\ifzwpl@onlycropmarks
  \ZWsetkeys{zwpl}{cropmarks}
\else

% Set the paper size

\def\zwpl@setpapersize#1,#2\zw@{\paperwidth\z@ \paperheight\z@
  \ifcat$#1$\else \paperwidth#1 \fi
  \ifcat$#2$\else \paperheight#2 \fi}

\ifcat$\zwpl@papersize$
  \zwpl@setpapersize,\zw@
\else
  \expandafter\zwpl@setpapersize\zwpl@papersize\zw@
\fi

\def\zwpl@switchpapdims{\@tempdima=\paperheight \paperheight=\paperwidth \paperwidth=\@tempdima}

\ifzwpl@AllowWidthHeightSwitching
  \ifzwpl@Landscape
    \ifdim\paperheight>\paperwidth \zwpl@switchpapdims\fi
  \else
    \ifdim\paperheight<\paperwidth \zwpl@switchpapdims\fi
  \fi
\fi

\AtBeginDocument{\ifcaseZWdriver \or
   \pdfpagewidth\paperwidth \pdfpageheight\paperheight
\else
   \special{papersize=\the\paperwidth,\the\paperheight}\fi}

\def\zwpl@setbpdim#1#2{\@tempdimc#2 \@tempdimc .99626\@tempdimc
    \edef#1{\strip@pt\@tempdimc}}

\def\zwpl@setboxex{\zwpl@setbpdim\zwpl@bleedorig\zwpl@croplength
    \@tempdima\paperwidth \advance\@tempdima-\zwpl@croplength
    \zwpl@setbpdim\zwpl@bleedwidth\@tempdima
    \@tempdima\paperheight \advance\@tempdima-\zwpl@croplength
    \zwpl@setbpdim\zwpl@bleedheight\@tempdima
    \@tempdimb\zwpl@croplength \advance\@tempdimb\zwpl@cropgap
    \zwpl@setbpdim\zwpl@trimorig\@tempdimb
    \@tempdima\paperwidth \advance\@tempdima-\@tempdimb
    \zwpl@setbpdim\zwpl@trimwidth\@tempdima
    \@tempdima\paperheight \advance\@tempdima-\@tempdimb
    \zwpl@setbpdim\zwpl@trimheight\@tempdima
    \edef\zw@tmp{/BleedBox [\zwpl@bleedorig\space \zwpl@bleedorig\space
                            \zwpl@bleedwidth\space \zwpl@bleedheight]
                 /TrimBox [\zwpl@trimorig\space \zwpl@trimorig\space
                           \zwpl@trimwidth\space \zwpl@trimheight]
                 /ArtBox [\zwpl@trimorig\space \zwpl@trimorig\space
                          \zwpl@trimwidth\space \zwpl@trimheight]}
    \ifcaseZWdriver
    \or
      \expandafter\pdfpageattr\expandafter{\zw@tmp}
    \or
      \edef\zw@tmpx{pdf:put @pages <<\zw@tmp>>}
      \expandafter\special\expandafter{\zw@tmpx}
    \else
      \special{! userdict begin
                 userdict /bop-hook known {/bop-hook load cvx /ZWBox-hook exch def} if
                 /bop-hook {
                   [/BleedBox [\zwpl@bleedorig\space \zwpl@bleedorig\space
                               \zwpl@bleedwidth\space \zwpl@bleedheight] /PAGE pdfmark
                   [/TrimBox [\zwpl@trimorig\space \zwpl@trimorig\space
                              \zwpl@trimwidth\space \zwpl@trimheight] /PAGE pdfmark
                   [/ArtBox [\zwpl@trimorig\space \zwpl@trimorig\space
                             \zwpl@trimwidth\space \zwpl@trimheight] /PAGE pdfmark
                   userdict /ZWBox-hook known {ZWBox-hook} if
                 } def
      end}
    \fi}

\ifzwpl@cropmarks
  \ifzwpl@noBboxes \else \AtBeginDocument{\zwpl@setboxex}\fi
\fi

\def\noBboxes{\def\zwpl@setboxex{}}
\@onlypreamble\noBboxes

% Two digits
\def\zwpl@num#1{\ifnum #1<10 0\fi \the#1}

% Date of TeXing in PDF format (without parentheses and D:)
\def\SetTeXingDate#1{\xdef#1{\the\year\zwpl@num\month\zwpl@num\day}}
\SetTeXingDate\DateOfTeXing

% Time of TeXing, you can modify Date of TeXing by:
% \edef\DateOfTeXing{\DateOfTeXing\TimeOfTeXing}
\count@=\time   \divide\count@ by 60
\edef\@temp{\zwpl@num\count@}%
\multiply\count@ by -60         \advance\count@ by \time
\edef\TimeOfTeXing{\@temp\zwpl@num\count@}%

% Default version of the modification date
\def\zwpl@modt{(D:\DateOfTeXing\TimeOfTeXing)}

% PDF info + PDF/X-1a
\def\SetPDFminorversion#1{\ZWifdriver{pdftex}{\pdfminorversion#1 \relax}}
\@onlypreamble\SetPDFminorversion

\ifzwpl@pdfx
  \ZWsetkeys{zwpl}{pdfminorversion=3}
  \let\SetPDFminorversion\@gobble
\fi

\ifcat$\zwpl@pdfminorversion$ \else \ZWifdriver{pdftex}{\pdfminorversion3 \relax}\fi
\def\zwpl@pdfxinfo{}
\ifzwpl@pdfx
  \def\zwpl@pdfxinfo{ /GTS_PDFXVersion (PDF/X-1:2001) /GTS_PDFXConformance (PDFX-1a:2001)}
  \ifcat$\zwpl@ICCfile$ \else
    \ifcase\zwpl@Driver
    \or
      \immediate\pdfobj stream attr{/N 4} file{\zwpl@ICCfile}
      \edef\zwpl@iccobj{\the\pdflastobj}
    \fi
  \fi
\fi

\edef\zwpl@pdftitle{\zwpl@title}
\ifcat$\zwpl@pdftitle$
  \ifzwpl@pdfx
    \ifcase\zwpl@Driver
    \or
      \pdfinfo{\zwpl@pdfinfo}
    \or
      \ifxetex \def\zwpl@crt{XeTeX}\else \def\zwpl@crt{TeX+dvipdfm}\fi
      \edef\zwpl@pdfcrt{ /Creator (\zwpl@crt) }
      \special{pdf:docinfo << \zwpl@pdfmoddate \zwpl@pdfcrt  \zwpl@pdfxinfo >>}
    \fi
  \fi
\else
  \edef\zwpl@pdftitle{/Title (\zwpl@pdftitle)}
  \ifcat$\zwpl@author$\def\zwpl@pdfauthor{}\else
    \edef\zwpl@pdfauthor{ /Author (\zwpl@author)}\fi
  \ifcat$\zwpl@subject$\def\zwpl@pdfsubject{}\else
    \edef\zwpl@pdfsubject{ /Subject (\zwpl@subject)}\fi
  \ifcat$\zwpl@keywords$\def\zwpl@pdfkeywords{}\else
    \edef\zwpl@pdfkeywords{ /Keywords (\zwpl@keywords)}\fi
  \edef\zwpl@pdfcreationdate{ /CreationDate \zwpl@modt}
  \edef\zwpl@pdfmoddate{ /ModDate \zwpl@modt}
  \ifcase\zwpl@Driver
  \or
    \pdfinfo{\zwpl@pdftitle \zwpl@pdfauthor \zwpl@pdfsubject \zwpl@pdfkeywords \zwpl@pdfxinfo}
  \or
    \ifxetex \def\zwpl@crt{XeTeX}\else \def\zwpl@crt{TeX+dvipdfm}\fi
    \edef\zwpl@pdfcrt{ /Creator (\zwpl@crt) }
    \special{pdf:docinfo << \zwpl@pdftitle \zwpl@pdfauthor \zwpl@pdfsubject \zwpl@pdfkeywords
                            \zwpl@pdfmoddate \zwpl@pdfcrt  \zwpl@pdfxinfo >>}
  \else
    \def\zwpl@pdfcrt{ /Creator (TeX+dvips) }
    \special{ps:[ \zwpl@pdftitle \zwpl@pdfauthor \zwpl@pdfsubject \zwpl@pdfkeywords
                  \zwpl@pdfcreationdate \zwpl@pdfmoddate \zwpl@pdfcrt /DOCINFO pdfmark}
  \fi
\fi

% Catalog entry for PDF/X
\ifzwpl@pdfx
  \ifx\zwpl@iccobj\undefined
    \def\zwpl@profile{}
  \else
    \def\zwpl@profile{ /DestOutputProfile \zwpl@iccobj\space 0 R}
  \fi
  \def\zwpl@catalog{/OutputIntents [ <<
    /Type/OutputIntent
    /S/GTS_PDFX
    /OutputCondition (\zwpl@OutputCondition)
    /OutputConditionIdentifier (\zwpl@OutputConditionIdentifier)
    /RegistryName (http://www/color.org)\zwpl@profile
  >> ]}
  \ifcase\zwpl@Driver
  \or
    \pdfcatalog{\zwpl@catalog}
  \or
    \special{pdf: put @catalog <<\zwpl@catalog>>}
  \fi
\fi

% Page margins
\ifcat$\zwpl@margins$ \else
  \ZWsetkeys{zwpl}{topmargin=\zwpl@margins,botmargin,
                 leftmargin=\zwpl@margins,rightmargin=\zwpl@margins}
\fi
\topmargin\zwpl@topmargin
\oddsidemargin\zwpl@leftmargin
\evensidemargin\zwpl@rightmargin
\textwidth\zwpl@textwidth
\textheight\zwpl@textheight
\headheight\zwpl@headheight
\headsep\zwpl@headsep
\footskip\zwpl@footskip
\ifcat$\zwpl@topskip$\else
  \topskip\zwpl@topskip
\fi

\ifcat$\zwpl@trim$ \else
  \ZWsetkeys{zwpl}{xtrim=\zwpl@trim,ytrim=\zwpl@trim}
\fi

% Paper width
\ifdim\paperwidth>\z@

\ifdim\textwidth>\z@
  \ifdim\oddsidemargin<\z@\else
  \ifdim\evensidemargin<\z@\else
    \textwidth -1in
\fi \fi \fi

\ifdim\textwidth>\z@
  \ifdim\oddsidemargin<\z@
    \ifdim\evensidemargin<\z@
      \oddsidemargin\paperwidth
      \advance\oddsidemargin-\textwidth
      \divide\oddsidemargin 2
      \evensidemargin\oddsidemargin
    \else
      \oddsidemargin\paperwidth
      \advance\oddsidemargin -\textwidth
      \advance\oddsidemargin -\evensidemargin
    \fi
  \else
    \evensidemargin\paperwidth
    \advance\evensidemargin -\textwidth
    \advance\evensidemargin -\oddsidemargin
  \fi
\else
  \ifdim\oddsidemargin<\z@
    \ifdim\evensidemargin<\z@
      \oddsidemargin\topmargin
      \evensidemargin\topmargin
    \else
      \oddsidemargin\evensidemargin
    \fi
  \else
    \ifdim\evensidemargin<\z@
      \evensidemargin\oddsidemargin
    \fi
  \fi
  \textwidth\paperwidth
  \advance\textwidth -\oddsidemargin
  \advance\textwidth -\evensidemargin
\fi

\else % calculate \paperwidth
  % \textwidth is the page body text without a spine, xspine, flaps, trim
  \ifcat$\zwpl@flap$\else \advance\textwidth\zwpl@flap \fi
  \ifcat$\zwpl@spine$\else
    % xspine ignored without spine
    \ifcat$\zwpl@xspine$\else
      \advance\textwidth\zwpl@xspine
    \fi
    \multiply\textwidth 2
    \advance\textwidth\zwpl@spine
  \fi
  \ifcat$\zwpl@xtrim$\else
    \@tempdima\zwpl@xtrim
    \advance\textwidth 2\@tempdima
  \fi
  \ifdim\oddsidemargin<\z@
    \ifdim\evensidemargin<\z@
      \oddsidemargin\topmargin
      \evensidemargin\topmargin
    \else
      \oddsidemargin\evensidemargin
    \fi
  \else
    \ifdim\evensidemargin<\z@
      \evensidemargin\oddsidemargin
    \fi
  \fi
  \paperwidth\textwidth
  \advance\paperwidth\oddsidemargin
  \advance\paperwidth\evensidemargin
\fi

% Paper height
\ifdim\paperheight>\z@

\ifdim\textheight>\z@ \else
  \ifcat$\zwpl@botmargin$
    \ZWsetkeys{zwpl}{botmargin}
  \fi
\fi

\ifcat$\zwpl@botmargin$\else
  \textheight\paperheight
  \advance\textheight -\topmargin
  \advance\textheight -\zwpl@botmargin
\fi

\else % calculate \paperheight
  \paperheight\topmargin
  \ifcat$\zwpl@ytrim$\else
    % add 2*ytrim to textheight
    \@tempdima\zwpl@ytrim
    \advance\textheight 2\@tempdima
  \fi
  \advance\paperheight\textheight
  \ifcat$\zwpl@botmargin$ \else
    \advance\paperheight\zwpl@botmargin
  \fi
\fi

\small\normalsize

\parskip\z@

% Set \textheight
\advance\textheight -\footskip
\advance\textheight -\headheight
\advance\textheight -\headsep
\ifzwpl@strictheight\else
  \ifzwpl@adjustfootskip
    \advance\footskip \textheight
  \else
    \advance\headsep \textheight
  \fi
  \advance\textheight -\topskip
  \divide\textheight by \baselineskip
  \count@ \textheight
  \textheight \count@\baselineskip
  \advance\textheight \topskip
  \ifzwpl@adjustfootskip
    \advance\footskip -\textheight
  \else
    \advance\headsep -\textheight
  \fi
\fi
\advance\topskip 100sp minus 500sp 

\advance\topmargin -1in
\advance\oddsidemargin -1in
\advance\evensidemargin -1in


% \vskip multiple baselines hacked for emTeX

\DeclareRobustCommand\vb{\begingroup \catcode`\-12 \catcode`\.12 \ZW@vb}
\newcommand*\ZW@vb[1][1]{\endgroup\vspace{#1\ZW@baselineskip}}

\newskip\ZW@baselineskip
\def\set@ZW@baselineskip{\ZW@baselineskip \baselineskip
  \advance\ZW@baselineskip 10sp minus 50sp }

\AtBeginDocument{\set@ZW@baselineskip}

% Correction \vskip

\newcommand*\Vcorr{\vskip 10sp minus 50sp}

% New odd/even page leaving an empty page of a specific style

\def\NewOddPage{\@ifstar{\ZW@s@oddpage}{\ZW@oddpage}}
\def\ZW@s@oddpage{\def\ZW@maybewarning{\PackageWarningNoLine{zwpagelayout}}\ZW@oddpage}
\newcommand*\ZW@oddpage[1][empty]{\clearpage
  \ifodd\c@page \else \thispagestyle{#1}\null\ZW@maybewarning{\ZW@oddwarning}\clearpage
    \let\ZW@maybewarning\@gobble
  \fi}

\def\NewEvenPage{\@ifstar{\ZW@s@evenpage}{\ZW@evenpage}}
\def\ZW@s@evenpage{\def\ZW@maybewarning{\PackageWarningNoLine{zwpagelayout}}\ZW@evenpage}
\newcommand*\ZW@evenpage[1][empty]{\clearpage
  \ifodd\c@page \thispagestyle{#1}\null\ZW@maybewarning{\ZW@evenwarning}\clearpage
    \let\ZW@maybewarning\@gobble
  \fi}

\let\ZW@maybewarning\@gobble
\def\SetOddPageMessage#{\gdef\ZW@oddwarning}
\def\SetEvenPageMessage#{\gdef\Z@@evenwarning}
\def\ZW@oddwarning{Empty page inserted}\let\ZW@evenwarning\ZW@oddwarning

% End of page layout setting

\fi % \ifzwpl@onlycropmarks ... \else ... \fi


% User defined dimensions

\def\CropFlap{\zwpl@flap}
\def\CropSpine{\zwpl@spine}
\def\CropXSpine{\zwpl@xspine}
\def\CropXtrim{\zwpl@xtrim}
\def\CropYtrim{\zwpl@ytrim}
\def\UserWidth{\zwpl@textwidth}
\def\UserLeftMargin{\zwpl@leftmargin}
\def\UserRightMargin{\zwpl@rightmargin}
\def\UserTopMargin{\zwpl@topmargin}
\def\UserBotMargin{\zwpl@botmargin}

% Handling color, needed in several cases
\ifzwpl@redefinetocmyk \zwpl@redefineblacktrue \fi
\def\zwpl@next{}
\def\zwpl@reqclr{\def\zwpl@next{\RequirePackage{color}}}
\expandafter\ifx\csname definecolor\endcsname\relax
  \ifzwpl@color \zwpl@reqclr \fi
  \ifzwpl@cropmarks \ifzwpl@nobleedclip \else \zwpl@reqclr \fi \fi
  \ifzwpl@redefineblack \zwpl@reqclr \fi
  \ifzwpl@overprint \zwpl@reqclr \fi
\fi
\zwpl@next
\expandafter\ifx\csname definecolor\endcsname\relax \else
  \definecolor{cmykblack}{cmyk}{0,0,0,1}
  \definecolor{grblack}{gray}{0}
  \ifzwpl@redefineblack
    \definecolor{black}{cmyk}{0,0,0,1}\color{black}
  \fi
  \definecolor{cmykred}{cmyk}{0,1,1,0}
  \definecolor{cmykgreen}{cmyk}{1,0,1,0}
  \definecolor{cmykblue}{cmyk}{1,1,0,0}
  \definecolor{rgbred}{rgb}{1,0,0}
  \definecolor{rgbgreen}{rgb}{0,1,0}
  \definecolor{rgbblue}{rgb}{0,0,1}
  \ifzwpl@redefinetocmyk
    \definecolor{red}{cmyk}{0,1,1,0}
    \definecolor{green}{cmyk}{1,0,1,0}
    \definecolor{blue}{cmyk}{1,1,0,0}
  \fi
\fi

% Black overprint user macros
\DeclareRobustCommand\SetOverprint{\zwpl@optrue \zwpl@setop \ignorespaces}
\DeclareRobustCommand\SetKnockout{\zwpl@opfalse \zwpl@setop \ignorespaces}
\DeclareRobustCommand\textoverprint[1]{{\SetOverprint#1}}
\DeclareRobustCommand\textknockout[1]{{\SetKnockout#1}}

% Black overprint implementation
\let\zwpl@setop\relax
\let\zwpl@resetop\relax
\newif\ifzwpl@op

\ifzwpl@overprint
\ifcase\zwpl@Driver
\or % pdftex
  \AtBeginDocument{%
    \immediate\pdfobj{<< /Type /ExtGState /op false /OP false /OPM 0 >>}
    \edef\op@off{\the\pdflastobj}
    \immediate\pdfobj{<< /Type /ExtGState /op true /OP true /OPM 1 >>}
    \edef\op@on{\the\pdflastobj}
    \immediate\pdfobj{ << %
      /GSko \op@off\space 0 R %
      /GSop \op@on\space 0 R >>}
    \edef\@extgs{\the\pdflastobj}
    \edef\next{\global\noexpand\pdfpageresources={\the\pdfpageresources
      \space /ExtGState \@extgs\space 0 R }}
    \next
  }
  % macros for setting and resetting the overprint state
  \def\zwpl@setop{\zwpl@resetop
    \aftergroup\zwpl@resetop}
  \def\zwpl@resetop{%
    \ifzwpl@op
      \pdfliteral direct {/GSop gs}%
    \else
      \pdfliteral direct {/GSko gs}%
    \fi
  }
\or % xetex
  \iffalse % disabled because it does not work
  \special{pdf: object @opoff << /Type /ExtGState /op false /OP false /OPM 0 >>}
  \special{pdf: object @opon << /Type /ExtGState /op true /OP true /OPM 1 >>}
  \special{pdf: object @extgs << /GSko @opoff /GSop @opon >>}
  %\special{pdf: object @ExtGS << /ExtGState @extgs >>}
  %\def\ZWAddResources{\special{pdf: put @thispage << /Resources @ExtGS >>}}
  %\def\ZWAddResources{\special{pdf: put @resources << /Resources @ExtGS >>}}
  \def\ZWAddResources{\special{pdf: put @resources << /ExtGState @extgs >>}}
  % macros for setting and resetting the overprint state
  \def\zwpl@setop{\zwpl@resetop
    \aftergroup\zwpl@resetop}
  \def\zwpl@resetop{%
    \ifzwpl@op
      \special{pdf: content /GSop gs}%
    \else
      \special{pdf: content /GSko gs}%
    \fi
    }
  \else
    \def\zwpl@setop{%
        \PackageWarningNoLine{zwpagelayout}{Overprint not yet supported in (x)dvipdfm(x)}}
    \let\zwpl@resetop\zwpl@setop
  \fi % iffalse
\else % dvips
  \def\zwpl@setop{\zwpl@resetop
    \aftergroup\zwpl@resetop}
  \def\zwpl@resetop{%
    \ifzwpl@op
      \special{ps: true setoverprint}%
    \else
      \special{ps: false setoverprint}%
    \fi
  }
\fi
\SetKnockout
\else
  \def\zwpl@setop{\global\let\zwpl@setop\relax
      \PackageWarningNoLine{zwpagelayout}{Use the "overprint" option to enable overprint.}}
\fi



% Handle cropmarks
\newdimen\zwpl@low \zwpl@low 1in
\advance\zwpl@low\topmargin
\advance\zwpl@low \headheight
\advance\zwpl@low \headsep
\advance\zwpl@low \textheight
\advance\zwpl@low \footskip
\advance\zwpl@low -\paperheight

\ifzwpl@cropmarks
  \AtBeginDocument{\expandafter\ifx\csname cropmarkstyle@\zwpl@cropmarkstyle\endcsname\relax
    \PackageWarningNoLine{zwpagelayout}{Unimplemented cropmarkstyle=\zwpl@cropmarkstyle}
  \fi \csname cropmarkstyle@\zwpl@cropmarkstyle @setup\endcsname}
  \let\zwpl@pagestyle\pagestyle
  \let\zwpl@thispagestyle\thispagestyle
  \def\pagestyle{\let\zwpl@next\zwpl@pagestyle \zwpl@testps}
  \def\thispagestyle{\let\zwpl@next\zwpl@thispagestyle \zwpl@testps}
\fi

\def\zwpl@testps#1{\xdef\zw@pstyle{ZW\ZWpercent#1\ZWpercent}%
    \@ifundefined{ps@\zw@pstyle}{\zwpl@patchps{#1}{\zw@pstyle}}{}\zwpl@next{\zw@pstyle}}

\def\zwpl@patchps#1#2{\expandafter\gdef\csname ps@#2\endcsname{\csname ps@#1\endcsname \zwpl@patchfoot}}

\def\zwpl@patchfoot{\let\zwpl@oddfoot\@oddfoot \let\zwpl@evenfoot\@evenfoot
  \def\@oddfoot{\zwpl@cropbox\zwpl@oddfoot}%
  \def\@evenfoot{\zwpl@cropbox\zwpl@evenfoot}}

% We must use the knockout mode in the cropmarks (added 2010-12-21)
\def\zwpl@cropbox{{\globaldefs=0\relax \ifzwpl@overprint\SetKnockout\fi
  \rlap{% Some parameters may be changed within the document
  \raisebox{\zwpl@low}[\z@][\z@]{\everyvbox{}\offinterlineskip\boxmaxdepth\z@
    \hskip -\hoffset \hskip -1in \hskip -
    \if@twoside \ifodd\c@page \oddsidemargin \else \evensidemargin \fi \else \oddsidemargin \fi
    \vbox to \paperheight{\hsize\paperwidth \parindent\z@
       \ifzwpl@nobleedclip\else
         \begingroup \vbox to \z@{\color{white}\noindent
           \rlap{\vbox to \paperheight{\hsize \paperwidth
                 \hrule width \hsize height \zwpl@croplength \vfill
                 \hrule width \hsize height \zwpl@croplength}}%
           \rlap{\vbox to \paperheight{\hsize\paperwidth
                 \vrule width \zwpl@croplength height \paperheight
                 \hfill \vrule width \zwpl@croplength}}%
         \vss}%
         \endgroup
       \fi
       \ifzwpl@color
         \edef\zwpl@temp{\noexpand\color[\zwpl@colormodel]{\zwpl@cropcolor}}\zwpl@temp
       \fi
       %%%\zwpl@cropfont \zwpl@cropfontsize \selectfont
       \normalfont \fontsize{10}{10}\selectfont
       \csname cropmarkstyle@\zwpl@cropmarkstyle\endcsname
    }%
}}}}

\ifzwpl@cropmarks \zwpl@patchfoot \fi

% General definitions for the crop marks
\newdimen\zwpl@len  \zwpl@len\zwpl@croplength
\newdimen\zwpl@gap  \zwpl@gap\zwpl@cropgap
\newdimen\zwpl@plus \newdimen\zwpl@minus
\zwpl@plus \zwpl@croplength
\zwpl@plus .5\zwpl@plus
\zwpl@minus -\zwpl@plus
\advance\zwpl@plus .2pt
\advance\zwpl@minus .2pt

\newdimen\zwpl@frame \zwpl@frame\paperheight

\ifzwpl@cropmarks
  \hoffset \zwpl@croplength
  \advance\hoffset \zwpl@gap
  \voffset\hoffset
  \advance\paperheight 2\voffset
  \advance\paperwidth 2\hoffset
\fi
\advance\zwpl@low -\voffset

\newdimen\zwpl@vsize
\zwpl@vsize\paperheight
\advance\zwpl@vsize -2\zwpl@croplength

\def\clap#1{\hbox to \z@{\hss #1\hss}}
\def\zwpl@cbox#1{\rlap{\hbox to \zwpl@croplength{\hss #1\hss}}}

\def\zwpl@vert{\vrule height \zwpl@croplength depth \z@ width .4pt }
\def\zwpl@horiz{\vrule height .2pt depth .2pt width \zwpl@croplength}
\def\zwpl@choriz{\vrule height \zwpl@plus depth \zwpl@minus width \zwpl@croplength}
\def\zwpl@cross{\zwpl@cbox{\zwpl@vert}\zwpl@choriz}
\def\zwpl@frm{\clap{\vrule height \zwpl@frame depth \z@ width .4pt}}
\def\zwpl@hh{\hbox to \hsize}
\def\zwpl@hline{\zwpl@hh{\zwpl@horiz \hskip\zwpl@gap
    \ifzwpl@cropframe
      \leaders \vrule height .2pt depth .2pt \hfill
    \else
      \hfill
    \fi
    \hskip\zwpl@gap \zwpl@horiz}}

\def\zwpl@cvert{\clap{\zwpl@vert}}

% Vertical crop lines for the default style
\newcommand*\zwpl@vline[1][]{%
  \zwpl@hh{\zwpl@cross \hskip\zwpl@gap \clap{\zwpl@vert}%
    \ifcat$\zwpl@xtrim$\else
      \hskip\zwpl@xtrim \clap{\zwpl@vert}%
    \fi
    \ifcat$\zwpl@flap$\else
      \hskip\zwpl@flap \clap{\zwpl@vert}%
    \fi
    \rlap{\hskip\zwpl@gap #1}%
    \hss
    \ifcat$\zwpl@spine$\else
      \ifcat$\zwpl@xspine$\else
        \clap{\zwpl@vert}\hskip\zwpl@xspine
      \fi
      \clap{\zwpl@vert}\hskip\zwpl@spine \clap{\zwpl@vert}%
      \ifcat$\zwpl@xspine$\else
        \hskip\zwpl@xspine \clap{\zwpl@vert}%
      \fi
      \hss
      \ifcat$\zwpl@flap$\else
        \clap{\zwpl@vert}\hskip\zwpl@flap
      \fi
    \fi
    \ifcat$\zwpl@xtrim$\else
      \clap{\zwpl@vert}\hskip\zwpl@xtrim
    \fi
    \clap{\zwpl@vert}\hskip\zwpl@gap \zwpl@cross}}

% Default crop mark style
\def\cropmarkstyle@default{%
  \expandafter\zwpl@vline\ifzwpl@color[\zwpl@showallcolors]\fi
  \vskip\zwpl@gap \zwpl@hline
  \ifcat$\zwpl@ytrim$\else
    \vskip\zwpl@ytrim \zwpl@hline
  \fi
  \vss
  \ifzwpl@cropframe
    \zwpl@hh{\hskip \hoffset \zwpl@frm
    \ifcat$\zwpl@xtrim$\else
      \hskip\zwpl@xtrim \zwpl@frm
    \fi
    \ifcat$\zwpl@flap$\else
      \hskip\zwpl@flap \zwpl@frm
    \fi
    \hss
    \ifcat$\zwpl@spine$\else
      \ifcat$\zwpl@xspine$\else
        \zwpl@frm \hskip\zwpl@xspine
      \fi
      \zwpl@frm \hskip\zwpl@spine \zwpl@frm
      \ifcat$\zwpl@xspine$\else
        \hskip\zwpl@xspine \zwpl@frm
      \fi
      \hss 
      \ifcat$\zwpl@flap$\else
        \zwpl@frm \hskip\zwpl@flap
      \fi
    \fi
    \ifcat$\zwpl@xtrim$\else
      \zwpl@frm \hskip\zwpl@xtrim
    \fi
    \zwpl@frm \hskip \hoffset}\vss
  \fi
  \ifcat$\zwpl@ytrim$\else
    \zwpl@hline \vskip\zwpl@ytrim
  \fi
  \zwpl@hline \vskip\zwpl@gap \zwpl@vline[\zwpl@printcroptitle]}

% Leaflet style
\def\zwpl@fold@use#1{\csname zwpl@fold@#1@leaf\endcsname}
\def\zwpl@fold@def#{\expandafter\expandafter\expandafter\def\zwpl@fold@use}

% We can do this because cropmark dimensions are set within a group
\def\zwpl@switchskips{\let\zwpl@temp\zwpl@leftskip
                      \let\zwpl@leftskip\zwpl@rightskip
                      \let\zwpl@rightskip\zwpl@temp}

\newif\ifzwpl@middle
\def\zwpl@nofoldcorr{\def\zwpl@foldcorr{0mm}}

% Leaflet initialization
\zwpl@fold@def{2}{\def\zwpl@leafcount{2}%
  \def\zwpl@leftskip{\hskip\zwpl@foldcorr}%
  \def\zwpl@rightskip{}}
\zwpl@fold@def{3left}{\def\zwpl@leafcount{3}%
  \def\zwpl@leftskip{\hskip\zwpl@foldcorr}%
  \def\zwpl@rightskip{}}
\zwpl@fold@def{3right}{\zwpl@fold@use{3left}\zwpl@switchskips}
\zwpl@fold@def{Z}{\def\zwpl@leftskip{}\let\zwpl@rightskip\zwpl@leftskip\zwpl@nofoldcorr}
\zwpl@fold@def{4}{\zwpl@fold@use{Z}\def\zwpl@leafcount{4}\zwpl@middletrue}

\def\cropmarkstyle@leaflet@setup{%
  \expandafter\expandafter\expandafter\ifx\zwpl@fold@use{\zwpl@fold}\relax
    \PackageWarningNoLine{zwpagelayout}{Unimplemented leaflet fold=\zwpl@fold.\MessageBreak
                                        Leaflet crop marks will not be printed.}
    \def\zwpl@cropmarkstyle{\ZWpercent}
  \else
    \zwpl@fold@use{\zwpl@fold}%
    % Test the variables by assignments
    \@tempskipa=\zwpl@foldcorr\relax
    \count@=\zwpl@leafcount\relax
  \fi
}

% vertical crop lines for a leaflet
\newcommand*\zwpl@vleaf[1][]{%
  \zwpl@hh{\zwpl@cross \hskip\zwpl@gap \zwpl@cvert
    \rlap{\hskip\zwpl@gap #1}\zwpl@leftskip
    \count@=1
    \loop
      \hfil
      \ifzwpl@middle
        \ifnum\count@>1
          \zwpl@middlefalse
        \else
          \zwpl@cvert
        \fi
      \else
        \zwpl@cvert
      \fi
      \advance\count@ 1
    \ifnum\count@<\zwpl@leafcount \repeat
    \hfil \zwpl@rightskip
    \zwpl@cvert\hskip\zwpl@gap \zwpl@cross}}

% Crop marks for a leaflet
\def\cropmarkstyle@leaflet{%
  \if@twoside \ifodd\c@page \else \zwpl@switchskips \fi \fi
  \expandafter\zwpl@vleaf\ifzwpl@color[\zwpl@showallcolors]\fi
  \vskip\zwpl@gap \zwpl@hline
  \vss
  \ifzwpl@cropframe
    \zwpl@hh{\hskip \hoffset \zwpl@frm \zwpl@leftskip
    \count@=1
    \loop
      \hfil \zwpl@frm
      \advance\count@ 1
    \ifnum\count@<\zwpl@leafcount \repeat
    \hfil \zwpl@rightskip
    \zwpl@frm \hskip \hoffset}\vss
  \fi
  \zwpl@hline \vskip\zwpl@gap \zwpl@vleaf[\zwpl@printcroptitle]}

% Print the crop title
\def\zwpl@printcroptitle{\raisebox{.8ex}[\z@][\z@]{\ifzwpl@usepagenumbers
  \ifzwpl@pagenumberfirst
    \thePageNumber 
    \ifcat$\zwpl@croptitle$\else
      \zwpl@cropseparator \zwpl@croptitle
    \fi
  \else
    \ifcat$\zwpl@croptitle$\else
      \zwpl@croptitle \zwpl@cropseparator
    \fi
    \thePageNumber
  \fi
\else
  \ifcat$zwpl@croptitle$\else
    \zwpl@croptitle
  \fi
\fi}}

\def\thePageNumber{\#\,\arabic{page}}

\def\zwpl@printcolor#1:#2:{\edef\zwpl@temp{\noexpand\textcolor[\zwpl@colormodel]{#2}{#1}}\zwpl@temp\space}

\def\zwpl@showcolors#1,#2*{\zwpl@printcolor#1:%
    \ifcat$#2$\let\zwpl@next\relax
    \else\def\zwpl@next{\zwpl@showcolors#2*}\fi \zwpl@next}

\def\zwpl@showallcolors{\raisebox{.2ex}[\z@][\z@]{\expandafter\zwpl@showcolors\zwpl@colors,*}}

% Reflect (should be at the end of the package)

\newcount\zwpl@Hshift
\newcount\zwpl@Vshift

% shift calculation
\def\zwpl@calcshift#1#2{#1#2
    \divide #1 8
    \multiply #1 5
    \divide #1 8
    \multiply #1 5
    \divide #1 25696 }

% pdftex version
\def\zwpl@Hreflect{\zwpl@calcshift\zwpl@Hshift\hsize
    \pdfliteral{-1 0 0 1 \the\zwpl@Hshift\space 0 cm}}

\def\zwpl@Vreflect{\@tempdima\vsize
    \advance\@tempdima-\topmargin
    \zwpl@calcshift\zwpl@Vshift\@tempdima
    \pdfliteral{1 0 0 -1 0 -\the\zwpl@Vshift\space cm}}

% (x)dvipdfm(x) version, modifies bop and eop
\def\zwpl@XHreflect{\zwpl@calcshift\zwpl@Hshift\paperwidth
    \special{pdf: bop q -1 0 0 1 \the\zwpl@Hshift\space 0 cm}\special{pdf: eop Q}}

\def\zwpl@XVreflect{\zwpl@calcshift\zwpl@Vshift\paperheight
    \special{pdf: bop q 1 0 0 -1 0 \the\zwpl@Vshift\space cm}\space{pdf: eop Q}}

% dvips version (modifies bop-hook)
\def\zwpl@psreflect#1#2#3#4{\zwpl@calcshift#3#4
    \special{! userdict begin
               userdict /bop-hook known {/bop-hook load cvx /ZW#1-hook exch def} if
               /bop-hook {
                   [#2] concat
                   userdict /ZW#1-hook known {ZW#1-hook} if
               } def end}}

\def\zwpl@psHreflect{\zwpl@psreflect H{-1 0 0 1 \the\zwpl@Hshift\space 0}\zwpl@Hshift\paperwidth}
\def\zwpl@psVreflect{\zwpl@psreflect V{1 0 0 -1 0 \the\zwpl@Vshift}\zwpl@Vshift\paperheight}
