OSDN Git Service

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