OSDN Git Service

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