6 %% Avoid multiple loading.
7 \csname luatexjabaseLoaded\endcsname
9 \endlinechar=\the\endlinechar
12 \def\luatexjabaseLoaded{\endinput}
14 \ifltj@in@latex %<*LaTeX>
15 \NeedsTeXFormat{LaTeX2e}
16 \ProvidesPackage{ltj-base}[2021-03-16]
19 %%------------------ Tiny helpers
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
29 \countdef\ltj@@count@zero=0 %
30 \luatexattributedef\ltj@@attr@zero=0 %
31 \dimendef\ltj@@dimen@zero=0 %
32 \skipdef\ltj@@skip@zero=0 %
35 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
37 \protected\def\ltj@q@stop{\ltj@q@stop@}
39 \protected\def\ltj@q@nil{\ltj@q@nil@}
41 \protected\def\ltj@q@mark{\ltj@q@mark@}
43 %! etoolbox の \csletcs.
45 \protected\def\ltj@csletcs#1#2{
46 \expandafter\let\csname#1\expandafter\endcsname
50 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
51 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
53 % Does \ifx<stuff> test in LaTeX style.
55 \ifx#1\expandafter\ltx@firstoftwo
56 \else\expandafter\ltx@secondoftwo\fi
59 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
61 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
62 %! <stuff> が空または空白文字からなるか.
63 % Checks if <stuff> is either empty or consisting only of spaces.
65 \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
66 \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
68 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
72 %%------------------ LaTeX vs plain
73 \ifltj@in@latex %<*LaTeX>
77 %(Defined in luatexja-core.sty.)
81 \ltj@csletcs{ifltj@in@plain}{iffalse}
86 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
87 \ltj@csletcs{ifltj@in@plain}{iftrue}
89 \ltj@csletcs{ifltj@in@plain}{iffalse}
93 %%------------------ Value-token handling
97 %% \ltj@gobble@num <number>
98 %! 次に続く整数を(2 回展開で)読み捨てる.
100 \directlua{token.scan_int()}
103 %% \ltj@gobble@glue <glue>
104 %! 次に続くグルー値を(2 回展開で)読み捨てる.
105 \def\ltj@gobble@glue{
106 \directlua{node.free(token.scan_glue())}
109 %% \ltj@gobble@dimen <dimen>
110 %! 次に続く寸法値を(2 回展開で)読み捨てる.
111 \def\ltj@gobble@dimen{
112 \directlua{token.scan_dimen()}
115 %% \ltj@grab@num <number>
116 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
117 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
119 %! ただし, 整数は以下の形式のいずれかでなければならない.
120 %! - 整数表記(10 進, 8 進, 16 進, 文字)
122 %! - 内部整数パラメタ, countdef トークン
123 %! (command_name が "assign_int" であるトークン).
124 %! (注意: まだ文字表記(`A)に対応していません.)
126 \directlua{tex.sprint(luatexbase.catcodetables['latex-package'], '{' .. token.scan_int() .. '}')}
129 %%------------------ Safe passing
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 にする.
136 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
137 % behaves same as \relax but is distinct from it
140 % The value passed to lua instead of malformed value tokens.
141 \def\ltj@safe@invalid{(nil)}
143 %% \ltj@safe@num{<number>}
145 % For a number (integer).
147 \ltj@safe@num@or\ltj@safe@invalid
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}
153 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
161 %% \ltj@safe@dimen{<dimen>}
163 % For a dimension. The result is a scaled-point value.
165 \ltj@safe@dimen@or\ltj@safe@invalid
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}
171 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
173 (\number\dimexpr#2\relax)
179 %! 整形式の整数 → 数値 : \number#1
180 %! 内部寸法 → 数値 : \number#1
181 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
183 %% \ltj@safe@str{<text>}
184 %! 文字列. (トークン列を非トークン化)
185 % Converts a token sequence to Lua string notation.
187 "\luatexluaescapestring{\detokenize{#1}}"
190 %%------------------ debug logging
191 \ifdefined\LuaTeXjaDebugEnabled
194 \RequireLuaModule{luatexja.debug}
196 %% \ltj@debug{<format>}{<arg>,...}
199 luatexja.base.debug(\ltj@safe@str{#1}
200 \ltj@if@blank{#2}{}{, }
205 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
206 \def\ltj@package@debug#1#2#3{
208 luatexja.base.package_debug(\ltj@safe@str{#1},
210 \ltj@if@blank{#3}{}{, }
218 \def\ltj@package@debug#1#2#3{}
222 %% \ltj@debug@logger\CS{<package>}
223 \def\ltj@debug@logger#1#2{
224 \def#1{\ltj@package@debug{#2}}
227 %% \ltj@afterbox <token><box>
228 %% -> \setbox\ltj@afbox<box><token>
229 %% idea from the atbegshi package
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}
236 \ifnum\ltj@afbox@@grouplevel<\currentgrouplevel\expandafter\aftergroup\fi\ltj@afbox@@arg
240 %%------------------ all done