% font-kor.tex
%
% To be used for Korean characters with ConTeXt.
%
% Last modified on 2002-10-15 by Jin-Hwan Cho <chofchof@ktug.or.kr>
% Last modified on 2006-08-31 by Dohyun Kim <nomosnomos at gmail.com>

\unprotect

\def\handleunicodeflowglyph#1#2%
  {\bgroup
   \edef\unicodeone{#1}%
   \@EA\afterassignment\@EA\dohandleunicodeflowglyph
     \@EA\chardef\@EA\nexttoken\@EA`\string#2\relax}

\def\dohandleunicodeflowglyph\relax
  {\futurelet\nextglyph\dodohandleunicodeflowglyph}

%D \dodohandleunicodeflowglyph
%D added by nomos according to Chof's implementation. see:
%D http://www.ktug.or.kr/jsboard/read.php?table=contrib&no=236&page=1
\def\dodohandleunicodeflowglyph% 
  {\edef\unicodetwo{\the\nexttoken}% 
   \unicodeposition=\unicodeone\unicodetwo\relax
   \handleunicodeglyph
   \egroup}


\def\koreanunicodescale   {1.00}
\def\koreanunicodeheight  {1.00}
\def\koreanunicodedepth   {1.00}
\def\koreaninterglyphskip {0pt plus .05em minus .01em}

\def\handlekoreanunicodeglyph%
  {% right
   \ifx '\nextglyph \chardef\koreanAstatus=1 \else
   \ifx ,\nextglyph \chardef\koreanAstatus=1 \else
   \ifx .\nextglyph \chardef\koreanAstatus=1 \else
   \ifx ?\nextglyph \chardef\koreanAstatus=1 \else
   \ifx ;\nextglyph \chardef\koreanAstatus=1 \else
   \ifx :\nextglyph \chardef\koreanAstatus=1 \else
   \ifx !\nextglyph \chardef\koreanAstatus=1 \else
   \ifx )\nextglyph \chardef\koreanAstatus=1 \else
   \ifx ]\nextglyph \chardef\koreanAstatus=1 \else
                    \chardef\koreanAstatus=0
   \fi\fi\fi\fi\fi\fi\fi\fi\fi
   \insertunicodeglyph
   \ifcase\koreanAstatus\relax\hskip\koreaninterglyphskip\fi}

%D \c!scale, \c!height, \c!depth, \c!command
%D corrected by nomos for compatibility with modern context
\setupunicodefont
  [korean]
  [   \c!scale=\koreanunicodescale,
      \c!height=\koreanunicodeheight,
      \c!depth=\koreanunicodedepth,
    \c!command=\handlekoreanunicodeglyph]

% KSCms mapping on linear range
%
% \starttyping
% if oldplane>0xa0
%   if oldposition>0xa0
%     slot = (oldplane-0xa1)*((0xfe-0xa1)+1) + oldposition
%   else
%     if oldposition>0x80
%       dec(oldposition,0x41+(0x80-0x7b+1)+(0x60-0x5b+1))
%     else if oldposition>0x60
%       dec(oldposition,0x41+(0x60-0x5b+1))
%     else
%       dec(oldposition,0x41)
%     fi
%     slot = (oldplane-0xa1)*((0xa0-0x81)+(0x7a-0x61)+(0x6a-0x41)+3)
%            + oldposition + 35 * 256 + (0xa0-0x81+1) * 178
%   fi
% else  
%   if oldposition>0x80
%     dec(oldposition,0x41+(0x80-0x7b+1)+(0x60-0x5b+1))
%   else if oldposition>0x60
%     dec(oldposition,0x41+(0x60-0x5b+1))
%   else
%     dec(oldposition,0x41)
%   fi
%   slot = (oldplane-0x81)*((0xfe-0x81)+(0x7a-0x61)+(0x6a-0x41)+3)
%          + oldposition + 35 * 256
% fi
% newplane = (slot div 0x100) + 1
% newposition = (slot mod 0x100)
% \stoptyping
%
% \defineucharmapping{KSCms}#1#2%
%   {\unicodeposition=#1
%    \ifnum#1>160
%      \ifnum#2>160
%        \advance\unicodeposition-161
%        \multiply\unicodeposition 94
%        \advance\unicodeposition #2
%        \advance\unicodeposition-161
%      \else
%        \advance\unicodeposition-161
%        \multiply\unicodeposition 84
%        \advance\unicodeposition #2
%        \advance\unicodeposition-\ifnum#2>128 77\else\ifnum#2>96 71\else65\fi\fi
%        \advance\unicodeposition 14656
%      \fi
%    \else
%      \advance\unicodeposition-129
%      \multiply\unicodeposition 178
%      \advance\unicodeposition #2
%      \advance\unicodeposition-\ifnum#2>128 77\else\ifnum#2>96 71\else65\fi\fi
%      \advance\unicodeposition 8960
%    \fi
%    \dorepositionunicode}

\defineucharmapping{KSCms}#1#2%
  {\unicodeposition=#1
   \advance\unicodeposition-\ifnum#1>160 161\else129\fi
   \multiply\unicodeposition\ifnum#1>160 \ifnum#2>160 94\else84\fi\else178\fi
   \advance\unicodeposition #2
   \advance\unicodeposition-\ifnum#2>128 77\else\ifnum#2>96 71\else65\fi\fi
   \advance\unicodeposition\ifnum#1>160 \ifnum#2>160 -84\else14656\fi\else8960\fi
   \dorepositionunicode}

% HLaTeX mapping
%
%\defineucharmapping{HLaTeX}#1#2%
%  {\unicodeposition#1
%   \ifnum#1>201 % Chinese characters (0xca-0xfd)
%     \advance\unicodeposition-202
%     \multiply\unicodeposition 94
%     \advance\unicodeposition #2
%     \advance\unicodeposition-161
%     \advance\unicodeposition 2304
%     \dorepositionunicode
%   \else\ifnum#1>175 % Korean characters (0xb0-0xc9)
%     \advance\unicodeposition-176
%     \multiply\unicodeposition 94
%     \advance\unicodeposition #2
%     \advance\unicodeposition-161
%     \DoDiv\unicodeposition by256to\scratchcounter
%     \edef\unicodeone{\the\scratchcounter}%
%     \DoMod\unicodeposition by256to\scratchcounter
%     \edef\unicodetwo{\the\scratchcounter}%
%   \else % Symbol characters (0xa1-0xac)
%     \unicodeposition#2
%     \ifnum#1=161 % 0xa1a2-0xa1fe
%       \advance\unicodeposition-162 % - 0xa2 + 0x00
%       \advance\unicodeposition 1024 \fi % 4 * 256 for 05
%     \ifnum#1=162 % 0xa2a1-0xa2e5
%       \advance\unicodeposition-68 % - 0xa1 + 0x5d
%       \advance\unicodeposition 1024 \fi % 4 * 256 for 05
%     \ifnum#1=163 % 0xa3
%       \ifnum#2<220 % 0xdc; for 0xa3a1-0xa3db
%         \advance\unicodeposition-161 % - 0xa1 + 0x00
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else\ifnum#2=220 % 0xdc; for 0xa3dc
%         \advance\unicodeposition-169 % - 0xdc + 0x33
%         \advance\unicodeposition 768 % 3 * 256 for 04
%       \else % 0xa3dd-0xa3fe
%         \advance\unicodeposition-162 % - 0xdd + 0x3b
%         \advance\unicodeposition 1536 \fi\fi\fi % 6 * 256 for 07
%     \ifnum#1=164 % 0xa4a1-0xa4fe (except for 0xa4d4 instead 0xa3dc)
%       \advance\unicodeposition-161 % - 0xa1 + 0x00
%       \advance\unicodeposition 768 \fi % 3 * 256 for 04
%     \ifnum#1=165 % 0xa5
%       \ifnum#2<171 % 0xab; for 0xa5a1-0xa5aa
%         \advance\unicodeposition-68 % - 0xa1 + 0x5d
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else\ifnum#2<186 % 0xba; for 0xa5b0-0xa5b9
%         \advance\unicodeposition-73 % - 0xb0 + 0x67
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else\ifnum#2<217 % 0xd9; for 0xa5c1-0xa5d8
%         \advance\unicodeposition-3 % - 0xc1 + 0xbe
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else % 0xa5e1-0xa5f8
%         \advance\unicodeposition-88 % - 0xe1 + 0x89
%         \advance\unicodeposition 1536 \fi\fi\fi\fi % 6 * 256 for 07
%     \ifnum#1=166 % 0xa6a1-0xa6e4
%       \advance\unicodeposition-161 % - 0xa1 + 0x00
%       \advance\unicodeposition 1280 \fi % 5 * 256 for 06
%     \ifnum#1=167 % 0xa7a1-0xa7ef
%       \advance\unicodeposition 1 % - 0xa1 + 0xa2
%       \advance\unicodeposition 1024 \fi % 4 * 256 for 05
%     \ifnum#1=168 % 0xa8
%       \ifnum#2<165 % 0xa5; for 0xa8a1-0xa8a4
%%        \advance\unicodeposition 0 % - 0xa1 + 0xa1
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else\ifnum#2=166 % 0xa8a6
%         \advance\unicodeposition-1 % - 0xa6 + 0xa5
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else\ifnum#2<176 % 0xb0; for 0xa8a8-0xa8af
%         \advance\unicodeposition-2 % - 0xa8 + 0xa6
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else\ifnum#2<246 % 0xf6; for 0xa8b1-0xa8f5
%         \advance\unicodeposition-83 % - 0xb1 + 0x5e
%         \advance\unicodeposition 768 % 3 * 256 for 04
%       \else % 0xa8f6-0xa8fe
%         \advance\unicodeposition-178 % - 0xf6 + 0x44
%         \advance\unicodeposition 1280 \fi\fi\fi\fi\fi % 5 * 256 for 06
%     \ifnum#1=169 % 0xa9
%       \ifnum#2<177 % 0xb1; for 0xa9a1-0xa9b0
%         \advance\unicodeposition 13 % - 0xa1 + 0xae
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else\ifnum#2<246 % 0xf6; for 0xa9b1-0xa9f5
%         \advance\unicodeposition-14 % - 0xb1 + 0xa3
%         \advance\unicodeposition 768 % 3 * 256 for 04
%       \else % 0xa9f6-0xa9fe
%         \advance\unicodeposition-169 % - 0xf6 + 0x4d
%         \advance\unicodeposition 1280 \fi\fi\fi % 5 * 256 for 06
%     \ifnum#1=170 % 0xaaa1-0xaaf3
%       \advance\unicodeposition-75 % - 0xa1 + 0x56
%       \advance\unicodeposition 1280 \fi % 5 * 256 for 06
%     \ifnum#1=171 % 0xaba1-0xabf6
%       \advance\unicodeposition 8 % - 0xa1 + 0xa9
%       \advance\unicodeposition 1280 \fi % 5 * 256 for 06
%     \ifnum#1=172 % 0xac
%       \ifnum#2<194 % 0xc2; for 0xaca1-0xacc1
%         \advance\unicodeposition 29 % - 0xa1 + 0xbe
%         \advance\unicodeposition 1536 % 6 * 256 for 07
%       \else % for 0xacd1-0xacf1
%         \advance\unicodeposition 14 % -0xd1 + 0xdf
%         \advance\unicodeposition 1536 \fi\fi % 6 * 256 for 07
%     \dorepositionunicode\fi\fi}

\defineucharmapping{HLaTeX}#1#2%
  {\ifnum#1>175
     \unicodeposition#1
     \advance\unicodeposition-\ifnum#1>201 202 \else176\fi
     \multiply\unicodeposition 94
     \advance\unicodeposition #2
     \advance\unicodeposition-161
     \ifnum#1>201 % Chinese characters (0xca-0xfd)
       \advance\unicodeposition 2304
       \dorepositionunicode
     \else % Korean characters (0xb0-0xc9)
       \DoDiv\unicodeposition by256to\scratchcounter
       \edef\unicodeone{\the\scratchcounter}%
       \DoMod\unicodeposition by256to\scratchcounter
       \edef\unicodetwo{\the\scratchcounter}%
     \fi
   \else % Symbol characters (0xa1-0xac)
     \ifnum#1=161        \unicodeposition  862 \fi % 0xa1a2-0xa1fe
     \ifnum#1=162        \unicodeposition  956 \fi % 0xa2a1-0xa2e5
     \ifnum#1=163
            \ifnum#2<220 \unicodeposition 1375     % 0xa3a1-0xa3db
       \else\ifnum#2=220 \unicodeposition  599     % 0xa3dc
       \else             \unicodeposition 1374     % 0xa3dd-0xa3fe
       \fi\fi\fi
     \ifnum#1=164        \unicodeposition  607 \fi % 0xa4a1-0xa4d3,0xa4d5-0xa4fe
     \ifnum#1=165
            \ifnum#2<171 \unicodeposition 1468     % 0xa5a1-0xa5aa
       \else\ifnum#2<186 \unicodeposition 1463     % 0xa5b0-0xa5b9
       \else\ifnum#2<217 \unicodeposition 1533     % 0xa5c1-0xa5d8
       \else             \unicodeposition 1448     % 0xa5e1-0xa5f8
       \fi\fi\fi\fi
     \ifnum#1=166        \unicodeposition 1119 \fi % 0xa6a1-0xa6e4
     \ifnum#1=167        \unicodeposition 1025 \fi % 0xa7a1-0xa7ef
     \ifnum#1=168
            \ifnum#2<165 \unicodeposition 1536     % 0xa8a1-0xa8a4
       \else\ifnum#2=166 \unicodeposition 1335     % 0xa8a6
       \else\ifnum#2<176 \unicodeposition 1334     % 0xa8a8-0xa8af
       \else\ifnum#2<246 \unicodeposition  685     % 0xa8b1-0xa8f5
       \else             \unicodeposition 1102     % 0xa8f6-0xa8fe
       \fi\fi\fi\fi\fi
     \ifnum#1=169
            \ifnum#2<177 \unicodeposition 1549     % 0xa9a1-0xa9b0
       \else\ifnum#2<246 \unicodeposition  754     % 0xa9b1-0xa9f5
       \else             \unicodeposition 1111     % 0xa9f6-0xa9fe
       \fi\fi\fi
     \ifnum#1=170        \unicodeposition 1205 \fi % 0xaaa1-0xaaf3
     \ifnum#1=171        \unicodeposition 1288 \fi % 0xaba1-0xabf6
     \ifnum#1=172
            \ifnum#2<194 \unicodeposition 1565     % 0xaca1-0xacc1
       \else             \unicodeposition 1550     % 0xacd1-0xacf1
       \fi\fi
     \advance\unicodeposition #2
     \dorepositionunicode\fi}

%\def\currentucharmapping{KSCms}
\def\currentucharmapping{HLaTeX}

\definefontsynonym [KSCmsBatangRegular]     [kscbt]
\definefontsynonym [KSCmsBatangSlanted]     [kscbto]
\definefontsynonym [KSCmsBatangItalic]      [kscbto]
\definefontsynonym [KSCmsBatangBold]        [kscbt]
\definefontsynonym [KSCmsBatangBoldSlanted] [kscbto]
\definefontsynonym [KSCmsBatangBoldItalic]  [kscbto]

%\defineunicodefont [Batang] [KSCmsBatang] [korean]

\definefontsynonym [HLMyoungjoRegular]     [wmj]
\definefontsynonym [HLMyoungjoSlanted]     [wmjo]
\definefontsynonym [HLMyoungjoItalic]      [wmjo]
\definefontsynonym [HLMyoungjoBold]        [wmjb]
\definefontsynonym [HLMyoungjoBoldSlanted] [wmjbo]
\definefontsynonym [HLMyoungjoBoldItalic]  [wmjbo]

\definefontsynonym [HLGothicRegular]       [wgt]
\definefontsynonym [HLGothicSlanted]       [wgto]
\definefontsynonym [HLGothicItalic]        [wgto]
\definefontsynonym [HLGothicBold]          [wgtb]
\definefontsynonym [HLGothicBoldSlanted]   [wgtbo]
\definefontsynonym [HLGothicBoldItalic]    [wgtbo]

\definefontsynonym [HLTazaRegular]         [wtt]
\definefontsynonym [HLTazaSlanted]         [wtto]
\definefontsynonym [HLTazaItalic]          [wtto]
\definefontsynonym [HLTazaBold]            [wtt]
\definefontsynonym [HLTazaBoldSlanted]     [wtto]
\definefontsynonym [HLTazaBoldItalic]      [wtto]

\defineunicodefont [Batang] [HLMyoungjo] [korean]
\defineunicodefont [Dotum]  [HLGothic]   [korean]
\defineunicodefont [Taja]   [HLTaza]     [korean]

%D for compatibility with font-kor.tex 
%D added by nomos
\let\Taza\Taja

\Batang


\protect \endinput
