% This is the file CJK.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-1995)

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


\newif\ifCJK@lowercase@
\CJK@lowercase@false
\DeclareOption{lowercase}{\CJK@lowercase@true}

\DeclareOption{global}{\let\CJK@global \global
                       \let\CJK@local \global}
\DeclareOption{local}{\let\CJK@global \relax
                      \let\CJK@local \global}
\DeclareOption{encapsulated}{\let\CJK@global \relax
                             \let\CJK@local \relax}

\ExecuteOptions{local}

\ProcessOptions


% we suppress any unwanted spaces produced by linefeeds.

\endlinechar \m@ne


% The \CJK@protect macros will protect its arguments. They expand to itselves.
%     Most of the following protection mechanism are modified macros from
%     german.sty .

\def\CJK@protect#1#2#3{
  \string\CJK@protect{\string#1}{\string#2}{\string#3}}

\def\CJK@protectx#1#2#3#4{
  \string\CJK@protectx{\string#1}{\string#2}{\string#3}{\string#4}}

\def\CJK@protectxx#1#2#3#4#5{
  \string\CJK@protectxx{\string#1}{\string#2}{\string#3}{\string#4}{\string#5}}


% a set of macros to make the expansion robust.
%
% please read CJK.txt for caveats concerning DC fonts and Big 5 encoding.

\def\CJK@char#1#2#3{
  \protect\CJK@@char\CJK@protect{#1}{#2}{#3}}
\def\CJK@@char#1#{
  \CJK@@@char}

\def\CJK@charx#1#2#3#4{
  \protect\CJK@@charx\CJK@protectx{#1}{#2}{#3}{#4}}
\def\CJK@@charx#1#{
  \CJK@@@charx}

% macros for hangul characters and UTF 8 encoding.

\def\CJK@altchar#1#2#3{
  \protect\CJK@@altchar\CJK@protect{#1}{#2}{#3}}
\def\CJK@@altchar#1#{
  \CJK@@@altchar}

\def\CJK@altcharx#1#2#3#4{
  \protect\CJK@@altcharx\CJK@protectx{#1}{#2}{#3}{#4}}
\def\CJK@@altcharx#1#{
  \CJK@@@altcharx}

% macros for punctuation characters.

\def\CJK@punctchar#1#2#3#4{
  \protect\CJK@@punctchar\CJK@protectx{#1}{#2}{#3}{#4}}
\def\CJK@@punctchar#1#{
  \CJK@@@punctchar}

\def\CJK@punctcharx#1#2#3#4#5{
  \protect\CJK@@punctcharx\CJK@protectxx{#1}{#2}{#3}{#4}{#5}}
\def\CJK@@punctcharx#1#{
  \CJK@@@punctcharx}


% the auxiliary macro \CJK@charToHex (\CJK@numbToHex) converts a character
%     (a number) #2 into a hex string and assigns it globally to #1.

\def\CJK@charToHex#1#2{
  {\count\z@ `#2
   \count\tw@ \count\z@
   \divide\count\z@ \sixt@@n
   \count@ \count\z@
   \multiply\count@ \sixt@@n
   \advance\count\tw@ -\count@
   \xdef#1{
     \hexnumber@{\count\z@}\hexnumber@{\count\tw@}}}}

\def\CJK@numbToHex#1#2{
  {\count\z@ #2
   \count\tw@ \count\z@
   \divide\count\z@ \sixt@@n
   \count@ \count\z@
   \multiply\count@ \sixt@@n
   \advance\count\tw@ -\count@
   \xdef#1{
     \hexnumber@{\count\z@}\hexnumber@{\count\tw@}}}}


% with \CJKchar you can access a character directly at a low level:
%     the parameters are the first and the second byte of the CJK character
%     code.

\let\CJK@altchar@ \relax        % only defined in hangul.chr (and UTF8.chr)
\let\CJK@altcharx@ \relax
\let\CJK@charx@ \relax          % not defined for pmC macros
\let\CJK@punctcharx@ \relax

\DeclareRobustCommand{\CJKchar}[2]{
  {\let\CJK@char \CJK@char@
   \let\CJK@charx \CJK@charx@
   \let\CJK@altchar \CJK@altchar@
   \let\CJK@altcharx \CJK@altcharx@
   \let\CJK@punctchar \CJK@punctchar@
   \let\CJK@punctcharx \CJK@punctcharx@

   \edef\CJK@temp{UTF8}
   \ifx\CJK@temp \CJK@enc
     \PackageError{CJK}{
       You can't use \protect\CJKchar\space here}{
       You must deactivate the UTF 8 encoding to use \protect\CJKchar.}
   \fi
   \CJK@numbToHex{\CJK@temp}{#1}
   \csname CJK@\CJK@temp\endcsname{#2}}}


% with \Unicode you can access a Unicode character directly at a low level:
%     the parameters are the first and the second byte of the Unicode (real
%     Unicode values, not UTF 2).

\DeclareRobustCommand{\Unicode}[2]{
  {\edef\CJK@temp{UTF8}
   \ifx\CJK@temp \CJK@enc
   \else
     \PackageError{CJK}{
       You can't use \protect\Unicode\space here}{
       You must activate the UTF 8 encoding to use \protect\Unicode.}
   \fi
   \CJK@numbToHex{\CJK@temp}{#1}
   \edef\CJK@plane{\CJK@temp}
   \usefont{U}{\CJK@enc}{\CJK@series}{\CJK@shape}

   \expandafter\CJK@ifNotInList
    \expandafter{\expandafter\CJK@uniPunct\expandafter}
    \expandafter{\CJK@plane}
     \CJK@testLastKern
     \symbol{#2}
   \else
     \CJK@numbToHex{\CJK@temp}{#2}
     \CJK@ifNotPostPunct{UTF8}{\CJK@plane}{\CJK@temp}
       \CJKglue
     \fi
     \symbol{#2}
     \CJK@ifNotPrePunct{UTF8}{\CJK@plane}{\CJK@temp}
     \else
       \CJK@kern
     \fi
   \fi}}


% you will find the innermost routines of the macros defined above in the
%     xxx.chr files. These routines are
%
%         \CJK@@@char, \CJK@@@charx,
%         \CJK@char@, \CJK@charx@, 
%         \CJK@@@altchar, \CJK@@@altcharx,
%         \CJK@altchar@, \CJK@altcharx@
%         \CJK@@@punctchar, \CJK@@@punctcharx,
%         \CJK@punctchar@, \CJK@punctcharx@    .


% we need to scan comma separated lists of characters codes (represented as
%     hexadecimal strings) to see whether a particular character is in the
%     list. The pseudo-\if macro \CJK@ifNotInList will expand to \ifalse in
%     this case. Parameter #1 is the list, #2 the actual character code to
%     test.
%
% see the documentation of the \ifnot@excluded macro in the doc package of the
%     standard LaTeX2e distribution for a detailed explanation of the
%     following tricky definitions.

\def\CJK@stop{\CJK@stop}

\def\CJK@ifNotInList#1#2{
  \def\CJK@tempa##1,#2,##2\CJK@stop{
    \def\CJK@tempb{##2}
    \ifx\CJK@tempb \@empty}
  \expandafter\CJK@tempa\expandafter,#1,#2,\CJK@stop}


% lists defined in CJK.enc for plane yy with encoding xx are assigned to
%     global macros \CJK@xx@yy@prePunct and \CJK@xx@yy@postPunct.

\def\CJK@prePunct#1#2#3{
  \expandafter\gdef\csname CJK@#1@#2@prePunct\endcsname{#3}}

\def\CJK@postPunct#1#2#3{
  \expandafter\gdef\csname CJK@#1@#2@postPunct\endcsname{#3}}


% the next two functions scan the lists of punctuation characters of a
%     particular plane which should not occur at the beginning or at the end
%     of a line. They will be used in the xxx.chr files. Parameters are the
%     encoding, the plane and the character to be tested. Again these macros
%     are pseudo-\ifs.

\def\CJK@ifNotPrePunct#1#2#3{
  \expandafter\expandafter\expandafter
  \CJK@ifNotInList
  \expandafter\csname CJK@#1@#2@prePunct\expandafter\endcsname
  \expandafter{#3}}

\def\CJK@ifNotPostPunct#1#2#3{
  \expandafter\expandafter\expandafter
  \CJK@ifNotInList
  \expandafter\csname CJK@#1@#2@postPunct\expandafter\endcsname
  \expandafter{#3}}


% CJK.enc contains the \CJK@...@enc macros.

\input CJK.enc


% \CJK@testLastKern checks the last kern. If it is equal to \CJK@kern,
%     \CJK@glue is suppressed.

\edef\CJK@kern{\kern 1sp}
\let\CJKkern \CJK@kern

\def\CJK@testLastKern{
  \ifdim\lastkern = 1sp
  \else
    \CJKglue
  \fi}


% we only check for lower and upper bounds, not for possible gaps in the range
%     of the second byte, as it happens for Big 5 encoding.

\def\CJK@err{
  \PackageError{CJK}{
    Invalid character code}{
    The second byte of the CJK code is out of range.\MessageBreak
    Do you use the right encoding scheme?}}

\def\CJK@errx{
  \PackageError{CJK}{
    Invalid character code}{
    The third byte of the CJK code is out of range.\MessageBreak
    Do you use the right encoding scheme?}}


% the low level macros \CJKenc and \CJKshape change CJK font attributes inside
%     of a CJK environment.

\DeclareRobustCommand{\CJKenc}[1]{
  \expandafter\ifx\csname CJK@#1@enc\endcsname \relax
    \PackageError{CJK}{
      The encoding #1 is not defined}{}
  \else
    \csname CJK@#1@enc\endcsname
  \fi
  \edef\CJK@enc{#1}}

\DeclareRobustCommand{\CJKshape}[1]{
  \edef\CJK@shape{#1}}


% we now define a set of new size-functions:
%     `CJK', `sCJK', `CJKfixed', and `sCJKfixed' are similar to `' (empty),
%     `s', `fixed', and `sfixed' except that the value of \CJK@plane is
%     appended to \font@name.

\DeclareSizeFunction{CJK}{\CJK@empty\@font@warning}
\DeclareSizeFunction{sCJK}{\CJK@empty\@font@info}

\def\CJK@empty#1{
  \@tempdimb \f@size\p@
  \ifx\optional@arg \@empty
  \else
    \@tempdimb \optional@arg\@tempdimb
    #1{Font shape `\curr@fontshape' will be\MessageBreak
       scaled to size \the\@tempdimb}
  \fi
  \edef\external@font{
    \mandatory@arg\CJK@plane\space at \the\@tempdimb}
  \xdef\font@name{
    \csname \curr@fontshape/\f@size/\CJK@plane\endcsname}}

\DeclareSizeFunction{CJKfixed}{\CJK@fixed\@font@warning}
\DeclareSizeFunction{sCJKfixed}{\CJK@fixed\@font@info}

\def\CJK@fixed#1{
  \ifx\optional@arg \@empty
    \edef\external@font{
      \mandatory@arg\CJK@plane}
  \else
    \edef\external@font{
      \mandatory@arg\CJK@plane\space at \optional@arg pt}
  \fi
  \xdef\font@name{
    \csname \curr@fontshape/\f@size/\CJK@plane\endcsname}
  #1{External font `\external@font' loaded for size\MessageBreak
     <\f@size>}}


% we also must redefine \pickup@font to append \CJK@plane to the font name if
%     the particular subfont is already known.

\def\pickup@font{
  \ifx\CJK@plane \@undefined
    \expandafter\ifx\font@name \relax%          % old definition
      \define@newfont
    \fi
  \else%                                        % CJK extension
    \expandafter
    \ifx\csname \curr@fontshape/\f@size/\CJK@plane\endcsname \relax
      \define@newfont
    \else
      \xdef\font@name{
        \csname \curr@fontshape/\f@size/\CJK@plane\endcsname}
    \fi
  \fi}


% \CJKglue defines the stretchability of CJK characters.

\newcommand{\CJKglue}{\hskip \z@ \@plus .08\baselineskip}


% increasing \CJKtolerance will allow more space between Hangul words.

\newcommand{\CJKtolerance}{400}


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

\def\CJK@active{
  \@tempcnta "0A1
  \loop
    \CJK@global\catcode\@tempcnta \active
    \ifnum\@tempcnta < "0FE
      \advance\@tempcnta \@ne
  \repeat}

\def\CJK@inactive{
  \@tempcnta "0A1
  \loop
    \CJK@global\catcode\@tempcnta 12
    \ifnum\@tempcnta < "0FE
      \advance\@tempcnta \@ne
  \repeat}


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

\CJK@active

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

\def\CJK@nameglet#1#2{
  \expandafter\CJK@global\expandafter\let\expandafter#1
  \csname CJK@#2\endcsname}

\def\CJK@let{
  \CJK@nameglet{^^a1}{A1}
  \CJK@nameglet{^^a2}{A2}
  \CJK@nameglet{^^a3}{A3}
  \CJK@nameglet{^^a4}{A4}
  \CJK@nameglet{^^a5}{A5}
  \CJK@nameglet{^^a6}{A6}
  \CJK@nameglet{^^a7}{A7}
  \CJK@nameglet{^^a8}{A8}
  \CJK@nameglet{^^a9}{A9}
  \CJK@nameglet{^^aa}{AA}
  \CJK@nameglet{^^ab}{AB}
  \CJK@nameglet{^^ac}{AC}
  \CJK@nameglet{^^ad}{AD}
  \CJK@nameglet{^^ae}{AE}
  \CJK@nameglet{^^af}{AF}
  \CJK@nameglet{^^b0}{B0}
  \CJK@nameglet{^^b1}{B1}
  \CJK@nameglet{^^b2}{B2}
  \CJK@nameglet{^^b3}{B3}
  \CJK@nameglet{^^b4}{B4}
  \CJK@nameglet{^^b5}{B5}
  \CJK@nameglet{^^b6}{B6}
  \CJK@nameglet{^^b7}{B7}
  \CJK@nameglet{^^b8}{B8}
  \CJK@nameglet{^^b9}{B9}
  \CJK@nameglet{^^ba}{BA}
  \CJK@nameglet{^^bb}{BB}
  \CJK@nameglet{^^bc}{BC}
  \CJK@nameglet{^^bd}{BD}
  \CJK@nameglet{^^be}{BE}
  \CJK@nameglet{^^bf}{BF}
  \CJK@nameglet{^^c0}{C0}
  \CJK@nameglet{^^c1}{C1}
  \CJK@nameglet{^^c2}{C2}
  \CJK@nameglet{^^c3}{C3}
  \CJK@nameglet{^^c4}{C4}
  \CJK@nameglet{^^c5}{C5}
  \CJK@nameglet{^^c6}{C6}
  \CJK@nameglet{^^c7}{C7}
  \CJK@nameglet{^^c8}{C8}
  \CJK@nameglet{^^c9}{C9}
  \CJK@nameglet{^^ca}{CA}
  \CJK@nameglet{^^cb}{CB}
  \CJK@nameglet{^^cc}{CC}
  \CJK@nameglet{^^cd}{CD}
  \CJK@nameglet{^^ce}{CE}
  \CJK@nameglet{^^cf}{CF}
  \CJK@nameglet{^^d0}{D0}
  \CJK@nameglet{^^d1}{D1}
  \CJK@nameglet{^^d2}{D2}
  \CJK@nameglet{^^d3}{D3}
  \CJK@nameglet{^^d4}{D4}
  \CJK@nameglet{^^d5}{D5}
  \CJK@nameglet{^^d6}{D6}
  \CJK@nameglet{^^d7}{D7}
  \CJK@nameglet{^^d8}{D8}
  \CJK@nameglet{^^d9}{D9}
  \CJK@nameglet{^^da}{DA}
  \CJK@nameglet{^^db}{DB}
  \CJK@nameglet{^^dc}{DC}
  \CJK@nameglet{^^dd}{DD}
  \CJK@nameglet{^^de}{DE}
  \CJK@nameglet{^^df}{DF}
  \CJK@nameglet{^^e0}{E0}
  \CJK@nameglet{^^e1}{E1}
  \CJK@nameglet{^^e2}{E2}
  \CJK@nameglet{^^e3}{E3}
  \CJK@nameglet{^^e4}{E4}
  \CJK@nameglet{^^e5}{E5}
  \CJK@nameglet{^^e6}{E6}
  \CJK@nameglet{^^e7}{E7}
  \CJK@nameglet{^^e8}{E8}
  \CJK@nameglet{^^e9}{E9}
  \CJK@nameglet{^^ea}{EA}
  \CJK@nameglet{^^eb}{EB}
  \CJK@nameglet{^^ec}{EC}
  \CJK@nameglet{^^ed}{ED}
  \CJK@nameglet{^^ee}{EE}
  \CJK@nameglet{^^ef}{EF}
  \CJK@nameglet{^^f0}{F0}
  \CJK@nameglet{^^f1}{F1}
  \CJK@nameglet{^^f2}{F2}
  \CJK@nameglet{^^f3}{F3}
  \CJK@nameglet{^^f4}{F4}
  \CJK@nameglet{^^f5}{F5}
  \CJK@nameglet{^^f6}{F6}
  \CJK@nameglet{^^f7}{F7}
  \CJK@nameglet{^^f8}{F8}
  \CJK@nameglet{^^f9}{F9}
  \CJK@nameglet{^^fa}{FA}
  \CJK@nameglet{^^fb}{FB}
  \CJK@nameglet{^^fc}{FC}
  \CJK@nameglet{^^fd}{FD}
  \CJK@nameglet{^^fe}{FE}}


% these auxiliary macros reset \lccode's and \uccode's of characters
%     0x80-0xFE. Interestingly LaTeX2e sets these values for non-DC fonts
%     also.

\def\CJK@upperReset{
  \@tempcnta "080
  \loop
    \CJK@local\uccode\@tempcnta \z@
    \ifnum\@tempcnta < "0FF
      \advance\@tempcnta \@ne
  \repeat}

\def\CJK@lowerReset{
  \@tempcnta "080
  \loop
    \CJK@local\lccode\@tempcnta \z@
    \ifnum\@tempcnta < "0FF
      \advance\@tempcnta \@ne
  \repeat}


% finally come the CJK-environments:
%
% CJK* will swallow spaces after a CJK character, whereas CJK will not.
%
% the first parameter selects the CJK encoding, the second the CJK font
%     shape.
%
% always end the line after saying \begin{CJK} or \begin{CJK*};
%     any characters on the same line will have already been read with
%     their non-CJK meanings.

\newenvironment{CJK}[2]{
  \CJK@upperReset
  \ifCJK@lowercase@
    \CJK@lowerReset
  \fi

  \CJK@active

  \edef\CJK@enc{#1}%                % this is the font family !
  \def\CJK@series{\f@series}
  \edef\CJK@shape{#2}

  \expandafter\ifx\csname CJK@#1@enc\endcsname \relax
    \PackageError{CJK}{
      The encoding #1 is not defined}{}
  \else
    \csname CJK@#1@enc\endcsname%   % must be defined for each CJK encoding
  \fi
  }{\CJK@inactive}

\def\CJK@altdefs{
  \def\CJK@char##1##2##3{
    \protect\CJK@@char\CJK@protect{##1}{##2}{##3}
    \ignorespaces}
  \def\CJK@charx##1##2##3##4{
    \protect\CJK@@charx\CJK@protectx{##1}{##2}{##3}{##4}
    \ignorespaces}
  \def\CJK@altchar##1##2##3{
    \protect\CJK@@altchar\CJK@protect{##1}{##2}{##3}
    \ignorespaces}
  \def\CJK@altcharx##1##2##3##4{
    \protect\CJK@@altcharx\CJK@protectx{##1}{##2}{##3}{##4}
    \ignorespaces}
  \def\CJK@punctchar##1##2##3##4{
    \protect\CJK@@punctchar\CJK@protectx{##1}{##2}{##3}{##4}
    \ignorespaces}
  \def\CJK@punctcharx##1##2##3##4##5{
    \protect\CJK@@punctcharx\CJK@protectxx{##1}{##2}{##3}{##4}{##5}
    \ignorespaces}}

\newenvironment{CJK*}[2]{
  \CJK@altdefs

  \CJK@upperReset
  \ifCJK@lowercase@
    \CJK@lowerReset
  \fi

  \CJK@active

  \edef\CJK@enc{#1}
  \def\CJK@series{\f@series}
  \edef\CJK@shape{#2}

  \expandafter\ifx\csname CJK@#1@enc\endcsname \relax
    \PackageError{CJK}{
      The encoding #1 is not defined}{}
  \else
    \csname CJK@#1@enc\endcsname
  \fi
  }{\CJK@inactive}


% switch the active characters back off until they are needed.

\CJK@inactive


% reset endline character.

\endlinechar `\^^M

\endinput
