OSDN Git Service

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