OSDN Git Service

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