%%% written by Dohyun Kim <nomos at ktug dot kr>
%%% public domain
%%%
%%% $Id: luatexko-core.tex,v 1.21 2011/07/22 03:27:04 nomos Exp $

% attributes
\newluatexattribute\finemathattr \finemathattr\@ne % default : finemathattr on
\newluatexattribute\cjtypesetattr
\newluatexattribute\dotemphattr
\newluatexattribute\autojosaattr
\newluatexattribute\rubyattr \chardef\rubyattrnumber\the\allocationnumber
\newluatexattribute\luakoglueattr % glues created by luatexko
\newluatexattribute\unicodeattr
\newluatexattribute\hangfntattr
\newluatexattribute\hanjfntattr
\newluatexattribute\verticalattr

% load lua modules
\RequireLuaModule{luatexko}

% japanese/chinese
\protected\def\japanese{\frenchspacing\cjtypesetattr\@ne
  \luatexkolatinspaceskip\parindent1em }
\protected\def\Schinese{\frenchspacing\cjtypesetattr\tw@
  \luatexkolatinspaceskip\parindent2em }
\protected\def\Tchinese{\frenchspacing\cjtypesetattr\thr@@
  \luatexkolatinspaceskip\parindent2em }
\let\chinese\Schinese
% korean in japanese/chinese env.
\protected\def\korean{\unsetluatexattribute\cjtypesetattr}

% inhibitglue
\protected\def\inhibitglue{{\unsetluatexattribute\cjtypesetattr\hskip\z@skip}}

% auto josa
\protected\def\은{{\autojosaattr\@ne 은}}
\let\는\은

\protected\def\을{{\autojosaattr\@ne 을}}
\let\를\을

\protected\def\와{{\autojosaattr\@ne 와}}
\let\과\와

\protected\def\가{{\autojosaattr\@ne 가}}
\protected\def\이{{\autojosaattr\@ne 이}}
\protected\def\라{\이라}

\protected\def\으{{\autojosaattr\@ne 으}}
\protected\def\로{\으로}

% dot-emphasis
\def\dotemphraise{0.4em }
\unless\ifdefined\bfseries\let\bfseries\bf\fi
\def\dotemphchar{\bfseries ^^^^02d9}
\newcount\dotemph@cnt \dotemph@cnt\z@
\protected\def\dotemph#1{{%
  \ifnum\dotemph@cnt > 100 \global\dotemph@cnt\z@ \fi
  \global\advance\dotemph@cnt\@ne
  \setbox\z@\hbox{\raise\dotemphraise\hbox{\dotemphchar}}\ht\z@\z@\dp\z@\z@
  \directlua{luatexko.dotemphnode[\the\dotemph@cnt] = node.copy_list(tex.box[0])}%
  \dotemphattr\dotemph@cnt  #1}}

% ruby : ruby.sty by Lemberg is excellent; but
%        it doesn't match well with luatexko, so implemented anew
%        with plaintex support and enhanced features.
\def\rubysize{0.5} % size of ruby compared to base character
\def\rubysep{0.1ex}  % distance between base character and ruby
\newif\ifruby@overlap
\let\rubyoverlap\ruby@overlaptrue
\let\rubynooverlap\ruby@overlapfalse
\rubyoverlap       % ruby may protrude over adjacent chars
\newcount\rubyattr@cnt\rubyattr@cnt\z@
\protected\def\ruby#1#2{%
  \ifnum\rubyattr@cnt > 1000 \global\rubyattr@cnt\z@ \fi
  \global\advance\rubyattr@cnt\@ne
  \begingroup
  \setbox\z@\hbox attr\rubyattrnumber\rubyattr@cnt{% base character
    \vrule width\z@ height.88em depth\z@\relax #1}%
  \setbox\tw@\hbox{% ruby
    \expandafter\dimen@\pdffontsize\the\font
    \dimen@\rubysize\dimen@
    \ifcsname selectfont\endcsname
      \fontsize\dimen@\dimen@\selectfont
    \else
      \resizecurrentfont{\the\dimen@}%
    \fi #2}\ht\tw@\z@\dp\tw@\z@
  \directlua{%
    luatexko.rubynode[\the\rubyattr@cnt] = {
	node.copy_list(tex.box[2]),
        \number\numexpr\dimexpr\rubysep\relax\relax,
	\ifruby@overlap true\else false\fi}}%
  \leavevmode\box\z@
  \endgroup}


%%% vertical typesetting
\def\luatexko@vertical{%
  \verticalattr\@ne
  \ifnum\cjtypesetattr<\@ne
    \cjtypesetattr\z@ % korean-vertical
    \spaceskip 0.5em plus 0.15em minus 0.06em
    \parindent 1em
  \fi
}
\protected\def\vertical#1{%
  \leavevmode
  \setbox\z@\vbox\bgroup
      \hsize#1\relax
      \luatexko@vertical
      \parindent\z@
      \everypar{}%
      \leftskip\z@
      \rightskip\z@ }
\protected\def\endvertical{\egroup\luatexkorotatebox\z@\box\z@}
\protected\def\luatexkorotatebox#1{%
  \ifvbox#1\count@\@ne\else\count@\z@\fi
  \dimen@\wd#1\dimen@ii\dimexpr\ht#1+\dp#1\relax
  \setbox#1\ifnum\count@=\@ne
  \vbox to\dimen@\bgroup\hbox to\dimen@ii\bgroup\hfil\else
  \hbox to\dimen@ii\bgroup\vbox to\dimen@\bgroup\fi
    \wd#1\z@ \ht#1\z@ \dp#1\z@
    \pdfliteral{ q 0 -1 1 0 0 0 cm}\box#1\pdfliteral{ Q}%
    \ifnum\count@=\@ne\egroup\vfil\else\vfil\egroup\hfil\fi\egroup}
% \vertlatinbox : latin chars inside vertical; not needed anymore
\protected\def\vertlatin#1{{\luatexkolatinspaceskip #1}}

\def\luatexkolatinspaceskip{\spaceskip\fontdimen\tw@\the\font
  plus \fontdimen\thr@@\the\font minus \fontdimen4\the\font}

%% underline using post_linebreak_filter
%%  ulem doesn't match well with luatexko;
%%  especially linebreak does not occur inside cjk words.
\def\ulinewidth{0.04em }
\def\ulinedown {0.21em }
\newcount\ulinebox@cnt\ulinebox@cnt\z@
\protected\def\uline#1{%
  \ifnum\ulinebox@cnt > 100 \global\ulinebox@cnt\z@ \fi
  \global\advance\ulinebox@cnt\@ne
  \dimen@\dimexpr\ulinedown+\ulinewidth\relax
  \dimen@ii-\dimexpr\ulinedown\relax
  \setbox\z@\hbox{\vrule width\z@ height\dimen@ii depth\dimen@}%
  \directlua{luatexko.ulinebox[\the\ulinebox@cnt]=node.copy_list(tex.box[0].head)}%
  \leavevmode\special{luako:ulinebegin=\the\ulinebox@cnt}%
  #1\special{luako:ulineend}}
%% strike-out
\protected\def\sout#1{{\def\ulinedown{-0.5ex-\ulinewidth/2}\uline{#1}}}
%% ulem with arbitray box
\protected\def\markoverwidth#1#2{%
  \ifnum\ulinebox@cnt > 100 \global\ulinebox@cnt\z@ \fi
  \global\advance\ulinebox@cnt\@ne
  \setbox\z@\hbox{#1}%
  \directlua{luatexko.ulinebox[\the\ulinebox@cnt]=node.copy_list(tex.box[0])}%
  \leavevmode\special{luako:ulinebegin=\the\ulinebox@cnt}%
  #2\special{luako:ulineend}}
\protected\def\uuline{\dimen@ii-\dimexpr\ulinedown\relax
  \dimen@\dimexpr\ulinedown+\ulinewidth\relax
  \markoverwidth{\vtop{\hrule width.2em height\dimen@ii depth\dimen@
  \kern.1em\hrule height\dimen@ii depth\dimen@}}}
\protected\def\uwave {\dimen@\dimexpr\ulinedown+0.3em\relax
  \markoverwidth{\lower\dimen@\hbox to 0.38em{\char"7E\hss}}}
\protected\def\xout  {\markoverwidth{\hbox to.333em{/\hss}}}

%%%%%
\def\luatexko@hangul@selectfont{%
  \begingroup
    \let\luatexko@hangul@selectfont\relax
    \let\luatexko@hanja@selectfont\relax
    \check@hangul@family
    \ifdefined\hangul@font \hangul@font \fi
    \xdef\luatexko@hangfont@temp{\fontname\font}%
  \endgroup
  \directlua{
    local hang = luatexko.hangulfont["\luatexko@hangfont@temp"]
    if hang then
      tex.setattribute("hangfntattr", hang)
    else
      local c = luatexko.hangulcount
      c = c + 1
      luatexko.hangulcount = c
      tex.setattribute("hangfntattr",c)
      luatexko.hangulfont["\luatexko@hangfont@temp"] = c
      luatexko.hangulfont[c] = "\luatexko@hangfont@temp"
    end }}
\def\luatexko@hanja@selectfont{%
  \begingroup
    \let\luatexko@hangul@selectfont\relax
    \let\luatexko@hanja@selectfont\relax
    \check@hanja@family
    \ifdefined\hanja@font \hanja@font \fi
    \xdef\luatexko@hanjfont@temp{\fontname\font}%
  \endgroup
  \directlua{
    local hanj = luatexko.hangulfont["\luatexko@hanjfont@temp"]
    if hanj then
      tex.setattribute("hanjfntattr", hanj)
    else
      local c = luatexko.hangulcount
      c = c + 1
      luatexko.hangulcount = c
      tex.setattribute("hanjfntattr",c)
      luatexko.hangulfont["\luatexko@hanjfont@temp"] = c
      luatexko.hangulfont[c] = "\luatexko@hanjfont@temp"
    end }%
    \ifnum\cjtypesetattr>\z@\luatexkolatinspaceskip\fi
    }

%% hangul in math mode -- general
\def\setmathhangulblock#1#2{%
  \count@="#1
  \loop
    \luatexUmathcode\count@ = 7 \symmathhangul \count@
    \ifnum\count@<"#2
      \advance\count@\@ne
      \repeat}

%% hangul normalization
\def\luatexhangulnormalize{\RequireLuaModule{hangulnormalize}%
  \afterassignment\luatexh@ngulnorm@lize\count@}
\def\luatexh@ngulnorm@lize{%
  \ifnum\count@=\z@
    \directlua{ hangulnormalize.unload() }%
  \else\ifnum\count@=\@ne % nfc
    \directlua{ hangulnormalize.compose() }%
  \else\ifnum\count@=\tw@ % nfd
    \directlua{ hangulnormalize.decompose() }%
  \fi\fi\fi}

%% uhc 2 utf8
\def\luatexuhcinputencoding{\RequireLuaModule{uhc2utf8}%
  \afterassignment\luatexuhcinput@ncoding\count@}
\def\luatexuhcinput@ncoding{%
  \ifnum\count@>\z@
    \directlua{ uhc2utf8.startconvert() }%
  \else
    \directlua{ uhc2utf8.stopconvert() }%
  \fi}

%% uhc file names
\def\luatexuhcfilenames{\RequireLuaModule{uhc2utf8}%
  \afterassignment\luatexuhcfilen@mes\count@}
\def\luatexuhcfilen@mes{%
  \ifnum\count@>\z@
    \directlua{ uhc2utf8.start_uhc_filename() }%
  \else
    \directlua{ uhc2utf8.stop_uhc_filename() }%
  \fi}

%% use hanja font for hanja chars
\def\hanjafontforhanja{\afterassignment\h@nj@fontforh@nj@\count@}
\def\h@nj@fontforh@nj@{%
  \ifnum\count@>\z@
    \directlua{ luatexko.hanjafontforhanja = true }%
  \else
    \directlua{ luatexko.hanjafontforhanja = false }%
  \fi}

%%% hope to be able to delete later
% mplib: btex .. etex is implemented.
\input luatexkomplib

% frenchspacing is default
\frenchspacing

% 말줄임표
\def\hellipsis{^^^^2026\kern-.1em ^^^^2026}

\endinput
