%
% cct.sty
% (this package is intended to replace CCT's preprocessor `cct.exe')
%
% version 0.0.9, by ZLB.
%

\edef\OldCatcodeOfAt!{\the\catcode`\@}			% save catcode of @
\catcode`\@=11
\newcount\@CCTcnta
\ifx\protect\undefined \let\protect=\relax \fi

%
% Define the first byte of a Chinese character as a macro with
% the second byte as its argument.
% (\protect's are needed for LaTeX's cross reference to work properly)
%
\def\@CCTMapChar#1{\def#1##1{\protect \CCTSetChar \protect #1 \protect ##1}}

%
% \@CCTSetChar <byte1> <byte2>				(\contentsline, etc.)
% \@CCTSetChar \protect <byte1> \protect <byte2>	(normal text)
%
% Note: the second `\expandafter' causes `\else ... \fi' to be skipped
%	(otherwise they become arguments of `\@CCTSetCharp').
% FIXME: <newline> before `\else' seems to cause trouble, why?
%
\def\CCTSetChar#1#2{%
  \ifx #1\protect
    \expandafter\@CCTSetChar\expandafter#2\else\@CCTSetChar #1 a #2\fi}

%
% Macro which actually typesets a Chinese character:
%   #1=<byte1>, #2=\protect or a dummy 'a', #3=<byte2>
%
% TODO: special actions for punctuation characters
%	(hint: whose first byte is either 0xa1 or 0xa3)
%
\chardef\@CCTote=128
\def\@CCTSetChar#1#2#3{{%
  \@CCTpre #1 #3%
  \@CCTcnta=\number`#1\relax
  \advance\@CCTcnta by -\@CCTote
  \char\@CCTcnta
  \@CCTcnta=\number`#3\relax
  \ifnum \@CCTcnta < \@CCTote
    \advance\@CCTcnta by \@CCTote 
  \else 
    \advance\@CCTcnta by -\@CCTote 
  \fi
  \char\@CCTcnta
  \@CCTpost #1 #3%
}}

\catcode`\!=11

%
% Note: removing \!SetZiju from \CC saves >%60 of processing time, should
%	check if it's really necessary (may also remove it from cchead.sty)
%
% We use \lastkern=1sp to tell that the previous char is a Chinese char,
% and the glue and line break restrictions are defined in \@CCTglue and
% \@CCTnobreak.
%
% This is a hack and it's based on the assumption that ordinary TeX macro
% packages/fonts should not generate \kern 1sp.
%

\newdimen\@CCTshrink \@CCTshrink=0pt
\def\@CCTnobreak{0}

\def\@CCTpre#1#2{%
  % \@CCTcnta == 1 ==> previous character is a Chinese character
  \@CCTcnta\lastkern 
  \ifnum\@CCTcnta=1
    \unkern \unkern \unskip
  \else
    \def\@CCTnobreak{0}\@CCTshrink=0pt
  \fi
  \ccdummy\!ZiTi\DaXiao
  % Warning: the \@CCTpre macro should preserve \@CCTcnta
  \csname @CCTpre\string #1\string #2\endcsname
  \if\@CCTnobreak 1\penalty 10000 \def\@CCTnobreak{0}\fi
  % if previous char is a Chinese char, insert a space here
  \ifnum\@CCTcnta=1 { }\fi
  \ifdim\@CCTshrink>0pt
    \hskip 0pt plus 0pt minus \@CCTshrink 
    \@CCTshrink=0pt%
  \fi
}

\def\@CCTpost#1#2{\csname @CCTpost\string #1\string #2\endcsname 
  \kern -1sp\kern 1sp}

\catcode`\!=12

\def\@CCTpunct#1#2#3#4#5#6{%
  \ifnum #3#4=0\else
    \expandafter\def\csname @CCTpre\string #1\string #2\endcsname{%
      \if#31\def\@CCTnobreak{1}\fi
      \if#41\advance\@CCTshrink by 0.5\ccwd\fi
    }%
  \fi
  \ifnum #3#4=0\else
    \expandafter\def\csname @CCTpost\string #1\string #2\endcsname{%
      \if#51\def\@CCTnobreak{1}\fi
      % Note: we insert the glue here otherwise it might be discarded 
      % if the next char is not a Chinese char. The glue amount is also
      % added to \@CCTshrink (thus the \@CCTpre macro of the next char
      % can detect this glue by testing \lastkern==1 and \@CCTshrink>0,
      % and undo this skip in order to insert penalties)
      \if#61\hskip 0pt plus 0pt minus 0.5\ccwd \@CCTshrink=0.5\ccwd \fi
    }%
  \fi
}

%
% Table of punctuation chars.
%
%			   before	   after
%		char	nobreak	glue 	nobreak	glue
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	1
\@CCTpunct	��	1	0	0	0
\@CCTpunct	��	1	0	0	0
\@CCTpunct	��	1	0	0	0
\@CCTpunct	��	1	0	0	0
\@CCTpunct	��	1	0	0	0
%
\@CCTpunct	��	0	1	1	0
\@CCTpunct	��	0	1	1	0
\@CCTpunct	��	0	1	1	0
\@CCTpunct	��	0	1	1	0
\@CCTpunct	��	0	1	1	0
\@CCTpunct	��	0	1	1	0
\@CCTpunct	��	0	0	1	0
\@CCTpunct	��	0	0	1	0
\@CCTpunct	��	0	0	1	0
\@CCTpunct	��	0	0	1	0
%
% Make chars 128-255 active
%
\@CCTcnta "80\relax
\loop
  \catcode\@CCTcnta \active
  \ifnum\@CCTcnta < "0FF \advance\@CCTcnta \@ne
\repeat

%
% FIXME: implement lines below within the loop above
%
\@CCTMapChar{^^80} \@CCTMapChar{^^81} \@CCTMapChar{^^82} \@CCTMapChar{^^83}
\@CCTMapChar{^^84} \@CCTMapChar{^^85} \@CCTMapChar{^^86} \@CCTMapChar{^^87}
\@CCTMapChar{^^88} \@CCTMapChar{^^89} \@CCTMapChar{^^8a} \@CCTMapChar{^^8b}
\@CCTMapChar{^^8c} \@CCTMapChar{^^8d} \@CCTMapChar{^^8e} \@CCTMapChar{^^8f}
\@CCTMapChar{^^90} \@CCTMapChar{^^91} \@CCTMapChar{^^92} \@CCTMapChar{^^93}
\@CCTMapChar{^^94} \@CCTMapChar{^^95} \@CCTMapChar{^^96} \@CCTMapChar{^^97}
\@CCTMapChar{^^98} \@CCTMapChar{^^99} \@CCTMapChar{^^9a} \@CCTMapChar{^^9b}
\@CCTMapChar{^^9c} \@CCTMapChar{^^9d} \@CCTMapChar{^^9e} \@CCTMapChar{^^9f}
\@CCTMapChar{^^a0} \@CCTMapChar{^^a1} \@CCTMapChar{^^a2} \@CCTMapChar{^^a3}
\@CCTMapChar{^^a4} \@CCTMapChar{^^a5} \@CCTMapChar{^^a6} \@CCTMapChar{^^a7}
\@CCTMapChar{^^a8} \@CCTMapChar{^^a9} \@CCTMapChar{^^aa} \@CCTMapChar{^^ab}
\@CCTMapChar{^^ac} \@CCTMapChar{^^ad} \@CCTMapChar{^^ae} \@CCTMapChar{^^af}
\@CCTMapChar{^^b0} \@CCTMapChar{^^b1} \@CCTMapChar{^^b2} \@CCTMapChar{^^b3}
\@CCTMapChar{^^b4} \@CCTMapChar{^^b5} \@CCTMapChar{^^b6} \@CCTMapChar{^^b7}
\@CCTMapChar{^^b8} \@CCTMapChar{^^b9} \@CCTMapChar{^^ba} \@CCTMapChar{^^bb}
\@CCTMapChar{^^bc} \@CCTMapChar{^^bd} \@CCTMapChar{^^be} \@CCTMapChar{^^bf}
\@CCTMapChar{^^c0} \@CCTMapChar{^^c1} \@CCTMapChar{^^c2} \@CCTMapChar{^^c3}
\@CCTMapChar{^^c4} \@CCTMapChar{^^c5} \@CCTMapChar{^^c6} \@CCTMapChar{^^c7}
\@CCTMapChar{^^c8} \@CCTMapChar{^^c9} \@CCTMapChar{^^ca} \@CCTMapChar{^^cb}
\@CCTMapChar{^^cc} \@CCTMapChar{^^cd} \@CCTMapChar{^^ce} \@CCTMapChar{^^cf}
\@CCTMapChar{^^d0} \@CCTMapChar{^^d1} \@CCTMapChar{^^d2} \@CCTMapChar{^^d3}
\@CCTMapChar{^^d4} \@CCTMapChar{^^d5} \@CCTMapChar{^^d6} \@CCTMapChar{^^d7}
\@CCTMapChar{^^d8} \@CCTMapChar{^^d9} \@CCTMapChar{^^da} \@CCTMapChar{^^db}
\@CCTMapChar{^^dc} \@CCTMapChar{^^dd} \@CCTMapChar{^^de} \@CCTMapChar{^^df}
\@CCTMapChar{^^e0} \@CCTMapChar{^^e1} \@CCTMapChar{^^e2} \@CCTMapChar{^^e3}
\@CCTMapChar{^^e4} \@CCTMapChar{^^e5} \@CCTMapChar{^^e6} \@CCTMapChar{^^e7}
\@CCTMapChar{^^e8} \@CCTMapChar{^^e9} \@CCTMapChar{^^ea} \@CCTMapChar{^^eb}
\@CCTMapChar{^^ec} \@CCTMapChar{^^ed} \@CCTMapChar{^^ee} \@CCTMapChar{^^ef}
\@CCTMapChar{^^f0} \@CCTMapChar{^^f1} \@CCTMapChar{^^f2} \@CCTMapChar{^^f3}
\@CCTMapChar{^^f4} \@CCTMapChar{^^f5} \@CCTMapChar{^^f6} \@CCTMapChar{^^f7}
\@CCTMapChar{^^f8} \@CCTMapChar{^^f9} \@CCTMapChar{^^fa} \@CCTMapChar{^^fb}
\@CCTMapChar{^^fc} \@CCTMapChar{^^fd} \@CCTMapChar{^^fe} \@CCTMapChar{^^ff}

%
% restore catcode of @
%
\catcode`\@=\OldCatcodeOfAt!

\endinput
