\message{BB}
\overfullrule=0pt
-\def\LaTeX{L\kern-.36em\setbox0=\hbox{T}\vbox to\ht0{\hbox{\sevenrm A}\vss}\kern-.15em\TeX}
+\def\LaTeX{L\kern-.36em\setbox0=\hbox{T}\vbox to\ht0{\hbox{\sx A}\vss}\kern-.15em\TeX}
\font\mff=manfnt at 10pt
\def\mf{{\mff META{\rm\-}FONT}}
\def\textfontii{\the\textfont2 }
\rm\tenipam abcほげほげ)(あいう本文本文……
\endtt
-\beginsection 実装解説
-
-\beginparagraph attributes, dimensions,$\,\ldots$
-
-以下はLua\TeX-jaパッケージ内で使用するattributeやその他の種類のレジスタである.
-上6つは内部処理用なので利用者が意識することはない.それ以外は,p\TeX に類似の名前の
-primitiveがあることから,意味は容易にわかるだろう:
-
-\item attribute |\luatexja@curjfnt|: 現在の和文フォント番号
-
-p\TeX では内部のグローバル変数で「現在の横組/縦組和文フォント」をそれぞれ保持していたが,
-当然ながら欧文用\TeX ではそのようなことはそのままではできない.
-node~$p$が保持しているattribute |\luatexja@curjfnt|の値$k$は,
-「もし$p$の中身が和文文字であれば,そのフォントは$k$番の番号のフォントである」という意味をもつ.
-
-\item attribute |\luatexja@charclass|: (和文文字の)文字クラス
-
-\item attribute |\luatexja@icflag|: この属性をもつkernはイタリック補正由来である
-
-p\TeX では,|\kern|由来のkernと,イタリック補正由来のkernを内部で区別していた.しかし,
-欧文用の\TeX ではそのような区別はなく,Lua\TeX においても区別がないようである.
-
-\item language |\luatexja@japanese|: 「日本語」に対応する|\language|番号
-
-\item attribute |\luatexja@yabaselineshift|: 欧文文字ベースラインの補正量.
-\itemitem {\bf sp$\fam\bffam {}=2^{-16}\,{\bf pt}$単位の整数値}で指定.
-正の値を指定すると,その分だけ欧文文字は下にずれる.
-\itemitem 数式中では,boxやruleもこの量だけずれる\hfil\break
-(よって,行中数式は全体が|\yabaselineshift|だけずれたように見える).
-\item attribute |\luatexja@ykbaselineshift|: 和文文字ベースラインの補正量.
-
-p\TeX では「和文が主」という考えからか,常に和文文字のベースラインが基準であり,
-欧文文字の方をずらすことになっていた.しかし,「欧文の中に和文をちょっと入れる」ような場合では,
-逆に和文文字をずらす方が理にかなっているので,和文文字のベースラインもずらせるようにした.
-
-また,これを用いることで%
-{\small 異なる文字サイズの文字を「上下中央揃え」で組む}ことも可能.
-\item skip |\kanjiskip|: 和文文字同士の間に入る空白量.
-\item skip |\xkanjiskip|: 和文文字と欧文文字の間に入る空白量.
-\item count |\jcharwidowpenalty|: {\bf 未実装}
-\item dimen |\zw|, |\zh|: 現在の和文フォントの「幅」/「高さ」(メトリックから指定)
-\item dimen |\jQ|, |\jH|${}= 0.25\,{\rm mm}$
-\enditem
-
-
\beginparagraph 和文フォントの定義
Lua\TeX-jaでは,大雑把にいうと
(|test01-noembed.pdf|を参照).
\enditem
+\beginparagraph 「和文文字の範囲」の設定
+
+\item |\defcharrange{<name>}{<char_range>}|: 文字範囲の定義.
+\itemitem <name>: 範囲を参照するためにつけるkey.
+\itemitem <range>: 文字範囲を|"100-"200, 800, 1701-|のように指定する.
+\itemT ASCII codeの範囲 (|0x00|--|0x7F|) は指定できない.
+\itemT 既に他の文字範囲に使われている領域を指定することはできない.
+また,内部処理の都合上,引数に指定している範囲達はdisjointでなければならない.
+\item 各「文字範囲」ごとに,以下の5種類の取り扱い方を設定できる.
+{\bf 下表の3, 4列目の部分についてはまだ未実装である.}
+
+\medskip
+
+\vbox{\leavevmode\hfill\leftskip=0pt\vbox{\lineskiplimit=\maxdimen\lineskip=0pt\halign{%
+\quad\hfil#\hskip.5\zw\vrule height 1.38\zh depth 0.62\zh%
+\hskip.5\zw&\hfil#\hfil\quad&\hfil#\hfil\quad&\hfil#\hfil\quad\cr
+\noalign{\hrule height 0.8pt}
+ &\bf 和文文字扱い&|\catcode|&\bf |\jcharwidowpenalty|無視\cr
+\noalign{\hrule}
+|punct| &○ &12 &○ \cr
+\noalign{\hrule}
+|letter|&○ &12 &NO \cr
+\noalign{\hrule}
+|kanji| &○ &11 &NO \cr
+\noalign{\hrule}
+|kana| &○ &11 &NO \cr
+\noalign{\hrule}
+|noncjk|&NO &|\catcode|依存&---\cr
+\noalign{\hrule height 0.8pt}
+}}\hfill\null}
+
+Lua\TeX-ja では文字範囲の設定用に5つのattributeを確保しており,
+{\bf 1つの文書中に設定できる文字範囲の数は49個までである.}
+
+\item デフォルトでは,|0x100|以降の文字コードは全部|kanji|扱いであり,さらに文字範囲として,
+\begintt
+ \defcharrange{iso88591}{"80-"FF}
+ \setjaparameter{jcharrange={iso8859-1=noncjk}}
+\endtt
+が定義されている{\small(つまり|0x80|--|0xFF|の範囲は欧文扱い)\inhibitglue}.
+
+TODO: 「{\setjaparameter{jcharrange={iso8859-1=kanji}}× (|U+00D7|)}」等,ISO 8859-1領域
+にマッピングされた文字の扱い.
+「{\setjaparameter{jcharrange={iso8859-1=kanji}}¢ (|U+00A2|)}」はHalfwidth and
+Fullwidth Formsに全角形(\char"FFE0)があるから%"
+luaotfloadの置換処理に割り込めばよいが…….
+\enditem
+
+
\beginparagraph 組版パラメタの調整
日本語組版用の各種パラメタの調整には,次の命令を用いる.
\item |asciixspmode={<chr_code>, <mode>}|★\par\noindent
同様に,p\TeX の|\xspcode|に対応した設定項目である.
\item |yabaselineshift=<dimen>|:
-欧文文字のベースライン補正量をdimensionで指定する.
+p\TeX の|\yabaselineshift|に対応したものであり,欧文文字のベースライン補正量をdimensionで指定する.
+\itemitem 正の値を指定すると,その分だけ欧文文字は下にずれることとなる.
+\itemitem 数式中では,boxやruleもこの量だけずれる\hfil\break
+(よって,行中数式は全体が|\yabaselineshift|だけずれたように見える).
\item |ykbaselineshift=<dimen>|:
和文文字のベースライン補正量をdimensionで指定する.
+p\TeX では「和文が主」という考えからか,常に和文文字のベースラインが基準であり,
+欧文文字の方をずらすことになっていた.しかし,「欧文の中に和文をちょっと入れる」ような場合では,
+逆に和文文字をずらす方が理にかなっているので,和文文字のベースラインもずらせるようにした.
+
+また,この値を適切に調整することで,%
+{\small 異なる文字サイズの文字を「上下中央揃え」で組む}ことも可能である.
+
\item |kanjiskip=<skip>|★\inhibitglue: |\kanjiskip=<skip>|と同じ意味.
\item |xkanjiskip=<skip>|★\inhibitglue: |\xkanjiskip=<skip>|と同じ意味.
\item |jcharwidowpenalty=<penalty>|★\inhibitglue: |\jcharwidowpenalty=<penalty>|と同じ意味.
\itemitem {\tt average}: 両者の相加平均.
\itemitem {\tt both}: 両者の合計値の幅をもつglue/kernを挿入する.
+\item |setjcharrange={<range_name>=<mode>}|:
+\itemitem <range_name>: |\defcharrange|で定義した「文字コードの範囲」か,|other|を指定する.
+|other|は,今まで定義した文字範囲の中に属さないような文字コード
+$c\in [\hbox{\tt 0x100},\infty)$全体の集合を表す.
+\itemitem <mode>: <range_name>で指定した範囲の文字の取扱を指定.
+
\enditem
-\beginparagraph inhibitglue
+\beginparagraph その他の命令
-|\inhibitglue|
-: 指定箇所での和文フォントメトリック由来のglue/kernの挿入を禁止する.
+\item skip |\kanjiskip|: 和文文字同士の間に入る空白量を指定.p\TeX の同名の命令と同様.
+\item skip |\xkanjiskip|: 和文文字と欧文文字の間に入る空白量.p\TeX の同名の命令と同様.
+\item count |\jcharwidowpenalty|: {\bf 未実装}
+\item dimen |\zw|, |\zh|: 現在の和文フォントの「幅」/「高さ」(メトリックから指定)
+\item dimen |\jQ|, |\jH|${}= 0.25\,{\rm mm}$.
+\item |\inhibitglue|:
+指定箇所でのJFM由来のglue/kernの挿入を禁止する.
内部的には,|user_id|が30111のwhatsit nodeを作成している{\small(メトリック由来の
glue/kern挿入処理で役目を終え,削除される)\inhibitglue}.
+\enditem
+
+\beginsection JFMについて
+
+Lua\TeX-jaで用いる和文用のメトリック情報は,次のような構文で書かれたLuaファイルである.
+見本として,|jfm-ujis.lua|を入れてある.
+
+\item |jfm.dir|: 組方向を指定する.将来的にはいずれ縦組(|'tate'|)を実装したいが,
+現時点では横組(|'yoko'|)のみの対応.
+\item |jfm.zw|, |jfm.zh|: それぞれ|\zw|, |\zh|のフォントサイズに対する割合を記述する.
+通常は両方とも1.0となるだろう.
+\item |jfm.define_char_type(<class>, <chars>)|
+
+p\TeX 用{\tt JFM}で言うところの「文字クラス」を定義する.
+\itemitem <class>は文字クラスを表す1以上$\hbox{\tt0x800}=2048$未満の整数.
+\itemitem <chars>には,<class>に属する「文字」達のUnicodeにおけるコード番号を
+リストの形|{...}|で記述する.
+
+また,このリストには,以下の「仮想的な文字」も指定可能である.
+\itemT |'lineend'|: 行末.
+\itemT |'boxbdd'|: 水平ボックスの先頭/末尾,段落の先頭/末尾.
+\itemT |'jcharbdd'|: 和文文字達の連続とそれ以外のもの(例えば欧文文字)との境界.
+\itemT |'diffmet'|: 異なるメトリックの和文文字間に入るglueの計算に使われる.
+
+\item |jfm.define_type_dim(<class>,<left>,<down>,<width>,<height>,<depth>,<italic>)|
+
+文字クラス<class>ごとに,文字の寸法のフォントサイズに対する割合を記述する.
+\itemitem <left>: 例えば開き括弧類は組版をする際には半角幅だが,TrueTypeフォント内では
+左に半角空白が付け加わって全角幅となっていることが多い.このような場合,逆に
+TrueTypeフォントを基準にすると,「左に半角幅ずらす」ことをしないといけない.
+<left>はその「左へのずらし量」を指定する.
+\itemitem <down>: 同様に,「下へのずらし量」を指定する.
+\itemitem <width>, <height>, <depth>: それぞれ幅,高さ,深さ.
+\itemitem <italic>: イタリック補正値(未実装).
+
+\item |jfm.define_glue(<bclass>, <aclass>, <width>, <stretch>, <shrink>)|
+
+文字クラス<bclass>の文字と<aclass>の文字の間に,自然長<width>,伸び<stretch>, 縮み<shrink>
+(いずれもフォントサイズ基準)のglueを挿入する.
+
+\item |jfm.define_kern(<bclass>, <aclass>, <width>)|
+
+文字クラス<bclass>の文字と<aclass>の文字の間に,幅<width>のkernを挿入.
+\enditem
-\beginparagraph 大まかな処理の流れ
+\beginsection 大まかな処理の流れ
-Lua\TeX-jaã\83\91ã\83\83ã\82±ã\83¼ã\82¸ã\81§ã\81¯ï¼\8c次ã\81®ã\82\88ã\81\86ã\81ªæµ\81ã\82\8cã\81§å\87¦ç\90\86ã\82\92è¡\8cã\81\86.
+Lua\TeX-jaã\83\91ã\83\83ã\82±ã\83¼ã\82¸ã\81§ã\81¯ï¼\8c次ã\81®ã\82\88ã\81\86ã\81ªæµ\81ã\82\8cã\81§å®\9fé\9a\9bã\81®å\87¦ç\90\86ã\82\92è¡\8cã\81£ã\81¦ã\81\84ã\82\8b.
-\item 行末空白の削除: |process_input_buffer| callback
+\item {\bf 行末空白の削除: |process_input_buffer| callback}
通常,\TeX において改行は空白とほぼ同じ意味であり,
改行した箇所には自動的に空白が入るようになっている.
この部分のコードは前田氏のjafontspecパッケージの部分から拝借したが,挿入する文字を|%|から
(通常使用されることはないと思われる)|U+FFFFF|へと変更している.
-\item 和文フォントへの置換: |hyphenate|, |hpack_filter| callbacks
+\item {\bf 和文フォントへの置換: |hyphenate|, |hpack_filter| callbacks}
この段階の前では,和文文字であっても,それを内部で表している|glyph_node|~$p$は,
「|\tenrm あ|」のように,欧文フォントが指定されている状態になっている.しかし,
\itemitem $p$の文字の文字クラスを計算し,その値をattribute |\luatexja@charclass|に格納.
これにより,|jp90|等のfeatureによりグリフが置換されても,文字クラスの値は保たれる.
-\item (luaotfloadパッケージによるグリフ置換等の処理はこの位置で行われる)
+\item {\bf (luaotfloadパッケージによるグリフ置換等の処理はこの位置で)}
-\item メトリック由来glue/kernの挿入: |pre_linebreak_filter|, |hpack_filter|
+\item {\bf JFM由来glue/kernの挿入: |pre_linebreak_filter|, |hpack_filter|}
ここで,メトリックに由来する和文文字間のglue/kernを挿入する.
基本的には連続する和文文字間に挿入するが,
両和文文字からそれぞれglue/kern |gb|, |ga|を計算し,そこから実際に入るglue/kernを
計算している(|\setjaparameter|中の|differentjfm|キーを参照).
-\item |\kanjiskip|, |\xkanjiskip|の挿入: |pre_linebreak_filter|, |hpack_filter|
+\item {\bf |\kanjiskip|, |\xkanjiskip|の挿入: |pre_linebreak_filter|, |hpack_filter|}
p\TeX の|adjust_hlist| procedureとほぼ同様の処理を用いて,
和文間glue |\kanjiskip|や和欧文間glue |\xkanjiskip|を
\itemT 「漢」と「ffi」間の空白挿入:「漢」と「f」間に入るかで判断
\itemT 「ffi」と「字」間の空白挿入:「i」と「字」間に入るかで判断
-\item ベースライン補正: |pre_linebreak_filter|, |hpack_filter|
+\item {\bf ベースライン補正: |pre_linebreak_filter|, |hpack_filter|}
この段階では,(主として)欧文文字のベースラインをずらす作業を行う.幸いにして,
Lua\TeX で文字を表す|glyph_node|には|y_offset| fieldがあるので,作業は楽である.
補正量は|\luatexja@ykblshift|の値で指定されるが,以前の「和文フォントへの置換」処理において,
|\luatexja@yablshift|へと値を移し変えているので,この段階では|\luatexja@yablshift|の値のみを気にしている.
-
さて,実際に補正されるのは次の場合である:
\itemitem 文字 (|glyph_node|)
\itemitem ボックス・rule(文中数式内部).これによって,数式全体が下がったように見えるはず.
-\item 和文文字の幅の補正: |pre_linebreak_filter|, |hpack_filter|
+\item {\bf 和文文字の幅の補正: |pre_linebreak_filter|, |hpack_filter|}
例えば,括弧類は「フォント中では全角幅だが,組版では半角幅として扱う」ことが多いが,このように
文字幅を補正する処理を最後に行う.jafontspecパッケージのように,補正対象となる|glyph_node|~$p$%
\enditem
-\beginparagraph 和文フォントメトリックについて
-
-Lua\TeX-jaで用いる和文用のメトリック情報は,次のような構文で書かれたLuaファイルである.
-見本として,|luatj-ujis.lua|を入れてある.
-
-\item |jfm.dir|: 組方向を指定する.将来的にはいずれ縦組(|'tate'|)を実装したいが,
-現時点では横組(|'yoko'|)のみの対応.
-\item |jfm.zw|, |jfm.zh|: それぞれ|\zw|, |\zh|のフォントサイズに対する割合を記述する.
-通常は両方とも1.0となるだろう.
-\item |jfm.define_char_type(<class>, <chars>)|
-
-p\TeX 用{\tt JFM}で言うところの「文字クラス」を定義する.
-\itemitem <class>は文字クラスを表す1以上$\hbox{\tt0x800}=2048$未満の整数.
-\itemitem <chars>には,<class>に属する「文字」達のUnicodeにおけるコード番号を
-リストの形|{...}|で記述する.
-
-また,このリストには,以下の「仮想的な文字」も指定可能である.
-\itemT |'boxbdd'|: 水平ボックスの先頭/末尾,段落の先頭/末尾.
-\itemT |'jcharbdd'|: 和文文字達の連続の境界.
-\itemT |'diffmet'|: 異なるメトリックの和文文字間に入るglueの計算に使われる.
-
-\item |jfm.define_type_dim(<class>,<left>,<down>,<width>,<height>,<depth>,<italic>)|
-
-文字クラス<class>ごとに,文字の寸法のフォントサイズに対する割合を記述する.
-\itemitem <left>: 例えば開き括弧類は組版をする際には半角幅だが,TrueTypeフォント内では
-左に半角空白が付け加わって全角幅となっていることが多い.このような場合,逆に
-TrueTypeフォントを基準にすると,「左に半角幅ずらす」ことをしないといけない.
-<left>はその「左へのずらし量」を指定する.
-\itemitem <down>: 同様に,「下へのずらし量」を指定する.
-\itemitem <width>, <height>, <depth>: それぞれ幅,高さ,深さ.
-\itemitem <italic>: イタリック補正値(未実装).
-
-\item |jfm.define_glue(<bclass>, <aclass>, <width>, <stretch>, <shrink>)|
-
-文字クラス<bclass>の文字と<aclass>の文字の間に,自然長<width>,伸び<stretch>, 縮み<shrink>
-(いずれもフォントサイズ基準)のglueを挿入する.
-
-\item |jfm.define_kern(<bclass>, <aclass>, <width>)|
-
-文字クラス<bclass>の文字と<aclass>の文字の間に,幅<width>のkernを挿入.
-
-\enditem
-
-
-
\vfill\eject
\leftline{{\big 組版サンプル}\hfill
\noindent 出典: 日本語Wikipediaの「\TeX」の項,2011/3/10}
\bigskip
%% sample
-\TeX(読み方については、「読み方」の小節を参照)は数学者・計算機科学者である
-ドナルド・クヌース (Donald~E. Knuth) により作られた組版処理ソフトウェアである。
+{\bf\TeX}(読み方については、「読み方」の小節を参照)は数学者・計算機科学者である
+ドナルド・クヌース (Donald~E. {\sc Knuth}) により作られた組版処理ソフトウェアである。
\beginsection 名称について
\beginparagraph 表記法
-正しくは“\TeX”と表記するが、それができない場合には
+正しくは“{\bf\TeX}”と表記するが、それができない場合には
“{\tt TeX}”と表記する(“{\tt TEX}”と表記するのは誤り)。
\beginparagraph 読み方
\TeX はマークアップ言語処理系であり、チューリング完全性を備えた関数型言語でもある。
文章そのものと、文章の構造を指定する命令とが混在して記述されたテキストファイルを読み込み、
そこに書かれた命令に従って文章を組版して、組版結果を{\tt DVI}形式のファイルに書き出す。
-{\tt DVI}形式というのは、装置に依存しない (device-independent) 中間形式である。
+{\tt DVI}形式というのは、装置に依存しない ({\bf d}e{\bf v}ice-{\bf i}ndependent) 中間形式である。
{\tt DVI}ファイルには紙面のどの位置にどの文字を配置するかといった情報が書き込まれている。
実際に紙に印刷したりディスプレイ上に表示したするためには、{\tt DVI}ファイルを解釈する
比較的よく知られている\TeX 上のマクロパッケージには、クヌース自身による plain \TeX、
一般的な文書記述に優れた\LaTeX\ ({\tt LaTeX})、数学的文書用の\AmS-\TeX などがある。
一般の使用者は、\TeX を直接使うよりも、\TeX に何らかのマクロパッケージを読み込ませたものを
-使うことの方が多い。そのため、これらのマクロパッケージのことも “\TeX” と呼ぶ場合があるが、
+使うことの方が多い。そのため、これらのマクロパッケージのことも“\TeX”と呼ぶ場合があるが、
本来は誤用である。
\TeX のマクロパッケージには、他にも次のようなものなどがある。
\item MusiX\TeX\ ({\tt MusiXTeX}) ……楽譜の記述に用いる。
\enditem
-\TeX とそれに関連するプログラム、および\TeX のマクロパッケージなどは CTAN(Comprehensive TeX Archive Network、
-包括 TeX アーカイブネットワーク)からダウンロードできる。
+\TeX とそれに関連するプログラム、および\TeX のマクロパッケージなどは CTAN({\bf C}omprehensive \TeX {\bf A}rchive {\bf N}etwork、
+包括\TeX アーカイブネットワーク)からダウンロードできる。
\beginsection 数式の表示例
\beginsection 生い立ち
-\TeX は、クヌースが自身の著書The Art of Computer Programmingを書いたときに、組版の汚さに憤慨し、
+\TeX は、クヌースが自身の著書{\it The Art of Computer Programming\/}を書いたときに、組版の汚さに憤慨し、
自分自身で心行くまで組版を制御するために作成したとされている。開発にあたって、伝統的な組版および
その関連技術に対する広範囲にわたる調査を行った。その調査結果を取り入れることで、\TeX は
商業品質の組版ができる柔軟で強力な組版システムになった。
\beginsection \TeX の日本語化
日本語組版処理のできる日本語版の\TeX および\LaTeX には、アスキー・メディアワークスによるp\TeX\
-(pTeX) およびp\LaTeX\ (pLaTeX) と、NTTの斉藤康己によるNTT J\TeX\ (NTT JTeX) およびNTT J\LaTeX\
-(NTT JLaTeX) などがある。
+({\tt pTeX}) およびp\LaTeX\ ({\tt pLaTeX}) と、NTTの斉藤康己によるNTT J\TeX\ ({\tt NTT JTeX}) およびNTT J\LaTeX\
+({\tt NTT JLaTeX}) などがある。
\TeX の日本語対応において技術的に最も大きな課題は、複数バイト文字コードへの対応である。
p\TeX(および前身の日本語\TeX)は、JIS X 0208を文字集合とした文字コード(ISO-2022-JP、EUC-JP、
-およびShift\_JIS)を直接扱う。DVIフォーマットは元々16ビット以上の文字コードを格納できる仕様が
+およびShift\_\thinspace JIS)を直接扱う。DVIフォーマットは元々16ビット以上の文字コードを格納できる仕様が
含まれていた。しかしオリジナルの英語版では使われていなかったため、既存プログラムの多くはp\TeX が
出力するDVIファイルを処理できない。またフォントに関係するファイルフォーマットが拡張されている。
これに対してNTT J\TeX は、複数の1バイト文字セットに分割することで対応している。例えば、
+local node_type = node.type
+local has_attr = node.has_attribute
+local node_insert_before = node.insert_before
+local node_insert_after = node.insert_after
+local node_hpack = node.hpack
+local round = tex.round
+local node_new = node.new
+local id_glyph = node.id('glyph')
+local id_glue_spec = node.id('glue_spec')
+local id_glue = node.id('glue')
+local id_whatsit = node.id('whatsit')
+local next_node = node.next
+local attr_jchar_class = luatexbase.attributes['luatexja@charclass']
+local attr_curjfnt = luatexbase.attributes['luatexja@curjfnt']
+local attr_yablshift = luatexbase.attributes['luatexja@yablshift']
+
-- error messages
function ltj.error(s,t)
tex.error('LuaTeX-ja error: ' .. s ,t)
end
-- return true if and only if p is a Japanese character node
-function ltj.is_japanese_glyph_node(p)
- return p and (node.type(p.id)=='glyph')
- and (p.font==node.has_attribute(p,luatexbase.attributes['luatexja@curjfnt']))
+local function is_japanese_glyph_node(p)
+ return p and (p.id==id_glyph)
+ and (p.font==has_attr(p,attr_curjfnt))
end
---------- Stack table
ltj.stack_ch_table={}; ltj.stack_ch_table[0]={}
-function ltj.new_stack_level()
+local function new_stack_level()
local i = tex.getcount('ltj@stack@pbp')
if tex.currentgrouplevel > tex.getcount('ltj@group@level@pbp') then
i = i+1 -- new stack level
return i
end
function ltj.set_ch_table(g,m,c,p)
- local i = ltj.new_stack_level()
+ local i = new_stack_level()
if not ltj.stack_ch_table[i][c] then ltj.stack_ch_table[i][c] = {} end
ltj.stack_ch_table[i][c][m] = p
if g=='global' then
end
end
-function ltj.get_penalty_table(m,c)
+local function get_penalty_table(m,c)
local i = tex.getcount('ltj@stack@pbp')
i = ltj.stack_ch_table[i][c]
if i then i=i[m] end
return i or 0
end
-function ltj.get_inhibit_xsp_table(c)
+local function get_inhibit_xsp_table(c)
local i = tex.getcount('ltj@stack@pbp')
i = ltj.stack_ch_table[i][c]
if i then i=i.xsp end
function ltj.out_ja_parameter_two(k,c)
if k == 'prebreakpenalty' then
- tex.write(ltj.get_penalty_table('pre',c))
+ tex.write(get_penalty_table('pre',c))
elseif k == 'postbreakpenalty' then
- tex.write(ltj.get_penalty_table('post',c))
+ tex.write(get_penalty_table('post',c))
elseif k == 'cjkxspmode' then
- local i = ltj.get_inhibit_xsp_table(c)
+ local i = get_inhibit_xsp_table(c)
if i==0 then tex.write('inhibit')
elseif i==1 then tex.write('postonly')
elseif i==2 then tex.write('preonly')
else tex.write('allow')
end
elseif k == 'asciixspmode' then
- local i = ltj.get_inhibit_xsp_table(c)
+ local i = get_inhibit_xsp_table(c)
if i==0 then tex.write('inhibit')
elseif i==2 then tex.write('postonly')
elseif i==1 then tex.write('preonly')
end
function ltj.create_ihb_node()
- local g=node.new(node.id('whatsit'), node.subtype('user_defined'))
+ local g=node_new(id_whatsit, node.subtype('user_defined'))
g.user_id=30111; g.type=number; g.value=1
node.write(g)
end
-function ltj.find_size_metric(px)
- if ltj.is_japanese_glyph_node(px) then
+local function find_size_metric(px)
+ if is_japanese_glyph_node(px) then
return ltj.font_metric_table[px.font].size, ltj.font_metric_table[px.font].jfm
else
return nil, nil
end
end
-function ltj.new_jfm_glue(size,mt,bc,ac)
+local function new_jfm_glue(size,mt,bc,ac)
-- mt: metric key, bc, ac: char classes
local g=nil
local h
local w=bc*0x800+ac
if ltj.metrics[mt].glue[w] then
- h=node.new(node.id('glue_spec'))
- h.width =tex.round(size*ltj.metrics[mt].glue[w].width)
- h.stretch=tex.round(size*ltj.metrics[mt].glue[w].stretch)
- h.shrink =tex.round(size*ltj.metrics[mt].glue[w].shrink)
+ h=node_new(id_glue_spec)
+ h.width =round(size*ltj.metrics[mt].glue[w].width)
+ h.stretch=round(size*ltj.metrics[mt].glue[w].stretch)
+ h.shrink =round(size*ltj.metrics[mt].glue[w].shrink)
h.stretch_order=0; h.shrink_order=0
- g=node.new(node.id('glue'))
+ g=node_new(id_glue)
g.subtype=0; g.spec=h; return g
elseif ltj.metrics[mt].kern[w] then
- g=node.new(node.id('kern'))
- g.subtype=0; g.kern=tex.round(size*ltj.metrics[mt].kern[w]); return g
+ g=node_new(node.id('kern'))
+ g.subtype=0; g.kern=round(size*ltj.metrics[mt].kern[w]); return g
else
return nil
end
end
-function ltj.calc_between_two_jchar(q,p)
+function calc_between_two_jchar(q,p)
-- q, p: node (possibly null)
local ps,pm,qs,qm,g,h
if not p then -- q is the last node
- qs, qm = ltj.find_size_metric(q)
+ qs, qm = find_size_metric(q)
if not qm then
return nil
else
- g=ltj.new_jfm_glue(qs,qm,
- node.has_attribute(q,luatexbase.attributes['luatexja@charclass']),
+ g=new_jfm_glue(qs,qm,
+ has_attr(q,attr_jchar_class),
ltj.find_char_type('boxbdd',qm))
end
elseif not q then
-- p is the first node etc.
- ps, pm = ltj.find_size_metric(p)
+ ps, pm = find_size_metric(p)
if not pm then
return nil
else
- g=ltj.new_jfm_glue(ps,pm,
+ g=new_jfm_glue(ps,pm,
ltj.find_char_type('boxbdd',pm),
- node.has_attribute(p,luatexbase.attributes['luatexja@charclass']))
+ has_attr(p,attr_jchar_class))
end
else -- p and q are not nil
- qs, qm = ltj.find_size_metric(q)
- ps, pm = ltj.find_size_metric(p)
+ qs, qm = find_size_metric(q)
+ ps, pm = find_size_metric(p)
if (not pm) and (not qm) then
-- Both p and q are NOT Japanese glyph node
return nil
elseif (qs==ps) and (qm==pm) then
-- Both p and q are Japanese glyph node, and same metric and size
- g=ltj.new_jfm_glue(ps,pm,
- node.has_attribute(q,luatexbase.attributes['luatexja@charclass']),
- node.has_attribute(p,luatexbase.attributes['luatexja@charclass']))
+ g=new_jfm_glue(ps,pm,
+ has_attr(q,attr_jchar_class),
+ has_attr(p,attr_jchar_class))
elseif not qm then
-- q is not Japanese glyph node
- g=ltj.new_jfm_glue(ps,pm,
+ g=new_jfm_glue(ps,pm,
ltj.find_char_type('jcharbdd',pm),
- node.has_attribute(p,luatexbase.attributes['luatexja@charclass']))
+ has_attr(p,attr_jchar_class))
elseif not pm then
-- p is not Japanese glyph node
- g=ltj.new_jfm_glue(qs,qm,
- node.has_attribute(q,luatexbase.attributes['luatexja@charclass']),
+ g=new_jfm_glue(qs,qm,
+ has_attr(q,attr_jchar_class),
ltj.find_char_type('jcharbdd',qm))
else
- g=ltj.new_jfm_glue(qs,qm,
- node.has_attribute(q,luatexbase.attributes['luatexja@charclass']),
+ g=new_jfm_glue(qs,qm,
+ has_attr(q,attr_jchar_class),
ltj.find_char_type('diffmet',qm))
- h=ltj.new_jfm_glue(ps,pm,
+ h=new_jfm_glue(ps,pm,
ltj.find_char_type('diffmet',pm),
- node.has_attribute(p,luatexbase.attributes['luatexja@charclass']))
+ has_attr(p,attr_jchar_class))
g=ltj.calc_between_two_jchar_aux(g,h)
end
end
-- o a whatsit node which contains local paragraph materials.
-- When we insert jfm glues, we ignore these nodes.
function ltj.is_parindent_box(p)
- if node.type(p.id)=='hlist' then
+ if node_type(p.id)=='hlist' then
return (p.subtype==3)
-- hlist (subtype=3) is a box by \parindent
- elseif node.type(p.id)=='whatsit' then
+ elseif p.id==id_whatsit then
return (p.subtype==node.subtype('local_par'))
end
end
-function ltj.add_kinsoku_penalty(head,p)
+local function add_kinsoku_penalty(head,p)
local c = p.char
- local e = ltj.get_penalty_table('pre',c)
+ local e = get_penalty_table('pre',c)
if e~=0 then
local q = node.prev(p)
- if q and node.type(q.id)=='penalty' then
+ if q and node_type(q.id)=='penalty' then
q.penalty=q.penalty+e
else
- q=node.new(node.id('penalty'))
+ q=node_new(node.id('penalty'))
q.penalty=e
- node.insert_before(head,p,q)
+ node_insert_before(head,p,q)
end
end
- e = ltj.get_penalty_table('post',c)
+ e = get_penalty_table('post',c)
if e~=0 then
- local q = node.next(p)
- if q and node.type(q.id)=='penalty' then
+ local q = next_node(p)
+ if q and node_type(q.id)=='penalty' then
q.penalty=q.penalty+e
return false
else
- q=node.new(node.id('penalty'))
+ q=node_new(node.id('penalty'))
q.penalty=e
- node.insert_after(head,p,q)
+ node_insert_after(head,p,q)
return true
end
end
-- Insert jfm glue: main routine
-function ltj.insert_jfm_glue(head)
+local function insert_jfm_glue(head)
local p = head
local q = nil -- the previous node of p
local g
local ihb_flag = false
+ local inserted_after_penalty = false
if not p then
return head
end
- while p and ltj.is_parindent_box(p) do p=node.next(p) end
+ while p and ltj.is_parindent_box(p) do p=next_node(p) end
while p do
- if node.type(p.id)=='whatsit' and p.subtype==node.subtype('user_defined')
+ if p.id==id_whatsit and p.subtype==node.subtype('user_defined')
and p.user_id==30111 then
- g=p; p=node.next(p);
+ g=p; p=next_node(p);
ihb_flag=true; head,p=node.remove(head, g)
else
- g=ltj.calc_between_two_jchar(q,p)
+ g=calc_between_two_jchar(q,p)
if g and (not ihb_flag) then
- h = node.insert_before(head,p,g)
+ h = node_insert_before(head,p,g)
if not q then head=h end
-- If p is the first node (=head), the skip is inserted
-- before head. So we must change head.
end
- q=p; ihb_flag=false
- if ltj.is_japanese_glyph_node(p)
- and ltj.add_kinsoku_penalty(head,p) then
- p=node.next(p)
+ --if is_japanese_glyph_node(q) then
+ -- node.insert(q, inserted_after_penalty)
+ --end
+ q=p; ihb_flag=false;
+ if is_japanese_glyph_node(p)
+ and add_kinsoku_penalty(head,p) then
+ p=next_node(p); inserted_after_penalty = true
+ else
+ inserted_after_penalty = false
end
- p=node.next(p)
+ p=next_node(p)
end
end
-- Insert skip after the last node
- g=ltj.calc_between_two_jchar(q,nil)
- if g then h = node.insert_after(head,q,g) end
+ g=calc_between_two_jchar(q,nil)
+ if g then h = node_insert_after(head,q,g) end
return head
end
local after_schar=1
local after_wchar=2
local insert_skip=no_skip
-function ltj.insert_kanji_skip(head)
- if ltj.auto_spacing then
- kanji_skip=tex.skip['kanjiskip']
- else
- kanji_skip=node.new(node.id('glue_spec'))
- kanji_skip.width=0; kanji_skip.stretch=0; kanji_skip.shrink=0
+
+
+-- In the next two function, cx is the Kanji code.
+local function insert_akxsp(head,q)
+ if get_inhibit_xsp_table(cx)<=1 then return end
+ local g = node_new(id_glue)
+ g.subtype=0; g.spec=node.copy(xkanji_skip)
+ node_insert_after(head,q,g)
+end
+
+local function insert_kaxsp(head,q,p)
+ local g=true
+ local c=p.char
+ while p.components and p.subtype
+ and math.floor(p.subtype/2)%2==1 do
+ p=p.components; c = p.char
end
- if ltj.auto_xspacing then
- xkanji_skip=tex.skip['xkanjiskip']
- else
- xkanji_skip=node.new(node.id('glue_spec'))
- xkanji_skip.width=0; xkanji_skip.stretch=0; xkanji_skip.shrink=0
+ if get_inhibit_xsp_table(c)%2 == 1 then
+ if get_inhibit_xsp_table(cx)%2==0 then g=false end
+ else
+ g=false
end
- local p=head -- 「現在のnode」
- local q=nil -- pの一つ前
- insert_skip=no_skip
- while p do
- if node.type(p.id)=='glyph' then
- repeat
- ltj.insks_around_char(head,q,p)
- q=p; p=node.next(p)
- until (not p) or node.type(p.id)~='glyph'
- else
- if node.type(p.id) == 'hlist' then
- ltj.insks_around_hbox(head,q,p)
- elseif node.type(p.id) == 'penalty' then
- ltj.insks_around_penalty(head,q,p)
- elseif node.type(p.id) == 'kern' then
- ltj.insks_around_kern(head,q,p)
- elseif node.type(p.id) == 'math' then
- ltj.insks_around_math(head,q,p)
- elseif node.type(p.id) == 'ins' or node.type(p.id) == 'mark'
- or node.type(p.id) == 'adjust'
- or node.type(p.id) == 'whatsit' then
- -- do nothing
- p=p
- else
- -- rule, disc, glue, margin_kern
- insert_skip=no_skip
- end
- q=p; p=node.next(p)
- end
+ if g then
+ g = node_new(id_glue)
+ g.subtype=0; g.spec=node.copy(xkanji_skip)
+ node_insert_after(head,q,g)
end
- return head
end
-function ltj.set_insert_skip_after_achar(p)
+
+local function set_insert_skip_after_achar(p)
local c=p.char
while p.components and p.subtype
and math.floor(p.subtype/2)%2==1 do
p=node.tail(p.components); c = p.char
end
- if ltj.get_inhibit_xsp_table(c)>=2 then
+ if get_inhibit_xsp_table(c)>=2 then
insert_skip=after_schar
else
insert_skip=no_skip
end
-- Insert \xkanjiskip before p, a glyph node
-function ltj.insks_around_char(head,q,p)
- local a=ltj.get_inhibit_xsp_table(p.char)
- if ltj.is_japanese_glyph_node(p) then
+local function insks_around_char(head,q,p)
+ if is_japanese_glyph_node(p) then
cx=p.char
- if ltj.is_japanese_glyph_node(q) then
- local g = node.new(node.id('glue'))
+ if is_japanese_glyph_node(q) then
+ local g = node_new(id_glue)
g.subtype=0; g.spec=node.copy(kanji_skip)
- node.insert_before(head,p,g)
+ node_insert_before(head,p,g)
elseif insert_skip==after_schar then
- ltj.insert_akxsp(head,q)
+ insert_akxsp(head,q)
end
insert_skip=after_wchar
else
if insert_skip==after_wchar then
- ltj.insert_kaxsp(head,q,p)
+ insert_kaxsp(head,q,p)
end
- ltj.set_insert_skip_after_achar(p)
- end
-end
-
--- In the next two function, cx is the Kanji code.
-function ltj.insert_akxsp(head,q)
- if ltj.get_inhibit_xsp_table(cx)<=1 then return end
- local g = node.new(node.id('glue'))
- g.subtype=0; g.spec=node.copy(xkanji_skip)
- node.insert_after(head,q,g)
-end
-
-function ltj.insert_kaxsp(head,q,p)
- local g=true
- local c=p.char
- while p.components and p.subtype
- and math.floor(p.subtype/2)%2==1 do
- p=p.components; c = p.char
- end
- if ltj.get_inhibit_xsp_table(c)%2 == 1 then
- if ltj.get_inhibit_xsp_table(cx)%2==0 then g=false end
- else
- g=false
- end
- if g then
- g = node.new(node.id('glue'))
- g.subtype=0; g.spec=node.copy(xkanji_skip)
- node.insert_after(head,q,g)
+ set_insert_skip_after_achar(p)
end
end
local first_char = nil
local last_char = nil
local find_first_char = nil
-function ltj.check_box(bp)
+local function check_box(bp)
local p = bp; local flag = false
while p do
- if node.type(p.id)=='glyph' then
+ local pt = node_type(p.id)
+ if pt=='glyph' then
repeat
if find_first_char then
first_char=p; find_first_char=false
end
- last_char=p; flag=true; p=node.next(p)
+ last_char=p; flag=true; p=next_node(p)
if not p then return flag end
- until node.type(p.id)~='glyph'
+ until p.id~=id_glyph
end
- if node.type(p.id)=='hlist' then
+ if pt=='hlist' then
flag=true
if p.shift==0 then
- if ltj.check_box(p.head) then flag=true end
+ if check_box(p.head) then flag=true end
else if find_first_char then
find_first_char=false
else
last_char=nil
end
end
- elseif node.type(p.id) == 'ins' or node.type(p.id) == 'mark'
- or node.type(p.id) == 'adjust'
- or node.type(p.id) == 'whatsit' or node.type(p.id) == 'penalty' then
+ elseif pt == 'ins' or pt == 'mark'
+ or pt == 'adjust'
+ or pt == 'whatsit' or pt == 'penalty' then
p=p
else
flag=true
last_char=nil
end
end
- p=node.next(p)
+ p=next_node(p)
end
return flag
end
-- Insert \xkanjiskip around p, an hbox
-function ltj.insks_around_hbox(head,q,p)
+local function insks_around_hbox(head,q,p)
if p.shift==0 then
find_first_char=true; first_char=nil; last_char=nil
- if ltj.check_box(p.head) then
+ if check_box(p.head) then
-- first char
- if ltj.is_japanese_glyph_node(first_char) then
+ if is_japanese_glyph_node(first_char) then
cx=first_char.char
if insert_skip==after_schar then
- ltj.insert_akxsp(head,q)
+ insert_akxsp(head,q)
elseif insert_skip==after_wchar then
- local g = node.new(node.id('glue'))
+ local g = node_new(id_glue)
g.subtype=0; g.spec=node.copy(kanji_skip)
- node.insert_before(head,p,g)
+ node_insert_before(head,p,g)
end
insert_skip=after_wchar
elseif first_char then
cx=first_char.char
if insert_skip==after_wchar then
- ltj.insert_kaxsp(head,q,first_char)
+ insert_kaxsp(head,q,first_char)
end
- ltj.set_insert_skip_after_achar(first_char)
+ set_insert_skip_after_achar(first_char)
end
-- last char
- if ltj.is_japanese_glyph_node(last_char) then
- if ltj.is_japanese_glyph_node(node.next(p)) then
- local g = node.new(node.id('glue'))
+ if is_japanese_glyph_node(last_char) then
+ if is_japanese_glyph_node(next_node(p)) then
+ local g = node_new(id_glue)
g.subtype=0; g.spec=node.copy(kanji_skip)
- node.insert_after(head,p,g)
+ node_insert_after(head,p,g)
end
insert_skip=after_wchar
elseif last_char then
- ltj.set_insert_skip_after_achar(last_char)
+ set_insert_skip_after_achar(last_char)
else insert_skip=no_skip
end
else insert_skip=no_skip
end
-- Insert \xkanjiskip around p, a penalty
-function ltj.insks_around_penalty(head,q,p)
- local r=node.next(p)
- if r and node.type(r.id)=='glyph' then
- if ltj.is_japanese_glyph_node(r) then
+local function insks_around_penalty(head,q,p)
+ local r=next_node(p)
+ if r and r.id==id_glyph then
+ if is_japanese_glyph_node(r) then
cx=r.char
- if ltj.is_japanese_glyph_node(p) then
- local g = node.new(node.id('glue'))
+ if is_japanese_glyph_node(p) then
+ local g = node_new(id_glue)
g.subtype=0; g.spec=node.copy(kanji_skip)
- node.insert_before(head,r,g)
+ node_insert_before(head,r,g)
elseif insert_skip==insert_schar then
- ltj.insert_akxsp(head,p)
+ insert_akxsp(head,p)
end
- q=p; p=node.next(p)
+ q=p; p=next_node(p)
insert_skip=after_wchar
else
if insert_skip==after_wchar then
- ltj.insert_kaxsp(head,p,r)
+ insert_kaxsp(head,p,r)
end
- ltj.set_insert_skip_after_achar(r)
+ set_insert_skip_after_achar(r)
end
end
end
-- Insert \xkanjiskip around p, a kern
-function ltj.insks_around_kern(head,q,p)
+local function insks_around_kern(head,q,p)
if p.subtype==1 then -- \kern or \/
- if node.has_attribute(p,luatexbase.attributes['luatexja@icflag']) then
- p=p -- p is a kern from \/: do nothing
- else
+ if not has_attr(p,luatexbase.attributes['luatexja@icflag']) then
insert_skip=no_skip
end
elseif p.subtype==2 then -- \accent: We ignore the accent character.
- local v = node.next(node.next(node.next(p)))
- if v and node.type(v.id)=='glyph' then
- ltj.insks_around_char(head,q,v)
+ local v = next_node(next_node(next_node(p)))
+ if v and v.id==id_glyph then
+ insks_around_char(head,q,v)
end
end
end
-- Insert \xkanjiskip around p, a math_node
-function ltj.insks_around_math(head,q,p)
+local function insks_around_math(head,q,p)
local g = { char = -1 }
if (p.subtype==0) and (insert_skip==after_wchar) then
- ltj.insert_kaxsp(head,q,g)
+ insert_kaxsp(head,q,g)
insert_skip=no_skip
else
- ltj.set_insert_skip_after_achar(g)
+ set_insert_skip_after_achar(g)
end
end
+local function insert_kanji_skip(head)
+ if ltj.auto_spacing then
+ kanji_skip=tex.skip['kanjiskip']
+ else
+ kanji_skip=node_new(id_glue_spec)
+ kanji_skip.width=0; kanji_skip.stretch=0; kanji_skip.shrink=0
+ end
+ if ltj.auto_xspacing then
+ xkanji_skip=tex.skip['xkanjiskip']
+ else
+ xkanji_skip=node_new(id_glue_spec)
+ xkanji_skip.width=0; xkanji_skip.stretch=0; xkanji_skip.shrink=0
+ end
+ local p=head -- 「現在のnode」
+ local q=nil -- pの一つ前
+ insert_skip=no_skip
+ while p do
+ local pt = node_type(p.id)
+ if pt=='glyph' then
+ repeat
+ insks_around_char(head,q,p)
+ q=p; p=next_node(p)
+ until (not p) or p.id~=id_glyph
+ else
+ if pt == 'hlist' then
+ insks_around_hbox(head,q,p)
+ elseif pt == 'penalty' then
+ insks_around_penalty(head,q,p)
+ elseif pt == 'kern' then
+ insks_around_kern(head,q,p)
+ elseif pt == 'math' then
+ insks_around_math(head,q,p)
+ elseif pt == 'ins' or pt == 'mark'
+ or pt == 'adjust'
+ or pt == 'whatsit' then
+ -- do nothing
+ p=p
+ else
+ -- rule, disc, glue, margin_kern
+ insert_skip=no_skip
+ end
+ q=p; p=next_node(p)
+ end
+ end
+ return head
+end
+
-- Shift baseline
-function ltj.baselineshift(head)
+local function baselineshift(head)
local p=head
local m=false -- is in math mode?
while p do
- local v=node.has_attribute(p,luatexbase.attributes['luatexja@yablshift'])
+ local v=has_attr(p,attr_yablshift)
if v then
- if node.type(p.id)=='glyph' then
+ local pt = node_type(p.id)
+ if pt=='glyph' then
p.yoffset=p.yoffset-v
- elseif node.type(p.id)=='math' then
+ elseif pt=='math' then
m=(p.subtype==0)
end
if m then -- boxes and rules are shifted only in math mode
- if node.type(p.id)=='hlist' or node.type(p.id)=='vlist' then
+ if pt=='hlist' or pt=='vlist' then
p.shift=p.shift+v
- elseif node.type(p.id)=='rule' then
+ elseif pt=='rule' then
p.height=p.height-v; p.depth=p.depth+v
end
end
end
- p=node.next(p)
+ p=next_node(p)
end
return head
end
+--====== Adjust the width of Japanese glyphs
+
+-- TeX's \hss
+local function get_hss()
+ local hss = node_new(id_glue)
+ local hss_spec = node_new(id_glue_spec)
+ hss_spec.width = 0
+ hss_spec.stretch = 65536
+ hss_spec.stretch_order = 2
+ hss_spec.shrink = 65536
+ hss_spec.shrink_order = 2
+ hss.spec = hss_spec
+ return hss
+end
+
+local function set_ja_width(head)
+ local p = head
+ local t,s,th, g, q,a
+ while p do
+ if is_japanese_glyph_node(p) then
+ t=ltj.metrics[ltj.font_metric_table[p.font].jfm]
+ s=t.char_type[has_attr(p,attr_jchar_class)]
+ if not(s.left==0.0 and s.down==0.0
+ and round(s.width*ltj.font_metric_table[p.font].size)==p.width) then
+ -- must be encapsuled by a \hbox
+ head, q = node.remove(head,p)
+ p.next=nil
+ p.yoffset=round(p.yoffset-ltj.font_metric_table[p.font].size*s.down)
+ p.xoffset=round(p.xoffset-ltj.font_metric_table[p.font].size*s.left)
+ node_insert_after(p,p,get_hss())
+ g=node_hpack(p, round(ltj.font_metric_table[p.font].size*s.width)
+ , 'exactly')
+ g.height=round(ltj.font_metric_table[p.font].size*s.height)
+ g.depth=round(ltj.font_metric_table[p.font].size*s.depth)
+ head,p = node_insert_before(head,q,g)
+ p=q
+ else p=next_node(p)
+ end
+ else p=next_node(p)
+ end
+ end
+ return head
+end
+
-- main process
-function ltj.main_process(head)
+local function main_process(head)
local p = head
- p = ltj.insert_jfm_glue(p)
- p = ltj.insert_kanji_skip(p)
- p = ltj.baselineshift(p)
- p = ltj.set_ja_width(p)
+ p = insert_jfm_glue(p)
+ p = insert_kanji_skip(p)
+ p = baselineshift(p)
+ p = set_ja_width(p)
return p
end
-- debug
local depth=""
function ltj.show_node_list(head)
- local p =head; local k = depth; local s
+ local p =head; local k = depth
depth=depth .. '.'
while p do
- s=node.type(p.id)
- if s == 'glyph' then
+ local pt=node_type(p.id)
+ if pt == 'glyph' then
print(depth .. ' glyph', p.subtype, utf.char(p.char), p.font)
- elseif s=='hlist' then
+ elseif pt=='hlist' then
print(depth .. ' hlist', p.subtype, '(' .. print_scaled(p.height)
.. '+' .. print_scaled(p.depth)
.. ')x' .. print_scaled(p.width) )
ltj.show_node_list(p.head)
depth=k
- elseif s=='whatsit' then
+ elseif pt == 'whatsit' then
print(depth .. ' whatsit', p.subtype)
- elseif s=='glue' then
+ elseif pt == 'glue' then
print(depth .. ' glue', p.subtype, print_spec(p.spec))
else
print(depth .. ' ' .. s, s.subtype)
end
- p=node.next(p)
+ p=next_node(p)
end
end
--- the following function is modified from jafontspec.lua (by K. Maeda).
--- Instead of "%", we use U+FFFFF for suppressing spaces.
-function ltj.process_input_buffer(buffer)
+local function process_input_buffer(buffer)
+ local c = utf.byte(buffer, utf.len(buffer))
+ local p = node.new(id_glyph)
+ p.char = c
if utf.len(buffer) > 0
- and ltj.is_ucs_in_japanese_char(utf.byte(buffer, utf.len(buffer))) then
+ and ltj.is_ucs_in_japanese_char(p) then
buffer = buffer .. string.char(0xF3,0xBF,0xBF,0xBF) -- U+FFFFF
end
return buffer
---------- Hyphenate
-function ltj.suppress_hyphenate_ja(head)
- local p=head
- while p do
- if node.type(p.id)=='glyph' and ltj.is_ucs_in_japanese_char(p.char) then
- local v = node.has_attribute(p,luatexbase.attributes['luatexja@curjfnt'])
- if v then
- p.font=v
- local l=ltj.find_char_type(p.char,ltj.font_metric_table[v].jfm)
- if not l then l=0 end
- node.set_attribute(p,luatexbase.attributes['luatexja@charclass'],l)
- end
- v=node.has_attribute(p,luatexbase.attributes['luatexja@ykblshift'])
- if v then
- node.set_attribute(p,luatexbase.attributes['luatexja@yablshift'],v)
- else
- node.unset_attribute(p,luatexbase.attributes['luatexja@yablshift'])
+local function suppress_hyphenate_ja(head)
+ local p
+ for p in node.traverse(head) do
+ if p.id == id_glyph then
+ local pc=p.char
+ if ltj.is_ucs_in_japanese_char(p) then
+ local v = has_attr(p,attr_curjfnt)
+ if v then
+ p.font=v
+ local l=ltj.find_char_type(pc,ltj.font_metric_table[v].jfm) or 0
+ node.set_attribute(p,attr_jchar_class,l)
+ end
+ v=has_attr(p,luatexbase.attributes['luatexja@ykblshift'])
+ if v then
+ node.set_attribute(p,attr_yablshift,v)
+ else
+ node.unset_attribute(p,attr_yablshift)
+ end
+ p.lang=ltj.ja_lang_number
end
- p.lang=ltj.ja_lang_number
end
- p=node.next(p)
end
lang.hyphenate(head)
return head -- 共通化のため値を返す
-- callbacks
luatexbase.add_to_callback('process_input_buffer',
function (buffer)
- return ltj.process_input_buffer(buffer)
+ return process_input_buffer(buffer)
end,'ltj.process_input_buffer')
luatexbase.add_to_callback('pre_linebreak_filter',
function (head,groupcode)
- return ltj.main_process(head)
+ return main_process(head)
end,'ltj.pre_linebreak_filter',2)
luatexbase.add_to_callback('hpack_filter',
function (head,groupcode,size,packtype)
- return ltj.main_process(head)
+ return main_process(head)
end,'ltj.hpack_filter',2)
--insert before callbacks from luaotfload
luatexbase.add_to_callback('hpack_filter',
function (head,groupcode,size,packtype)
- return ltj.suppress_hyphenate_ja(head)
+ return suppress_hyphenate_ja(head)
end,'ltj.hpack_filter_pre',0)
luatexbase.add_to_callback('hyphenate',
function (head,tail)
- return ltj.suppress_hyphenate_ja(head)
+ return suppress_hyphenate_ja(head)
end,'ltj.hyphenate')
+local has_attr = node.has_attribute
+local jfmfname
+
--====== METRIC
jfm={}; jfm.char_type={}; jfm.glue={}; jfm.kern={}
ltj.metrics={} -- this table stores all metric informations
ltj.font_metric_table={}
-function ltj.search_metric(key)
+local function search_metric(key)
for i,v in ipairs(ltj.metrics) do
if v.name==key then return i end
end
return nil
end
+-- return nil iff ltj.metrics[ind] is a bad metric
+local function consistency_check(ind)
+ local t = ltj.metrics[ind]
+ local r = ind
+ if t.dir~='yoko' then -- TODO: tate?
+ r=nil
+ elseif type(t.zw)~='number' or type(t.zh)~='number' then
+ r=nil -- .zw, .zh must be present
+ else
+ local lbt = ltj.find_char_type('lindend',ind)
+ if lbt~=0 and t.char_type[lbt].chars~={'linebdd'} then
+ r=nil -- 'linebdd' must be isolated char_type
+ end
+ end
+ if not r then ltj.metrics[ind] = nil end
+ return r
+end
+
function ltj.load_jfont_metric()
- if ltj.jfmfname=='' then
- ltj.error('no JFM specified',
- {[1]='To load and define a Japanese font, the name of JFM must be specified.',
- [2]="The JFM 'ujis' will be used for now."})
- ltj.jfmfname='ujis'
- end
- jfm.name=ltj.jfmfname .. ':' .. ltj.jfmvar;
- local i = ltj.search_metric(jfm.name)
- local t = {}
- if i then return i end
- jfm.char_type={}; jfm.glue={}; jfm.kern={}
- ltj.loadlua('jfm-' .. ltj.jfmfname .. '.lua');
- if jfm.dir~='yoko' then
- ltj.error("jfm.dir must be 'yoko'", {}); return nil
+ if jfmfname=='' then
+ ltj.error('no JFM specified',
+ {[1]='To load and define a Japanese font, the name of JFM must be specified.',
+ [2]="The JFM 'ujis' will be used for now."})
+ jfmfname='ujis'
end
+ jfm.name=jfmfname .. ':' .. ltj.jfmvar
+ local i = search_metric(jfm.name)
+ local t = {}
+ if i then return i end
+ jfm.char_type={}; jfm.glue={}; jfm.kern={}
+ ltj.loadlua('jfm-' .. jfmfname .. '.lua')
t.name=jfm.name
t.dir=jfm.dir; t.zw=jfm.zw; t.zh=jfm.zh
t.char_type=jfm.char_type
t.glue=jfm.glue; t.kern=jfm.kern
table.insert(ltj.metrics,t)
- return #ltj.metrics
+ return consistency_check(#ltj.metrics)
end
function ltj.find_char_type(c,m)
local j=ltj.load_jfont_metric()
local fn=font.id(ltj.cstemp)
local f = font.fonts[fn]
+ if not j then
+ ltj.error("bad JFM '" .. jfmfname .. "'",
+ {[1]='The JFM file you specified is not valid JFM file.',
+ [2]='Defining Japanese font is cancelled.'})
+ return
+ end
ltj.font_metric_table[fn]={}
ltj.font_metric_table[fn].jfm=j; ltj.font_metric_table[fn].size=f.size
tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
return fontdata
end
--- extract ltj.jfmfname and ltj.jfmvar
+-- extract jfmfname and ltj.jfmvar
function ltj.extract_metric(name)
local basename=name
local tmp = utf.sub(basename, 1, 5)
- ltj.jfmfname = ''
+ jfmfname = ''
ltj.jfmvar = ''
if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
basename = utf.sub(basename, 6)
while p do
local q= utf.find(basename, ";",p+1) or utf.len(basename)+1
if utf.sub(basename,p,p+3)=='jfm=' and q>p+4 then
- ltj.jfmfname = utf.sub(basename,p+4,q-1)
+ jfmfname = utf.sub(basename,p+4,q-1)
elseif utf.sub(basename,p,p+6)=='jfmvar=' and q>p+6 then
ltj.jfmvar = utf.sub(basename,p+7,q-1)
end
end
---====== Adjust the width of Japanese glyphs
+--====== Range of Japanese characters.
+local threshold = 0x100 -- must be >=0x100
+-- below threshold: kcat_table_main[chr_code] = index
+-- above threshold: kcat_table_range =
+-- { [1] = {b_1, b_2, ...},
+-- [2] = {i_1, i_2, ...} }
+-- ( Characters b_i<=chr_code <b_{i+1} have the index i_i )
+-- kcat_table_index = index1, index2, ...
+
+-- init
+local ucs_out = 0x110000
+local kcat_table_main = {}
+kcat_table_range = { [1] = {threshold,ucs_out}, [2] = {0,-1} }
+kcat_table_index = { [0] = 'other' ,
+ [1] = 'iso8859-1'}
--- TeX's \hss
-function ltj.get_hss()
- local hss = node.new(node.id("glue"))
- local hss_spec = node.new(node.id("glue_spec"))
- hss_spec.width = 0
- hss_spec.stretch = 65536
- hss_spec.stretch_order = 2
- hss_spec.shrink = 65536
- hss_spec.shrink_order = 2
- hss.spec = hss_spec
- return hss
+local kc_kanji = 0
+local kc_kana = 1
+local kc_letter = 2
+local kc_punct = 3
+local kc_noncjk = 4
+
+for i=0x80,0xFF do
+ kcat_table_main[i]=1
+end
+for i=0x100,threshold-1 do
+ kcat_table_main[i]=0
end
-function ltj.set_ja_width(head)
- local p = head
- local t,s,th, g, q,a
- while p do
- if ltj.is_japanese_glyph_node(p) then
- t=ltj.metrics[ltj.font_metric_table[p.font].jfm]
- s=t.char_type[node.has_attribute(p,luatexbase.attributes['luatexja@charclass'])]
- if not(s.left==0.0 and s.down==0.0
- and tex.round(s.width*ltj.font_metric_table[p.font].size)==p.width) then
- -- must be encapsuled by a \hbox
- head, q = node.remove(head,p)
- p.next=nil
- p.yoffset=tex.round(p.yoffset-ltj.font_metric_table[p.font].size*s.down)
- p.xoffset=tex.round(p.xoffset-ltj.font_metric_table[p.font].size*s.left)
- node.insert_after(p,p,ltj.get_hss())
- g=node.hpack(p, tex.round(ltj.font_metric_table[p.font].size*s.width)
- , 'exactly')
- g.height=tex.round(ltj.font_metric_table[p.font].size*s.height)
- g.depth=tex.round(ltj.font_metric_table[p.font].size*s.depth)
- head,p = node.insert_before(head,q,g)
- p=q
- else p=node.next(p)
+local function add_jchar_range(b,e,ind)
+ -- We assume that e>=b
+ if b<threshold then
+ for i=math.max(0x80,b),math.min(threshold-1,e) do
+ kcat_table_main[i]=ind
+ end
+ if e<threshold then return true else b=threshold end
+ end
+ local insp
+ for i,v in ipairs(kcat_table_range[1]) do
+ if v>e then
+ insp = i-1; break
+ end
+ end
+ if kcat_table_range[1][insp]>b or kcat_table_range[2][insp]>1 then
+ ltj.error("Bad character range",{}); return nil -- error
+ end
+ if kcat_table_range[1][insp]<b then
+ -- now [insp]¢« <b .. b .. [insp+1]¢« >e
+ table.insert(kcat_table_range[1],insp+1,b)
+ table.insert(kcat_table_range[2], insp+1, kcat_table_range[2][insp])
+ insp=insp+1
+ end
+ -- [insp]¢« =b .. e .. [insp+1]¢« >e
+ table.insert(kcat_table_range[1], insp+1,e+1)
+ table.insert(kcat_table_range[2], insp+1, kcat_table_range[2][insp])
+ kcat_table_range[2][insp]=ind
+end
+
+function ltj.def_jchar_range(b,e,name)
+ local ind = #kcat_table_index+1
+ for i,v in pairs(kcat_table_index) do
+ if v==name then ind=i; break end
+ end
+ if ind>=50 then
+ ltj.error("No room for new character range",{}); return -- error
+ end
+ if ind == #kcat_table_index+1 then
+ table.insert(kcat_table_index, name)
+ print('New char range: ' .. name, ind)
+ end
+ add_jchar_range(b,e,ind)
+end
+
+local function get_char_kcatcode(p)
+ local i
+ local c = p.char
+ if c<0x80 then return kc_noncjk
+ elseif c<threshold then i=kcat_table_main[c]
+ else
+ for j,v in ipairs(kcat_table_range[1]) do
+ if v>c then
+ i = kcat_table_range[2][j-1]; break
end
- else p=node.next(p)
end
end
- return head
+ return math.floor(has_attr(p,
+ luatexbase.attributes['luatexja@kcat'..math.floor(i/10)])
+ /math.pow(8, i%10))%8
+end
+
+-- ÏÂʸʸ»ú¤Èǧ¼±¤¹¤ë unicode ¤ÎÈÏ°Ï
+function ltj.is_ucs_in_japanese_char(p)
+ return (get_char_kcatcode(p)~=kc_noncjk)
end
+
+function ltj.set_jchar_range(g, name,kc)
+ local ind = 0
+ for i,v in pairs(kcat_table_index) do
+ if v==name then ind=i; break end
+ end
+ local attr = luatexbase.attributes['luatexja@kcat'..math.floor(ind/10)]
+ local a = tex.getattribute(attr)
+ local k = math.pow(8, ind%10)
+ tex.setattribute(g,attr,(math.floor(a/k/8)*8+kc)*k+a%k)
+end
\ No newline at end of file