% This is the file CNS.sty of the CJK package
%   for using asiatic logographs (Chinese/Japanese/Korean) with LaTeX2e
%
% created by Werner Lemberg <a7621gac@awiuni11.bitnet>
%
% Version 2.5 (10-Apr-1994)

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\def\fileversion{2.5}
\def\filedate{1995/04/10}
\ProvidesPackage{CNS}[\filedate\space\fileversion]


\newif\ifCNS@compressed@
\DeclareOption{compressed}{\CNS@compressed@true}
\DeclareOption{uncompressed}{\CNS@compressed@false}

\ExecuteOptions{compressed}
\ProcessOptions

\RequirePackage{CJK}[\filedate\space\fileversion]


% we suppress any unwanted spaces produced by linefeeds.

\endlinechar \m@ne

\ifx\CNSpreproc \@undefined
  \PackageWarningNoLine{CNS}{
    This file is not preprocessed.\MessageBreak
    Will not understand CEF encoded CNS characters.}
\fi

% The \CNS@protect macro is similar to the \CJK@protect.. macros, but it has
%     only two parameters.

\def\CNS@protect#1#2{
  \string\CNS@protect{\string#1}{\string#2}}


% a set of macros to make the expansion robust.

\def\CNS@char#1#2#3{
  \protect\CNS@@char\CJK@protect{#1}{#2}{#3}}
\def\CNS@@char#1#{
  \CNS@@@char}
\def\CNS@@@char#1#2#3{
  \edef\CNS@enc{CNS-#1}
  \csname CNS@#2\endcsname{#3}}

\def\CNS@punctchar#1#2#3{
  \protect\CNS@@punctchar\CJK@protect{#1}{#2}{#3}}
\def\CNS@@punctchar#1#{
  \CNS@@@punctchar}
\def\CNS@@@punctchar#1#2#3{
  \ifCNS@compressed@
    \CNS@namegdef{A1}##1{\CNS@punctch{A1}{01}{-161}{##1}}
    \CNS@namegdef{A2}##1{\CNS@punctch{A2}{01}{ -67}{##1}}
  \else
    \CNS@namegdef{A1}##1{\CNS@punctch{A1}{a1}{0}{##1}}
    \CNS@namegdef{A2}##1{\CNS@punctch{A2}{a2}{0}{##1}}
  \fi

  \edef\CNS@enc{CNS-#1}
  \csname CNS@#2\endcsname{#3}}

\def\CNS@BigFivechar#1#2{
  \protect\CNS@@BigFivechar\CNS@protect{#1}{#2}}
\def\CNS@@BigFivechar#1#{
  \CNS@BigFivech}


% with \CNSchar you can access a character directly at a low level:
%     the parameters are the first, second and third byte of the CNS character
%     code. The first byte, representing the plane, must be in the range 1-7;
%     second and third byte should be numbers between 0x21-0x7E.
%
% If the first parameter is 0, Big 5 encoded characters are selected. In this
%     case the second parameter must be in the range 0xA1-0xFE, and the third
%     between 0x40-0x7E or 0xA1-0x7E inclusive.

\DeclareRobustCommand{\CNSchar}[3]{
  {\CJK@numbToHex{\CJK@temp}{#2}
   \csname CJK@8#1\endcsname\CJK@temp.#3.}}


% you will find the \CNS@xx macros in the file CNS.enc, and its innermost
%     routines in CNS.chr . These macros are
%
%         \CNS@ch, \CNS@chx,
%         \CNS@punctch,
%         \CNS@BigFivech      .

\input CNS.chr
\input CNS.enc


% the low level macro \CNSshape changes the CNS font shape inside of a CJK
%     environment.

\DeclareRobustCommand{\CNSshape}[1]{
  \edef\CNS@shape{#1}}

% the low level macro \CNSBigFiveshape changes the Big 5 font shape (the plane
%     `0' in the Chinese Encoding Framework) inside of a CJK environment.

\DeclareRobustCommand{\CNSBigFiveshape}[1]{
  \edef\CNS@BigFiveshape{#1}}


% make all characters possibly used as the first byte active so they can become
%     macros.

\def\CNS@active{
  \@tempcnta "080
  \loop
    \CJK@global\catcode\@tempcnta \active
    \ifnum\@tempcnta < "087
      \advance\@tempcnta \@ne
  \repeat}

\def\CNS@inactive{
  \@tempcnta "080
  \loop
    \CJK@global\catcode\@tempcnta 12
    \ifnum\@tempcnta < "087
      \advance\@tempcnta \@ne
  \repeat}


% switch on characters so we can define the macros with them active.

\CNS@active

% we assign to each active character ^^xx the macro \CJK@XX.

\def\CNS@let{
  \CJK@nameglet{^^80}{80}
  \CJK@nameglet{^^81}{81}
  \CJK@nameglet{^^82}{82}
  \CJK@nameglet{^^83}{83}
  \CJK@nameglet{^^84}{84}
  \CJK@nameglet{^^85}{85}
  \CJK@nameglet{^^86}{86}
  \CJK@nameglet{^^87}{87}}

\CNS@inactive


% CNS encoding:
%     each plane has 94x94 characters (0x21-0x7E x 0x21-0x7E).
%
% the CNS planes are mapped to the characters 0x81-0x87 to allow use of other
%     CJK encodings at the same time.
%
% plane `0' in CEF are Big 5 encoded characters and will be mapped to 0x80.

\def\CNS@encoding{
  \CJK@namegdef{80}##1.##2.{\CNS@BigFivechar{##1}{##2}}
  \CJK@namegdef{81}##1.##2.{\CNS@punctchar{01}{##1}{##2}}
  \CJK@namegdef{82}##1.##2.{\CNS@char{02}{##1}{##2}}
  \CJK@namegdef{83}##1.##2.{\CNS@char{03}{##1}{##2}}
  \CJK@namegdef{84}##1.##2.{\CNS@char{04}{##1}{##2}}
  \CJK@namegdef{85}##1.##2.{\CNS@char{05}{##1}{##2}}
  \CJK@namegdef{86}##1.##2.{\CNS@char{06}{##1}{##2}}
  \CJK@namegdef{87}##1.##2.{\CNS@char{07}{##1}{##2}}}


% here are the punctuation tables. We suppress spaces additionally in this
%     group.

\begingroup
  \catcode`\ 9\relax

  \CJK@prePunct {CNS-01}{21}{3E, 42, 46, 4A, 4E, 52, 56, 5A, 5E, 60, 62,
                             64, 66, 68, 6A,
                             6C, 70}
  \CJK@postPunct{CNS-01}{21}{22, 23, 24, 25, 26, 27, 28, 29,
                             2A, 2C, 2D, 2E, 2F, 30, 31, 32,
                             33, 34, 35,
                             3F, 43, 47, 4B, 4F, 53, 57, 5B, 5F, 61, 63,
                             65, 67, 69, 6B}
  \CJK@prePunct {CNS-01}{22}{2D, 2E, 63, 64, 65, 66, 67, 69,
                             6C, 6E}
  \CJK@postPunct{CNS-01}{22}{68, 6A, 6B, 6D,
                             6F, 70, 71, 72, 73, 74, 75, 76, 77, 78}
\endgroup


% Instead of defining a new environment, we modify some commands to activate
%     CNS if you enter a CJK environment.

\let\CJK@active@ \CJK@active
\def\CJK@active{
  \CNS@active
  \CJK@active@
  \CNS@encoding}

\let\CJK@inactive@ \CJK@inactive
\def\CJK@inactive{
  \CNS@inactive
  \CJK@inactive@}

\let\CJK@let@ \CJK@let
\def\CJK@let{
  \CNS@let
  \CJK@let@}

\let\CJK@altdefs@ \CJK@altdefs
\def\CJK@altdefs{
  \def\CNS@char##1##2##3{
    \protect\CNS@@char\CJK@protect{##1}{##2}{##3}
    \ignorespaces}
  \def\CNS@punctchar##1##2##3{
    \protect\CNS@@punctchar\CJK@protect{##1}{##2}{##3}
    \ignorespaces}
  \def\CNS@BigFivechar##1##2{
    \protect\CNS@@BigFivechar\CNS@protect{##1}{##2}
    \ignorespaces}
  \CJK@altdefs@}

\chardef\CNS@min "021
\chardef\CNS@max "07E
\chardef\CNS@BigFiveFirstMin "0A1
\chardef\CNS@BigFiveFirstMax "0FE
\chardef\CNS@BigFiveSecondMin "040
\chardef\CNS@BigFiveSecondMax "0FE


\def\CNS@series{\f@series}
\edef\CNS@shape{song}
\def\CNS@BigFiveSeries{\f@series}
\edef\CNS@BigFiveShape{song}


% reset endline character.

\endlinechar `\^^M

\endinput
