OSDN Git Service

Fixed ticket #26398, and whatsits from LuaTeX-ja stack system and \inhibitglue are...
[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}[2011/04/01 v0.1]
69 \fi                             %</LaTeX>
70
71 %% Load prerequisite packages.
72 %! 参考: W32TeX の LuaTeX 
73 %!     2010/11/27 v0.65.0 r3988 (LuaTeX-ja の最低要件)
74 %!     2010/07/10 v0.60.2 r3735 (TL2010 とほぼ同じ)
75 \endlinechar=13 %
76 %! 外部パッケージは普通の \endlinechar で読まないと危険.
77 \ifltj@in@latex                 %<*LaTeX>
78   \RequirePackage{luaotfload} [2010/11/11] % v1.21
79   \RequirePackage{infwarerr}  [2010/04/08] % v1.3
80   \RequirePackage{ltxcmds}    [2010/04/26] % v1.7
81   \RequirePackage{pdftexcmds} [2010/04/01] % v0.9
82   \RequirePackage{xkeyval}    [2008/08/13] % v2.6a
83 \else                           %<*!LaTeX>
84   \input luaotfload.sty
85   \input infwarerr.sty
86   \input ltxcmds.sty
87   \input pdftexcmds.sty
88   \input xkeyval
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<65
101   \@PackageError{luatexja-core}
102     {This LuaTeX engine is too old for this package;\MessageBreak
103      at least v0.65 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
117 %% Load all sub-packages.
118 \ifltj@in@latex                 %<*LaTeX>
119   \RequirePackage{luatexja-cctbreg}[2011/04/01]
120   \RequirePackage{luatexja-base}[2011/04/01]
121 \else                           %<*!LaTeX>
122   \input luatexja-cctbreg.sty %
123   \input luatexja-base.sty %
124 \fi                             %</LaTeX>
125
126 %%------------------
127 \endlinechar=13
128 %! この中に master の luatexja-core.sty の内容を挿入する
129 %! ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
130 % LuaTeX-ja core TeX source
131
132 \catcode"FFFFF=14 %"
133
134 \def\ltj@loaded{hoge}
135
136 \newcount\ltj@tempcnta
137 \newcount\ltj@tempcntb
138 \newcount\ltj@tempcntc
139 \newdimen\ltj@tempdima
140 \newskip\ltj@tempskipa
141
142 \newdimen\ltj@zw
143 \newdimen\ltj@zh
144 \newdimen\jQ \jQ=0.25mm
145 \newdimen\jH \jH=0.25mm
146
147 %%%%%%%% error message
148 \def\ltj@defnum{0}
149 \def\ltj@defdimen{0}
150
151
152 %%%%%%%% Attributes for Japanese typesetting.
153 \newluatexattribute\ltj@curjfnt   % index for ``current Japanese font''
154 \newluatexattribute\jfam          % index for current jfam
155 \newluatexattribute\ltj@charclass % 
156 \newluatexattribute\ltj@yablshift % attribute for \yabaselineshift
157 \newluatexattribute\ltj@ykblshift % attribute for \ykbaselineshift
158 \newluatexattribute\ltj@autospc   % attribute for autospacing
159 \newluatexattribute\ltj@autoxspc  % attribute for autoxspacing
160 \newluatexattribute\ltj@icflag    % attribute for italic correction
161 \newlanguage\ltj@japanese
162
163 %%%%%%%% Loading lua files
164 \directlua{%
165   utf = unicode.utf8
166   ltj = {}
167   function ltj.loadlua(file)
168     local path = assert(kpse.find_file(file, 'tex'),
169       "File '"..file.."' not found")
170     texio.write_nl("("..path..")")
171     dofile(path)
172   end
173   ltj.loadlua('luatexja-core.lua')
174 }
175
176
177 %%%%%%%% Attributes for character ranges
178 \ltj@tempcnta=0
179 \loop\ifnum\ltj@tempcnta<7
180   \expandafter\newluatexattribute%
181     \csname ltj@kcat\the\ltj@tempcnta\endcsname
182   \csname ltj@kcat\the\ltj@tempcnta\endcsname=0
183   \advance\ltj@tempcnta by1
184 \repeat
185
186 %%%%%%%% \asluastring
187 \def\asluastring#1{'\luaescapestring{\detokenize{#1}}'}
188
189 %%%%%%%% Redefine \/
190 %\let\ltj@ic=\/ \protected\def\/{{\ltj@icflag=1\ltj@ic}}
191 \protected\def\/{\directlua{luatexja.jfont.append_italic()}}
192
193 %%%%%%%% \jfont\CS={...:...;jfm=metric;...}, \globaljfont
194 \protected\def\jfont{\afterassignment\ltj@@jfont\directlua{luatexja.jfont.jfontdefX(false)}}
195 \protected\def\globaljfont{%
196   \afterassignment\ltj@@jfont\directlua{luatexja.jfont.jfontdefX(true)}}
197 \def\ltj@@jfont{\directlua{luatexja.jfont.jfontdefY()}}
198
199 \def\zw{\directlua{luatexja.jfont.load_zw()}\ltj@zw}
200 \def\zh{\directlua{luatexja.jfont.load_zh()}\ltj@zh}
201
202 %%%%%%%% \inhibitglue
203 \protected\def\inhibitglue{\directlua{luatexja.jfmglue.create_inhibitglue_node()}}
204
205 %%%%%%%% \yoko, \tate, \ifydir, \iftdir
206 %% At this moment, tategaki mode is not implemented; 
207 %% there is only yokogaki mode now.
208 \let\yoko\relax
209 \let\tate\relax
210 \newif\ifydir\ydirtrue
211 \newif\iftdir\tdirfalse
212
213 %%%%%%%% \ltjdefcharrange<name>{100-200,3000-,5000,...}
214 \protected\def\ltjdefcharrange#1#2{%
215   {\ltj@tempcntc=#1 \expandafter\ltj@@dcrange#2,,}\ignorespaces}
216 \def\ltj@@dcrange#1,{\def\ltj@temp{#1}%
217   \ifx\ltj@temp\empty\let\@next=\relax\else
218   \ltj@@dcrangeA{#1}\let\@next=\ltj@@dcrange\fi\@next}
219 \def\ltj@@dcrangeA#1{\ltj@@dcrangeB#1--\@nil}
220 \def\ltj@@dcrangeB#1-#2-#3\@nil{\def\ltj@temp{#3}%
221   \ifx\ltj@temp\empty
222     \ltj@tempcnta=#1 \ltj@tempcntb=\ltj@tempcnta
223   \else
224     \def\ltj@temp{#1}%
225     \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
226     \def\ltj@temp{#2}%
227     \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
228   \fi
229   \directlua{luatexja.charrange.add_char_range(\the\ltj@tempcnta,\the\ltj@tempcntb,
230     \the\ltj@tempcntc)}%
231   }
232
233 %%%%%%%% \ltjsetparameter
234 \newcount\ltj@@stack \newcount\ltj@@group@level
235 \ltj@@group@level=0 \ltj@@stack=0
236
237 % prebreakpenalty = {<char_code>, <penalty>}
238 \define@key[ltj]{japaram}{kcatcode}{%
239   \expandafter\ltj@@set@stack#1:{kcat}{0}{0x7FFFFFFF}}
240 \define@key[ltj]{japaram}{prebreakpenalty}{%
241   \expandafter\ltj@@set@stack#1:{pre}{-10000}{10000}}
242 \define@key[ltj]{japaram}{postbreakpenalty}{%
243   \expandafter\ltj@@set@stack#1:{post}{-10000}{10000}}
244 \def\ltj@@set@stack#1,#2:#3#4#5{%
245   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, \asluastring{#3},
246     \ltj@safe@num@or\ltj@defnum{#1},\ltj@safe@num@or\ltj@defnum{#2},#4,#5)}}
247
248 % jatextfont = {<char_code>, <font_cs>}
249 \define@key[ltj]{japaram}{jatextfont}{%
250   \expandafter\ltj@@set@stackfont#1:{mjtext}}
251 \define@key[ltj]{japaram}{jascriptfont}{%
252   \expandafter\ltj@@set@stackfont#1:{mjscr}}
253 \define@key[ltj]{japaram}{jascriptscriptfont}{%
254   \expandafter\ltj@@set@stackfont#1:{mjss}}
255 \def\ltj@@set@stackfont#1,#2:#3{%
256   \directlua{%
257     ltj.global_temp = tex.globaldefs; 
258     tex.globaldefs = 0}%
259   \ltj@tempcnta=\ltj@curjfnt\ltj@curjfnt=-1 #2%
260   \ifnum\ltj@curjfnt=-1
261     \@PackageError{luatexja-core}%
262     {\string#2 is not a control sequence that represents \MessageBreak
263      a Japanese font}{}%
264   \else
265     \directlua{luatexja.stack.set_stack_font(ltj.isglobal, \asluastring{#3},
266     \ltj@safe@num@or\ltj@defnum{#1}, \the\ltj@curjfnt)}%
267   \fi
268   \ltj@curjfnt=\ltj@tempcnta
269   \directlua{tex.globaldefs = ltj.global_temp}%
270   }
271
272 % yalbaselineshift = <dimen>
273 % yjabaselineshift = <dimen>
274 \define@key[ltj]{japaram}{yalbaselineshift}{%
275   \directlua{tex.setattribute(ltj.isglobal, 
276     luatexbase.attributes['ltj@yablshift'],
277     \ltj@safe@dimen@or\ltj@defdimen{#1})}}
278 \define@key[ltj]{japaram}{yjabaselineshift}{%
279   \directlua{tex.setattribute(ltj.isglobal, 
280     luatexbase.attributes['ltj@ykblshift'],
281     \ltj@safe@dimen@or\ltj@defdimen{#1})}}
282
283 % jaxspmode = {<char_code>, <mode>}
284 % mode: inhibit, preonly, postonly, allow
285 %    or       0        2         1      3
286 \define@key[ltj]{japaram}{jaxspmode}{%        \inhibitxspcode
287   \expandafter\ltj@set@jaxspmode#1:\relax}
288 \def\ltj@set@jaxspmode#1,#2:{%
289   \lowercase{\edef\ltj@temp{#2}}%
290   \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
291   \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
292   \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
293   \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
294   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, 'xsp', 
295     \ltj@safe@num@or\ltj@defnum{#1},
296     \ltj@safe@num@or\ltj@defnum{\ltj@temp},0,3)}}
297
298 % alxspmode = {<char_code>, <mode>}
299 % mode: inhibit, preonly, postonly, allow
300 %    or       0        1         2      3
301 \define@key[ltj]{japaram}{alxspmode}{%        \inhibitxspcode
302   \expandafter\ltj@set@alxspmode#1:\relax}
303 \def\ltj@set@alxspmode#1,#2:{%
304   \lowercase{\edef\ltj@temp{#2}}%
305   \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
306   \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
307   \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
308   \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
309   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, 'xsp', 
310     \ltj@safe@num@or\ltj@defnum{#1},
311     \ltj@safe@num@or\ltj@defnum{\ltj@temp},0,3)}}
312
313 % autospacing = <bool> (default: true)
314 \define@boolkey[ltj]{japaram}{autospacing}[true]{%
315   \ifltj@japaram@autospacing
316     \directlua{ltj.ext_print_global()}\ltj@autospc=1
317   \else
318     \directlua{ltj.ext_print_global()}\ltj@autospc=0
319   \fi}
320
321 % autoxspacing = <bool> (default: true)
322 \define@boolkey[ltj]{japaram}{autoxspacing}[true]{%
323   \ifltj@japaram@autoxspacing
324     \directlua{ltj.ext_print_global()}\ltj@autoxspc=1
325   \else
326     \directlua{ltj.ext_print_global()}\ltj@autoxspc=0
327   \fi}
328
329 % [x]]kanjiskip = <dimen>
330 \define@key[ltj]{japaram}{kanjiskip}{%              % SKIP
331   \directlua{luatexja.stack.set_stack_skip(ltj.isglobal, 'kanjiskip', \ltj@safe@glue{#1})}}
332 \define@key[ltj]{japaram}{xkanjiskip}{%             % SKIP
333   \directlua{luatexja.stack.set_stack_skip(ltj.isglobal, 'xkanjiskip', \ltj@safe@glue{#1})}}
334
335 % jcharwidowpenalty = <count>
336 \define@key[ltj]{japaram}{jcharwidowpenalty}{%      %COUNT
337   \ltj@@set@stack0,#1:{jwp}{-10000}{10000}}
338
339 % differentjfm = { large | small | average | both }
340 \define@choicekey*+[ltj]{japaram}{differentjfm}[\ltj@temp\ltj@tempr]%
341   {large,small,average,both}{%
342   \ifcase\ltj@tempr
343     \directlua{luatexja.jfmglue.diffmet_rule = math.max}\or
344     \directlua{luatexja.jfmglue.diffmet_rule = math.min}\or
345     \directlua{luatexja.jfmglue.diffmet_rule = math.two_average}\or
346     \directlua{luatexja.jfmglue.diffmet_rule = math.two_add}
347   \fi
348 }{\ltj@PackageError{luatexja}{invalid argument `#1' for `differentjfm'}{%
349   Argument for `differentjfm' must be one of `large', `small', `average',^^J%
350   and `both'. }}
351   % large, small, average, both
352
353
354 % jacharrange = { +-<range_number> }
355 \define@key[ltj]{japaram}{jacharrange}{\expandafter\ltj@@scrange#1,,}
356 \def\ltj@@scrange#1,{%
357   \edef\ltj@temp{#1}%
358   \ifx\ltj@temp\empty\let\next=\relax\else\let\next=\ltj@@scrange
359     \directlua{luatexja.charrange.toggle_char_range(%
360       ltj.is_global,\ltj@safe@num@or\ltj@defnum{#1})}%
361   \fi\next
362 }
363
364 \protected\def\ltjsetparameter#1{%
365   \ifnum\globaldefs>0\directlua{ltj.isglobal='global'}\else\directlua{ltj.isglobal=''}\fi
366   \setkeys[ltj]{japaram}{#1}\ignorespaces}
367 \protected\def\ltjglobalsetparameter#1{%
368   \ifnum\globaldefs<0\directlua{ltj.isglobal=''}\else\directlua{ltj.isglobal='global'}\fi
369   \setkeys[ltj]{japaram}{#1}\ignorespaces}
370
371 %%%%%%%% 
372 \def\ltj@@decl@array@param#1{%
373   \expandafter\let\csname ltj@@array@param/#1\endcsname=t%
374 }
375 \ltj@@decl@array@param{prebreakpenalty}
376 \ltj@@decl@array@param{postbreakpenalty}
377 \ltj@@decl@array@param{jaxspmode}
378 \ltj@@decl@array@param{alxspmode}
379 \ltj@@decl@array@param{kcatcode}
380 \ltj@@decl@array@param{jacharrange}
381 \ltj@@decl@array@param{chartorange}
382 \def\ltjgetparameter#1{%
383   \ifcsname ltj@@array@param/#1\endcsname
384     \expandafter\ltx@firstoftwo
385   \else
386     \expandafter\ltx@secondoftwo
387   \fi
388   {\ltj@@getparam@two{#1}}%
389   {\ltj@@getparam@one{#1}}%
390 }
391 \def\ltj@@getparam@one#1{%
392   \directlua{ltj.ext_get_parameter_unary('#1')}}%
393 \def\ltj@@getparam@two#1#2{%
394   \directlua{ltj.ext_get_parameter_binary('#1', \ltj@safe@num{#2})}%
395 }
396
397
398
399 %\def\ltj@temp{plain}
400 %\ifx\fmtname\ltj@temp
401 %  \message{plain format: loading luatexja-plain.tex}
402 %\fi
403 %\input luatexja-plain.tex
404 %\catcode`\@=12
405 %\endinput
406 %! ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
407
408 %%------------------ Load module for the format
409
410 \ifltj@in@latex                 %<*LaTeX>
411   \RequirePackage{luatexja-latex}[2011/04/01]
412 \else\ifltj@in@plain            %<*plain>
413   \input luatexja-plain.sty %
414 \fi\fi                          %</LaTeX|plain>
415
416 %%------------------ all done
417 \ltj@core@AtEnd
418 \endinput
419 %% EOF