OSDN Git Service

Add \kchar and \achar, and a patch for xunicode.sty (in luatexja-fontspec.sty).
[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
188 %%%%%%%% \asluastring
189 \def\asluastring#1{'\luaescapestring{\detokenize{#1}}'}
190
191 %%%%%%%% Redefine \/
192 \protected\def\/{\relax\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 %%%%%%%% \zw, \zh
201 \def\zw{\directlua{luatexja.jfont.load_zw()}\ltj@zw}
202 \def\zh{\directlua{luatexja.jfont.load_zh()}\ltj@zh}
203
204 %%%%%%%% \inhibitglue
205 \let\new@everypar=\everypar
206 \protected\def\inhibitglue{\relax%
207   \ifvmode
208     \directlua{ltj.global_temp = tex.globaldefs; tex.globaldefs = 0}% trick
209     \xdef\@@tempa{\the\everypar}%
210     \xdef\@@tempb{\everypar={\@@tempa}\@@tempa\noexpand\ltj@@ihb}%
211     \everypar\expandafter{\@@tempb}%
212     \directlua{tex.globaldefs = ltj.global_temp}% trick
213   \else\ifhmode\ltj@@ihb\fi\fi}
214 \def\ltj@@ihb{\directlua{luatexja.jfmglue.create_inhibitglue_node()}}
215
216 %%%%%%%% \yoko, \tate, \ifydir, \iftdir
217 %% At this moment, tategaki mode is not implemented; 
218 %% there is only yokogaki mode now.
219 \let\yoko\relax
220 \let\tate\relax
221 \newif\ifydir\ydirtrue
222 \newif\iftdir\tdirfalse
223
224 %%%%%%%% \ltjdefcharrange<name>{100-200,3000-,5000,...}
225 \protected\def\ltjdefcharrange#1#2{%
226   {\ltj@tempcntc=#1 \expandafter\ltj@@dcrange#2,,}\ignorespaces}
227 \def\ltj@@dcrange#1,{\def\ltj@temp{#1}%
228   \ifx\ltj@temp\empty\let\@next=\relax\else
229   \ltj@@dcrangeA{#1}\let\@next=\ltj@@dcrange\fi\@next}
230 \def\ltj@@dcrangeA#1{\ltj@@dcrangeB#1--\@nil}
231 \def\ltj@@dcrangeB#1-#2-#3\@nil{\def\ltj@temp{#3}%
232   \ifx\ltj@temp\empty
233     \ltj@tempcnta=#1 \ltj@tempcntb=\ltj@tempcnta
234   \else
235     \def\ltj@temp{#1}%
236     \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
237     \def\ltj@temp{#2}%
238     \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
239   \fi
240   \directlua{luatexja.charrange.add_char_range(\the\ltj@tempcnta,\the\ltj@tempcntb,
241     \the\ltj@tempcntc)}%
242   }
243
244 %%%%%%%% \ltjsetparameter
245 \newcount\ltj@@stack \newcount\ltj@@group@level
246 \ltj@@group@level=0 \ltj@@stack=0
247
248 % prebreakpenalty = {<char_code>, <penalty>}
249 \define@key[ltj]{japaram}{kcatcode}{%
250   \expandafter\ltj@@set@stack#1:{kcat}{0}{0x7FFFFFFF}}
251 \define@key[ltj]{japaram}{prebreakpenalty}{%
252   \expandafter\ltj@@set@stack#1:{pre}{-10000}{10000}}
253 \define@key[ltj]{japaram}{postbreakpenalty}{%
254   \expandafter\ltj@@set@stack#1:{post}{-10000}{10000}}
255 \def\ltj@@set@stack#1,#2:#3#4#5{%
256   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, \asluastring{#3},
257     \ltj@safe@num@or\ltj@defnum{#1},\ltj@safe@num@or\ltj@defnum{#2},#4,#5)}}
258
259 % jatextfont = {<char_code>, <font_cs>}
260 \define@key[ltj]{japaram}{jatextfont}{%
261   \expandafter\ltj@@set@stackfont#1:{mjtext}}
262 \define@key[ltj]{japaram}{jascriptfont}{%
263   \expandafter\ltj@@set@stackfont#1:{mjscr}}
264 \define@key[ltj]{japaram}{jascriptscriptfont}{%
265   \expandafter\ltj@@set@stackfont#1:{mjss}}
266 \def\ltj@@set@stackfont#1,#2:#3{%
267   \directlua{%
268     ltj.global_temp = tex.globaldefs; 
269     tex.globaldefs = 0}%
270   \ltj@tempcnta=\ltj@curjfnt\ltj@curjfnt=-1 #2%
271   \ifnum\ltj@curjfnt=-1
272     \@PackageError{luatexja-core}%
273     {\string#2 is not a control sequence that represents \MessageBreak
274      a Japanese font}{}%
275   \else
276     \directlua{luatexja.stack.set_stack_font(ltj.isglobal, \asluastring{#3},
277     \ltj@safe@num@or\ltj@defnum{#1}, \the\ltj@curjfnt)}%
278   \fi
279   \ltj@curjfnt=\ltj@tempcnta
280   \directlua{tex.globaldefs = ltj.global_temp}%
281   }
282
283 % yalbaselineshift = <dimen>
284 % yjabaselineshift = <dimen>
285 \define@key[ltj]{japaram}{yalbaselineshift}{%
286   \directlua{tex.setattribute(ltj.isglobal, 
287     luatexbase.attributes['ltj@yablshift'],
288     \ltj@safe@dimen@or\ltj@defdimen{#1})}}
289 \define@key[ltj]{japaram}{yjabaselineshift}{%
290   \directlua{tex.setattribute(ltj.isglobal, 
291     luatexbase.attributes['ltj@ykblshift'],
292     \ltj@safe@dimen@or\ltj@defdimen{#1})}}
293
294 % jaxspmode = {<char_code>, <mode>}
295 % mode: inhibit, preonly, postonly, allow
296 %    or       0        2         1      3
297 \define@key[ltj]{japaram}{jaxspmode}{%        \inhibitxspcode
298   \expandafter\ltj@set@jaxspmode#1:\relax}
299 \def\ltj@set@jaxspmode#1,#2:{%
300   \lowercase{\edef\ltj@temp{#2}}%
301   \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
302   \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
303   \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
304   \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
305   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, 'xsp', 
306     \ltj@safe@num@or\ltj@defnum{#1},
307     \ltj@safe@num@or\ltj@defnum{\ltj@temp},0,3)}}
308
309 % alxspmode = {<char_code>, <mode>}
310 % mode: inhibit, preonly, postonly, allow
311 %    or       0        1         2      3
312 \define@key[ltj]{japaram}{alxspmode}{%        \inhibitxspcode
313   \expandafter\ltj@set@alxspmode#1:\relax}
314 \def\ltj@set@alxspmode#1,#2:{%
315   \lowercase{\edef\ltj@temp{#2}}%
316   \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
317   \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
318   \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
319   \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
320   \directlua{luatexja.stack.set_stack_table(ltj.isglobal, 'xsp', 
321     \ltj@safe@num@or\ltj@defnum{#1},
322     \ltj@safe@num@or\ltj@defnum{\ltj@temp},0,3)}}
323
324 % autospacing = <bool> (default: true)
325 \define@boolkey[ltj]{japaram}{autospacing}[true]{%
326   \ifltj@japaram@autospacing
327     \directlua{ltj.ext_print_global()}\ltj@autospc=1
328   \else
329     \directlua{ltj.ext_print_global()}\ltj@autospc=0
330   \fi}
331
332 % autoxspacing = <bool> (default: true)
333 \define@boolkey[ltj]{japaram}{autoxspacing}[true]{%
334   \ifltj@japaram@autoxspacing
335     \directlua{ltj.ext_print_global()}\ltj@autoxspc=1
336   \else
337     \directlua{ltj.ext_print_global()}\ltj@autoxspc=0
338   \fi}
339
340 % [x]]kanjiskip = <dimen>
341 \define@key[ltj]{japaram}{kanjiskip}{%              % SKIP
342   \directlua{luatexja.stack.set_stack_skip(ltj.isglobal, 'kanjiskip', \ltj@safe@glue{#1})}}
343 \define@key[ltj]{japaram}{xkanjiskip}{%             % SKIP
344   \directlua{luatexja.stack.set_stack_skip(ltj.isglobal, 'xkanjiskip', \ltj@safe@glue{#1})}}
345
346 % jcharwidowpenalty = <count>
347 \define@key[ltj]{japaram}{jcharwidowpenalty}{%      %COUNT
348   \ltj@@set@stack0,#1:{jwp}{-10000}{10000}}
349
350 % differentjfm = { large | small | average | both }
351 \define@choicekey*+[ltj]{japaram}{differentjfm}[\ltj@temp\ltj@tempr]%
352   {large,small,average,both}{%
353   \ifcase\ltj@tempr
354     \directlua{luatexja.jfmglue.diffmet_rule = math.max}\or
355     \directlua{luatexja.jfmglue.diffmet_rule = math.min}\or
356     \directlua{luatexja.jfmglue.diffmet_rule = math.two_average}\or
357     \directlua{luatexja.jfmglue.diffmet_rule = math.two_add}
358   \fi
359 }{\ltj@PackageError{luatexja}{invalid argument `#1' for `differentjfm'}{%
360   Argument for `differentjfm' must be one of `large', `small', `average',^^J%
361   and `both'. }}
362   % large, small, average, both
363
364
365 % jacharrange = { +-<range_number> }
366 \define@key[ltj]{japaram}{jacharrange}{\expandafter\ltj@@scrange#1,,}
367 \def\ltj@@scrange#1,{%
368   \edef\ltj@temp{#1}%
369   \ifx\ltj@temp\empty\let\next=\relax\else\let\next=\ltj@@scrange
370     \directlua{luatexja.charrange.toggle_char_range(%
371       ltj.is_global,\ltj@safe@num@or\ltj@defnum{#1})}%
372   \fi\next
373 }
374
375 \protected\def\ltjsetparameter#1{%
376   \ifnum\globaldefs>0\directlua{ltj.isglobal='global'}\else\directlua{ltj.isglobal=''}\fi
377   \setkeys[ltj]{japaram}{#1}\ignorespaces}
378 \protected\def\ltjglobalsetparameter#1{%
379   \ifnum\globaldefs<0\directlua{ltj.isglobal=''}\else\directlua{ltj.isglobal='global'}\fi
380   \setkeys[ltj]{japaram}{#1}\ignorespaces}
381
382 %%%%%%%% \ltjgetparameter
383 \def\ltj@@decl@array@param#1{%
384   \expandafter\let\csname ltj@@array@param/#1\endcsname=t%
385 }
386 \ltj@@decl@array@param{prebreakpenalty}
387 \ltj@@decl@array@param{postbreakpenalty}
388 \ltj@@decl@array@param{jaxspmode}
389 \ltj@@decl@array@param{alxspmode}
390 \ltj@@decl@array@param{kcatcode}
391 \ltj@@decl@array@param{jacharrange}
392 \ltj@@decl@array@param{chartorange}
393 \def\ltjgetparameter#1{%
394   \ifcsname ltj@@array@param/#1\endcsname
395     \expandafter\ltx@firstoftwo
396   \else
397     \expandafter\ltx@secondoftwo
398   \fi
399   {\ltj@@getparam@two{#1}}%
400   {\ltj@@getparam@one{#1}}%
401 }
402 \def\ltj@@getparam@one#1{%
403   \directlua{ltj.ext_get_parameter_unary('#1')}}%
404 \def\ltj@@getparam@two#1#2{%
405   \directlua{ltj.ext_get_parameter_binary('#1', \ltj@safe@num{#2})}%
406 }
407
408 %%%%%%%% \achar<number>, \kchar<number>
409 \let\ltj@@origchar=\char
410 \protected\def\kchar{\afterassignment\ltj@@kchar\ltj@tempcnta}
411 \def\ltj@@kchar{{%
412     \csname ltj@kcat0\endcsname=0
413     \csname ltj@kcat1\endcsname=0
414     \csname ltj@kcat2\endcsname=0
415     \csname ltj@kcat3\endcsname=0
416     \csname ltj@kcat4\endcsname=0
417     \csname ltj@kcat5\endcsname=0
418     \csname ltj@kcat6\endcsname=0
419     \ltj@@origchar\ltj@tempcnta}}
420 \protected\def\achar{\afterassignment\ltj@@achar\ltj@tempcnta}
421 \def\ltj@@achar{{%
422     \csname ltj@kcat0\endcsname="7FFFFFFF %"
423     \csname ltj@kcat1\endcsname="7FFFFFFF %"
424     \csname ltj@kcat2\endcsname="7FFFFFFF %"
425     \csname ltj@kcat3\endcsname="7FFFFFFF %"
426     \csname ltj@kcat4\endcsname="7FFFFFFF %"
427     \csname ltj@kcat5\endcsname="7FFFFFFF %"
428     \csname ltj@kcat6\endcsname="7FFFFFFF %"
429     \ltj@@origchar\ltj@tempcnta}}
430
431
432
433
434 %%------------------ Load module for the format
435
436 \ifltj@in@latex                 %<*LaTeX>
437   \RequirePackage{luatexja-latex}[2011/04/01]
438 \else\ifltj@in@plain            %<*plain>
439   \input luatexja-plain.sty %
440 \fi\fi                          %</LaTeX|plain>
441
442 %%------------------ all done
443 \ltj@core@AtEnd
444 \endinput
445 %% EOF