%
%  convert tbl files:
%
%  UTF --> gbk 

\documentclass[12pt]{article}
\begin{document}
\makeatletter

\@tempcnta "080\relax
  \loop
    \catcode\@tempcnta \active
    \ifnum\@tempcnta < "0FE\relax
      \advance\@tempcnta \@ne
  \repeat

\newwrite\fdout

\def\punctnumber#1#2#3#4#5{
\expandafter\def\csname punct@#1#2#3\endcsname{\string#4\string#5}}

\input{gbklist.tex}

\def\UTFpunct#1#2#3#4\relax{
\immediate\write\fdout{\string\GBKpunct\space\csname
   punct@\number `#1\number`#2\number`#3\endcsname#4\relax}}

\def\UTFpunctbreak#1#2#3#4\relax{
\immediate\write\fdout{\string\GBKpunctbreak\space\csname
   punct@\number `#1\number`#2\number`#3\endcsname#4\relax}}

\def\UTFkern#1#2#3#4#5#6#7\relax{
\immediate\write\fdout{\string\GBKkern\space\csname
   punct@\number `#1\number`#2\number`#3\endcsname
   \csname
   punct@\number `#4\number`#5\number`#6\endcsname
   #7\relax}}

\def\xeCJK@defkern{\immediate\write\fdout{\string\xeCJK@defkern}}


\def\tblversion{2008/06/20}

\bgroup
\catcode`\%=11
\catcode`\{=11
\catcode`\}=11
\catcode`\(=1
\catcode`\)=2
\global\def\percent(%)
\global\def\lbrace({)
\global\def\rbrace(})
\egroup



\def\outputtblfileheader{
  \immediate\write\fdout{\percent\space }
  \immediate\write\fdout{\percent\space This is the file \gbktblfilename.tbl\space version
  \tblversion}
  \bgroup
  \count@\time
  \divide\count@ 60
  \count2=-\count@
  \multiply\count2 60
  \advance\count2 \time
  \edef\today{%
    \the\year/\two@digits{\the\month}/\two@digits{\the\day}%
    \space\space at\space
      \two@digits{\the\count@}:\two@digits{\the\count2}}
  \immediate\write\fdout{\percent\space This is a generated file [\today].}
  \egroup
  \immediate\write\fdout{\percent\space Do not modify it!}
  \immediate\write\fdout{\percent\space }
  \immediate\write\fdout{\percent\space }
  \immediate\write\fdout{\percent\space Note:}
  \immediate\write\fdout{\percent\space\space\space    1) 'rule' is the width of the \string\vrule\space prepended/appended to the char.}
  \immediate\write\fdout{\percent\space\space\space     2) 'flag,sp,st,sh' means 'flag,space,stretch,shrink', the glue is}
  \immediate\write\fdout{\percent\space\space\space   defined by:  'space'em plus 'stretch'em minus 'shrink'em}
  \immediate\write\fdout{\percent\space\space\space  (it is ignored when flag is not 1).}
  \immediate\write\fdout{\percent\space\space\space    3) 'nbflag' means 'nobreak flag'.}
  \immediate\write\fdout{\percent\space\space\space    4) For more information on the tbl file, we refer to the CCT package.}
  \immediate\write\fdout{\percent\space\space\space   set rule to '0', pre glue to '0,0,0,0', post glue to '0,-0.2,0,0.2'.}
  \immediate\write\fdout{\percent\space\space\space}
  \immediate\write\fdout{\percent\space\space\space                 pre \space\space\space\space\space\space\space              post}
  \immediate\write\fdout{\percent\space\space\space      ------------------------------  ------------------------------}
  \immediate\write\fdout{\percent\space\space\space                    glue \space\space\space\space\space\space              glue}
  \immediate\write\fdout{\percent\space\space\space     char  nbflag  rule    (flag,sp,st,sh) nbflag  rule    (flag,sp,st,sh)}
  \immediate\write\fdout{\percent\space\space\space     ----  ------------------------------  ------------------------------}
}


\def\punctstyle#1{
  \ifcsname xeCJK@ps@#1\endcsname
    \edef\xeCJK@pstyle{\csname xeCJK@ps@#1\endcsname}
  \else
    \typeout{Warning: Punctstyle #1\space is not defined.}
  \fi}

\def\xeCJK@ps@kaiming{k}
\def\xeCJK@ps@banjiao{b}
\def\xeCJK@ps@quanjiao{q}
\def\xeCJK@ps@CCT{c}

\def\makegbktbl#1#2#3{
\punctstyle{#1}
\edef\gbktblfilename{gbk\xeCJK@pstyle#3}
\immediate\openout\fdout=\gbktblfilename.tbl
\outputtblfileheader
\immediate\write\fdout{\percent}
\immediate\write\fdout{\percent\space \space  \string\punctstyle{#1}}
\immediate\write\fdout{\percent}
\immediate\write\fdout{\string\expandafter\string\gdef\string\csname\space
           xeCJK@\gbktblfilename\string\endcsname\lbrace\percent}
\input{chs\xeCJK@pstyle#2.tbl}

\csname xeCJK@chs\xeCJK@pstyle#2\endcsname

\immediate\write\fdout{\rbrace\percent}
\immediate\write\fdout{\string\endinput}
\immediate\closeout\fdout}

\def\makegbktbls#1#2{
\makegbktbl{CCT}{#1}{#2}
\makegbktbl{banjiao}{#1}{#2}
\makegbktbl{quanjiao}{#1}{#2}
\makegbktbl{kaiming}{#1}{#2}}


\makegbktbls{simsun}{song}
\makegbktbls{simhei}{hei}
\makegbktbls{lisu}{li}
\makegbktbls{youyuan}{you}
\makegbktbls{kaiti}{kai}
\makegbktbls{fangsong}{fs}
\makegbktbls{def}{def}


\end{document}
