OSDN Git Service

HanYi fonts to luatexja-zhfonts.sty.
[luatex-ja/luatexja.git] / src / ltj-cctbreg.sty
1 %
2 % ltj-cctbreg.sty
3 %
4
5 %! luatexbase と luatex パッケージのカテゴリコードテーブルの割当に
6 %! 関する衝突を回避する.
7 %! この衝突に関して「外部のパッチ」で解決しようとするのは, あまり
8 %! 筋が良くなく, できるなら避けたいが, 今のままだと LuaTeX-ja を
9 %! LaTeX + fontspec で用いた時に確実に影響が出る. また衝突を残した
10 %! ままだとカテゴリコードテーブルが実質的に使えない訳で, これも
11 %! 残念なことである.
12 %! 「近いうちに両パッケージの中で解決される」ことを期待しつつ,
13 %! 「衝突がない」という前提で開発を行いたいので, 暫定的処置として,
14 %! このモジュールを用意する.
15
16 %! これは外から読まれない
17 %% Avoid multiple loading.
18 \csname luatexjacctbregLoaded\endcsname
19 \edef\ltj@cctbreg@AtEnd{%
20 \endlinechar=\the\endlinechar
21 \relax}
22 \endlinechar=-1 %
23 \def\luatexjacctbregLoaded{\endinput}
24
25 \ifltj@in@latex                 %<*LaTeX>
26   \NeedsTeXFormat{LaTeX2e}
27   \ProvidesPackage{ltj-cctbreg}[2013/05/14]
28 \fi                             %</LaTeX>
29
30 % LS = Size of storage reserved for luatex-pkg.
31 \chardef\ltj@cctb@lower@stor@size=16384
32
33 % LL = (upper bound of cctb index used by luatexbase.sty and luatex.sty) - 1
34 \chardef\ltj@cctb@limit=32766
35
36 %-------------------
37
38 \newcount\ltj@@cctb@failure
39
40 \ltj@@cctb@failure=0
41 \def\ltj@@cctb@check@failure{
42   \ifnum\ltj@@cctb@failure>0 %
43     \@PackageWarningNoLine{ltj-cctbreg}
44       {Cannot apply the patch (\the\ltj@@cctb@failure)}
45     \ltj@cctbreg@AtEnd
46   \expandafter\endinput\fi\relax
47 }
48
49 \chardef\ltj@@cctb@lower@stor@max=
50   \numexpr\ltj@cctb@lower@stor@size-1\relax
51 \chardef\ltj@@cctb@lower@stor@safemax=
52   \numexpr\ltj@cctb@lower@stor@size-8\relax
53
54 \chardef\ltj@@tempcctb@a=\ltj@cctb@lower@stor@size
55 \chardef\ltj@@tempcctb@b=\numexpr\ltj@cctb@lower@stor@size+1\relax
56 \def\ltj@@copy@cctb#1#2{%
57   % Note: The current cctb number is supposed to be zero.
58   \luatexcatcodetable#1\relax
59   \luatexsavecatcodetable#2\relax
60   \luatexcatcodetable0\relax
61 }
62
63 %-------------------
64
65 % The luatexbase-cctb package stores its preset settings to
66 % registers 1--6, and new user allocations go to number 7, 8, ....
67
68 \ifdefined\lltxb@catcodetable@alloc
69   \ifnum\lltxb@catcodetable@alloc>\ltj@@cctb@lower@stor@safemax
70     % Failure: luatexbase-cctb alloc pointer is too high!
71     \ltj@@cctb@failure=2 %
72   \fi
73 \else
74   % Failure: luatexbase-cctb not loaded!
75   \ltj@@cctb@failure=1 %
76 \fi
77
78 \begingroup
79 \endlinechar=13
80 \gdef\ltj@@cctb@org@def#1{%
81   \ifnum\lltxb@catcodetable@alloc<65535\relax
82     \global\advance\lltxb@catcodetable@alloc\tw@
83     \allocationnumber\lltxb@catcodetable@alloc
84     \global\chardef#1\allocationnumber
85     \luatexinitcatcodetable\allocationnumber
86     \begingroup\escapechar\m@ne
87     \luatexbase@directlua{luatexbase.catcodetabledef_from_tex(
88       '\luatexluaescapestring{\string#1}', '\number\allocationnumber')}%
89     \endgroup
90     \wlog{\string#1=\string\luatexcatcodetable\the\allocationnumber}%
91   \else
92     \errmessage{No room for a new \string\luatexcatcodetable}%
93   \fi}
94 \endgroup
95
96 \unless\ifx\ltj@@cctb@org@def\newluatexcatcodetable
97   % Failure: luatexbase-cctb is not of expected version!
98 %%% \bebo <- the meaning of this control sequence?
99   \ltj@@cctb@failure=3 %
100 \fi
101
102 \ifdefined\LuT@AllocAttribute
103   \ifnum\LuT@AllocAttribute>\ltj@@cctb@lower@stor@safemax
104     % Failure: luatex-pkg alloc pointer is too high!
105     \ltj@@cctb@failure=4 %
106   \fi
107 \fi
108 \ifdefined\CatcodeTableStack
109   \ifnum\CatcodeTableStack>\ltj@@cctb@lower@stor@safemax
110     % Failure: luatex-pkg cctb-stack pointer is too high!
111     \ltj@@cctb@failure=5 %
112   \fi
113 \fi
114
115 \ltj@@cctb@check@failure
116
117 \ltx@firstofone{
118   \newluatexcatcodetable\cctb@seven
119   \catcode12345=14 %
120   \luatexsavecatcodetable\cctb@seven
121   \catcode12345=12 %
122 }
123
124 %% Save the register 7 (to a temporary register)
125 \unless\ifnum\lltxb@catcodetable@alloc<7 %
126   \ltj@@copy@cctb{7}{\ltj@@tempcctb@a}
127 \fi
128
129 %-------------------
130
131 \begingroup
132 \endlinechar=13
133 \gdef\newluatexcatcodetable#1{%
134   \ifnum\lltxb@catcodetable@alloc<\ltj@cctb@limit\relax
135     \global\advance\lltxb@catcodetable@alloc\tw@
136     \allocationnumber\lltxb@catcodetable@alloc
137     \global\chardef#1\allocationnumber
138     \luatexinitcatcodetable\allocationnumber
139     \begingroup\escapechar\m@ne
140     \luatexbase@directlua{luatexbase.catcodetabledef_from_tex(
141       '\luatexluaescapestring{\string#1}', '\number\allocationnumber')}%
142     \endgroup
143     \wlog{\string#1=\string\luatexcatcodetable\the\allocationnumber}%
144   \else
145     \errmessage{No room for a new \string\luatexcatcodetable}%
146   \fi}
147 \endgroup
148
149 %-------------------
150
151 % The luatex package stores its preset settings to registers 1, 3, 5, 7
152 % and new user allocations go to odd number 9, 11, 13, etc.; registers
153 % with even number are reserved for the "category code table stack".
154
155 % I relocate all the future allocation by luatexbase-cctb to the "higher"
156 % area (number >= \ltj@cctb@lower@stor@size), which is simply done by
157 % moving the allocation pointer. And all the preset settings of
158 % luatexbase-cctb (those of luatex-pkg is a subset) are prepared again.
159 % The colliding user allocation cannot be helped, but in the fortunate
160 % case where luatex-pkg is not yet loaded or its allocating function
161 % is never invoked, the only endangered user allocation is register 7
162 % of luatexbase-cctb, which is rescued by simply reassining the table,
163 % since the all presets are migrated to the higher area.
164
165 \unless\ifdefined\LuT@AllocAttribute
166   \ifltj@in@latex
167     \RequirePackage{luatex}[2010/03/01]
168   \else
169     \input luatex.sty\relax
170   \fi
171 \fi
172
173 \edef\ltj@@tempa{\meaning\LuT@AllocAttribute}
174 \edef\ltj@@tempb{\meaning\lltxb@catcodetable@alloc}
175 \ifx\ltj@@tempa\ltj@@tempb
176   % Failure: luatexbase-cctb and luatex-pkg seem to be synchronized!
177   \ltj@@cctb@failure=6 %
178 \fi
179 \ltj@@cctb@check@failure
180
181 \unless\ifnum 7=
182  \ifdefined\CatcodeTableLaTeX \CatcodeTableLaTeX
183  \else 0 \fi
184   % Failure: luatex-pkg is not of expected version!!
185   \ltj@@cctb@failure=100 %
186 \fi
187
188 \def\ltj@@cctb@org@def#1{
189   \ifnum\LuT@AllocCatcodeTable<1114110 %
190     \global\advance\LuT@AllocCatcodeTable by\tw@
191     \allocationnumber=\LuT@AllocCatcodeTable
192     \global\chardef#1=\allocationnumber
193     \wlog{
194       \string#1=\string\catcodetable\the\allocationnumber
195     }
196   \else
197     \errmessage{No room for a new \string\catcodetable}
198   \fi
199 }
200 \unless\ifx\ltj@@cctb@org@def\newcatcodetable
201   % Failure: luatex-pkg is not of expected version!!
202   \ltj@@cctb@failure=100 %
203 \fi
204
205 \def\ltj@@cctb@org@def{%
206   \ifnum\CatcodeTableStack<65534\relax
207     \global\advance\CatcodeTableStack\tw@
208   \else
209     \errmessage{CatcodeTableStack overflow}%
210   \fi}
211 \unless\ifx\ltj@@cctb@org@def\IncCatcodeTableStack
212   % Failure: luatex-pkg is not of expected version!!
213   \ltj@@cctb@failure=100 %
214 \fi
215 \ltj@@cctb@check@failure
216
217 %% Restore the register 7 which is overwritten by luatex-pkg.
218 \unless\ifnum\lltxb@catcodetable@alloc<7 %
219   \ltj@@copy@cctb{\ltj@@tempcctb@a}{7}
220 \fi
221
222 %% Move the pointer of luatexbase-cctb to higher area.
223 \lltxb@catcodetable@alloc=\ltj@cctb@lower@stor@size
224
225 %% Restrict the allocation by luatex-pkg to lower area.
226 \chardef\ltj@@cctb@lower@stor@check=
227  \numexpr\ltj@cctb@lower@stor@size-2\relax
228 \def\newcatcodetable#1{
229   \ifnum\LuT@AllocCatcodeTable<\ltj@@cctb@lower@stor@check
230     \global\advance\LuT@AllocCatcodeTable by\tw@
231     \allocationnumber=\LuT@AllocCatcodeTable
232     \global\chardef#1=\allocationnumber
233     \wlog{
234       \string#1=\string\catcodetable\the\allocationnumber
235     }
236   \else
237     \errmessage{No room for a new \string\catcodetable}
238   \fi
239 }
240 \def\IncCatcodeTableStack{%
241   \ifnum\CatcodeTableStack<\ltj@@cctb@lower@stor@check
242     \global\advance\CatcodeTableStack by\tw@
243   \else
244     \@PackageError{luatex}{%
245       Catcode table stack overflow%
246     }\@ehd
247   \fi
248 }
249
250 %------------------- Re-create the presets
251
252 \newluatexcatcodetable\CatcodeTableIniTeX
253 \newluatexcatcodetable\CatcodeTableString
254 \setluatexcatcodetable\CatcodeTableString{%
255   \luatexcatcodetable\CatcodeTableIniTeX
256   \catcode0   12 % nul
257   \catcode13  12 % carriage return
258   \catcode37  12 % percent
259   \SetCatcodeRange{65}{90}{12}% A-Z
260   \SetCatcodeRange{97}{122}{12}% a-z
261   \catcode92  12 % backslash
262   \catcode127 12 }
263 \newluatexcatcodetable\CatcodeTableOther
264 \setluatexcatcodetable\CatcodeTableOther{%
265   \luatexcatcodetable\CatcodeTableString
266   \catcode32 12 }
267 \newluatexcatcodetable\CatcodeTableLaTeX
268 \setluatexcatcodetable\CatcodeTableLaTeX{%
269   \luatexcatcodetable\CatcodeTableIniTeX
270   \SetCatcodeRange{0}{31}{15}%
271   \catcode9   10 % tab
272   \catcode12  13 % form feed
273   \catcode13  5  % carriage return
274   \catcode35  6  % hash
275   \catcode36  3  % dollar
276   \catcode38  4  % ampersand
277   \catcode94  7  % circumflex
278   \catcode95  8  % underscore
279   \catcode123 1  % brace left
280   \catcode125 2  % brace right
281   \catcode126 13 % tilde
282   \catcode127 15 }
283 \newluatexcatcodetable\CatcodeTableLaTeXAtLetter
284 \setluatexcatcodetable\CatcodeTableLaTeXAtLetter{%
285   \luatexcatcodetable\CatcodeTableLaTeX
286   \catcode64=11 }
287 \newluatexcatcodetable\CatcodeTableExpl
288 \setluatexcatcodetable\CatcodeTableExpl{%
289   \luatexcatcodetable\CatcodeTableLaTeX
290   \catcode126 10 % tilde is a space char
291   \catcode32  9  % space is ignored
292   \catcode9   9  % tab also ignored
293   \catcode95  11 % underscore letter
294   \catcode58  11 % colon letter
295   }
296 \luatexbase@directlua{luatexbase.catcodetable_do_shortcuts()}
297
298 %%------------------ all done
299 \ltj@cctbreg@AtEnd
300 \endinput
301 %% EOF