OSDN Git Service

ba88ec9450d2dff3fdfa7b98011f66309c7f970f
[luatex-ja/luatexja.git] / src / ltj-base.sty
1 %
2 % ltj-base.sty
3 %
4
5 %! これは外から読まれない
6 %% Avoid multiple loading.
7 \csname luatexjabaseLoaded\endcsname
8 \edef\ltj@base@AtEnd{%
9 \endlinechar=\the\endlinechar
10 \relax}
11 \endlinechar=-1 %
12 \def\luatexjabaseLoaded{\endinput}
13
14 \ifltj@in@latex                 %<*LaTeX>
15   \NeedsTeXFormat{LaTeX2e}
16   \ProvidesPackage{ltj-base}[2021-03-16]
17 \fi                             %</LaTeX>
18
19 %%------------------ Tiny helpers
20
21 %% Registers
22 %\newcount\ltj@tempcnta % defined at luatexja-core.sty
23 %\newcount\ltj@tempdima % defined at luatexja-core.sty
24 \newcount\ltj@tempcntb
25 \newcount\ltj@tempcntc
26 % \newskip\ltj@tempskipa unused
27 % \newtoks\ltj@temptoks unused
28
29 \countdef\ltj@@count@zero=0 %
30 \luatexattributedef\ltj@@attr@zero=0 %
31 \dimendef\ltj@@dimen@zero=0 %
32 \skipdef\ltj@@skip@zero=0 %
33
34 %% Quarks
35 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
36 %% \ltj@q@stop
37 \protected\def\ltj@q@stop{\ltj@q@stop@}
38 %% \ltj@q@nil
39 \protected\def\ltj@q@nil{\ltj@q@nil@}
40 %% \ltj@q@mark
41 \protected\def\ltj@q@mark{\ltj@q@mark@}
42
43 %! etoolbox の \csletcs.
44 %% \ltj@csletcs
45 \protected\def\ltj@csletcs#1#2{
46   \expandafter\let\csname#1\expandafter\endcsname
47   \csname#2\endcsname
48 }
49
50 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
51 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
52 %! 脱出が可能であること.
53 % Does \ifx<stuff> test in LaTeX style.
54 \long\def\ltj@ifx#1{
55   \ifx#1\expandafter\ltx@firstoftwo
56   \else\expandafter\ltx@secondoftwo\fi
57 }
58
59 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
60
61 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
62 %! <stuff> が空または空白文字からなるか.
63 % Checks if <stuff> is either empty or consisting only of spaces.
64 \def\ltj@if@blank#1{
65   \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
66    \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
67 }
68 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
69   #4
70 }
71
72 %%------------------ LaTeX vs plain
73 \ifltj@in@latex            %<*LaTeX>
74
75 %% \ifltj@in@latex
76 %! LaTeX であるか.
77 %(Defined in luatexja-core.sty.)
78
79 %% \ifltj@in@plain
80 %! plain であるか.
81 \ltj@csletcs{ifltj@in@plain}{iffalse}
82
83 \else                           %<*!LaTeX>
84
85 %% \ifltj@in@plain
86 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
87   \ltj@csletcs{ifltj@in@plain}{iftrue}
88 \else
89   \ltj@csletcs{ifltj@in@plain}{iffalse}
90 \fi
91
92 \fi                             %</LaTeX>
93 %%------------------ Value-token handling
94
95 %% helper stuffs
96
97 %% \ltj@gobble@num <number>
98 %! 次に続く整数を(2 回展開で)読み捨てる. 
99 \def\ltj@gobble@num{
100   \directlua{token.scan_int()}
101 }
102
103 %% \ltj@gobble@glue <glue>
104 %! 次に続くグルー値を(2 回展開で)読み捨てる. 
105 \def\ltj@gobble@glue{
106   \directlua{node.free(token.scan_glue())}
107 }
108
109 %% \ltj@gobble@dimen <dimen>
110 %! 次に続く寸法値を(2 回展開で)読み捨てる. 
111 \def\ltj@gobble@dimen{
112   \directlua{token.scan_dimen()}
113 }
114
115 %% \ltj@grab@num <number>
116 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
117 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
118 %! 展開する.
119 %! ただし, 整数は以下の形式のいずれかでなければならない.
120 %! - 整数表記(10 進, 8 進, 16 進, 文字)
121 %! - chardef トークン
122 %! - 内部整数パラメタ, countdef トークン
123 %!   (command_name が "assign_int" であるトークン).
124 %! (注意: まだ文字表記(`A)に対応していません.)
125 \def\ltj@grab@num{
126   \directlua{tex.sprint('{' .. token.scan_int() .. '}')}
127 }
128
129 %%------------------ Safe passing
130
131 % These macros convert a token sequence denoting a TeX value to its
132 % suitable notation in Lua, and when the argument is malformed then
133 % 'nil' is returned. They are all fully-expandable.
134 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
135
136 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
137   % behaves same as \relax but is distinct from it
138
139 %% \ltj@safe@invalid
140 % The value passed to lua instead of malformed value tokens.
141 \def\ltj@safe@invalid{(nil)}
142
143 %% \ltj@safe@num{<number>}
144 %! 整数.
145 % For a number (integer).
146 \def\ltj@safe@num{
147   \ltj@safe@num@or\ltj@safe@invalid
148 }
149 \def\ltj@safe@num@or#1#2{
150   \expandafter\expandafter\expandafter\ltj@@safe@numA
151    \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
152 }
153 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
154   \ltj@if@blank{#1}{
155     (\number#2)
156   }{
157     #3
158   }
159 }
160
161 %% \ltj@safe@dimen{<dimen>}
162 %! 寸法値.
163 % For a dimension. The result is a scaled-point value.
164 \def\ltj@safe@dimen{
165   \ltj@safe@dimen@or\ltj@safe@invalid
166 }
167 \def\ltj@safe@dimen@or#1#2{
168   \expandafter\expandafter\expandafter\ltj@@safe@dimenA
169    \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
170 }
171 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
172   \ltj@if@blank{#1}{
173     (\number\dimexpr#2\relax)
174   }{
175     #3
176   }
177 }
178
179 %! 整形式の整数 → 数値 : \number#1
180 %! 内部寸法 → 数値 : \number#1
181 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
182
183 %% \ltj@safe@str{<text>}
184 %! 文字列. (トークン列を非トークン化)
185 % Converts a token sequence to Lua string notation.
186 \def\ltj@safe@str#1{
187   "\luatexluaescapestring{\detokenize{#1}}"
188 }
189
190 %%------------------ debug logging
191 \ifdefined\LuaTeXjaDebugEnabled
192
193 %% Load Lua module
194 \RequireLuaModule{luatexja.debug}
195
196 %% \ltj@debug{<format>}{<arg>,...}
197 \def\ltj@debug#1#2{
198   \directlua{
199     luatexja.base.debug(\ltj@safe@str{#1}
200       \ltj@if@blank{#2}{}{, }
201       #2)
202   }
203 }
204
205 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
206 \def\ltj@package@debug#1#2#3{
207   \directlua{
208     luatexja.base.package_debug(\ltj@safe@str{#1},
209       \ltj@safe@str{#2}
210       \ltj@if@blank{#3}{}{, }
211       #3)
212   }
213 }
214
215 \else
216
217 \def\ltj@debug#1#2{}
218 \def\ltj@package@debug#1#2#3{}
219
220 \fi
221
222 %% \ltj@debug@logger\CS{<package>}
223 \def\ltj@debug@logger#1#2{
224   \def#1{\ltj@package@debug{#2}}
225 }
226
227 %% \ltj@afterbox <token><box>
228 %% -> \setbox\ltj@afbox<box><token>
229 %% idea from the atbegshi package
230 \newbox\ltj@afbox
231 \directlua{luatexja.afbox_number=\the\ltj@afbox}
232 \protected\def\ltj@afterbox#1{%
233   \def\ltj@afbox@@arg{#1}\edef\ltj@afbox@@grouplevel{\number\currentgrouplevel}%
234   \afterassignment\ltj@afbox@@\setbox\ltj@afbox}
235 \def\ltj@afbox@@{%
236   \ifnum\ltj@afbox@@grouplevel<\currentgrouplevel\expandafter\aftergroup\fi\ltj@afbox@@arg
237 }
238
239
240 %%------------------ all done
241 \ltj@base@AtEnd
242 \endinput
243 %% EOF