OSDN Git Service

Import from old kitagawa_ruby branch (may not work now)
[luatex-ja/luatexja.git] / src / addons / luatexja-ruby.sty
1 %
2 % luatexja-ruby.sty
3 %
4
5 % LaTeX only!
6 \NeedsTeXFormat{LaTeX2e}
7 \ProvidesPackage{luatexja-ruby}[2012/4/21 v0.1]
8 \RequirePackage{luatexja}
9
10 %%------------------
11 \newluatexattribute\ltj@rubyattr
12 \RequireLuaTeXjaSubmodule{ruby}
13
14 % ltjset/getparameter への追加設定
15 % これらは段落単位の設定.
16
17 %% 引数:ルビ全角を単位とした実数
18 %% この文字への pre-, post-intrusion の許容量
19 \define@key[ltj]{japaram}{rubypreintrusion}{%
20   \expandafter\ltj@@set@stack@real#1:{ripre}{0}{0x7FFFFFFF}}
21 \define@key[ltj]{japaram}{rubypostintrusion}{%
22   \expandafter\ltj@@set@stack@real#1:{ripost}{0}{0x7FFFFFFF}}
23 \def\ltj@@set@stack@real#1,#2:#3#4#5{%
24   \directlua{luatexja.stack.set_stack_table(luatexja.isglobal, \asluastring{#3},
25     \ltj@safe@num@or\ltj@defnum{#1},\ltj@safe@real{#2},#4,#5)}}
26
27 % ルビ用のキー設定
28 \def\ltj@@rkeydef#1{
29   \define@key[ltj]{ruby}{#1}{\expandafter\def\csname ltj@@rubyip@#1\endcsname{##1}}
30 }
31 %% attr_ruby_mode
32 %% bit 0: intrusion を有効にするか(1: 有効)
33 %% bit 1: 前後の intrusion 許容量を小さい方に揃える (1: yes)
34 %% bit 2, 3: intrusion をどう使って親文字を配置する 
35 %%   00: intrusion なしでとりあえず計算し,左右の突出分を進入に割り当てる
36 %%   01: pre-intrusion でまかなえるだけまかない,無理なら post- も使う
37 %%   10: post を優先
38 %%   11: 2 min (pre,post) までは pre, post に均等配分しようとする
39 %%     >=01 で,intrusion で賄えきれなかった場合はいつものように伸長する.
40 %% bit 4: 熟語ルビの際の処理方法(0: 常にグループ,1: 可能な限りブロックごとに)
41 %%        ↑bit 4 は今は無効
42 \ltj@@rkeydef{mode}
43 %% intrusion 量強制固定(bit 0, bit 1 より優先,負数で「自動」)
44 %% attr は sp 単位だが,ユーザーはルビ全角単位で指定する
45 %% attr_ruby_maxprep, attr_ruby_maxpostp
46 \ltj@@rkeydef{intrusionpre}
47 \ltj@@rkeydef{intrusionpost}
48 %% 親文字伸長の際の比,{0}{1}{1} などと0--7 の数 3 つで指定
49 %% attr_ruby_stretch (head left,middle,right)(end)(middle) 27-bits 
50 \ltj@@rkeydef{stretchhead} % 行頭形
51 \ltj@@rkeydef{stretchend}  % 行末形
52 \ltj@@rkeydef{stretch}     % 行中形
53 %% ルビが伸長するときの比 {1}{2}{1} などと0--7 の数 3 つで指定
54 %% attr_ruby_mode 上位部分
55 \ltj@@rkeydef{stretchruby}
56 %% ルビ<親のとき,ルビと親文字の端の最大値 
57 %% attr_ruby_maxmargin
58 %% attr は sp 単位だが,ユーザーは親文字全角単位で指定
59 \ltj@@rkeydef{maxmargin}
60 \savekeys[ltj]{ruby}{%
61   mode, intrusionpre, intrusionpost, 
62   stretchhead, stretchend, stretch, stretchruby, 
63   maxmargin, rubysize, 
64 }
65
66 %%%%%%%% setkeys の別名
67 \protected\def\ltjsetruby{\setkeys[ltj]{ruby}}
68
69 % ここからは ruby マクロ内でなんとかされる事項
70 %% ルビと親文字の大きさの比
71 \ltj@@rkeydef{rubysize}
72
73 % 肩つき用簡易設定.
74 \define@key[ltj]{ruby}{kata}[none]{\setkeys[ltj]{ruby}{mode=9,stretchruby=001}}
75 % 拡張肩つき用簡易設定.
76 \define@key[ltj]{ruby}{ekata}[none]{\setkeys[ltj]{ruby}{stretch=001,stretchruby=001}}
77
78 %%%%%%%% 補助関数
79
80 % Lua ソースに渡す table 生成
81 \def\ltj@@ruby@create@table#1{% #1: ルビ全角
82     \string{
83       rubyzw = \ltj@safe@dimen{#1}, 
84       maxmargin = \ltj@safe@dimen{\ltj@@rubyip@maxmargin\zw},
85       intrusionpre = \ltj@safe@dimen{\ltj@@rubyip@intrusionpre#1},
86       intrusionpost = \ltj@safe@dimen{\ltj@@rubyip@intrusionpost#1},
87       stretch 
88         = 262144 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchhead
89           + 512 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchend
90           + \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretch,
91       mode 
92         = 2097152 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchruby
93           + \ltj@safe@num{\ltj@@rubyip@mode},
94     \string}
95 }
96 \def\ltj@@ruby@cts#1#2#3{%
97   ((\ltj@safe@num{#1}) * 64 + (\ltj@safe@num{#2}) * 8 + \ltj@safe@num{#3})%
98 }
99
100
101 %%%%%%%% ZR さんの PXrubrica パッケージ中のコードから引用・改変
102 % \pxrr@decompbar: a|bc -> \ltj@@ruby@mark{a}\ltj@@ruby@mark{bc}
103 \let\pxrr@res\empty
104 \def\pxrr@ifx#1{%
105   \ifx#1\expandafter\@firstoftwo
106   \else\expandafter\@secondoftwo
107   \fi
108 }
109 \def\pxrr@nil{\noexpand\pxrr@nil}
110 \def\pxrr@end{\noexpand\pxrr@end}
111 \def\pxrr@appto#1#2{%
112   \expandafter\def\expandafter#1\expandafter{#1#2}%
113 }
114 \def\pxrr@decompbar#1{%
115   \let\pxrr@res\@empty
116   \pxrr@decompbar@loopa\pxrr@nil#1|\pxrr@end|%
117 }
118 \def\pxrr@decompbar@loopa#1|{%
119   \expandafter\pxrr@decompbar@loopb\expandafter{\@gobble#1}%
120 }
121 \def\pxrr@decompbar@loopb#1{%
122   \pxrr@decompbar@loopc#1\relax\pxrr@nil{#1}%
123 }
124 \def\pxrr@decompbar@loopc#1#2\pxrr@nil#3{%
125   \pxrr@ifx{#1\pxrr@end}{}{%
126     \ifx\pxrr@res\@empty
127       \def\pxrr@res{\ltj@@ruby@mark}%
128     \else
129       \pxrr@appto\pxrr@res{\ltj@@ruby@mark}%
130     \fi
131     \pxrr@appto\pxrr@res{{#3}}%
132     \pxrr@decompbar@loopa\pxrr@nil
133   }%
134 }
135 %%%%%%%% ここまで
136
137 %%%%%%%% TeX command
138 \protected\def\ltjruby{\@ifnextchar[\ltj@@ruby{\ltj@@ruby[]}}%]
139 \let\ruby=\ltjruby
140 \def\ltj@@ruby[#1]#2#3{{% #1: option #2: 親文字群,#3: ルビ文字列群,共に| 区切り
141   \setkeys[ltj]{ruby}{#1}%
142   \directlua{ruby_tmplist_r = \string{\string}; ruby_tmplist_p = \string{\string}}%
143   \leavevmode\dimen0=\f@size pt\dimen1=\ltj@@rubyip@rubysize\dimen0%
144   % 引数展開,テーブルにセット
145   \pxrr@decompbar{#2}{\let\ltj@@ruby@mark\ltj@@ruby@sp\pxrr@res}%
146   \pxrr@decompbar{#3}{\let\ltj@@ruby@mark\ltj@@ruby@sr\pxrr@res}%
147   {\fontsize{\ltj@@rubyip@rubysize\dimen0}\z@\selectfont\global\dimen1=\zw}%
148   \directlua{%
149     luatexja.ruby.texiface(\ltj@@ruby@create@table{\dimen1},
150     ruby_tmplist_r, ruby_tmplist_p)}%
151 }}
152
153 \def\ltj@@ruby@sr#1{%
154   \setbox0=\hbox{\fontsize{\dimen1}\z@\selectfont#1}%
155   \directlua{table.insert(ruby_tmplist_r, node.copy(tex.box[0]))}%
156 }
157 \def\ltj@@ruby@sp#1{%
158   \setbox0=\hbox{\selectfont#1}%
159   \directlua{table.insert(ruby_tmplist_p, node.copy(tex.box[0]))}%
160 }
161
162 % 初期値.要調整
163 %% ひらがな
164 \count@="3040\loop\relax\ifnum \count@<"30A0
165   \ltjsetparameter{rubypreintrusion={\the\count@,1}, 
166     rubypostintrusion={\the\count@,1}}
167   \advance\count@1\repeat
168 %% 「」
169 \ltjsetparameter{rubypreintrusion={`「,1}, 
170   rubypostintrusion={`」,1}}
171 %% ,、
172 \ltjsetparameter{rubypreintrusion={`,,1}, 
173   rubypostintrusion={`,,1}}
174 \ltjsetparameter{rubypreintrusion={`、,1}, 
175   rubypostintrusion={`、,1}}
176 %% ・
177 \ltjsetparameter{rubypreintrusion={`・,1}, 
178   rubypostintrusion={`・,1}}
179
180 \setkeys[ltj]{ruby}{
181   maxmargin=0.5, 
182   stretchruby={1}{2}{1}, stretch = {1}{2}{1},
183   stretchhead = {0}{1}{1}, stretchend = {1}{1}{0},
184   intrusionpre = -1, intrusionpost = -1, 
185   mode = 1, rubysize = 0.5
186 }
187
188 \endinput