OSDN Git Service

use getFOO/setFOO helpers instead of getfield/setfield
[luatex-ja/luatexja.git] / src / patches / lltjp-atbegshi.sty
1 %
2 % lltjp-atbegshi.sty
3 %
4
5 \NeedsTeXFormat{LaTeX2e}
6 \ProvidesPackage{lltjp-atbegshi}[2022-08-12 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 \if_cs_exist:N \__shipout_execute_main_cont:Nnnn %% 2021-06-01
17 \cs_new_eq:NN \__platex_original_shipout_execute_main_cont:Nnnn
18               \__shipout_execute_main_cont:Nnnn
19 \cs_set:Npn \__shipout_execute_main_cont:Nnnn #1#2#3#4 {
20   % if the box #1 is not a \yoko-box (= horizontal writing),
21   % then make it a \yoko-box behorehand.
22   \platex_if_box_yoko:NF #1 {
23     \vbox_set:Nn #1 { \platex_direction_yoko: \box_use:N #1 }
24   }
25   % if the current direction is not \yoko,
26   % then enclose \__shipout_execute_main_cont:Nnnn with
27   % a dummy \yoko-box named \l__platex_shipout_dummy_box.
28   \platex_if_direction_yoko:TF {
29     \__platex_original_shipout_execute_main_cont:Nnnn { #1 } { #2 } { #3 } { #4 }
30   }{
31     \vbox_set:Nn \l__platex_shipout_dummy_box {
32       \platex_direction_yoko:
33       \__platex_original_shipout_execute_main_cont:Nnnn { #1 } { #2 } { #3 } { #4 }
34     }
35     % [Limitation] the code above may discard some contents,
36     % so we'd like to put it back by \box\l__platex_shipout_dummy_box.
37     % however, an infinite loop occurs if we uncomment the line below
38     % so we can't.
39     %\box_use:N \l__platex_shipout_dummy_box
40     \box_clear:N \l__platex_shipout_dummy_box
41   }
42 }
43
44 \else: %% 2020-10-01
45 \cs_new_eq:NN \__platex_original_shipout_execute_cont: \__shipout_execute_cont:
46 \cs_set:Npn \__shipout_execute_cont:
47 {
48   \platex_if_box_yoko:NF \l_shipout_box {
49     \vbox_set:Nn \l_shipout_box { \platex_direction_yoko: \box_use:N \l_shipout_box }
50   }
51   \platex_if_direction_yoko:TF {
52     \__platex_original_shipout_execute_cont:
53   }{
54     \vbox_set:Nn \l__platex_shipout_dummy_box {
55       \platex_direction_yoko: \__platex_original_shipout_execute_cont:
56     }
57     \box_clear:N \l__platex_shipout_dummy_box
58   }
59 }
60 \fi:
61 %</ltshipout>-----------------------------------
62 \ExplSyntaxOff
63 \expandafter\endinput
64 \fi
65 \RequirePackage{atbegshi}
66 %<*atbegshi>-------------------------------------
67 % patching atbegshi.sty
68 %%%  following codes are almost copied from
69 %%%  pxatbegshi.sty 2019/09/07 v0.5 (in platex-tools bundle, by H. Yamashita).
70
71 \ifdefined\AtBeginShipoutOriginalShipout
72   \let\AtBeginShipoutOriginalShipout\ltj@shipout
73 \fi
74 \AtBeginShipoutInit
75 \DeclareRobustCommand \AtBeginDvi [1]{%
76   \global \setbox \@begindvibox
77     \vbox{\yoko\unvbox \@begindvibox #1}%
78 }
79 %% internal macros are locally effective
80 \begingroup
81
82 % helpers
83 %% unique tokens
84 \def\pxabgs@mark{\pxabgs@mark@}
85 \def\pxabgs@fin{\pxabgs@fin@}
86
87 %% \pxabgs@patch@cmd\CMD{<orig>}{<new>}
88 \def\pxabgs@patch@cmd#1#2#3{%
89   \ifx#1\relax\else
90   \def\pxabgs@next##1#2##2\pxabgs@mark##3\pxabgs@fin{%
91     \ifx\pxabgs@mark##3\pxabgs@mark\let#1\relax
92     \else
93       \def\pxabgs@fragment{##2}\def#1{##1#3##2}%
94     \fi}%
95   \expandafter\pxabgs@next#1\pxabgs@mark#2\pxabgs@mark\pxabgs@fin
96   \fi
97 }
98
99 % prepare
100 \ifdefined\AtBegShi@Output
101   \let\pxabgs@AtBegShi@Output\AtBegShi@Output
102 \else\let\pxabgs@AtBegShi@Output\relax\fi
103 % try first patch
104 \pxabgs@patch@cmd\pxabgs@AtBegShi@Output
105   {\let\AtBegShi@OrgProtect\protect}%
106   {\setbox8\vbox\bgroup\yoko\let\AtBegShi@OrgProtect\protect}
107 \pxabgs@patch@cmd\pxabgs@AtBegShi@Output
108   {\AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox}%
109   {\AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox\egroup\setbox8\box\voidb@x}
110 \pxabgs@patch@cmd\pxabgs@AtBegShi@Output
111   {%
112     \begingroup
113       \setbox\AtBeginShipoutBox\box\AtBeginShipoutBox
114     \endgroup
115     \let\protect\AtBegShi@OrgProtect
116   }{%
117     \begingroup
118       \setbox\AtBeginShipoutBox\box\AtBeginShipoutBox
119     \endgroup
120     \let\protect\AtBegShi@OrgProtect
121     \egroup
122   }
123 % commit the change only when successful
124 \ifx\pxabgs@AtBegShi@Output\relax
125   \@PackageWarningNoLine{lltjp-atbegshi}{%
126     Failed in patching \string\AtBegShi@Output.\MessageBreak
127     This may cause error in tate direction%
128   }
129 \else
130   \global\let\AtBegShi@Output\pxabgs@AtBegShi@Output
131 \fi
132
133 \endgroup
134 %% internal macros are no longer effective
135 %</atbegshi>-------------------------------------
136 \endinput