OSDN Git Service

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