%D \module
%D   [       file=font-chi,
%D        version=1999.10.10,
%D          title=\CONTEXT\ Font Macros,
%D       subtitle=Chinese,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D    suggestions=Wang Lei,  
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for 
%C details. 

\writestatus{loading}{Context Font Macros / Chinese}

%D Still to be implemented: 
%D
%D \startopsomming
%D \som columns left right touch  
%D \som distance = (hsize-n*bodyfontsize)/(n-1)
%D \som char grids 
%D \som char tables 
%D \som all kind of rotated combinations 
%D \som hanging puctuation
%D \som a few more encodings
%D \som rotation list 
%D \stopopsomming

%M \stelkorpsin[chi]
%M 
%M \def\WangLei%
%M   {\purechinese{\uchar{205}{245}\uchar{192}{218}}}

\useencoding[chi] 

%D When \WangLei\ sent me a mail asking if \CONTEXT\ was able
%D to support Chinese, I wasn't sure if the answer could be
%D yes. I knew that those languages, rich of glyphs, were
%D typeset by \TEX, so in principle it should be possible. I
%D asked or some more input and was told that there were
%D \LATEX\ styles regarding those languages. When I unzipped
%D the accompanying files, it became clear that I had to
%D implement support for Chinese from scratch. There was a
%D multitude of font, with rather unfamiliar encodings, a
%D large collection of files with |<|at least for me|>|
%D unknown purposes, and worse, the documentation was mainly
%D in Chinese.
%D
%D So, \WangLei\ and I started exchanging some emails and it
%D soon became clear that supporting Chinese was not that
%D complicated at all. It mostly came to dealing with handling
%D \UNICODE\ fonts. It also became clear that everything
%D Chinese took place in the upper region of the eight bit
%D character set. I wrote some macros that could process the
%D small \type {Hello World} file \WangLei\ had send me, and
%D after some bug fixes real Chinese came out. I started to
%D like the look and fel of Chinese glyphs, so on we went.
%D
%D The first comments concerned spacing. The mix of English
%D and Chinese demands some rather deliberate handling of
%D spacing. Breaking lines was not so much a problem, and
%D could be solved by adding some glue between Chinese glyphs.
%D
%D In the meantime had asked \WangLei\ for some language bound
%D labels and texts, and implementing these was rather
%D straightforward. But, there were still some issues to deal
%D with: conversion of numbers, date handling and index
%D sorting. 
%D 
%D I consider(ed) writing Chinese support to be a nice puzzle,
%D since I have to act on chinese \CONTEXT\ code, where I only
%D understand the \CONTEXT\ part. The drawings \WangLei\ made
%D me (in drawing packages) were of great help. Since I write
%D these modules from scratch, although I fall back on some
%D basic encoding and font modules, I consider them to be
%D rather clean. This cannot be said of all \CONTEXT\ font
%D modules \type {-)}. 

\unprotect

%D Because Chinese glyphs have more height than the average 
%D Latin glyph, and at the same time don't have much depth, 
%D we adapt the scale. 
%D
%D Before and after the glyph we have to deal with Chinese 
%D spacing. Special attention is given to punctuation. 
%D
%D \starttypen 
%D [some short nice chinese text with () and english]
%D \stoptypen 
%D
%D We insert a bit of stretch and introduce a signal to 
%D keep track of previous characters. 

%D \macros
%D   {chineseunicodescale, chineseinterglyphskip,
%D    chineseunicodeheight, chineseunicodedepth, chinesespace}
%D
%D There are a few variables, that can be (re|)|set 
%D depending on the current font. They default to: 

\def\chineseunicodescale  {1.00} % not smaller than .85 
\def\chineseunicodeheight {1.00}
\def\chineseunicodedepth  {1.00}
\def\chineseinterglyphskip{0pt   plus .05em minus .01em} 
\def\chinesesurroundskip  {.25em plus .15em minus .05em}

%D We define a few signals. (Another examples of using 
%D such signals can be found in the module \type {m-units}.) 

\newsignal\chineseLsignal % left boundary character 
\newsignal\chineseRsignal % right boundary character 
\newsignal\chineseSsignal % any other character (symbol)

\chardef\chineseBstatus=0 % 0=unknown 1=left 2=right 3=chinese 4=nospace
\chardef\chineseAstatus=0 % 0=unknown 1=left 2=right 3=space
\chardef\chineseSstatus=0 % 0=unknown 1=left 2=right

\sfcode`(=2000  % a temporary hack

\def\chinesenobreak% 
 {\relax
  \iftracechinese
    \nobreak
    \kern-.5pt\color[red]{\vrule\!!width1pt}\kern-.5pt
    \nobreak
  \else
    \nobreak
  \fi}

\newif\ifcorrectchineseboundarychars

\def\chineseunskip%
  {\unskip\unskip\unskip\unskip}

\def\insertchineseglyph%
  {\iftracechinese\tracedchineseglyph\else\insertunicodeglyph\fi}

\newif\ifverticalchinese 

\def\handlechineseunicodeglyph%
  {\ifinpagebody
     \horizontalchineseunicodeglyph
   \else\ifverticalchinese
     \verticalchineseunicodeglyph
   \else
     \horizontalchineseunicodeglyph
   \fi\fi}

% This is a previous, very encoding specific, attempt. The 
% current implementation is more versatile.  
%
% \starttypen 
% \def\chineseleftglyph   {\chardef\chineseSstatus=1 }
% \def\chineserightglyph  {\chardef\chineseSstatus=2 }
% \def\chinesecenterglyph {\chardef\chineseSstatus=3 }
%
% \def\analyzechineseunicodeglyph%
%   {% left  
%    \ifnum\unicodeposition=161174 \chineseleftglyph   \else
%    \ifnum\unicodeposition=161176 \chineseleftglyph   \else
%    \ifnum\unicodeposition=161178 \chineseleftglyph   \else
%    \ifnum\unicodeposition=161180 \chineseleftglyph   \else
%    \ifnum\unicodeposition=161182 \chineseleftglyph   \else
%    \ifnum\unicodeposition=161184 \chineseleftglyph   \else
%    \ifnum\unicodeposition=161186 \chineseleftglyph   \else
%    \ifnum\unicodeposition=161188 \chineseleftglyph   \else
%    \ifnum\unicodeposition=161190 \chineseleftglyph   \else
%    \ifnum\unicodeposition=163168 \chineseleftglyph   \else
%    \ifnum\unicodeposition=163219 \chineseleftglyph   \else
%    \ifnum\unicodeposition=163224 \chineseleftglyph   \else
%    \ifnum\unicodeposition=163251 \chineseleftglyph   \else
%    % right
%    \ifnum\unicodeposition=161162 \chineserightglyph  \else
%    \ifnum\unicodeposition=161163 \chineserightglyph  \else
%    \ifnum\unicodeposition=161175 \chineserightglyph  \else
%    \ifnum\unicodeposition=161177 \chineserightglyph  \else
%    \ifnum\unicodeposition=161179 \chineserightglyph  \else
%    \ifnum\unicodeposition=161181 \chineserightglyph  \else
%    \ifnum\unicodeposition=161183 \chineserightglyph  \else
%    \ifnum\unicodeposition=161185 \chineserightglyph  \else
%    \ifnum\unicodeposition=161187 \chineserightglyph  \else
%    \ifnum\unicodeposition=161189 \chineserightglyph  \else
%    \ifnum\unicodeposition=161191 \chineserightglyph  \else
%    \ifnum\unicodeposition=161227 \chineserightglyph  \else
%    \ifnum\unicodeposition=161228 \chineserightglyph  \else
%    \ifnum\unicodeposition=161229 \chineserightglyph  \else
%    \ifnum\unicodeposition=163161 \chineserightglyph  \else
%    \ifnum\unicodeposition=163162 \chineserightglyph  \else
%    \ifnum\unicodeposition=163167 \chineserightglyph  \else
%    \ifnum\unicodeposition=163169 \chineserightglyph  \else
%    \ifnum\unicodeposition=163172 \chineserightglyph  \else
%    \ifnum\unicodeposition=163174 \chineserightglyph  \else
%    \ifnum\unicodeposition=163186 \chineserightglyph  \else
%    \ifnum\unicodeposition=163187 \chineserightglyph  \else
%    \ifnum\unicodeposition=163191 \chineserightglyph  \else
%    \ifnum\unicodeposition=163221 \chineserightglyph  \else
%    \ifnum\unicodeposition=163253 \chineserightglyph  \else
%    % center
%    \ifnum\unicodeposition=161164 \chinesecenterglyph \else
%    \ifnum\unicodeposition=161167 \chinesecenterglyph \else
%    \ifnum\unicodeposition=161173 \chinesecenterglyph \else
%    \ifnum\unicodeposition=163190 \chinesecenterglyph \else
%    % unknown
%    \chardef\chineseSstatus=0
%    \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi
%    \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi}
% \stoptypen 
%
% chinese classes: left=1|right=2|center=3
 
\def\analyzechineseunicodeglyph%
  {\chardef\chineseSstatus=0\getvalue{uc\the\unicodeposition}\relax}

\def\analyzechineseunicodeenviroment%
  {% left
   \ifx (\nextglyph              \chardef\chineseAstatus=1 \else
   \ifx [\nextglyph              \chardef\chineseAstatus=1 \else
   % right
   \ifx ,\nextglyph              \chardef\chineseAstatus=2 \else
   \ifx .\nextglyph              \chardef\chineseAstatus=2 \else
   \ifx ?\nextglyph              \chardef\chineseAstatus=2 \else
   \ifx ;\nextglyph              \chardef\chineseAstatus=2 \else
   \ifx :\nextglyph              \chardef\chineseAstatus=2 \else
   \ifx !\nextglyph              \chardef\chineseAstatus=2 \else
   \ifx )\nextglyph              \chardef\chineseAstatus=2 \else
   \ifx ]\nextglyph              \chardef\chineseAstatus=2 \else
   % space
   \ifx\nextglyph\blankspace     \chardef\chineseAstatus=3 \else
   \ifx\nextglyph\space          \chardef\chineseAstatus=3 \else
                                 \chardef\chineseAstatus=0
   % unknown
   \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi\fi
   % maybe save the last skip?
   \chardef\chineseBstatus=\ifdim\lastskip=\!!zeropoint 4 \else0 \fi
   \ifdim\lastskip=\chineseLsignal \chardef\chineseBstatus=1 \else
   \ifdim\lastskip=\chineseRsignal \chardef\chineseBstatus=2 \else
   \ifdim\lastskip=\chineseSsignal \chardef\chineseBstatus=3 \else
     \scratchskip=\lastskip \unskip
     \ifdim\lastskip=\chineseLsignal \chardef\chineseBstatus=1 \else
     \ifdim\lastskip=\chineseRsignal \chardef\chineseBstatus=2 \else
     \ifdim\lastskip=\chineseSsignal \chardef\chineseBstatus=3 \else
       \space\scratchskip=\lastskip \unskip
       \setbox\scratchbox=\hbox\bgroup
         (\space
         \ifdim\lastskip=\scratchskip
           \egroup \chardef\chineseBstatus=1
         \else
           \egroup
           \ifdim\scratchskip=\!!zeropoint \chardef\chineseBstatus=4 \fi
         \fi
     \fi\fi\fi
   \fi\fi\fi}
    
\def\horizontalchineseunicodeglyph
  {\relax
   \ifprocessingverbatim
     \insertunicodeglyph
   \else
     \ifhmode\else\dontleavehmode\fi % added
     \setunicodescale\chineseunicodescale
     \setunicodestrut\chineseunicodeheight\chineseunicodedepth
     \analyzechineseunicodeglyph
     \analyzechineseunicodeenviroment
     \ifcase\chineseSstatus\relax
       \ifcase\chineseBstatus\relax
         \chineseunskip
         \hskip\chinesesurroundskip      % unknown
       \or
         \chineseunskip
         \chinesenobreak                 % left
       \or
         \ifcorrectchineseboundarychars\else\chineseunskip\fi 
         \hskip\chineseinterglyphskip    % right
       \or
         \chineseunskip
         \hskip\chineseinterglyphskip    % chinese
       \or
                                         % whatever
       \fi
       \insertchineseglyph
       \ifcase\chineseAstatus\relax
         \hskip\chineseinterglyphskip    % unknown
         \hskip\chinesesurroundskip
         \ifcase\chineseSstatus
           \hskip\chineseSsignal
         \or
           \hskip\chineseLsignal
         \else
           \hskip\chineseRsignal
         \fi
       \or
         \hskip\chineseinterglyphskip    % left
         \chinesenobreak
         \hskip\chineseLsignal
       \or
         \chinesenobreak                 % right
         \hskip\chineseinterglyphskip
         \chinesenobreak
         \hskip\chineseRsignal
       \or
         \hskip\chineseinterglyphskip    % space
         \hskip\chinesesurroundskip
         \hskip\chineseSsignal
       \fi
     \or % left
       \ifcorrectchineseboundarychars 
         \let\unicodecharcommand\chineseleftcharcommand
       \fi
       \chineseunskip
       \insertchineseglyph
       \chinesenobreak
       \hskip\chineseLsignal
     \or % right
       \ifcorrectchineseboundarychars 
         \let\unicodecharcommand\chineserightcharcommand
       \fi
       \chineseunskip
       \chinesenobreak
       \insertchineseglyph
       \hskip\chineseRsignal
     \else % center
       \chineseunskip  
       \chinesenobreak
       \insertchineseglyph
       \hskip\chineseinterglyphskip
       \hskip\chineseRsignal
     \fi
     \aftergroup\ignorespaces            % watch this
   \fi}

\def\verticalchineseunicodeglyph%
  {\relax
   \ifprocessingverbatim
     \insertunicodeglyph
   \else
     \setunicodescale\chineseunicodescale
     \setunicodestrut\chineseunicodeheight\chineseunicodedepth
     \analyzechineseunicodeglyph
     \setbox\scratchbox=\hbox to \hsize
       {\hss
        \ifcase\chineseSstatus\relax
          \insertchineseglyph
        \else % left / right
          \setbox\scratchbox=\hbox{\insertchineseglyph}%
          \rotate[\c!rotatie=270]{\box\scratchbox}%
        \fi
        \hss}%
     \ht\scratchbox=\unicodeheight\ht\strutbox
     \dp\scratchbox=\unicodedepth \dp\strutbox
     \nointerlineskip
     \ifcase\prevchineseSstatus\relax  
       \ifnum\chineseSstatus=2\nobreak\else\allowbreak\fi
     \or % left 
       \nobreak
     \or % right 
       \ifnum\chineseSstatus=2\nobreak\else\allowbreak\fi
     \fi
     \global\chardef\prevchineseSstatus=\chineseSstatus % pagebody ... 
     \box\scratchbox
     \aftergroup\ignorespaces                           % watch this
   \fi}

%D \macros
%D   {setupchinese,startvertical,nochinese}
%D
%D The previous macros implement horizontal as well as 
%D vertical typesetting. Vertical typesetting is implemented
%D on top of the multi||column routines. 

\def\setupchinese%
  {\dodoubleargument\getparameters[\??vt]}

\def\startvertical%
  {\dosingleempty\dostartvertical}

\def\dostartvertical[#1]%
  {\bgroup
   \def\maxnofcolumns{25}%
   \verticalchinesetrue
   \global\chardef\prevchineseSstatus=0 
   \let\nochinese=\nochineseinvertical
   \startkolommen
     [\c!richting=\@@vtrichting,
      \c!balanceren=\@@vtbalanceren,
      \c!afstand=\@@vtafstand,
      \c!n=\@@vtn,
      #1]} 

\def\stopvertical%
  {\stopkolommen
   \egroup}

\setupchinese
  [\c!richting=\v!links,
   \c!balanceren=\v!nee,
   \c!n=12,
   \c!afstand=1.5\bodyfontsize]

%D We can set up vertical typesetting with \type 
%D {\setupchinese}.

%D \macros
%D   {nochineseinvertical}
%D
%D English (non chinese) text is typeset rotated:

\def\nochineseinvertical#1%
  {\par
   \setbox\scratchbox=\hbox{\strut#1}
   \getnoflines{\wd\scratchbox}
   \setbox\scratchbox=\hbox to \noflines\openlineheight
     {\hss\box\scratchbox\hss}
   \hbox to \hsize
     {\hss
      \rotate
        [\c!rotatie=270]
        {\vbox to \ht\scratchbox{\vss\box\scratchbox\vss}}%
      \hss}
   \par}

%D \macros
%D   {correctchineseboundarychars}
%D
%D Careful reading of the previous macro learns that we 
%D treat left and right glyphs differently. When we say 
%D
%D \starttypen 
%D \correctchineseboundarycharstrue
%D \stoptypen
%D
%D For the moment correction in on by default. 

\correctchineseboundarycharstrue

\def\chineserightcharcommand#1%
  {\iftracechinese\ruledhbox\else\hbox\fi \!!to .5em{#1\hss}%
   \hskip.25em\!!plus .25em\relax}%

\def\chineseleftcharcommand#1%
  {\ifnum\chineseBstatus<4 \hskip.25em \!!plus .25em\relax\fi
   \iftracechinese\ruledhbox\else\hbox\fi \!!to .5em{\hss#1}}%

% \def\chineserightcharcommand#1%
%   {\setbox\scratchbox=\hbox{#1}%
%    \scratchdimen=.5em 
%    \ifdim\wd\scratchbox>\scratchdimen
%      \iftracechinese\ruledhbox\else\hbox\fi \!!to \scratchdimen
%        {\box\scratchbox\hss}%
%      \hskip.5\scratchdimen\!!plus.5\scratchdimen\relax
%    \else
%      \box\scratchbox
%    \fi}

% \def\chineseleftcharcommand#1%
%   {\setbox\scratchbox=\hbox{#1}%
%    \scratchdimen=.5em 
%    \ifdim\wd\scratchbox>\scratchdimen
%      \ifnum\chineseBstatus<4 
%        \hskip.5\scratchdimen\!!plus.5\scratchdimen\relax
%      \fi
%      \iftracechinese\ruledhbox\else\hbox\fi \!!to \scratchdimen
%        {\hss\box\scratchbox}%
%    \else
%      \box\scratchbox
%    \fi}

%D The long list of numbers in the previous macro identify the 
%D characters where special care is needed for breaking lines. 
%D A linebreak is not permitted before: 
%D 
%D \def\DoIt #1 #2 %
%D   {\hbox{\hbox to 2em{\uchar{#1}{#2}\hss}#1 #2}\par}
%D 
%D \startkolommen[n=5] 
%D \DoIt 161 162 \DoIt 161 163 \DoIt 161 164 \DoIt 161 167 \DoIt 161 173 
%D \DoIt 161 175 \DoIt 161 177 \DoIt 161 179 \DoIt 161 181 \DoIt 161 183 
%D \DoIt 161 185 \DoIt 161 187 \DoIt 161 189 \DoIt 161 191 \DoIt 161 227
%D \DoIt 161 228 \DoIt 161 229 \DoIt 163 161 \DoIt 163 162 \DoIt 163 167 
%D \DoIt 163 169 \DoIt 163 172 \DoIt 163 174 \DoIt 163 186 \DoIt 163 187 
%D \DoIt 163 190 \DoIt 163 191 \DoIt 163 221 \DoIt 163 253
%D \stopkolommen
%D 
%D A linebreak is not permitted after the following glyphs: 
%D 
%D \startkolommen[n=5]
%D \DoIt 161 174 \DoIt 161 176 \DoIt 161 178 \DoIt 161 180 \DoIt 161 182 
%D \DoIt 161 184 \DoIt 161 186 \DoIt 161 188 \DoIt 161 190 \DoIt 163 168 
%D \DoIt 163 219 \DoIt 163 224 \DoIt 163 251
%D \stopkolommen

%D \macros 
%D   {tracechinesetrue, showchinesetracelegend}
%D
%D When we say \type {\tracechinesetrue}, we get some 
%D insight in the way \CONTEXT\ handles the Chinese glyphs. 
%D The symbols and color used represent: 
%D 
%D \showchinesetracelegend

\newif\iftracechinese 

\def\showchinesetracelegend%
  {\definetabulate[\s!dummy][|c|l|l|l|]%
   \startdummy
     \HL \NC \bf key \NC \bf meaning \NC \bf glyph \NC \bf keys \NC \NR \HL
     \NC u \NC unknown character type \NC \color[green]{current} \NC u l r \NC\NR
     \NC l \NC left boundary character \NC \color[red]{previous} \NC u l r c n \NC\NR
     \NC r \NC right boundary character \NC \color[blue]{next} \NC u l r s \NC\NR
     \NC c \NC chinese character \NC \NC \NC\NR
     \NC s \NC following space \NC \NC \NC\NR 
     \NC n \NC no preceding space \NC \NC \NC\NR \HL
   \stopdummy}

\def\tracedchineseglyph%
  {\leavevmode
   \ruledhbox
     {\setbox\scratchbox=\hbox{\insertunicodeglyph}%
      \hbox to \wd\scratchbox
        {\localcolortrue
         \copy\scratchbox
         \infofont
         \hskip-\wd\scratchbox
         \hbox to \wd\scratchbox
           {\hss
            \color[green]
              {\ifcase\chineseSstatus\relax u\or l\or r\fi}%
            \hss}%
         \hskip-\wd\scratchbox
         \lower\dp\scratchbox\hbox to \wd\scratchbox
           {\hss
            \color[red]
              {\ifcase\chineseBstatus\relax u\or l\or r\or c\or n\fi}%
            \hss
            \color[blue]
              {\ifcase\chineseAstatus\relax u\or l\or r\or s\fi}%
            \hss}}}}

%D The following example shows how tracing works. 
%D 
%D \start \tracechinesetrue
%D 在这一次更新中我们将介绍对中文的支持。当本文的中文译者王磊
%D 询问我 \nochinese{\CONTEXT} 是否能处理汉语时，他已经尝试过
%D 运行现有的宏集，但是没有得到结果。这意味着对中文的支持还没
%D 有实现。
%D 
%D 在这一(次更新)中我们 (将介) 绍对中文的支持。当本文的中文译,
%D 者王磊询问我\nochinese{\CONTEXT}是否能处理汉语时，他已经尝
%D 试过运行现有的宏集，但是没有得到结果。这意味着对中文的支持
%D 还没有实现。.
%D \stop

%D Because fonts are defined each time a \UNICODE\ is 
%D encountered |<|which is less inefficient than one would 
%D imagine, because \TEX\ is optimized quite well in this 
%D repect|>| we can define macros like this to take care of 
%D font switches. When available, one can add definitions 
%D for italic, slanted, bold fonts and combinations of these. 

%D \macros 
%D   {chinesenumber}
%D
%D The chinese numbering systems rather straightforward. First 
%D there are the digits: 
%D 
%D \starttabulatie[|c|c|c|c|c|c|c|c|c|c|]
%D \NC 0 \NC 1 \NC 2 \NC 3 \NC 4 \NC 5 \NC 6 \NC 7 \NC 8 \NC 9 \NC\NR
%D \NC \chinesenumber{0} \NC \chinesenumber{1} \NC \chinesenumber{2} 
%D \NC \chinesenumber{3} \NC \chinesenumber{4} \NC \chinesenumber{5} 
%D \NC \chinesenumber{6} \NC \chinesenumber{7} \NC \chinesenumber{8} 
%D \NC \chinesenumber{9} \NC\NR
%D \stoptabulatie
%D 
%D Apart from these numbers, we have dedicated representations 
%D of some powers of~$10$.
%D 
%D \starttabulatie[|c|c|c|c|c|]
%D \NC 10 \NC 100 \NC 1000 \NC 10000 \NC 100000000 \NC\NR 
%D \NC \chinesenumber{10} \NC \chinesenumber{100}    
%D \NC \chinesenumber{1000} \NC \chinesenumber{10000} 
%D \NC \chinesenumber{100000000} \NC\NR
%D \stoptabulatie
%D 
%D The number~12 is a combination of $1\times10+2$, or:
%D \chinesenumber {12}, while~22 becomes \chinesenumber {22}.
%D The numbers below 20 are treated a bit different, just like 
%D numbers with series of $0$'s. So $2\times10$ comes out as 
%D two glyphs, but $1\times10$ as one, because in the latter 
%D case the~$1$ is redundant. The same is true for the powers 
%D of~10. 
%D 
%D \starttabulatie[|r|r|r|r|r|r|]
%D \NC     1 \NC \chinesenumber     {1} \NC
%D         9 \NC \chinesenumber     {9} \NC
%D         4 \NC \chinesenumber     {4} \NC\NR
%D \NC    11 \NC \chinesenumber    {11} \NC
%D        99 \NC \chinesenumber    {99} \NC
%D        16 \NC \chinesenumber    {16} \NC\NR
%D \NC   111 \NC \chinesenumber   {111} \NC
%D       999 \NC \chinesenumber   {999} \NC
%D       256 \NC \chinesenumber   {256} \NC\NR
%D \NC  1111 \NC \chinesenumber  {1111} \NC
%D      9999 \NC \chinesenumber  {9999} \NC
%D     65536 \NC \chinesenumber {65536} \NC\NR
%D \stoptabulatie
%D 
%D The implementation is rather simple. For internal purposes,
%D we let zero expand to~0. The digits $0-9$ and numbers $10$,
%D $100$, $1000$, $10000$ and $100000000$ are hard coded. 

% This was the first implementation, before \WangLei\ asked 
% me to look into Big Five encoding, so, like everthing in 
% \TEX, things become a bit more complicated, but also more 
% versatile. 
%
% \starttypen 
% \def\chinesedigit#1%
%   {\ifnum      #1=100000000 \uchar{210}{218}% 100000000
%    \else\ifnum #1=10000     \uchar{205}{242}% 10000
%    \else\ifnum #1=1000      \uchar{199}{167}% 1000
%    \else\ifnum #1=100       \uchar{176}{217}% 100
%    \else\ifnum #1=10        \uchar{202}{174}% 10  
%    \else\ifcase#1           \uchar{193}{227}% 0
%    \or                      \uchar{210}{187}% 1
%    \or                      \uchar{182}{254}% 2
%    \or                      \uchar{200}{253}% 3
%    \or                      \uchar{203}{196}% 4
%    \or                      \uchar{206}{229}% 5
%    \or                      \uchar{193}{249}% 6
%    \or                      \uchar{198}{223}% 7
%    \or                      \uchar{176}{203}% 8
%    \or                      \uchar{190}{197}% 9
%    \fi\fi\fi\fi\fi\fi}
% \stoptypen 

%D We use a dirty trick to enable Chinese Capital Digits. In
%D the encoding vectors, we define these by appending a suffix 
%D \type {*} to the digit. For the moment this method works ok. 
%D With \type {\capstyle} one can switch to those capitalized 
%D digits. 

\def\enablechinesecapdigits%
  {\def\chinesedigitsuffix{*}}

\def\disablechinesecapdigits%
  {\let\chinesedigitsuffix\empty}

\disablechinesecapdigits  

\let\capstyle\enablechinesecapdigits

\def\chinesedigit#1%
  {\udigit{\chineseencoding}{\number#1\chinesedigitsuffix}}

\def\chinesenumber#1%
  {\expandafter\dochinesenumber\number#1\relax}

\def\dochinesenumber#1#2\relax
  {\ifnum#1#2<10 % 1-10
     \chinesedigit{#1}%
   \else\ifnum#1#2<20 % 11-99
     \chinesedigit{10}%
     \dodochinesenumberA#2\relax
   \else
     \dodochinesenumber#1#2\relax
   \fi\fi}

\def\dodochinesenumber#1#2\relax
  {\ifnum#1=0
     \chinesedigit{0}%
     \dododochinesenumber0#2\relax
   \else\ifnum#1#2<10 % 1-10
     \chinesedigit{#1#2}%
     \dodochinesenumberA#2\relax
   \else\ifnum#1#2<100 % 11-99
     \dodochinesenumber#1\relax
     \chinesedigit{10}%
     \dodochinesenumberA#2\relax
   \else\ifnum#1#2<1000 % 100-999
     \dodochinesenumber#1\relax
     \chinesedigit{100}%
     \dodochinesenumberA#2\relax
   \else\ifnum#1#2<10000 % 1000-9999
     \dodochinesenumber#1\relax
     \chinesedigit{1000}%
     \dodochinesenumberA#2\relax
   \else\ifnum#1#2<100000 % 10000-99999
     \dodochinesenumber#1\relax
     \chinesedigit{10000}%
     \dodochinesenumberA#2\relax
   \else\ifnum#1#2<1000000 % 100000-999999
     \dodochinesenumberB#1#2\relax
   \else\ifnum#1#2<10000000 % 1000000-9999999
     \dodochinesenumberC#1#2\relax
   \else\ifnum#1#2<100000000 % 10000000-99999999
     \dodochinesenumberD#1#2\relax
   \else\ifnum#1#2<1000000000 % 10000000-99999999
     \dochinesenumber#1\relax
     \chinesedigit{100000000}%
     \dododochinesenumber#2\relax
   \else
     \dodochinesenumberE#1#2\relax
   \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

\def\dododochinesenumber#1#2\relax
  {\ifnum#1=0
     \ifnum0#2>0 \dododochinesenumber#2\relax \fi
   \else
     \dodochinesenumber#1#2\relax
   \fi}

\def\dodochinesenumberA#1\relax
  {\ifcase0#1 \else\dodochinesenumber#1\relax\fi}

\def\dodochinesenumberB#1#2#3\relax
  {\dochinesenumber#1#2\relax
   \chinesedigit{10000}%
   \dododochinesenumber#3\relax} 

\def\dodochinesenumberC#1#2#3#4\relax
  {\dochinesenumber#1#2#3\relax
   \chinesedigit{10000}%
   \dododochinesenumber#4\relax} 

\def\dodochinesenumberD#1#2#3#4#5\relax
  {\dochinesenumber#1#2#3#4\relax
   \chinesedigit{10000}%
   \dododochinesenumber#5\relax} 

\def\dodochinesenumberE#1#2#3\relax
  {\dochinesenumber#1#2\relax
   \chinesedigit{100000000}%
   \dododochinesenumber#3\relax} 

%D The next table demonstates the correctness of the 
%D conversion macro. 
%D 
%D \startbuffer
%D \starttabel[|l|l|l|l|l|l|]
%D \HL
%D \NC number     \NC converter                  \NC hard coded \NC
%D     number     \NC converter                  \NC hard coded\NC \SR
%D \HL
%D \NC 1          \NC \chinesenumber{1}          \NC 一 \NC
%D     0          \NC \chinesenumber{0}          \NC 零 \NC\FR
%D \NC 11         \NC \chinesenumber{11}         \NC 十一 \NC
%D     10         \NC \chinesenumber{10}         \NC 十  \NC\MR
%D \NC 111        \NC \chinesenumber{111}        \NC 一百一十一 \NC
%D     100        \NC \chinesenumber{100}        \NC 一百 \NC\MR
%D \NC 101        \NC \chinesenumber{101}        \NC 一百零一 \NC
%D     120        \NC \chinesenumber{120}        \NC 一百二十 \NC\MR
%D \NC 1111       \NC \chinesenumber{1111}       \NC 一千一百一十一 \NC
%D     1000       \NC \chinesenumber{1000}       \NC 一千 \NC\MR
%D \NC 1001       \NC \chinesenumber{1001}       \NC 一千零一 \NC
%D     1020       \NC \chinesenumber{1020}       \NC 一千零二十 \NC\MR
%D \NC 11111      \NC \chinesenumber{11111}      \NC 一万一千一百一十一 \NC
%D     10000      \NC \chinesenumber{10000}      \NC 一万 \NC\MR
%D \NC 10001      \NC \chinesenumber{10001}      \NC 一万零一 \NC
%D     10200      \NC \chinesenumber{10200}      \NC 一万零二百 \NC\LR
%D \HL
%D \NC 111111     \NC \chinesenumber{111111}     \NC 十一万一千一百一十一 \NC
%D     100000     \NC \chinesenumber{100000}     \NC 十万 \NC\FR
%D \NC 1111111    \NC \chinesenumber{1111111}    \NC 一百一十一万一千一百一十一 \NC
%D     1000000    \NC \chinesenumber{1000000}    \NC 一百万 \NC\MR
%D \NC 11111111   \NC \chinesenumber{11111111}   \NC 一千一百一十一万一千一百一十一 \NC
%D     10000000   \NC \chinesenumber{10000000}   \NC 一千万 \NC\MR
%D \NC 111111111  \NC \chinesenumber{111111111}  \NC 一亿一千一百一十一万一千一百一十一 \NC
%D     100000000  \NC \chinesenumber{100000000}  \NC 一亿 \NC\MR
%D \NC 1111111111 \NC \chinesenumber{1111111111} \NC 十一亿一千一百一十一万一千一百一十一 \NC
%D     1000000000 \NC \chinesenumber{1000000000} \NC 十亿 \NC\LR
%D \HL
%D \stoptabel
%D \stopbuffer
%D
%D \plaatstabel{Some Chinese number examples.}{\haalbuffer}

%D Although the conversion can be hooked into most commands
%D that deal with numbers |<|those familiar with \TEX\ macro
%D programming will notice that the macro if fully
%D expandable|>| in many cases Chinese documents use western
%D digits. So, in practice, the change that the next example 
%D shows up, is minimal. 
%D 
%D \starttabulatie[|r|r|c|]
%D \NC  2546 \NC \chinesenumber  {2546} \NC     \NC\NR 
%D \NC  9258 \NC \chinesenumber  {9258} \NC $+$ \NC\NR 
%D \HL 
%D \NC 11804 \NC \chinesenumber {11804} \NC     \NC\NR
%D \stoptabulatie
%D 
%D While in arabic arithmics addition leads to more digits, in
%D Chinese the number of glyphs can (temporary) decrease.
%D Given that the number of people dealing with Chinese is
%D considerably larger than the number of latin speaking
%D people, a successor of \TEX\ definitely must provide a
%D \type {\chinesenumeral} primitive. 

\defineconversion[c]      [\chinesenumber]
\defineconversion[chinese][\chinesenumber]

%D \macros
%D   {SimChi, TraChi}
%D
%D These components enable us to construct dedicated Chinese
%D font switches, like: 

\setupunicodefont
  [chinese]
  [   \c!schaal=\chineseunicodescale,  
      \c!hoogte=\chineseunicodeheight, 
      \c!diepte=\chineseunicodedepth,  
   \c!conversie=\chinesenumber,
   \c!commandos=\setchineseencoding, % needed for digits 
    \c!commando=\handlechineseunicodeglyph]

\defineunicodefont [SimChi] [SimplifiedChinese]  [chinese]
\defineunicodefont [TraChi] [TraditionalChinese] [chinese]

%D For the moment, this encoding is implemented rather ugly. The 
%D trick is to move the encoding value from the current font 
%D definition to the \type {\chineseencoding} macro.  

\def\setchineseencoding% normally SomeChineseRegular 
  {\getfontfileparameters\unicodestyle
   \ifx\currentfontfileencoding\undefined \else
     \let\chineseencoding\currentfontfileencoding
   \fi}

%D We could have said: 
%D 
%D \starttypen
%D \defineunicodefont 
%D   [SimChi] 
%D   [SimplifiedChinese]
%D   [   \c!schaal=\chineseunicodescale,  
%D       \c!hoogte=\chineseunicodeheight, 
%D       \c!diepte=\chineseunicodedepth,  
%D    \c!conversie=\chinesenumber,
%D    \c!commandos=\setchineseencoding, % needed for digits 
%D     \c!commando=\handlechineseunicodeglyph]
%D \stoptypen
%D 
%D However, the former definitions is more general. Next we 
%D map a few fonts: 

\definefontsynonym [SimplifiedChineseRegular]      [gbsong]   [encoding=gbk]
\definefontsynonym [SimplifiedChineseSlanted]      [gbsongsl] [encoding=gbk]
\definefontsynonym [SimplifiedChineseItalic]       [gbsongsl] [encoding=gbk]
\definefontsynonym [SimplifiedChineseBold]         [gbhei]    [encoding=gbk]
\definefontsynonym [SimplifiedChineseBoldSlanted]  [gbheisl]  [encoding=gbk]
\definefontsynonym [SimplifiedChineseBoldItalic]   [gbheisl]  [encoding=gbk]

% Yet to be defined:  

\definefontsynonym [TraditionalChineseRegular]     [b5ming]   [encoding=big5]
\definefontsynonym [TraditionalChineseSlanted]     [b5mingsl] [encoding=big5]
\definefontsynonym [TraditionalChineseItalic]      [b5mingsl] [encoding=big5]
\definefontsynonym [TraditionalChineseBold]        [b5hei]    [encoding=big5]
\definefontsynonym [TraditionalChineseBoldSlanted] [b5heisl]  [encoding=big5]
\definefontsynonym [TraditionalChineseBoldItalic]  [b5heisl]  [encoding=big5]

%D We default to these so called Simplified Chinese fonts. 

\SimChi

%D \macros
%D   {purechinese}
%D 
%D Use this macro to suppress spacing around Chinese text. 

\def\purechinese#1% evt geen rek 
  {\hskip\chineseSsignal\relax
   #1\unskip\unskip\unskip
   \hskip\chineseSsignal\relax}

%D \macros
%D   {stillchinese}
%D 
%D Use the next macro when you want the next item to be put 
%D tight to the previous chinese character.

\def\stillchinese%
  {\hskip\chineseSsignal\relax}

%D \macros 
%D   {nochinese}
%D
%D When we want to be sure of non||Chinese inline text, 
%D we can package the for instance english text in \type 
%D {\nochinese}. 

\def\nochinese#1%
  {\unskip\unskip\unskip
   \hskip\chinesesurroundskip
   \hskip\chineseinterglyphskip\relax
   #1%
   \hskip\chineseinterglyphskip\relax
   \hskip\chinesesurroundskip
   \ignorespaces}

%D Now it's time for some real Chinese. This example 
%D also shows that font switching is supported. 
%D 
%D \startsmaller
%D \regelmidden{\tfd 水调歌头} 
%D \blanko
%D \regelmidden{\tfb 丙辰中秋，欢饮达旦，大醉作此篇，兼怀子由。} 
%D \blanko
%D \regelmidden{\tfb 苏轼} 
%D \blanko
%D 明月几时有？把酒问青天。不知天上宫阙，今夕是何年？我欲乘风归去
%D ，又恐琼楼玉宇，高处不胜寒。起舞弄清影，何似在人间？ 
%D \blanko
%D 转朱阁，低户，照无眠。不应有恨，何事偏向别时圆？人有悲欢离合，
%D 月有阴晴圆缺，此事古难全。但愿人长久，千里共婵娟。
%D \stopsmaller
%D 
%D The english translation is: 
%D 
%D \startsmaller
%D \regelmidden{\tfd Tune: Prelude to the Melody of Water} 
%D \blanko 
%D \regelmidden{\tfb On the night of the Mid||Autumn Festival of
%D 1076, I drank happily till dawn and wrote this in my cups
%D while thinking of Zi||you.} 
%D \blanko 
%D \regelmidden{\tfb Su Shi} 
%D \blanko
%D How long will the bright moon appear? Wine||cup in hand, I
%D ask the sky. I do not know waht time of year it would be
%D tonight in the palace on high. Riding the wind, there I
%D would fly, yet I fear the crystal palace would be far too
%D high and cold for me. I rise and dance, with my shadow I
%D play. On high as on earth, would it be as gay? 
%D \blanko 
%D The moon goes round the mansion red though gauze||draped
%D windows soft to shed her light upon the sleepless bed.
%D Against man she should have no spite. Why then when people
%D part is she oft full and bright? Men have sorrow and joy,
%D they part or meet again; The moon may be bright or dim, she
%D may wax or wane. There has been nothing perfect since the
%D olden days. So let us wish that man will live long as he
%D can! Though miles apart, we'll share the beauty she
%D displays. 
%D \stopsmaller

%D This package is written in the city of Hasselt, a pretty
%D small town in the |<|compare to China real small|>|
%D Netherlands. Like most dutch cities, even this small one
%D has a chinese restaurant, run by chinese, speaking chinese,
%D and |<|indeed|>| writing chinese. Eating there will never
%D be the same, since now I can at least pretend to know the
%D glyphs all around the place. The numbers should pose me no
%D problems, but I fear I will never manage to recognize those
%D scribles they draw on their pads when thay take your order. 

\protect \endinput
