%
%	hlatexp.ltx
%	2-byte character handler for hLaTeXp
%	1998/11/20 version
%
%	Jae Choon Cha <jccha@knot.kaist.ac.kr>
%	Department of Mathematics
%	Korea Advanced Institute of Science and Technology
%
%	Copyright (C) Jae Choon Cha. All Rights are reserved.
%	You are not allowed to change this file.
%	This file is part of hLaTeXp package. You are not allowed to distribute
%	this file alone. This file should be distributed as part of hLaTeXp
%	package of the author.
%	This file (and any derivation) cannot be sold or distributed with
%	any commercial product or used in any commercial organization without
%	additional agreement with the author.
%
%	For suggestions and bug reports of UNCHANGED version, send e-mail to
%	jccha@knot.kaist.ac.kr.
%

% Format name, date and identifier.
\def\hlatexfmtname{Hangul LaTeX2e (hLaTeXp)}
\def\hlatexfmtdate{1998/11/20}
\typeout{\hlatexfmtname\space<\hlatexfmtdate>}
\def\this@is@hlatexp{}
%
%	This 2-byte character engine is essentially a hack to TeX and LaTeX's
%	font handling mechanism. A word of confession: The present
%	implementation is very dirty, nasty, and hard to be read, in order to
%	work on the old good TeX (in which only 8-bit characters are supported)
%	and to be efficient. So I try to explain ideas and key macro names to
%	help reading.
%
%	The basic idea is to associate to each TeX font (like cmr10, cmr9) for
%	single byte characters (SBC) a set of font names for double byte
%	characters (DBC). Why not a font but a set of fonts? Different fonts
%	are associated to the same SBC font, for Hangul, Symbol and Hanja.  The
%	macro names which defines this association is as follows:
%
%	\cmr10@db@hgfn	Hangul font name for cmr10 (e.g. xnmj)
%	\cmr10@db@sbfn	Symbol font name for cmr10 (e.g. xsymb)
%	\cmr10@db@hjfn	Hanja font name for cmr10 (e.g. xmj)
%
%	Initially, the association table is read from dbfont.map file when
%	\begin{document} is processed.
%
%	There is another internal association, which maps each loaded SBC fonts
%	(like \myfont where \font\myfont=cmr10 has been executed) to Hangul
%	fonts. This binding is done when a SBC font is loaded, and the font
%	loading part of LaTeX NFSS is hacked to do this.
%
%	Whenever a TeX font \myfont is loaded by \font\myfont=cmr10, the
%	following hangul fonts are loaded.
%
%	\myfont@hgA, \myfont@hgB	Associated fonts for Hangul (e.g xnmj1)
%
%	The present implementation expects x-series fonts, in which a Hangul
%	font is splitted into two TeX fonts because of 8-bit limitation.
%	Therefore we have two macro names.  Similarly, one Symbol (resp. Hanja)
%	font is splitted into 5 (resp. 20) TeX fonts.
%
%	This association of SBC fonts to Hangul fonts is used to make
%	typesetting fast. Indeed, Hanja and Symbol fonts are not loaded when a
%	SBC font is loaded, and they are dynamically loaded when a Hanja or
%	Symbol character is firstly used. The following macros are defined when
%	a SBC font is loaded to help this dynamic loading.
%
%	\myfont@size		Loaded size of \myfont (decimal number in pt)
%	\myfont@extname		External font name for \myfont (e.g. cmr10)
%
%	From the above macros, the first association table is accessed to get
%	Hanja and Symbol font names when they are loaded dynamically.
%
%	As explained before, Hangul, Symbol and Hanja fonts are associated to a
%	TeX font for SBC and use for DBC. In other words, these fonts become
%	"current fonts" for DBC when the TeX font for SBC is selected as the
%	current font. (\selectfont macro is hacked to do this.) The fonts for
%	current DBC fonts are as follows.
%
%	\dbcfont@hgA .. \dbcfont@hgB	Fonts for Hangul characters.
%	\dbcfont@sbA .. \dbcfont@sbE	Fonts for Symbol characters.
%	\dbcfont@hjA .. \dbcfont@hjT	Fonts for Hanja characters.
%
%	Indeed, the current fonts for DBC may changed by hacking these macros.
%	\SelectHangulFont macro does this.
%

% Add DBC font engine.
\makeatletter
\newif\iftrace@dbcfont \trace@dbcfontfalse
\newif\ifauto@subst@dbfsize \auto@subst@dbfsizetrue
\def\dbfsize@subst#1{%
  \ifdim #1<6.07pt 5.8\else
  \ifdim #1<6.63pt 6.33\else
  \ifdim #1<7.63pt 6.93\else
  \ifdim #1<8.73pt 8.33\else
  \ifdim #1<9.57pt 9.13\else
  \ifdim #1<10.48pt 10\else
  \ifdim #1<11.48pt 10.96\else
  \ifdim #1<12.57pt 12\else
  \ifdim #1<13.77pt 13.13\else
  \ifdim #1<15.84pt 14.4\else
  \ifdim #1<19pt 17.27\else
  \ifdim #1<22.8pt 20.73\else
  \ifdim #1<27.37pt 24.87\else 29.87\fi
  \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi pt}
\begingroup
\def\r#1{\let#1\relax}
\def\xafter{\noexpand\expandafter}
\def\csn{\noexpand\csname}
\def\endcsn{\noexpand\endcsname}
\def\str{\noexpand\string}
% Let each current DBC fonts be null.
\begingroup
\def\d#1{\global\expandafter\let\csname dbcfont@#1\endcsname\nullfont}
\def\e#1{\global\expandafter\let\csname dbcfont@#1\endcsname\relax}
\d{hgA}\d{hgB}%
\e{sbA}\e{sbB}\e{sbC}\e{sbD}\e{sbE}%
\e{hjA}\e{hjB}\e{hjC}\e{hjD}\e{hjE}%
\e{hjF}\e{hjG}\e{hjH}\e{hjI}\e{hjJ}%
\e{hjK}\e{hjL}\e{hjM}\e{hjN}\e{hjO}%
\e{hjP}\e{hjQ}\e{hjR}\e{hjS}\e{hjT}%
\endgroup
% Define \select@dbcfont.
\begingroup
\r\@font@info \r\fontname
\r\if \r\ifnum \r\ifx \r\else \r\fi
\r\get@kfont@extname@size \r\kfont@extname \r\kfont@name \r\dbcfont@name
\r\load@dbcfont
\r\iftrace@dbcfont
\r\show \r\showthe
\def\d#1{\xafter\let\xafter \csname dbcfont@#1\endcsname
  \csn\kfont@name @#1\endcsn}
\def\e#1{\xafter\let\xafter \csname dbcfont@#1\endcsname\relax}
\xdef\select@dbcfont#1{\begingroup\escapechar\m@ne
  \edef\kfont@name{#1}\xdef\kfont@name{\xafter\str\kfont@name}\endgroup
  \let\dbcfont@name\kfont@name
  \xafter\get@kfont@extname@size\fontname#1 \relax
  \xafter\if\csn\kfont@extname @map@level\endcsn\relax
    \iftrace@dbcfont{\escapechar"5C%
      \@font@info{#1selected without DBC font}}\fi
  \else
    \d{hgA}\d{hgB}%
    \e{sbA}\e{sbB}\e{sbC}\e{sbD}\e{sbE}%
    \e{hjA}\e{hjB}\e{hjC}\e{hjD}\e{hjE}%
    \e{hjF}\e{hjG}\e{hjH}\e{hjI}\e{hjJ}%
    \e{hjK}\e{hjL}\e{hjM}\e{hjN}\e{hjO}%
    \e{hjP}\e{hjQ}\e{hjR}\e{hjS}\e{hjT}%
    \iftrace@dbcfont{\escapechar"5C%
      \@font@info{Select DBC font #1(\fontname\dbcfont@hgA,deferred,deferred)}}\fi
  \fi
}
\endgroup
% Define \load@dbcfont.
\begingroup
\r\ifx \r\if \r\else \r\fi \r\fontname
\r\@font@info \r\typeout
\r\kfont@size \r\kfont@extname \r\kfont@name
\r\get@kfont@extname@size \r\delete@at \r\empty
\r\ifauto@subst@dbfsize \r\dbfsize@subst \r\iftrace@dbcfont
\xdef\load@dbcfont#1{%
  \xafter\get@kfont@extname@size\fontname#1 \relax
  \xafter\if\csn\kfont@extname @map@level\endcsn\relax
    \iftrace@dbcfont{\escapechar"5C%
      \@font@info{#1loaded without DBC font}}\fi
  \else
    \ifx\kfont@size\empty
      \edef\kfont@size{\csn\kfont@extname @designsize\endcsn pt}%
    \else\edef\kfont@size{\xafter\delete@at\kfont@size\relax}\fi
    \begingroup\escapechar\m@ne \edef\kfont@name{#1}%
    \xdef\kfont@name{\xafter\str\kfont@name}\endgroup
    \ifauto@subst@dbfsize
      \edef\kfont@size{\dbfsize@subst{\kfont@size}}\fi
    \xafter\xdef\csn\kfont@name @size\endcsn{\kfont@size}%
    \xafter\xdef\csn\kfont@name @extname\endcsn{\kfont@extname}%
    \global\xafter\font\csn\kfont@name @hgA\endcsn
      \csn\kfont@extname @db@hgfn\endcsn 1 at\kfont@size
    \hyphenchar\csn\kfont@name @hgA\endcsn\m@ne
    \global\xafter\font\csn\kfont@name @hgB\endcsn
      \csn\kfont@extname @db@hgfn\endcsn 2 at\kfont@size
    \hyphenchar\csn\kfont@name @hgB\endcsn\m@ne
    \iftrace@dbcfont{\escapechar"5C%
      \@font@info{DBC font engine info: Load Hangul for \csn\kfont@name\endcsn}}\fi
  \fi
}
\gdef\delete@at#1#2 #3 \relax{#3}
\gdef\get@kfont@extname@size#1 #2\relax{%
\def\kfont@extname{#1}\def\kfont@size{#2}}
\endgroup
% Define \load@dbcfontsbA,...,\load@dbcfonthjA,...
\begingroup
\r\ifx \r\fi \r\iftrace@dbcfont \r\typeout \r\dbcfont@name
\def\d#1#2{\expandafter\xdef\csname load@dbcfont\f #2\endcsname{%
  \ifx\csname dbcfont@\f #2\endcsname\relax
  \iftrace@dbcfont
    \typeout{DBC font engine info: Load \e #1 for: \csn\dbcfont@name\endcsn}\fi
  \font\csname dbcfont@\f #2\endcsname
  \csn\csn\dbcfont@name @extname\endcsn @db@\f fn\endcsn #1
    at\csn\dbcfont@name @size\endcsn
  \hyphenchar\csname dbcfont@\f #2\endcsname\m@ne
  \global\xafter\let\csn\dbcfont@name @\f #2\endcsn
  \csname dbcfont@\f #2\endcsname\fi}}
\def\e{Symbol}\def\f{sb}
\d 1A\d 2B\d 3C\d 4D\d 5E%
\def\e{Hanja}\def\f{hj}
\d{01}A\d{02}B\d{03}C\d{04}D\d{05}E\d{06}F\d{07}G\d{08}H\d{09}I\d{10}J%
\d{11}K\d{12}L\d{13}M\d{14}N\d{15}O\d{16}P\d{17}Q\d{18}R\d{19}S\d{20}T%
\endgroup
% Define \load@dbcfont@map.
\begingroup
\gdef\load@dbcfont@map{\begingroup
  \def\end@of@input{\end}
  \def\readloop{\read1 to\buf \expandafter\splitbuf\buf \relax
    \ifx\extfn\end@of@input \let\next\relax \else
    \expandafter\xdef\csname\extfn @map@level\endcsname{0}%
    \expandafter\xdef\csname\extfn @designsize\endcsname{\dsize}
    \expandafter\xdef\csname\extfn @db@hgfn\endcsname{\dbhgfn}
    \expandafter\xdef\csname\extfn @db@sbfn\endcsname{\dbsbfn}
    \expandafter\xdef\csname\extfn @db@hjfn\endcsname{\dbhjfn}
    \iftrace@dbcfont{\escapechar"5C%
      \@font@info{Map DBC font \extfn: initial level,%
        (\dbhgfn,\dbsbfn,\dbhjfn)}}\fi
    \let\next=\readloop \fi\next}
  \def\splitbuf ##1,##2,##3,##4,##5 \relax{\def\extfn{##1}\def\dsize{##2}%
    \def\dbhgfn{##3}\def\dbsbfn{##4}\def\dbhjfn{##5}}
  \openin1=dbfont.map \readloop \closein1
  \endgroup}
\endgroup
% Let \documentclass read DBC font mapping table.
\gdef\documentclass{\load@dbcfont@map\let\load@dbcfont@map\relax
  \let\documentclass\@twoclasseserror
  \let\usepackage\RequirePackage
  \@fileswithoptions\@clsextension}
\gdef\documentstyle{\load@dbcfont@map\let\load@dbcfont@map\relax
  \makeatletter\input{latex209.def}\makeatother
  \documentclass}
% Define \MapDBCFont.
\begingroup
\gdef\mapdbcfont@arg@@#1,#2 \relax{\def\extfn{#1}\def\dsize{#2}}
\gdef\mapdbcfont@arg@@@#1,#2,#3 \relax{%
  \def\dbhgfn{#1}\def\dbsbfn{#2}\def\dbhjfn{#3}}
\gdef\MapDBCFont#1#2{\begingroup \load@dbcfont@map
  \mapdbcfont@arg@@#1 \relax \mapdbcfont@arg@@@#2 \relax
  \xdef\levelname{\expandafter\noexpand\csname\extfn @map@level\endcsname}%
  \expandafter\if\levelname\relax \expandafter\def\levelname{0}%
    \else \count\@ne\levelname \advance\count\@ne\@ne
    \expandafter\xdef\levelname{\number\count\@ne}\fi
  \expandafter\xdef\csname\extfn @designsize\endcsname{\dsize}%
  \expandafter\xdef\csname\extfn @db@hgfn\endcsname{\dbhgfn}%
  \expandafter\xdef\csname\extfn @db@sbfn\endcsname{\dbsbfn}%
  \expandafter\xdef\csname\extfn @db@hjfn\endcsname{\dbhjfn}%
  \iftrace@dbcfont{\escapechar"5C%
    \@font@info{Map DBC font \extfn: level=\levelname,%
      (\dbhgfn,\dbsbfn,\dbhjfn)}}\fi
  \endgroup} 
\endgroup
% Define 2-byte characters.
\begingroup
\def\strip#1 {#1}
\def\loopA{\count2="A1\loopB \ifodd\count1\message{.}\fi
  \advance\count1 by1 \ifnum\count1<"C9 \let\n=\loopA\else\let\n=\relax\fi\n}
\def\loopB{\read1 to\h\read1 to\a\read1 to\b\read1 to\c\def\body{}
  \expandafter\xdef\csname\number\count1@\number\count2\endcsname{%
    \expandafter\noexpand\ifcase\expandafter\strip\h \or\dbcfont@hgA\or\dbcfont@hgB\fi
    \ifnum\a=0\else\char\expandafter\strip\a
    \ifnum\b=0\else\char\expandafter\strip\b
    \ifnum\c=0\else\char\expandafter\strip\c\fi\fi\fi}
  \advance\count2 by1 \ifnum\count2<"FF \let\m=\loopB\else\let\m=\relax\fi\m}
\message{Defining 2-byte characters}
\openin1=hcode.dat \count1="B0\loopA \closein1
\endgroup
%% Add 2-byte character handler.
\global\chardef\@xiii@=13\global\chardef\@xii@=12
\global\chardef\dbcp@n@lty=0
\global\newdimen\dbs@pkern \global\dbs@pkern=7sp
\global\newdimen\dbs@pkernchk
\begingroup %Define DBC activator and deactivator.
\def\e{\ifnum\count\@ne<"100
  \edef\d{\d \catcode\the\count\@ne\f}\advance\count\@ne by1
  \else\let\n\relax \fi\n}
\def\f{\@xiii@}\def\d{}\count\@ne="80\let\n\e \e
\global\let\makedbcharactive\d
\def\f{\@xii@}\def\d{}\count\@ne="80\let\n\e \e
\global\let\makedbcharother\d
\endgroup
\makedbcharactive
\begingroup %Define hangul characters.
\r\ifdim \r\fi \r\number
\count\@ne=176
\def\d{\ifdim\lastkern=\dbs@pkernchk\penalty\dbcp@n@lty\fi\unhbox\voidb@x
  \hbox\bgroup\csn\the\count\@ne @\number`##1\endcsn\egroup\kern\dbs@pkern}
\def\e{\advance\count\@ne by\@ne}
\xdef^^b0#1{\d}\e \xdef^^b1#1{\d}\e \xdef^^b2#1{\d}\e \xdef^^b3#1{\d}\e
\xdef^^b4#1{\d}\e \xdef^^b5#1{\d}\e \xdef^^b6#1{\d}\e \xdef^^b7#1{\d}\e
\xdef^^b8#1{\d}\e \xdef^^b9#1{\d}\e \xdef^^ba#1{\d}\e \xdef^^bb#1{\d}\e
\xdef^^bc#1{\d}\e \xdef^^bd#1{\d}\e \xdef^^be#1{\d}\e \xdef^^bf#1{\d}\e
\xdef^^c0#1{\d}\e \xdef^^c1#1{\d}\e \xdef^^c2#1{\d}\e \xdef^^c3#1{\d}\e
\xdef^^c4#1{\d}\e \xdef^^c5#1{\d}\e \xdef^^c6#1{\d}\e \xdef^^c7#1{\d}\e
\xdef^^c8#1{\d}\e
\endgroup
\begingroup %Define symbol and hanja characters.
\def\d#1#2{%
  \noexpand\ifdim\lastkern=\dbs@pkernchk\penalty\dbcp@n@lty\noexpand\fi
  \unhbox\voidb@x \expandafter\noexpand\csname load@dbcfont\fn #1\endcsname
  \hbox\bgroup\expandafter\noexpand\csname dbcfont@\fn #1\endcsname
  \count\@ne=`##1\advance\count\@ne#2\char\count\@ne\egroup\kern\dbs@pkern}
\def\D#1#2#3#4{%
  \noexpand\ifdim\lastkern=\dbs@pkernchk\penalty\dbcp@n@lty\noexpand\fi
  \unhbox\voidb@x
  \noexpand\ifnum`##1<#3
    \expandafter\noexpand\csname load@dbcfont\fn #1\endcsname
    \hbox\bgroup\expandafter\noexpand\csname dbcfont@\fn #1\endcsname
    \count\@ne=`##1\advance\count\@ne#4%
  \noexpand\else \expandafter\noexpand\csname load@dbcfont\fn#2\endcsname
    \hbox\bgroup\expandafter\noexpand\csname dbcfont@\fn #2\endcsname
    \count\@ne=`##1\advance\count\@ne-#3%
  \noexpand\fi \char\count\@ne\egroup\kern\dbs@pkern}
\def\fn{sb}
\xdef^^a1#1{\d A{-"A0}}\xdef^^a2#1{\d A{-"40}}\xdef^^a3#1{\D AB{"E0}{"20}}
\xdef^^a4#1{\d B{-"80}}\xdef^^a5#1{\d B{-"20}}\xdef^^a6#1{\D BC{"C0}{"40}}
\xdef^^a7#1{\d C{-"60}}\xdef^^a8#1{\d C{0}}\xdef^^a9#1{\d D{-"A0}}
\xdef^^aa#1{\d D{-"40}}\xdef^^ab#1{\D DE{"E0}{"20}}\xdef^^ac#1{\d E{-"80}}
\def\fn{hj}
\xdef^^ca#1{\d A{-"A1}}\xdef^^cb#1{\d A{-"43}}\xdef^^cc#1{\D AB{"E5}{"1B}}
\xdef^^cd#1{\d B{-"87}}\xdef^^ce#1{\d B{-"29}}\xdef^^cf#1{\D BC{"CB}{"35}}
\xdef^^d0#1{\d C{-"6D}}\xdef^^d1#1{\d C{-"0F}}\xdef^^d2#1{\D CD{"B1}{"4F}}
\xdef^^d3#1{\d D{-"53}}\xdef^^d4#1{\D DE{"F5}{"0B}}\xdef^^d5#1{\d E{-"97}}
\xdef^^d6#1{\d E{-"39}}\xdef^^d7#1{\D EF{"DB}{"25}}\xdef^^d8#1{\d F{-"7D}}
\xdef^^d9#1{\d F{-"1F}}\xdef^^da#1{\D FG{"C1}{"3F}}\xdef^^db#1{\d G{-"63}}
\xdef^^dc#1{\d G{-"05}}\xdef^^dd#1{\D GH{"A7}{"59}}\xdef^^de#1{\d H{-"49}}
\xdef^^df#1{\D HI{"EB}{"15}}\xdef^^e0#1{\d I{-"8D}}\xdef^^e1#1{\d I{-"2F}}
\xdef^^e2#1{\D IJ{"D1}{"2F}}\xdef^^e3#1{\d J{-"73}}\xdef^^e4#1{\d J{-"15}}
\xdef^^e5#1{\D JK{"B7}{"49}}\xdef^^e6#1{\d K{-"59}}\xdef^^e7#1{\D KL{"FB}{"05}}
\xdef^^e8#1{\d L{-"9D}}\xdef^^e9#1{\d L{-"3F}}\xdef^^ea#1{\D LM{"E1}{"1F}}
\xdef^^eb#1{\d M{-"83}}\xdef^^ec#1{\d M{-"25}}\xdef^^ed#1{\D MN{"C7}{"39}}
\xdef^^ee#1{\d N{-"69}}\xdef^^ef#1{\d N{-"0B}}\xdef^^f0#1{\D NO{"AD}{"53}}
\xdef^^f1#1{\d O{-"4F}}\xdef^^f2#1{\D OP{"F1}{"0F}}\xdef^^f3#1{\d P{-"93}}
\xdef^^f4#1{\d P{-"35}}\xdef^^f5#1{\D PQ{"D7}{"29}}\xdef^^f6#1{\d Q{-"79}}
\xdef^^f7#1{\d Q{-"1B}}\xdef^^f8#1{\D QR{"BD}{"43}}\xdef^^f9#1{\d R{-"5F}}
\xdef^^fa#1{\d R{-"01}}\xdef^^fb#1{\D RS{"A3}{"5D}}\xdef^^fc#1{\d S{-"45}}
\xdef^^fd#1{\D ST{"E7}{"19}}
\endgroup
\endgroup
\makedbcharactive
\DeclareRobustCommand{\hangulbreaking}{\dbs@pkernchk=\dbs@pkern}
\DeclareRobustCommand{\nohangulbreaking}{\dbs@pkernchk=3sp\relax}
\hangulbreaking
\def\makedbcharstring{\makedbcharprotected\let\db@protect\string}
\def\makedbcharnoexpand{\makedbcharprotected\let\db@protect\noexpand}
\def\makedbcharprotected{%
\def^^80{\db@protect^^80}\def^^81{\db@protect^^81}\def^^82{\db@protect^^82}\def^^83{\db@protect^^83}%
\def^^84{\db@protect^^84}\def^^85{\db@protect^^85}\def^^86{\db@protect^^86}\def^^87{\db@protect^^87}%
\def^^88{\db@protect^^88}\def^^89{\db@protect^^89}\def^^8a{\db@protect^^8a}\def^^8b{\db@protect^^8b}%
\def^^8c{\db@protect^^8c}\def^^8d{\db@protect^^8d}\def^^8e{\db@protect^^8e}\def^^8f{\db@protect^^8f}%
\def^^90{\db@protect^^90}\def^^91{\db@protect^^91}\def^^92{\db@protect^^92}\def^^93{\db@protect^^93}%
\def^^94{\db@protect^^94}\def^^95{\db@protect^^95}\def^^96{\db@protect^^96}\def^^97{\db@protect^^97}%
\def^^98{\db@protect^^98}\def^^99{\db@protect^^99}\def^^9a{\db@protect^^9a}\def^^9b{\db@protect^^9b}%
\def^^9c{\db@protect^^9c}\def^^9d{\db@protect^^9d}\def^^9e{\db@protect^^9e}\def^^9f{\db@protect^^9f}%
\def^^a0{\db@protect^^a0}\def^^a1{\db@protect^^a1}\def^^a2{\db@protect^^a2}\def^^a3{\db@protect^^a3}%
\def^^a4{\db@protect^^a4}\def^^a5{\db@protect^^a5}\def^^a6{\db@protect^^a6}\def^^a7{\db@protect^^a7}%
\def^^a8{\db@protect^^a8}\def^^a9{\db@protect^^a9}\def^^aa{\db@protect^^aa}\def^^ab{\db@protect^^ab}%
\def^^ac{\db@protect^^ac}\def^^ad{\db@protect^^ad}\def^^ae{\db@protect^^ae}\def^^af{\db@protect^^af}%
\def^^b0{\db@protect^^b0}\def^^b1{\db@protect^^b1}\def^^b2{\db@protect^^b2}\def^^b3{\db@protect^^b3}%
\def^^b4{\db@protect^^b4}\def^^b5{\db@protect^^b5}\def^^b6{\db@protect^^b6}\def^^b7{\db@protect^^b7}%
\def^^b8{\db@protect^^b8}\def^^b9{\db@protect^^b9}\def^^ba{\db@protect^^ba}\def^^bb{\db@protect^^bb}%
\def^^bc{\db@protect^^bc}\def^^bd{\db@protect^^bd}\def^^be{\db@protect^^be}\def^^bf{\db@protect^^bf}%
\def^^c0{\db@protect^^c0}\def^^c1{\db@protect^^c1}\def^^c2{\db@protect^^c2}\def^^c3{\db@protect^^c3}%
\def^^c4{\db@protect^^c4}\def^^c5{\db@protect^^c5}\def^^c6{\db@protect^^c6}\def^^c7{\db@protect^^c7}%
\def^^c8{\db@protect^^c8}\def^^c9{\db@protect^^c9}\def^^ca{\db@protect^^ca}\def^^cb{\db@protect^^cb}%
\def^^cc{\db@protect^^cc}\def^^cd{\db@protect^^cd}\def^^ce{\db@protect^^ce}\def^^cf{\db@protect^^cf}%
\def^^d0{\db@protect^^d0}\def^^d1{\db@protect^^d1}\def^^d2{\db@protect^^d2}\def^^d3{\db@protect^^d3}%
\def^^d4{\db@protect^^d4}\def^^d5{\db@protect^^d5}\def^^d6{\db@protect^^d6}\def^^d7{\db@protect^^d7}%
\def^^d8{\db@protect^^d8}\def^^d9{\db@protect^^d9}\def^^da{\db@protect^^da}\def^^db{\db@protect^^db}%
\def^^dc{\db@protect^^dc}\def^^dd{\db@protect^^dd}\def^^de{\db@protect^^de}\def^^df{\db@protect^^df}%
\def^^e0{\db@protect^^e0}\def^^e1{\db@protect^^e1}\def^^e2{\db@protect^^e2}\def^^e3{\db@protect^^e3}%
\def^^e4{\db@protect^^e4}\def^^e5{\db@protect^^e5}\def^^e6{\db@protect^^e6}\def^^e7{\db@protect^^e7}%
\def^^e8{\db@protect^^e8}\def^^e9{\db@protect^^e9}\def^^ea{\db@protect^^ea}\def^^eb{\db@protect^^eb}%
\def^^ec{\db@protect^^ec}\def^^ed{\db@protect^^ed}\def^^ee{\db@protect^^ee}\def^^ef{\db@protect^^ef}%
\def^^f0{\db@protect^^f0}\def^^f1{\db@protect^^f1}\def^^f2{\db@protect^^f2}\def^^f3{\db@protect^^f3}%
\def^^f4{\db@protect^^f4}\def^^f5{\db@protect^^f5}\def^^f6{\db@protect^^f6}\def^^f7{\db@protect^^f7}%
\def^^f8{\db@protect^^f8}\def^^f9{\db@protect^^f9}\def^^fa{\db@protect^^fa}\def^^fb{\db@protect^^fb}%
\def^^fc{\db@protect^^fc}\def^^fd{\db@protect^^fd}\def^^fe{\db@protect^^fe}\def^^ff{\db@protect^^ff}}
% Define SelectHangulFont, SelectSymbolFont and SelectHanjaFont.
\gdef\selfont@sub#1#2#3{
  \expandafter\font\csname dbcfont@#2\endcsname=#1#3 at%
    \ifauto@subst@dbfsize\dbfsize@subst{\f@size pt}\else\f@size pt\fi}
\DeclareRobustCommand{\SelectHangulFont}[1]{%
  \selfont@sub{#1}{hgA}1\selfont@sub{#1}{hgB}2} 
\DeclareRobustCommand{\SelectSymbolFont}[1]{%
  \selfont@sub{#1}{sbA}1\selfont@sub{#1}{sbB}2%
  \selfont@sub{#1}{sbC}3\selfont@sub{#1}{sbD}4%
  \selfont@sub{#1}{sbE}5} 
\DeclareRobustCommand{\SelectHanjaFont}[1]{%
  \selfont@sub{#1}{hjA}{01}\selfont@sub{#1}{hjB}{02}%
  \selfont@sub{#1}{hjC}{03}\selfont@sub{#1}{hjD}{04}%
  \selfont@sub{#1}{hjE}{05}\selfont@sub{#1}{hjF}{06}%
  \selfont@sub{#1}{hjG}{07}\selfont@sub{#1}{hjH}{08}%
  \selfont@sub{#1}{hjI}{09}\selfont@sub{#1}{hjJ}{10}%
  \selfont@sub{#1}{hjK}{11}\selfont@sub{#1}{hjL}{12}%
  \selfont@sub{#1}{hjM}{13}\selfont@sub{#1}{hjN}{14}%
  \selfont@sub{#1}{hjO}{15}\selfont@sub{#1}{hjP}{16}%
  \selfont@sub{#1}{hjQ}{17}\selfont@sub{#1}{hjR}{18}%
  \selfont@sub{#1}{hjS}{19}\selfont@sub{#1}{hjT}{20}}
%% Modify part of NFSS2 to load and select fonts for 2-byte characters.
\def\@use@text@encoding#1{%
   \edef\f@encoding{#1}%
   \xdef\font@name{%
      \csname\curr@fontshape/\f@size\endcsname
   }%
   \pickup@font
   \font@name \select@dbcfont\font@name
   \@@enc@update
}
\def\do@subst@correction{%
       \xdef\subst@correction{%
          \font@name \noexpand\select@dbcfont{\font@name}%
          \global\expandafter\font
            \csname \curr@fontshape/\f@size\endcsname
            \noexpand\fontname\font\relax
            \noexpand\load@dbcfont{\csname \curr@fontshape/\f@size\endcsname}%
           \relax}%
       \aftergroup\subst@correction
}
\def\extract@font{%
   \get@external@font
   \global\expandafter\font\font@name\external@font\relax
   \load@dbcfont{\font@name}\relax
    \font@name \relax \select@dbcfont\font@name
    \csname \f@encoding+\f@family\endcsname
    \csname\curr@fontshape\endcsname
    \relax
       }
\DeclareRobustCommand\selectfont
        {%
    \ifx\f@linespread\baselinestretch \else
      \set@fontsize\baselinestretch\f@size\f@baselineskip \fi
    \xdef\font@name{%
      \csname\curr@fontshape/\f@size\endcsname}%
    \pickup@font
    \font@name \select@dbcfont\font@name
    \size@update
    \enc@update
    }
