% LuaTeX-ja core TeX source

\catcode`\@=11
\catcode"FFFFF=14 %"

\input luaotfload.sty
\input ltxcmds.sty
\input xkeyval.tex
\input infwarerr.sty
\ifdefined\ltj@loaded\endinput\fi
\def\ltj@loaded{hoge}

\newcount\ltj@tempcnta
\newcount\ltj@tempcntb
\newcount\ltj@tempcntc
\newdimen\ltj@tempdima
\newskip\ltj@tempskipa

\newdimen\zw
\newdimen\zh
\newdimen\jQ \jQ=0.25mm
\newdimen\jH \jH=0.25mm

%%%%%%%% Attributes for Japanese typesetting.
\newluatexattribute\ltj@curjfnt   % index for ``current Japanese font''
\newluatexattribute\ltj@charclass % 
\newluatexattribute\ltj@yablshift % attribute for \yabaselineshift
\newluatexattribute\ltj@ykblshift % attribute for \ykbaselineshift
\newluatexattribute\ltj@autospc   % attribute for autospacing
\newluatexattribute\ltj@autoxspc  % attribute for autoxspacing
\newluatexattribute\ltj@icflag    % attribute for italic correction
\newlanguage\ltj@japanese

%%%%%%%% Loading lua files
\directlua{%
  utf = unicode.utf8
  ltj = {}
  function ltj.loadlua(file)
    local path = assert(kpse.find_file(file, 'tex'),
      "File '"..file.."' not found")
    texio.write_nl("("..path..")")
    dofile(path)
  end
  require('lualibs')
  ltj.loadlua('luatexja-rmlgbm.lua') 
    % For Ryumin-Light and GothicBBB-Medium.
  ltj.loadlua('luatexja-jfont.lua')
  ltj.loadlua('luatexja-core.lua')
  ltj.loadlua('luatexja-jfmglue.lua')
  ltj.loadlua('luatexja-xkanji.lua')
}


%%%%%%%% Attributes for character ranges
\ltj@tempcnta=0
\loop\ifnum\ltj@tempcnta<7
  \expandafter\newluatexattribute%
    \csname ltj@kcat\the\ltj@tempcnta\endcsname
  \csname ltj@kcat\the\ltj@tempcnta\endcsname=0
  \advance\ltj@tempcnta by1
\repeat

%%%%%%%% \asluastring
\def\asluastring#1{'\luaescapestring{\detokenize{#1}}'}

%%%%%%%% Redefine \/
%\let\ltj@ic=\/ \protected\def\/{{\ltj@icflag=1\ltj@ic}}
\protected\def\/{\directlua{ltj.ext_append_italic()}}

%%%%%%%% \jfont\CS={...:...;jfm=metric;...}, \globaljfont
\protected\def\jfont{\afterassignment\ltj@@jfont\directlua{ltj.ext_jfontdefX(false)}}
\protected\def\globaljfont{%
  \afterassignment\ltj@@jfont\directlua{ltj.ext_jfontdefX(true)}}
\def\ltj@@jfont{\directlua{ltj.ext_jfontdefY()}}

%%%%%%%% \inhibitglue
\protected\def\inhibitglue{\directlua{ltj.ext_create_inhibitglue_node()}}

%%%%%%%% \ltjdefcharrange<name>{100-200,3000-,5000,...}
\protected\def\ltjdefcharrange#1#2{%
  \ltj@tempcntc=#1 \expandafter\ltj@@dcrange#2,,\ignorespaces}
\def\ltj@@dcrange#1,{\def\ltj@temp{#1}%
  \ifx\ltj@temp\empty\let\@next=\relax\else
  \ltj@@dcrangeA{#1}\let\@next=\ltj@@dcrange\fi\@next}
\def\ltj@@dcrangeA#1{\ltj@@dcrangeB#1--\@nil}
\def\ltj@@dcrangeB#1-#2-#3\@nil{\def\ltj@temp{#3}%
  \ifx\ltj@temp\empty
    \ltj@tempcnta=#1 \ltj@tempcntb=\ltj@tempcnta
  \else
    \def\ltj@temp{#1}%
    \ifx\ltj@temp\empty\ltj@tempcnta='200 \else\ltj@tempcnta=#1 \fi
    \def\ltj@temp{#2}%
    \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF \else\ltj@tempcntb=#2 \fi%"
  \fi
  \directlua{ltj.ext_add_char_range(\the\ltj@tempcnta,\the\ltj@tempcntb,
    \the\ltj@tempcntc)}%
  }

%%%%%%%% \ltjsetparameter
\newcount\ltj@@stack \newcount\ltj@@group@level
\ltj@@group@level=0 \ltj@@stack=0

% prebreakpenalty = {<char_code>, <penalty>}
\define@key[ltj]{japaram}{kcatcode}{%
  \expandafter\ltj@@set@stack#1:{kcat}{0}{0x7FFFFFFF}}
\define@key[ltj]{japaram}{prebreakpenalty}{%
  \expandafter\ltj@@set@stack#1:{pre}{-10000}{10000}}
\define@key[ltj]{japaram}{postbreakpenalty}{%
  \expandafter\ltj@@set@stack#1:{post}{-10000}{10000}}
\def\ltj@@set@stack#1,#2:#3#4#5{%
  \ltj@tempcnta=#1\relax \ltj@tempcntb=#2\relax
  \directlua{ltj.ext_set_stack_table(ltj.isglobal, \asluastring{#3},
    \the\ltj@tempcnta,tex.getcount('ltj@tempcntb'),#4,#5)}}

% yalbaselineshift = <dimen>
% yjabaselineshift = <dimen>
\define@key[ltj]{japaram}{yalbaselineshift}{%
  \ltj@tempdima=#1\relax%
  \directlua{tex.setattribute(ltj.isglobal, 
    luatexbase.attributes['ltj@yablshift'],
    tex.getdimen('ltj@tempdima'))}}
\define@key[ltj]{japaram}{yjabaselineshift}{%
  \ltj@tempdima=#1\relax%
  \directlua{tex.setattribute(ltj.isglobal, 
    luatexbase.attributes['ltj@ykblshift'],
    tex.getdimen('ltj@tempdima'))}}

% jaxspmode = {<char_code>, <mode>}
% mode: inhibit, preonly, postonly, allow
%    or       0        2         1      3
\define@key[ltj]{japaram}{jaxspmode}{%        \inhibitxspcode
  \expandafter\ltj@set@jaxspmode#1:\relax}
\def\ltj@set@jaxspmode#1,#2:{%
  \lowercase{\edef\ltj@temp{#2}}%
  \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
  \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
  \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
  \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
  \ltj@tempcnta=#1\relax\ltj@tempcntb=\ltj@temp\relax
  \directlua{ltj.ext_set_stack_table(ltj.isglobal, 'xsp', \the\ltj@tempcnta,
    \the\ltj@tempcntb,0,3)}}

% alxspmode = {<char_code>, <mode>}
% mode: inhibit, preonly, postonly, allow
%    or       0        1         2      3
\define@key[ltj]{japaram}{alxspmode}{%        \inhibitxspcode
  \expandafter\ltj@set@alxspmode#1:\relax}
\def\ltj@set@alxspmode#1,#2:{%
  \lowercase{\edef\ltj@temp{#2}}%
  \def\ltj@tempa{inhibit}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{0}\fi
  \def\ltj@tempa{preonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{1}\fi
  \def\ltj@tempa{postonly}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{2}\fi
  \def\ltj@tempa{allow}\ifx\ltj@temp\ltj@tempa\def\ltj@temp{3}\fi
  \ltj@tempcnta=#1\relax\ltj@tempcntb=\ltj@temp\relax
  \directlua{ltj.ext_set_stack_table(ltj.isglobal, 'xsp', \the\ltj@tempcnta,
    \the\ltj@tempcntb,0,3)}}

% autospacing = <bool> (default: true)
\define@boolkey[ltj]{japaram}{autospacing}[true]{%
  \ifltj@japaram@autospacing
    \directlua{ltj.ext_print_global()}\ltj@autospc=1
  \else
    \directlua{ltj.ext_print_global()}\ltj@autospc=0
  \fi}

% autoxspacing = <bool> (default: true)
\define@boolkey[ltj]{japaram}{autoxspacing}[true]{%
  \ifltj@japaram@autoxspacing
    \directlua{ltj.ext_print_global()}\ltj@autoxspc=1
  \else
    \directlua{ltj.ext_print_global()}\ltj@autoxspc=0
  \fi}

\define@key[ltj]{japaram}{kanjiskip}{%              % SKIP
  \ltj@tempskipa=#1\relax
  \directlua{ltj.ext_set_stack_skip(ltj.isglobal, 'kanjiskip')}}
\define@key[ltj]{japaram}{xkanjiskip}{%              % SKIP
  \ltj@tempskipa=#1\relax
  \directlua{ltj.ext_set_stack_skip(ltj.isglobal, 'xkanjiskip')}}
\define@key[ltj]{japaram}{jcharwidowpenalty}{%      %COUNT
  \ltj@tempcnta=#1 
  \directlua{ltj.ext_set_stack_table(ltj.isglobal, 'jwp', 0,
    \the\ltj@tempcnta, -10000, 10000)}}

% differentjfm = { large | small | average | both }
\define@choicekey*+[ltj]{japaram}{differentjfm}[\ltj@temp\ltj@tempr]%
  {large,small,average,both}{%
  \ifcase\ltj@tempr
    \directlua{ltj.ja_diffmet_rule = math.max}\or
    \directlua{ltj.ja_diffmet_rule = math.min}\or
    \directlua{ltj.ja_diffmet_rule = math.two_average}\or
    \directlua{ltj.ja_diffmet_rule = math.two_add}
  \fi
}{\@PackageWarning{luatexja}{ignored invalid argument '#1' for 'differentjfm'}}
  % large, small, average, both


% jacharrange = { +-<range_number> }
\define@key[ltj]{japaram}{jacharrange}{\expandafter\ltj@@scrange#1,,}
\def\ltj@@scrange#1,{%
  \edef\ltj@temp{#1}%
  \ifx\ltj@temp\empty\let\next=\relax\else\let\next=\ltj@@scrange
    \ltj@tempcnta=#1 \directlua{ltj.ext_toggle_char_range(%
      ltj.is_global,\the\ltj@tempcnta)}%
  \fi\next
}

\protected\def\ltjsetparameter#1{\directlua{ltj.isglobal=''}%
  \setkeys[ltj]{japaram}{#1}\ignorespaces}
\protected\def\ltjglobalsetparameter#1{\directlua{ltj.isglobal='global'}%
  \setkeys[ltj]{japaram}{#1}\ignorespaces}

%%%%%%%% 
\def\ltj@@decl@array@param#1{%
  \expandafter\let\csname ltj@@array@param/#1\endcsname=t%
}
\ltj@@decl@array@param{prebreakpenalty}
\ltj@@decl@array@param{postbreakpenalty}
\ltj@@decl@array@param{jaxspmode}
\ltj@@decl@array@param{alxspmode}
\ltj@@decl@array@param{kcatcode}
\ltj@@decl@array@param{jacharrange}
\ltj@@decl@array@param{chartorange}
\def\ltjgetparameter#1{%
  \ifcsname ltj@@array@param/#1\endcsname
    \expandafter\ltx@firstoftwo
  \else
    \expandafter\ltx@secondoftwo
  \fi
  {\ltj@@getparam@two{#1}}%
  {\ltj@@getparam@one{#1}}%
}
\def\ltj@@getparam@one#1{%
  \directlua{ltj.ext_get_parameter_unary('#1')}}%
\def\ltj@@getparam@two#1#2{\ltj@tempcnta=#2
  \directlua{ltj.ext_get_parameter_binary('#1', \the\ltj@tempcnta)}%
}



\def\ltj@temp{plain}
\ifx\fmtname\ltj@temp
  \message{plain format: loading luatexja-plain.tex}
\fi
\input luatexja-plain.tex
\catcode`\@=12
\endinput
