OSDN Git Service

Ruby: use the aux file to record the width of overhang
[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}[2014/02/06 v0.1]
8 \RequirePackage{luatexja}
9
10 %%------------------
11 \newluatexattribute\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   \expandafter\ltj@@set@stack@real#1:{RIPRE}{0}{0x7FFFFFFF}}
22 \define@key[ltj]{japaram}{rubypostintrusion}{%
23   \expandafter\ltj@@set@stack@real#1:{RIPOST}{0}{0x7FFFFFFF}}
24 \def\ltj@@set@stack@real#1,#2:#3#4#5{%
25   \directlua{luatexja.stack.set_stack_perchar(luatexja.stack_table_index.#3,
26     \ltj@safe@num@or\ltj@defnum{#1},\ltj@safe@real{#2},#4,#5)}}
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{intrusionpre}
48 \ltj@@rkeydef{intrusionpost}
49 %% 親文字伸長の際の比,{0}{1}{1} などと0--7 の数 3 つで指定
50 %% attr_ruby_stretch (head left,middle,right)(end)(middle) 27-bits 
51 \ltj@@rkeydef{stretchhead} % 行頭形
52 \ltj@@rkeydef{stretchend}  % 行末形
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 \savekeys[ltj]{ruby}{%
62   mode, intrusionpre, intrusionpost, 
63   stretchhead, stretchend, stretch, stretchruby, 
64   maxmargin, rubysize, 
65 }
66
67 %%%%%%%% setkeys の別名
68 \protected\def\ltjsetruby{\setkeys[ltj]{ruby}}
69
70 % ここからは ruby マクロ内でなんとかされる事項
71 %% ルビと親文字の大きさの比
72 \ltj@@rkeydef{rubysize}
73
74 % 肩つき用簡易設定.
75 \define@key[ltj]{ruby}{kata}[none]{\setkeys[ltj]{ruby}{mode=9,stretchruby=001}}
76 % 拡張肩つき用簡易設定.
77 \define@key[ltj]{ruby}{ekata}[none]{\setkeys[ltj]{ruby}{stretch=001,stretchruby=001}}
78
79 %%%%%%%% 補助関数
80
81 % Lua ソースに渡す table 生成
82 \def\ltj@@ruby@create@table#1{% #1: ルビ全角
83     \string{
84       rubyzw = \ltj@safe@dimen{#1}, 
85       maxmargin = \ltj@safe@dimen{\ltj@@rubyip@maxmargin\zw},
86       intrusionpre = \ltj@safe@dimen{\ltj@@rubyip@intrusionpre#1},
87       intrusionpost = \ltj@safe@dimen{\ltj@@rubyip@intrusionpost#1},
88       stretch 
89         = 262144 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchhead
90           + 512 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchend
91           + \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretch,
92       mode 
93         = 2097152 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchruby
94           + \ltj@safe@num{\ltj@@rubyip@mode},
95     \string}
96 }
97 \def\ltj@@ruby@cts#1#2#3{%
98   ((\ltj@safe@num{#1}) * 64 + (\ltj@safe@num{#2}) * 8 + \ltj@safe@num{#3})%
99 }
100
101
102 %%%%%%%% ZR さんの PXrubrica パッケージ中のコードから引用・改変
103 % \pxrr@decompbar: a|bc -> \ltj@@ruby@mark{a}\ltj@@ruby@mark{bc}
104 \let\pxrr@res\empty
105 \def\pxrr@ifx#1{%
106   \ifx#1\expandafter\@firstoftwo
107   \else\expandafter\@secondoftwo
108   \fi
109 }
110 \def\pxrr@nil{\noexpand\pxrr@nil}
111 \def\pxrr@end{\noexpand\pxrr@end}
112 \def\pxrr@appto#1#2{%
113   \expandafter\def\expandafter#1\expandafter{#1#2}%
114 }
115 \def\pxrr@decompbar#1{%
116   \let\pxrr@res\@empty
117   \pxrr@decompbar@loopa\pxrr@nil#1|\pxrr@end|%
118 }
119 \def\pxrr@decompbar@loopa#1|{%
120   \expandafter\pxrr@decompbar@loopb\expandafter{\@gobble#1}%
121 }
122 \def\pxrr@decompbar@loopb#1{%
123   \pxrr@decompbar@loopc#1\relax\pxrr@nil{#1}%
124 }
125 \def\pxrr@decompbar@loopc#1#2\pxrr@nil#3{%
126   \pxrr@ifx{#1\pxrr@end}{}{%
127     \ifx\pxrr@res\@empty
128       \def\pxrr@res{\ltj@@ruby@mark}%
129     \else
130       \pxrr@appto\pxrr@res{\ltj@@ruby@mark}%
131     \fi
132     \pxrr@appto\pxrr@res{{#3}}%
133     \pxrr@decompbar@loopa\pxrr@nil
134   }%
135 }
136 %%%%%%%% ここまで
137
138 %%%%%%%% TeX command
139 \protected\def\ltjruby{\@ifnextchar[\ltj@@ruby{\ltj@@ruby[]}}%]
140 \let\ruby=\ltjruby
141 \def\ltj@@ruby[#1]#2#3{{% #1: option #2: 親文字群,#3: ルビ文字列群,共に| 区切り
142   \setkeys[ltj]{ruby}{#1}%
143   \directlua{ruby_tmplist_r = \string{\string}; ruby_tmplist_p = \string{\string}}%
144   \leavevmode\dimen0=\f@size pt\dimen1=\ltj@@rubyip@rubysize\dimen0%
145   % 引数展開,テーブルにセット
146   \pxrr@decompbar{#2}{\let\ltj@@ruby@mark\ltj@@ruby@sp\pxrr@res}%
147   \pxrr@decompbar{#3}{\let\ltj@@ruby@mark\ltj@@ruby@sr\pxrr@res}%
148   {\fontsize{\ltj@@rubyip@rubysize\dimen0}\z@\selectfont\global\dimen1=\zw}%
149   \directlua{%
150     luatexja.ruby.texiface(\ltj@@ruby@create@table{\dimen1},
151     ruby_tmplist_r, ruby_tmplist_p)}%
152 }}
153
154 \def\ltj@@ruby@sr#1{%
155   \setbox0=\hbox{\fontsize{\dimen1}\z@\selectfont#1}%
156   \directlua{table.insert(ruby_tmplist_r, luatexja.ruby.cpbox())}%
157 }
158 \def\ltj@@ruby@sp#1{%
159   \setbox0=\hbox{\selectfont#1}%
160   \directlua{table.insert(ruby_tmplist_p, luatexja.ruby.cpbox())}%
161 }
162
163 % 初期値.要調整
164 %% ひらがな
165 \count@="3040\loop\relax\ifnum \count@<"30A0
166   \ltjsetparameter{rubypreintrusion={\the\count@,1}, 
167     rubypostintrusion={\the\count@,1}}
168   \advance\count@1\repeat
169 %% 「」
170 \ltjsetparameter{rubypreintrusion={`「,1}, 
171   rubypostintrusion={`」,1}}
172 %% ,、
173 \ltjsetparameter{rubypreintrusion={`,,1}, 
174   rubypostintrusion={`,,1}}
175 \ltjsetparameter{rubypreintrusion={`、,1}, 
176   rubypostintrusion={`、,1}}
177 %% ・
178 \ltjsetparameter{rubypreintrusion={`・,1}, 
179   rubypostintrusion={`・,1}}
180
181 \setkeys[ltj]{ruby}{
182   maxmargin=0.5, 
183   stretchruby={1}{2}{1}, stretch = {1}{2}{1},
184   stretchhead = {0}{1}{1}, stretchend = {1}{1}{0},
185   intrusionpre = -1, intrusionpost = -1, 
186   mode = 1, rubysize = 0.5
187 }
188
189 \endinput