OSDN Git Service

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