OSDN Git Service

Merge commit '5219ca4' into kitagawa_lt20201001
[luatex-ja/luatexja.git] / src / luatexja-core.sty
1 %
2 % luatexja-core.sty
3 %
4
5 %! 外から読まれるので仰々しくなってます
6 %% Avoid multiple loading.
7 \csname\if11luatexjacoreLoaded\fi\endcsname
8 \edef\x{%
9 \catcode32=\the\catcode32%
10 \catcode33=\the\catcode33%
11 \catcode35=\the\catcode35%
12 \catcode40=\the\catcode40%
13 \catcode41=\the\catcode41%
14 \catcode42=\the\catcode42%
15 \catcode43=\the\catcode43%
16 \catcode44=\the\catcode44%
17 \catcode45=\the\catcode45%
18 \catcode46=\the\catcode46%
19 \catcode58=\the\catcode58%
20 \catcode59=\the\catcode59%
21 \catcode60=\the\catcode60%
22 \catcode61=\the\catcode61%
23 \catcode62=\the\catcode62%
24 \catcode64=\the\catcode64%
25 \catcode91=\the\catcode91%
26 \catcode93=\the\catcode93%
27 \catcode94=\the\catcode94%
28 \catcode95=\the\catcode95%
29 \catcode126=\the\catcode126%
30 \endlinechar=\the\endlinechar
31 \relax}
32 \catcode32=10 %< >
33 \catcode33=12 %<">
34 \catcode35=6  %<#>
35 \catcode40=12 %<(>
36 \catcode41=12 %<)>
37 \catcode42=12 %<*>
38 \catcode43=12 %<+>
39 \catcode44=12 %<,>
40 \catcode45=12 %<->
41 \catcode46=12 %<.>
42 \catcode58=12 %<:>
43 \catcode59=12 %<;>
44 \catcode60=12 %<<>
45 \catcode61=12 %<=>
46 \catcode62=12 %<>>
47 \catcode64=11 %<@>
48 \catcode91=12 %<[>
49 \catcode93=12 %<]>
50 \catcode94=7  %<^>
51 \catcode95=8  %<_>
52 \catcode126=13 %<~>
53 \endlinechar=-1 %
54 %! なお, 上のcatcode設定はcoreとcompatで同一にしておく.
55 \edef\ltj@core@AtEnd{\x
56   \noexpand\let\noexpand\ltj@core@AtEnd\relax}
57 \def\luatexjacoreLoaded{\endinput}
58
59 %% Check if LaTeX is used.
60 \begingroup\expandafter\expandafter\expandafter\endgroup
61 \expandafter\ifx\csname RequirePackage\endcsname\relax %<*!LaTeX>
62   \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
63     \csname iffalse\endcsname
64 \else                           %<*LaTeX>
65   \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
66     \csname iftrue\endcsname
67   \NeedsTeXFormat{LaTeX2e}
68   \ProvidesPackage{luatexja-core}[2020-08-04 Core of LuaTeX-ja]
69 \fi                             %</LaTeX>
70
71 %% Load prerequisite packages.
72 \endlinechar=13 %
73 \newif\ifltj@everyhook@avail\ltj@everyhook@availfalse
74 %! 外部パッケージは普通の \endlinechar で読まないと危険.
75 \ifltj@in@latex                 %<*LaTeX>
76   \RequirePackage{luatexbase}
77   \RequirePackage{luaotfload}
78   \RequirePackage{ltxcmds}    [2011/11/09] % v1.22
79   \RequirePackage{pdftexcmds} [2011/11/29] % v0.20
80   \RequirePackage{xkeyval}    [2012/10/14] % v2.6b
81   \RequirePackage{etoolbox}
82   \RequirePackage{lltjp-atbegshi}
83   \IfFileExists{everyhook.sty}{\RequirePackage{everyhook}\ltj@everyhook@availtrue}{}
84 \else                           %<*!LaTeX>
85   \input luatexbase.sty
86   \input luaotfload.sty
87   \input ltxcmds.sty
88   \input pdftexcmds.sty
89   \input xkeyval
90 \fi                             %</LaTeX>
91 \endlinechar=-1 %
92
93 %% Check if LuaTeX is used
94 \ltx@LocalExpandAfter\ifx\csname directlua\endcsname\relax
95   % Loading is silently aborted, since the notice of the wrong
96   % engine is already shown in luaotfload.
97   \ltj@core@AtEnd
98 \expandafter\endinput\fi\relax
99
100 %% Check LuaTeX version.
101 \ifnum\luatexversion<100
102   \@PackageError{luatexja-core}
103     {This LuaTeX engine is too old for this package;\MessageBreak
104      at least 1.0.0 is needed}
105     {Package loading is aborted now.\MessageBreak}
106 \expandafter\endinput\fi\relax
107
108 %% \LuaTeXjaAvailable
109 %! 環境検査にパスした時に定義される.
110 \let\LuaTeXjaAvailable=t %
111
112 %%------------------
113 \endlinechar=13
114
115 %! ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
116 % LuaTeX-ja core TeX source
117
118 \newcount\ltjlineendcomment
119 \ltjlineendcomment="FFFFF
120 \catcode\ltjlineendcomment=14 %"
121
122 \def\ltj@loaded{hoge}
123
124 \newdimen\ltj@dimen@zw
125 \newdimen\ltj@dimen@zh
126 \newdimen\ltj@dimen@jQ \ltj@dimen@jQ=0.25mm
127 \let\jQ=\ltj@dimen@jQ \let\jH=\jQ
128
129 %%%%%%%% error message
130 \def\ltj@defnum{0}
131 \def\ltj@defdimen{0}
132
133 %%%%%%%% Attributes for Japanese typesetting.
134 \newattribute\ltj@icflag    % attribute for italic correction
135 \newattribute\ltj@curjfnt   % index for ``current horizontal Japanese font''
136 \newattribute\ltj@curtfnt   % index for ``current vertical Japanese font''
137 \newattribute\ltj@charclass %
138 \newattribute\ltj@charcode %
139 \newattribute\ltj@autospc   % attribute for autospacing
140 \newattribute\ltj@autoxspc  % attribute for autoxspacing
141 \newattribute\ltj@yablshift % attribute for yalbaselineshift
142 \newattribute\ltj@ykblshift % attribute for yjabaselineshift
143 \newattribute\ltj@tablshift % attribute for talbaselineshift
144 \newattribute\ltj@tkblshift % attribute for tjabaselineshift
145 \newattribute\jfam          % index for current jfam
146 \newattribute\ltj@vert@ori  % <=0: UAX#50, >0: always upright
147
148 \newattribute\ltj@dir
149 \newlanguage\ltj@@japanese
150 \ltj@dir\z@
151 \ltj@icflag\z@
152 \ltj@charclass\z@
153 \ltj@curjfnt\m@ne
154 \ltj@curtfnt\m@ne
155
156 %%%%%%%% Attributes for character ranges
157 \newcount\ltj@tempcnta
158 \ltj@tempcnta\z@
159 \loop\ifnum\ltj@tempcnta<7
160   \expandafter\newattribute%
161     \csname ltj@kcat\the\ltj@tempcnta\endcsname
162   \csname ltj@kcat\the\ltj@tempcnta\endcsname=0
163   \advance\ltj@tempcnta\@ne
164 \repeat
165
166
167 %%%%%%%% Loading luatexja.lua
168 %% Quark tokens needed in ltj-base
169 \protected\def\ltj@@q@escape{\ltj@@q@escape@}
170 \protected\def\ltj@@q@escapenum{\ltj@@q@escapenum@}
171
172 \directlua{%
173   luatexja = {};
174   luatexja.lang_ja = \the\ltj@@japanese;
175   dofile(assert(kpse.find_file('luatexja.lua', 'tex'),
176       "File `luatexja.lua' not found"))
177 }
178
179 %% \RequireLuaTeXjaSubmodule{name}[date]
180 % A TeX version of luatexja.load_module(name)
181 % The second optional argument [date] is simply ignored.
182 \def\RequireLuaTeXjaSubmodule#1{%
183   \@ifnextchar[{\ltj@requirelua@submod{#1}}{\ltj@requirelua@submod{#1}[]}}
184 \def\ltj@requirelua@submod#1[#2]{%
185   \directlua{luatexja.load_module("\luatexluaescapestring{#1}")}}
186
187
188 %%%%%%%% Load all sub-packages.
189 \ifltj@in@latex                 %<*LaTeX>
190   \RequirePackage{ltj-base}
191 \else                           %<*!LaTeX>
192   \input ltj-base.sty %
193 \fi                             %</LaTeX>
194
195 %%%%%%%% \asluastring
196 \def\asluastring#1{'\luatexluaescapestring{\detokenize{#1}}'}
197
198 %%%%%%%% Redefine \/
199 \newluafunction\ltj@@italic@inner
200 \newluafunction\ltj@@start@time@measure@inner
201 \newluafunction\ltj@@stop@time@measure@inner
202 \directlua{
203   local t = lua.get_functions_table()
204   local start_time_measure = luatexja.base.start_time_measure
205   local stop_time_measure = luatexja.base.stop_time_measure
206   local scan_arg = token.scan_argument
207   t[\the\ltj@@italic@inner] = luatexja.jfont.append_italic
208   t[\the\ltj@@start@time@measure@inner] = function()
209     start_time_measure(scan_arg())
210   end
211   t[\the\ltj@@stop@time@measure@inner] = function()
212     stop_time_measure(scan_arg())
213   end
214 }
215 \protected\def\/{\relax\luafunction\ltj@@italic@inner}
216 \luadef\ltj@@start@time@measure\ltj@@start@time@measure@inner
217 \luadef\ltj@@stop@time@measure\ltj@@stop@time@measure@inner
218
219 %%%%%%%% \jfont\CS={...:...;jfm=metric;...}, \globaljfont
220 \protected\def\jfont#1{%
221   \afterassignment\ltj@@jfont\directlua{luatexja.jfont.jfontdefX(false, 'yoko','\luatexluaescapestring{\noexpand#1}')}}
222 \protected\def\globaljfont#1{%
223   \afterassignment\ltj@@jfont\directlua{luatexja.jfont.jfontdefX(true,  'yoko','\luatexluaescapestring{\noexpand#1}')}}
224 \newluafunction\ltj@@jfont@inner
225 \directlua{
226   local t = lua.get_functions_table()
227   t[\the\ltj@@jfont@inner] = luatexja.jfont.jfontdefY
228 }
229 \def\ltj@@jfont{\luafunction\ltj@@jfont@inner}
230
231 %%%%%%%% \tfont\CS={...:...;jfm=metric;...}, \globaltfont
232 \protected\def\tfont#1{%
233   \afterassignment\ltj@@jfont\directlua{luatexja.jfont.jfontdefX(false, 'tate','\luatexluaescapestring{\noexpand#1}')}}
234 \protected\def\globaltfont#1{%
235   \afterassignment\ltj@@jfont\directlua{luatexja.jfont.jfontdefX(true,  'tate','\luatexluaescapestring{\noexpand#1}')}}
236
237 %%%%%%%% \zw, \zh
238 \newluafunction\ltj@zw@inner
239 \newluafunction\ltj@zh@inner
240 \directlua{
241   local t, setdimen, ltjf = lua.get_functions_table(), tex.setdimen, luatexja.jfont
242   t[\the\ltj@zw@inner] = function() setdimen('ltj@dimen@zw', ltjf.get_zw()) end
243   t[\the\ltj@zh@inner] = function() setdimen('ltj@dimen@zh', ltjf.get_zh()) end
244 }
245 \protected\def\ltj@zw{\luafunction\ltj@zw@inner\ltj@dimen@zw}
246 \protected\def\ltj@zh{\luafunction\ltj@zh@inner\ltj@dimen@zh}
247 \let\zw=\ltj@zw
248 \let\zh=\ltj@zh
249
250 %%%%%%%% \inhibitglue
251 \newluafunction\ltj@reset@globaldefs@inner
252 \newluafunction\ltj@restore@globaldefs@inner
253 \newluafunction\ltj@@ihb@inner
254 \newluafunction\ltj@@fakeparbegin@inner
255 \newluafunction\ltj@@fakeboxbdd@inner
256 \directlua{
257   local t = lua.get_functions_table()
258   t[\the\ltj@reset@globaldefs@inner] 
259      = function() luatexja.global_temp = tex.globaldefs; tex.globaldefs = 0 end
260   t[\the\ltj@restore@globaldefs@inner] 
261      = function() tex.globaldefs = luatexja.global_temp end
262   t[\the\ltj@@ihb@inner] = luatexja.jfmglue.create_inhibitglue_node
263   t[\the\ltj@@fakeparbegin@inner] = luatexja.jfmglue.create_beginpar_node
264   t[\the\ltj@@fakeboxbdd@inner] = luatexja.jfmglue.create_boxbdd_node
265 }
266 \protected\luadef\ltj@reset@globaldefs  \ltj@reset@globaldefs@inner
267 \protected\luadef\ltj@restore@globaldefs\ltj@restore@globaldefs@inner
268 \protected\def\inhibitglue{\ifhmode\relax\luafunction\ltj@@ihb@inner\fi}
269 \protected\def\ltjfakeparbegin{\ifhmode\relax\luafunction\ltj@@fakeparbegin@inner\fi}
270 \protected\def\ltjfakeboxbdd{\ifhmode\relax\luafunction\ltj@@fakeboxbdd@inner\fi}
271
272
273 %%%%%%%% \ltjdefcharrange<name>{100-200,3000-,5000,...}
274 \protected\def\ltjdefcharrange#1#2{%
275   {\ltj@tempcntc=#1 \expandafter\ltj@@dcrange#2,,}\ignorespaces}
276 \def\ltj@@dcrange#1,{\def\ltj@temp{#1}%
277   \ifx\ltj@temp\empty\let\@next=\relax\else
278   \ltj@@dcrangeA{#1}\let\@next=\ltj@@dcrange\fi\@next}
279 \def\ltj@@dcrangeA#1{\ltj@@dcrangeB#1--\@nil}
280 \def\ltj@@dcrangeB#1-#2-#3\@nil{\def\ltj@temp{#3}%
281   \ifx\ltj@temp\empty
282     \ltj@tempcnta=#1 \ltj@tempcntb=\ltj@tempcnta
283   \else
284     \def\ltj@temp{#1}%
285     \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
286     \def\ltj@temp{#2}%
287     \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
288   \fi
289   \directlua{luatexja.charrange.add_char_range(\the\ltj@tempcnta,\the\ltj@tempcntb,
290     \the\ltj@tempcntc)}%
291   }
292
293 %%%%%%%% \ltjsetparameter
294 \newcount\ltj@@stack \newcount\ltj@@group@level
295 \ltj@@group@level\z@\ltj@@stack\z@
296
297 \setbox\z@\hbox{%
298   \directlua{% This value depends on the version of LuaTeX.
299     luatexja.stack.hmode = -tex.nest[tex.nest.ptr].mode
300   }%
301 }
302 \setbox\z@\vbox{%
303   \directlua{% This value depends on the version of LuaTeX.
304     luatexja.stack.vmode = -tex.nest[tex.nest.ptr].mode
305   }%
306 }
307 \setbox\z@\box\voidb@x
308
309 % prebreakpenalty = {<char_code>, <penalty>}
310 \define@key[ltj]{japaram}{kcatcode}{\ltj@@set@stack{KCAT}{0}{0x7FFFFFFF}#1\relax}
311 \define@key[ltj]{japaram}{prebreakpenalty}{\ltj@@set@stack{PRE}{-10000}{10000}#1\relax}
312 \define@key[ltj]{japaram}{postbreakpenalty}{\ltj@@set@stack{POST}{-10000}{10000}#1\relax}
313 \def\ltj@@set@stack#1#2#3{%
314   \directlua{luatexja.stack.set_stack_perchar(luatexja.stack_table_index.#1,#2,#3)}}
315 %\def\ltj@@set@stack#1,#2:#3#4#5{%
316 %  \directlua{luatexja.stack.set_stack_perchar(luatexja.stack_table_index.#3,
317 %    \ltj@safe@num@or{nil}{#1},\ltj@safe@num@or{nil}{#2},#4,#5)}}
318
319 % jatextfont = {<char_code>, <font_cs>}
320 % only horizontal font is supported
321 \define@key[ltj]{japaram}{jatextfont}{%
322   \expandafter\ltj@@set@stackfont#1:{MJT}}
323 \define@key[ltj]{japaram}{jascriptfont}{%
324   \expandafter\ltj@@set@stackfont#1:{MJS}}
325 \define@key[ltj]{japaram}{jascriptscriptfont}{%
326   \expandafter\ltj@@set@stackfont#1:{MJSS}}
327 \protected\def\ltj@@set@stackfont#1,#2:#3{%
328   \ltj@reset@globaldefs
329   \ltj@tempcnta=\ltj@curjfnt\ltj@curjfnt\m@ne#2%
330   \ifnum\ltj@curjfnt=\m@ne
331     \@PackageError{luatexja-core}%
332     {\string#2 is not a control sequence that represents \MessageBreak
333      a Japanese font}{}%
334   \else
335     \directlua{luatexja.stack.set_stack_font(luatexja.stack_table_index.#3,
336     \ltj@safe@num@or{nil}{#1}, \the\ltj@curjfnt)}%
337   \fi
338   \ltj@curjfnt=\ltj@tempcnta
339   \ltj@restore@globaldefs
340   }
341
342 % {y,t}{ja,al}baselineshift = <dimen>
343 % yjabaselineshift = <dimen>
344 \define@key[ltj]{japaram}{yalbaselineshift}{%
345   \directlua{tex.setattribute(luatexja.isglobal,
346     luatexbase.attributes['ltj@yablshift'], token.scan_dimen())}#1\relax}
347 \define@key[ltj]{japaram}{yjabaselineshift}{%
348   \directlua{tex.setattribute(luatexja.isglobal,
349     luatexbase.attributes['ltj@ykblshift'], token.scan_dimen())}#1\relax}
350 \define@key[ltj]{japaram}{talbaselineshift}{%
351   \directlua{tex.setattribute(luatexja.isglobal,
352     luatexbase.attributes['ltj@tablshift'], token.scan_dimen())}#1\relax}
353 \define@key[ltj]{japaram}{tjabaselineshift}{%
354   \directlua{tex.setattribute(luatexja.isglobal,
355     luatexbase.attributes['ltj@tkblshift'], token.scan_dimen())}#1\relax}
356
357 % jaxspmode = {<char_code>, <mode>}
358 % mode: inhibit, preonly, postonly, allow
359 %    or       0        1         2      3
360 \define@key[ltj]{japaram}{jaxspmode}{%        \inhibitxspcode
361   \expandafter\ltj@set@jaxspmode#1:\relax}
362 \def\ltj@set@jaxspmode#1,#2:{%
363   \lowercase{\edef\ltj@temp{#2}}%
364   \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
365   \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
366   \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
367   \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
368   \ltj@@set@stack{XSP}03#1 \ltj@temp}
369
370 % alxspmode = {<char_code>, <mode>}
371 % mode: inhibit, preonly, postonly, allow
372 %    or       0        1         2      3
373 \define@key[ltj]{japaram}{alxspmode}{%        \inhibitxspcode
374   \expandafter\ltj@set@jaxspmode#1:\relax}% same code as jaxspmode
375
376 % autospacing = <bool> (default: true)
377 \define@boolkey[ltj]{japaram}{autospacing}[true]{%
378   \ifltj@japaram@autospacing
379     \directlua{luatexja.ext_print_global()}\ltj@autospc\@ne
380   \else
381     \directlua{luatexja.ext_print_global()}\ltj@autospc\z@
382   \fi}
383
384 % autoxspacing = <bool> (default: true)
385 \define@boolkey[ltj]{japaram}{autoxspacing}[true]{%
386   \ifltj@japaram@autoxspacing
387     \directlua{luatexja.ext_print_global()}\ltj@autoxspc\@ne
388   \else
389     \directlua{luatexja.ext_print_global()}\ltj@autoxspc\z@
390   \fi}
391
392 % [x]]kanjiskip = <skip>
393 \define@key[ltj]{japaram}{kanjiskip}{\ltjsetkanjiskip#1}
394 \define@key[ltj]{japaram}{xkanjiskip}{\ltjsetxkanjiskip#1}
395 \newluafunction\ltjsetkanjiskip@inner
396 \newluafunction\ltjsetxkanjiskip@inner
397 \directlua{
398   local t, scan_glue = lua.get_functions_table(), token.scan_glue
399   local sss, sti = luatexja.stack.set_stack_skip, luatexja.stack_table_index
400   t[\the\ltjsetkanjiskip@inner] = function()
401     local d = scan_glue(); sss(sti.KSK, d); node.free(d)
402   end
403   t[\the\ltjsetxkanjiskip@inner] = function()
404     local d = scan_glue(); sss(sti.XSK, d); node.free(d)
405   end
406 }
407 \protected\luadef\ltjsetkanjiskip \ltjsetkanjiskip@inner
408 \protected\luadef\ltjsetxkanjiskip\ltjsetxkanjiskip@inner
409
410 % jcharwidowpenalty = <count>
411 \define@key[ltj]{japaram}{jcharwidowpenalty}{%      %COUNT
412   \ltj@@set@stack{JWP}{-10000}{10000}\z@#1 }
413
414 % differentjfm = { large | small | average | pleft | pright | paverage | both}
415 \define@choicekey*+[ltj]{japaram}{differentjfm}[\ltj@temp\ltj@tempr]%
416   {large,small,average,pleft,pright,paverage,both}{%
417   \ifcase\ltj@tempr
418     \directlua{luatexja.jfmglue.diffmet_rule = math.max}\or
419     \directlua{luatexja.jfmglue.diffmet_rule = math.min}\or
420     \directlua{luatexja.jfmglue.diffmet_rule = math.two_average}\or
421     \directlua{luatexja.jfmglue.diffmet_rule = math.two_pleft}\or
422     \directlua{luatexja.jfmglue.diffmet_rule = math.two_pright}\or
423     \directlua{luatexja.jfmglue.diffmet_rule = math.two_paverage}\or
424     \directlua{luatexja.jfmglue.diffmet_rule = math.two_add}
425   \fi
426 }{\@PackageError{luatexja}{invalid argument `#1' for `differentjfm'}{%
427   Argument for `differentjfm' must be one of `large', `small', `average',^^J%
428   `pleft', `pright', 'paverage' or `both'. }}
429   % large, small, average, pleft, pright, paverage, both
430
431
432 % jacharrange = { +-<range_number> }
433 \define@key[ltj]{japaram}{jacharrange}{\expandafter\ltj@@scrange#1,,}
434 \def\ltj@@scrange#1,{%
435   \edef\ltj@temp{#1}%
436   \ifx\ltj@temp\empty\let\ltj@@next=\relax\else\let\ltj@@next=\ltj@@scrange
437     \directlua{luatexja.charrange.toggle_char_range(%
438       luatexja.is_global,\ltj@safe@num@or{nil}{#1})}%
439   \fi\ltj@@next
440 }
441
442 \protected\def\ltj@setpar@global{%
443   \relax\ifnum\globaldefs>0\directlua{luatexja.isglobal='global'}%
444     \else\directlua{luatexja.isglobal=''}\fi
445 }
446 \protected\def\ltjsetparameter#1{%
447   \ltj@@start@time@measure{set_par}%
448   \ltj@setpar@global\setkeys[ltj]{japaram}{#1}%
449   \ltj@@stop@time@measure{set_par}%
450   \ignorespaces}
451 \protected\def\ltjglobalsetparameter#1{%
452   \relax\ifnum\globaldefs<0\directlua{luatexja.isglobal=''}%
453     \else\directlua{luatexja.isglobal='global'}\fi%
454   \ltj@@start@time@measure{set_par}%
455   \setkeys[ltj]{japaram}{#1}%
456   \ltj@@stop@time@measure{set_par}%
457   \ignorespaces}
458
459 %%%%%%%% \ltjgetparameter
460 \def\ltj@@decl@array@param#1{%
461   \expandafter\let\csname ltj@@array@param/#1\endcsname=t%
462 }
463 \ltj@@decl@array@param{prebreakpenalty}
464 \ltj@@decl@array@param{postbreakpenalty}
465 \ltj@@decl@array@param{jaxspmode}
466 \ltj@@decl@array@param{alxspmode}
467 \ltj@@decl@array@param{kcatcode}
468 \ltj@@decl@array@param{jacharrange}
469 \ltj@@decl@array@param{chartorange}
470 \ltj@@decl@array@param{boxdir}
471 \def\ltjgetparameter#1{%
472   \ltj@@start@time@measure{get_par}%
473   % stop call is moved in luatexja.lua
474   \ifcsname ltj@@array@param/#1\endcsname
475     \expandafter\ltx@firstoftwo
476   \else
477     \expandafter\ltx@secondoftwo
478   \fi
479   {\ltj@@getparam@two{#1}}%
480   {\ltj@@getparam@one{#1}}%
481 }
482 \newluafunction\ltj@@getparam@one@inner
483 \directlua{
484   local t = lua.get_functions_table()
485   t[\the\ltj@@getparam@one@inner] = luatexja.ext_get_parameter_unary
486 }
487 \luadef\ltj@@getparam@one\ltj@@getparam@one@inner
488 \def\ltj@@getparam@two#1#2{%
489   \directlua{luatexja.ext_get_parameter_binary('#1', \ltj@safe@num@or{nil}{#2})}%
490 }
491
492 %%%%%%%% \ltjalchar<number>, \ltjjachar<number>
493 \let\ltj@@origchar=\char
494 \newluafunction\ltj@alljachar@inner
495 \directlua{
496   local t, set_attr = lua.get_functions_table(), tex.setattribute
497   local get_attr, floor = tex.getattribute, math.floor
498   local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
499   local lang_ja = luatexja.lang_ja
500   t[\the\ltj@alljachar@inner] = function()
501     font.current(get_attr(attr_curjfnt)); tex.language=lang_ja;
502     for i=0,6 do
503       set_attr(luatexbase.attributes['ltj@kcat' .. floor(i)],0)
504     end
505   end
506 }
507 \protected\luadef\ltj@alljachar\ltj@alljachar@inner
508 \protected\def\ltj@allalchar{\relax
509     \csname ltj@kcat0\endcsname="7FFFFFFF %"
510     \csname ltj@kcat1\endcsname="7FFFFFFF %"
511     \csname ltj@kcat2\endcsname="7FFFFFFF %"
512     \csname ltj@kcat3\endcsname="7FFFFFFF %"
513     \csname ltj@kcat4\endcsname="7FFFFFFF %"
514     \csname ltj@kcat5\endcsname="7FFFFFFF %"
515     \csname ltj@kcat6\endcsname="7FFFFFFF }%"
516
517 \protected\def\ltjjachar{\afterassignment\ltj@@jachar\ltj@tempcnta}
518 \def\ltj@@jachar{{\directlua{tex.globaldefs=0}\ltj@alljachar\ltj@@origchar\ltj@tempcnta}}
519 \protected\def\ltjalchar{\afterassignment\ltj@@alchar\ltj@tempcnta}
520 \def\ltj@@alchar{{\directlua{tex.globaldefs=0}\ltj@allalchar\ltj@@origchar\ltj@tempcnta}}
521
522 %%%%%%%% \ltjsetmathletter<number>, \ltjunsetmathletter<number>
523 \protected\def\ltjsetmathletter#1{%
524   \directlua{luatexja.math.is_math_letters[token.scan_int()]=true}#1\relax
525 }
526 \protected\def\ltjunsetmathletter#1{%
527   \directlua{luatexja.math.is_math_letters[token.scan_int()]=false}#1\relax
528 }
529
530 %%%%%%%% \ltjdeclarealtfont<base_font_cs><alt_font_cs>{100-200,3000-,5000,...}
531 % <base_font_cs> の 第 3 引数の文字達は <alt_font_cs> で組む
532 % いつも global
533 \def\ltj@@getjfontnumber#1{% result in \ltj@tempcntc
534   \ltj@reset@globaldefs
535   \begingroup\ltj@curjfnt\m@ne\ltj@curtfnt\m@ne\relax
536   #1\directlua{luatexja.__temp = -(\the\ltj@curjfnt)*(\the\ltj@curtfnt)}\endgroup
537   \ltj@tempcntc=\directlua{tex.sprint(luatexja.__temp)}\relax
538   \ifnum\ltj@tempcntc=\m@ne
539     \@PackageError{luatexja-core}%
540     {\string#1 is not a control sequence that represents \MessageBreak
541      a Japanese font}{I'll use the current horizontal Japanese font instead.^^J}%
542     \ltj@tempcntc=\ltj@curjfnt
543   \fi\ltj@restore@globaldefs}
544
545 \protected\def\ltjdeclarealtfont#1#2#3{%
546   {\ltj@@getjfontnumber#1\@tempcnta=\ltj@tempcntc% \@tempcnta: 基底フォント
547    \ltj@@getjfontnumber#2%
548    \expandafter\ltj@@altfont#3,,}\ignorespaces}
549 \def\ltj@@altfont#1,{\def\ltj@temp{#1}%
550   \ifx\ltj@temp\empty\let\@next=\relax\else
551   \ltj@@altfontA{#1}\let\@next=\ltj@@altfont\fi\@next}
552 \def\ltj@@altfontA#1{\ltj@@altfontB#1--\@nil}
553 \def\ltj@@altfontB#1-#2-#3\@nil{\def\ltj@temp{#3}%
554   \ifx\ltj@temp\empty
555     \ltj@tempcnta=#1 \ltj@tempcntb=\ltj@tempcnta
556   \else
557     \def\ltj@temp{#1}%
558     \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
559     \def\ltj@temp{#2}%
560     \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
561   \fi
562   \directlua{luatexja.jfont.set_alt_font(
563     \the\ltj@tempcnta,\the\ltj@tempcntb,
564     \the\ltj@tempcntc, \the\@tempcnta)}%
565   }
566
567 %%%%%%%% \ltjclearaltfont<font_cs>
568 % <font_cs> の文字は全部 <font_cs> 本来の文字で組む
569 \protected\def\ltjclearaltfont#1{%
570   {\ltj@@getjfontnumber#1%
571    \directlua{luatexja.jfont.clear_alt_font(\the\ltj@tempcntc)}}\ignorespaces}
572
573
574 %%%%%%%% \yoko, \tate,\dtou
575 \protected\def\tate{%
576   \directlua{luatexja.direction.set_list_direction(3, 'tate')}%
577 }
578 \protected\def\yoko{%
579   \directlua{luatexja.direction.set_list_direction(4, 'yoko')}%
580 }
581 \protected\def\dtou{%
582   \directlua{luatexja.direction.set_list_direction(1, 'dtou')}%
583 }
584 \protected\def\utod{% 「縦数式ディレクション」
585   \directlua{luatexja.direction.set_list_direction(11, 'utod')}%
586 }
587 \define@key[ltj]{japaram}{direction}{%% always local
588   \directlua{luatexja.direction.set_list_direction(1, 'ltjsetparameter')}%
589 }
590
591 \yoko
592
593 %%%% insert
594 \let\orig@insert=\insert
595 \newluafunction\ltj@@lua@populate@insertwhatsit@inner
596 \directlua{
597   local t = lua.get_functions_table()
598   t[\the\ltj@@lua@populate@insertwhatsit@inner]
599     = luatexja.direction.populate_insertion_dir_whatsit
600 }
601 \luadef\ltj@@lua@populate@insertwhatsit\ltj@@lua@populate@insertwhatsit@inner
602 \protected\def\insert{\begingroup\afterassignment\ltj@@insert@A\ltj@tempcnta}
603 \protected\def\ltj@@insert@A{\orig@insert\ltj@tempcnta\bgroup
604   \aftergroup\ltj@@insert@B\let\ltj@@next}
605 \protected\def\ltj@@insert@B{\ltj@@lua@populate@insertwhatsit\endgroup}
606
607 %%%% vadjust
608 \let\ltj@@orig@vadjust=\vadjust
609 \protected\def\vadjust{%
610   \ltj@@orig@vadjust\bgroup
611   \directlua{luatexja.direction.set_list_direction_hook('adj')}%
612   \aftergroup\ltj@@vadjust@check@dir\let\ltj@@next}
613 \protected\def\ltj@@vadjust@check@dir{%
614   \directlua{luatexja.direction.check_adjust_direction()}%
615 }
616
617 %%%% \hbox, \vbox, \vtop
618 \setbox\z@\hbox{%
619   \primitive\everymath{}%
620   $\directlua{% This value depends on the version of LuaTeX.
621      luatexja.stack.mmode = -tex.nest[tex.nest.ptr].mode
622   }$%
623 }
624 \setbox\z@\box\voidb@x
625 \newluafunction\ltj@@box@dirhook@inner
626 \newluafunction\ltj@@reset@badness@inner
627 \directlua{
628   local t, ltjd = lua.get_functions_table(), luatexja.direction
629   t[\the\ltj@@box@dirhook@inner]
630     = function() ltjd.set_list_direction_hook() end
631   t[\the\ltj@@reset@badness@inner] = function()
632     if luatexja.global_temp then
633       tex.hbadness= tex.hbadness or luatexja.hbadness_temp
634       tex.vbadness= tex.vbadness or luatexja.vbadness_temp
635       tex.globaldefs= luatexja.global_temp or tex.globaldefs
636     end
637   end
638 }
639 \luadef\ltj@@box@dirhook\ltj@@box@dirhook@inner
640 \ifltj@everyhook@avail% using everyhook package
641   \PushPreHook{hbox}{\ltj@@box@dirhook\aftergroup\ltj@@reset@badness}
642   \PushPreHook{vbox}{\ltj@@box@dirhook\aftergroup\ltj@@reset@badness}
643 \else
644   \let\ltj@@everyhbox=\everyhbox\let\ltj@@everyvbox=\everyvbox
645   \newtoks\ltj@@i@everyhbox\newtoks\ltj@@i@everyvbox
646   \let\everyhbox=\ltj@@i@everyhbox\let\everyvbox=\ltj@@i@everyvbox
647   \ltj@@everyhbox{\ltj@@box@dirhook
648     \aftergroup\ltj@@reset@badness\the\ltj@@i@everyhbox}
649   \ltj@@everyvbox{\ltj@@box@dirhook
650     \aftergroup\ltj@@reset@badness\the\ltj@@i@everyvbox}
651 \fi
652
653 \protected\def\ltj@@reset@badness{\luafunction\ltj@@reset@badness@inner}
654
655 %%%% \raise, \lower, \moveleft, \moveright
656 \newluafunction\ltj@@lua@raisebox@inner
657 \directlua{
658   local t = lua.get_functions_table()
659   t[\the\ltj@@lua@raisebox@inner]=luatexja.direction.raise_box
660 }
661 \protected\luadef\ltj@@lua@raisebox\ltj@@lua@raisebox@inner
662 \let\ltj@@orig@raise=\raise
663 \let\ltj@@orig@lower=\lower
664 \protected\def\raise{\ltj@reset@globaldefs\afterassignment\ltj@@raise\ltj@tempdima}
665 \protected\def\lower{\ltj@reset@globaldefs\afterassignment\ltj@@raise\ltj@tempdima-}
666 \protected\def\ltj@@raise{%
667   \ltj@restore@globaldefs
668   \edef\ltj@@raise@a{%
669     \ltj@@lua@raisebox\ltj@@orig@raise\the\ltj@tempdima\noexpand\box\ltj@afbox
670   }\ltj@afterbox\ltj@@raise@a}
671 \let\ltj@@orig@moveleft=\moveleft
672 \let\ltj@@orig@moveright=\moveright
673 \protected\def\moveleft{\ltj@reset@globaldefs\afterassignment\ltj@@moveleft\ltj@tempdima}
674 \protected\def\moveright{\ltj@reset@globaldefs\afterassignment\ltj@@moveleft\ltj@tempdima-}
675 \protected\def\ltj@@moveleft{%
676   \ltj@restore@globaldefs
677   \edef\ltj@@moveleft@a{%
678     \ltj@@lua@raisebox\ltj@@orig@moveleft\the\ltj@tempdima\noexpand\box\ltj@afbox
679   }\ltj@afterbox\ltj@@moveleft@a}
680
681 %%%%\vsplit
682 \let\ltj@@orig@vsplit=\vsplit
683 \protected\def\vsplit{\directlua{luatexja.direction.vsplit()}}
684
685 %%%% \vcenter
686 \let\ltj@@orig@vcenter=\vcenter
687 \protected\def\vcenter{\ltj@afterbox\ltj@@vcenter\vbox}
688 \protected\def\ltj@@vcenter{%
689   \ltj@@lua@raisebox\ltj@@orig@vcenter{\box\ltj@afbox}%
690 }
691
692 %%%% \shipout
693 \newluafunction\ltj@@lua@shipout@inner
694 \directlua{
695   local t = lua.get_functions_table()
696   t[\the\ltj@@lua@shipout@inner]=luatexja.direction.shipout
697 }
698 \protected\luadef\ltj@@lua@shipout\ltj@@lua@shipout@inner
699 \protected\def\ltj@shipout{\ltj@afterbox\ltj@@lua@shipout}
700 \ifprimitive\shipout\let\shipout\ltj@shipout\fi
701 \ifcsname tex_shipout:D\endcsname
702   \expandafter\let\csname tex_shipout:D\endcsname\ltj@shipout
703 \fi
704 \ifdefined\AtBeginShipoutOriginalShipout
705   \let\AtBeginShipoutOriginalShipout\ltj@shipout
706 \fi
707 %%%% \unhbox and \unvbox
708 \newluafunction\ltj@@lua@unboxcheckdir@inner
709 \directlua{
710   local t = lua.get_functions_table()
711   t[\the\ltj@@lua@unboxcheckdir@inner]=luatexja.direction.unbox_check_dir
712 }
713 \luadef\ltj@@lua@unboxcheckdir\ltj@@lua@unboxcheckdir@inner
714 \long\def \@gobbletwo #1#2{}
715 \let\ltj@@orig@unhbox=\unhbox
716 \let\ltj@@orig@unvbox=\unvbox
717 \protected\def\unhbox{\ltj@reset@globaldefs\afterassignment\ltj@@unhbox\ltj@tempcnta}
718 \protected\def\unvbox{\ltj@reset@globaldefs\afterassignment\ltj@@unvbox\ltj@tempcnta}
719 \protected\def\ltj@@unhbox{%
720   \ltj@@lua@unboxcheckdir\ltj@@orig@unhbox\ltj@tempcnta}
721 \protected\def\ltj@@unvbox{%
722   \ltj@@lua@unboxcheckdir\ltj@@orig@unvbox\ltj@tempcnta}
723 \let\ltj@@orig@unhcopy=\unhcopy
724 \let\ltj@@orig@unvcopy=\unvcopy
725 \protected\def\unhcopy{\ltj@reset@globaldefs\afterassignment\ltj@@unhcopy\ltj@tempcnta}
726 \protected\def\unvcopy{\ltj@reset@globaldefs\afterassignment\ltj@@unvcopy\ltj@tempcnta}
727 \protected\def\ltj@@unhcopy{%
728   \directlua{luatexja.direction.unbox_check_dir(true)}%
729   \ltj@@orig@unhcopy\ltj@tempcnta
730   \directlua{luatexja.direction.uncopy_restore_whatsit()}}
731 \protected\def\ltj@@unvcopy{%
732   \directlua{luatexja.direction.unbox_check_dir(true)}%
733   \ltj@@orig@unvcopy\ltj@tempcnta
734   \directlua{luatexja.direction.uncopy_restore_whatsit()}}
735
736 %%%% lastbox
737 \let\ltj@@orig@lastbox=\lastbox
738 \protected\def\lastbox{%
739   \directlua{luatexja.direction.lastbox_hook()}\ltj@@orig@lastbox
740 }
741
742 %%%%%%%% \ltjgetwd<box_num> etc.
743 \def\ltj@@getwd{%
744   \directlua{luatexja.direction.get_box_dim('width', token.scan_int())}}
745 \def\ltj@@getht{%
746   \directlua{luatexja.direction.get_box_dim('height', token.scan_int())}}
747 \def\ltj@@getdp{%
748   \directlua{luatexja.direction.get_box_dim('depth', token.scan_int())}}
749 \let\ltjgetwd=\ltj@@getwd\let\ltjgetht=\ltj@@getht\let\ltjgetdp=\ltj@@getdp
750
751 %%%%%%%% \ltjsetwd<box_num>=<dimen> etc.
752 \protected\def\ltjsetwd{\directlua{luatexja.direction.set_box_dim('width')}}
753 \protected\def\ltjsetht{\directlua{luatexja.direction.set_box_dim('height')}}
754 \protected\def\ltjsetdp{\directlua{luatexja.direction.set_box_dim('depth')}}
755
756
757 %%------------------ Load module for the format
758
759 \directlua{luatexja.load_kinsoku()}
760 \ifltj@in@latex                 %<*LaTeX>
761   \RequirePackage{ltj-latex}
762 \else\ifltj@in@plain            %<*plain>
763   \input ltj-plain.sty %
764 \fi\fi                          %</LaTeX|plain>
765
766 \newcatcodetable\ltj@temp@cctb
767 \def\ltj@overwrite@catcodetable#1{%
768   \setluatexcatcodetable\ltj@temp@cctb{\luatexcatcodetable#1}%
769   \setluatexcatcodetable#1{\luatexcatcodetable\ltj@temp@cctb\catcode\ltjlineendcomment=14}%"
770 }
771 \ltj@overwrite@catcodetable\CatcodeTableLaTeX
772 \ltj@overwrite@catcodetable\CatcodeTableLaTeXAtLetter
773 \ltj@overwrite@catcodetable\CatcodeTableExpl
774
775
776 %%------------------ all done
777 \ltj@core@AtEnd
778 \endinput
779 %% EOF