6 \NeedsTeXFormat{LaTeX2e}
7 \ProvidesPackage{luatexja-ruby}[2021-05-04 v0.52]
8 \RequirePackage{luatexja}
11 \newattribute\ltj@rubyattr
13 \RequireLuaTeXjaSubmodule{ruby}
15 % ltjset/getparameter への追加設定
19 %% この文字への pre-, post-intrusion の許容量
20 \define@key[ltj]{japaram}{rubypreintrusion}{%
21 \ltj@@set@stack@real{RIPRE}{-0x7FFFFFFF}{0x7FFFFFFF}#1 }
22 \define@key[ltj]{japaram}{rubypostintrusion}{%
23 \ltj@@set@stack@real{RIPOST}{-0x7FFFFFFF}{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, token.scan_word)}}
30 \define@key[ltj]{ruby}{#1}{\expandafter\def\csname ltj@@rubyip@#1\endcsname{##1}}
33 %% bit 0: intrusion を有効にするか(1: 有効)
34 %% bit 1: 前後の intrusion 許容量を小さい方に揃える (1: yes)
35 %% bit 2, 3: intrusion をどう使って親文字を配置するか
36 %% 00: intrusion なしでとりあえず計算し,左右の突出分を進入に割り当てる
37 %% 01: pre-intrusion でまかなえるだけまかない,無理なら post- も使う
39 %% 11: 2 min (pre,post) までは pre, post に均等配分しようとする
40 %% >=01 で,intrusion で賄えきれなかった場合はいつものように伸長する.
41 %% bit 4: 熟語ルビの際の処理方法(0: 常にグループ,1: 可能な限りブロックごとに)
44 %% intrusion 量強制固定(bit 0, bit 1 より優先,負数で「自動」)
45 %% attr は sp 単位だが,ユーザーはルビ全角単位で指定する
46 %% attr_ruby_maxprep, attr_ruby_maxpostp
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}
63 %% attr は sp 単位だが,ユーザーは親文字全角単位で指定
64 \ltj@@rkeydef{intergap}
66 %% epsilon: モノルビ * n にする際に,ルビの方がどれだけ親文字より長い
67 %% 状況を許容するか.親文字全角単位.計算誤差を想定.
68 \ltj@@rkeydef{epsilon}
70 \ltj@@rkeydef{kenten}%% 圏点文字
71 \ltj@@rkeydef{fontcmd}%% フォント
72 \define@boolkey[ltj]{ruby}{rubysmash}[true]{}
74 \ltj@@rkeydef{ybaseheight} % 縦組以外 (yoko, utod, dtou)
75 \ltj@@rkeydef{tbaseheight} % 縦組
76 \define@key[ltj]{ruby}{baseheight}{%
77 \expandafter\def\csname ltj@@rubyip@ybaseheight\endcsname{#1}%
78 \expandafter\def\csname ltj@@rubyip@tbaseheight\endcsname{#1}%
80 %% これらの値が正のとき,親文字の高さをこの値(\zh 単位)とみなす
81 \define@boolkey[ltj]{ruby}{intrude_jfmgk}[true]{}
82 \define@boolkey[ltj]{ruby}{intrude_xkanjiskip}[true]{}
83 \define@boolkey[ltj]{ruby}{intrude_kanjiskip}[true]{}
87 \protected\def\ltjsetruby{\setkeys[ltj]{ruby}}
89 % ここからは ruby マクロ内でなんとかされる事項
94 \define@key[ltj]{ruby}{naka}[none]{\setkeys[ltj]{ruby}{mode=1, stretch=121, stretchruby=121}}
96 \define@key[ltj]{ruby}{kata}[none]{\setkeys[ltj]{ruby}{mode=9, stretch=121, stretchruby=001}}
100 % Lua ソースに渡す table 生成
103 \gdef\ltj@@ruby@create@table#1{% #1: ルビ全角
105 eps = \ltj@safe@dimen{\ltj@@rubyip@epsilon\zw},
106 before_jfmgk = 0, after_jfmgk = 0,
107 rubyzw = \ltj@safe@dimen{#1},
108 maxmargin = \ltj@safe@dimen{\ltj@@rubyip@maxmargin\zw},
109 pre = \ltj@safe@dimen{\ltj@@rubyip@pre#1},
110 post = \ltj@safe@dimen{\ltj@@rubyip@post#1},
111 intergap = \ltj@safe@dimen{\ltj@@rubyip@intergap\zh},
113 = 262144 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchbol
114 + 512 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretcheol
115 + \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretch,
117 = (2097152 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchruby
118 + \ltj@safe@num{\ltj@@rubyip@mode}
119 + 1048576 * \ifltj@ruby@rubysmash1\else0\fi),
120 baseheight = \ltj@safe@dimen{\ifnum\ltjgetparameter{direction}=3
121 \ltj@@rubyip@tbaseheight\else\ltj@@rubyip@ybaseheight\fi\zh},
122 intrude_jfmgk = \string{
123 \ifltj@ruby@intrude_jfmgk [luatexja.icflag_table.FROM_JFM]=true,\fi
124 \ifltj@ruby@intrude_kanjiskip
125 [luatexja.icflag_table.KANJI_SKIP]=true,
126 [luatexja.icflag_table.KANJI_SKIP_JFM]=true,
128 \ifltj@ruby@intrude_xkanjiskip
129 [luatexja.icflag_table.XKANJI_SKIP]=true,
130 [luatexja.icflag_table.XKANJI_SKIP_JFM]=true,
137 %%% 1098765432109876543210976543210
138 %%% |st_ruby|! |--| ← mode
139 \def\ltj@@ruby@cts#1#2#3{%
140 ((\ltj@safe@num{#1}) * 64 + (\ltj@safe@num{#2}) * 8 + \ltj@safe@num{#3})%
144 %%%%%%%% ZR さんの PXrubrica パッケージ中のコードから引用・改変
145 % \pxrr@decompbar: a|bc -> \ltj@@ruby@mark{a}\ltj@@ruby@mark{bc}
148 \ifx#1\expandafter\@firstoftwo
149 \else\expandafter\@secondoftwo
152 \def\pxrr@nil{\noexpand\pxrr@nil}
153 \def\pxrr@end{\noexpand\pxrr@end}
154 \def\pxrr@appto#1#2{%
155 \expandafter\def\expandafter#1\expandafter{#1#2}%
157 \def\pxrr@decompbar#1{%
159 \pxrr@decompbar@loopa\pxrr@nil#1|\pxrr@end|%
161 \def\pxrr@decompbar@loopa#1|{%
162 \expandafter\pxrr@decompbar@loopb\expandafter{\@gobble#1}%
164 \def\pxrr@decompbar@loopb#1{%
165 \pxrr@decompbar@loopc#1\relax\pxrr@nil{#1}%
167 \def\pxrr@decompbar@loopc#1#2\pxrr@nil#3{%
168 \pxrr@ifx{#1\pxrr@end}{}{%
170 \def\pxrr@res{\ltj@@ruby@mark}%
172 \pxrr@appto\pxrr@res{\ltj@@ruby@mark}%
174 \pxrr@appto\pxrr@res{{#3}}%
175 \pxrr@decompbar@loopa\pxrr@nil
181 \protected\def\ltjruby{\@ifnextchar[\ltj@@ruby{\ltj@@ruby[]}}%]
183 \ifdefined\ruby\else\let\ruby=\ltjruby\fi
184 \directlua{luatexja.ruby.read_old_break_info()}%
186 \def\ltj@@ruby[#1]#2#3{{% #1: option #2: 親文字群,#3: ルビ文字列群,共に| 区切り
187 \setkeys[ltj]{ruby}{#1}%
188 \directlua{luatexja.ruby.ruby_tmplist_r = \string{\string};
189 luatexja.ruby.ruby_tmplist_p = \string{\string}}%
190 \leavevmode\dimen0=\f@size pt\dimen1=\ltj@@rubyip@size\dimen0%
192 \pxrr@decompbar{#2}{\let\ltj@@ruby@mark\ltj@@ruby@sp\pxrr@res}%
193 \pxrr@decompbar{#3}{\let\ltj@@ruby@mark\ltj@@ruby@sr\pxrr@res}%
194 {\fontsize{\ltj@@rubyip@size\dimen0}\z@\selectfont\ltj@@rubyip@fontcmd\global\dimen1=\zw}%
196 luatexja.ruby.texiface(\ltj@@ruby@create@table{\dimen1},
197 luatexja.ruby.ruby_tmplist_r, luatexja.ruby.ruby_tmplist_p)}%
200 \def\ltj@@ruby@sr#1{%
201 \setbox0=\hbox{\fontsize{\dimen1}\z@\ltj@@rubyip@fontcmd\selectfont#1}%
202 \directlua{table.insert(luatexja.ruby.ruby_tmplist_r, luatexja.ruby.cpbox())}%
204 \def\ltj@@ruby@sp#1{%
205 \setbox0=\hbox{\selectfont#1}%
206 \directlua{table.insert(luatexja.ruby.ruby_tmplist_p, luatexja.ruby.cpbox())}%
209 \protected\def\ltjkenten{\@ifnextchar[\ltj@@kenten{\ltj@@kenten[]}}%]
210 \let\kenten=\ltjkenten
211 \def\ltj@@kenten[#1]#2{{%
212 \setkeys[ltj]{ruby}{#1, stretchruby=101}%
213 \@tfor\ltj@@kenten@temp:=#2\do{\ltj@@ruby[]{\ltj@@kenten@temp}{\ltj@@rubyip@kenten}}%
218 \count@="3040\loop\relax\ifnum \count@<"30A0
219 \ltjsetparameter{rubypreintrusion={\the\count@,1},
220 rubypostintrusion={\the\count@,1}}
221 \advance\count@1\repeat
223 \count@="30A0\loop\relax\ifnum \count@<"3100
224 \ltjsetparameter{rubypreintrusion={\the\count@,1},
225 rubypostintrusion={\the\count@,1}}
226 \advance\count@1\repeat
228 \count@="1B000\loop\relax\ifnum \count@<"1B170
229 \ltjsetparameter{rubypreintrusion={\the\count@,1},
230 rubypostintrusion={\the\count@,1}}
231 \advance\count@1\repeat
233 \@for\@tmp:=`\‘,`\“,`\〈,`\《,`\「,`\『,`\【,`\〔,`\〖,`\〘,`\〝,`\(,`\[,`\{,`\⦅ \do
234 {\ltjsetparameter{rubypreintrusion={\@tmp,-1}}}
235 \@for\@tmp:=`\’,`\”,`\〉,`\》,`\」,`\』,`\】,`\〕,`\〗,`\〙,`\〟,`\),`\],`\},`\⦆ \do
236 {\ltjsetparameter{rubypostintrusion={\@tmp,-1}}}
238 \@for\@tmp:=`\・,`\:,`\;,`\―,`\‥,`\…,`〳,`\〴,`\〵,"2014 \do
239 {\ltjsetparameter{rubypreintrusion={\@tmp,0.5},rubypostintrusion={\@tmp,0.5}}}
241 \@for\@tmp:=`\、,`\,,`\。,`\. \do
242 {\ltjsetparameter{rubypostintrusion={\@tmp,-1}}}
244 \ltjsetparameter{rubypreintrusion={-1,1}}
247 pre=-1, post=-1, mode=1,
248 stretchruby={1}{2}{1}, stretch = {1}{2}{1},
249 stretchbol={0}{1}{1}, stretcheol={1}{1}{0},
250 maxmargin=0.5, size=0.5, intergap=0, rubysmash=false,
251 kenten=\textbullet, fontcmd=\relax, ybaseheight=0.88, tbaseheight=0.5,
252 intrude_jfmgk, intrude_kanjiskip, intrude_xkanjiskip,