%
% lltjfont.sty: derived from plfonts.dtx in pLaTeX.
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lltjfont}[2014/10/29 Patch to NFSS2 for LuaTeX-ja]

\let\k@encoding\@empty
\let\ck@encoding\@empty
% LuaTeX-ja uses JY3 and JT3 encodings.
\def\cy@encoding{JY3}\def\ct@encoding{JT3}

\let\k@family\@empty
\let\k@series\@empty
\let\k@shape\@empty
\def\curr@kfontshape{\k@encoding/\k@family/\k@series/\k@shape}
\def\rel@fontshape{\f@encoding/\f@family/\f@series/\f@shape}
\ifdefined\Cht\else\newdimen\Cht\fi
\ifdefined\cht\else\newdimen\cht\fi
\ifdefined\Cdp\else\newdimen\Cdp\fi
\ifdefined\cdp\else\newdimen\cdp\fi
\ifdefined\Cwd\else\newdimen\Cwd\fi
\ifdefined\cwd\else\newdimen\cwd\fi
\ifdefined\Cvs\else\newdimen\Cvs\fi
\ifdefined\cvs\else\newdimen\cvs\fi
\ifdefined\Chs\else\newdimen\Chs\fi
\ifdefined\chs\else\newdimen\chs\fi
\ifdefined\cHT\else\newdimen\cHT\fi
%\let\afont\font

\newbox\dstrutbox
\newbox\tstrutbox
\newbox\zstrutbox
\def\strut{\relax
  \ifnum\ltjgetparameter{direction}=1 % dtou
    \ifmmode\copy\dstrutbox\else\unhcopy\dstrutbox\fi
  \else\ifnum\ltjgetparameter{direction}=4 % yoko
    \ifmmode\copy\strutbox\else\unhcopy\strutbox\fi
  \else % tate and utod
    \ifmmode\copy\tstrutbox\else\unhcopy\tstrutbox\fi
  \fi\fi
}
\def\tstrut{\relax\hbox{\tate
   \ifmmode\copy\tstrutbox\else\unhcopy\tstrutbox\fi}}
\def\zstrut{\relax\hbox{\tate
   \ifmmode\copy\zstrutbox\else\unhcopy\zstrutbox\fi}}
\def\DeclareFontEncoding{%
  \begingroup
  \nfss@catcodes
  \expandafter\endgroup
  \DeclareFontEncoding@}
\def\DeclareFontEncoding@#1#2#3{%
  \expandafter
  \ifx\csname T@#1\endcsname\relax
     \def\cdp@elt{\noexpand\cdp@elt}%
     \xdef\cdp@list{\cdp@list\cdp@elt{#1}%
                    {\default@family}{\default@series}%
                    {\default@shape}}%
     \expandafter\let\csname#1-cmd\endcsname\@changed@cmd
     \def\enc@elt{\noexpand\enc@elt}%
  \else
     \@font@info{Redeclaring font encoding #1}%
  \fi
  \global\@namedef{T@#1}{#2}%
  \global\@namedef{M@#1}{\default@M#3}%
  \xdef\LastDeclaredEncoding{#1}%
  }
\def\DeclareKanjiEncoding#1{%
  \@latex@warning{%
     The \string\DeclareKanjiEncoding\space is obsoleted command.  Please use
     \MessageBreak
     the \string\DeclareTateKanjiEncoding\space for `Tate-kumi' encoding, and
     \MessageBreak
     the \string\DeclareYokoKanjiEncoding\space for `Yoko-kumi' encoding.
     \MessageBreak
     I treat the `#1' encoding as `Yoko-kumi'.}
  \DeclareYokoKanjiEncoding{#1}%
}
\def\DeclareYokoKanjiEncoding{%
  \begingroup
  \nfss@catcodes
  \expandafter\endgroup
  \DeclareYokoKanjiEncoding@}
\def\DeclareYokoKanjiEncoding@#1#2#3{%
  \expandafter\ifx\csname T@#1\endcsname\relax
    \def\cdp@elt{\noexpand\cdp@elt}%
    \xdef\cdp@list{\cdp@list\cdp@elt{#1}%
                    {\default@k@family}{\default@k@series}%
                    {\default@k@shape}}%
    \expandafter\let\csname#1-cmd\endcsname\@changed@kcmd
     \directlua{luatexja.jfont.add_kyenc_list('\luatexluaescapestring{#1}')}%
  \else
    \@font@info{Redeclaring KANJI (yoko) font encoding #1}%
  \fi
  \global\@namedef{T@#1}{#2}%
  \global\@namedef{M@#1}{\default@KM#3}%
  }
\def\DeclareTateKanjiEncoding{%
  \begingroup
  \nfss@catcodes
  \expandafter\endgroup
  \DeclareTateKanjiEncoding@}
\def\DeclareTateKanjiEncoding@#1#2#3{%
  \expandafter\ifx\csname T@#1\endcsname\relax
    \def\cdp@elt{\noexpand\cdp@elt}%
    \xdef\cdp@list{\cdp@list\cdp@elt{#1}%
                    {\default@k@family}{\default@k@series}%
                    {\default@k@shape}}%
    \expandafter\let\csname#1-cmd\endcsname\@changed@kcmd
    \directlua{luatexja.jfont.add_ktenc_list('\luatexluaescapestring{#1}')}%
  \else
    \@font@info{Redeclaring KANJI (tate) font encoding #1}%
  \fi
  \global\@namedef{T@#1}{#2}%
  \global\@namedef{M@#1}{\default@KM#3}%
  }
\@onlypreamble\DeclareKanjiEncoding
\@onlypreamble\DeclareYokoKanjiEncoding
\@onlypreamble\DeclareYokoKanjiEncoding@
\@onlypreamble\DeclareTateKanjiEncoding
\@onlypreamble\DeclareTateKanjiEncoding@
\def\DeclareKanjiEncodingDefaults#1#2{%
  \ifx\relax#1\else
    \ifx\default@KT\@empty\else
      \@font@info{Overwriting KANJI encoding scheme text defaults}%
    \fi
    \gdef\default@KT{#1}%
  \fi
  \ifx\relax#2\else
    \ifx\default@KM\@empty\else
      \@font@info{Overwriting KANJI encoding scheme math defaults}%
    \fi
    \gdef\default@KM{#2}%
  \fi}
\let\default@KT\@empty
\let\default@KM\@empty
\@onlypreamble\DeclareKanjiEncodingDefaults
\def\DeclareFontFamily#1#2#3{%
 \@ifundefined{T@#1}%
    {\@latex@error{Encoding scheme `#1' unknown}\@eha}%
    {\def\reserved@a{#3}%
     \global
     \expandafter\let\csname #1+#2\expandafter\endcsname
            \ifx \reserved@a\@empty
              \@empty
            \else \reserved@a
            \fi
    }%
}
\def\DeclareKanjiFamily#1#2#3{%
 \@ifundefined{T@#1}%
    {\@latex@error{KANJI Encoding scheme `#1' unknown}\@eha}%
    {\def\reserved@a{#3}%
     \global
     \expandafter\let\csname #1+#2\expandafter\endcsname
            \ifx \reserved@a\@empty
              \@empty
            \else \reserved@a
            \fi
     }%
}
\def\DeclareKanjiSubstitution#1#2#3#4{%
  \expandafter\ifx\csname T@#1\endcsname\relax
    \@latex@error{KANJI Encoding scheme `#1' unknown}\@eha
  \else
    \begingroup
       \def\reserved@a{#1}%
       \toks@{}%
       \def\cdp@elt##1##2##3##4{%
         \def\reserved@b{##1}%
         \ifx\reserved@a\reserved@b
           \addto@hook\toks@{\cdp@elt{#1}{#2}{#3}{#4}}%
         \else
           \addto@hook\toks@{\cdp@elt{##1}{##2}{##3}{##4}}%
         \fi}%
       \cdp@list
       \xdef\cdp@list{\the\toks@}%
    \endgroup
    \global\@namedef{D@#1}{\def\default@family{#2}%
                           \def\default@series{#3}%
                           \def\default@shape{#4}}%
  \fi}
\def\DeclareErrorKanjiFont#1#2#3#4#5{%
   \xdef\error@kfontshape{%
      \noexpand\expandafter\noexpand\split@name\noexpand\string
      \expandafter\noexpand\csname#1/#2/#3/#4/#5\endcsname
      \noexpand\@nil}%
   \gdef\default@k@family{#2}%
   \gdef\default@k@series{#3}%
   \gdef\default@k@shape{#4}%
   \global\let\k@family\default@k@family
   \global\let\k@series\default@k@series
   \global\let\k@shape\default@k@shape
   \gdef\f@size{#5}%
   \gdef\f@baselineskip{#5pt}}
\@onlypreamble\DeclareKanjiSubstitution
\@onlypreamble\DeclareErrorKanjiFont
\def\DeclareFixedFont#1#2#3#4#5#6{%
   \begingroup
      \math@fontsfalse
      \every@math@size{}%
      \fontsize{#6}\z@
      \directlua{luatexja.jfont.is_kyenc('\luatexluaescapestring{#2}')}\ifin@%
        \usekanji{#2}{#3}{#4}{#5}%
        \global\let#1\getjfont
      \else
        \directlua{luatexja.jfont.is_ktenc('\luatexluaescapestring{#2}')}\ifin@%
          \usekanji{#2}{#3}{#4}{#5}%
          \global\let#1\gettfont
        \else
          \useroman{#2}{#3}{#4}{#5}%
          \global\expandafter\let\expandafter#1\the\font
        \fi
      \fi
   \endgroup
  }
\def\DLMfontsw@standard#1#2#3{#1{#2{#3}}}
\def\reDeclareMathAlphabet#1#2#3{%
  \ifdefined#2
    \@temptokena\expandafter{#2}%
  \else
    \@temptokena{#2}%
  \fi
  \ifdefined#3
    \toks0\expandafter{#3}%
  \else
    \toks0{#3}%
  \fi
  \long\protected\edef#1{%
    \noexpand\DLMfontsw@standard{\the\@temptokena}{\the\toks0}}%
}
\@onlypreamble\reDeclareMathAlphabet
\def\all@shape{all}%
\def\DeclareRelationFont#1#2#3#4#5#6#7#8{%
  \def\rel@shape{#4}%
  \ifx\rel@shape\@empty
     \global
     \expandafter\def\csname rel@#1/#2/#3/all\endcsname{%
       \romanencoding{#5}\romanfamily{#6}%
       \romanseries{#7}}%
  \else
     \global
     \expandafter\def\csname rel@#1/#2/#3/#4\endcsname{%
       \romanencoding{#5}\romanfamily{#6}%
       \romanseries{#7}\romanshape{#8}}%
  \fi
}
\def\SetRelationFont#1#2#3#4#5#6#7#8{%
  \def\rel@shape{#4}%
  \ifx\rel@shape\@empty
     \expandafter\def\csname rel@#1/#2/#3/all\endcsname{%
       \romanencoding{#5}\romanfamily{#6}%
       \romanseries{#7}}%
  \else
     \expandafter\def\csname rel@#1/#2/#3/#4\endcsname{%
       \romanencoding{#5}\romanfamily{#6}%
       \romanseries{#7}\romanshape{#8}}%
  \fi
}
\newif\if@knjcmd
\def\userelfont{\@knjcmdtrue}


\RequirePackage{everysel}
\let\ltj@@EverySelectfont@Init=\@EverySelectfont@Init
\expandafter\expandafter\expandafter\let
\expandafter\expandafter\csname ltj@@orig@selectfont\endcsname\csname selectfont\space\endcsname
\@EverySelectfont@Init
\def\@EverySelectfont@Init{%
  \expandafter\let\csname selectfont\space \endcsname \ltj@@orig@selectfont
  \ltj@@EverySelectfont@Init
}
\def\ltj@selectfont@patch{%
 \directlua{luatexja.base.start_time_measure('selectfont')}%
 \let\tmp@error@fontshape\error@fontshape
  \let\error@fontshape\error@kfontshape
  \directlua{luatexja.jfont.is_kyenc('\luatexluaescapestring{\k@encoding}')}\ifin@%
    \let\cy@encoding\k@encoding
    \edef\ct@encoding{\csname t@enc@\k@encoding\endcsname}%
  \else
    \directlua{luatexja.jfont.is_ktenc('\luatexluaescapestring{\k@encoding}')}\ifin@%
     \let\ct@encoding\k@encoding
     \edef\cy@encoding{\csname y@enc@\k@encoding\endcsname}%
    \else
      \@latex@error{KANJI Encoding scheme `\k@encoding' unknown}\@eha
    \fi
  \fi
%
  \let\k@encoding\ct@encoding
  \xdef\font@name{\csname\curr@kfontshape/\f@size\endcsname}%
  \pickup@tfont
  \font@name
  \ltj@@does@alt@set{\curr@kfontshape}%
    % alt fonts の定義
    \directlua{luatexja.jfont.output_alt_font_cmd('t', '\luatexluaescapestring{\curr@kfontshape}')}%
    % 定義した alt fonts のデータを \ltjdeclarealtfont に渡すのと同等の処理をする
    \directlua{luatexja.jfont.pickup_alt_font_a('\f@size')}%
  \fi
%
  \let\k@encoding\cy@encoding
  \xdef\font@name{\csname\curr@kfontshape/\f@size\endcsname}%
  \pickup@jfont
  \font@name
  \ltj@@does@alt@set{\curr@kfontshape}%
    % alt fonts の定義
    \directlua{luatexja.jfont.output_alt_font_cmd('y', '\luatexluaescapestring{\curr@kfontshape}')}%
    % 定義した alt fonts のデータを \ltjdeclarealtfont に渡すのと同等の処理をする
    \directlua{luatexja.jfont.pickup_alt_font_a('\f@size')}%
  \fi
%
  \edef\tmp@item{{\k@encoding}}\expandafter\def\expandafter\k@encoding\tmp@item
  \kenc@update
  \let\error@fontshape\tmp@error@fontshape
%
  \if@knjcmd \@knjcmdfalse
    \expandafter\ifx
    \csname rel@\k@encoding/\k@family/\k@series/\k@shape\endcsname\relax
      \expandafter\ifx
         \csname rel@\k@encoding/\k@family/\k@series/all\endcsname\relax
      \else
         \csname rel@\k@encoding/\k@family/\k@series/all\endcsname\selectfont
         % 警告: 再び \selectfont を呼んでいる．無限ループの可能性あり
      \fi
    \else
       \csname rel@\k@encoding/\k@family/\k@series/\k@shape\endcsname\selectfont
       % 警告: 再び \selectfont を呼んでいる．無限ループの可能性あり
    \fi
  \fi
  \directlua{luatexja.base.stop_time_measure('selectfont')}%
}
\EverySelectfont{\ltj@selectfont@patch}

\def\KanjiEncodingPair#1#2{\@namedef{t@enc@#1}{#2}\@namedef{y@enc@#2}{#1}}
\def\set@fontsize#1#2#3{%
    \@defaultunits\@tempdimb#2pt\relax\@nnil
    \edef\f@size{\strip@pt\@tempdimb}%
    \@defaultunits\@tempskipa#3pt\relax\@nnil
    \edef\f@baselineskip{\the\@tempskipa}%
    \edef\f@linespread{#1}%
    \let\baselinestretch\f@linespread
    \def\size@update{%
      \baselineskip\f@baselineskip\relax
      \baselineskip\f@linespread\baselineskip
      \normalbaselineskip\baselineskip
      \adjustbaseline
      \setbox\strutbox\hbox{\yoko
          \vrule\@width\z@
                \@height.7\baselineskip \@depth.3\baselineskip}%
      \setbox\dstrutbox\hbox{\dtou
          \vrule\@width\z@
                \@height.7\baselineskip \@depth.3\baselineskip}%
      \setbox\tstrutbox\hbox{\tate
          \vrule\@width\z@
                \@height.5\baselineskip \@depth.5\baselineskip}%
      \setbox\zstrutbox\hbox{\tate
          \vrule\@width\z@
                \@height.7\baselineskip \@depth.3\baselineskip}%
        \let\size@update\relax}}
\newbox\adjust@box
\newbox\adjust@box@aux
\newdimen\adjust@dimen
\def\adjustbaseline{%
    \setbox\adjust@box\hbox{%
      \ltj@ykblshift=0 \ltj@tkblshift=0漢}%
    \cht\ht\adjust@box
    \cdp\dp\adjust@box
    \cwd\wd\adjust@box
    \cvs\normalbaselineskip
    \chs\cwd
    \cHT\cht \advance\cHT\cdp
    \setbox\adjust@box@aux\hbox{\tate\ltj@tkblshift=0漢}%
    \setbox\adjust@box\hbox{\tate\ltj@tablshift=0 M}%
    \adjust@dimen\ht\adjust@box
    \advance\adjust@dimen\dp\adjust@box
    \advance\adjust@dimen-\ht\adjust@box@aux
    \divide\adjust@dimen\tw@
    \advance\adjust@dimen0.5\dp\adjust@box@aux
    \advance\adjust@dimen-\dp\adjust@box
    \ltj@tablshift=\adjust@dimen}
\DeclareRobustCommand\romanencoding[1]{%
    \expandafter\ifx\csname T@#1\endcsname\relax
      \@latex@error{Encoding scheme `#1' unknown}\@eha
    \else
      \edef\f@encoding{#1}%
      \ifx\cf@encoding\f@encoding
        \let\enc@update\relax
      \else
        \let\enc@update\@@enc@update
      \fi
    \fi
}
\DeclareRobustCommand\kanjiencoding[1]{%
    \expandafter\ifx\csname T@#1\endcsname\relax
      \@latex@error{KANJI Encoding scheme `#1' unknown}\@eha
    \else
      \edef\k@encoding{#1}%
      \ifx\ck@encoding\k@encoding
         \let\kenc@update\relax
      \else
         \let\kenc@update\@@kenc@update
      \fi
    \fi
}
\DeclareRobustCommand\fontencoding[1]{%
  \directlua{luatexja.jfont.is_kyenc('\luatexluaescapestring{#1}')}\ifin@%
  \kanjiencoding{#1}\else\romanencoding{#1}\fi}
\def\@@kenc@update{%
  \default@KT
  \csname T@\k@encoding\endcsname
  \csname D@\k@encoding\endcsname
  \let\kenc@update\relax
  \let\ck@encoding\k@encoding
  \edef\tmp@enc{'\luatexluaescapestring{\k@encoding}'}
  \directlua{luatexja.jfont.is_kyenc(\tmp@enc)}\ifin@%
    \let\cy@encoding\k@encoding
  \else
    \directlua{luatexja.jfont.is_ktenc(\tmp@enc)}\ifin@%
      \let\ct@encoding\k@encoding
    \else
      \@latex@error{KANJI Encoding scheme `\k@encoding' unknown}\@eha
    \fi
  \fi
}
\let\kenc@update\relax
\def\@changed@kcmd#1#2{%
   \ifx\protect\@typeset@protect
      \@inmathwarn#1%
      \expandafter\ifx\csname\ck@encoding\string#1\endcsname\relax
         \expandafter\ifx\csname ?\string#1\endcsname\relax
            \expandafter\def\csname ?\string#1\endcsname{%
               \TextSymbolUnavailable#1%
            }%
         \fi
         \global\expandafter\let
               \csname\cf@encoding \string#1\expandafter\endcsname
               \csname ?\string#1\endcsname
      \fi
      \csname\ck@encoding\string#1%
         \expandafter\endcsname
   \else
      \noexpand#1%
   \fi}
\newif\if@notkfam
\newif\if@notffam
\newif\if@tempswz
\newif\if@ltj@use@fd
\@ltj@use@fdtrue
\DeclareRobustCommand\romanfamily[1]{\edef\f@family{#1}}
\DeclareRobustCommand\kanjifamily[1]{\edef\k@family{#1}}
\DeclareRobustCommand\fontfamily[1]{%
  \@notkfamfalse\@notffamfalse
  \edef\tmp@item{'\luatexluaescapestring{#1}'}%
  \edef\tmp@enc{'\luatexluaescapestring{\k@encoding}'}%
  \ifcsname \k@encoding+#1\endcsname % if \k@encoding+#1 is already defined
    \edef\k@family{#1}%
  \else\if@ltj@use@fd
     % search font definition file...
     \directlua{luatexja.jfont.is_kfam(\tmp@enc, \tmp@item)}%
     \ifin@\edef\k@family{#1}\else%
       \directlua{luatexja.jfont.is_Nkfam(\tmp@enc, \tmp@item)}%
       \ifin@\@notkfamtrue\else%
         % filename of font definition file is always lowercase
         \edef\@temp{\lowercase{\noexpand\IfFileExists{\k@encoding#1.fd}}}%
         \message{(I search kanjifont definition file)}%
         \@temp{\@tempswztrue}{\@tempswzfalse}%
         \if@tempswz
           \directlua{luatexja.jfont.add_kfam_list(\tmp@enc, \tmp@item)}%
	   \edef\k@family{#1}%
         \else
           \directlua{luatexja.jfont.add_Nkfam_list(\tmp@enc, \tmp@item)}%
	   \@notkfamtrue%
         \fi
       \fi
     \fi
  \else % do not use font definition file
     \@notkfamtrue%
  \fi\fi
  \edef\tmp@enc{'\luatexluaescapestring{\f@encoding}'}%
  \ifcsname \f@encoding+#1\endcsname
    \edef\f@family{#1}%
  \else % search font definition file...
     \directlua{luatexja.jfont.is_ffam(\tmp@enc, \tmp@item)}%
     \ifin@\edef\f@family{#1}\else
       \directlua{luatexja.jfont.is_Nffam(\tmp@enc, \tmp@item)}%
       \ifin@\@notffamtrue\else
         % filename of font definition file is always lowercase
         \edef\@temp{\lowercase{\noexpand\IfFileExists{\f@encoding#1.fd}}}%
         \message{(I search font definition file)}%
         \@temp{\@tempswztrue}{\@tempswzfalse}%
         \if@tempswz
           \directlua{luatexja.jfont.add_ffam_list(\tmp@enc, \tmp@item)}%
	   \edef\f@family{#1}%
         \else
           \directlua{luatexja.jfont.add_Nffam_list(\tmp@enc, \tmp@item)}%
           \@notffamtrue%
         \fi
       \fi
     \fi
  \fi
  \if@notkfam\if@notffam
      {\ifcsname D@\k@encoding\endcsname\@nameuse{D@\k@encoding}\fi
        \xdef\@@temp{\default@family}}\edef\k@family{\@@temp}%
      {\ifcsname D@\f@encoding\endcsname\@nameuse{D@\f@encoding}\fi
        \xdef\@@temp{\default@family}}\edef\f@family{\@@temp}%
  \fi\fi
}
\DeclareRobustCommand\romanseries[1]{\edef\f@series{#1}}
\DeclareRobustCommand\kanjiseries[1]{\edef\k@series{#1}}
\DeclareRobustCommand\fontseries[1]{\kanjiseries{#1}\romanseries{#1}}
\DeclareRobustCommand\romanshape[1]{\edef\f@shape{#1}}
\DeclareRobustCommand\kanjishape[1]{\edef\k@shape{#1}}
\DeclareRobustCommand\fontshape[1]{\kanjishape{#1}\romanshape{#1}}
\def\usekanji#1#2#3#4{%
    \kanjiencoding{#1}\kanjifamily{#2}\kanjiseries{#3}\kanjishape{#4}%
    \selectfont\ignorespaces}
\def\useroman#1#2#3#4{%
    \romanencoding{#1}\romanfamily{#2}\romanseries{#3}\romanshape{#4}%
    \selectfont\ignorespaces}
\protected\def\usefont#1#2#3#4{%
  \directlua{luatexja.jfont.is_kenc('\luatexluaescapestring{#1}')}\ifin@%
  \usekanji{#1}{#2}{#3}{#4}%
  \else\useroman{#1}{#2}{#3}{#4}%
  \fi}
\DeclareRobustCommand\normalfont{%
    \kanjiencoding{\kanjiencodingdefault}%
    \kanjifamily{\kanjifamilydefault}%
    \kanjiseries{\kanjiseriesdefault}%
    \kanjishape{\kanjishapedefault}%
    \romanencoding{\encodingdefault}%
    \romanfamily{\familydefault}%
    \romanseries{\seriesdefault}%
    \romanshape{\shapedefault}%
    \selectfont\ignorespaces}
\let\reset@font\normalfont
\DeclareRobustCommand\mcfamily
        {\not@math@alphabet\mcfamily\mathmc
         \kanjifamily\mcdefault\selectfont}
\DeclareRobustCommand\gtfamily
        {\not@math@alphabet\gtfamily\mathgt
         \kanjifamily\gtdefault\selectfont}
\let\romanprocess@table\process@table
\def\kanjiprocess@table{%
  \kanjiencoding{\kanjiencodingdefault}%
  \kanjifamily{\kanjifamilydefault}%
  \kanjiseries{\kanjiseriesdefault}%
  \kanjishape{\kanjishapedefault}%
}
\def\process@table{%
  \romanprocess@table
  \kanjiprocess@table
}
\@onlypreamble\romanprocess@table
\@onlypreamble\kanjiprocess@table
\DeclareTextCommandDefault{\textunderscore}{%
  \leavevmode\kern.06em
  \vbox{\hrule\@width.3em}}

%%%%%% LuaTeX-ja specific

% #1 の展開結果から encoding 部分をとりだし，それが和文用かどうかの結果を \ifin@ に代入
\def\ltj@@IsFontJapanese#1{%
  \directlua{luatexja.jfont.is_kenc(string.match(
      '\luatexluaescapestring{#1}', '[^/]+'))}}


\let\extract@afont\extract@font
\def\pickup@jfont{%
    \expandafter \ifx \font@name \relax
       \let\extract@font\extract@jfont
       \define@newfont
       \let\extract@font\extract@afont
    \fi
    \let\getjfont\font@name}
\def\pickup@tfont{%
    \expandafter \ifx \font@name \relax
       \let\extract@font\extract@tfont
       \define@newfont
       \let\extract@font\extract@afont
    \fi
    \let\gettfont\font@name}

% 異なる和文 enc/fam/ser/shape からは異なるフォント番号が振られるように
% わざと「ゴミをつける」
\def\ltj@@patch@external@font#1 at{%
  #1\directlua{luatexja.jfont.print_aftl_address()}\space at%
}
\def\extract@jfont{%
   \get@external@font
    \ltj@@does@alt@set{\f@encoding/\f@family/\f@series/\f@shape}%
      \edef\external@font{\expandafter\ltj@@patch@external@font\external@font}%
    \fi
    \expandafter\globaljfont\font@name\external@font\relax% ここで時間がかかる
    \font@name%\global\zw=\zw\global\zh=\zh
    \csname \f@encoding+\f@family\endcsname
    \csname\curr@fontshape\endcsname}
\def\extract@tfont{%
   \get@external@font
    \ltj@@does@alt@set{\f@encoding/\f@family/\f@series/\f@shape}%
      \edef\external@font{\expandafter\ltj@@patch@external@font\external@font}%
    \fi
    \expandafter\globaltfont\font@name\external@font\relax
    \font@name%\global\zw=\zw\global\zh=\zh
    \csname \f@encoding+\f@family\endcsname
    \csname\curr@fontshape\endcsname}

\let\ltj@@al@do@subst@correction=\do@subst@correction
\def\ltj@@ja@do@subst@correction{%
    \xdef\subst@correction{%
      \font@name
      \global\expandafter\let\csname \curr@fontshape/\f@size\endcsname\font@name%
      \relax}%
    \aftergroup\subst@correction
}
\def\do@subst@correction{%
   \ltj@@IsFontJapanese{\curr@fontshape}\ifin@%
     \ltj@@ja@do@subst@correction
   \else
     \ltj@@al@do@subst@correction
   \fi
}
\let\@@italiccorr=\/

%%%%%% Japanese font for math mode

% \M@<enc> から その <enc> が和文用か調べる
{\catcode`M=12%
\gdef\ltj@@mathJapaneseFonts#1M#2#3\relax{\ltj@@IsFontJapanese{#3}}}

\let\ltj@@al@getanddefine@fonts=\getanddefine@fonts
\def\ltj@@ja@getanddefine@fonts#1#2{%
  \xdef\font@name{\csname \string#2/\tf@size\endcsname}%
  \pickup@jfont\let\textfont@name\font@name
  \xdef\font@name{\csname \string#2/\sf@size\endcsname}%
  \pickup@jfont\let\scriptfont@name\font@name
  \xdef\font@name{\csname \string#2/\ssf@size\endcsname}%
  \pickup@jfont
  \edef\math@fonts{\math@fonts\ltj@setpar@global%
    \ltj@@set@stackfont#1,\textfont@name:{MJT}%
    \ltj@@set@stackfont#1,\scriptfont@name:{MJS}%
    \ltj@@set@stackfont#1,\font@name:{MJSS}%
  }%
}

\def\getanddefine@fonts#1#2{%
  \ltj@tempcnta=#1\ltj@@IsFontJapanese{\string#2}%
  \ifin@\let\ltj@temp=\ltj@@ja@getanddefine@fonts%
  \else \let\ltj@temp=\ltj@@al@getanddefine@fonts\fi
  \ltj@temp{#1}{#2}%
}
\def\use@mathgroup#1#2{\relax\ifmmode
  \math@bgroup
    \expandafter\ifx\csname M@\f@encoding\endcsname#1\else
    #1\fi\ltj@tempcnta=#2 \expandafter\ltj@@mathJapaneseFonts\string#1\relax%
    \ifin@\jfam#2\relax\else\mathgroup#2\relax\fi
  \expandafter\math@egroup\fi}%

%%%%%% Alternate Japanese Fonts
%%%% \DeclareAlternateKanjiFont<Benc><Bfam><Bser><Bshape><Aenc><Afam><Aser><Ashape><range>
% いつも global
\def\DeclareAlternateKanjiFont#1#2#3#4#5#6#7#8#9{%
  \edef\@temp{#9}%
  \ifx\@temp\@empty\else
    {\def\ltj@temp@bfont{#1/#2/#3/#4}\def\ltj@temp@afont{#5/#6/#7/#8}%
     \expandafter\ltj@@altfont@latex#9,,}\ignorespaces
  \fi}
\def\ltj@@altfont@latex#1,{\def\ltj@temp{#1}%
  \ifx\ltj@temp\empty\let\@next=\relax\else
  \ltj@@altfont@latexA{#1}\let\@next=\ltj@@altfont@latex\fi\@next}
\def\ltj@@altfont@latexA#1{\ltj@@altfont@latexB#1--\@nil}
\def\ltj@@altfont@latexB#1-#2-#3\@nil{\def\ltj@temp{#3}%
  \ifx\ltj@temp\empty
    \ltj@tempcnta=#1\relax\ltj@tempcntb=\ltj@tempcnta
  \else
    \def\ltj@temp{#1}%
    \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
    \def\ltj@temp{#2}%
    \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
  \fi
  \directlua{luatexja.jfont.set_alt_font_latex(
    \the\ltj@tempcnta,\the\ltj@tempcntb,
    '\luatexluaescapestring{\ltj@temp@afont}',
    '\luatexluaescapestring{\ltj@temp@bfont}')}%
  }

%%%% \ClearAlternateKanjiFont<Benc><Bfam><Bser><Bshape>
% いつも global
\def\ClearAlternateKanjiFont#1#2#3#4{%
   \directlua{luatexja.jfont.clear_alt_font_latex('\luatexluaescapestring{#1/#2/#3/#4}')}}

\def\ltj@pickup@altfont@auxy#1{%
  \begingroup\edef\font@name{\csname #1/\f@size\endcsname}\pickup@jfont\endgroup%
}
\def\ltj@pickup@altfont@auxt#1{%
  \begingroup\edef\font@name{\csname #1/\f@size\endcsname}\pickup@tfont\endgroup%
}
\def\ltj@pickup@altfont@copy#1#2{%
  \ltj@@getjfontnumber#1%
  \directlua{luatexja.jfont.pickup_alt_font_b(\the\ltj@tempcntc,'\luatexluaescapestring{#2}')}%
}

\def\ltj@@does@alt@set#1{%
  \directlua{luatexja.jfont.does_alt_set'\luatexluaescapestring{#1}'}}

\endinput
