summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/gentombow/pxesopic.sty
blob: 567b9d0a2d246dda6902cdbfdd90ba874018edb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
%
% pxesopic.sty
% written by Hironobu Yamashita (@aminophen)
%
% This package is part of the gentombow bundle.
% https://github.com/aminophen/gentombow
%
% This package is expected to be compatible with
%   * eso-pic.sty
%     2015/07/21 v2.0g -- 2020/09/27 v2.0i
%
% --------------------------------------------------------------
% [Note]
% The package ``pxesopic.sty'' relies heavily on internals
% of ``plautopatch.sty''.
% Also, ``pxesopic.sty'' is incompatible with ``memoir.cls''.
% --------------------------------------------------------------
%

%% package declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pxesopic}
    [2020/10/11 v0.4c Patch to eso-pic for tombow]

%% preparations
\def\pxesop@pkgname{pxesopic}
\def\pxesop@error{\PackageError\pxesop@pkgname}
\def\pxesop@warn{\PackageWarningNoLine\pxesop@pkgname}
\let\pxesop@debug\@gobble
%\def\pxesop@debug#1{\typeout{\pxesop@pkgname: [DEBUG] #1}}% for debug

\@ifclassloaded{memoir}{%
  \pxesop@warn{This package should not be loaded with memoir.cls}}{}

\@ifpackageloaded{eso-pic}{%
  \pxesop@error{%
    Package eso-pic is already loaded!\MessageBreak
    Load pxesopic earlier}\@ehc
}{}

%% hooks
\ifx\declare@file@substitution\@undefined
  % LaTeX2e 2020-02-02 PL5 or older
  \RequirePackage{filehook}
  \let\pxesop@ExecuteAfterPackage=\AtEndOfPackageFile
  \let\pxesop@ExecuteBeforePackage=\AtBeginOfPackageFile
\else
  % LaTeX2e 2020-10-01 or newer
  \def\pxesop@ExecuteAfterPackage#1{\AddToHook{package/after/#1}}
  \def\pxesop@ExecuteBeforePackage#1{\AddToHook{package/before/#1}}
\fi

%% the contents of \AtBeginShipout is dependent on \iftombow;
%% delay execution of \AtBeginShipout inside ``eso-pic.sty''
%% until \AtBeginDocument, to support both (u)pLaTeX kernel
%% and ``gentombow.sty''
\RequirePackage{atbegshi}% eso-pic requires atbegshi
\ifx\pfmtname\@undefined\else
  \ifx\AtBegShi@Output\@undefined
    % LaTeX2e 2020-10-01: an emulation by ``atbegshi-ltx.sty''
    % => pLaTeX2e 2020-10-01 natively supports it
  \else
    % older: the real ``atbegshi.sty'' is loaded
    \IfFileExists{pxatbegshi.sty}{\RequirePackage{pxatbegshi}}{}
  \fi
\fi
%
\pxesop@ExecuteBeforePackage{eso-pic}{\pxesop@before}
\pxesop@ExecuteAfterPackage{eso-pic}{\pxesop@after}
%
\def\pxesop@before{%
  \let\pxesop@AtBeginShipout\AtBeginShipout
  \def\AtBeginShipout##1{\def\pxesop@hook{##1}}% hide it
  \pxesop@debug{\noexpand\AtBeginShipout disabled}%
  \let\pxesop@before\relax
}
\def\pxesop@after{%
  \let\AtBeginShipout\pxesop@AtBeginShipout    % restore
  \let\pxesop@AtBeginShipout\relax
  \pxesop@debug{\noexpand\AtBeginShipout restored}%
  \let\@unknownoptionerror\relax %% avoid an error
  \pxesop@debug{\noexpand\@unknownoptionerror disabled}%
  \let\pxesop@after\relax
}
%%

%% load it; be careful not to cause unknown option error!
\expandafter\ifx\csname @unknownoptionerror\endcsname
  \pxesop@error{Failed to save \noexpand\@unknownoptionerror}\@ehc
\fi
\let\pxesop@unknownoptionerror\@unknownoptionerror
%
\chardef\pxesop@status\z@
\@ifpackageloaded{plautopatch}{%
  \expandafter\ifx\csname platpc@begin@eso-pic\endcsname\relax
    % ``plautopatch.sty'' is loaded but the macro is \relax
    % => means that ``pxesopic.sty'' is automatically loaded
    \@ifl@t@r{\fmtversion}{2020/10/01}{%
      \chardef\pxesop@status\@ne
    }{}%
  \fi
}{}
%
%% pass all options to keyval-style ``eso-pic.sty''
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{eso-pic}}
\ProcessOptions\relax
\RequirePackage{eso-pic}
%
%% ensure internal dimensions added in ``eso-pic.sty'' v2.0i
\ifx\ESO@tempdima\@undefined \newdimen\ESO@tempdima \fi
\ifx\ESO@tempdimb\@undefined \newdimen\ESO@tempdimb \fi
%
%% recover the error
\ifnum\pxesop@status>\z@ % auto
  \def\pxesop@pkgtohook{eso-pic}%
  % note that ``plautopatch.sty'' discards the hook
  % on LaTeX2e 2020-02-02 PL5 or older
  \ifx\declare@file@substitution\@undefined
    \let\pxesop@pkgtohook\pxesop@pkgname
    % throw an error for unsupported usage
    % [FIXME] the error below is actually unnecessary when
    % none of ``eso-pic.sty'' options is used; how to detect that?
    \pxesop@error{Try loading \pxesop@pkgname\space by hand}\@ehc
  \fi
\else
  \let\pxesop@pkgtohook\pxesop@pkgname
\fi
\pxesop@ExecuteAfterPackage\pxesop@pkgtohook{%
  \let\@unknownoptionerror\pxesop@unknownoptionerror
  \pxesop@debug{\noexpand\@unknownoptionerror restored}%
}
%%

%% do it
%-- similar to bounddvi.sty (checked 2020/10/08 v8.2)
% detection of \iftombow availability is delayed until \AtBeginDocument,
% to support both (u)pLaTeX kernel and ``gentombow.sty''
% Note: this package does not consider changing \iftombow status
% in the middle of a single document.
\AtBeginDocument{\pxesop@do}
\def\pxesop@do{%
  \ifx\tombowtrue\@undefined
    \pxesop@debug{Tombow feature unavailable, no patch applied}%
    \let\pxesop@switch\pxesop@orig % skip if-tokens and restore
  \fi
  \pxesop@switch}
%
\def\pxesop@switch{%
  \iftombow \pxesop@ensure@stock \pxesop@new
  \else \pxesop@orig \fi}
%
\def\pxesop@ensure@stock{%
  %\iftombow
    % if \stockwidth/\stockheight are not available,
    % define them now
    \ifx\stockwidth\@undefined \newdimen\stockwidth \fi
    \ifx\stockheight\@undefined \newdimen\stockheight \fi
    % if \stockwidth already has effective value, do nothing;
    % otherwise assume pLaTeX-style tombow and set it to
    % \paperwidth + 2in (similar for \stockheight)
    \ifdim\stockwidth>\z@\else
      \stockwidth\paperwidth \advance\stockwidth2in
    \fi
    \ifdim\stockheight>\z@\else
      \stockheight\paperheight \advance\stockheight2in
    \fi
  %\fi
}
%--
%
%% for \iftombow is FALSE => restore from ``eso-pic.sty''
\expandafter\def\expandafter\pxesop@orig\expandafter{%
  \expandafter\AtBeginShipout\expandafter{\pxesop@hook}}
%
%% for \iftombow is TRUE => redefine by ``pxesopic.sty''
\def\pxesop@new{%
  %% \AtStockLowerLeft: based on TRUE of \ESO@isMEMOIR
  \renewcommand\AtStockLowerLeft[1]{%
    \begingroup
      \ESO@tempdima=-\stockwidth
      \advance\ESO@tempdima\paperwidth
      \ESO@tempdimb=-\stockheight
      \advance\ESO@tempdimb\paperheight
      %% specific BEGIN
      \advance\ESO@tempdima 1in\relax
      \advance\ESO@tempdimb 1in\relax
      \advance\ESO@tempdima \hoffset\relax
      \advance\ESO@tempdimb \voffset\relax
      %% specific END
      \AtPageLowerLeft{%
        \put(\LenToUnit{\ESO@tempdima},\LenToUnit{\ESO@tempdimb}){##1}%
      }%
    \endgroup
  }% ===
  %% \AtStockUpperLeft: based on TRUE of \ESO@isMEMOIR
  \renewcommand\AtStockUpperLeft[1]{%
    \AtStockLowerLeft{%
      \put(0,\LenToUnit{\stockheight}){##1}%
    }%
  }% ===
  %% \AtStockCenter: based on TRUE of \ESO@isMEMOIR
  \renewcommand\AtStockCenter[1]{%
    \AtStockLowerLeft{%
      \put(\LenToUnit{.5\stockwidth},\LenToUnit{.5\stockheight}){##1}%
    }%
  }%
  %% \AtTextUpperLeft: same as FALSE of \ESO@isMEMOIR
  %  (no change)
  %
  %% \AtBeginShipout: based on TRUE of \ESO@isMEMOIR
  \AtBeginShipout{%
    \ESO@tempdima=\dimexpr(\stockwidth-\paperwidth)/2\relax
    \ESO@tempdimb=-\dimexpr(\stockheight+\paperheight)/2\relax
    \ifESO@texcoord
      \advance\ESO@tempdimb\paperheight % [FIXME] not sure!
    \fi
    %% specific BEGIN
    \advance\ESO@tempdima -\hoffset\relax
    \advance\ESO@tempdimb \voffset\relax
    %% specific END
    \nointerlineskip
    \AtBeginShipoutUpperLeft{%
      \put(\LenToUnit{\ESO@tempdima},\LenToUnit{\ESO@tempdimb}){%
        \ESO@HookIIIBG\ESO@HookIBG\ESO@HookIIBG
        \global\let\ESO@HookIIBG\@empty
      }%
    }%
    \AtBeginShipoutUpperLeftForeground{%
      \put(\LenToUnit{\ESO@tempdima},\LenToUnit{\ESO@tempdimb}){%
        \ESO@HookIFG\ESO@HookIIFG\ESO@HookIIIFG
        \global\let\ESO@HookIIFG\@empty
      }%
    }%
  }% ===
}
%%

\endinput
%% EOF