OSDN Git Service

lltjfont: NFSS review (WIP)
[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}[2020-08-04]
17 \fi                             %</LaTeX>
18
19 %%------------------ Tiny helpers
20
21 %% Registers
22 %\newcount\ltj@tempcnta % defined at luatexja-core.sty
23 \newcount\ltj@tempcntb
24 \newcount\ltj@tempcntc
25 \newdimen\ltj@tempdima
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 の \letcs, \cslet, \csletcs.
44 %% \ltj@letcs
45 \protected\def\ltj@letcs#1#2{
46   \expandafter\let\expandafter#1\csname#2\endcsname
47 }
48
49 %% \ltj@cslet
50 \protected\def\ltj@cslet#1#2{
51   \expandafter\let\csname#1\endcsname#2
52 }
53
54 %% \ltj@csletcs
55 \protected\def\ltj@csletcs#1#2{
56   \expandafter\let\csname#1\expandafter\endcsname
57   \csname#2\endcsname
58 }
59
60 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
61 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
62 %! 脱出が可能であること.
63 % Does \ifx<stuff> test in LaTeX style.
64 \long\def\ltj@ifx#1{
65   \ifx#1\expandafter\ltx@firstoftwo
66   \else\expandafter\ltx@secondoftwo\fi
67 }
68
69 %% \ltj@if@empty{<stuff>}{<yes>}{<no>}
70 %! <stuff> が空であるか.
71 % Checks if <stuff> is empty.
72 \long\def\ltj@if@empty#1{
73   \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
74 }
75 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
76
77 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
78 %! <stuff> が空または空白文字からなるか.
79 % Checks if <stuff> is either empty or consisting only of spaces.
80 \def\ltj@if@blank#1{
81   \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
82    \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
83 }
84 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
85   #4
86 }
87
88 %%------------------ LaTeX vs plain
89 \ifltj@in@latex            %<*LaTeX>
90
91 %% \ifltj@in@latex
92 %! LaTeX であるか.
93 %(Defined in luatexja-core.sty.)
94
95 %% \ifltj@in@plain
96 %! plain であるか.
97 \ltj@csletcs{ifltj@in@plain}{iffalse}
98
99 %% \ltj@print{<message>}
100 %! 端末への出力.
101 \let\ltj@print\typeout
102
103 \else                           %<*!LaTeX>
104
105 %% \ifltj@in@plain
106 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
107   \ltj@csletcs{ifltj@in@plain}{iftrue}
108 \else
109   \ltj@csletcs{ifltj@in@plain}{iffalse}
110 \fi
111
112 %% \ltj@print{<message>}
113 \def\ltj@print#1{
114   \immediate\write16{#1}
115 }
116
117 \fi                             %</LaTeX>
118 %%------------------ Value-token handling
119
120 %% helper stuffs
121
122 %% \ltj@gobble@num <number>
123 %! 次に続く整数を(2 回展開で)読み捨てる. 
124 \def\ltj@gobble@num{
125   \directlua{token.scan_int()}
126 }
127
128 %% \ltj@gobble@glue <glue>
129 %! 次に続くグルー値を(2 回展開で)読み捨てる. 
130 \def\ltj@gobble@glue{
131   \directlua{token.scan_glue()}
132 }
133
134 %% \ltj@gobble@dimen <dimen>
135 %! 次に続く寸法値を(2 回展開で)読み捨てる. 
136 \def\ltj@gobble@dimen{
137   \directlua{token.scan_dimen()}
138 }
139
140 %% \ltj@grab@num <number>
141 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
142 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
143 %! 展開する.
144 %! ただし, 整数は以下の形式のいずれかでなければならない.
145 %! - 整数表記(10 進, 8 進, 16 進, 文字)
146 %! - chardef トークン
147 %! - 内部整数パラメタ, countdef トークン
148 %!   (command_name が "assign_int" であるトークン).
149 %! (注意: まだ文字表記(`A)に対応していません.)
150 \def\ltj@grab@num{
151   \directlua{tex.sprint('{' .. token.scan_int() .. '}')}
152 }
153
154 %%------------------ Safe passing
155
156 % These macros convert a token sequence denoting a TeX value to its
157 % suitable notation in Lua, and when the argument is malformed then
158 % 'nil' is returned. They are all fully-expandable.
159 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
160
161 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
162   % behaves same as \relax but is distinct from it
163
164 %% \ltj@safe@invalid
165 % The value passed to lua instead of malformed value tokens.
166 \def\ltj@safe@invalid{(nil)}
167
168 %% \ltj@safe@num{<number>}
169 %! 整数.
170 % For a number (integer).
171 \def\ltj@safe@num{
172   \ltj@safe@num@or\ltj@safe@invalid
173 }
174 \def\ltj@safe@num@or#1#2{
175   \expandafter\expandafter\expandafter\ltj@@safe@numA
176    \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
177 }
178 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
179   \ltj@if@blank{#1}{
180     (\number#2)
181   }{
182     #3
183   }
184 }
185
186 %% \ltj@safe@dimen{<dimen>}
187 %! 寸法値.
188 % For a dimension. The result is a scaled-point value.
189 \def\ltj@safe@dimen{
190   \ltj@safe@dimen@or\ltj@safe@invalid
191 }
192 \def\ltj@safe@dimen@or#1#2{
193   \expandafter\expandafter\expandafter\ltj@@safe@dimenA
194    \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
195 }
196 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
197   \ltj@if@blank{#1}{
198     (\number\dimexpr#2\relax)
199   }{
200     #3
201   }
202 }
203
204 %% \ltj@safe@real{<real>}
205 %! 実数. これは十進表記に限る.
206 % For a real number given in decimal notation or a macro that
207 % expands to such notation.
208 \def\ltj@safe@real#1{
209   (tonumber("\luatexluaescapestring{#1}"))
210 }
211
212 %% \ltj@val@counter{<counter>}
213 %! LaTeX カウンタの現在値.
214 % For the current value of a LaTeX counter.
215 \def\ltj@val@counter#1{
216   (\expandafter\number\csname c@#1\endcsname)
217 }
218
219 %% \ltj@val@skip{<skip>}
220 % For the current value of a skip (or LaTeX-length) parameter.
221 %! グルーレジスタ(LaTeX 長さ変数).
222 %! 整形式のグルー値にも使える.
223 \def\ltj@val@skip#1{
224   (luatexja.base.to_skip("\the\glueexpr#1\relax"))
225 }
226 %! 整形式の整数 → 数値 : \number#1
227 %! 内部寸法 → 数値 : \number#1
228 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
229
230 %% \ltj@safe@str{<text>}
231 %! 文字列. (トークン列を非トークン化)
232 % Converts a token sequence to Lua string notation.
233 \def\ltj@safe@str#1{
234   "\luatexluaescapestring{\detokenize{#1}}"
235 }
236
237 %% \ltj@luaescape{<text>}
238 %! 非トークン化して Lua エスケープ.
239 \def\ltj@luaescape#1{
240   \luatexluaescapestring{\detokenize{#1}}
241 }
242 %% \ltj@luaxescape{<text>}
243 %! 非トークン化せずに(展開ありで) Lua エスケープ.
244 %! つまり単なる \luatexescape.
245 \let\ltj@luaxescape\luatexluaescapestring
246
247
248 %%------------------ debug logging
249 \ifdefined\LuaTeXjaDebugEnabled
250
251 %% Load Lua module
252 \RequireLuaModule{luatexja.debug}
253
254 %% \ltj@debug{<format>}{<arg>,...}
255 \def\ltj@debug#1#2{
256   \directlua{
257     luatexja.base.debug(\ltj@safe@str{#1}
258       \ltj@if@blank{#2}{}{, }
259       #2)
260   }
261 }
262
263 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
264 \def\ltj@package@debug#1#2#3{
265   \directlua{
266     luatexja.base.package_debug(\ltj@safe@str{#1},
267       \ltj@safe@str{#2}
268       \ltj@if@blank{#3}{}{, }
269       #3)
270   }
271 }
272
273 \else
274
275 \def\ltj@debug#1#2{}
276 \def\ltj@package@debug#1#2#3{}
277
278 \fi
279
280 %% \ltj@debug@logger\CS{<package>}
281 \def\ltj@debug@logger#1#2{
282   \def#1{\ltj@package@debug{#2}}
283 }
284
285 %% \ltj@afterbox <token><box>
286 %% -> \setbox\ltj@afbox<box><token>
287 %% from Sonja Maus, ``Looking Ahead for a <box>'',
288 %%      TUGBoat, 11, No. 4, 1990.
289 \newbox\ltj@afbox
290 \directlua{luatexja.afbox_number=\the\ltj@afbox}
291 \protected\def\ltj@afterbox#1{%
292   \def\ltj@afb@xarg{#1}%
293   \afterassignment\ltj@afb@x
294   \chardef\ltj@@next`.}
295 \def\ltj@afb@x{\futurelet\ltj@@next\ltj@afb@xtest}
296 \def\ltj@afb@xtest{%
297   \ifcase\ifx\ltj@@next\hbox\tw@\fi
298          \ifx\ltj@@next\vbox\tw@\fi
299          \ifx\ltj@@next\vtop\tw@\fi
300          \ifx\ltj@@next\box\@ne\fi
301          \ifx\ltj@@next\copy\@ne\fi
302          \ifx\ltj@@next\vsplit\@ne\fi
303          \ifx\ltj@@next\lastbox\@ne\fi
304          0% ``A <box> was ...'' error will be causes by \setbox later anyway.
305   \or\afterassignment\ltj@afb@xarg
306   \or\afterassignment\ltj@afb@xagarg
307   \fi
308   \setbox\ltj@afbox
309 }
310 \def\ltj@afb@xagarg{\aftergroup\ltj@afb@xarg}
311
312
313 %%------------------ all done
314 \ltj@base@AtEnd
315 \endinput
316 %% EOF