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

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lltjfont}[2023-05-16 Patch to NFSS2 for LuaTeX-ja]

%%%%%%%% LaTeX2e version detection
\newcount\ltj@@latex@plv \ltj@@latex@plv=1500

%% patching \DeclareTextComposite
\def\ltj@chardef@text@cmd#1{%
  \let\@ifdefinable\@@ifdefinable%
  \edef\ltj@chardef@text@cmd@name{\expandafter\@gobble\string#1}%
  \afterassignment\ltj@chardef@text@cmd@\ltj@tempcnta
}
\def\ltj@chardef@text@cmd@{%
  \ifnum\ltj@tempcnta>"7F
    \expandafter\edef\csname \ltj@chardef@text@cmd@name\endcsname{%
      \noexpand\ltjalchar\the\ltj@tempcnta\space
    }%
  \else
    \expandafter\chardef\csname \ltj@chardef@text@cmd@name\endcsname\ltj@tempcnta
  \fi
}
\def\DeclareTextSymbol#1#2#3{%
  \@dec@text@cmd\ltj@chardef@text@cmd#1{#2}#3\relax
}

\let\ltj@temp@DeclareTextComposite=\DeclareTextComposite
\def\DeclareTextComposite#1#2#3#4{}%
\input{tuenc.def}
\let\DeclareTextComposite=\ltj@temp@DeclareTextComposite
\expandafter\patchcmd\csname TU\string\textasteriskcentered\endcsname{\char}{\ltjalchar}{}{}


\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

\let\ystrutbox\strutbox
\newbox\dstrutbox
\newbox\tstrutbox
\newbox\zstrutbox
\def\strutbox{%
  \ifnum\ltjgetparameter{direction}=1 \dstrutbox       % dtou
  \else\ifnum\ltjgetparameter{direction}=4 \ystrutbox  % yoko
  \else\tstrutbox  % tate and utod
  \fi\fi
}
\DeclareRobustCommand\strut{\relax
  \ifnum\ltjgetparameter{direction}=1 % dtou
    \ifmmode\copy\dstrutbox\else\unhcopy\dstrutbox\fi
  \else\ifnum\ltjgetparameter{direction}=4 % yoko
    \ifmmode\copy\ystrutbox\else\unhcopy\ystrutbox\fi
  \else % tate and utod
    \ifmmode\copy\tstrutbox\else\unhcopy\tstrutbox\fi
  \fi\fi
}

\DeclareRobustCommand\ystrut{\relax\hbox{\ltj@@orig@yoko
   \ifmmode\copy\ystrutbox\else\unhcopy\ystrutbox\fi}}
\DeclareRobustCommand\tstrut{\relax\hbox{\ltj@@orig@tate
   \ifmmode\copy\tstrutbox\else\unhcopy\tstrutbox\fi}}
\DeclareRobustCommand\dstrut{\relax\hbox{\ltj@@orig@dtou
   \ifmmode\copy\dstrutbox\else\unhcopy\dstrutbox\fi}}
\DeclareRobustCommand\zstrut{\relax\hbox{\ltj@@orig@utod
   \ifmmode\copy\zstrutbox\else\unhcopy\zstrutbox\fi}}



\newluafunction\ltj@@add@fenc@@inner
\newluafunction\ltj@@add@kyenc@@inner
\newluafunction\ltj@@add@ktenc@@inner
\newluafunction\ltj@@is@kyenc@@inner
\newluafunction\ltj@@is@ktenc@@inner
\newluafunction\ltj@@is@kenc@@inner
\newluafunction\ltj@@is@ffam@@inner
\newluafunction\ltj@@add@ffam@@inner
\newluafunction\ltj@@add@kfam@@inner
\directlua{%
  local lf = lua.get_functions_table()
  lf[\the\ltj@@add@fenc@@inner]  = luatexja.jfont.add_fenc_list
  lf[\the\ltj@@add@kyenc@@inner] = luatexja.jfont.add_kyenc_list
  lf[\the\ltj@@add@ktenc@@inner] = luatexja.jfont.add_ktenc_list
  lf[\the\ltj@@is@kyenc@@inner]  = luatexja.jfont.is_kyenc
  lf[\the\ltj@@is@ktenc@@inner]  = luatexja.jfont.is_ktenc
  lf[\the\ltj@@is@kenc@@inner]   = luatexja.jfont.is_kenc
  lf[\the\ltj@@is@ffam@@inner]   = luatexja.jfont.is_ffam
  lf[\the\ltj@@add@ffam@@inner]  = luatexja.jfont.add_ffam
  lf[\the\ltj@@add@kfam@@inner]  = luatexja.jfont.add_kfam
}
\luadef\ltj@@add@fenc \ltj@@add@fenc@@inner
\luadef\ltj@@add@kyenc\ltj@@add@kyenc@@inner
\luadef\ltj@@add@ktenc\ltj@@add@ktenc@@inner
\luadef\ltj@@is@kyenc \ltj@@is@kyenc@@inner
\luadef\ltj@@is@ktenc \ltj@@is@ktenc@@inner
\luadef\ltj@@is@kenc  \ltj@@is@kenc@@inner
\luadef\ltj@@is@ffam  \ltj@@is@ffam@@inner
\luadef\ltj@@add@ffam \ltj@@add@ffam@@inner
\luadef\ltj@@add@kfam \ltj@@add@kfam@@inner

{\def\cdp@elt#1#2#3#4{\ltj@@add@fenc{#1}}\cdp@list}

\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
     \ltj@@add@fenc{#1}%
  \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
    \ltj@@add@kyenc{#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
    \ltj@@add@ktenc{#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
     \ltj@@add@ffam{#2}%
    }%
}
\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
     \ltj@@add@kfam{#2}%
     }%
}
\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@k@family{#2}%
                           \def\default@k@series{#3}%
                           \def\default@k@shape{#4}}%
  \fi}
\@onlypreamble\DeclareKanjiSubstitution

\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}%
   }
\@onlypreamble\DeclareErrorKanjiFont

\let\wrong@ja@fontshape\wrong@fontshape
\let\wrong@al@fontshape\wrong@fontshape
\patchcmd\wrong@ja@fontshape{\default@shape}{\default@k@shape}{}{}
\patchcmd\wrong@ja@fontshape{\default@series}{\default@k@series}{}{}
\patchcmd\wrong@ja@fontshape{\default@family}{\default@k@family}{}{}
\def\wrong@fontshape{%
  \ltj@@is@kenc{\f@encoding}\ifin@%
    \wrong@ja@fontshape
  \else
    \wrong@al@fontshape
  \fi
}

\newif\ifltjselectfont@force@define
\def\DeclareFixedFont#1#2#3#4#5#6{%
   \begingroup
      \math@fontsfalse
      \every@math@size{}%
      \fontsize{#6}\z@
      \ltj@@is@kyenc{#2}\ifin@%
        \ltjselectfont@force@definetrue
        \usekanji{#2}{#3}{#4}{#5}%
        \global\let#1\getjfont
        \ltjselectfont@force@definefalse
      \else
        \ltj@@is@ktenc{#2}\ifin@%
          \ltjselectfont@force@definetrue
          \usekanji{#2}{#3}{#4}{#5}%
          \global\let#1\gettfont
          \ltjselectfont@force@definefalse
        \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
\DeclareRobustCommand\userelfont{\@knjcmdtrue}

\def\ltj@selectfont@tate{%
  \let\k@encoding\ct@encoding
  \xdef\font@name{\csname\curr@kfontshape/\f@size\endcsname}%
  \pickup@tfont
  \ifnum\ltjgetparameter{direction}=3 \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}
\def\ltj@selectfont@yoko{%
  \let\k@encoding\cy@encoding
  \xdef\font@name{\csname\curr@kfontshape/\f@size\endcsname}%
  \pickup@jfont
  \unless\ifnum\ltjgetparameter{direction}=3 \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}

\AddToHook{selectfont}[luatexja]{%
  \ltj@@start@time@measure{selectfont}%
  \ifx\delayed@k@adjustment\@empty
  \else
    \let\k@shape@saved\k@shape
    \let\k@series@saved\k@series
    \delayed@k@adjustment
    \begingroup\let\f@encoding\k@encoding\let\f@family\k@family
      \maybe@load@fontshape\endgroup
    \ifcsname \k@encoding/\k@family/\k@series/\k@shape \endcsname
    \else
      \let\k@shape\k@shape@saved
      \let\k@series\k@series@saved
      \let\delayed@merge@kanji@shape\merge@kanji@shape
      \let\delayed@merge@kanji@series\merge@kanji@series
      \delayed@k@adjustment
      \let\delayed@merge@kanji@shape\merge@kanji@shape@without@substitution
      \let\delayed@merge@kanji@series\merge@kanji@series@without@substitution
    \fi
    \let\delayed@k@adjustment\@empty
  \fi
  \@forced@series@kanjifalse
  \let\tmp@error@fontshape\error@fontshape
  \let\error@fontshape\error@kfontshape
  \ltj@@is@kyenc{\k@encoding}\ifin@%
    \let\cy@encoding\k@encoding
    \ensure@KanjiEncodingPair{t}%
    \edef\ct@encoding{\csname t@enc@\k@encoding\endcsname}%
  \else
    \ltj@@is@ktenc{\k@encoding}\ifin@%
      \let\ct@encoding\k@encoding
      \ensure@KanjiEncodingPair{y}%
      \edef\cy@encoding{\csname y@enc@\k@encoding\endcsname}%
    \else
      \@latex@error{KANJI Encoding scheme `\k@encoding' unknown}\@eha
    \fi
  \fi
  \global\let\ltj@afont@name=\font@name
  \ltj@selectfont@tate\ltj@selectfont@yoko
%
  \edef\tmp@item{{\k@encoding}}\expandafter\def\expandafter\k@encoding\tmp@item
  \kenc@update
  \global\let\font@name=\ltj@afont@name
  \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
  \global\let\font@name=\ltj@afont@name
  \ltj@@stop@time@measure{selectfont}%
}

\bgroup
  \def\ltj@@dir@patch#1#2#3{%
    \global\csletcs{ltj@@orig@#1}{#1}%
      \protected\expandafter\xdef\csname #1\endcsname{%
        \csname ltj@@orig@#1\endcsname
        \unexpanded{\ifnum#3<\z@\let\ltj@@protect\protect\set@typeset@protect}%
        \unexpanded{\global\let\ltj@afont@name=\font@name}%
        \expandonce{\csname ltj@selectfont@#2\endcsname}%
        \unexpanded{\global\let\font@name=\ltj@afont@name}%
        \unexpanded{\let\protect\ltj@@protect\fi}%
      }%
    }
  \ltj@@dir@patch{yoko}{yoko}{\ltj@curjfnt}
  \ltj@@dir@patch{tate}{tate}{\ltj@curtfnt}
  \ltj@@dir@patch{utod}{yoko}{\ltj@curjfnt}
  \ltj@@dir@patch{dtou}{yoko}{\ltj@curjfnt}
  \global\let\ltj@@box@dirhook@orig=\ltj@@box@dirhook
  \gdef\ltj@@box@dirhook{%
     \ltj@@box@dirhook@orig
     \ifnum\ltjgetparameter{direction}=11
       \ifnum\ltj@curjfnt<\z@
         \let\ltj@@protect\protect\set@typeset@protect
         \global\let\ltj@afont@name=\font@name
         \ltj@selectfont@yoko
         \global\let\font@name=\ltj@afont@name
         \let\protect\ltj@@protect
       \fi
     \fi
  }
\egroup

\def\KanjiEncodingPair#1#2{\@namedef{t@enc@#1}{#2}\@namedef{y@enc@#2}{#1}}
\def\ensure@KanjiEncodingPair#1{%
  \edef\reserved@a{\csname #1@enc@\k@encoding\endcsname}%
  \edef\reserved@b{\csname c#1@encoding\endcsname}%
  \ifcat\relax\reserved@a
    \@latex@error
      {KANJI Encoding pair for `\k@encoding' undefined}%
      {Use \string\KanjiEncodingPair, falling back to `\reserved@b'...}%
    \expandafter\edef\reserved@a{\reserved@b}%
  \fi}

\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\ystrutbox\hbox{\ltj@@orig@yoko
          \vrule\@width\z@
                \@height.7\baselineskip \@depth.3\baselineskip}%
      \setbox\dstrutbox\hbox{\ltj@@orig@dtou
          \vrule\@width\z@
                \@height.7\baselineskip \@depth.3\baselineskip}%
      \setbox\tstrutbox\hbox{\ltj@@orig@tate
          \vrule\@width\z@
                \@height.5\baselineskip \@depth.5\baselineskip}%
      \setbox\zstrutbox\hbox{\ltj@@orig@utod
          \vrule\@width\z@
                \@height.7\baselineskip \@depth.3\baselineskip}%
     \let\size@update\relax}}

\newluafunction\ltj@@adjust@baseline@inner
\begingroup\catcode`\~=12
\directlua{%
  local nulltable = {}
  local t = lua.get_functions_table()
  local getfont, dir_tate = font.getfont, luatexja.dir_table.dir_tate
  local fmt, get_attr = luatexja.jfont.font_metric_table, tex.getattribute
  local getcount=tex.getcount
  local setdimen, set_attr = tex.setdimen, tex.setattribute
  local dir = luatexja.unary_pars.direction
  t[\the\ltj@@adjust@baseline@inner] = function()
    local ft = fmt[get_attr('ltj@curtfnt')] or nulltable
    ft = ft.char_type or nulltable
    local fk = ft[0] or nulltable
    local ht, dp, wd = fk.height or 0, fk.depth or 0,
                       fk.width or ft.zw or 0
    local fm 
      = ((getfont(font.current()) or nulltable)
        .characters or nulltable)[77] or nulltable %% 'M'
    set_attr('ltj@tablshift',
      tex.round(0.5*((fm.height or 0)- (fm.depth or 0) - ht+ dp)))
    if dir() ~= dir_tate then
      ft = fmt[get_attr('ltj@curjfnt')] or nulltable
      ft = ft.char_type or nulltable
      fk = ft[0] or nulltable
      ht, dp, wd = fk.height or 0, fk.depth or 0,
                   fk.width or ft.zw or 0
    end
    setdimen('cht', ht); setdimen('cdp', dp)
    setdimen('cwd', wd); setdimen('chs', wd)
    setdimen('cHT', ht+dp)
  end
}\endgroup
\DeclareRobustCommand\adjustbaseline{\luafunction\ltj@@adjust@baseline@inner
   \cvs\normalbaselineskip}
\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]{%
  \ltj@@is@kenc{#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
  \ltj@@is@kyenc{\k@encoding}\ifin@
    \let\cy@encoding\k@encoding
  \else
    \ltj@@is@ktenc{\k@encoding}\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
\def\@ltj@use@fd{true}
\DeclareRobustCommand\romanfamily[1]{\edef\f@family{#1}}
\DeclareRobustCommand\kanjifamily[1]{\edef\k@family{#1}}
\DeclareRobustCommand\fontfamily[1]{%
  \@notkfamfalse\@notffamfalse
  %
  \directlua{luatexja.jfont.search_kfam()}{#1}{\@ltj@use@fd}%
  \ifin@\edef\k@family{#1}\else\@notkfamtrue\fi
  %
  % alphabetic fonts part
  \ltj@@is@ffam{#1}%
  \ifin@\edef\f@family{#1}\else
    % checking is_stored_ffam is not sufficient,
    % since LuaTeX-ja doesn't know whats fonts are defined before LuaTeX-ja is loaded.
    \ifcsname \f@encoding+#1\endcsname % easy case
      \ltj@@add@ffam{#1}\edef\f@family{#1}%
    \else
      \@tempswafalse
      \def\cdp@elt##1{\ifcsname ##1+#1\endcsname%
        \@tempswatrue\def\cdp@elt####1\relax{}%
      \fi}%
      \directlua{luatexja.jfont.search_ffam_declared()}\relax%
      \if@tempswz
        \ltj@@add@ffam{#1}\edef\f@family{#1}%
      \else
        % now we search Nffam_list and font definition files.
        \directlua{luatexja.jfont.search_ffam_fd()}{#1}%
        \ifin@\edef\f@family{#1}\else\@notffamtrue\fi
      \fi
    \fi
  \fi
  %
  \if@notkfam\if@notffam
    \edef\k@family{#1}\edef\f@family{#1}%
  \fi\fi
}

%%%%%%
\newif\if@shape@roman@kanji

\def\merge@kanji@series#1{%
  \expandafter\expandafter\expandafter
  \merge@kanji@series@
    \csname series@\k@series @#1\endcsname
    {#1}%
    \@nil
}
\def\merge@kanji@shape#1{%
  \expandafter\expandafter\expandafter
  \merge@kanji@shape@
    \csname shape@\k@shape @#1\endcsname
    {#1}%
    \@nil
}

  \def\set@target@series@kanji#1{%
      \edef\k@series{#1}%
      \series@maybe@drop@one@m\k@series\k@series
  }

\def\merge@kanji@series@#1#2#3\@nil{%
  \def\reserved@a{#3}%
  \ifx\reserved@a\@empty
    \set@target@series@kanji{#2}%
  \else
    \begingroup\let\f@encoding\k@encoding\let\f@family\k@family
    \maybe@load@fontshape\endgroup
    \edef\reserved@a{\k@encoding /\k@family /#1/\k@shape}%
     \ifcsname \reserved@a \endcsname
       \set@target@series@kanji{#1}%
    \else
       \ifcsname \k@encoding /\k@family /#2/\k@shape \endcsname
         \set@target@series@kanji{#2}%
         {\let\curr@fontshape\curr@kfontshape\@font@shape@subst@warning}%
       \else
         \set@target@series@kanji{#3}%
         {\let\curr@fontshape\curr@kfontshape\@font@shape@subst@warning}%
       \fi
    \fi
  \fi
}
\def\merge@kanji@series@without@substitution#1{%
  \expandafter\expandafter\expandafter
  \merge@kanji@series@without@substitution@
    \csname series@\k@series @#1\endcsname
    {#1}%
    \@nil
}
\def\merge@kanji@series@without@substitution@#1#2#3\@nil{%
  \def\reserved@a{#3}%
  \ifx\reserved@a\@empty
    \set@target@series@kanji{#2}%
  \else
    \set@target@series@kanji{#1}%
  \fi
}
\let\delayed@merge@kanji@series\merge@kanji@series@without@substitution

\def\merge@kanji@shape@#1#2#3\@nil{%
  \def\reserved@a{#3}%
  \ifx\reserved@a\@empty
    \if@shape@roman@kanji
      \set@safe@kanji@shape{#2}{}%
    \else
      \edef\k@shape{#2}%
    \fi
  \else
    \edef\reserved@a{\k@encoding /\k@family /\k@series/#1}%
     \ifcsname \reserved@a\endcsname
       \edef\k@shape{#1}%
    \else
       \ifcsname \k@encoding /\k@family /\k@series/#2\endcsname
         \edef\k@shape{#2}%
         {\let\curr@fontshape\curr@kfontshape\@font@shape@subst@warning}%
       \else
         \if@shape@roman@kanji
           \set@safe@kanji@shape{#3}%
           {{\let\curr@fontshape\curr@kfontshape\@font@shape@subst@warning}}%
         \else
           \edef\k@shape{#3}%
           {\let\curr@fontshape\curr@kfontshape\@font@shape@subst@warning}%
         \fi
       \fi
    \fi
  \fi
}
\def\merge@kanji@shape@without@substitution#1{%
  \expandafter\expandafter\expandafter
  \merge@kanji@shape@without@substitution@
    \csname shape@\k@shape @#1\endcsname
    {#1}%
    \@nil
}
\def\merge@kanji@shape@without@substitution@#1#2#3\@nil{%
  \def\reserved@a{#3}%
  \ifx\reserved@a\@empty
    \edef\k@shape{#2}%
  \else
    \edef\k@shape{#1}%
  \fi
}
\let\delayed@merge@kanji@shape\merge@kanji@shape@without@substitution

\newif\if@forced@series@kanji
  \DeclareRobustCommand\romanseries[1]{\@forced@seriesfalse
      \expandafter\def\expandafter\delayed@f@adjustment\expandafter
          {\delayed@f@adjustment\delayed@merge@font@series{#1}}}
  \DeclareRobustCommand\kanjiseries[1]{\@forced@series@kanjifalse
      \expandafter\def\expandafter\delayed@k@adjustment\expandafter
          {\delayed@k@adjustment\delayed@merge@kanji@series{#1}}}
  \DeclareRobustCommand\romanseriesforce[1]{\@forced@seriestrue
      \expandafter\def\expandafter\delayed@f@adjustment\expandafter
          {\delayed@f@adjustment\edef\f@series{#1}}}
  \DeclareRobustCommand\kanjiseriesforce[1]{\@forced@series@kanjitrue
      \expandafter\def\expandafter\delayed@k@adjustment\expandafter
          {\delayed@k@adjustment\edef\k@series{#1}}}
  \let\delayed@k@adjustment\@empty
\DeclareRobustCommand\fontseries[1]{\kanjiseries{#1}\romanseries{#1}}%
\DeclareRobustCommand\fontseriesforce[1]{\kanjiseriesforce{#1}\romanseriesforce{#1}}%

  \DeclareRobustCommand\romanshape[1]{%
       \expandafter\def\expandafter\delayed@f@adjustment\expandafter
           {\delayed@f@adjustment\delayed@merge@font@shape{#1}}}
  \DeclareRobustCommand\kanjishape[1]{%
       \expandafter\def\expandafter\delayed@k@adjustment\expandafter
           {\delayed@k@adjustment\delayed@merge@kanji@shape{#1}}}
  \DeclareRobustCommand\fontshape[1]{%
      \romanshape{#1}%
      \expandafter\def\expandafter\delayed@k@adjustment\expandafter
          {\delayed@k@adjustment\@shape@roman@kanjitrue
           \delayed@merge@kanji@shape{#1}\@shape@roman@kanjifalse}}
  \DeclareRobustCommand\romanshapeforce[1]{%
      \expandafter\def\expandafter\delayed@f@adjustment\expandafter
          {\delayed@f@adjustment\edef\f@shape{#1}}}
  \DeclareRobustCommand\kanjishapeforce[1]{%
      \expandafter\def\expandafter\delayed@k@adjustment\expandafter
          {\delayed@k@adjustment\edef\k@shape{#1}}}
  \DeclareRobustCommand\fontshapeforce[1]{%
     \romanshapeforce{#1}%
     \expandafter\def\expandafter\delayed@k@adjustment\expandafter
         {\delayed@k@adjustment\set@safe@kanji@shape{#1}{}}}
\DeclareRobustCommand\usekanji[4]{%
    \kanjiencoding{#1}\edef\k@family{#2}%
    \set@target@series@kanji{#3}\edef\k@shape{#4}%
    \let\delayed@k@adjustment\@empty
    \selectfont\ignorespaces}
\DeclareRobustCommand\useroman[4]{%
    \romanencoding{#1}\edef\f@family{#2}%
    \set@target@series{#3}\edef\f@shape{#4}%
    \let\delayed@f@adjustment\@empty
    \selectfont\ignorespaces}
%%%%

\def\set@safe@kanji@shape#1#2{%
  \edef\reserved@b{\k@encoding /\k@family /\k@series/#1}%
  \ifcsname \reserved@b\endcsname
    \edef\k@shape{#1}%
    #2%
  \else
    \@kanji@shape@nochange@info{\reserved@b}%
  \fi
}
\def\@kanji@shape@nochange@info#1{%
    \@font@info{Kanji font shape `#1' undefined\MessageBreak
                No change}%
}

\protected\def\usefont#1#2#3#4{%
  \ltj@@is@kenc{#1}\ifin@\usekanji{#1}{#2}{#3}{#4}%
  \else\useroman{#1}{#2}{#3}{#4}%
  \fi}

\let\prepare@family@series@update@kanji=\prepare@family@series@update
\patchcmd\prepare@family@series@update{\fontfamily}{\romanfamily}{}{}
\patchcmd\prepare@family@series@update@kanji{\if@forced@series}{\if@forced@series@kanji}{}{}
\patchcmd\prepare@family@series@update@kanji{\fontfamily}{\kanjifamily}{}{}
\patchcmd\prepare@family@series@update@kanji{\update@series@target@value}{\update@series@target@value@kanji}{}{}
\patchcmd\prepare@family@series@update@kanji{\f@family}{\k@family}{}{}
\patchcmd\prepare@family@series@update@kanji{\@meta@family@list}{\@meta@family@list@kanji}{}{}
\patchcmd\prepare@family@series@update@kanji{\fontfamily}{\kanjifamily}{}{}
\patchcmd\prepare@family@series@update@kanji{\maybe@load@fontshape}
  {\begingroup\let\f@encoding\k@encoding\let\f@family\k@family
    \maybe@load@fontshape\endgroup}{}{}
\patchcmd\prepare@family@series@update@kanji{\f@series}{\k@series}{}{}
\patchcmd\prepare@family@series@update@kanji{\f@series}{\k@series}{}{}
\let\update@series@target@value@kanji=\update@series@target@value
\patchcmd\update@series@target@value@kanji{\f@family}{\k@family}{}{}
\patchcmd\update@series@target@value@kanji{\f@series}{\k@series}{}{}
\patchcmd\update@series@target@value@kanji{\f@series}{\k@series}{}{}
\patchcmd\update@series@target@value@kanji{\f@series}{\k@series}{}{}
\patchcmd\update@series@target@value@kanji{\f@series}{\k@series}{}{}
\def\@meta@family@list@kanji{\@elt{mc}\@elt{gt}\@elt{jtt}}

\AddToHook{bfseries/defaults}{%
  \let\bfseries@mc\bfdef@ult
  \let\bfseries@gt\bfdef@ult
  \let\bfseries@jtt\bfdef@ult
}
\AddToHook{mdseries/defaults}{%
  \let\mdseries@mc\mddef@ult
  \let\mdseries@gt\mddef@ult
  \let\mdseries@jtt\mddef@ult
}

\expandafter\patchcmd\csname bfseries \endcsname{%
  \ifx\f@family\rmdef@ult      \fontseries\bfseries@rm
  \else\ifx\f@family\sfdef@ult \fontseries\bfseries@sf
  \else\ifx\f@family\ttdef@ult \fontseries\bfseries@tt
  \else                        \fontseries\bfdefault
  \fi\fi\fi
}{%
  \ifx\f@family\rmdef@ult      \romanseries\bfseries@rm
  \else\ifx\f@family\sfdef@ult \romanseries\bfseries@sf
  \else\ifx\f@family\ttdef@ult \romanseries\bfseries@tt
  \else                        \romanseries\bfdefault
  \fi\fi\fi
}{}{}
\expandafter\patchcmd\csname mdseries \endcsname{%
  \ifx\f@family\rmdef@ult      \fontseries\mdseries@rm
  \else\ifx\f@family\sfdef@ult \fontseries\mdseries@sf
  \else\ifx\f@family\ttdef@ult \fontseries\mdseries@tt
  \else                        \fontseries\mddefault
  \fi\fi\fi
}{%
  \ifx\f@family\rmdef@ult      \romanseries\mdseries@rm
  \else\ifx\f@family\sfdef@ult \romanseries\mdseries@sf
  \else\ifx\f@family\ttdef@ult \romanseries\mdseries@tt
  \else                        \romanseries\mddefault
  \fi\fi\fi
}{}{}

\AddToHook{bfseries}{%
  \ifx\k@family\mcdef@ult      \kanjiseries\bfseries@mc
  \else\ifx\k@family\gtdef@ult \kanjiseries\bfseries@gt
  \else\ifx\k@family\jttdef@ult\kanjiseries\bfseries@jtt
  \else                        \kanjiseries\bfdefault
  \fi\fi\fi
}
\AddToHook{mdseries}{%
  \ifx\k@family\mcdef@ult      \kanjiseries\mdseries@mc
  \else\ifx\k@family\gtdef@ult \kanjiseries\mdseries@gt
  \else\ifx\k@family\jttdef@ult\kanjiseries\mdseries@jtt
  \else                        \kanjiseries\mddefault
  \fi\fi\fi
}

\AddToHook{expand@font@defaults}{%
  \edef\mcdef@ult{\mcdefault}%
  \edef\gtdef@ult{\gtdefault}%
  \edef\jttdef@ult{\jttdefault}%
  \edef\kanjidef@ult{\kanjifamilydefault}}

\appto\init@series@setup{%
  \ifx\kanjiseriesdefault\kanjiseriesdefault@kernel
    \mdseries
    \let\kanjiseriesdefault\k@series
  \fi
}

\AddToHook{normalfont}{%
  \kanjiencoding{\kanjiencodingdefault}%
  \edef\k@family{\kanjifamilydefault}%
  \edef\k@series{\kanjiseriesdefault}%
  \edef\k@shape{\kanjishapedefault}%
  \let\delayed@k@adjustment\empty
}

\DeclareRobustCommand\mcfamily{%
  \unless\ifltj@disablejfam\not@math@alphabet\mcfamily\mathmc\fi
  \prepare@family@series@update@kanji{mc}\mcdefault
  \selectfont}
\DeclareRobustCommand\gtfamily{%
  \unless\ifltj@disablejfam\not@math@alphabet\gtfamily\mathgt\fi
  \prepare@family@series@update@kanji{gt}\gtdefault
  \selectfont}


\let\romanprocess@table\process@table
\def\kanjiprocess@table{%
  \kanjiencoding\kanjiencodingdefault%
  \edef\k@family{\kanjifamilydefault}%
  \edef\k@series{\kanjiseriesdefault}%
  \edef\k@shape{\kanjishapedefault}%
}
\def\process@table{%
  \romanprocess@table
  \kanjiprocess@table
}
\@onlypreamble\romanprocess@table
\@onlypreamble\kanjiprocess@table

%%%%%% LuaTeX-ja specific

% #1/#2 の展開結果から encoding 部分 (#1) をとりだし，それが和文用かどうかの結果を \ifin@ に代入
\def\ltj@@IsFontJapanese#1/#2\relax{\ltj@@is@kenc{#1}}

\let\extract@afont\extract@font
\def\pickup@jfont{%
  \ifnum
    \unless\ifnum\ltjgetparameter{direction}=3 1\else
    \ifltjselectfont@force@define 1\else 0\fi\fi =1 %
    \expandafter \ifx \font@name \relax
       \let\extract@font\extract@jfont
       \define@newfont
       \let\extract@font\extract@afont
    \fi
    \let\getjfont\font@name
  \else
    \begingroup\escapechar\m@ne
      \ifcsname ltj@@fontjfm/\expandafter\string\font@name\endcsname\else
        \let\extract@font\extract@jfont@onlyjfm
        \define@newfont
        \let\extract@font\extract@afont
      \fi
    \endgroup
    \csname ltj@@fontjfm/\expandafter\expandafter\expandafter\@gobble\expandafter\string\font@name\endcsname
  \fi
}
\def\pickup@tfont{%
  \ifnum
    \ifnum\ltjgetparameter{direction}=3 1\else
    \ifltjselectfont@force@define 1\else 0\fi\fi =1 %
    \expandafter \ifx \font@name \relax
       \let\extract@font\extract@tfont
       \define@newfont
       \let\extract@font\extract@afont
    \fi
    \let\gettfont\font@name
  \else
    \begingroup\escapechar\m@ne
      \ifcsname ltj@@fontjfm/\expandafter\string\font@name\endcsname\else
        \let\extract@font\extract@tfont@onlyjfm
        \define@newfont
        \let\extract@font\extract@afont
      \fi
    \endgroup
    \csname ltj@@fontjfm/\expandafter\expandafter\expandafter\@gobble\expandafter\string\font@name\endcsname
  \fi
}

% 異なる和文 enc/fam/ser/shape からは異なるフォント番号が振られるように
% わざと「ゴミをつける」
\def\ltj@@patch@external@font#1 at{%
  \directlua{
        local s = '\luatexluaescapestring{#1}'
        local is_braced, is_quoted
        if s:sub(1,1)=='{' and s:sub(-1)=='}' then is_braced=true; s=s:sub(2,-2) end
        if s:sub(1,1)=='"' and s:sub(-1)=='"' then is_quoted=true; s=s:sub(2,-2) end
        s=s..luatexja.jfont.print_aftl_address()
        if is_braced then s='{'..s..'}' elseif is_quoted then s='"'..s..'"' end
        %print(s, is_quoted, is_braced)
    tex.sprint(-2, s)}%
  \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}%
    }%
    \expandafter\globaljfont\font@name\external@font\relax\font@name% ここで時間がかかる
    \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}%
    }%
    \expandafter\globaltfont\font@name\external@font\relax\font@name
    \csname \f@encoding+\f@family\endcsname
    \csname\curr@fontshape\endcsname}

\newluafunction\ltj@@jfont@@jfmonly@@inner
\directlua{%
  local lf = lua.get_functions_table()
  lf[\the\ltj@@jfont@@jfmonly@@inner]  = luatexja.jfont.load_jfmonly
}
\luadef\ltj@@jfont@@jfmonly  \ltj@@jfont@@jfmonly@@inner
\def\extract@jfont@onlyjfm{%
  \get@external@font\ltj@@convert@input@path@to@lua
  \csxdef{ltj@@fontjfm/\expandafter\string\font@name}{%
    \ltj@@jfont@@jfmonly{\external@font}{yoko}%
}}
\def\extract@tfont@onlyjfm{%
  \get@external@font\ltj@@convert@input@path@to@lua
  \csxdef{ltj@@fontjfm/\expandafter\string\font@name}{%
    \ltj@@jfont@@jfmonly{\external@font}{tate}%
}}

\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@@is@kenc{\f@encoding}\ifin@%
     \ltj@@ja@do@subst@correction
   \else
     \ltj@@al@do@subst@correction
   \fi
}
\let\@@italiccorr=\/

%%%%%% Japanese font for math mode
\unless\ifltj@disablejfam

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

\let\ltj@@al@getanddefine@fonts=\getanddefine@fonts
\def\ltj@@ja@getanddefine@fonts#1#2{%
  \csletcs{if@ltj@tempif}{ifltjselectfont@force@define}%
  \ltjselectfont@force@definetrue
  \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
  \csletcs{ifltjselectfont@force@define}{if@ltj@tempif}%
  \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\expandafter\ltj@@IsFontJapanese\string#2/\relax%
  \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}%

\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
\newluafunction\ltj@@does@alt@set@@inner
\directlua{%
  local lf = lua.get_functions_table()
  lf[\the\ltj@@does@alt@set@@inner]  = luatexja.jfont.does_alt_set
}
\def\ltj@@does@alt@set#1#2{\luafunction\ltj@@does@alt@set@@inner{#1}{#2}}
\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}')}%
}

%%%% send \input@path data to lua
\def\ltj@@convert@input@path@to@lua{%
  \ltj@@cipath@clear
  \ifdefined\input@path
    \expandafter\@tfor\expandafter\ltj@temp\expandafter:\expandafter=\input@path\do{\ltj@@cipath@add{\ltj@temp}}%
  \fi
}
\newluafunction\ltj@@cipath@clear@inner
\directlua{
  (lua.get_functions_table())[\the\ltj@@cipath@clear@inner] = luatexja.input_path_clear
}
\protected\luadef\ltj@@cipath@clear\ltj@@cipath@clear@inner
\def\ltj@@cipath@add#1{\directlua{luatexja.input_path_add('\luatexluaescapestring{#1}')}}
\endinput
