OSDN Git Service

Changed line-breaking code (CR*LF -> LF).
[luatex-ja/luatexja.git] / doc / ajt-devel-ltja.tex
1 %#!lualatex ajt-devel-ltja
2 \documentclass{ajt}
3
4 %%% Packages used in this paper
5
6 %%% Font setting for \LuaTeX; this is extract from ajt.cls
7 \makeatletter
8   \if@print
9     \RequirePackage{fontspec,xunicode}
10     \RequirePackage{luatextra}
11     \setmainfont[Mapping=tex-text]{Palatino LT Std}
12     \setsansfont[Mapping=tex-text]{Optima LT Std}
13   \else
14     \RequirePackage{fontspec,luatextra}
15     \setmainfont[Mapping=tex-text]{TeX Gyre Pagella} % \simeq Palatino
16   \fi
17
18 %%% LuaTeX-ja
19 \usepackage{luatexja,luatexja-fontspec}
20 \ltjsetparameter{jacharrange={-3,-8}}
21 \DeclareFontShape{JY3}{mc}{m}{n}{<-> s*[0.92489] file:ipam.ttf:jfm=ujis}{}
22 \DeclareFontShape{JY3}{gt}{m}{n}{<-> s*[0.92489] file:ipag.ttf:jfm=ujis}{}
23 % quick hack: monospaced Japanese font by \ttfamily
24 \DeclareKanjiFamily{JY3}{\ttdefault}{}{}
25 \DeclareFontShape{JY3}{\ttdefault}{m}{n}{<-> s*[0.92489] file:ipag.ttf:jfm=mono}{}
26
27
28 %%% LTXexample environment
29 \usepackage{showexpl,lltjlisting}
30 \lstset{basicstyle=\ttfamily\small, width=0.3\textwidth,  basewidth=.5em}
31
32 %%% Verbatim environment
33 \usepackage{fancyvrb}
34 \CustomVerbatimEnvironment{code}{Verbatim}%
35 {numbers=left,xleftmargin=1.5em,baselinestretch=1.069,fontsize=\small}
36 \CustomVerbatimEnvironment{codewithoutnum}{Verbatim}%
37 {xleftmargin=1.5em,baselinestretch=1.069,fontsize=\small}
38 \CustomVerbatimEnvironment{codewithoutnumsmall}{Verbatim}%
39 {xleftmargin=1.5em,baselinestretch=1.0,fontsize=\footnotesize}
40 \DefineShortVerb{\|}
41
42 %%% Others
43 \usepackage{mflogo,booktabs}
44 \definecolor{grayx}{gray}{0.85}
45
46 %%% Mandatory article metadata %%%
47 \title{Development of \LuaTeX-ja package}
48 \author{Hironori Kitagawa {\normalsize 北川 弘典}}
49 \address{\LuaTeX-ja project team}
50 \email{h\_kitagawa2001@yahoo.co.jp}
51
52 \keywords{\TeX, p\TeX, \LuaTeX, \LuaTeX-ja, Japanese}
53 \abstract{%
54 \LuaTeX-ja package is a macro package for typesetting Japanese
55 documents under \LuaTeX.  The package has more flexibility of
56 typesetting than \pTeX, which is widely used Japanese extension of \TeX,
57 and has corrected some unwanted features of \pTeX.
58 In this paper, we describe specifications, the current status and some
59 internal processing methods of \LuaTeX-ja.
60 }
61
62 \newcommand{\parname}[1]{\textsf{#1}}
63 \newcommand{\jstrut}{\vrule width0pt height\cht depth\cdp}
64 \newcommand{\imagfm}[1]{\ifvmode\leavevmode\fi%
65   \hbox{\fboxsep=0pt\fbox{\setbox0=\hbox{#1}\copy0\kern-\wd0
66   \smash{\vrule width \wd0 height 0.4pt depth0.4pt}}}}
67 \begin{document}
68
69 %%% Do not forget to start with \maketitle!
70 \maketitle
71
72 \section{Introduction}
73 \subsection{History}
74 To typeset Japanese documents with \TeX, ASCII \pTeX~\cite{ptex} has
75 been widely used in Japan.  There are other methods---for example, using
76 Omega and OTP~\cite{omega}, or with the CJK package---to do so, however,
77 these alternative methods did not become a majority.  The author thinks
78 that this is because \pTeX\ enables us to produce high-quality documents
79 (e.g.,~supporting vertical typesetting), and the appearance of \pTeX\ is
80 earlier than that of alternatives described above.
81
82 However, \pTeX\ has been left behind from the extensions of \TeX\
83 such as \eTeX\ and \pdfTeX, and the diffusion of UTF-8 encoding.  In
84 recent years, the situation has become better, because of development
85 of |ptexenc|~\cite{ptexenc} by Nobuyuki Tsuchimura (\hbox{土村展之}),
86 $\varepsilon$-\pTeX~\cite{eptex} by the author,~and u\pTeX~\cite{uptex}
87 by Takuji Tanaka (田中琢爾). However, continuing this approach, namely, to develop
88 an engine extension localized for Japanese, is not wise. This approach
89 needs lots of work for \emph{each} engine, and since \LuaTeX\ has an ability
90 to hook \TeX's internal process by using Lua callbacks, the necessity of
91 an engine extension is getting smaller.
92
93
94 There were several experimental attempts to typeset
95 Japanese documents with \LuaTeX\ before. Here we cite three examples:
96 \begin{itemize}
97 \item |luaums.sty|~\cite{luaums} developed by the author. This
98       experimental package is for creating a certain Japanese-based presentation
99       with \LuaTeX.
100 \item the \emph{luajalayout} package~\cite{luajalayout}, formerly known as the
101       \emph{jafontspec} package, by Kazuki Maeda (前田一貴). This package is based on
102       \LaTeXe\ and \emph{fontspec} package.
103 \item the \emph{luajp-test} package~\cite{luajp-test}, a test package made by
104       Atsuhito Kohda (香田温人), based on articles on the web page~\cite{joylua}.
105 \end{itemize}
106 However, these packages are based on \LaTeXe, and do not have much
107 ability to control the typesetting rule. And it is inefficient that more
108 than one people separately develop similar packages.  Development of the
109 \LuaTeX-ja package is started initially by the author and Kazuki Maeda, because of
110 these situations.
111
112 \subsection{Development policy of \LuaTeX-ja}
113 \label{ssec-pol} 
114 The first aim of \LuaTeX-ja project is to implement features (from the
115 `primitive' level) of \pTeX\ as macros under \LuaTeX, so \LuaTeX-ja is
116 much affected by \pTeX.  However, as development proceeds, some
117 technical/conceptual difficulties are arisen. Hence we changed the aim
118 of the project as follows:
119 \begin{itemize}
120 \item\emph{\LuaTeX-ja offers at least the same flexibility of
121      typesetting that p\TeX\ has.}
122
123      We think that the ability of producing outputs conformed to
124      JIS~X~4051~\cite{jisx4051}, the Japanese Industrial Standard for
125      typesetting, or to a technical note~\cite{w3c} by W3C is not enough;
126      if one wants to produce very incoherent outputs for some reason, it
127      should be possible.
128 In this point, previous attempts of Japanese typesetting with \LuaTeX\
129      which we cited in the previous subsection are inadequate.
130
131 \pTeX\ has some flexibility of typesetting, by changing internal
132      parameters such as |\kanjiskip| or |\prebreakpenalty|, and by using
133      custom JFM (Japanese TFM). Therefore we decided to include these
134      functionality to \LuaTeX-ja.
135
136 \item\emph{\LuaTeX-ja isn't mere re-implementation or porting of \pTeX;
137      some (technically and/or conceptually) inconvenient features of
138      \pTeX\ are modified.} 
139
140      We describe this point in more detail at the next section.
141 \end{itemize}
142
143
144 \subsection{Overview of the processes}
145 \label{ssec-over}
146 We describe an outline of \LuaTeX-ja's process in order.
147 \begin{itemize}
148 \item In the |process_input_buffer| callback: treatment of breaking
149       lines after a Japanese character (in Subsection~\ref{ssec-line}).
150
151 \item In the |hyphenate| callback: font replacement.
152
153 \LuaTeX-ja looks into for each \textit{glyph\_node}~$p$ in the list. If
154            the character represented by $p$ is considered as a Japanese
155            character, the font used in $p$ is replaced by the value of
156            |\ltj@curjfnt|, an attribute for `the current Japanese font'
157            at~$p$.
158
159 Furthermore the subtype of $p$ is subtracted by 1 to suppress
160            hyphenation around it by \LuaTeX, because later processes of
161            \LuaTeX-ja take care of all things about Japanese characters.
162
163 \item In |pre_linebreak_filter| and |hpack_filter| callbacks:
164
165 \begin{enumerate}
166 \item \LuaTeX-ja has its own stack system, and the current horizontal
167       list is traversed in this stage to determine what is the level of
168       \LuaTeX-ja's internal stack at the end of the list (in
169       Subsection~\ref{ssec-stack}).
170
171 \item In this stage, \LuaTeX-ja inserts glues/kerns for Japanese
172       typesetting in the list. This is the core of \LuaTeX-ja (in
173       Subsection~\ref{ssec-jglue}).
174
175 \item To make a match between a metric and a real font, sometimes
176       adjustument of the position of (Japanese) glyphs are performed
177       (Subsection~\ref{ssec-width}).
178 \end{enumerate}
179 \item In the |mlist_to_hlist| callback: replacement of Japanese characters in math formulas.
180 This stage is similar to adjustument of the position of glyphs (see
181       above), so we omit it from this paper.
182 \end{itemize}
183
184 \subsection{Contents of this paper}
185 Here we describe the contents of the rest of this paper briefly.  In
186 Section~\ref{sec:differences_with_ptex},
187 we describe major differences between \pTeX\ and \LuaTeX-ja.
188 The next section, Section~\ref{sec:distinction_of_characters},
189 is concentrated on a problem `how we
190 distinguish between Japanese characters and alphabetic characters'. In
191 Section~\ref{sec:current_status}, we show rest of features of \LuaTeX-ja package, and
192 current status of the package.  Finally, in Section~\ref{sec:implementation}, we describe some
193 internal routines of \LuaTeX-ja.
194
195 \subsection{General information of the project}
196 This \LuaTeX-ja project is hosted by SourceForge.jp. The official wiki
197 is located on
198 \url{http://sourceforge.jp/projects/luatex-ja/wiki/}.  There is
199 no stable version on October 15, 2011, however a set of developer sources can be
200 obtained from the git repository.  Members of the project team are as follows
201 (in random order): Hironori Kitagawa, Kazuki Maeda, Takayuki Yato,
202 Yusuke Kuroki, Noriyuki Abe, Munehiro Yamamoto, Tomoaki Honda,
203 and~Shuzaburo Saito.
204
205
206 \section{Major differences with \pTeX}
207 \label{sec:differences_with_ptex}
208 In this section, we explain several major differences between \pTeX\
209 and our \LuaTeX-ja.  For general information of Japanese typesetting and the
210 overview of \pTeX, please see Okumura~\cite{ptexjp}.
211
212
213 \subsection{Names of control sequences}
214 \label{ssec-csname} Because \pTeX\ is an engine modification of Knuth's
215 original \TeX82 engine, some primitives added by it take a form that is
216 very difficult to be simulated by a macro.  For example, an additional
217 primitive |\prebreakpenalty|$\langle\hbox{\it
218 char\_code}\rangle$|[=]|$\langle\hbox{\it penalty}\rangle$ in \pTeX\
219 sets the amount of penalty inserted before a character whose code is
220 $\langle\hbox{\it char\_code}\rangle$ to $\langle\hbox{\it
221 penalty}\rangle$, and this form |\prebreakpenalty|$\langle\hbox{\it
222 char\_code}\rangle$ can be also used for retrieving the value.
223
224 Moreover, there are some parameters which values of them at the end of a
225 horizontal box or that of a paragraph are effective in whole box or
226 paragraph.  These parameters were implemented as additional internal
227 parameters in \pTeX. However, the implementation of these parameters in
228 \LuaTeX-ja is not so easy; we will discuss it in
229 Subsection~\ref{ssec-stack}.
230
231 From above two~problems we discussed above, the assignment and retrieval
232 of most parameters in \LuaTeX-ja are summarized into the following
233 three~control sequences:
234 \begin{itemize}
235 \item |\ltjsetparameter{|$\langle\hbox{\it
236       name}\rangle$|=|$\langle\hbox{\it value}\rangle$|,...}|: for local
237       assignment.
238 \item |\ltjglobalsetparameter|: for global assignment. These two control
239       sequences obey the value of |\globaldefs| primitive.
240 \item |\ltjgetparameter{|$\langle\hbox{\it
241       name}\rangle$|}[{|$\langle\hbox{\it optional
242       argument}\rangle$|}]|: for retrieval. The returned value is always
243       a string.
244 \end{itemize}
245
246 \subsection{Line-break after a Japanese character}
247 \label{ssec-line} 
248
249 Japanese texts can break lines almost everywhere, in contrast with
250 alphabetic texts can break lines only between words (or use
251 hyphenation). Hence, \pTeX's input processor is modified so that a
252 line-break after a Japanese character doesn't emit a space. However,
253 there is no way to customize the input processor of \LuaTeX, other than
254 to hack its CWEB-source. All a macro package can do is to modify an input line before
255 when \LuaTeX\ begin to process it, inside the |process_input_buffer|
256 callback.
257
258 Hence, in \LuaTeX-ja, a comment letter (we reserve U+FFFFF for this
259 purpose) will be appended to an input line, if this line ends with a Japanese
260 character.\footnote{Strictly speaking, it also requires that the catcode
261 of the end-line character is 5~(\emph{end-of-line}). This condition is
262 useful under the verbatim environment.}  One might jump to a conclusion
263 that the treatment of a line-break by \pTeX\ and that of \LuaTeX-ja are
264 totally same, however they are different in the respect that \LuaTeX-ja's
265 judgement whether a comment letter will be appended the line is done
266 \emph{before} the line is actually processed by \LuaTeX.
267
268 Figure~\ref{fig-linebreak} shows an example of this situation; the
269 command at the first line marks most of Japanese characters as
270 `non-Japanese characters'. In other words, from that command onward, the
271 letter `あ' will be treated as an alphabetic character by
272 \LuaTeX-ja. Then, it is natural to have a space between `あ' and `y' in
273 the output, where the actual output in the figure does not so.  This is
274 because `あ' is considered a Japanese character by \LuaTeX-ja,
275 when \LuaTeX-ja does a decision whether U+FFFFF will be added to the
276 input line~2.
277
278 \begin{figure}
279 \begin{LTXexample}
280 \font\x=IPAMincho \x
281 \ltjsetparameter{jacharrange={-6}}xあ
282 y
283 \end{LTXexample}
284 \caption{A notable sample showing the treatment of a line-break after a
285 Japanese character.}\label{fig-linebreak}
286 \end{figure}
287
288 \subsection{Separation between `real' fonts and metrics}
289 \label{ssec-sepmet}
290
291 Traditionally, most Japanese fonts used in typesetting are not
292 proportional, that is, most glyphs have same size (in most cases,
293 square-shaped). Hence, it is not rare that the contents of different
294 JFMs are essentially same, and only differ in their names. For example,
295 |min10.tfm| and |goth10.tfm|, which are JFMs shipped with \pTeX\ for
296 seriffed \emph{mincho} family and sans-seriffed \emph{gothic} family,
297 differ their |FAMILY| and |FACE| only. Moreover, |jis.tfm| and
298 |jisg.tfm|, which consists a parts of \emph{jis} font metric, which is
299 used in \emph{jsclasses}~\cite{jsclasses} by Haruhiko Okumura (奥村晴彦),
300 are totally same as binary files.  Considering this situation, we
301 decided to separate `real' fonts and metrics used for them in
302 \LuaTeX-ja. Typical declarations of Japanese fonts in the style of plain
303 \TeX\ are shown in Figure~\ref{fig-jfdef}. We would like to add several
304 remarks:
305 \begin{itemize}
306 \item A control sequence |\jfont| must be used for Japanese fonts, instead of |\font|.
307 \item \LuaTeX-ja automatically loads the \emph{luaotfload} package, so
308       |file:| and |name:| prefixes, and various font features can be
309       used as the line~1 in Figure~\ref{fig-jfdef}.
310 \item The |jfm| key specifies the metric for the font. In
311       Figure~\ref{fig-jfdef}, both fonts will use a metric stored in a
312       Lua script named |jfm-ujis.lua|. This metric is the standard
313       metric in \LuaTeX-ja, and is based on JFMs used in the \emph{otf}
314       package~\cite{otf}.
315 \item The |psft:| prefix can be used to specify name-only, non-embedded
316       fonts. When one display a pdf with these fonts, actual fonts which
317       will be used for them depend on a pdf reader. 
318 \end{itemize}
319 The specification of a metric for \LuaTeX-ja is similar to that of a JFM
320 (see \cite{ptexjp}); characters are grouped into several classes, the
321 size information of characters are specified for each class, and
322 glue/kern insertions are specified for each pair of classes. Although
323 the author have not tried, it may be possible to develop a program that
324 `converts' a JFM to a metric for \LuaTeX-ja.  \LuaTeX-ja offers three
325 metrics by default; |jfm-ujis.lua|, |jfm-jis.lua| based on the
326 \emph{jis} font metric, and |jfm-min.lua| based on old |min10.tfm|.
327
328  Note that |-kern| in features
329 is important, because kerning information from real font itself will
330 clash with glue/kern informations from the metric.
331
332 \begin{figure}
333 \begin{verbatim}
334 \jfont\foo=file:ipam.ttf:jfm=ujis;script=latn;-kern;+jp04 at 12pt
335 \jfont\bar=psft:Ryumin-Light:jfm=ujis at 10pt
336 \end{verbatim}
337 \caption{Typical declarations of Japanese fonts.}
338 \label{fig-jfdef}
339 \end{figure}
340
341 \subsection{Insertion of glues/kerns for Japanese typesetting: timing}
342 \label{ssec-jglue}
343
344 As described in \cite{luatexref}, \LuaTeX's kerning and ligaturing
345 processes are totally different from those of \TeX82.  \TeX82's process is
346 done just when a (sequence of) character is appended to the current
347 list. Thus we can interrupt this process by writing as
348 |f{}irm|. However, \LuaTeX's process is \emph{node-based}, that is, the
349 process will be done when a horizontal box or a paragraph is ended, so
350 |f{}irm| and |firm| yield  same outputs under \LuaTeX.
351
352 The situation for Japanese characters is more complicated.
353 Glues (and kerns) which are needed for Japanese
354 typesetting will be divided into the following three categories:
355 \begin{itemize}
356 \item Glue (or kern) from the metric of Japanese fonts (\emph{JFM glue},
357       for short). 
358
359 \item Default glue between a Japanese character and an alphabetic
360       character (\emph{xkanjiskip}, for short), usually 1/4 of
361       full-width (\emph{shibuaki}) with some stretch and shrink for
362       justifying each line.
363 \item Default glue between two consecutive Japanese characters
364       (\emph{kanjiskip}, for short). The main reason of this glue is to
365       enable breaking lines almost everywhere in Japanese texts. In most
366       cases, its natural width is zero, and some stretch/shrink for
367       justifying each line.
368 \end{itemize}
369 In \pTeX, these three kinds of glues are treated differently. A JFM glue
370 is inserted when a (sequence of) Japanese character is appended to the
371 current list, same as the case of alphabetic characters in \TeX82. This
372 means that one can interrupt the insertion process by saying |{}|.  A
373 \emph{xkanjiskip} is inserted just before `hpack' or line-breaking of a
374 paragraph; this timing is somewhat similar to that of \LuaTeX's kerning
375 process. Finally, A \emph{kanjiskip} is not appeared as a node anywhere;
376 only appears implicitly in calculation of the width of a horizontal box,
377 that of breaking lines, and the actual output process to a DVI
378 file. These specifications made \pTeX's behavior very hard to
379 understand.
380
381 \LuaTeX-ja inserts glues in all three categories simultaneously inside
382 |hpack_filter| and |pre_linebreak_filter| callbacks.  The reasons of
383 this specification are to behave like alphabetic characters in \LuaTeX\
384 (as described in the first paragraph), and to clarify the specification
385 for \LuaTeX-ja's process.
386
387 \subsection{Insertion of glues/kerns for Japanese typesetting: specification}
388 \begin{table}
389 \caption{Examples of differences between \pTeX\ and \LuaTeX-ja.}
390 \label{tab-jfmglue}
391 \begin{center}
392 \begin{tabular}{llllllll}
393 \toprule
394 &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}\\
395 Input      &|あ】{}【〙\/〘|        &|い』\/a| &|う)\hbox{}(| &|え]\special{}[|\\\midrule
396 \pTeX      &あ】\hbox{}【〙\hbox{}〘&い』\/a   &う)\hbox{}(   &え]\hbox{}[\\
397 \LuaTeX-ja &あ】{}【〙\/〘          &い』\/a   &う)\hbox{}(   &え]\special{}[\\
398 \bottomrule
399 \end{tabular}
400 \end{center}
401 \end{table}
402
403 \begin{figure}
404 \begin{center}
405 \fontsize{40}{40}\selectfont
406 \imagfm{\jstrut あ}%
407 \imagfm{\jstrut 】\inhibitglue}%
408 \imagfm{\jstrut\kern.5\zw}%
409 \imagfm{\jstrut\kern.5\zw}%
410 \imagfm{\jstrut\inhibitglue【}%
411 \imagfm{\jstrut 〙\inhibitglue}%
412 \imagfm{\jstrut\kern.5\zw}%
413 \imagfm{\jstrut\kern.5\zw}%
414 \imagfm{\jstrut\inhibitglue〘}%
415 \end{center}
416 \caption{Detail of (1) in Table~\ref{tab-jfmglue}.}
417 \label{fig-ptexjfm}
418 \end{figure}
419
420 Now we will take a look inside the insertion process itself, and describe 4~points.
421
422 \begin{description}
423 \item[Ignored Nodes]
424 As noted in the previous subsection, the insertion process in \pTeX\ can
425            be interrupted by saying |{}| or anything else\footnote{This
426            is why some tricks like \texttt{ちょ\char`\{\char`\}っと} for
427            \texttt{min10.tfm} and other `old' JFMs work.}. This leads
428            the second row in Table~\ref{tab-jfmglue}, or
429            Figure~\ref{fig-ptexjfm}. `The process is interrupted' means
430            that \pTeX\ does not think the letter `】\inhibitglue' is
431            followed by `\inhibitglue【', hence two half-width glues are
432            inserted between between `】\inhibitglue' and `\inhibitglue【',
433            where one is from `】\inhibitglue' and another is from
434            `\inhibitglue【'.
435
436            On the other hand, in \LuaTeX-ja, the process is done inside
437            |hpack_filter| and |pre_linebreak_filter| callbacks. Hence,
438            \emph{anything that does not make any node will be
439            ignored}\ in \LuaTeX-ja, as shown in (1) in
440            Table~\ref{tab-jfmglue}. \LuaTeX-ja also ignores any nodes
441            which does not make any contribution to current horizontal
442            list---\emph{ins\_node}, \emph{adjust\_node},
443            \emph{mark\_node}, \emph{whatsit\_node} and
444            \emph{penalty\_node}---, as shown in (4).
445
446
447 By the way, around a \emph{glyph\_node} $p$ there may be some nld odes
448            attached to $p$. These are an accent and kerns for
449            positioning it, and a kern from the italic
450            correction\footnote{\TeX82 (and \LuaTeX) does not distinguish
451            between explicit kern and a kern for italic correction. To
452            distinguish them, an additional subtype for kern is introduced
453            in \pTeX. On the other hand, \LuaTeX-ja uses an additional attribute and
454            redefines \texttt{\char`\\/}.} for $p$. It is natural that
455            these attachments should be ignored inside the process. Hence
456            \LuaTeX-ja takes this approach, as the latest version of
457            \pTeX\ (p3.2). This explains (2) in the figure.
458
459 Summerizing above, one should put an empty horizontal box |\hbox{}| to
460            where he wants to interrupt the insertion process in
461            \LuaTeX-ja as (3) in the figure.
462
463 \item[Fonts with the Same Metric]
464 Recall that \LuaTeX-ja separated `real' fonts and metrics, as in Subsection~\ref{ssec-sepmet}. 
465 Consider the following input, where all Japanese fonts use same metric
466            (in \LuaTeX-ja), and |\gt| selects \emph{gothic} family for
467            the current Japanese font family:
468 \begin{quote}
469 \begin{verbatim}
470 明朝)\gt (ゴシック
471 \end{verbatim}
472 \end{quote}
473 If the above input is processed by \pTeX, because the insertion process is
474            interrupt by |\gt|, the result looks like
475 \begin{quote}
476 \mc 明朝)\hbox{}\gt (ゴシック
477 \end{quote}
478 However this seems to be unnatural, since two Japanese fonts in the
479            output use the same metric, i.e.,~the same
480            typesetting rule.  Hence, we decided that Japanese fonts with
481            the same metric are treated as one font in the insertion
482            process of \LuaTeX-ja. Thus, the output from the above input
483            in \LuaTeX-ja looks like:
484 \begin{quote}
485 \mc 明朝)\gt (ゴシック
486 \end{quote}
487 One might have the situation that this default behavior is not
488            suitable. \LuaTeX-ja offers a way to cope with this case, but
489            we leave it to the manual~\cite{man}.
490
491 \item[Fonts with Different Metrics] 
492 In the case where two consecutive Japanese characters use different metrics and/or
493            different size is similar. Consider the following input where
494            the \emph{mincho} family and the \emph{gothic} family use
495            different metrics:
496 \begin{quote}
497 \begin{verbatim}
498 漢)\gt (漢)\large (大
499 \end{verbatim}
500 \end{quote}
501 As the previous paragraph, this input yields the following, by \pTeX:
502 \begin{quote}
503 \mc 漢)\hbox{}\gt (漢)\hbox{}\large (大
504 \end{quote}
505 We thought that amounts of spaces between parentheses in above output
506            are too much. So we changed the default behavior of
507            \LuaTeX-ja so that the amount of a glue between two Japanese
508            characters with different metrics is the average of a glue
509            from the left character and that from the right
510            character. For example, Figure~\ref{fig-diffmet} shows the
511            output from above input. The width of glue indicated `(1)' is
512            $(a/2 + a/2)/2 = 0.5a$, and the width of glue indicated `(2)'
513            is $(a/2 + 1.2a/2)/2 = 0.55a$. This default behavior can be
514            changed by \textsf{diffrentmet} parameter of \LuaTeX-ja.
515
516 \begin{figure}
517 \begin{center}
518 \fontsize{40}{40}\selectfont
519 \imagfm{\jstrut\smash{%
520   \vtop{\lineskiplimit=\maxdimen\lineskip2pt\halign{#\cr漢\cr
521     \small\vrule height .5ex depth .5ex\hrulefill\ \lower.5ex\hbox{$a$}\ 
522     \hrulefill\vrule height .5ex depth .5ex\cr}}}}%
523 \imagfm{\jstrut )\inhibitglue}%
524 \hbox to .5\zw{\hss\normalsize (1)\hss}%
525 \imagfm{\jstrut\inhibitglue\gt (}%
526 \imagfm{\jstrut\gt 漢}%
527 \imagfm{\jstrut\gt )\inhibitglue}%
528 \hbox to .55\zw{\hss\normalsize (2)\hss}%
529 \imagfm{\fontsize{48}{48}\selectfont\jstrut\gt\inhibitglue (}%
530 \imagfm{\fontsize{48}{48}\selectfont\jstrut\smash{%
531   \vtop{\lineskiplimit=\maxdimen\lineskip2pt\halign{#\cr\gt 大\cr
532     \small\vrule height .5ex depth .5ex\hrulefill\ \lower.5ex\hbox{$1.2a$}\ 
533     \hrulefill\vrule height .5ex depth .5ex\cr}}}}
534 \end{center}
535 \caption{Fonts with different metrics.}
536 \label{fig-diffmet}
537 \end{figure}
538
539 \item[\emph{kanjiskip} and \emph{xkanjiskip}]
540 In \pTeX, the value of \emph{xkanjiskip} is controlled by a skip named
541            |\xkanjiskip|. A defect of this implementation is that the
542            value of \emph{xkanjiskip} is not connected with the size of
543            the currnt Japanese font. It seems that |EXTRASPACE|,
544            |EXTRASTRETCH|, |EXTRASHRINK| parameters in a JFM are
545            reserved for specifying the default value of
546            \emph{xkanjiskip} in a unit of the design size, but \pTeX\
547            did not use these parameters. 
548
549 Considering this situation of p\TeX, \LuaTeX-ja can use the value of
550            \emph{xkanjiskip} that specified in a metric. If the value of
551            \emph{xkanjiskip} on user side (this is the
552            \textsf{xkanjiskip} parameter in |\ltjsetparameter|) is
553            |\maxdimen|, then \LuaTeX-ja use the specification from
554            the current used metric as the actual value of
555            \emph{xkanjiskip}.
556 This description also applies for \emph{kanjiskip}.
557 \end{description}
558
559 \section{Distinction of characters}
560 \label{sec:distinction_of_characters}
561 Since \LuaTeX\ can handle Unicode characters natively, it is a major
562 problem that how we distinguish Japanese characters and alphabetic
563 characters. For example, the multiplication sign (U+00D7) exists both in
564 ISO-8859-1 (hence in Latin-1 Supplement in Unicode) and in the basic
565 Japanese character set JIS~X~0208. It is not desirable that this
566 character is treated as an alphabetic char, because this symbol is often
567 used in the sense of `negative' in Japan. 
568
569 \subsection{Character ranges}
570 Before we describe the approach taken is \LuaTeX-ja, we review the
571 approach taken by u\pTeX.  u\pTeX\ extends the |\kcatcode| primitive in
572 \pTeX, to use this primitive for setting how a character is treated
573 among alphabetic characters~(15), \emph{kanji}~(16), \emph{kana}~(17),
574 \emph{kanji}, \emph{Hangul}~(17), or~\emph{other CJK characters}~(18).
575 The assignment to |\kcatcode| can be done by a Unicode
576 block\footnote{There are some exceptions. For example, U+FF00--FFEF
577 (Halfwidth and Fullwidth Forms) are divided into three blocks in recent
578 u\pTeX.}.
579
580 \LuaTeX-ja adopted a different approach. There are many Unicode blocks
581            in Basic Multilingual Plane which are not included in
582            Japanese fonts, it is inconvenient if we treat by a Unicode
583            block.  Furthermore, JIS~X~0208 are not just union of Unicode
584            blocks; for example, the intersection of JIS~X~0208 and
585            Latin-1 Supplement is shown in
586            Table~\ref{tab-inter}. Considering these two points, to
587            customize the range of Japanese characters in \LuaTeX-ja, one
588            has to define ranges of character codes in his source in advance.
589
590
591 \begin{table}
592 \caption{Intersection of JIS~X~0208 and Latin-1 Supplement.}
593 \label{tab-inter}
594 \begin{center}
595 \begin{tabular}{llll}
596 \ltjjachar"A7 (U+00A7),&
597 \ltjjachar"A8 (U+00A8),&
598 \ltjjachar"B0 (U+00B0),&
599 \ltjjachar"B1 (U+00B1),\\
600 \ltjjachar"B4 (U+00B4),&
601 \ltjjachar"B6 (U+00B6),&
602 \ltjjachar"D7 (U+00D7),&
603 \ltjjachar"F7 (U+00F7)
604 \end{tabular}
605 \end{center}
606 \end{table}
607
608 %%Example...
609
610 We note that \LuaTeX-ja offers two additional control sequence,
611       |\ltjjachar| and |\ltjalchar|. They are similar to |\char|
612       primitive, but |\ltjjachar| always yields a Japanese character (if
613       the argument is more than or equal to 128) and |\ltjalchar| always
614       yields an alphabetic character, regardless of the argument. 
615
616 \subsection{Default setting of ranges}
617 Patches for plain \TeX\ and \LaTeXe of \LuaTeX-ja predefines 8~character
618 ranges, as shown in Table~\ref{tab-chrrng}.  Almost of these ranges are
619 just the union of Unicode blocks, and determined from the Adobe-Japan1-6
620 character collection~\cite{aj16}, and JIS~X~0208. Among these 8~ranges,
621 the ranges~2, 3, 6, 7, and~8 are considered ranges of Japanese
622 characters, and others are considered ranges of alphabetic
623 characters\footnote{Note that ranges 3~and~8 are considered ranges of
624 alphabetic characters in this paper.}. We remark on ranges 2~and~8:
625 \begin{description}
626 \item[The range~2]
627 JIS~X~0208 includes Greek letters and Cyrillic letters, however, these
628            letters cannot be used for typesetting Greek or Russian, of
629            course. Hence it is reasonable that Greek letters and
630            Cyrillic consist another character range.
631 \item[The range~8] 
632 If one want to use 8-bit TFMs, such as T1 or TS1 encodings, he should
633            mark this range~8 as a range of alphabetic characters by
634 \begin{quote}
635 |\ltjsetparameter{jacharrange={-8}}|
636 \end{quote}
637 This is because some 8-bit TFMs have a glyph in this range; for example,
638            the character `\OE' is located at |"D7| in the T1 encoding. %"
639 \end{description}
640
641
642 \begin{table}
643 \caption{Predefined ranges in \LuaTeX-ja.}
644 \label{tab-chrrng}
645 \begin{center}
646 \begin{tabular}{@{\bf}rl}
647 1&(Additional) Latin characters which are not belonged in the range~8.\\
648 2&Greek and Cyrillic letters.\\
649 3&Punctuations and miscellaneous symbols.\\
650 4&Unicode blocks which does not intersect with Adobe-Japan1-6.\\
651 5&Surrogates and supplementary private use Areas.\\
652 6&Characters used in Japanese typesetting.\\
653 7&Characters possibly used in CJK typesetting, but not in Japanese.\\
654 8&Characters in Table~\ref{tab-inter}.
655 \end{tabular}
656 \end{center}
657 \end{table}
658
659 \subsection{Control sequences producing Unicode characters}
660 \label{ssec-unichar}
661
662 The \emph{fontspec} package\footnote{Preciously
663 saying, it is the \emph{xunicode} package, originally a package for
664 \XeTeX and automatically loaded by the \emph{fontspec} package.} offer
665 various control sequences that produce Unicode characters.  However, they as
666 it stands cannot work with the default range setting of \LuaTeX-ja.  For
667 example, |\textquotedblleft| is just an abbreviation of
668 |\char"201C\relax| %"
669 and the character U+201C (LEFT DOUBLE QUOTATION
670 MARK) is treated as an Japanese character, because it belongs to the
671 range~3. 
672 This problem is resolved by using |\ltjalchar| instead of the |\char| primitive. 
673 It is included in an optional package named \texttt{luatexja-\penalty0fontspec.sty}.
674 Figure~\ref{fig-unitxt} ...
675
676 \begin{figure}
677 \begin{LTXexample}
678 ×, \char`×,   % depend on range setting 
679 \ltjalchar`×, % alphabetic char
680 \ltjjachar`×, % Japanese char
681 \texttimes     % alph. char (by fontspec)
682 \end{LTXexample}
683 \caption{Control sequences producing a Unicode character.}
684 \label{fig-unitxt}
685 \end{figure}
686
687 The situation looks similar in math formulas, but in fact it differs.
688 Control sequences that represents ordinary symbols defined by the
689 \emph{unicode-math} package is just synonym of a character. For example,
690 the meaning of |\otimes| is just the character U+2297 (CIRCLED TIMES),
691 which is included in the range~3.  However, it is difficult to define a
692 control sequence like |\ltjalUmathchar| as a counterpart of
693 |\Umathchar|, since an input like `|\sum^\ltjalUmathchar ...|' has to be
694 permitted.
695
696 However, we couldn't include a solution to this problem in time for this
697 paper, due to a lack of time. We are just testing a solution that we
698 will explain it below:
699 \begin{itemize}
700 \item \LuaTeX-ja has a list of character codes which will be treated as
701       alphabetic characters in math mode. Considering 8-bit TFMs for
702       math symbols, this list includes natural numbers between |"80| and
703       |"FF| by default.
704 \item Redefine internal commands defined in the \emph{unicode-math}
705       package so that
706 codes of characters which are mentioned in the \emph{unicode-math}
707       package will be included in the list.
708 \end{itemize}
709
710
711 We would like to extend treatments described in this section to 8-bit
712 font encodings, but we leave it to further development too.
713
714 \section{Current status of development}
715 \label{sec:current_status}
716 At the moment, \LuaTeX-ja can be used under plain \TeX, and under
717 \LaTeXe. Generally speaking, one only has to read |luatexja.sty|, by
718 |\input| command or |\usepackage| (in~\LaTeXe), if you merely want to
719 typeset Japanese characters.  We look more detail by parts. 
720
721 \subsection{`Engine extension'}
722 The lowest part of \LuaTeX-ja corresponds the \pTeX\ extension as
723 \emph{an engine extension of \TeX}. We, the project menbers, think that
724 this part is almost done. There is one more feature of \LuaTeX-ja which
725 we are going to explain:
726
727 \begin{description}
728 \item[Shifting Baseline]
729 In order to make a match between Japanese fonts and alphabetic fonts,
730            sometimes shifting the baseline of alphabetic characters may
731            be needed. \pTeX\ has a dimension |\ybaselineshift|, which
732            corresponds the amount of shifting down the baseline of alphabetic
733            characters. This is useful for Japanese-based documents, but
734            not for documents mainly in languages with alphabetic
735            characters.
736
737 Hence, \LuaTeX-ja extends \pTeX's |\ybaselineshift| to Japanese
738            characters. Namely, \LuaTeX-ja offers two parameters,
739            \textsf{yjabaselineshift} and \textsf{yalbaselineshift}, for the
740            amount of shifting the baseline of Japanese characters and
741            that of alphabetic characters, respectively. 
742 \begin{figure}
743 \begin{center}
744 \fontsize{40}{40}\selectfont\fboxsep0mm
745 \vrule width 0.9\textwidth height0.4pt depth0.4pt\kern-0.9\textwidth
746 \hbox to 0.9\linewidth{%
747 \hfil
748 \raise-10pt\imagfm{\jstrut 漢}%
749 \raise-10pt\imagfm{\jstrut 字}\hskip.25\zw%
750 \imagfm{p}%
751 \imagfm{h}%
752 \hfil\hfil
753 \imagfm{\jstrut 漢}%
754 \imagfm{\jstrut 字}\hskip.25\zw%
755 \raise-10pt\imagfm{p}%
756 \raise-10pt\imagfm{h}%
757 \hfil
758 }
759 \end{center}
760
761 \caption{First example of shifting baseline.}
762 \label{fig-bls}
763 \end{figure}
764
765 \begin{figure}
766 \begin{center}
767 \fontsize{30}{30}\selectfont\fboxsep0mm
768 \vrule width 0.9\textwidth height0.4pt depth0.4pt\kern-0.9\textwidth
769 \hbox to 0.9\linewidth{%
770 \hfil
771 \imagfm{a}%
772 \imagfm{b}\hskip.25\zw%
773 \imagfm{\jstrut 本}%
774 \imagfm{\jstrut 文}\hskip.33333\zw%
775 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont\jstrut\inhibitglue (}%
776 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont\jstrut 注}%
777 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont\jstrut 釈}\hskip.1666667\zw%
778 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont c}%
779 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont o}%
780 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont m}%
781 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont m}%
782 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont e}%
783 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont n}%
784 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont t}%
785 \raise3.514582pt\imagfm{\fontsize{20}{20}\selectfont\jstrut )\inhibitglue}%
786 \hskip.33333\zw%
787 \imagfm{\jstrut 本}%
788 \imagfm{\jstrut 文}%
789 \hfil
790 }
791 \end{center}
792
793 \caption{Second example of shifting baseline.}
794 \label{fig-small}
795 \end{figure}
796
797 An example output is shown in Figure~\ref{fig-bls}. The left half is the
798            output when \textsf{yjabaselineshift} is positive, hence the
799            baseline of Japanese characters is shifted down. On the other
800            hand, the right half is the output when
801            \textsf{yalbaselineshift} is positive, hence the baseline of
802            alphabetic characters is shifted. Figure~\ref{fig-small}
803            shows an intresting use of these parameters.
804
805 \end{description}
806 Note that \LuaTeX-ja doesn't support vertical typesetting, \emph{tategaki}, for now. 
807
808 \subsection{Patches for plain \TeX\ and \LaTeXe}
809 \pTeX\ has a patch for plain \TeX, namely |ptex.tex|, that for \LaTeXe\
810 macro (this patch and \LaTeXe\ consist \emph{p\LaTeXe}), and
811 |kinsoku.tex| which includes the default setting of \emph{kinsoku
812 shori}, the Japanese hyphenation.  We ported them to \LuaTeX-ja, except
813 the codes related to vertical typesetting, because \LuaTeX-ja doesn't
814 support vertical typesetting yet. We remark one point related to the
815 porting:
816 \begin{description}
817
818 \item[Behavior of\/ {\tt\char92fontfamily\/}]
819 The control sequence |\fontfamily| in p\LaTeXe\ changes the current alphabetic
820            font family and/or the current Japanese font family,
821            depending the argument. More concretely,
822            |\fontfamily{|$\langle\hbox{\it arg\/}\rangle$|}| changes the
823            current alphabetic font family to $\langle\hbox{\it
824            arg\/}\rangle$, if and only if one of the following
825            conditions are satisfied:
826 \begin{itemize}
827 \item An alphabetic font family named $\langle\hbox{\it arg\/}\rangle$ in
828       \emph{some} alphabetic encoding already defined in the document.
829 \item There exists an alphabetic encoding $\langle\hbox{\it
830       enc\/}\rangle$ already defined in the document such that a font
831       definition file $\langle\hbox{\it enc\/}\rangle\langle\hbox{\it
832       arg\/}\rangle$|.fd| (all lowercase) exists.
833 \end{itemize}
834 The same criterion is used for changing Japanese font family.
835
836 To work this behavior well, a list of all (alphabetic) encodings defined
837            already in the document is needed. However, since \LuaTeX-ja
838            is loaded as a package, \LuaTeX-ja cannot have this list.
839            Hence \LuaTeX-ja adopted a different approach, namely
840            |\fontfamily{|$\langle\hbox{\it arg\/}\rangle$|}| changes the
841            current alphabetic font family to $\langle\hbox{\it
842            arg\/}\rangle$, if and only if:
843 \begin{itemize}
844 \item An alphabetic font family named $\langle\hbox{\it arg\/}\rangle$
845       in the current alphabetic encoding $\langle\hbox{\it
846       enc\/}\rangle$ already defined in the document.
847 \item A  font definition file $\langle\hbox{\it enc\/}\rangle\langle\hbox{\it
848       arg\/}\rangle$|.fd| (all lowercase) exists.
849 \end{itemize}
850
851
852 \end{description}
853
854
855
856 \subsection{Classes for Japanese documents}
857 To produce `high-quality' Japanese documents, we need not only that
858 Japanese characters are correctly placed, but also class files for
859 Japanese documents. In \pTeX, there are two major families of classes:
860 \emph{jclasses} which is distributed with the official p\LaTeXe\ macros,
861 and \emph{jsclasses}.  At the present, \LuaTeX-ja
862 simply contains their counterparts: \emph{ltjclasses} and
863 \emph{ltjsclasses}. However, the policy on classess is not determined
864 now, and we hope to have another family of classes which are useful in
865 commercial printing.  In the author's opinion, \emph{ltjclasses} is
866 better to stay as an example of porting of class files for \pTeX\ to
867 \LuaTeX-ja.
868
869 \subsection{Patches for packages}
870 Apart from patches for the \LaTeXe~kernel and classes for Japanese
871 documents, we need to make patches for several packages. At the present,
872 we considered the following packages, and made patches or porting for
873 the former two packages.
874
875 \begin{description}
876 \item[The \emph{fontspec} package] The \emph{fontspec} package is built
877            on NFSS2, hence control sequences offered by the
878            \emph{fontspec} package, such as |\setmainfont|, are only
879            effective for alphabetic fonts if \LuaTeX-ja is loaded.
880            \texttt{luatexja-\penalty0fontspec.sty} (not automatically
881            loaded) offers these counterparts for Japanese fonts, with
882            additional `j' in the name of control sequences, such as
883            |\setmainjfont|. As described in
884            Subsection~\ref{ssec-unichar}, it also includes a patch for
885            control sequences producing Unicode characters.
886
887 \item[The \emph{otf} package]
888 This package is widely used in \pTeX\ for characters which is
889 not in JIS~X~0208, and for using more than one weight in \emph{mincho}
890 and \emph{gothic} font families. Therefore \LuaTeX-ja supports features
891 in the \emph{otf} package, by loading \texttt{luatexja-\penalty0otf.sty}
892            manually. Note that characters by |\UTF{xxxx}| and
893            |\CID{xxxx}| are not appended to the current list as a
894            \emph{glyph\_node}, so they are not affected by callbacks by
895            the \emph{luaotfload} package. We have another remark; |\CID|
896            does not work with TrueType fonts.
897
898 \item[The \emph{listings} package]
899 It is well-known that there is a patch |jlisting.sty| of the
900            \emph{listings} package for p\LaTeXe. Generally speaking, it
901            also can be used in \LuaTeX-ja. However, it seems to be that
902            a Japanese character after a space does not recieve any
903            process of the \emph{listings} package; this is inconvinient
904            when we use the \emph{showexpl} package.
905 \end{description}
906
907
908
909 \section{Implementation}
910 \subsection{Handling of Japanese fonts}
911 In \pTeX, there are three slots for maintaining current fonts, namely
912 |\font| for alphabetic fonts, |\jfont| for Japanese font (in horizontal
913 direction) and |\tfont| for Japanese font (in vertical direction). With
914 these slots, we can manage the current font for alphabetic characters
915 and that for Japanese characters separately in \pTeX.  However, \LuaTeX\
916 has only one slot for maintaining the current font, as \TeX82.  This
917 situation leads a problem: how can we maintain the `current Japanese
918 font'?
919
920 There are three approaches for this problem. One approach is to make a
921 mapping table from alphabetic fonts to corresponding Japanese fonts
922 (here we don't assume that NFSS2 is available).  Another approach is
923 that we always use composite fonts with alphabetic fonts and Japanese
924 fonts. The third approach is that the information of the current
925 Japanese font is stored in an attribute. We adopted the third approach,
926 since \LuaTeX-ja is much affected by \pTeX\ as we noted in
927 Subsection~\ref{ssec-pol}.
928
929 As in Figure~\ref{fig-jfdef}, \LuaTeX-ja uses |\jfont| for defining
930 Japanese font, as \pTeX.  However, because the information of the current
931 Japanese font is stored into an attribute, control sequences defined by
932 |\jfont| (e.g.,~|\foo| and |\bar| in Figure~\ref{fig-jfdef}) is
933 not representing a font by the means of \TeX82. In other words, each of
934 these control sequences is just an assignment to an attribute, therefore
935 they cannot be an argument of |\the|, |\fontname|, nor |\textfont|.
936
937
938 Callbacks by the \emph{luaotfload} package, e.g.,~replacement of glyphs
939 according to font features, are executed just after `Examination of
940 Stack Level' (see Subsection~\ref{ssec-over}). Note that calculation of
941 character classes for each Japanese character is done \emph{after} the
942 these callbacks for now. 
943
944 \subsection{Stack management}
945 \label{ssec-stack}
946
947 As we noted in Subsection~\ref{ssec-csname}, parameters that the values
948 at the end of a horizontal box or that of a paragraph are effective in
949 whole box or paragraph, such as \emph{kanjiskip}, cannot be implemented
950 by internal integers or registers of other types in \TeX. We explain it
951 in this section.
952
953 \begin{figure}
954 \begin{lstlisting}
955 void package(int c)
956 {
957     ...
958     d = box_max_depth;
959     unsave();
960     save_ptr -= 4;
961     if (cur_list.mode_field == -hmode) {
962         cur_box = filtered_hpack(cur_list.head_field,
963                                  cur_list.tail_field, saved_value(1),
964                                  saved_level(1), grp, saved_level(2));
965         subtype(cur_box) = HLIST_SUBTYPE_HBOX;
966     } else {
967 \end{lstlisting}
968 \caption{An extract of a CWEB-source \texttt{tex/packaging.w} of \LuaTeX.}
969 \label{fig-ltsrc}
970 \end{figure}
971
972 Figure~\ref{fig-ltsrc} is an extract of a CWEB-source
973 \texttt{tex/packaging.w} of \LuaTeX\ (SVN revision 4358). This function
974 is called just when an explicit |\hbox{...}| or |\vbox{...}| is ended, and
975 the function |filtered_hpack()| is where the |hpack_filter| and then the
976 actual `hpack' process are performed. Notice that the |unsave()|
977 function is called before |filtered_hpack()|. This is the problem;
978 because of |unsave()|, we can retrive only the values of registers
979 \emph{outside} the box, even in the |hpack_filter| callback.
980
981 To cope with this problem, \LuaTeX-ja has its own stack system, based on
982 Lua codes in \cite{stack-mail}. Furthermore, \emph{whatsit} nodes whose
983 \emph{user\_id} is 30112 (\emph{stack\_node}, for short) will be
984 appended to the current horizontal list each time the current stack
985 level is incremented, and their values are the values of
986 |\currentgrouplevel| at that time. In the beginning of the |hpack_filter|
987 callback, the list in question is traversed to determine whether the
988 stack level at the end of the list and that outside the box coincides.
989
990 Let $x$ be the value of |\currentgrouplevel|, and $y$ be the current
991 stack level, both inside the |hpack_filter| callback, i.e.,~outside a
992 horizontal box. Consider a list which represents the content of the box,
993 then we have:
994 \begin{itemize}
995 \item A \emph{stack\_node} whose value is $x+1$ (because all materials in
996       the box are included in a group |\hbox{...}|, the value is at
997       least $x+1$) in the list represents an assignment related to the
998       stack system in just top-level of the list, like
999 \begin{quote}
1000 \begin{verbatim}
1001 \hbox{...(assignment)...}
1002 \end{verbatim}
1003 \end{quote}
1004 In this case, the current stack level is incremented to $y+1$ after the assignment.
1005 \item A \emph{stack\_node} whose value is more than  $x+1$ in the list represents
1006 an assignment inside another group contained in the box. For example,
1007       the following input creates
1008 a \emph{stack\_node} whose value is $x+3=(x+1)+2$:
1009 \begin{quote}
1010 \begin{verbatim}
1011 \hbox{...{...{...(assignment)}...}...}
1012 \end{verbatim}
1013 \end{quote}
1014 \end{itemize}
1015 Thus, we can conclude that the stack level at the end of the list is
1016 $y+1$, if and only if there is a \emph{stack\_node} whose value is
1017 $x+1$. Otherwise, the stack level is just $y$.
1018
1019 \subsection{Adjustment of the position of Japanese characters}
1020 \label{ssec-width}
1021
1022 The size of a glyph specified in a metric and that of a real font
1023 usually differ. For example, the letter `\inhibitglue【' is half-width
1024 in |jfm-ujis.lua| or |jis.tfm|, while this letter is full-width like `【'
1025 in most TrueType fonts used in Japanese typesetting, such as
1026 IPA~Mincho. Hence the adjustment of position of such glyphs is
1027 needed. In the context of \pTeX, this process was performed using virtual fonts.
1028
1029 On the other hand, Lua\TeX-ja does the adjustment by encapsuling a glyph
1030 into a horizontal box. There are two main reasons why we adopted this
1031 method; one is that we feared Lua codes for coexisting with callbacks by
1032 |luaotfload| package would be large if we use virtual fonts, and the
1033 other is to cope with shifting of the baseline of characters at the
1034 same time. 
1035
1036 \begin{figure}
1037 \begin{center}\unitlength=9pt\small
1038 \begin{picture}(15,12)(-1,-3)
1039
1040 \color{grayx}% real glyph
1041 \put(-1,-1.5){\vrule width 6\unitlength height 7\unitlength depth 2.5\unitlength}
1042
1043 \color{black}% real glyph :step1
1044 \thicklines
1045 \put(-1,-1.5){\line(0,1){7}\line(0,-1){2.5}}
1046 \put(5,-1.5){\line(0,1){7}\line(0,-1){2.5}}
1047 \put(-1,5.5){\line(1,0){6}}
1048 \put(-1,-4){\line(1,0){6}}
1049 \put(-1,0){\makebox(0,0)[r]{\strut$R$\,}}
1050
1051 \thicklines
1052 \put(0,0){\vector(0,1){9}\line(0,-1){3}\vector(1,0){12}}
1053 \put(12,9){\makebox(0,0)[rt]{\strut$M$\,}}
1054 \put(12,0){\line(0,1){9}\vector(0,-1){3}}
1055 \put(0,9){\line(1,0){12}}
1056 \put(0,-3){\line(1,0){12}}
1057 \put(0.2,4.5){\makebox(0,0)[l]{\texttt{height}}}
1058 \put(12.2,-1.5){\makebox(0,0)[l]{\texttt{depth}}}
1059 \put(6,0.2){\makebox(0,0)[b]{\texttt{width}}}
1060
1061 \thicklines
1062 \put(3,0){\line(0,1){7}\line(0,-1){2.5}\line(1,0){6}}
1063 \put(9,0){\line(0,1){7}\line(0,-1){2.5}}
1064 \put(3,7){\line(1,0){6}}
1065 \put(3,-2.5){\line(1,0){6}}
1066 \newsavebox{\eqdist}
1067 \savebox{\eqdist}(0,0)[c]{%
1068   \thinlines
1069   \put(-0.08,0.2){\line(0,-1){0.4}}%
1070   \put(0.08,0.2){\line(0,-1){0.4}}}
1071 \put(1.5,0){\usebox{\eqdist}}
1072 \put(10.5,0){\usebox{\eqdist}}
1073
1074 \thicklines
1075 \put(3,-1.5){\vector(-1,0){4}}
1076 \put(1,-1.7){\makebox(0,0)[t]{\texttt{left}}}
1077 \put(3,0){\vector(0,-1){1.5}}
1078 \put(3.2,-0.75){\makebox(0,0)[l]{\texttt{down}}}
1079 \end{picture} 
1080 \end{center}
1081 \caption{The position of the `real' glyph.}
1082 \label{fig-pos}
1083 \end{figure}
1084
1085 Figure~\ref{fig-pos} shows the adjustment process. A large square $M$ is
1086 the imaginary body which is specified in the metric, and a vertical
1087 rectangle is the imaginary body of a real glyph. First, the real glyph
1088 is aligned with respect to the width of $M$. In the figure, the real
1089 glyph is aligned `middle'; this setting is useful for the full-width
1090 middle dot `・'. We have other settings, namely, `left' and `right'.
1091 After that, it is shifted according to the value of |left| and |down|,
1092 which are specified in the metric. The final position of the real glyph
1093 is shown by the gray rectangle~$R$. If the amount of shifting the baseline is
1094 not zero, $M$ (and hence the real glyph) is shifted by that amount.
1095
1096 We would like to remark briefly about the vertical position of a glyph.
1097 A JFM (or the metric used in \LuaTeX-ja) and the real font used for it
1098 may have different height or depth.  In that case, it may look better if
1099 the real glyph is shifted vertically to match the height-depth ratio
1100 specified in the metric. This situation is carefully studied by
1101 Otobe~\cite{min10}. Here the policy on this problem is not determined
1102 now, however we would like to offer several solutions in future development.
1103
1104 \section{Conclusion}
1105 We have discussed about our \LuaTeX-ja package, which is much affected
1106 by \pTeX. For now, it can be used for experimental use, however there
1107 are much refinements which are needed for regular use. The author hopes
1108 that this paper and this project contribute the typesetting Japanese,
1109 and possibly other Asian languages, under \LuaTeX.
1110
1111 \section*{Acknowledgements}
1112 The author would like to thank Ken Nakano and Hideaki Togashi for their
1113 development of ASCII \pTeX.  The author is very grateful to Haruhiko
1114 Okumura for his leadership in the Japanese \TeX\ community. The author
1115 is also very grateful to members of \LuaTeX-ja project team for their
1116 valuable cooperation in development.
1117
1118 %%% The style of the bibiliogrphy is `amsplain'.
1119 \providecommand{\bysame}{\leavevmode\hbox to3em{\hrulefill}\thinspace}
1120 \providecommand{\href}[2]{#2}
1121 \begin{thebibliography}{99}
1122
1123 \bibitem{aj16}
1124 Adobe Systems Incorporated, \emph{Adobe-Japan1-6 Character Collection
1125         for CID-Keyed Fonts}, Technical Note~\#5078, 2004.
1126 \url{http://partners.adobe.com/public/developer/en/font/5078.Adobe-Japan1-6.pdf}
1127
1128 \bibitem{ptex}
1129 ASCII MEDIA WORKS,アスキー日本語\TeX\ (\pTeX).\url{http://ascii.asciimw.jp/pb/ptex/}
1130
1131 \bibitem{omega}
1132 Jin-Hwan~Cho and Haruhiko Okumura, \emph{Typesetting CJK Languages with Omega},
1133 \TeX, XML, and Digital Typography, Lecture Notes in Computer Science, vol.~3130,
1134 Springer, 2004, 139--148.
1135
1136 \bibitem{joylua}
1137 Yannis Haralambous. \emph{The Joy of \LuaTeX}. \url{http://luatex.bluwiki.com/}
1138
1139 \bibitem{jisx4051}
1140 Japanese Industrial Standards Committee. \emph{JIS~X~4051: Formatting
1141         rules for Japanese documents}, 1993, 1995, 2004.
1142
1143 \bibitem{eptex}
1144 北川弘典,$\varepsilon$-\pTeX についてのwiki.
1145 \url{http://sourceforge.jp/projects/eptex/wiki/FrontPage}
1146
1147 \bibitem{luaums}
1148 北川弘典,\LuaTeX で日本語.
1149 \url{http://oku.edu.mie-u.ac.jp/tex/mod/forum/discuss.php?d=378}
1150
1151 \bibitem{luatexref}
1152 \LuaTeX\ development team, \emph{The \LuaTeX\ reference}. 
1153 \url{http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf} (snapshot of SVN trunk)
1154
1155 \bibitem{man}
1156 \LuaTeX-ja project team, \emph{The \LuaTeX-ja package}. 
1157 Not completed for now. Available at |doc/man-en.pdf| (in English) or
1158         |doc/man-ja.pdf| (in Japanese)
1159 in the Git repository.
1160
1161 \bibitem{luajp-test}
1162 香田温人,\LuaTeX と日本語.
1163 \url{http://www1.pm.tokushima-u.ac.jp/~kohda/tex/luatex-old.html}
1164
1165 \bibitem{luajalayout}
1166 前田一貴,luajalayout パッケージ---Lua\LaTeX によ
1167         る日本語組版---.
1168 \url{http://www-is.amp.i.kyoto-u.ac.jp/lab/kmaeda/lualatex/luajalayout/}
1169
1170 \bibitem{jsclasses}
1171 奥村晴彦,p\LaTeXe 新ドキュメントクラス.
1172 \url{http://oku.edu.mie-u.ac.jp/~okumura/jsclasses/}
1173
1174 \bibitem{ptexjp}
1175 Haruhiko Okumura, \emph{\pTeX\ and Japanese Typesetting},
1176         The Asian Journal of \TeX\ \textbf{2}~(2008), 43--51.
1177
1178 \bibitem{min10}
1179 乙部厳己,min10フォントについて.
1180 \url{http://argent.shinshu-u.ac.jp/~otobe/tex/files/min10.pdf}
1181
1182 \bibitem{otf}
1183 齋藤修三郎,Open Type Font用VF.
1184 \url{http://psitau.kitunebi.com/otf.html}
1185
1186 \bibitem{stack-mail}
1187 Jonathan Sauer, \emph{[Dev-luatex] tex.currentgrouplevel}. 
1188 \url{http://www.ntg.nl/pipermail/dev-luatex/2008-August/001765.html}
1189
1190 \bibitem{uptex}
1191 Takuji Tanaka, \emph{u\pTeX, up\LaTeX---unicode version of \pTeX, p\LaTeX}.
1192 \url{http://homepage3.nifty.com/ttk/comp/tex/uptex_en.html}
1193
1194 \bibitem{ptexenc}
1195 Nobuyuki Tsuchimura, \emph{Development of a Japanese \TeX\ Distribution~`ptetex3'},
1196 Computer Software\ \textbf{24} (2007), no.~4, 40--50, (in Japanese).
1197
1198 \bibitem{w3c}
1199 W3C Working Group, \emph{Requirements for Japanese Text Layout}. 
1200 \url{http://www.w3.org/TR/jlreq/}
1201 \end{thebibliography}
1202
1203 \end{document}