% luatexko.sty
%
% Copyright (c) 2013-2014 Dohyun Kim  <nomos at ktug org>
%
% 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.

\ifx\luatexkocatcodeofatchar\undefined\else
  \expandafter\endinput\fi % no multiple loading
\ifx\ProvidesPackage\relax \let\ProvidesPackage\undefined \fi
\ifx\ProvidesPackage\undefined % plain tex
  \edef\luatexkocatcodeofatchar{\catcode`@=\the\catcode`@}
  \input luatexko-core.sty
  %\hangulfont{name:NanumGothic:interlatincjk=0.125em} at 10pt
  %\setmathhangulfont{NanumGothic}
  \luatexkocatcodeofatchar
  \expandafter\endinput
\fi

\ProvidesPackage{luatexko}[2014/06/17 v1.6 Typesetting Korean with LuaLaTeX]

%% package options
\newif\if@hangul
\newif\if@hanja
\newif\if@unfonts
\newif\if@luatexko@loadfontspec\@luatexko@loadfontspectrue
\DeclareOption{hangul}{\@hangultrue}
\DeclareOption{hanja}{\@hangultrue\@hanjatrue}
\DeclareOption{unfonts}{\@unfontstrue}
\DeclareOption{nofontspec}{\@luatexko@loadfontspecfalse}
\ProcessOptions\relax

\if@luatexko@loadfontspec
  %% euenc redefines \f@size by \DeclareErrorFont. sigh
  \let\luatexko@f@size\f@size
  \RequirePackage{fontspec}[2014/05/25]
  \let\f@size\luatexko@f@size
\fi

\RequirePackage{luatexko-core}

\if@luatexko@loadfontspec
  %% fontspec Korean options
  \newfontfeature{YetHangul}{script=hang}
  \newfontfeature{InterHangul}{interhangul=#1}
  \newfontfeature{InterLatinCJK}{interlatincjk=#1}
  \newfontfeature{PunctRaise}{punctraise=#1}
  \newfontfeature{CharRaise}{charraise=#1}
  \newfontfeature{NoEmbed}{embedding=no}
  \aliasfontfeature{NoEmbed}{NoEmbedding}
  \newfontfeature{protrusion}{protrusion=#1}
  \newfontfeature{expansion}{expansion=#1}
  \ExplSyntaxOn
  \DeclareDocumentCommand \newhangulfontfeature {mmmm}
    {
      \keys_define:nn { fontspec }
        {
          #1 .code:n =
            {
              \keys_set:nn { fontspec } { #2 = {##1} } #4
            },
          #1 .default:n = {#3}
        }
    }
  \newhangulfontfeature{Protrusion}{protrusion}{default}
    {\aftergroup\pdfprotrudechars\aftergroup\tw@}
  \newhangulfontfeature{Expansion}{expansion}{default}
    {\aftergroup\pdfadjustspacing\aftergroup\tw@}
  %% default font features
  \cs_set_eq:NN \defaulthangulfontfeatures    \defaultfontfeatures
  \cs_set_eq:NN \defaulthanjafontfeatures     \defaultfontfeatures
  \cs_set_eq:NN \defaultfallbackfontfeatures  \defaultfontfeatures
  \defaultfontfeatures
    [
      \serifhangul@font, \serifhanja@font, \seriffallback@font,
      \sanshangul@font,  \sanshanja@font,  \sansfallback@font,
    ]
    {Ligatures=TeX}
  %% macros for Korean fonts
  \DeclareDocumentCommand \setmainhangulfont { O{} m O{} }
    {
      \newfontfamily \serifhangul@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \rmdefault
        {
          \cs_set_eq:NN \hangul@font \serifhangul@font
          \luatexko@hangul@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \setmainhanjafont { O{} m O{} }
    {
      \newfontfamily \serifhanja@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \rmdefault
        {
          \cs_set_eq:NN \hanja@font \serifhanja@font
          \luatexko@hanja@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \setmainfallbackfont { O{} m O{} }
    {
      \newfontfamily \seriffallback@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \rmdefault
        {
          \cs_set_eq:NN \fallback@font \seriffallback@font
          \luatexko@fallback@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \setsanshangulfont { O{} m O{} }
    {
      \newfontfamily \sanshangul@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \sfdefault
        {
          \cs_set_eq:NN \hangul@font \sanshangul@font
          \luatexko@hangul@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \setsanshanjafont { O{} m O{} }
    {
      \newfontfamily \sanshanja@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \sfdefault
        {
          \cs_set_eq:NN \hanja@font \sanshanja@font
          \luatexko@hanja@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \setsansfallbackfont { O{} m O{} }
    {
      \newfontfamily \sansfallback@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \sfdefault
        {
          \cs_set_eq:NN \fallback@font \sansfallback@font
          \luatexko@fallback@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \setmonohangulfont { O{} m O{} }
    {
      \newfontfamily \monohangul@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \ttdefault
        {
          \cs_set_eq:NN \hangul@font \monohangul@font
          \luatexko@hangul@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \setmonohanjafont { O{} m O{} }
    {
      \newfontfamily \monohanja@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \ttdefault
        {
          \cs_set_eq:NN \hanja@font \monohanja@font
          \luatexko@hanja@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \setmonofallbackfont { O{} m O{} }
    {
      \newfontfamily \monofallback@font [#1,#3] {#2}
      \str_if_eq_x:nnT \familydefault \ttdefault
        {
          \cs_set_eq:NN \fallback@font \monofallback@font
          \luatexko@fallback@selectfont
        }
      \ignorespaces
    }
  \DeclareDocumentCommand \newhangulfontfamily { m O{} m O{} }
    {
      \exp_args:Nc \newfontfamily { luatexkofont@ \token_to_str:N #1 } [#2,#4] {#3}
      \DeclareRobustCommand #1
        {
          \cs_set_eq:Nc \hangul@font { luatexkofont@ \token_to_str:N #1 }
          \luatexko@hangul@selectfont
        }
    }
  \DeclareDocumentCommand \newhanjafontfamily { m O{} m O{} }
    {
      \exp_args:Nc \newfontfamily { luatexkofont@ \token_to_str:N #1 } [#2,#4] {#3}
      \DeclareRobustCommand #1
        {
          \cs_set_eq:Nc \hanja@font { luatexkofont@ \token_to_str:N #1 }
          \luatexko@hanja@selectfont
        }
    }
  \DeclareDocumentCommand \newfallbackfontfamily { m O{} m O{} }
    {
      \exp_args:Nc \newfontfamily { luatexkofont@ \token_to_str:N #1 } [#2,#4] {#3}
      \DeclareRobustCommand #1
        {
          \cs_set_eq:Nc \fallback@font { luatexkofont@ \token_to_str:N #1 }
          \luatexko@fallback@selectfont
        }
    }
  \DeclareDocumentCommand \newhangulfontface { m O{} m }
    {
      \exp_args:Nc \newfontface { luatexkofont@ \token_to_str:N #1 } [#2] {#3}
      \DeclareRobustCommand #1
        {
          \cs_set_eq:Nc \hangul@font { luatexkofont@ \token_to_str:N #1 }
          \luatexko@hangul@selectfont
        }
    }
  \DeclareDocumentCommand \newhanjafontface { m O{} m O{} }
    {
      \exp_args:Nc \newfontface { luatexkofont@ \token_to_str:N #1 } [#2,#4] {#3}
      \DeclareRobustCommand #1
        {
          \cs_set_eq:Nc \hanja@font { luatexkofont@ \token_to_str:N #1 }
          \luatexko@hanja@selectfont
        }
    }
  \DeclareDocumentCommand \newfallbackfontface { m O{} m O{} }
    {
      \exp_args:Nc \newfontface { luatexkofont@ \token_to_str:N #1 } [#2,#4] {#3}
      \DeclareRobustCommand #1
        {
          \cs_set_eq:Nc \fallback@font { luatexkofont@ \token_to_str:N #1 }
          \luatexko@fallback@selectfont
        }
    }
  \DeclareDocumentCommand \hangulfontspec { O{} m O{} }
    {
      \newfontfamily \hangul@font [#1,#3] {#2}
      \luatexko@hangul@selectfont
      \ignorespaces
    }
  \cs_set_eq:NN \adhochangulfont \hangulfontspec
  \DeclareDocumentCommand \hanjafontspec { O{} m O{} }
    {
      \newfontfamily \hanja@font [#1,#3] {#2}
      \luatexko@hanja@selectfont
      \ignorespaces
    }
  \cs_set_eq:NN \adhochanjafont \hanjafontspec
  \DeclareDocumentCommand \fallbackfontspec { O{} m O{} }
    {
      \newfontfamily \fallback@font [#1,#3] {#2}
      \luatexko@fallback@selectfont
      \ignorespaces
    }
  \cs_set_eq:NN \adhocfallbackfont \fallbackfontspec
  \DeclareDocumentCommand \addhangulfontfeature { m }
    {
      \group_begin:
      \cs_if_exist:NT \hangul@font { \hangul@font }
      \addfontfeature {#1}
      \use:x
        {
          \group_end:
          \exp_not:N \tl_set:Nn \exp_not:N \hangul@font
            {
              \exp_not:N \fontfamily { \f@family } \exp_not:N \selectfont
            }
        }
      \luatexko@hangul@selectfont
      \ignorespaces
    }
  \cs_set_eq:NN \addhangulfontfeatures \addhangulfontfeature
  \DeclareDocumentCommand \addhanjafontfeature { m }
    {
      \group_begin:
      \cs_if_exist:NT \hanja@font { \hanja@font }
      \addfontfeature {#1}
      \use:x
        {
          \group_end:
          \exp_not:N \tl_set:Nn \exp_not:N \hanja@font
            {
              \exp_not:N \fontfamily { \f@family } \exp_not:N \selectfont
            }
        }
      \luatexko@hanja@selectfont
      \ignorespaces
    }
  \cs_set_eq:NN \addhanjafontfeatures \addhanjafontfeature
  \DeclareDocumentCommand \addfallbackfontfeature { m }
    {
      \group_begin:
      \cs_if_exist:NT \fallback@font { \fallback@font }
      \addfontfeature {#1}
      \use:x
        {
          \group_end:
          \exp_not:N \tl_set:Nn \exp_not:N \fallback@font
            {
              \exp_not:N \fontfamily { \f@family } \exp_not:N \selectfont
            }
        }
      \luatexko@fallback@selectfont
      \ignorespaces
    }
  \cs_set_eq:NN \addfallbackfontfeatures \addfallbackfontfeature
  % hangul math font command
  \DeclareDocumentCommand \setmathhangulfont { O{} m O{} }
    {
      \fontspec_set_family:Nnn \luatexko@math@hangul@family {#1,#3} {#2}
    }
  \ExplSyntaxOff
\fi

\RequirePackage{everysel}
\EverySelectfont{%
  \luatexko@hangul@selectfont
  \luatexko@hanja@selectfont
  \luatexko@fallback@selectfont
}

% serif    : finemathattr = 2
% sans     : finemathattr = 1
% tt       : finemathattr = 0
% verbatim : finemathattr = nil
\def\luatexko@serif@fonts{%
  \let\hangul@font\serifhangul@font
  \let\hanja@font\serifhanja@font
  \let\fallback@font\seriffallback@font
  \finemathattr\tw@
}
\def\luatexko@sans@fonts{%
  \let\hangul@font\sanshangul@font
  \let\hanja@font\sanshanja@font
  \let\fallback@font\sansfallback@font
  \finemathattr\@ne
}
\def\luatexko@mono@fonts{%
  \let\hangul@font\monohangul@font
  \let\hanja@font\monohanja@font
  \let\fallback@font\monofallback@font
  \finemathattr\z@
}

\protected\edef\rmfamily{%
  \unexpanded{\luatexko@serif@fonts}\unexpanded\expandafter{\rmfamily}}
\protected\edef\sffamily{%
  \unexpanded{\luatexko@sans@fonts}\unexpanded\expandafter{\sffamily}}
\protected\edef\ttfamily{%
  \unexpanded{\luatexko@mono@fonts}\unexpanded\expandafter{\ttfamily}}
\edef\verbatim@font{\unexpanded\expandafter{\verbatim@font
  \unsetluatexattribute\finemathattr}}

% don't forget to redefine \normalfont
\expandafter\ifx\familydefault\rmdefault
  \protected\edef\normalfont{%
    \unexpanded{\luatexko@serif@fonts}\unexpanded\expandafter{\normalfont}}
  \finemathattr\tw@
\else\expandafter\ifx\familydefault\sfdefault
  \protected\edef\normalfont{%
    \unexpanded{\luatexko@sans@fonts}\unexpanded\expandafter{\normalfont}}
  \finemathattr\@ne
\else
  \protected\edef\normalfont{%
    \unexpanded{\luatexko@mono@fonts}\unexpanded\expandafter{\normalfont}}
  \finemathattr\z@
\fi\fi
\let\reset@font\normalfont

\if@unfonts\if@luatexko@loadfontspec
  \setmainhangulfont[
    InterHangul=-0.04em,
    InterLatinCJK=0.125em,
    PunctRaise=-0.125em,
    Ligatures=TeX,
    SlantedFont=*, SlantedFeatures={FakeSlant=0.2},
    ItalicFont=*,  ItalicFeatures={FakeSlant=0.2},
    BoldSlantedFont=*Bold, BoldSlantedFeatures={FakeSlant=0.2},
    BoldItalicFont=*Bold,  BoldItalicFeatures={FakeSlant=0.2},
  ]{UnBatang}
  \setsanshangulfont[
    InterLatinCJK=0.125em,
    PunctRaise=-0.125em,
    Ligatures=TeX,
    SlantedFont=*, SlantedFeatures={FakeSlant=0.2},
    ItalicFont=*,  ItalicFeatures={FakeSlant=0.2},
    BoldSlantedFont=*Bold, BoldSlantedFeatures={FakeSlant=0.2},
    BoldItalicFont=*Bold,  BoldItalicFeatures={FakeSlant=0.2},
  ]{UnDotum}
  \setmonohangulfont[
    SlantedFont=*, SlantedFeatures={FakeSlant=0.2},
    ItalicFont=*,  ItalicFeatures={FakeSlant=0.2},
    BoldSlantedFont=*Bold, BoldSlantedFeatures={FakeSlant=0.2},
    BoldItalicFont=*Bold,  BoldItalicFeatures={FakeSlant=0.2},
  ]{UnDotum}
\fi\fi

\AtBeginDocument{
\unless\ifdefined\serifhangul@font\let\serifhangul@font\sanshangul@font\fi
\unless\ifdefined\sanshangul@font \let\sanshangul@font\serifhangul@font\fi
\unless\ifdefined\monohangul@font \let\monohangul@font\sanshangul@font \fi
\unless\ifdefined\serifhangul@font\unless\ifdefined\sanshangul@font
  \hangulpunctuations=0 % nanumtype1
\fi\fi
% for hyperref
\@ifpackageloaded{hyperref}{\pdfstringdefDisableCommands{%
  \let\dotemph\@firstofone
  \let\ruby\@firstoftwo
  \let\uline\@firstofone
  \let\sout\@firstofone
  \let\uuline\@firstofone
  \let\xout\@firstofone
  \let\uwave\@firstofone
  \let\dashuline\@firstofone
  \let\dotuline\@firstofone
  \let\actualtext\@firstofone
  \let\inhibitglue\empty
  \def\는{는}%
  \def\은{은}%
  \def\을{을}%
  \def\를{를}%
  \def\와{와}%
  \def\과{과}%
  \def\가{가}%
  \def\이{이}%
  \def\라{라}%
  \def\으{으}%
  \def\로{로}%
  }}{}
}

\if@luatexko@loadfontspec
  %% hangul in math --- latex
  \AtBeginDocument{
    \begingroup
      \unless\ifdefined\luatexko@math@hangul@family
        \ifdefined\hangul@font \hangul@font \fi
        \xdef\luatexko@math@hangul@family{\f@family}%
      \fi
    \endgroup
    \DeclareSymbolFont{mathhangul}%
      \zf@enc\luatexko@math@hangul@family\mddefault\updefault
    \ifcsname\zf@enc/\luatexko@math@hangul@family/\bfdefault/\updefault\endcsname
      \SetSymbolFont{mathhangul}{bold}%
        \zf@enc\luatexko@math@hangul@family\bfdefault\updefault
    \fi
    \setmathhangulblock{AC00}{D7A3}
  }
\fi

\RequirePackage{kolabels-utf}
\if@hangul
  \AtBeginDocument{\RequirePackage{konames-utf}}
  \linespread{1.3888}
  \footnotesep=1.3888\footnotesep
  \skip\footins=1.3888\skip\footins plus6pt minus3pt
  \hangulpunctuations=1
  \frenchspacing
\fi

\endinput
