%D \module
%D   [      file=t-zhspacing,
%D        version=2009.05.11,
%D          title=\CONTEXT\ User Module,
%D       subtitle=Zh-Spacing,
%D         author=Yue Wang,
%D           date=\currentdate,
%D      copyright=Yue Wang
%D          email=yuleopen@gmail.com,
%D        license=BSD]

\writestatus{loading}{Context User Module / Zh-Spacing}
\unprotect

%D Macro package \type{zhspacing} was written by Yin Dian in order to 
%D typeset Simplified Chinese in \XeTeX. The macro package uses \XeTeX's
%D inter-char token mechanism in order to support the basic Chinese 
%D typesetting rules. This macro aims to port \type{zhspacing} to Hans
%D Hagen's \ConTeXt\ format.

%D \ConTeXt\ MKII does not load unicode-letters.tex automatically
%D when the format is dumped. What's worse, standard \ConTeXt\ 
%D distribution does not have unicode-letters.tex included. 
%D So in order to define all the default \XeTeX\ character classes
%D as in plain \TeX\ and \LaTeX, we striped the related macros
%D from unicode-letters.tex to \type{zhspacing-unicode}.


\def\ID #1 #2 {\set{#1}{#2}{\global\XeTeXcharclass\n=1 \global\catcode\n=11 }}
\countdef\n=255
\def\set#1#2#3{\n="#1 \loop #3 \ifnum\n<"#2 \advance\n by 1 \repeat}
\def\loop#1\repeat{\def\body{#1}\iterate}
\def\iterate{\body \let\next\iterate \else\let\next\relax\fi \next}
\let\repeat=\fi \ID 3400 4DB5 \ID 4E00 9FC3 \ID 20000 2A6D6

%D \ConTeXt\ does not have the right catcode for zhspacing.sty defined.
%D So we should define the catcode correctly in order to load it.

\catcode`\!=12
\catcode`\@=11
\catcode`\?=12


%D zhspacing uses \TeX\ commands \type{\lq} and \type{\rq} in plain TeX.
%D However, these are not defined in \ConTeXt. So we define the two macros.

\def\lq{`} \def\rq{'}

%D And now we can load \type{zhspacing.sty} safely.

\input zhspacing.sty


%D Last thing: we define the typescript for Adobe Chinese fonts.

\starttypescript [serif] [myzhfont]
  \definefontsynonym [Serif]           [ZhSerif]
  \definefontsynonym [SerifBold]       [ZhSerifBold]
  \definefontsynonym [SerifItalic]     [ZhSerifItalic]
  \definefontsynonym [SerifBoldItalic] [ZhSerifBoldItalic]
\stoptypescript

\starttypescript [serif] [myzhfont]
  \definefontsynonym [ZhSerif]           [name:AdobeSongStd-Light]   
  \definefontsynonym [ZhSerifBold]       [name:AdobeHeitiStd-Regular]
  \definefontsynonym [ZhSerifItalic]     [name:AdobeKaitiStd-Regular]
  \definefontsynonym [ZhSerifBoldItalic] [name:AdobeHeitiStd-Regular]
\stoptypescript

\starttypescript [sans] [myzhfont]
  \definefontsynonym [Sans]           [ZhSans]           
  \definefontsynonym [SansBold]       [ZhSansBold]       
  \definefontsynonym [SansItalic]     [ZhSansItalic]     
  \definefontsynonym [SansBoldItalic] [ZhSansBoldItalic] 
\stoptypescript

\starttypescript [sans] [myzhfont]
  \definefontsynonym [ZhSans]           [name:AdobeKaitiStd-Regular] 
  \definefontsynonym [ZhSansBold]       [name:AdobeHeitiStd-Regular] 
  \definefontsynonym [ZhSansItalic]     [name:AdobeKaitiStd-Regular] 
  \definefontsynonym [ZhSansBoldItalic] [name:AdobeHeitiStd-Regular] 
\stoptypescript

\starttypescript [mono] [myzhfont]
  \definefontsynonym [Mono]           [ZhMono]          
  \definefontsynonym [MonoBold]       [ZhMonoBold]      
  \definefontsynonym [MonoItalic]     [ZhMonoItalic]    
  \definefontsynonym [MonoBoldItalic] [ZhMonoBoldItalic]
\stoptypescript

\starttypescript [mono] [myzhfont]
  \definefontsynonym [ZhMono]           [name:AdobeFangsongStd-Regular]
  \definefontsynonym [ZhMonoBold]       [name:AdobeHeitiStd-Regular]   
  \definefontsynonym [ZhMonoItalic]     [name:AdobeFangsongStd-Regular]
  \definefontsynonym [ZhMonoBoldItalic] [name:AdobeHeitiStd-Regular]   
\stoptypescript


\starttypescript[myzhfont]
  \definetypeface [myzhfont] [rm] [serif] [myzhfont] [default]
  \definetypeface [myzhfont] [ss] [sans]  [myzhfont] [default]
  \definetypeface [myzhfont] [tt] [mono]  [myzhfont] [default]
\stoptypescript

\usetypescript[myzhfont]

%D We set all the \type{zhxxxfont} used in \type{zhspacing.sty}.

\zhspacing
\def\zhongwen{\pushcurrentfont\myzhfont\popcurrentfont}
\def\zhfont{\zhongwen}
\def\zhpunctfont{\zhongwen}
\def\zhcjkextafont{\zhongwen}
\def\zhcjkextbfont{\zhongwen}


\protect