%%% This is file `dhucs.sty`
%%
%% (C) Copyright 2007 Koaunghi Un <koaunghi@kornet.net>
%%                    Dohyun Kim  <nomos@ktug.or.kr>
%%                    Kangsoo Kim <karnes@ktug.or.kr>
%%
%% 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.
%%
%% Read LICENSE included in the package.
%%
%% --------------------------------------------------------
%%
%% changelog:
%% 2008/03/15  4.1.2   redefine hyphendemerits only under finemath
%% 2008/01/30  4.1.1   fix a bug of heading mark
%% 2008/01/29  4.1.0   write raw chars in aux, toc, idx ...
%% 2007/12/28  4.0.14  compatible with footmisc.sty (reported by 서상현)
%% 2007/12/01  4.0.13  spacefactor=1000 between hangul and `)'
%% 2007/10/30  4.0.12  fix on \dhucs@next == \ifx
%% 2007/10/29  4.0.11  nobreak after \nobreak in \cjksymbolskip
%% 2007/10/25  4.0.10  simplify code related to \dhucs@next
%% 2007/10/07  4.0.9   bug fix related to \dhucs@next == \fi
%% 2007/07/18  4.0.8   part of ko.TeX
%%                     PUA is now hangul class 2 (= hanja)
%% 2007/06/30  4.0.7   skip instead of kern after cjksymbol
%% 2007/06/26  4.0.6   option strictcharcheck introduced
%% 2007/06/26  4.0.5   redefine U+2018, U+2019, U+201C, U+201D
%% 2007/06/25  4.0.4   fix bugs introduced by previous minor upgrade
%% 2007/06/22  4.0.3   lowering multiple punctuations
%% 2007/06/16  4.0.2   support ulem.sty
%% 2007/06/14  4.0.1   \hu was too normal a CS. use \dhucs@hu intead.
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{dhucs}
  [2008/03/15 v4.1.2  typesetting UTF-8 Korean documents]

\begingroup
\@ifundefined{eTeXversion}
  {\PackageError{kotex}
    {Not running under e-TeX}
    {This package requires e-TeX.}%
   \aftergroup\endinput}
  {}
\endgroup

%% french spacing is default
\frenchspacing

%% strictcharcheck is option
\let\dhucs@check@if@char@exists\relax

%% Options. hangul, nonfrench, nojosa, hanja, finemath.
\newif\if@nonfrench\@nonfrenchfalse
\DeclareOption{nonfrench}{\@nonfrenchtrue\nonfrenchspacing}
\newif\if@hangul\@hangulfalse
\DeclareOption{hangul}{\@hangultrue}
\newif\if@nojosa\@nojosafalse
\DeclareOption{nojosa}{\@nojosatrue}
\newif\if@hanja\@hanjafalse
\DeclareOption{hanja}{\@hanjatrue\@hangultrue}
\newif\if@dhucsfinemath\@dhucsfinemathfalse
\DeclareOption{finemath}{\@dhucsfinemathtrue}
\DeclareOption{strictcharcheck}{%
  \newcommand*\dhucs@check@if@char@exists{%
    \unless\iffontchar\font@name\h@ngulch@r
      \PackageError{kotex}{Character \number\h@ngulch@r\space
        doesn't exist in font \fontname\font@name}{}%
    \fi}}
\ProcessOptions

%% we use latex's standard utf8.def
\RequirePackage[utf8]{inputenc}

%% modified from utf8.def
\def\UTFviii@two@octets#1#2{%
  \ifx\protect\noexpand
    \string#1\string#2%
  \else
    \ifcsname u8:\string#1\string#2\endcsname
      \expandafter\expandafter\expandafter\csname
        u8:\string#1\string#2%
      \expandafter\expandafter\expandafter\endcsname
    \else
      \expandafter\expandafter\expandafter\unihangul@two@octets
      \expandafter\expandafter\expandafter#1%
      \expandafter\expandafter\expandafter#2%
    \fi
  \fi
}
\def\unihangul@two@octets#1#2{%
    \expandafter\unihangulchar\expandafter{%
      \number\numexpr
	(`#1 - 192) * 64 +
	(`#2 - 128) \relax}}
\def\UTFviii@three@octets#1#2#3{%
  \ifx\protect\noexpand
    \string#1\string#2\string#3%
  \else
    \ifcsname u8:\string#1\string#2\string#3\endcsname
      \expandafter\expandafter\expandafter\csname
        u8:\string#1\string#2\string#3%
      \expandafter\expandafter\expandafter\endcsname
    \else
      \expandafter\expandafter\expandafter\unihangul@three@octets
      \expandafter\expandafter\expandafter#1%
      \expandafter\expandafter\expandafter#2%
      \expandafter\expandafter\expandafter#3%
    \fi
  \fi 
}
\def\unihangul@three@octets#1#2#3{%
    \expandafter\unihangulchar\expandafter{%
      \number\numexpr
	(`#1 - 224) * 4096 +
	(`#2 - 128) * 64 +
	(`#3 - 128) \relax}}
\def\UTFviii@four@octets#1#2#3#4{%
  \ifx\protect\noexpand
    \string#1\string#2\string#3\string#4%
  \else
    \ifcsname u8:\string#1\string#2\string#3\string#4\endcsname
      \expandafter\expandafter\expandafter\csname
        u8:\string#1\string#2\string#3\string#4%
      \expandafter\expandafter\expandafter\endcsname
    \else
      \expandafter\expandafter\expandafter\unihangul@four@octets
      \expandafter\expandafter\expandafter#1%
      \expandafter\expandafter\expandafter#2%
      \expandafter\expandafter\expandafter#3%
      \expandafter\expandafter\expandafter#4%
    \fi
  \fi
}
\def\unihangul@four@octets#1#2#3#4{%
    \expandafter\unihangulchar\expandafter{%
      \number\numexpr
	(`#1 - 240) * 262144 +
	(`#2 - 128) * 4096 +
	(`#3 - 128) * 64 +
	(`#4 - 128) \relax}}

\long\def\protected@write#1#2#3{%
  \begingroup
    \def\UTFviii@two@octets##1##2{%
                            \string##1\string##2}%
    \def\UTFviii@three@octets##1##2##3{%
                            \string##1\string##2\string##3}%
    \def\UTFviii@four@octets##1##2##3##4{%
                            \string##1\string##2\string##3\string##4}%
    \let\thepage\relax
    #2%
    \let\protect\@unexpandable@protect
    \edef\reserved@a{\write#1{#3}}%
    \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi
}

% modified from lucenc.def
\DeclareFontEncoding{LUC}{}{}
\DeclareFontSubstitution{LUC}{utbt}{m}{n}
\providecommand*\LUC@@empty[1]{%
  \edef\mandatory@arg{\mandatory@arg\h@ngulpl@ne}%
  \ifx\optional@arg\@empty
    \empty@sfcnt#1%
  \else % suppress redundant warning
    \ifcsname dhucsluc@\curr@fontshape/\optional@arg\endcsname
      \empty@sfcnt\@gobble
    \else
      \empty@sfcnt#1\expandafter
      \gdef\csname dhucsluc@\curr@fontshape/\optional@arg\endcsname{}%
    \fi
  \fi
  \xdef\font@name{\csname \curr@fontshape/\f@size/\h@ngulpl@ne\endcsname}}
\providecommand*\LUC@@sub[1]{% suppress redundant warning
  \ifcsname dhucsluc@\mandatory@arg/\f@size\endcsname
    \sub@sfcnt\@gobble
  \else
    \expandafter\gdef\csname dhucsluc@\mandatory@arg/\f@size\endcsname{}%
    \sub@sfcnt#1%
  \fi}
\DeclareSizeFunction{LUC}{\LUC@@empty\@font@warning}
\DeclareSizeFunction{LUCb}{\LUC@@empty\@font@warning}
\DeclareSizeFunction{sLUC}{\LUC@@empty\@font@info}
\DeclareSizeFunction{sLUCb}{\LUC@@empty\@font@info}
\DeclareSizeFunction{LUCsub}{\LUC@@sub\@font@warning}
\DeclareSizeFunction{LUCssub}{\LUC@@sub\@font@info}

%% subfont plane and char slot
\newcommand*\geth@ngulpl@ne@ndch@r{%
  \count@\numexpr % 1 / 256 = .00390625
    \number\dimexpr.00390625\dimexpr\unih@ngulpoint sp\relax\relax
    \relax
  \chardef\h@ngulch@r % hangul char slot
    \number\numexpr\unih@ngulpoint-\@cclvi*\count@\relax
  \let\h@ngulpl@ne\@empty % hangul subfont plane
  \ifnum\count@>\@cclv
    \ifnum\count@>4095
      \def\h@ngulpl@ne{1}\count@\numexpr\count@-4096\relax
    \else
      \def\h@ngulpl@ne{0}%
    \fi
    \@tempcnta\numexpr % 1 / 256 = .00390625
      \number\dimexpr.00390625\dimexpr\count@ sp\relax\relax
      \relax
    \edef\h@ngulpl@ne{\h@ngulpl@ne\dhucsnumt@hex\@tempcnta}%
    \count@\numexpr\count@-\@cclvi*\@tempcnta\relax
  \fi
  \@tempcnta\numexpr % 1 / 16 = .0625
    \number\dimexpr.0625\dimexpr\count@ sp\relax\relax
    \relax
  \edef\h@ngulpl@ne{\h@ngulpl@ne\dhucsnumt@hex\@tempcnta}%
  \count@\numexpr\count@-\sixt@@n*\@tempcnta\relax
  \edef\h@ngulpl@ne{\h@ngulpl@ne\dhucsnumt@hex\count@}%
}
\newcommand*\dhucsnumt@hex[1]{\ifcase\number#1
  0\or 1\or 2\or 3\or 4\or 5\or 6\or 7\or
  8\or 9\or a\or b\or c\or d\or e\or f\else \dhucserr@r\fi
}

%% redefinition of \pickup@font (seems to be needed for microtype.sty)
\edef\pickup@font{%
  \unexpanded{%
    \ifcsname h@ngulpl@ne\endcsname
      \ifcsname \curr@fontshape/\f@size/\h@ngulpl@ne\endcsname
        \xdef\font@name{\csname \curr@fontshape/\f@size/\h@ngulpl@ne\endcsname}%
      \else  
        \dhucs@define@newfont 
      \fi
    \else}%
      \unexpanded\expandafter{\pickup@font
    \fi}}
\let\dhucs@define@newfont\define@newfont


%% hangul font specific dimens
\newcommand*\dhucs@hu{}
\newcommand*\dhucs@interhchar{}
\newcommand*\dhucs@fullstoplower{}
\newcommand*\dhucs@questionlower{}
\newcommand*\dhucs@exclamationlower{}

%% dhucs-interword.sty will use this command.
\newcommand*\setInterHangulSkip[1]{\def\dhucs@interhchar{#1}}


%% hangul font selection interface
\protected\def\SetHangulFonts#1#2#3{\def\dhucs@serifhangulfont{#1}%
  \def\dhucs@sanshangulfont{#2}\def\dhucs@monohangulfont{#3}}
\protected\def\SetHanjaFonts#1#2#3{\def\dhucs@serifhanjafont{#1}%
  \def\dhucs@sanshanjafont{#2}\def\dhucs@monohanjafont{#3}}
\protected\def\SetSerifFonts#1#2{%
  \def\dhucs@serifhangulfont{#1}\def\dhucs@serifhanjafont{#2}}
\protected\def\SetSansFonts#1#2{%
  \def\dhucs@sanshangulfont{#1}\def\dhucs@sanshanjafont{#2}}
\protected\def\SetAdhocFonts#1#2{\def\dhucs@serifhangulfont{#1}%
  \def\dhucs@sanshangulfont{#1}\def\dhucs@monohangulfont{#1}%
  \def\dhucs@serifhanjafont{#2}\def\dhucs@sanshanjafont{#2}%
  \def\dhucs@monohanjafont{#2}}

\newcommand*\usehangulfontspec[1]{%
  \IfFileExists{hfontspec.#1}
    {\begingroup
    \endlinechar\m@ne
    \openin\z@=hfontspec.#1\relax \get@hfnt@spec@ \closein\z@
    \endgroup}
    {\PackageWarning{kotex}
      { Cannot find hangul font-spec file hfontspec.#1 \MessageBreak}}}
\def\get@hfnt@spec@{%
    \read\z@ to \@tempb
    \ifx\@tempb\@empty
      \let\next\relax
    \else
      \expandafter\get@hfnt@spec@@\@tempb\@@nil
      \let\next\get@hfnt@spec@
    \fi\next}
\def\get@hfnt@spec@@#1 = #2\@@nil{%
  \expandafter\gdef\csname dhucs@#1\endcsname{#2}}


%% \unih@ngulpoint is unicode code point
\newcount\unih@ngulpoint
%% for autojosa (automatic particle selection)
\newcount\@josa

%% protect \unihangulchar
\protected\def\unihangulchar#1{% 
  \unih@ngulpoint#1\relax
  \futurelet\dhucs@next\unihangulchar@@}
\newcommand*\unihangulchar@@{% check what comes next to hangul?
  % \nexttoh@ngul = 1 : ascii opening parentheses
  % \nexttoh@ngul = 2 : ascii closing parentheses
  % \nexttoh@ngul = 3 : ascii numbers
  % \nexttoh@ngul = 4 : ascii fullstop 
  % \nexttoh@ngul = 5 : ascii question mark
  % \nexttoh@ngul = 6 : ascii exclamation mark
  % \nexttoh@ngul = 0 : all others
  \chardef\nexttoh@ngul\z@
  % \puncnobre@k = 0 : this is not char ``orphan'' case
  % \puncnobre@k = 1 : do not break before char ``orphan''
  \chardef\puncnobre@k\z@
  % avoiding char ``orphan'' operates only in four cases :
  % [hangulchar] [par]
  % [hangulchar] [space] [par]
  % [hangulchar] [.?!] [par]
  % [hangulchar] [.?!] [space] [par]
  \ifx\fi\dhucs@next \expandafter\let\csname dhucs@next\endcsname\relax \fi
  \ifx\ifx\dhucs@next \expandafter\let\csname dhucs@next\endcsname\relax \fi
  \ifx\par\dhucs@next
    \chardef\puncnobre@k\@ne
    \let\unihangul@branch\unihangulchar@@@
  \else
    \ifx\@sptoken\dhucs@next
      \let\unihangul@branch\check@next@to@sptoken
    \else
      \if\noexpand\dhucs@next\relax
      \else
        \check@dhucs@next@ascii456123\@nil
      \fi
      \ifnum\nexttoh@ngul>\thr@@
        \let\unihangul@branch\check@next@to@puncs
      \else
        \let\unihangul@branch\unihangulchar@@@
      \fi 
    \fi
  \fi
  \unihangul@branch
}

\newcommand*\check@next@to@sptoken{%
  \afterassignment\check@next@to@sptoken@\let\dhucs@next= }
\newcommand*\check@next@to@sptoken@{%
  \futurelet\dhucs@nextnext\check@next@to@sptoken@@}
\newcommand*\check@next@to@sptoken@@{%
  \ifx\dhucs@nextnext\par
    \chardef\puncnobre@k\@ne
  \fi
  \unihangulchar@@@\dhucs@next
}

\newcommand*\check@next@to@puncs{%
  \afterassignment\check@next@to@puncs@\let\dhucs@next= }
\newcommand*\check@next@to@puncs@{%
  \futurelet\dhucs@nextnext\check@next@to@puncs@@}
\newcommand*\check@next@to@puncs@@{%
  \ifx\dhucs@nextnext\par
    \chardef\puncnobre@k\@ne
    \expandafter\unihangulchar@@@\expandafter\dhucs@next
  \else
    \ifx\dhucs@nextnext\@sptoken
      \expandafter\expandafter\expandafter\check@next@to@punc@sptoken
    \else
      \expandafter\expandafter\expandafter\unihangulchar@@@
      \expandafter\expandafter\expandafter\dhucs@next
    \fi
  \fi
}

\newcommand*\check@next@to@punc@sptoken{%
  \afterassignment\check@next@to@punc@sptoken@\let\dhucs@nextnext= }
\newcommand*\check@next@to@punc@sptoken@{%
  \futurelet\dhucs@nextnextnext\check@next@to@punc@sptoken@@}
\newcommand*\check@next@to@punc@sptoken@@{%
  \ifx\dhucs@nextnextnext\par
    \chardef\puncnobre@k\@ne
  \fi
  \unihangulchar@@@\dhucs@next\dhucs@nextnext
}

%% see lucenc.dfu
\newcommand*\check@dhucs@next@ascii[1]{%
  \def\@tempa{#1}%
  \ifx#1\@nil
    \let\next\relax
  \else
    \let\next\check@dhucs@next@ascii
    \edef\@tempb{\csname nexttohangul-#1\endcsname}%
    \expandafter\check@dhucs@next@ascii@\@tempb\@nnil
  \fi\next
}
\newcommand*\check@dhucs@next@ascii@[1]{%
  \ifx#1\@nnil\let\next@\relax
  \else
    \ifx#1\dhucs@next
      \chardef\nexttoh@ngul\@tempa
      \let\next@\remove@to@nnil \let\next\remove@to@nil
    \else
      \let\next@\check@dhucs@next@ascii@
    \fi
  \fi\next@
}
\def\remove@to@nil#1\@nil{}

%% now print out \unihangulchar
\newcommand*\unihangulchar@@@{%
  \getthish@ngulcl@ss
  \geth@ngulfontn@me
  \dobeforeh@ngulch@r
  \begingroup
    \geth@ngulpl@ne@ndch@r
    \leavevmode
    \fontencoding{LUC}\fontfamily\h@ngulfontn@me\selectfont
    \dhucs@check@if@char@exists
    \dhucs@emph\h@ngulch@r % bangjom emphasis
    \h@ngulch@r
  \endgroup
  \josa@buf@del\global\@josa\unih@ngulpoint
  \do@fterh@ngulch@r
}

%% ... why is this needed?
\newcommand*\hfontfamilynameprefix{ut}

%% hangul space factor 
\mathchardef\Hangul@spacefactor=1001\relax
\mathchardef\Hanja@spacefactor=1002\relax
\mathchardef\nobreak@spacefactor=998\relax

\newcommand*\getthish@ngulcl@ss{% what is current hangul category?
  % \thish@ngul = 1 : hangul
  % \thish@ngul = 2 : hanja, hiragana, katakana
  % \thish@ngul = 3 : opening parentheses; NOT in use: see lucenc.dfu
  % \thish@ngul = 4 : closing parentheses; NOT in use: see lucenc.dfu
  % \thish@ngul = 0 : all others
  \ifnum\unih@ngulpoint<12352
    \chardef\thish@ngul\z@ % cjk symbols
  \else\ifnum\unih@ngulpoint<12539
    \chardef\thish@ngul\tw@ % hiragana, katakana
  \else\ifnum\unih@ngulpoint<13312
    \chardef\thish@ngul\z@ % cjk symbols
  \else\ifnum\unih@ngulpoint<44032
    \chardef\thish@ngul\tw@ % hanja ext A; hanja unified
  \else\ifnum\unih@ngulpoint<57344
    \chardef\thish@ngul\@ne % hangul 
  \else\ifnum\unih@ngulpoint<64256
    \chardef\thish@ngul\tw@ % hanja compatibillity
  \else\ifnum\unih@ngulpoint<65520
    \chardef\thish@ngul\z@ % full/half width marks
  \else
    \chardef\thish@ngul\tw@ % hanja ext. B mostly 
  \fi\fi\fi\fi\fi \fi\fi
}

\newcommand*\geth@ngulfontn@me{%
  \ifnum\thish@ngul=\@ne
    \def\hangul@hanja{hangul}%
  \else
    \def\hangul@hanja{hanja}%
  \fi
  \edef\@tempa{\f@family}\edef\@tempb{\rmdefault}\edef\@tempc{\sfdefault}%
  \ifx\@tempa\@tempb
    \edef\h@ngulfontn@me{\csname dhucs@serif\hangul@hanja font\endcsname}%
  \else\ifx\@tempa\@tempc
    \edef\h@ngulfontn@me{\csname dhucs@sans\hangul@hanja font\endcsname}%
  \else
    \edef\h@ngulfontn@me{\csname dhucs@mono\hangul@hanja font\endcsname}%
    \def\dhucs@hu{\z@}% do not insert micro-space when ttfamily,
                      % under which it should be fixed-width.
  \fi\fi
}

%% disallow linebreak after opening parentheses
\sfcode`\(=\nobreak@spacefactor
\sfcode`\`=\nobreak@spacefactor
\sfcode`\[=\nobreak@spacefactor
%% TeX inserts \discretionary{}{}{} always after hyphen char,
%% so we can safely give nobreak-spacefactor to ``-'' to suppress
%% unwanted micro-space after latin hyphen char.
\sfcode`\-=\nobreak@spacefactor

\newcommand*\dobeforeh@ngulch@r{%
  \ifhmode
    \ifcase\lastnodetype
      % e-TeX's lastnodetype 0 is char node
      \dhucs@normal@break@before@cjk
    \or % hlist node 
        % As \hbox returns spacefactor to 1000, 
        % we cannot identify what was before this korean char.
        % So, allowing linebreak is somewhat dangerous; We Do, however.
      \ifcase\thish@ngul
        \or \ifcase\puncnobre@k\breakbetweenhanja
              \or\nobreakbetweenhanja\fi
        \or \ifcase\puncnobre@k\breakbetweenhanja
              \or\nobreakbetweenhanja\fi
      \fi
    \or % vlist node
    \or % rule node
    \or % ins node
    \or % mark node
    \or % adjust node
    \or % ligature node
      \ifnum\spacefactor>\nobreak@spacefactor
        \ifcase\thish@ngul 
          \or \ifcase\puncnobre@k\breakafterasciichar
                \or\nobreakafterasciichar\fi
          \or \ifcase\puncnobre@k\breakafterasciichar
                \or\nobreakafterasciichar\fi
        \fi
      \fi
    \or % disc node
    \or % whatsit node
        % how do we know what there was before whatsit?
      \dhucs@normal@break@before@cjk
    \or % math node
      \ifcase\thish@ngul
        \or \ifcase\puncnobre@k\breakafterinlinemath
              \or\nobreakafterinlinemath\fi
        \or \ifcase\puncnobre@k\breakafterinlinemath
              \or\nobreakafterinlinemath\fi
      \fi
    \or % glue node
    \or % kern node, which is another source of headache!
      \ifdim\lastkern=\z@
        % is it safe to break lines after \kern 0pt?
        \dhucs@normal@break@before@cjk
      \else
        \ifnum\spacefactor>\nobreak@spacefactor
          \ifdim\lastkern=\@tempdima % see redefinition of \sw@slant
            \ifnum\spacefactor>\@m
              \ifcase\thish@ngul
                \or \ifcase\puncnobre@k\breakbetweenhanja
                      \or\nobreakbetweenhanja\fi
                \or \ifcase\puncnobre@k\breakbetweenhanja
                      \or\nobreakbetweenhanja\fi
              \fi
            \else
              \ifcase\thish@ngul
                \or \ifcase\puncnobre@k\breakafterasciichar
                      \or\nobreakafterasciichar\fi
                \or \ifcase\puncnobre@k\breakafterasciichar
                      \or\nobreakafterasciichar\fi
              \fi
            \fi
          \else
            \ifcase\thish@ngul
                \or \ifcase\puncnobre@k\breakbetweenhanja
                      \or\nobreakbetweenhanja\fi
                \or \ifcase\puncnobre@k\breakbetweenhanja
                      \or\nobreakbetweenhanja\fi
            \fi
          \fi
        \fi
      \fi
      \@tempdima\z@
    \or % penalty node
        % Even after \nobreak, inter-hangulchar kern should be applied!
      \ifnum\lastpenalty<\@M 
        \dhucs@normal@break@before@cjk
      \else
        \chardef\puncnobre@k\@ne
        \dhucs@normal@break@before@cjk
      \fi
    \or % unset node
    \or % math mode node
    \fi
  \fi
}

%% detect last italiccorr
\edef\sw@slant{\unexpanded\expandafter{\sw@slant\@tempdima\lastkern}}

\newcommand*\dhucs@normal@break@before@cjk{%
  \ifnum\spacefactor>\nobreak@spacefactor
    \ifcase\thish@ngul
        % other cjk char: do nothing
    \or % hangul
      \ifnum\spacefactor=\Hangul@spacefactor
        \ifcase\puncnobre@k\breakbetweenhangul
          \or\nobreakbetweenhangul\fi
      \else
        \ifnum\spacefactor=\Hanja@spacefactor
          \ifcase\puncnobre@k\breakbetweenhanja
            \or\nobreakbetweenhanja\fi
        \else
          \ifcase\puncnobre@k\breakafterasciichar
            \or\nobreakafterasciichar\fi
        \fi
      \fi
    \or % hanja, hiragana, katakana
      \ifnum\spacefactor=\Hangul@spacefactor
        \ifcase\puncnobre@k\breakbetweenhanja
          \or\nobreakbetweenhanja\fi
      \else
        \ifnum\spacefactor=\Hanja@spacefactor
          \ifcase\puncnobre@k\breakbetweenhanja
            \or\nobreakbetweenhanja\fi
        \else
          \ifcase\puncnobre@k\breakafterasciichar
            \or\nobreakafterasciichar\fi
        \fi
      \fi
    \fi
  \fi
}

\newcommand*\do@fterh@ngulch@r{%
  \ifhmode
    \ifcase\nexttoh@ngul
      \ifcase\thish@ngul
          \declarehanjaspacefactor
      \or \declarehangulspacefactor
      \or \declarehanjaspacefactor
      \or \declarenobreakspacefactor
      \or \declarehanjaspacefactor
      \fi
    \or \breakbeforeasciichar % (
    \or % )
      \spacefactor\@m
    \or \kernbeforeasciichar % 123
    \or \expandafter\expandafter\expandafter\asciifullstopafterhangul % .
    \or \expandafter\expandafter\expandafter\asciiquestionafterhangul % ?
    \or \expandafter\expandafter\expandafter\asciiexclamationafterhangul % !
    \fi
  \fi
}

\newcommand*\declarenobreakspacefactor{\spacefactor\nobreak@spacefactor}
\newcommand*\declarehangulspacefactor{\spacefactor\Hangul@spacefactor}
\newcommand*\declarehanjaspacefactor{\spacefactor\Hanja@spacefactor}

%% default linebreaking command
\newcommand*\breakbetweenhanja{\discretionary{}{}{}}
\newcommand*\nobreakbetweenhanja{}

%% under option [finemath], hfontspec.un becomes active.
%% otherwize, all font-spec value is 0pt.
\if@dhucsfinemath
  %% As Korean typesetting allows linebreak at any point between hangul char,
  %% \doublehyphendemerits does not have meaning any longer.
  \doublehyphendemerits=0
  %% Now that some code to suppress char ``orphan'' has been added,
  %% \finalhyphendemerits does not have much meaning, either. 
  %% Moreover, \finalhyphendemerits discourages linebreak of final word 
  %% within a paragraph; undesirable when the final word is long. 
  \finalhyphendemerits=0
  %%
  \usehangulfontspec{ut}
  \newcommand*\breakbetweenhangul{\discretionary{}{}{\kern\dhucs@interhchar}}
  \newcommand*\nobreakbetweenhangul{\kern\dhucs@interhchar}
  % \dimexpr used, because \newdimen\something\something=1em fixes
  % \something's dimen size when it is initially assigned. 
  % In this case, for example, \dhucs@hu applied to normal size is the same size
  % as to \Huge size font. This does not seem to be what we want. 
  % Accordingly, we decided to declare \dhucs@hu as a macro, not a dimen.
  % Now that \dhucs@hu is a macro, e-TeX's \dimexpr enables simple definition
  % of its multiplication.
  \newcommand*\breakafterinlinemath{\hskip\dimexpr\dhucs@hu*3\relax\relax}
  % At above line, \dimexpr absorbs the first \relax;
  % second \relax terminates \hskip's arguments.
  % On the other hand, as \kern does not scan for ``plus'' or ``minus,''
  % another \relax seems to be redundant at following line.
  \newcommand*\nobreakafterinlinemath{\kern\dimexpr\dhucs@hu*3\relax}
  \newcommand*\breakafterasciichar{\hskip\dimexpr\dhucs@hu*2\relax\relax}
  \newcommand*\nobreakafterasciichar{\kern\dimexpr\dhucs@hu*2\relax}
  \newcommand*\breakbeforeasciichar{\hskip\dimexpr\dhucs@hu*2\relax\relax}
  \newcommand*\kernbeforeasciichar{\kern\dimexpr\dhucs@hu*2\relax}
  \newcommand*\asciifullstopafterhangul[1]{%
    \lower\dhucs@fullstoplower\hbox\bgroup#1\dhucs@check@another@punc}
  \newcommand*\asciiquestionafterhangul[1]{%
    \lower\dhucs@questionlower\hbox\bgroup
      \let\dhucs@kern@before@quot@char\dhucs@kern@before@quot@char@
      \kern\dimexpr\dhucs@hu*3\relax#1\dhucs@check@another@punc}
  \newcommand*\asciiexclamationafterhangul[1]{%
    \lower\dhucs@exclamationlower\hbox\bgroup
      \let\dhucs@kern@before@quot@char\dhucs@kern@before@quot@char@
      \kern\dimexpr\dhucs@hu*4\relax#1\dhucs@check@another@punc}
  \newcommand*\dhucs@kern@before@quot@char@{%
    \ifx'\dhucs@next\kern\dimexpr\dhucs@hu*3\relax\fi
    \spacefactor1003\relax} % for U+2019, U+201D; see lucenc.dfu
  \let\dhucs@kern@before@quot@char\relax
  %% lowering multiple puctuations:
  %% If next char is a punctuation, do not close \hbox and scan another char. 
  \newcommand*\dhucs@check@another@punc{%
    \futurelet\dhucs@next\dhucs@check@another@punc@}
  \newcommand*\dhucs@check@another@punc@{%
    \chardef\nexttoh@ngul\z@
    \if\noexpand\dhucs@next\relax\else
      \check@dhucs@next@ascii456\@nil
    \fi
    \dhucs@check@another@punc@@}
  \newcommand*\dhucs@check@another@punc@@{%
    \ifcase\nexttoh@ngul
          \expandafter\dhucs@check@another@punc@end
      \or \expandafter\dhucs@check@another@punc@end
      \or \expandafter\dhucs@check@another@punc@end
      \or \expandafter\dhucs@check@another@punc@end
      \or \let\dhucs@kern@before@quot@char\relax
          \expandafter\dhucs@check@another@punc@@@
      \or \let\dhucs@kern@before@quot@char\dhucs@kern@before@quot@char@
          \expandafter\dhucs@check@another@punc@@@
      \or \let\dhucs@kern@before@quot@char\dhucs@kern@before@quot@char@
          \expandafter\dhucs@check@another@punc@@@
    \fi}
  \newcommand*\dhucs@check@another@punc@@@[1]{%
    \kern\dhucs@hu\relax #1\dhucs@check@another@punc}
  \newcommand*\dhucs@check@another@punc@end{% 
    \dhucs@kern@before@quot@char
    \global\count@\spacefactor\egroup\spacefactor\count@}
\else
  \usehangulfontspec{default}
  \newcommand*\breakafterinlinemath{\hskip\z@skip}
  \newcommand*\nobreakafterinlinemath{}
  \newcommand*\breakbetweenhangul{\discretionary{}{}{}}
  \newcommand*\nobreakbetweenhangul{}
  \newcommand*\breakafterasciichar{\hskip\z@skip}
  \newcommand*\nobreakafterasciichar{}
  \newcommand*\breakbeforeasciichar{\hskip\z@skip}
  \newcommand*\kernbeforeasciichar{}
  \newcommand*\asciifullstopafterhangul{}
  \newcommand*\asciiquestionafterhangul{}
  \newcommand*\asciiexclamationafterhangul{}
\fi

%% linebreaking related to cjk symbols
\newcommand*\cjksymbolextraspace{\discretionary{}{}{}}
\newcommand*\cjksymbolskip{%
  \ifhmode
    \ifnum\spacefactor>\nobreak@spacefactor
      \ifnum\lastpenalty<\@M
        \cjksymbolextraspace
      \fi
    \fi
  \else
    \leavevmode
  \fi}
\newcommand*\postcjksymskip{\ifhmode\declarehanjaspacefactor\fi}
\newcommand*\postcjksymnobreak{\ifhmode\declarehanjaspacefactor\nobreak\fi}
\newcommand*\postcjksymbol{\ifhmode\declarehanjaspacefactor\fi}
\newcommand*\kernbeforelatinquoteclose{%
  \ifhmode\ifnum\spacefactor=1003 \kern\dimexpr\dhucs@hu*3\relax\fi\fi}
\newcommand*\cjksymbolunskip{}

%% \disablehangulfontspec disables all the font-spefic spaces and dimens
%% except the space after inline math.
\newcommand*\disablehangulfontspec{%
  \def\breakbetweenhangul{\discretionary{}{}{}}
  \let\nobreakbetweenhangul\@empty
  \def\breakafterasciichar{\hskip\z@skip}
  \let\nobreakafterasciichar\@empty
  \let\breakbeforeasciichar\breakafterasciichar
  \let\kernbeforeasciichar\@empty
  \let\asciifullstopafterhangul\relax
  \let\asciiquestionafterhangul\relax
  \let\asciiexclamationafterhangul\relax
}

%% \disablehangullinebreak disables all linebreaking 
%% related to hangul typesetting.
\newcommand*\disablehangullinebreak{%
  \let\unihangulchar@\unihangulchar@@@
  \let\dobeforeh@ngulch@r\relax
  \let\do@fterh@ngulch@r\relax
  \let\cjksymbolskip\leavevmode}


%% bangjom emphasis
\let\dhucs@emph\@gobble
\newdimen\dhucs@emph@raise
\newcommand*\dotemph{}
\protected\def\dotemph#1{\begingroup
  \settoheight{\dhucs@emph@raise}{#1}%
  \addtolength{\dhucs@emph@raise}{-.4em}%
  \let\dhucs@emph\dot@emph
  #1\endgroup}
\newcommand*\dot@emph[1]{%
  \ifnum\thish@ngul>\z@ %\ifnum\thish@ngul<\thr@@ % hangul or hanja
    \settowidth{\dimen@}{#1}%
    \rlap{\raise\dhucs@emph@raise
      \hb@xt@\dimen@{\normalfont\bfseries\hss˙\hss}}%
  \fi %\fi
}

%% Autojosa and hangul.
\newtoks\josatoks
\def\josa@buf@del{\global\josatoks{^^J}}\josa@buf@del
\protected\def\jong{\josa@buf@del\global\@josa\z@}
\protected\def\rieul{\josa@buf@del\global\@josa\@ne}
\protected\def\jung{\josa@buf@del\global\@josa\tw@}

\AtBeginDocument{%
  \if@dhucsfinemath
    % redefine ref/pageref for finemath
    \let\@setref@dhucs@orig@\@setref
    \def\@setref#1#2#3{\@setref@dhucs@orig@{#1}{#2}{#3}%
      \ifx#1\relax\else
        \bgroup\setbox\z@\lastbox\egroup
      \fi}%
  \fi
  \if@nojosa\else
    % redefine ref/pageref for autojosa
    \let\@setref@dhucs@orig\@setref
    \def\@setref#1#2#3{\@setref@dhucs@orig{#1}{#2}{#3}%
      \ifx#1\relax\else
        \bgroup
        \dhucs@make@cjkchar@null
        \edef\@temp{\expandafter#2#1}\global\josatoks\expandafter{\@temp}%
        \egroup
      \fi}%
    % redefine cite for autojosa: base only or with hyperref.sty
    \let\@cite@ofmt@dhucs@orig\@cite@ofmt
    \def\@cite@ofmt#1{\@cite@ofmt@dhucs@orig{#1}%
      \bgroup
      \dhucs@make@cjkchar@null
      \def\hyper@@link[##1]##2##3##4{##4}%
      \edef\@temp{#1}\global\josatoks\expandafter{\@temp}%
      \egroup}%
  \fi
  % version 4.0.14 :
  % redefine footref for autojosa (footmisc v5.3d)
  \@ifpackageloaded{footmisc}{%
    \if@nojosa\else
      \renewcommand\footref[1]{%
        \begingroup
          \let\@setref\@setref@dhucs@orig@
          \unrestored@protected@xdef\@thefnmark{\ref{#1}}%
        \endgroup
        \@footnotemark
        \global\josatoks\expandafter{\@thefnmark}}%      
    \fi
  }{}
  % redefine cite for autojosa: with cite.sty
  \@ifpackageloaded{cite}{%
    \if@nojosa\else
      \let\citeform@dhucs@orig\citeform
      \def\citeform#1{\citeform@dhucs@orig{#1}%
        \bgroup
        \dhucs@make@cjkchar@null
        \def\hyper@@link[##1]##2##3##4{##4}%
        \edef\@temp{#1}\global\josatoks\expandafter{\@temp}%
        \egroup}%
    \fi
  }{}%
  % redefine cite for autojosa: with natbib.sty (NOT a full support yet)
  \@ifpackageloaded{natbib}{%
    \if@nojosa\else
      \expandafter\def\expandafter\hyper@natlinkend\expandafter{%
        \hyper@natlinkend
        \bgroup%\def\protect##1##2{^^J}% how to do about \protect
        \ifnum\NAT@ctype=1
          \edef\@temp{\NAT@nm}%
        \else
          \ifNAT@numbers
          \edef\@temp{\NAT@num}\else
          \edef\@temp{\NAT@date}\fi
        \fi
        \global\josatoks\expandafter{\@temp}%
        \egroup}%
    \fi
  }{}%
  % support of autojosa for apacite.sty
  \@ifpackageloaded{apacite}{%
    \if@nojosa\else
      \let\dhucs@@cite@orig\@@cite
      \def\@@cite<#1>[#2]#3{%
        \dhucs@@cite@orig<#1>[#2]{#3}%
        \begingroup
        \dhucs@make@cjkchar@null
        \def\hyper@@link[##1]##2##3##4{##4}%
        \if@Y@cite
          \@for\@citeb:=#3\do{%
            \@ifundefined{b@\@citeb\@extra@b@citeb}{}{%
              \edef\@temp{\csname Y@\@citeb\@extra@b@citeb\endcsname}}}%
        \else
          \let\BCA\@firstoftwo
          \@for\@citeb:=#3\do{%
            \@ifundefined{b@\@citeb\@extra@b@citeb}{}{%
              \edef\@temp{\csname b@\@citeb\@extra@b@citeb\endcsname}}}%
        \fi
        \global\josatoks\expandafter{\@temp}%
        \endgroup}%
    \fi
  }{}%
}
\newcommand*\dh@unichar@null@[1]{^^J}
\newcommand*\dhucs@make@cjkchar@null{\let\unihangulchar\dh@unichar@null@}
\newcommand*\josa@hyper@link{%
  \begingroup
  \toks@{}\expandafter\josa@hyper@link@\the\josatoks\@nil
  \endgroup
}
\newcommand*\josa@hyper@link@{%
  \afterassignment\josa@link@reversetoks\let\dhucs@tmp@next= }
\newcommand*\josa@link@reversetoks{%
  \ifx\dhucs@tmp@next\@nil
    \expandafter\josa@hyper@link@@
  \else 
    \if\noexpand\dhucs@tmp@next\relax
    \else\ifx)\dhucs@tmp@next
    \else\ifx]\dhucs@tmp@next
    \else\ifcat\dhucs@cat@eleven\dhucs@tmp@next
      \edef\act{\noexpand\toks@{\expandafter\strip@letter@meaning@prefix
        \meaning\dhucs@tmp@next\the\toks@}}\act % by little tree
    \else\ifcat\dhucs@cat@twelve\dhucs@tmp@next
      \edef\act{\noexpand\toks@{\expandafter\strip@character@meaning@prefix
        \meaning\dhucs@tmp@next\the\toks@}}\act % by little tree
    \fi\fi\fi\fi\fi
    \expandafter\josa@hyper@link@
  \fi}

\begingroup
\catcode`\A=11
\catcode`\0=12
\global\let\dhucs@cat@eleven=A
\global\let\dhucs@cat@twelve=0
\endgroup

\expandafter\def\expandafter\strip@character@meaning@prefix
  \detokenize{the character} #1{#1}
\expandafter\def\expandafter\strip@letter@meaning@prefix
  \detokenize{the letter} #1{#1}

\newcommand*\josa@hyper@link@@{%
  \expandafter\josa@hyper@link@@@\the\toks@\@empty\@empty\@empty\@empty\@nil}
\def\josa@hyper@link@@@#1#2#3#4\@nil{%
  \if#1^^J\else
    \if 1#1\rieul\else
    \if 3#1\jong\else
    \if 6#1\jong\else
    \if 7#1\rieul\else
    \if 8#1\rieul\else
    \if 0#1\jong\else
    \if l#1\rieul\else
    \if m#1\jong\else
    \if n#1\jong\else
    \if L#1\rieul\else
    \if M#1\jong\else
    \if N#1\jong\else
      \jung
    \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi\fi
    \if n#2\if g#1\jong\fi\fi%skating
    \if a#2\if p#1\jong\fi\fi%cap
    \if u#2\if p#1\jong\fi\fi%cup
    \if a#2\if t#1\jong\fi\fi%cat
    \if e#2\if t#1\jong\fi\fi%Hamlet
    \if i#2\if t#1\jong\fi\fi%summit
    \if o#2\if t#1\jong\fi\fi%not
    \if u#2\if t#1\jong\fi\fi%nut
    \if o#2\if k#1\jong\fi\fi%book
    \if i#2\if c#1\jong\fi\fi%picnic
    \if l#2\if e#1\rieul\fi\fi%apple
    \if i#3\if m#2\if e#1\jong\fi\fi\fi%time
    \if i#3\if n#2\if e#1\jong\fi\fi\fi%mine
    \if a#3\if c#2\if k#1\jong\fi\fi\fi%back
    \if i#3\if c#2\if k#1\jong\fi\fi\fi%pick
    \if o#3\if a#2\if t#1\jung\fi\fi\fi%boat
    \if T#3\if e#2\if X#1\jong\fi\fi\fi%TeX
  \fi}
\if@nojosa
  \let\josa@hyper@link\relax
\fi 
\newcommand*\josa@hanja@hangul[1]{\begingroup
  \count@=\z@
  \openin1=#1\relax \josa@hanja \closein1
  \endgroup}
\newcommand*\josa@hanja{\read1 to \@tempa
  \ifnum\count@=\@josa
    \global\@josa\@tempa \let\next\relax
  \else
    \advance\count@\@ne \let\next\josa@hanja
  \fi \next}
\protected\def\makejosa#1#2{%
  \josa@hyper@link
  \ifnum\@josa>19967 \ifnum\@josa<40870 %%% CJK UNIFIED IDEOGRAPHS
    \advance\@josa-19968 % "4E00
    \josa@hanja@hangul{hanja_hg.tab}\fi\fi
  \ifnum\@josa>13311 \ifnum\@josa<19894 %%% CJK UNIFIED IDEOGRAPHS EXT. A
    \advance\@josa-13312 % "3400
    \josa@hanja@hangul{hjexa_hg.tab}\fi\fi
  \ifnum\@josa>63743 \ifnum\@josa<64046 %%% CJK COMPATIBILITY IDEOGRAPHS
    \advance\@josa-63744 % "F900
    \josa@hanja@hangul{hjcom_hg.tab}\fi\fi
  \ifnum\@josa>44031 \ifnum\@josa<55204 %%% hangul syllables
    \advance\@josa-44032 % "AC00
    \count@=\@josa \divide\count@28 \multiply\count@28
    \advance\@josa-\count@ \advance\@josa4519 % "11A7
    \ifnum\@josa=4519 \jung
    \else\ifnum\@josa=4527 \rieul % "11AF
    \else \jong \fi\fi \fi\fi
  \ifnum\@josa>12592 \ifnum\@josa<12623 %%% ㄱㄴㄷㄹ
    \ifnum\@josa=12601 \rieul
    \else\jong\fi \fi\fi
  \ifnum\@josa>12895 \ifnum\@josa<12924 \advance\@josa-96 \fi\fi
  \ifnum\@josa>12799 \ifnum\@josa<12814 %%% ㉠ or ㈀
    \ifnum\@josa=12803 \rieul
    \else\jong\fi \fi\fi
  \ifnum\@josa>12813 \ifnum\@josa<12829 %%% ㉮ or ㈎
    \jung \fi\fi
  \ifnum\@josa>9311 \ifnum\@josa<9372 %%% ① or ⑴
    \advance\@josa\m@ne \num@josa{\@josa}\fi\fi
  \ifnum\@josa>9423 \ifnum\@josa<9450 \advance\@josa-26 \fi\fi
  \ifnum\@josa>9397 \ifnum\@josa<9424 \advance\@josa-26 \fi\fi
  \ifnum\@josa>9371 \ifnum\@josa<9398 %%% ⓐ or ⒜
    \ifnum\@josa=9383 \rieul\else % l
    \ifnum\@josa=9384 \jong\else  % m
    \ifnum\@josa=9385 \jong\else  % n
    \jung\fi\fi\fi \fi\fi
  \ifnum\@josa>8543 \ifnum\@josa<8556 % ⅰⅱⅲ
    \advance\@josa-3 \num@josa{\@josa}\fi\fi
  \ifnum\@josa>8559 \ifnum\@josa<8572 % ⅠⅡⅢ
    \advance\@josa\@ne \num@josa{\@josa}\fi\fi
  \ifnum\@josa=\@ne
    \ifx\@empty#1\@empty \else#2\relax\fi% rieul
  \else
    \ifnum\@josa=\tw@ #1\relax% jung
    \else #2\relax% jong
    \fi
  \fi}
\newcommand*\num@josa[1]{\bgroup\count@#1%
  \ifnum\count@<\z@ \count@-\count@ \fi\count2\count@
  \divide\count2 10 \multiply\count2 10 \advance\count@-\count2
  \ifcase\count@\jong\or\rieul\or\jung\or\jong\or\jung\or
    \jung\or\jong\or\rieul\or\rieul\or\jung\fi\egroup}
\newcommand*\error@josa{\PackageError{kotex}{Undefined Control Sequence}{}}

%% \nobreak다: see http://kts.ktug.kr/node/204
\newcommand*\nobreak@hangul@da{\nobreak다}

\newcommand*\^^ea[2]{%
  \if\string#1\string^^b0%
    \if\string#2\string^^80\makejosa{가}{이}\else\error@josa\fi% 가
  \else\if\string#1\string^^b3%
    \if\string#2\string^^bc\makejosa{와}{과}\else\error@josa\fi% 과
  \else\error@josa \fi\fi}
\newcommand*\^^eb[2]{%
  \if\string#1\string^^8b\if\string#2\string^^a4% avoid orphan "다"
    \expandafter\expandafter\expandafter\nobreak@hangul@da\else\error@josa\fi
  \else\if\string#1\string^^8a%
    \if\string#2\string^^94\makejosa{는}{은}\else\error@josa\fi% 는
  \else\if\string#1\string^^9d%
    \if\string#2\string^^bc\makejosa{라}{이라}\else\error@josa\fi% 라
  \else\if\string#1\string^^a1%
    \if\string#2\string^^9c\makejosa{}{으}로\else\error@josa\fi% 로
  \else\if\string#1\string^^a5%
    \if\string#2\string^^bc\makejosa{를}{을}\else\error@josa\fi% 를
  \else\error@josa \fi\fi\fi\fi\fi}
\newcommand*\^^ec[2]{%
  \edef\@tempa{\string#1}\edef\@tempb{\string#2}%
  \futurelet\next\@josa@begin@ec}
\def\@josa@begin@ec{%
  \if\@tempa\string^^99%
    \if\@tempb\string^^80\makejosa{와}{과}\else\error@josa\fi% 와
  \else\if\@tempa\string^^9c%
    \if\@tempb\string^^bc\makejosa{}{으}\else\error@josa\fi% 으
  \else\if\@tempa\string^^9d%
    \if\@tempb\string^^80\makejosa{는}{은}% 은
    \else\if\@tempb\string^^84\makejosa{를}{을}% 을
    \else\if\@tempb\string^^b4%
      \ifx\next^^eb\makejosa\relax{이}% 이라
      \else\makejosa{가}{이}% 이
      \fi
    \else\error@josa \fi\fi\fi
  \else\error@josa \fi\fi\fi}

%% hangul item titles
\newcommand*\jaso[1]{\@jaso{\@nameuse{c@#1}}}
\newcommand*\@jaso[1]{\ifcase#1\or
  ㄱ\or ㄴ\or ㄷ\or ㄹ\or ㅁ\or ㅂ\or ㅅ\or ㅇ\or
  ㅈ\or ㅊ\or ㅋ\or ㅌ\or ㅍ\or ㅎ\else\@ctrerr\fi}
\newcommand*\gana[1]{\@gana{\@nameuse{c@#1}}}
\newcommand*\@gana[1]{\ifcase#1\or
  가\or 나\or 다\or 라\or 마\or 바\or 사\or 아\or
  자\or 차\or 카\or 타\or 파\or 하\else\@ctrerr\fi}
\newcommand*\ojaso[1]{\@ojaso{\@nameuse{c@#1}}}
\newcommand*\@ojaso[1]{\ifcase#1\or
  ㉠\or ㉡\or ㉢\or ㉣\or ㉤\or ㉥\or ㉦\or ㉧\or
  ㉨\or ㉩\or ㉪\or ㉫\or ㉬\or ㉭\else\@ctrerr\fi}
\newcommand*\ogana[1]{\@ogana{\@nameuse{c@#1}}}
\newcommand*\@ogana[1]{\ifcase#1\or
  ㉮\or ㉯\or ㉰\or ㉱\or ㉲\or ㉳\or ㉴\or ㉵\or
  ㉶\or ㉷\or ㉸\or ㉹\or ㉺\or ㉻\else\@ctrerr\fi}
\newcommand*\pjaso[1]{\@pjaso{\@nameuse{c@#1}}}
\newcommand*\@pjaso[1]{\ifcase#1\or
  ㈀\or ㈁\or ㈂\or ㈃\or ㈄\or ㈅\or ㈆\or ㈇\or
  ㈈\or ㈉\or ㈊\or ㈋\or ㈌\or ㈍\else\@ctrerr\fi}
\newcommand*\pgana[1]{\@pgana{\@nameuse{c@#1}}}
\newcommand*\@pgana[1]{\ifcase#1\or
  ㈎\or ㈏\or ㈐\or ㈑\or ㈒\or ㈓\or ㈔\or ㈕\or
  ㈖\or ㈗\or ㈘\or ㈙\or ㈚\or ㈛\else\@ctrerr\fi}
\newcommand*\onum[1]{\@onum{\@nameuse{c@#1}}}
\newcommand*\@onum[1]{\ifcase#1\or
  ①\or ②\or ③\or ④\or ⑤\or ⑥\or ⑦\or ⑧\or
  ⑨\or ⑩\or ⑪\or ⑫\or ⑬\or ⑭\or ⑮\else\@ctrerr\fi}
\newcommand*\pnum[1]{\@pnum{\@nameuse{c@#1}}}
\newcommand*\@pnum[1]{\ifcase#1\or
  ⑴\or ⑵\or ⑶\or ⑷\or ⑸\or ⑹\or ⑺\or ⑻\or
  ⑼\or ⑽\or ⑾\or ⑿\or ⒀\or ⒁\or ⒂\else\@ctrerr\fi}
\newcommand*\oeng[1]{\@oeng{\@nameuse{c@#1}}}
\newcommand*\@oeng[1]{\ifcase#1\or
  ⓐ\or ⓑ\or ⓒ\or ⓓ\or ⓔ\or ⓕ\or ⓖ\or ⓗ\or ⓘ\or
  ⓙ\or ⓚ\or ⓛ\or ⓜ\or ⓝ\or ⓞ\or ⓟ\or ⓠ\or ⓡ\or
  ⓢ\or ⓣ\or ⓤ\or ⓥ\or ⓦ\or ⓧ\or ⓨ\or ⓩ\else\@ctrerr\fi}
\newcommand*\peng[1]{\@peng{\@nameuse{c@#1}}}
\newcommand*\@peng[1]{\ifcase#1\or
  ⒜\or ⒝\or ⒞\or ⒟\or ⒠\or ⒡\or ⒢\or ⒣\or ⒤\or
  ⒥\or ⒦\or ⒧\or ⒨\or ⒩\or ⒪\or ⒫\or ⒬\or ⒭\or
  ⒮\or ⒯\or ⒰\or ⒱\or ⒲\or ⒳\or ⒴\or ⒵\else\@ctrerr\fi}
\newcommand*\hnum[1]{\@hnum{\@nameuse{c@#1}}}
\newcommand*\@hnum[1]{\ifcase#1\or
  하나\or 둘\or 셋\or 넷\or 다섯\or 여섯\or 일곱\or 여덟\or
  아홉\or 열\or 열하나\or 열둘\or 열셋\or 열넷\or 열다섯\or
  열여섯\or 열일곱\or 열여덟\or 열아홉\or 스물\or 스물하나\or
  스물둘\or 스물셋\or 스물넷\else\@ctrerr\fi}
\newcommand*\Hnum[1]{\@Hnum{\@nameuse{c@#1}}}
\newcommand*\@Hnum[1]{\ifcase#1\or
  첫\or 둘\or 셋\or 넷\or 다섯\or 여섯\or 일곱\or 여덟\or
  아홉\or 열\or 열한\or 열두\or 열셋\or 열넷\or 열다섯\or
  열여섯\or 열일곱\or 열여덟\or 열아홉\or 스무\or 스물한\or
  스물두\or 스물셋\or 스물넷\else\@ctrerr\fi 째}
\newcommand*\hroman[1]{\@hroman{\@nameuse{c@#1}}}
\newcommand*\@hroman[1]{\ifcase#1\or
  ⅰ\or ⅱ\or ⅲ\or ⅳ\or ⅴ\or ⅵ\or ⅶ\or ⅷ\or
  ⅸ\or ⅹ\or ⅺ\or ⅻ\else\@ctrerr\fi}
\newcommand*\hRoman[1]{\@hRoman{\@nameuse{c@#1}}}
\newcommand*\@hRoman[1]{\ifcase#1\or
  Ⅰ\or Ⅱ\or Ⅲ\or Ⅳ\or Ⅴ\or Ⅵ\or Ⅶ\or Ⅷ\or
  Ⅸ\or Ⅹ\or Ⅺ\or Ⅻ\else\@ctrerr\fi}

\AtBeginDocument{
%% compatibility with apacite.sty (reported by slomo)
%% see http://www.ktug.or.kr/jsboard/read.php?table=operate&no=21183
%  \@ifpackageloaded{apacite}{%
%    \@ifpackagewith{apacite}{noaccentfix}{}{%
%      \def\unihangulchar@APAC#1{uni#1}%
%      \g@addto@macro\@pre@accentfix{%
%        \let\old@unihangulchar@APAC\unihangulchar
%        \let\unihangulchar\unihangulchar@APAC}%
%      \g@addto@macro\@post@accentfix{%
%        \let\unihangulchar\old@unihangulchar@APAC}%
%    }%
%  }{}%
%% compatibility with ulem.sty
%% allowing linebreaking inside ulem'ed text.
  \@ifpackageloaded{ulem}{%
    \let\orig@lastnodetype\lastnodetype
    \edef\UL@stop{\unexpanded{%
        \ifnum\lastkern=\thr@@ % empty hbox
        \else
          \global\chardef\dhucs@ul@lastnodetype\orig@lastnodetype
          \global\mathchardef\dhucs@ul@spacefactor\spacefactor
        \fi}\unexpanded\expandafter{\UL@stop}}
    \def\dhucs@ul@stop{\ifx\UL@start\@empty
      \ifx\ \LA@space\else\UL@stop\fi\fi}
    \def\dhucs@ul@start{\ifx\UL@start\@empty\else
      \ifx\ \LA@space\else\UL@start\fi\fi}
    \addto@hook\UL@hook{%
      \def\discretionary#1#2#3{%
        \ifx\\#3\\\hskip\z@skip\else\hskip\dhucs@interhchar\fi}%
      \def\unihangulchar#1{\dhucs@ul@stop
        \unih@ngulpoint#1\relax \futurelet\dhucs@next\unihangulchar@@}%
      \edef\dobeforeh@ngulch@r{%
        \unexpanded{\ifdefined\dhucs@ul@lastnodetype
            \let\lastnodetype\dhucs@ul@lastnodetype
            \spacefactor\dhucs@ul@spacefactor
          \fi}\unexpanded\expandafter{\dobeforeh@ngulch@r\dhucs@ul@start}}%
      \edef\breakbeforeasciichar{\unexpanded{\dhucs@ul@stop}%
        \unexpanded\expandafter{\breakbeforeasciichar\dhucs@ul@start}}%
      \edef\cjksymbolextraspace{\unexpanded{\dhucs@ul@stop}%
        \unexpanded\expandafter{\cjksymbolextraspace\dhucs@ul@start}}%
      \let\hskip\LA@hskip
      \let\asciiexclamationafterhangul\relax
      \let\asciiquestionafterhangul\relax
      \let\asciifullstopafterhangul\relax
    }%
  }{}%
}

%% use hyperref's unichar support
\unless\ifdefined\pdfstringdefPreHook
  \let\pdfstringdefPreHook\@empty\fi
\g@addto@macro\pdfstringdefPreHook{%
  \let\unihangulchar\HyPsd@unichar
  \let\makejosa\@secondoftwo
  \let\dotemph\@firstofone
  \let\jong\@empty
  \let\jung\@empty
  \let\rieul\@empty
  \def\SetHangulFonts#1#2#3{}%
  \def\SetHanjaFonts#1#2#3{}%
  \def\SetSerifFonts#1#2{}%
  \def\SetSansFonts#1#2{}%
  \def\SetAdhocFonts#1#2{}%
}


%% for downward compatibility
\let\PrerenderUnicode\@gobble
\let\dhucsPrerenderUnicode\@gobble
\let\BreakBeforeAsciiOParen\@gobble
\let\unichar\unihangulchar

%% load ucs-hangul upon [hangul] option
\if@hangul
\InputIfFileExists{kosections-utf.tex}{}{%
  \PackageWarning{kotex}{kosections-utf.tex doesn't exist!}{}}%
\fi

\endinput
