%% File `kotexutf-core.tex`
%%
%% Copyright (C) 2009-2022 Dohyun Kim <nomos at ktug org>
%% Copyright (C) 2015-2022 Kangsoo Kim <karnes 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.
%%
\expandafter\ifx\csname eTeXversion\endcsname\relax
  \errhelp{kotexutf requires e-TeX. Please run etex or pdftex.}
  \errmessage{kotexutf : Not running under e-TeX.}
  \expandafter\endinput
\fi

%% frenchspacing is default
\frenchspacing

\def\unihangul@two@octets#1#2{%
    \expandafter\unihangulchar\expandafter{%
      \number\numexpr
       (`#1 - 192) * 64 +
       (`#2 - 128) \relax}}
\def\unihangul@three@octets#1#2#3{%
    \expandafter\unihangulchar\expandafter{%
      \number\numexpr
       (`#1 - 224) * 4096 +
       (`#2 - 128) * 64 +
       (`#3 - 128) \relax}}
\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}}

\def\unihangul@ii@octs#1#2{%
  \ifincsname
    \string#1\string#2%
  \else
    \ifx\protect\relax
      \ifcsname u8:\string#1\string#2\endcsname
        \csname u8:\string#1\string#2\endcsname
      \else
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter\unihangul@two@octets
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#1%
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#2%
      \fi
    \else
      \noexpand#1\noexpand#2%
    \fi
  \fi
}
\def\unihangul@iii@octs#1#2#3{%
  \ifincsname
    \string#1\string#2\string#3%
  \else
    \ifx\protect\relax
      \ifcsname u8:\string#1\string#2\string#3\endcsname
        \csname u8:\string#1\string#2\string#3\endcsname
      \else
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter\unihangul@three@octets
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#1%
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#2%
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#3%
      \fi
    \else
      \noexpand#1\noexpand#2\noexpand#3%
    \fi
  \fi
}
\def\unihangul@iv@octs#1#2#3#4{%
  \ifincsname
    \string#1\string#2\string#3\string#4%
  \else
    \ifx\protect\relax
      \ifcsname u8:\string#1\string#2\string#3\string#4\endcsname
        \csname U8:\string#1\string#2\string#3\string#4\endcsname
      \else
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter\unihangul@four@octets
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#1%
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#2%
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#3%
        \expandafter\expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter#4%
      \fi
    \else
      \noexpand#1\noexpand#2\noexpand#3\noexpand#4%
    \fi
  \fi
}

\count@"80 \loop
  \uccode\count@\count@
  \lccode\count@\count@
\ifnum\count@<"BF \advance\count@\@ne \repeat

\count@"C2 \loop
  \uccode\count@\count@
  \lccode\count@\count@
  \begingroup
  \lccode`\~\count@
  \lowercase{\endgroup \def~{\unihangul@ii@octs~}}
\ifnum\count@<"DF \advance\count@\@ne \repeat

\count@"E0 \loop
  \uccode\count@\count@
  \lccode\count@\count@
  \begingroup
  \lccode`\~\count@
  \lowercase{\endgroup \def~{\unihangul@iii@octs~}}
\ifnum\count@<"EF \advance\count@\@ne \repeat

\count@"F0 \loop
  \uccode\count@\count@
  \lccode\count@\count@
  \begingroup
  \lccode`\~\count@
  \lowercase{\endgroup \def~{\unihangul@iv@octs~}}
\ifnum\count@<"F4 \advance\count@\@ne \repeat

%% subfont plane and char slot
\def\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@}%
}
\def\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
}

%% hangul font specific dimens
%% changes for avoiding errors on MiKTeX, 2015/10/07, by Karnes.
%\def\dhucs@hu{}
%\def\dhucs@interhchar{}
%\def\dhucs@fullstoplower{}
%\def\dhucs@questionlower{}
%\def\dhucs@exclamationlower{}
\def\dhucs@hu{\z@}
\def\dhucs@interhchar{\z@}
\def\dhucs@fullstoplower{\z@}
\def\dhucs@exclamationlower{\z@}
\def\dhucs@questionlower{\z@}
\def\dhucs@serifhangulfont{nanummj}
\def\dhucs@sanshangulfont{nanumgt}
\def\dhucs@monohangulfont{nanumgt}
\def\dhucs@serifhanjafont{nanumgt}
\def\dhucs@sanshanjafont{nanumgt}
\def\dhucs@monohanjafont{nanumgt}

\def\usehangulfontspec#1{%
  \begingroup
    \immediate\openin\z@=hfontspec.#1 %
    \ifeof\z@
      \message{\space\space *** Cannot find hangul font-spec file hfontspec.#1 ***\space\space}
    \else
      \endlinechar\m@ne
      \get@hfnt@spec@
    \fi
    \immediate\closein\z@
  \endgroup}
\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

%% protect \unihangulchar
\protected\def\unihangulchar#1{%
  \unih@ngulpoint#1\relax
  \futurelet\dhucs@next\unihangulchar@@}
\def\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
}

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

\def\check@next@to@puncs{%
  \afterassignment\check@next@to@puncs@\let\dhucs@next= }
\def\check@next@to@puncs@{%
  \futurelet\dhucs@nextnext\check@next@to@puncs@@}
\def\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
}

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

%% see lucenc.dfu
\def\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
}
\def\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{}
\def\remove@to@nnil#1\@nnil{}

%%% if \strictcharcheck is defined ...
\ifdefined\strictcharcheck
  \def\dhucs@check@if@char@exists{%
    \unless\iffontchar\font@name\h@ngulch@r
      \errmessage{Character \number\h@ngulch@r\space
        doesn't exist in font \fontname\font@name}%
    \fi}
\else
  \let\dhucs@check@if@char@exists\relax
\fi

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

\def\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
}

%% disallow linebreak after opening parentheses
\sfcode`\(=\nobreak@spacefactor
\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

\def\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
}

\ifdefined\sw@slant
  %% detect last italiccorr
  \edef\sw@slant{\unexpanded\expandafter{\sw@slant\@tempdima\lastkern}}
\else
  \unless\ifdefined\@tempdima \newdimen\@tempdima\@tempdima=0pt \fi
\fi

\def\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
}

\def\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
}

\def\declarenobreakspacefactor{\spacefactor\nobreak@spacefactor}
\def\declarehangulspacefactor{\spacefactor\Hangul@spacefactor}
\def\declarehanjaspacefactor{\spacefactor\Hanja@spacefactor}

%% default linebreaking command
\def\breakbetweenhanja{\discretionary{}{}{}}
\def\nobreakbetweenhanja{}

%% under option [finemath], hfontspec.un becomes active.
%% otherwize, all font-spec value is 0pt.
\ifdefined\finemath %\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{default}
  \def\breakbetweenhangul{\discretionary{}{}{\kern\dhucs@interhchar}}
  \def\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.
  \def\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.
  \def\nobreakafterinlinemath{\kern\dimexpr\dhucs@hu*3\relax}
  \def\breakafterasciichar{\hskip\dimexpr\dhucs@hu*2\relax\relax}
  \def\nobreakafterasciichar{\kern\dimexpr\dhucs@hu*2\relax}
  \def\breakbeforeasciichar{\hskip\dimexpr\dhucs@hu*2\relax\relax}
  \def\kernbeforeasciichar{\kern\dimexpr\dhucs@hu*2\relax}
  \def\asciifullstopafterhangul#1{%
    \lower\dhucs@fullstoplower\hbox\bgroup#1\dhucs@check@another@punc}
  \def\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}
  \def\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}
  \def\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.
  \def\dhucs@check@another@punc{%
    \futurelet\dhucs@next\dhucs@check@another@punc@}
  \def\dhucs@check@another@punc@{%
    \chardef\nexttoh@ngul\z@
    \if\noexpand\dhucs@next\relax\else
      \check@dhucs@next@ascii456\@nil
    \fi
    \dhucs@check@another@punc@@}
  \def\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}
  \def\dhucs@check@another@punc@@@#1{%
    \kern\dhucs@hu\relax #1\dhucs@check@another@punc}
  \def\dhucs@check@another@punc@end{%
    \dhucs@kern@before@quot@char
    \global\count@\spacefactor\egroup\spacefactor\count@}
\else
  \usehangulfontspec{default}
  \def\breakafterinlinemath{\hskip\z@skip}
  \def\nobreakafterinlinemath{}
  \def\breakbetweenhangul{\discretionary{}{}{}}
  \def\nobreakbetweenhangul{}
  \def\breakafterasciichar{\hskip\z@skip}
  \def\nobreakafterasciichar{}
  \def\breakbeforeasciichar{\hskip\z@skip}
  \def\kernbeforeasciichar{}
  \def\asciifullstopafterhangul{}
  \def\asciiquestionafterhangul{}
  \def\asciiexclamationafterhangul{}
\fi

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

%% \disablehangulfontspec disables all the font-spefic spaces and dimens
%% except the space after inline math.
\def\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.
\def\disablehangullinebreak{%
  \let\unihangulchar@\unihangulchar@@@
  \let\dobeforeh@ngulch@r\relax
  \let\do@fterh@ngulch@r\relax
  \let\cjksymbolskip\leavevmode}


%% bangjom emphasis
\def\dhucs@emph#1{}
\newdimen\dhucs@emph@raise
\protected\def\dotemph#1{\begingroup
  \setbox\z@\hbox{#1}\dhucs@emph@raise\ht\z@
  \advance\dhucs@emph@raise -.4em
  \let\dhucs@emph\dot@emph
  #1\endgroup}
\def\dot@emph#1{%
  \ifnum\thish@ngul>\z@ %\ifnum\thish@ngul<\thr@@ % hangul or hanja
    \setbox\z@\hbox{#1}\dimen@\wd\z@
    \rlap{\raise\dhucs@emph@raise
      \hbox to\dimen@{\normalfont\bfseries\hss˙\hss}}\fi %\fi
}

\endinput

