OSDN Git Service

07f16731be8e1b38992eb7d13677f13034006414
[luatex-ja/luatexja.git] / src / patches / lltjp-atbegshi.sty
1 %
2 % lltjp-atbegshi.sty
3 %
4
5 \NeedsTeXFormat{LaTeX2e}
6 \ProvidesPackage{lltjp-atbegshi}[2020-09-27 Patching \string\shipout\space hooks for LuaTeX-ja]
7
8 % concept: execute all hooks inside yoko direction \vbox
9
10 \ifltj@ltfilehook@avail
11 %<*ltshipout>-----------------------------------
12 %
13 \RequirePackage{expl3}
14 \ExplSyntaxOn\makeatletter
15 \box_new:N \l__platex_shipout_dummy_box
16 \cs_new_eq:NN \__platex_original_shipout_execute_cont: \__shipout_execute_cont:
17 \cs_set:Npn \__shipout_execute_cont:
18 {
19   % if \l_shipout_box is not a \yoko-box (= horizontal writing),
20   % then make it a \yoko-box behorehand.
21   \unless\ifnum\ltjgetparameter{boxdir}{\l_shipout_box}=4
22     \vbox_set:Nn \l_shipout_box { \yoko \box_use:N \l_shipout_box }
23   \fi
24   % if the current direction is not \yoko,
25   % then enclose \__shipout_execute_cont: with
26   % a dummy \yoko-box named \l__platex_shipout_dummy_box.
27   \ifnum\ltjgetparameter{direction}=4
28     \__platex_original_shipout_execute_cont:
29   \else
30     \vbox_set:Nn \l__platex_shipout_dummy_box {
31       \yoko \__platex_original_shipout_execute_cont:
32     }
33     % [Limitation] the code above may discard some contents,
34     % so we'd like to put it back by \box\l__platex_shipout_dummy_box.
35     % however, an infinite loop occurs if we uncomment the line below
36     % so we can't.
37     %\box_use:N \l__platex_shipout_dummy_box
38   \fi
39 }
40 %</ltshipout>-----------------------------------
41
42 \expandafter\endinput
43 \fi
44 \RequirePackage{atbegshi}
45 %<*atbegshi>-------------------------------------
46 % patching atbegshi.sty
47 %%%  following codes are almost copied from
48 %%%  pxatbegshi.sty 2019/09/07 v0.5 (in platex-tools bundle, by H. Yamashita).
49
50 \ifdefined\AtBeginShipoutOriginalShipout
51   \let\AtBeginShipoutOriginalShipout\ltj@shipout
52 \fi
53 \AtBeginShipoutInit
54 \DeclareRobustCommand \AtBeginDvi [1]{%
55   \global \setbox \@begindvibox
56     \vbox{\yoko\unvbox \@begindvibox #1}%
57 }
58 %% internal macros are locally effective
59 \begingroup
60
61 % helpers
62 %% unique tokens
63 \def\pxabgs@mark{\pxabgs@mark@}
64 \def\pxabgs@fin{\pxabgs@fin@}
65
66 %% \pxabgs@patch@cmd\CMD{<orig>}{<new>}
67 \def\pxabgs@patch@cmd#1#2#3{%
68   \ifx#1\relax\else
69   \def\pxabgs@next##1#2##2\pxabgs@mark##3\pxabgs@fin{%
70     \ifx\pxabgs@mark##3\pxabgs@mark\let#1\relax
71     \else
72       \def\pxabgs@fragment{##2}\def#1{##1#3##2}%
73     \fi}%
74   \expandafter\pxabgs@next#1\pxabgs@mark#2\pxabgs@mark\pxabgs@fin
75   \fi
76 }
77
78 % prepare
79 \ifdefined\AtBegShi@Output
80   \let\pxabgs@AtBegShi@Output\AtBegShi@Output
81 \else\let\pxabgs@AtBegShi@Output\relax\fi
82 % try first patch
83 \pxabgs@patch@cmd\pxabgs@AtBegShi@Output
84   {\let\AtBegShi@OrgProtect\protect}%
85   {\setbox8\vbox\bgroup\yoko\let\AtBegShi@OrgProtect\protect}
86 \pxabgs@patch@cmd\pxabgs@AtBegShi@Output
87   {\AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox}%
88   {\AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox\egroup}
89 \pxabgs@patch@cmd\pxabgs@AtBegShi@Output
90   {%
91     \begingroup
92       \setbox\AtBeginShipoutBox\box\AtBeginShipoutBox
93     \endgroup
94     \let\protect\AtBegShi@OrgProtect
95   }{%
96     \begingroup
97       \setbox\AtBeginShipoutBox\box\AtBeginShipoutBox
98     \endgroup
99     \let\protect\AtBegShi@OrgProtect
100     \egroup
101   }
102 % commit the change only when successful
103 \ifx\pxabgs@AtBegShi@Output\relax
104   \@PackageWarningNoLine{lltjp-atbegshi}{%
105     Failed in patching \string\AtBegShi@Output.\MessageBreak
106     This may cause error in tate direction%
107   }
108 \else
109   \global\let\AtBegShi@Output\pxabgs@AtBegShi@Output
110 \fi
111
112 \endgroup
113 %% internal macros are no longer effective
114 %</atbegshi>-------------------------------------
115 \endinput