summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/luatexja/src/ltj-base.sty
blob: d46b54a1faf01b354aa5b3e7e1a41a59d3a869b4 (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
240
241
242
243
%
% ltj-base.sty
%

%! これは外から読まれない
%% Avoid multiple loading.
\csname luatexjabaseLoaded\endcsname
\edef\ltj@base@AtEnd{%
\endlinechar=\the\endlinechar
\relax}
\endlinechar=-1 %
\def\luatexjabaseLoaded{\endinput}

\ifltj@in@latex                 %<*LaTeX>
  \NeedsTeXFormat{LaTeX2e}
  \ProvidesPackage{ltj-base}[2021-03-16]
\fi                             %</LaTeX>

%%------------------ Tiny helpers

%% Registers
%\newcount\ltj@tempcnta % defined at luatexja-core.sty
\newcount\ltj@tempcntb
\newcount\ltj@tempcntc
\newdimen\ltj@tempdima
% \newskip\ltj@tempskipa unused
% \newtoks\ltj@temptoks unused

\countdef\ltj@@count@zero=0 %
\luatexattributedef\ltj@@attr@zero=0 %
\dimendef\ltj@@dimen@zero=0 %
\skipdef\ltj@@skip@zero=0 %

%% Quarks
%! ただ expl3 の quark とは異なり展開されるとエラーになる.
%% \ltj@q@stop
\protected\def\ltj@q@stop{\ltj@q@stop@}
%% \ltj@q@nil
\protected\def\ltj@q@nil{\ltj@q@nil@}
%% \ltj@q@mark
\protected\def\ltj@q@mark{\ltj@q@mark@}

%! etoolbox の \csletcs.
%% \ltj@csletcs
\protected\def\ltj@csletcs#1#2{
  \expandafter\let\csname#1\expandafter\endcsname
  \csname#2\endcsname
}

%% \ltj@ifx{<sutff>}{<yes>}{<no>}
%! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
%! 脱出が可能であること.
% Does \ifx<stuff> test in LaTeX style.
\long\def\ltj@ifx#1{
  \ifx#1\expandafter\ltx@firstoftwo
  \else\expandafter\ltx@secondoftwo\fi
}

\protected\def\ltj@@q@empty{\ltj@@q@empty@}

%% \ltj@if@blank{<stuff>}{<yes>}{<no>}
%! <stuff> が空または空白文字からなるか.
% Checks if <stuff> is either empty or consisting only of spaces.
\def\ltj@if@blank#1{
  \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
   \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
}
\long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
  #4
}

%%------------------ LaTeX vs plain
\ifltj@in@latex            %<*LaTeX>

%% \ifltj@in@latex
%! LaTeX であるか.
%(Defined in luatexja-core.sty.)

%% \ifltj@in@plain
%! plain であるか.
\ltj@csletcs{ifltj@in@plain}{iffalse}

\else                           %<*!LaTeX>

%% \ifltj@in@plain
\ifnum\pdf@strcmp{\fmtname}{plain}=0 %
  \ltj@csletcs{ifltj@in@plain}{iftrue}
\else
  \ltj@csletcs{ifltj@in@plain}{iffalse}
\fi

\fi                             %</LaTeX>
%%------------------ Value-token handling

%% helper stuffs

%% \ltj@gobble@num <number>
%! 次に続く整数を(2 回展開で)読み捨てる. 
\def\ltj@gobble@num{
  \directlua{token.scan_int()}
}

%% \ltj@gobble@glue <glue>
%! 次に続くグルー値を(2 回展開で)読み捨てる. 
\def\ltj@gobble@glue{
  \directlua{node.free(token.scan_glue())}
}

%% \ltj@gobble@dimen <dimen>
%! 次に続く寸法値を(2 回展開で)読み捨てる. 
\def\ltj@gobble@dimen{
  \directlua{token.scan_dimen()}
}

%% \ltj@grab@num <number>
%! 次に続く整数を読み取って, それと等しい整数を表すトークン列
%! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
%! 展開する.
%! ただし, 整数は以下の形式のいずれかでなければならない.
%! - 整数表記(10 進, 8 進, 16 進, 文字)
%! - chardef トークン
%! - 内部整数パラメタ, countdef トークン
%!   (command_name が "assign_int" であるトークン).
%! (注意: まだ文字表記(`A)に対応していません.)
\def\ltj@grab@num{
  \directlua{tex.sprint('{' .. token.scan_int() .. '}')}
}

%%------------------ Safe passing

% These macros convert a token sequence denoting a TeX value to its
% suitable notation in Lua, and when the argument is malformed then
% 'nil' is returned. They are all fully-expandable.
%! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.

\def\ltj@@safe@end{\noexpand\ltj@@safe@end}
  % behaves same as \relax but is distinct from it

%% \ltj@safe@invalid
% The value passed to lua instead of malformed value tokens.
\def\ltj@safe@invalid{(nil)}

%% \ltj@safe@num{<number>}
%! 整数.
% For a number (integer).
\def\ltj@safe@num{
  \ltj@safe@num@or\ltj@safe@invalid
}
\def\ltj@safe@num@or#1#2{
  \expandafter\expandafter\expandafter\ltj@@safe@numA
   \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
}
\def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
  \ltj@if@blank{#1}{
    (\number#2)
  }{
    #3
  }
}

%% \ltj@safe@dimen{<dimen>}
%! 寸法値.
% For a dimension. The result is a scaled-point value.
\def\ltj@safe@dimen{
  \ltj@safe@dimen@or\ltj@safe@invalid
}
\def\ltj@safe@dimen@or#1#2{
  \expandafter\expandafter\expandafter\ltj@@safe@dimenA
   \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
}
\def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
  \ltj@if@blank{#1}{
    (\number\dimexpr#2\relax)
  }{
    #3
  }
}

%! 整形式の整数 → 数値 : \number#1
%! 内部寸法 → 数値 : \number#1
%! 整形式の寸法 → 数値 : \number\dimexpr#1\relax

%% \ltj@safe@str{<text>}
%! 文字列. (トークン列を非トークン化)
% Converts a token sequence to Lua string notation.
\def\ltj@safe@str#1{
  "\luatexluaescapestring{\detokenize{#1}}"
}

%%------------------ debug logging
\ifdefined\LuaTeXjaDebugEnabled

%% Load Lua module
\RequireLuaModule{luatexja.debug}

%% \ltj@debug{<format>}{<arg>,...}
\def\ltj@debug#1#2{
  \directlua{
    luatexja.base.debug(\ltj@safe@str{#1}
      \ltj@if@blank{#2}{}{, }
      #2)
  }
}

%% \ltj@package@debug{<package>}{<format>}{<arg>,...}
\def\ltj@package@debug#1#2#3{
  \directlua{
    luatexja.base.package_debug(\ltj@safe@str{#1},
      \ltj@safe@str{#2}
      \ltj@if@blank{#3}{}{, }
      #3)
  }
}

\else

\def\ltj@debug#1#2{}
\def\ltj@package@debug#1#2#3{}

\fi

%% \ltj@debug@logger\CS{<package>}
\def\ltj@debug@logger#1#2{
  \def#1{\ltj@package@debug{#2}}
}

%% \ltj@afterbox <token><box>
%% -> \setbox\ltj@afbox<box><token>
%% idea from the atbegshi package
\newbox\ltj@afbox
\directlua{luatexja.afbox_number=\the\ltj@afbox}
\protected\def\ltj@afterbox#1{%
  \def\ltj@afbox@@arg{#1}\edef\ltj@afbox@@grouplevel{\number\currentgrouplevel}%
  \afterassignment\ltj@afbox@@\setbox\ltj@afbox}
\def\ltj@afbox@@{%
  \ifnum\ltj@afbox@@grouplevel<\currentgrouplevel\expandafter\aftergroup\fi\ltj@afbox@@arg
}


%%------------------ all done
\ltj@base@AtEnd
\endinput
%% EOF