
\unprotect

\useencoding[chi]

\chardef\koreanSstatus=0 % 0=unknown 1=left 2>=right 
\def\analyzekoreanglyph % beware, no zerocount !
  {\chardef\koreanSstatus0\getvalue{uc\number\unicodeposition}\relax}

\newdimen\interhangulglyph\interhangulglyph -.03266em
\mathchardef\hangulspacefactor=1001
\mathchardef\asciiparenopen=998

\sfcode`\(=\asciiparenopen
\sfcode`\[=\asciiparenopen
\sfcode`\`=\asciiparenopen

\expandafter\def\expandafter\quoteleft\expandafter{\quoteleft
  \ifhmode\spacefactor=\asciiparenopen\fi}
\expandafter\def\expandafter\quotedblleft\expandafter{\quotedblleft
  \ifhmode\spacefactor=\asciiparenopen\fi}

\def\handlekoreanunicodeglyph
  {%
  \analyzekoreanglyph
  \ifhmode
    \ifdim\lastskip=\zeropoint
      \ifnum\lastpenalty<10000 % added 20070206
        \ifnum\spacefactor>\asciiparenopen
          \ifnum\koreanSstatus<2 % RIGHT가 아니면
            \ifnum\spacefactor=\hangulspacefactor
              \discretionary{}{}{\kern\interhangulglyph}%
            \else
              \hskip\zeropoint\relax
            \fi
          \fi
        \fi
      \fi
    \fi
  \fi
  %
  \insertunicodeglyph
  %
  \ifx(\nextutoken
    \hskip\zeropoint\relax
  \else
    \ifx[\nextutoken
      \hskip\zeropoint\relax
    \fi
  \fi
  %
  \ifhmode  
    \ifnum\koreanSstatus=1 % LEFT이면 
      \spacefactor=\asciiparenopen
    \else
      \spacefactor=\hangulspacefactor
    \fi
  \fi 
  }

%%%% pdftex에서 한글 검색·추출
%%%\ifx\pdfoutput\undefined
%%%\else
%%%  \ifx\pdfoutput\relax
%%%  \else
%%%    \ifnum\pdfoutput>0
%%%      \def\unicodecharcommand#1%
%%%        {#1\expandafter\ifx\csname DHFontName-\fontname\font.\endcsname\relax
%%%         \AddUniSubCmap\unicodefont\unicodeone
%%%         \expandafter\gdef\csname DHFontName-\fontname\font.\endcsname{}\fi
%%%        }
%%%      \def\AddUniSubCmap#1#2%
%%%        {\expandafter\ifx\csname DHucs-CMap-#2\endcsname\relax
%%%           \immediate\pdfobj stream {%
%%%             /CIDInit /ProcSet findresource begin
%%%             12 dict begin begincmap
%%%             /CIDSystemInfo << /Registry (TeX)
%%%               /Ordering (Uni#2) /Supplement 0 >> def
%%%             /CMapName /TeX-Uni#2-0 def
%%%             /CMapType 2 def
%%%             1 begincodespacerange <00> <FF> endcodespacerange
%%%             1 beginbfrange <00> <FF> <#200> endbfrange
%%%             endcmap CMapName currentdict /CMap defineresource pop end end }%
%%%           \expandafter\xdef\csname DHucs-CMap-#2\endcsname{\the\pdflastobj}%
%%%         \fi
%%%         \pdffontattr#1{/ToUnicode \csname DHucs-CMap-#2\endcsname\space 0 R}%
%%%        }
%%%    \fi
%%%  \fi
%%%\fi

\def\koreanunicodescale   {1.00}
\def\koreanunicodeheight  {1.00}
\def\koreanunicodedepth   {1.00}

\setupunicodefont
  [korean]
  [ \c!scale=\koreanunicodescale,
    \c!height=\koreanunicodeheight,
    \c!depth=\koreanunicodedepth,
    \c!command=\handlekoreanunicodeglyph]

\definefontsynonym [UnicodeBatangRegular]     [outbtm]  [encoding=cjk-uni]
\definefontsynonym [UnicodeBatangSlanted]     [outbtmo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeBatangItalic]      [outbtmo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeBatangBold]        [outbtb]  [encoding=cjk-uni]
\definefontsynonym [UnicodeBatangBoldSlanted] [outbtbo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeBatangBoldItalic]  [outbtbo]  [encoding=cjk-uni]

\definefontsynonym [UnicodeDotumRegular]      [outgtm]  [encoding=cjk-uni]
\definefontsynonym [UnicodeDotumSlanted]      [outgtmo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeDotumItalic]       [outgtmo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeDotumBold]         [outgtb]  [encoding=cjk-uni]
\definefontsynonym [UnicodeDotumBoldSlanted]  [outgtbo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeDotumBoldItalic]   [outgtbo]  [encoding=cjk-uni]

\definefontsynonym [UnicodeTazaRegular]       [outtzm]  [encoding=cjk-uni]
\definefontsynonym [UnicodeTazaSlanted]       [outtzmo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeTazaItalic]        [outtzmo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeTazaBold]          [outtzm]  [encoding=cjk-uni]
\definefontsynonym [UnicodeTazaBoldSlanted]   [outtzmo]  [encoding=cjk-uni]
\definefontsynonym [UnicodeTazaBoldItalic]    [outtzmo]  [encoding=cjk-uni]


\ifx\pdfoutput\undefined
\else
  \ifx\pdfoutput\relax
  \else
    \ifnum\pdfoutput>0
      \loadmapfile [kotex-base.map]
    \fi
  \fi
\fi

\defineunicodefont [Batang] [UnicodeBatang] [korean]
\defineunicodefont [Dotum]  [UnicodeDotum]  [korean]
\defineunicodefont [Taza]   [UnicodeTaza]   [korean]

\let\rm!original\rm
\let\ss!original\ss
\let\tt!original\tt
\let\hw!original\hw
\let\cg!original\cg

\def\SetHangulFonts#1#2#3% 손글씨와 붓글씨는 일단 고딕으로. 나중에 수정하자.
  {\def\rm{\rm!original #1}%
   \def\ss{\ss!original #2}%
   \def\tt{\tt!original #3}%
   \def\hw{\hw!original #2}% 손글씨
   \def\cg{\cg!original #2}% 붓글씨
  }

% 한글폰트셋 지정 사용자 인터페이스: \SetHangulFonts{명조}{고딕}{타자}
% 물론 \Pilgi 따위의 임시글꼴을 만들어 문서 중에 사용할 수도 있다.
\SetHangulFonts\Batang\Dotum\Taza

\Batang

\protect

