% xetexko-font.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.

\ProvidesPackage{xetexko-font}[2014/08/04 v2.9 LaTeX Font commands for XeTeX-ko]

\let\xetexko@f@size\f@size
\RequirePackage{fontspec}[2014/05/25]
\let\f@size\xetexko@f@size

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% for old hangul rendering
\newfontfeature{YetHangul}{script=hang}

\ExplSyntaxOn
%%%%%%%%%%%%%%%%%%%%%
% hangul font options
\tl_new:N \l_xtxko_font_opts_tl
\tl_new:N \l_xtxko_font_opts_init_tl
\tl_set:Nn \l_xtxko_font_opts_init_tl
  {
    \tl_clear:N \xetexkointerhchar
    \tl_clear:N \xetexkolowerexclamation
    \tl_clear:N \xetexkolowerperiod
    \tl_clear:N \xetexkolowercomma
    \tl_clear:N \xetexkolowerquestion
    \tl_clear:N \xetexkopostexclamationkern
    \tl_clear:N \xetexkopostperiodkern
    \tl_clear:N \xetexkopostcommakern
    \tl_clear:N \xetexkopostquestionkern
    \tl_clear:N \xetexkopreexclamationkern
    \tl_clear:N \xetexkopreperiodkern
    \tl_clear:N \xetexkoprecommakern
    \tl_clear:N \xetexkoprequestionkern
    \tl_clear:N \xetexkoquoteraise
    \tl_clear:N \xetexkoquotewidth
    \tl_clear:N \xetexkocharraise
    \tl_set:Nn  \xetexkohu            {0.06em}
    \tl_set:Nn  \xetexkopostmathskip  {\dim_eval:n {\xetexkohu*2}}
  }
\cs_new:Nn \__xtxko_add_font_opt:Nn
  {
    \tl_put_right:Nn \l_xtxko_font_opts_tl { \tl_set:Nn #1 {#2} }
  }
\keys_define:nn { xtxko-font }
  {
    hu .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkohu                  {#1} } ,
    interhchar .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkointerhchar          {#1} } ,
    lowerexclamation .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkolowerexclamation    {#1} } ,
    lowerperiod .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkolowerperiod         {#1} } ,
    lowercomma .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkolowercomma          {#1} } ,
    lowerquestion .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkolowerquestion       {#1} } ,
    postexclamationkern .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkopostexclamationkern {#1} } ,
    postmathskip .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkopostmathskip        {#1} } ,
    postperiodkern .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkopostperiodkern      {#1} } ,
    postcommakern .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkopostcommakern       {#1} } ,
    postquestionkern .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkopostquestionkern    {#1} } ,
    preexclamationkern .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkopreexclamationkern  {#1} } ,
    preperiodkern .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkopreperiodkern       {#1} } ,
    precommakern .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkoprecommakern        {#1} } ,
    prequestionkern .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkoprequestionkern     {#1} } ,
    quoteraise .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkoquoteraise          {#1} } ,
    quotewidth .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkoquotewidth          {#1} } ,
    charraise .code:n =
      { \__xtxko_add_font_opt:Nn \xetexkocharraise           {#1} } ,
    InterLatinCJK .meta:n = { hu          = {#1} } ,
    InterHangul   .meta:n = { interhchar  = {#1} } ,
    PunctRaise    .meta:n = { lowerperiod = { \dim_eval:n {-#1} } } ,
    CharRaise     .meta:n = { charraise   = {#1} } ,
  }
\tl_new:N \l_xtxko_rem_tl
\tl_set_eq:NN \l_xtxko_hangul_font_opts_init_tl \l_xtxko_font_opts_init_tl
\cs_new:Nn \__xtxko_hangul_assign_font_opts:n
  {
    \tl_set_eq:NN \l_xtxko_font_opts_tl \l_xtxko_hangul_font_opts_init_tl
    \keys_set_known:nxN { xtxko-font } {#1} \l_xtxko_rem_tl
  }
\tl_set_eq:NN \l_xtxko_hanja_font_opts_init_tl  \l_xtxko_font_opts_init_tl
\cs_new:Nn \__xtxko_hanja_assign_font_opts:n
  {
    \tl_set_eq:NN \l_xtxko_font_opts_tl \l_xtxko_hanja_font_opts_init_tl
    \keys_set_known:nxN { xtxko-font } {#1} \l_xtxko_rem_tl
  }
\cs_new:Nn \__xtxko_pass_args:nnn
  {
    \IfNoValueTF {#2}
      { \__xtxko_post_args:w {#1} {#3} }
      { #1 {#2} {#3} }
  }
\NewDocumentCommand \__xtxko_post_args:w { m m O{} }
  { #1 {#3} {#2} }
%%%%%%%%%%%%%%%%%%%%
% user font commands
\DeclareDocumentCommand \setmainhangulfont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_setmainhangulfont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_setmainhangulfont:nn
  {
    \__xtxko_hangul_assign_font_opts:n {#1}
    \newfontfamily \kormainfont [\l_xtxko_rem_tl] {#2}
    \tl_put_right:NV \kormainfont \l_xtxko_font_opts_tl
    \ignorespaces
  }
\DeclareDocumentCommand \setsanshangulfont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_setsanshangulfont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_setsanshangulfont:nn
  {
    \__xtxko_hangul_assign_font_opts:n {#1}
    \newfontfamily \korsansfont [\l_xtxko_rem_tl] {#2}
    \tl_put_right:NV \korsansfont \l_xtxko_font_opts_tl
    \ignorespaces
  }
\DeclareDocumentCommand \setmonohangulfont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_setmonohangulfont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_setmonohangulfont:nn
  {
    \newfontfamily \kormonofont [#1] {#2}
    \ignorespaces
  }
\DeclareDocumentCommand \setmainhanjafont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_setmainhanjafont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_setmainhanjafont:nn
  {
    \__xtxko_hanja_assign_font_opts:n {#1}
    \newfontfamily \hnjmainfont [\l_xtxko_rem_tl] {#2}
    \tl_put_right:NV \hnjmainfont \l_xtxko_font_opts_tl
    \ignorespaces
  }
\DeclareDocumentCommand \setsanshanjafont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_setsanshanjafont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_setsanshanjafont:nn
  {
    \__xtxko_hanja_assign_font_opts:n {#1}
    \newfontfamily \hnjsansfont [\l_xtxko_rem_tl] {#2}
    \tl_put_right:NV \hnjsansfont \l_xtxko_font_opts_tl
    \ignorespaces
  }
\DeclareDocumentCommand \setmonohanjafont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_setmonohanjafont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_setmonohanjafont:nn
  {
    \newfontfamily \hnjmonofont [#1] {#2}
    \ignorespaces
  }
%% {hangul,hanja}fontspec = adhoc{hangul,hanja}font
\DeclareDocumentCommand \adhochangulfont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_adhochangulfont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_adhochangulfont:nn
  {
    \__xtxko_hangul_assign_font_opts:n {#1}
    \newfontfamily \XK@adhoc@hangul@font [\l_xtxko_rem_tl] {#2}
    \tl_put_right:NV \XK@adhoc@hangul@font \l_xtxko_font_opts_tl
    \tl_set:Nn \XKhangulfont
      {
        \XK@storeltnfont
        \tl_set_eq:NN \XKcurrentfont \c_one
        \XK@adhoc@hangul@font
        \XK@storecjkfont
      }
    \ignorespaces
  }
\cs_set_eq:NN \hangulfontspec \adhochangulfont
\DeclareDocumentCommand \adhochanjafont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_adhochanjafont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_adhochanjafont:nn
  {
    \__xtxko_hanja_assign_font_opts:n {#1}
    \newfontfamily \XK@adhoc@hanja@font [\l_xtxko_rem_tl] {#2}
    \tl_put_right:NV \XK@adhoc@hanja@font \l_xtxko_font_opts_tl
    \tl_set:Nn \XKhanjafont
      {
        \XK@storeltnfont
        \tl_set_eq:NN \XKcurrentfont \c_two
        \XK@adhoc@hanja@font
        \XK@storecjkfont
      }
    \ignorespaces
  }
\cs_set_eq:NN \hanjafontspec \adhochanjafont
%% new{hangul,hanja}font{family,face}
\DeclareDocumentCommand \newhangulfontfamily { m o m }
  { \__xtxko_pass_args:nnn { \__xtxko_newhangulfontfamily:Nnn #1 } {#2} {#3} }
\cs_new:Nn \__xtxko_newhangulfontfamily:Nnn
  {
    \__xtxko_hangul_assign_font_opts:n {#2}
    \exp_args:Nc \newfontfamily { XK@newfont@family \token_to_str:N #1 }
      [\l_xtxko_rem_tl] {#3}
    \tl_put_right:cV { XK@newfont@family \token_to_str:N #1 }
      \l_xtxko_font_opts_tl
    \DeclareRobustCommand #1
      {
        \tl_set:Nn \XKhangulfont
          {
            \XK@storeltnfont
            \tl_set_eq:NN \XKcurrentfont \c_one
            \use:c { XK@newfont@family \token_to_str:N #1 }
            \XK@storecjkfont
          }
      }
  }
\DeclareDocumentCommand \newhanjafontfamily { m o m }
  { \__xtxko_pass_args:nnn { \__xtxko_newhanjafontfamily:Nnn #1 } {#2} {#3} }
\cs_new:Nn \__xtxko_newhanjafontfamily:Nnn
  {
    \__xtxko_hanja_assign_font_opts:n {#2}
    \exp_args:Nc \newfontfamily { XK@newfont@family \token_to_str:N #1 }
      [\l_xtxko_rem_tl] {#3}
    \tl_put_right:cV { XK@newfont@family \token_to_str:N #1 }
      \l_xtxko_font_opts_tl
    \DeclareRobustCommand #1
      {
        \tl_set:Nn \XKhanjafont
          {
            \XK@storeltnfont
            \tl_set_eq:NN \XKcurrentfont \c_two
            \use:c { XK@newfont@family \token_to_str:N #1 }
            \XK@storecjkfont
          }
      }
  }
\DeclareDocumentCommand \newhangulfontface { m o m }
  { \__xtxko_pass_args:nnn { \__xtxko_newhangulfontface:Nnn #1 } {#2} {#3} }
\cs_new:Nn \__xtxko_newhangulfontface:Nnn
  {
    \__xtxko_hangul_assign_font_opts:n {#2}
    \exp_args:Nc \newfontface { XK@newfont@family \token_to_str:N #1 }
      [\l_xtxko_rem_tl] {#3}
    \tl_put_right:cV { XK@newfont@family \token_to_str:N #1 }
      \l_xtxko_font_opts_tl
    \DeclareRobustCommand #1
      {
        \tl_set:Nn \XKhangulfont
          {
            \XK@storeltnfont
            \tl_set_eq:NN \XKcurrentfont \c_one
            \use:c { XK@newfont@family \token_to_str:N #1 }
            \XK@storecjkfont
          }
      }
  }
\DeclareDocumentCommand \newhanjafontface { m o m }
  { \__xtxko_pass_args:nnn { \__xtxko_newhanjafontface:Nnn #1 } {#2} {#3} }
\cs_new:Nn \__xtxko_newhanjafontface:Nnn
  {
    \__xtxko_hanja_assign_font_opts:n {#2}
    \exp_args:Nc \newfontface { XK@newfont@family \token_to_str:N #1 }
      [\l_xtxko_rem_tl] {#3}
    \tl_put_right:cV { XK@newfont@family \token_to_str:N #1 }
      \l_xtxko_font_opts_tl
    \DeclareRobustCommand #1
      {
        \tl_set:Nn \XKhanjafont
          {
            \XK@storeltnfont
            \tl_set_eq:NN \XKcurrentfont \c_two
            \use:c { XK@newfont@family \token_to_str:N #1 }
            \XK@storecjkfont
          }
      }
  }
%% add{hangul,hanja}fontfeature[s]
\DeclareDocumentCommand \addhangulfontfeature { m }
  {
    \group_begin:
    \XKhangulfont
    \tl_clear:N \l_xtxko_font_opts_tl
    \keys_set_known:nxN { xtxko-font } {#1} \l_xtxko_rem_tl
    \addfontfeature {\l_xtxko_rem_tl}
    \use:x
      {
        \group_end:
        \exp_not:N \tl_set:Nn \exp_not:N \XKhangulfont
          {
            \exp_not:V \XKhangulfont
            \exp_not:N \fontfamily { \f@family } \exp_not:N \selectfont
            \exp_not:V \l_xtxko_font_opts_tl
            \exp_not:N \XK@storecjkfont
          }
      }
    \ignorespaces
  }
\cs_set_eq:NN \addhangulfontfeatures \addhangulfontfeature
\DeclareDocumentCommand \addhanjafontfeature { m }
  {
    \group_begin:
    \XKhanjafont
    \tl_clear:N \l_xtxko_font_opts_tl
    \keys_set_known:nxN { xtxko-font } {#1} \l_xtxko_rem_tl
    \addfontfeature {\l_xtxko_rem_tl}
    \use:x
      {
        \group_end:
        \exp_not:N \tl_set:Nn \exp_not:N \XKhanjafont
          {
            \exp_not:V \XKhanjafont
            \exp_not:N \fontfamily { \f@family } \exp_not:N \selectfont
            \exp_not:V \l_xtxko_font_opts_tl
            \exp_not:N \XK@storecjkfont
          }
      }
    \ignorespaces
  }
\cs_set_eq:NN \addhanjafontfeatures \addhanjafontfeature
%% default font features
\defaultfontfeatures
  [
    \kormainfont, \korsansfont, \hnjmainfont, \hnjsansfont,
  ]
  {Ligatures=TeX}
\DeclareDocumentCommand \defaulthangulfontfeatures { t+ o m } % o is ignored
  {
    \IfBooleanTF {#1}
      {
        \tl_set_eq:NN \l_xtxko_font_opts_tl \l_xtxko_hangul_font_opts_init_tl
      }
      {
        \tl_set_eq:NN \l_xtxko_font_opts_tl \l_xtxko_font_opts_init_tl
      }
    \keys_set_known:nxN { xtxko-font } {#3} \l_xtxko_rem_tl % rem_tl ignored
    \tl_set_eq:NN \l_xtxko_hangul_font_opts_init_tl \l_xtxko_font_opts_tl
  }
\DeclareDocumentCommand \defaulthanjafontfeatures { t+ o m } % o is ignored
  {
    \IfBooleanTF {#1}
      {
        \tl_set_eq:NN \l_xtxko_font_opts_tl \l_xtxko_hanja_font_opts_init_tl
      }
      {
        \tl_set_eq:NN \l_xtxko_font_opts_tl \l_xtxko_font_opts_init_tl
      }
    \keys_set_known:nxN { xtxko-font } {#3} \l_xtxko_rem_tl % rem_tl ignored
    \tl_set_eq:NN \l_xtxko_hanja_font_opts_init_tl \l_xtxko_font_opts_tl
  }
%% math hangul font
\DeclareDocumentCommand \setmathhangulfont { o m }
  { \__xtxko_pass_args:nnn \__xtxko_setmathhangulfont:nn {#1} {#2} }
\cs_new:Nn \__xtxko_setmathhangulfont:nn
  {
    \fontspec_set_family:Nnn \xetexko@math@hangul@family {#1} {#2}
  }
%%%%%%%%%%%%%%%%%%%%
%% xetexkofontregime
\keys_define:nn { xtxko-char }
  {
    alphs       .code:n = \use:c { #1alphs } ,
    nums        .code:n = \use:c { #1nums } ,
    parens      .code:n = \use:c { #1parens } ,
    quotes      .code:n = \use:c { #1quotes } ,
    puncts      .code:n = \use:c { #1puncts } ,
    colons      .code:n = \use:c { #1colons } ,
    hyphens     .code:n = \use:c { #1hyphens } ,
    cjksymbols  .code:n = \use:c { #1cjksymbols } ,
  }
\DeclareDocumentCommand \xetexkofontregime { o m }
  { \__xtxko_pass_args:nnn \__xtxko_xetexkofontregime:nn {#1} {#2} }
\cs_new:Nn \__xtxko_xetexkofontregime:nn
  {
    \use:c { #2marks }
    \keys_set:nn { xtxko-char } { #1 }
  }
\ExplSyntaxOff

%%%%%%%%%%%%%%%%%%%%%%%%%%
% set unfonts if [unfonts]
\if@unfonts
  %% 한글폰트 지정하기 :
  \setmainhangulfont
    [ ExternalLocation,
      Mapping=tex-text,
      BoldFont=UnBatangBold,
      ItalicFont=UnBatang, ItalicFeatures={FakeSlant=0.17},
      SlantedFont=UnBatang, SlantedFeatures={FakeSlant=0.17},
      BoldItalicFont=UnBatangBold, BoldItalicFeatures={FakeSlant=0.17},
      BoldSlantedFont=UnBatangBold, BoldSlantedFeatures={FakeSlant=0.17},
      interhchar=-0.045em,
      lowerperiod=0.1em ]
    {UnBatang}
  \setsanshangulfont
    [ ExternalLocation,
      Mapping=tex-text,
      BoldFont=UnDotumBold,
      ItalicFont=UnDotum, ItalicFeatures={FakeSlant=0.17},
      SlantedFont=UnDotum, SlantedFeatures={FakeSlant=0.17},
      BoldItalicFont=UnDotumBold, BoldItalicFeatures={FakeSlant=0.17},
      BoldSlantedFont=UnDotumBold, BoldSlantedFeatures={FakeSlant=0.17},
      lowerperiod=0.1em ]
    {UnDotum}
  \setmonohangulfont
    [ ExternalLocation,
      Scale=1.05,
      BoldFont=UnTaza, BoldFeatures={FakeBold=2},
      ItalicFont=UnTaza, ItalicFeatures={FakeSlant=0.17},
      SlantedFont=UnTaza, SlantedFeatures={FakeSlant=0.17},
      BoldItalicFont=UnTaza, BoldItalicFeatures={FakeBold=2,FakeSlant=0.17},
      BoldSlantedFont=UnTaza, BoldSlantedFeatures={FakeBold=2,FakeSlant=0.17}]
    {UnTaza}
  \let\hnjmonofont\korsansfont
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%
% font switching for latex
\def\XKstoreprevfont{%
  \ifx\f@family\XKcjkprevfont\let\f@family\XKltnprevfont\fi
}
\def\XK@storeltnfont{%
  \ifx\f@family\XKcjkprevfont\else\let\XKltnprevfont\f@family\fi
}
\def\XK@storecjkfont{%
  \let\XKcjkprevfont\f@family
  \edef\XKplaincjkprevfont{\the\font}%
}
\edef\XKlatinfont{\unexpanded\expandafter{\XKstoreprevfont
    \chardef\XKcurrentfont\z@
    \edef\XK@plain@curr@font{\the\font}%
    \ifx\XK@plain@curr@font\XKplaincjkprevfont
      \selectfont
    \fi
}}

\def\XK@rm@korean@font{%
  \def\XKhangulfont{%
    \XK@storeltnfont
    \chardef\XKcurrentfont\@ne\kormainfont
    \XK@storecjkfont
  }%
  \def\XKhanjafont {%
    \XK@storeltnfont
    \chardef\XKcurrentfont\tw@\hnjmainfont
    \XK@storecjkfont
  }%
}
\def\XK@sf@korean@font{%
  \def\XKhangulfont{%
    \XK@storeltnfont
    \chardef\XKcurrentfont\@ne\korsansfont
    \XK@storecjkfont
  }%
  \def\XKhanjafont {%
    \XK@storeltnfont
    \chardef\XKcurrentfont\tw@\hnjsansfont
    \XK@storecjkfont
  }%
}
\def\XK@tt@korean@font{%
  \def\XKhangulfont{%
    \XK@storeltnfont
    \chardef\XKcurrentfont\@ne\kormonofont
    \XK@storecjkfont
  }%
  \def\XKhanjafont {%
    \XK@storeltnfont
    \chardef\XKcurrentfont\tw@\hnjmonofont
    \XK@storecjkfont
  }%
}

\edef\rmfamily{\unexpanded\expandafter{\rmfamily\XK@rm@korean@font}}
\edef\sffamily{\unexpanded\expandafter{\sffamily\XK@sf@korean@font}}
\edef\ttfamily{\unexpanded\expandafter{\ttfamily
    \XK@tt@korean@font\disablehangulspacing
}}
\edef\verbatim@font{\unexpanded\expandafter{\verbatim@font
    \disablehangulspacingandlinebreak
    \let\XKstoreprevfont\XKlatinfont
}}

\expandafter\ifx\familydefault\rmdefault
  \XK@rm@korean@font
  \protected\edef\normalfont{\unexpanded\expandafter{\normalfont
    \XK@rm@korean@font}}
\else \expandafter\ifx\familydefault\sfdefault
  \XK@sf@korean@font
  \protected\edef\normalfont{\unexpanded\expandafter{\normalfont
    \XK@sf@korean@font}}
\else
  \XK@tt@korean@font
  \protected\edef\normalfont{\unexpanded\expandafter{\normalfont
    \XK@tt@korean@font}}
\fi\fi
\let\reset@font\normalfont

% for default font loading
\def\XKiffontisaccessible#1#2#3{%
  \begingroup
  \suppressfontnotfounderror\@ne
  \font\x="#1" at 10pt
  \expandafter
  \endgroup
  \ifx\x\nullfont #3\else #2\fi
}
\def\XKiffonthasunichar#1#2#3#4{%
  \begingroup #1\expandafter\endgroup
  \iffontchar\font"#2\relax #3\else #4\fi
}

\AtBeginDocument{%
  %%%%%%%%%%%%%%%%%%%%%%%%%%
  % default main hangul: nanummyeongjo, batang if absent
  \ifdefined\kormainfont\else
    \XKiffonthasunichar{\fontfamily\rmdefault\selectfont}{AC00}
      {\let\kormainfont\relax}
      {\XKiffontisaccessible{NanumMyeongjoExtraBold}
        {\setmainhangulfont{NanumMyeongjo}[BoldFont=*ExtraBold]}
        {\XKiffontisaccessible{NanumMyeongjo}
          {\setmainhangulfont{NanumMyeongjo}}
          {\XKiffontisaccessible{Batang}
            {\setmainhangulfont{Batang}[AutoFakeBold]}
            {\let\kormainfont\relax}%
          }%
        }%
      }%
  \fi
  % default main hanja: nanumgothic, batang if absent
  \ifdefined\hnjmainfont\else
    \XKiffonthasunichar{\kormainfont}{4E00}
      {\let\hnjmainfont\kormainfont}
      {\XKiffontisaccessible{NanumGothic}
        {\setmainhanjafont{NanumGothic}}
        {\XKiffontisaccessible{Batang}
          {\setmainhanjafont{Batang}[AutoFakeBold]}
          {\let\hnjmainfont\kormainfont}%
        }%
      }%
  \fi
  % default sans hangul: nanumgothic, dotum if absent
  \ifdefined\korsansfont\else
    \XKiffonthasunichar{\fontfamily\sfdefault\selectfont}{AC00}
      {\let\korsansfont\relax}
      {\XKiffontisaccessible{NanumGothic}
        {\setsanshangulfont{NanumGothic}}
        {\XKiffontisaccessible{Dotum}
          {\setsanshangulfont{Dotum}[AutoFakeBold]}
          {\let\korsansfont\relax}%
        }%
      }%
  \fi
  % default sans hanja: nanumgothic, dotum if absent
  \ifdefined\hnjsansfont\else
    \XKiffonthasunichar{\korsansfont}{4E00}
      {\let\hnjsansfont\korsansfont}
      {\XKiffontisaccessible{NanumGothic}
        {\setsanshanjafont{NanumGothic}}
        {\XKiffontisaccessible{Dotum}
          {\setsanshanjafont{Dotum}[AutoFakeBold]}
          {\let\hnjsansfont\korsansfont}%
        }%
      }%
  \fi
  \ifdefined\kormonofont\else \let\kormonofont\korsansfont \fi
  \ifdefined\hnjmonofont\else \let\hnjmonofont\kormonofont \fi
  %%%%%%%%%%%%%%%%%%%%%
  % hangul in math mode
  \begingroup
    \ifx\xetexko@math@hangul@family\undefined
      \edef\@tempb{\sfdefault}\edef\@tempc{\ttdefault}%
      \ifx\f@family\@tempb \korsansfont \else
      \ifx\f@family\@tempc \kormonofont \else
      \kormainfont \fi\fi
      \xdef\xetexko@math@hangul@family{\f@family}\fi
  \endgroup
  \DeclareSymbolFont{mathhangul}\zf@enc\xetexko@math@hangul@family
    \mddefault\updefault
  \ifcsname \zf@enc/\xetexko@math@hangul@family/\bfdefault/\updefault\endcsname
    \SetSymbolFont{mathhangul}{bold}\zf@enc\xetexko@math@hangul@family
      \bfdefault\updefault
  \fi
  \setmathhangulblock{AC00}{D7A3}
}

%%%%%%%%%%%%%%%%%%%%%%%
% under [hangul] option
\if@hangul
  \linespread{1.3888}
  \footnotesep=1.3888\footnotesep
  \skip\footins=1.3888\skip\footins plus6pt minus3pt
  % frenchspacing is default
  \frenchspacing
\fi

\endinput
