% luatexko-core.sty
%
% Copyright (c) 2013-2016 Dohyun Kim  <nomos at ktug org>
%                         Soojin Nam  <jsunam at gmail com>
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2006/05/20 or later.

\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname selectfont\endcsname\relax
  \input luaotfload.sty
  \ifcsname ifpdf\endcsname\else \input ifpdf.sty \fi
  \catcode`@ = 11
\else
  \ProvidesPackage{luatexko-core}[2016/05/08 v1.14 Typesetting Korean with LuaTeX]
  \RequirePackage{luaotfload}[2016/04/21]
  \RequirePackage{ifpdf}
\fi

\ifdefined\fontid\else
  \let\fontid\luatexfontid % latex  2015/10/01
\fi
\ifdefined\newattribute\else % luaotfload 2.6
  \let\newattribute\newluatexattribute
  \let\unsetattribute\unsetluatexattribute
  \let\scantextokens\luatexscantextokens
  \let\luaescapestring\luatexluaescapestring
\fi
\ifdefined\pdffontsize\else % luatex 0.87
  \def\pdffontsize{\pdffeedback fontsize}
  \protected\def\pdfliteral{\pdfextension literal}
\fi

%% make hangul catcode 12
\count@"1100
\loop \catcode\count@ 12
  \ifnum\count@<"115F \advance\count@\@ne \repeat
\count@"3131
\loop \catcode\count@ 12
  \ifnum\count@<"318E \advance\count@\@ne \repeat
\count@"A960
\loop \catcode\count@ 12
  \ifnum\count@<"A97C \advance\count@\@ne \repeat
\count@"AC00
\loop \catcode\count@ 12
  \ifnum\count@<"D7A3 \advance\count@\@ne \repeat

%% attributes
\newattribute\finemathattr \finemathattr\tw@ % finemath on by default
\newattribute\cjtypesetattr
\newattribute\dotemphattr
\newattribute\autojosaattr
\newattribute\luakorubyattr \chardef\luakorubyattrnum\the\allocationnumber
\newattribute\hangulfntattr
\newattribute\hanjafntattr
\newattribute\fallbackfntattr
\newattribute\hangulpunctsattr

%% load lua modules
\directlua{require "luatexko"}

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

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

%% auto josa
\def\josaignoreparens{\afterassignment\josa@ignore@parens\count@}
\def\josa@ignore@parens{%
  \ifcase\count@  \chardef\josa@attr@level\@ne
  \else           \chardef\josa@attr@level\tw@
  \fi
}
\josaignoreparens=\z@

\protected\def\은{{\autojosaattr\josa@attr@level 은}}
\let\는\은

\protected\def\을{{\autojosaattr\josa@attr@level 을}}
\let\를\을

\protected\def\와{{\autojosaattr\josa@attr@level 와}}
\let\과\와

\protected\def\가{{\autojosaattr\josa@attr@level 가}}
\protected\def\이{{\autojosaattr\josa@attr@level 이}}
\protected\def\라{\이라}

\protected\def\으{{\autojosaattr\josa@attr@level 으}}
\protected\def\로{\으로}

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

%% ruby : ruby.sty by Lemberg is excellent; but
%%        it doesn't match well with luatexko, so implemented anew
%%        with plain tex support and enhanced features.
\def\rubysize{0.6}  % size of ruby compared to base character
\def\rubysep{0.1ex} % distance between base character and ruby
% users can define \rubyfont : eg. \font\rubyfont={foo:+ruby}
\newif\ifruby@overlap
\let\rubyoverlap\ruby@overlaptrue
\let\rubynooverlap\ruby@overlapfalse
\rubyoverlap       % ruby may protrude over adjacent chars
\newcount\luakorubyattrcnt\luakorubyattrcnt\z@
\protected\def\ruby#1#2{%
  \ifnum\luakorubyattrcnt > 65535 \luakorubyattrcnt\z@ \fi
  \global\advance\luakorubyattrcnt\@ne
  \begingroup
  \setbox\z@\hbox attr\luakorubyattrnum\luakorubyattrcnt{% base character
    \vrule width\z@ height.88em depth\z@\relax #1}%
  \setbox\tw@\hbox{% ruby
    \ifdefined\rubyfont \rubyfont \fi
    \ifcsname selectfont\endcsname % latex
      \expandafter\dimen@\pdffontsize\font
      \dimen@\rubysize\dimen@
      \fontsize\dimen@\dimen@\selectfont
    \else % plain
      \luatexko@resize@font\font\resized@font\font\rubysize
      \ifdefined\hangul@font
        \luatexko@resize@font\hangulfont{}\hangul@font\rubysize
      \fi
      \ifdefined\hanja@font
        \luatexko@resize@font\hanjafont{}\hanja@font\rubysize
      \fi
      \ifdefined\fallback@font
        \luatexko@resize@font\fallbackfont{}\fallback@font\rubysize
      \fi
    \fi #2}\ht\tw@\z@\dp\tw@\z@
  \directlua{%
    luatexko.rubynode[\the\luakorubyattrcnt] = {
      node.copy_list(tex.box[2]),
      \number\numexpr\dimexpr\rubysep\relax\relax,
      \ifruby@overlap true\else false\fi}}%
  \leavevmode\box\z@
  \endgroup}

\unless\ifcsname selectfont\endcsname
% font resize under plain tex
\begingroup\edef\x{\endgroup
  \def\noexpand\rem@vesizepart##1\detokenize{ at}##2\noexpand\end{##1}}\x
\def\luatexko@resize@font#1#2#3#4{%
  \begingroup
  \edef\x@{\noexpand\rem@vesizepart\fontname#3\detokenize{ at}\noexpand\end}%
  \edef\y@{\endgroup#1\noexpand#2 \x@\space at#4\dimexpr\pdffontsize#3\relax}%
  \y@#2}
\fi

% xxruby: consecutive ruby annotations
\protected\def\xxruby#1#2{%
  \begingroup\def\basestr{#1}\def\rubystr{#2}\luako@xxrubyxx\endgroup
}
\def\luako@xxrubyxx{%
  \ifx\empty\basestr\else
    \expandafter\luako@ruby@get@base@char\basestr\end
    \expandafter\luako@ruby@get@ruby@char\rubystr\end
    \ruby\basechar\rubychar
    \expandafter\luako@xxrubyxx
  \fi
}
\def\luako@ruby@get@base@char#1#2\end{\def\basechar{#1}\def\basestr{#2}}
\def\luako@ruby@get@ruby@char#1#2\end{\def\rubychar{#1}\def\rubystr{#2}}

%% vertical typesetting
%% EXPERIMENTAL code
\def\luatexko@vertical{%
  \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
      \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@
    \luatexko@rotate@clockwise{\box#1}%
    \ifnum\count@=\@ne\egroup\vfil\else\vfil\egroup\hfil\fi\egroup}
\def\luatexko@rotate@clockwise#1{%
  \ifpdf\pdfliteral{q 0 -1 1 0 0 0 cm}#1\pdfliteral{Q}%
  \else\special{pdf:btrans rotate -90}#1\special{pdf:etrans}%
  \fi
}

% do not veticalize headline/footline
\ifcsname selectfont\endcsname % latex
  \def\verticaltypesetting{%
    \maxdepth\z@ \luatexko@vertical
    \dimen@\textwidth \textwidth\textheight \textheight\dimen@
    \edef\@outputpage{\unexpanded{\luatexkorotatebox\@outputbox
    \textwidth\textheight}\unexpanded\expandafter{\@outputpage}}}
\else % plain
  \def\verticaltypesetting{%
    \dimen@\hsize\hsize\vsize\vsize\dimen@
    \edef\plainoutput{\unexpanded{\dimen@\hsize\hsize\vsize\vsize\dimen@}%
      \unexpanded\expandafter{\plainoutput}}%
    \def\pagebody{\setbox\z@\vbox to\hsize{\boxmaxdepth=\maxdepth
      \pagecontents}\luatexkorotatebox\z@\box\z@}%
    \maxdepth\z@ \luatexko@vertical}
\fi

% horizontal box inside vertical typesetting
\protected\def\horizontal#1{%
  \setbox\z@\vbox\bgroup
    \ifnum\cjtypesetattr<\@ne
      \unsetattribute\cjtypesetattr
      \spaceskip\fontdimen2\font
                plus\fontdimen3\font
                minus\fontdimen4\font
    \fi
    \hsize#1\relax
    \everypar{}%
    \leftskip\z@
    \rightskip\z@
    \parindent\z@
}
\protected\def\endhorizontal{\egroup\luatexkounrotatebox\z@\box\z@}
\protected\def\luatexkounrotatebox#1{%
  \leavevmode
  \vbox to\wd#1\bgroup
    \vfill
    \hbox to\dimexpr\ht#1+\dp#1\bgroup
      \wd#1\z@\ht#1\z@\dp#1\z@
      \luatexko@rotate@counterclockwise{\box#1}%
      \hfill
    \egroup
  \egroup
}
\def\luatexko@rotate@counterclockwise#1{%
  \ifpdf\pdfliteral{q 0 1 -1 0 0 0 cm}#1\pdfliteral{Q}%
  \else \special{pdf:btrans rotate 90}#1\special{pdf:etrans}%
  \fi
}

%% underline using post_linebreak_filter
%%  ulem doesn't work well with luatexko;
%%  especially linebreak does not occur inside cjk words.
\def\ulinewidth{0.04em }
\def\ulinedown {0.25em }
\newcount\luakoulineboxcnt\luakoulineboxcnt\z@
\protected\def\uline#1{%
  \begingroup
  \ifnum\luakoulineboxcnt > 65535 \luakoulineboxcnt\z@ \fi
  \global\advance\luakoulineboxcnt\@ne
  \edef\luako@temp@{\the\luakoulineboxcnt}%
  \leavevmode
  \dimen@\dimexpr\ulinedown+\ulinewidth\relax
  \dimen@ii-\dimexpr\ulinedown\relax
  \setbox\z@\hbox{\vrule width0pt height\dimen@ii depth\dimen@}%
  \directlua{ luatexko.ulinebox[\luako@temp@]=node.copy_list(tex.box[0].head)
              luatexko.uline_boundary(\luako@temp@,true) }%
  #1%
  \directlua{ luatexko.uline_boundary(\luako@temp@,false) }%
  \endgroup}
\protected\def\sout#1{{\def\ulinedown{-0.5ex-\ulinewidth/2}\uline{#1}}}
% ulem with arbitray box
\protected\def\markoverwith#1#2{%
  \begingroup
  \ifnum\luakoulineboxcnt > 65535 \luakoulineboxcnt\z@ \fi
  \global\advance\luakoulineboxcnt\@ne
  \edef\luako@temp@{\the\luakoulineboxcnt}%
  \leavevmode
  \setbox\z@\hbox{#1}%
  \directlua{ luatexko.ulinebox[\luako@temp@]=node.copy_list(tex.box[0])
              luatexko.uline_boundary(\luako@temp@,true) }%
  #2%
  \directlua{ luatexko.uline_boundary(\luako@temp@,false) }%
  \endgroup}
\protected\def\uuline{\dimen@ii-\dimexpr\ulinedown\relax
  \dimen@\dimexpr\ulinedown+\ulinewidth\relax
  \markoverwith{\vtop{\hrule width.1em height\dimen@ii depth\dimen@
  \kern.1em\hrule height\dimen@ii depth\dimen@}}}
\protected\def\uwave {\ifx\ltxko@sixly\undefined \font\ltxko@sixly=lasy6 \fi
  \markoverwith{\lower4\p@\hbox{\ltxko@sixly\char58}}}
\protected\def\xout  {\markoverwith{\hbox to.35em{/\hss}}}
\protected\def\dashuline{\markoverwith{\kern.125em\vtop{\kern\ulinedown
  \hrule width.3em height\z@ depth\ulinewidth}\kern.125em}}
\protected\def\dotuline{\dimen@\dimexpr\ulinedown+0.1ex\relax
  \markoverwith{\lower\dimen@\hbox{\kern.1em .\kern.04em}}}

%% hangul font -- core
\def\luatexko@hangul@selectfont{%
  \ifdefined\hangul@font
  \begingroup
  \let\luatexko@hangul@selectfont\relax
  \let\luatexko@hanja@selectfont\relax
  \let\luatexko@fallback@selectfont\relax
  \hangul@font
  \expandafter\expandafter\expandafter
  \endgroup
  \expandafter\hangulfntattr\fontid\font\relax
  \fi
}
\def\luatexko@hanja@selectfont{%
  \ifdefined\hanja@font
  \begingroup
  \let\luatexko@hangul@selectfont\relax
  \let\luatexko@hanja@selectfont\relax
  \let\luatexko@fallback@selectfont\relax
  \hanja@font
  \expandafter\expandafter\expandafter
  \endgroup
  \expandafter\hanjafntattr\fontid\font\relax
  \fi
}
\def\luatexko@fallback@selectfont{%
  \ifdefined\fallback@font
  \begingroup
  \let\luatexko@hangul@selectfont\relax
  \let\luatexko@hanja@selectfont\relax
  \let\luatexko@fallback@selectfont\relax
  \fallback@font
  \expandafter\expandafter\expandafter
  \endgroup
  \expandafter\fallbackfntattr\fontid\font\relax
  \fi
}

% user interface for plain tex
\protected\def\hangulfont
  {\afterassignment\luatexko@hangul@selectfont\font\hangul@font}
\protected\def\hanjafont
  {\afterassignment\luatexko@hanja@selectfont \font\hanja@font }
\protected\def\fallbackfont
  {\afterassignment\luatexko@fallback@selectfont\font\fallback@font}

%% hangul in math mode -- general
\def\setmathhangulblock#1#2{%
  \count@="#1
  \loop
    \Umathcode\count@ = 7 \symmathhangul \count@
    \ifnum\count@<"#2
      \advance\count@\@ne
  \repeat}
\unless\ifcsname selectfont\endcsname
  % for plain; see luatexko.sty for latex
  \newfam\symmathhangul
  \def\setmathhangulfont#1#{\setmathhangul@font@}
  \def\setmathhangul@font@#1{%
    \font\tenhangul   = "#1" at 10pt
    \font\sevenhangul = "#1" at 7pt
    \font\fivehangul  = "#1" at 5pt
    \textfont\symmathhangul         = \tenhangul
    \scriptfont\symmathhangul       = \sevenhangul
    \scriptscriptfont\symmathhangul = \fivehangul}
  \setmathhangulblock{AC00}{D7A3}
\fi

%% hangul normalization
\def\luatexhangulnormalize{\directlua{require "luatexko-normalize"}%
  \afterassignment\luatexh@ngulnorm@lize\count@}
\def\luatexh@ngulnorm@lize{%
  \ifcase\count@ % 0: none
    \directlua{ luatexkonormalize.unload() }%
  \or % 1: nfc
    \directlua{ luatexkonormalize.compose() }%
  \else % 2: nfd
    \directlua{ luatexkonormalize.decompose() }%
  \fi}

%% uhc to utf8
\def\luatexuhcinputencoding{\directlua{require "luatexko-uhc2utf8"}%
  \afterassignment\luatexuhcinput@ncoding\count@}
\def\luatexuhcinput@ncoding{%
  \ifcase\count@
    \directlua{ luatexkouhc2utf8.stopconvert() }%
  \else
    \directlua{ luatexkouhc2utf8.startconvert() }%
  \fi}

%% uhc file names
\def\luatexuhcfilenames{\directlua{require "luatexko-uhc2utf8"}%
  \afterassignment\luatexuhcfilen@mes\count@}
\def\luatexuhcfilen@mes{%
  \ifcase\count@
    \directlua{ luatexkouhc2utf8.stop_uhc_filename() }%
  \else
    \directlua{ luatexkouhc2utf8.start_uhc_filename() }%
  \fi}

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

% latin punctuations typeset with hangul fonts
\def\hangulpunctuations{\afterassignment\h@ngulpunctu@tions\count@}
\def\h@ngulpunctu@tions{%
  \ifcase\count@
    \unsetattribute\hangulpunctsattr
  \else
    \hangulpunctsattr\@ne
  \fi
}
\def\registerpunctuations#1{\registerpunctu@tions#1,*,}
\def\registerpunctu@tions#1,{%
  \ifx\empty#1\empty
    \expandafter\registerpunctu@tions
  \else\ifx*#1\else
    \directlua{ luatexko.hangulpunctuations[\number#1]=true }
    \expandafter\expandafter\expandafter\registerpunctu@tions
  \fi\fi
}
\def\unregisterpunctuations#1{\unregisterpunctu@tions#1,*,}
\def\unregisterpunctu@tions#1,{%
  \ifx\empty#1\empty
    \expandafter\unregisterpunctu@tions
  \else\ifx*#1\else
    \directlua{ luatexko.hangulpunctuations[\number#1]=nil }
    \expandafter\expandafter\expandafter\unregisterpunctu@tions
  \fi\fi
}

% Actual Text: beware that #2 should not be divided into separate pages.
% \actualtext* variant inserts space char from phvr8r. see accsupp manual
\protected\def\actualtext#1#{%
  \ifx\empty#1\empty
    \expandafter\actualtext@\expandafter!%
  \else
    \expandafter\actualtext@\expandafter*%
  \fi
}
\def\actualtext@{%
  \begingroup
  \catcode`\\=12 \catcode`\$=12 \catcode`\&=12 \catcode`\#=12
  \catcode`\^=12 \catcode`\_=12 \catcode`\~=12 %catcode`\%=12
  \actualtext@@
}
\def\actualtext@@#1#2{%
  \endgroup
  \luatexko@pdfspecial{/Span<</ActualText\directlua{
    luatexko.actualtext("\luaescapestring{\detokenize{#2}}")
  }>>BDC}%
  \scantextokens{#2}%
  \ifx*#1%
    \begingroup
    \ifdefined\actualtext@spacefont\else
      \global\font\actualtext@spacefont=phvr8r\relax
    \fi
    \setbox\z@\hbox{\actualtext@spacefont\char32}%
    \wd\z@\z@\ht\z@\z@\dp\z@\z@
    \leavevmode\copy\z@
    \endgroup
  \fi
  \luatexko@pdfspecial{EMC}%
}
\def\luatexko@pdfspecial#1{%
  \ifpdf\pdfliteral page{#1}%
  \else\special{pdf:content #1}%
  \fi
}

%% 말줄임표
\def\hellipsis{^^^^2026^^^^2026}

\endinput
