OSDN Git Service

Fix ticket 26455.
[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 \newtoks\ltj@temptoks
142
143 \newdimen\ltj@zw
144 \newdimen\ltj@zh
145 \newdimen\jQ \jQ=0.25mm
146 \let\jH=\jQ
147
148 %%%%%%%% error message
149 \def\ltj@defnum{0}
150 \def\ltj@defdimen{0}
151
152
153 %%%%%%%% Attributes for Japanese typesetting.
154 \newluatexattribute\ltj@curjfnt   % index for ``current Japanese font''
155 \newluatexattribute\jfam          % index for current jfam
156 \newluatexattribute\ltj@charclass % 
157 \newluatexattribute\ltj@yablshift % attribute for \yabaselineshift
158 \newluatexattribute\ltj@ykblshift % attribute for \ykbaselineshift
159 \newluatexattribute\ltj@autospc   % attribute for autospacing
160 \newluatexattribute\ltj@autoxspc  % attribute for autoxspacing
161 \newluatexattribute\ltj@icflag    % attribute for italic correction
162 \newlanguage\ltj@japanese
163
164 %%%%%%%% Loading lua files
165 \directlua{%
166   utf = unicode.utf8
167   ltj = {}
168   function ltj.loadlua(file)
169     local path = assert(kpse.find_file(file, 'tex'),
170       "File '"..file.."' not found")
171     texio.write_nl("("..path..")")
172     dofile(path)
173   end
174   ltj.loadlua('luatexja-core.lua')
175 }
176
177
178 %%%%%%%% Attributes for character ranges
179 \ltj@tempcnta=0
180 \loop\ifnum\ltj@tempcnta<7
181   \expandafter\newluatexattribute%
182     \csname ltj@kcat\the\ltj@tempcnta\endcsname
183   \csname ltj@kcat\the\ltj@tempcnta\endcsname=0
184   \advance\ltj@tempcnta by1
185 \repeat
186
187 %%%%%%%% \asluastring
188 \def\asluastring#1{'\luaescapestring{\detokenize{#1}}'}
189
190 %%%%%%%% Redefine \/
191 %\let\ltj@ic=\/ \protected\def\/{{\ltj@icflag=1\ltj@ic}}
192 \protected\def\/{\directlua{luatexja.jfont.append_italic()}}
193
194 %%%%%%%% \jfont\CS={...:...;jfm=metric;...}, \globaljfont
195 \protected\def\jfont{\afterassignment\ltj@@jfont\directlua{luatexja.jfont.jfontdefX(false)}}
196 \protected\def\globaljfont{%
197   \afterassignment\ltj@@jfont\directlua{luatexja.jfont.jfontdefX(true)}}
198 \def\ltj@@jfont{\directlua{luatexja.jfont.jfontdefY()}}
199
200 \def\zw{\directlua{luatexja.jfont.load_zw()}\ltj@zw}
201 \def\zh{\directlua{luatexja.jfont.load_zh()}\ltj@zh}
202
203 %%%%%%%% \inhibitglue
204 \let\new@everypar=\everypar
205 \protected\def\inhibitglue{%
206   \ifvmode
207   \directlua{ltj.global_temp = tex.globaldefs; tex.globaldefs = 0}% trick
208     \xdef\@@tempa{\the\everypar}%
209     \xdef\@@tempb{\everypar={\@@tempa}\@@tempa\noexpand\ltj@@ihb}%
210     \everypar\expandafter{\@@tempb}%
211     \directlua{tex.globaldefs = ltj.global_temp}% trick
212   \else\ifhmode\ltj@@ihb\fi\fi}
213 \def\ltj@@ihb{\directlua{luatexja.jfmglue.create_inhibitglue_node()}}
214
215 %%%%%%%% \yoko, \tate, \ifydir, \iftdir
216 %% At this moment, tategaki mode is not implemented; 
217 %% there is only yokogaki mode now.
218 \let\yoko\relax
219 \let\tate\relax
220 \newif\ifydir\ydirtrue
221 \newif\iftdir\tdirfalse
222
223 %%%%%%%% \ltjdefcharrange<name>{100-200,3000-,5000,...}
224 \protected\def\ltjdefcharrange#1#2{%
225   {\ltj@tempcntc=#1 \expandafter\ltj@@dcrange#2,,}\ignorespaces}
226 \def\ltj@@dcrange#1,{\def\ltj@temp{#1}%
227   \ifx\ltj@temp\empty\let\@next=\relax\else
228   \ltj@@dcrangeA{#1}\let\@next=\ltj@@dcrange\fi\@next}
229 \def\ltj@@dcrangeA#1{\ltj@@dcrangeB#1--\@nil}
230 \def\ltj@@dcrangeB#1-#2-#3\@nil{\def\ltj@temp{#3}%
231   \ifx\ltj@temp\empty
232     \ltj@tempcnta=#1 \ltj@tempcntb=\ltj@tempcnta
233   \else
234     \def\ltj@temp{#1}%
235     \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
236     \def\ltj@temp{#2}%
237     \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
238   \fi
239   \directlua{luatexja.charrange.add_char_range(\the\ltj@tempcnta,\the\ltj@tempcntb,
240     \the\ltj@tempcntc)}%
241   }
242
243 %%%%%%%% \ltjsetparameter
244 \newcount\ltj@@stack \newcount\ltj@@group@level
245 \ltj@@group@level=0 \ltj@@stack=0
246
247 % prebreakpenalty = {<char_code>, <penalty>}
248 \define@key[ltj]{japaram}{kcatcode}{%
249   \expandafter\ltj@@set@stack#1:{kcat}{0}{0x7FFFFFFF}}
250 \define@key[ltj]{japaram}{prebreakpenalty}{%
251   \expandafter\ltj@@set@stack#1:{pre}{-10000}{10000}}
252 \define@key[ltj]{japaram}{postbreakpenalty}{%
253   \expandafter\ltj@@set@stack#1:{post}{-10000}{10000}}
254 \def\ltj@@set@stack#1,#2:#3#4#5{%
255   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, \asluastring{#3},
256     \ltj@safe@num@or\ltj@defnum{#1},\ltj@safe@num@or\ltj@defnum{#2},#4,#5)}}
257
258 % jatextfont = {<char_code>, <font_cs>}
259 \define@key[ltj]{japaram}{jatextfont}{%
260   \expandafter\ltj@@set@stackfont#1:{mjtext}}
261 \define@key[ltj]{japaram}{jascriptfont}{%
262   \expandafter\ltj@@set@stackfont#1:{mjscr}}
263 \define@key[ltj]{japaram}{jascriptscriptfont}{%
264   \expandafter\ltj@@set@stackfont#1:{mjss}}
265 \def\ltj@@set@stackfont#1,#2:#3{%
266   \directlua{%
267     ltj.global_temp = tex.globaldefs; 
268     tex.globaldefs = 0}%
269   \ltj@tempcnta=\ltj@curjfnt\ltj@curjfnt=-1 #2%
270   \ifnum\ltj@curjfnt=-1
271     \@PackageError{luatexja-core}%
272     {\string#2 is not a control sequence that represents \MessageBreak
273      a Japanese font}{}%
274   \else
275     \directlua{luatexja.stack.set_stack_font(ltj.isglobal, \asluastring{#3},
276     \ltj@safe@num@or\ltj@defnum{#1}, \the\ltj@curjfnt)}%
277   \fi
278   \ltj@curjfnt=\ltj@tempcnta
279   \directlua{tex.globaldefs = ltj.global_temp}%
280   }
281
282 % yalbaselineshift = <dimen>
283 % yjabaselineshift = <dimen>
284 \define@key[ltj]{japaram}{yalbaselineshift}{%
285   \directlua{tex.setattribute(ltj.isglobal, 
286     luatexbase.attributes['ltj@yablshift'],
287     \ltj@safe@dimen@or\ltj@defdimen{#1})}}
288 \define@key[ltj]{japaram}{yjabaselineshift}{%
289   \directlua{tex.setattribute(ltj.isglobal, 
290     luatexbase.attributes['ltj@ykblshift'],
291     \ltj@safe@dimen@or\ltj@defdimen{#1})}}
292
293 % jaxspmode = {<char_code>, <mode>}
294 % mode: inhibit, preonly, postonly, allow
295 %    or       0        2         1      3
296 \define@key[ltj]{japaram}{jaxspmode}{%        \inhibitxspcode
297   \expandafter\ltj@set@jaxspmode#1:\relax}
298 \def\ltj@set@jaxspmode#1,#2:{%
299   \lowercase{\edef\ltj@temp{#2}}%
300   \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
301   \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
302   \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
303   \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
304   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, 'xsp', 
305     \ltj@safe@num@or\ltj@defnum{#1},
306     \ltj@safe@num@or\ltj@defnum{\ltj@temp},0,3)}}
307
308 % alxspmode = {<char_code>, <mode>}
309 % mode: inhibit, preonly, postonly, allow
310 %    or       0        1         2      3
311 \define@key[ltj]{japaram}{alxspmode}{%        \inhibitxspcode
312   \expandafter\ltj@set@alxspmode#1:\relax}
313 \def\ltj@set@alxspmode#1,#2:{%
314   \lowercase{\edef\ltj@temp{#2}}%
315   \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
316   \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
317   \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
318   \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
319   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, 'xsp', 
320     \ltj@safe@num@or\ltj@defnum{#1},
321     \ltj@safe@num@or\ltj@defnum{\ltj@temp},0,3)}}
322
323 % autospacing = <bool> (default: true)
324 \define@boolkey[ltj]{japaram}{autospacing}[true]{%
325   \ifltj@japaram@autospacing
326     \directlua{ltj.ext_print_global()}\ltj@autospc=1
327   \else
328     \directlua{ltj.ext_print_global()}\ltj@autospc=0
329   \fi}
330
331 % autoxspacing = <bool> (default: true)
332 \define@boolkey[ltj]{japaram}{autoxspacing}[true]{%
333   \ifltj@japaram@autoxspacing
334     \directlua{ltj.ext_print_global()}\ltj@autoxspc=1
335   \else
336     \directlua{ltj.ext_print_global()}\ltj@autoxspc=0
337   \fi}
338
339 % [x]]kanjiskip = <dimen>
340 \define@key[ltj]{japaram}{kanjiskip}{%              % SKIP
341   \directlua{luatexja.stack.set_stack_skip(ltj.isglobal, 'kanjiskip', \ltj@safe@glue{#1})}}
342 \define@key[ltj]{japaram}{xkanjiskip}{%             % SKIP
343   \directlua{luatexja.stack.set_stack_skip(ltj.isglobal, 'xkanjiskip', \ltj@safe@glue{#1})}}
344
345 % jcharwidowpenalty = <count>
346 \define@key[ltj]{japaram}{jcharwidowpenalty}{%      %COUNT
347   \ltj@@set@stack0,#1:{jwp}{-10000}{10000}}
348
349 % differentjfm = { large | small | average | both }
350 \define@choicekey*+[ltj]{japaram}{differentjfm}[\ltj@temp\ltj@tempr]%
351   {large,small,average,both}{%
352   \ifcase\ltj@tempr
353     \directlua{luatexja.jfmglue.diffmet_rule = math.max}\or
354     \directlua{luatexja.jfmglue.diffmet_rule = math.min}\or
355     \directlua{luatexja.jfmglue.diffmet_rule = math.two_average}\or
356     \directlua{luatexja.jfmglue.diffmet_rule = math.two_add}
357   \fi
358 }{\ltj@PackageError{luatexja}{invalid argument `#1' for `differentjfm'}{%
359   Argument for `differentjfm' must be one of `large', `small', `average',^^J%
360   and `both'. }}
361   % large, small, average, both
362
363
364 % jacharrange = { +-<range_number> }
365 \define@key[ltj]{japaram}{jacharrange}{\expandafter\ltj@@scrange#1,,}
366 \def\ltj@@scrange#1,{%
367   \edef\ltj@temp{#1}%
368   \ifx\ltj@temp\empty\let\next=\relax\else\let\next=\ltj@@scrange
369     \directlua{luatexja.charrange.toggle_char_range(%
370       ltj.is_global,\ltj@safe@num@or\ltj@defnum{#1})}%
371   \fi\next
372 }
373
374 \protected\def\ltjsetparameter#1{%
375   \ifnum\globaldefs>0\directlua{ltj.isglobal='global'}\else\directlua{ltj.isglobal=''}\fi
376   \setkeys[ltj]{japaram}{#1}\ignorespaces}
377 \protected\def\ltjglobalsetparameter#1{%
378   \ifnum\globaldefs<0\directlua{ltj.isglobal=''}\else\directlua{ltj.isglobal='global'}\fi
379   \setkeys[ltj]{japaram}{#1}\ignorespaces}
380
381 %%%%%%%% 
382 \def\ltj@@decl@array@param#1{%
383   \expandafter\let\csname ltj@@array@param/#1\endcsname=t%
384 }
385 \ltj@@decl@array@param{prebreakpenalty}
386 \ltj@@decl@array@param{postbreakpenalty}
387 \ltj@@decl@array@param{jaxspmode}
388 \ltj@@decl@array@param{alxspmode}
389 \ltj@@decl@array@param{kcatcode}
390 \ltj@@decl@array@param{jacharrange}
391 \ltj@@decl@array@param{chartorange}
392 \def\ltjgetparameter#1{%
393   \ifcsname ltj@@array@param/#1\endcsname
394     \expandafter\ltx@firstoftwo
395   \else
396     \expandafter\ltx@secondoftwo
397   \fi
398   {\ltj@@getparam@two{#1}}%
399   {\ltj@@getparam@one{#1}}%
400 }
401 \def\ltj@@getparam@one#1{%
402   \directlua{ltj.ext_get_parameter_unary('#1')}}%
403 \def\ltj@@getparam@two#1#2{%
404   \directlua{ltj.ext_get_parameter_binary('#1', \ltj@safe@num{#2})}%
405 }
406
407
408
409 %\def\ltj@temp{plain}
410 %\ifx\fmtname\ltj@temp
411 %  \message{plain format: loading luatexja-plain.tex}
412 %\fi
413 %\input luatexja-plain.tex
414 %\catcode`\@=12
415 %\endinput
416 %! ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
417
418 %%------------------ Load module for the format
419
420 \ifltj@in@latex                 %<*LaTeX>
421   \RequirePackage{luatexja-latex}[2011/04/01]
422 \else\ifltj@in@plain            %<*plain>
423   \input luatexja-plain.sty %
424 \fi\fi                          %</LaTeX|plain>
425
426 %%------------------ all done
427 \ltj@core@AtEnd
428 \endinput
429 %% EOF