% ctex-common.def: common definitions for all ctex packages/classes
% vim:ft=tex

\@ifclassloaded{article}{\def\CTEX@cls@article{}}{}
\@ifclassloaded{report}{\def\CTEX@cls@report{}}{}
\@ifclassloaded{book}{\def\CTEX@cls@book{}}{}

% Load some extra packages

\RequirePackage{ifpdf}
\RequirePackage{ifxetex}

\ifxetex
  \CTEX@GBKfalse % XeTeX always use UTF-8 as default encoding
\fi

\RequirePackage{keyval}[1999/03/16]
\ifCTEX@indent
  \RequirePackage{indentfirst}
\fi
\RequirePackage{fix-cm}
\ifCTEX@fancyhdr
  \RequirePackage{fancyhdr}
\fi

% Useful definitions

\DeclareRobustCommand\CTeX{C\kern-.05em\TeX{}}
\newcommand*\CTEX@key{\define@key{CTEX}}
\newcommand*\CTEXoptions[1][]{\setkeys{CTEX}{#1}}
\newcommand*\CTEX@subkey[1]{\define@key{CTEX#1}}
\newcommand*\CTEXsetup[2][]{\setkeys{CTEX#2}{#1}}

\def\ifCTEX@cls#1{%
  \expandafter\ifx\csname CTEX@cls@#1\endcsname\relax
    \expandafter\@secondoftwo
  \else
    \expandafter\@firstoftwo
  \fi}
\def\CTEX@replacecommand#1#2#3{%
  \expandafter\expandafter\expandafter\let\expandafter
    \csname #1#3\expandafter\endcsname
    \csname #2#3\endcsname
  \expandafter\expandafter\expandafter\def\expandafter
    \csname #2#3\expandafter\endcsname
    {\csname #1#3\endcsname}}

% Select engine: xetex has the highest priority, if not,
% then try cct, otherwise we use traditional cjk.

\ifxetex
  \input{ctex-xecjk-engine.def}
\else
  \ifCTEX@cct
    \input{ctex-cct-engine.def}
  \else
    \input{ctex-cjk-engine.def}
  \fi
\fi

\AtEndOfPackage{%
  \makeatletter
  \ifCTEX@GBK
    \input{ctex-gbk.def}
  \else
    \input{ctex-utf8.def}
  \fi\makeatother}

\newcommand*\CTEXindent{\CTEXsetfont\parindent2\ccwd}
\newcommand*\CTEXnoindent{\parindent\z@}
\ifCTEX@indent
  \AtBeginDocument{\CTEXindent}
\fi
\def\CTEX@spaceChar{\hskip \f@size \p@}
\def\baselinestretch{1.3}

% About font sizing

\def\CTEX@fs@eight{5.02}   \def\CTEX@fs@eightskip{6.02}   %八号字    5bp
\def\CTEX@fs@seven{5.52}   \def\CTEX@fs@sevenskip{6.62}   %七号字  5.5bp
\def\CTEX@fs@ssix{6.52}    \def\CTEX@fs@ssixskip{7.83}    %小六号  6.5bp
\def\CTEX@fs@six{7.53}     \def\CTEX@fs@sixskip{9.03}     %六号字  7.5bp
\def\CTEX@fs@sfive{9.03}   \def\CTEX@fs@sfiveskip{10.84}  %小五号    9bp
\def\CTEX@fs@five{10.54}   \def\CTEX@fs@fiveskip{12.65}   %五号字 10.5bp
\def\CTEX@fs@sfour{12.05}  \def\CTEX@fs@sfourskip{14.45}  %小四号   12bp
\def\CTEX@fs@four{14.05}   \def\CTEX@fs@fourskip{16.86}   %四号字   14bp
\def\CTEX@fs@sthree{15.06} \def\CTEX@fs@sthreeskip{18.07} %小三号   15bp
\def\CTEX@fs@three{16.06}  \def\CTEX@fs@threeskip{19.27}  %三号字   16bp
\def\CTEX@fs@stwo{18.07}   \def\CTEX@fs@stwoskip{21.68}   %小二号   18bp
\def\CTEX@fs@two{22.08}    \def\CTEX@fs@twoskip{26.50}    %二号字   22bp
\def\CTEX@fs@sone{24.09}   \def\CTEX@fs@soneskip{28.91}   %小一号   24bp
\def\CTEX@fs@one{26.10}    \def\CTEX@fs@oneskip{31.32}    %一号字   26bp
\def\CTEX@fs@szero{36.14}  \def\CTEX@fs@szeroskip{43.36}  %小初号   36bp
\def\CTEX@fs@zero{42.16}   \def\CTEX@fs@zeroskip{50.59}   %初号字   42bp
\DeclareMathSizes{\CTEX@fs@eight}{\CTEX@fs@eight}{5}{5}
\DeclareMathSizes{\CTEX@fs@seven}{\CTEX@fs@seven}{5}{5}
\DeclareMathSizes{\CTEX@fs@ssix}{\CTEX@fs@ssix}{5}{5}
\DeclareMathSizes{\CTEX@fs@six}{\CTEX@fs@six}{5}{5}
\DeclareMathSizes{\CTEX@fs@sfive}{\CTEX@fs@sfive}{6}{5}
\DeclareMathSizes{\CTEX@fs@five}{\CTEX@fs@five}{7}{5}
\DeclareMathSizes{\CTEX@fs@sfour}{\CTEX@fs@sfour}{8}{6}
\DeclareMathSizes{\CTEX@fs@four}
                 {\CTEX@fs@four}{\CTEX@fs@five}{\CTEX@fs@six}
\DeclareMathSizes{\CTEX@fs@sthree}
                 {\CTEX@fs@sthree}{\CTEX@fs@sfour}{\CTEX@fs@sfive}
\DeclareMathSizes{\CTEX@fs@three}
                 {\CTEX@fs@three}{\CTEX@fs@four}{\CTEX@fs@five}
\DeclareMathSizes{\CTEX@fs@stwo}
                 {\CTEX@fs@stwo}{\CTEX@fs@sthree}{\CTEX@fs@sfour}
\DeclareMathSizes{\CTEX@fs@two}
                 {\CTEX@fs@two}{\CTEX@fs@three}{\CTEX@fs@four}
\DeclareMathSizes{\CTEX@fs@sone}
                 {\CTEX@fs@sone}{\CTEX@fs@stwo}{\CTEX@fs@sthree}
\DeclareMathSizes{\CTEX@fs@one}
                 {\CTEX@fs@one}{\CTEX@fs@two}{\CTEX@fs@three}
\DeclareMathSizes{\CTEX@fs@szero}
                 {\CTEX@fs@szero}{\CTEX@fs@sone}{\CTEX@fs@stwo}
\DeclareMathSizes{\CTEX@fs@zero}
                 {\CTEX@fs@zero}{\CTEX@fs@one}{\CTEX@fs@two}
\def\CTEX@zihao{}
\DeclareRobustCommand*\zihao[1]{\def\CTEX@zihao{#1}%
  \ifnum #11<0%
    \@tempcnta=-#1
    \ifcase\@tempcnta%
        \fontsize\CTEX@fs@szero\CTEX@fs@szeroskip%
    \or \fontsize\CTEX@fs@sone\CTEX@fs@soneskip%
    \or \fontsize\CTEX@fs@stwo\CTEX@fs@stwoskip%
    \or \fontsize\CTEX@fs@sthree\CTEX@fs@sthreeskip%
    \or \fontsize\CTEX@fs@sfour\CTEX@fs@sfourskip%
    \or \fontsize\CTEX@fs@sfive\CTEX@fs@sfiveskip%
    \or \fontsize\CTEX@fs@ssix\CTEX@fs@ssixskip%
    \else \PackageError{ctex}{%
            Undefined Chinese font size in command \protect\zihao}{%
            The old font size is used if you continue.}%
    \fi%
  \else%
    \@tempcnta=#1
    \ifcase\@tempcnta%
        \fontsize\CTEX@fs@zero\CTEX@fs@zeroskip%
    \or \fontsize\CTEX@fs@one\CTEX@fs@oneskip%
    \or \fontsize\CTEX@fs@two\CTEX@fs@twoskip%
    \or \fontsize\CTEX@fs@three\CTEX@fs@threeskip%
    \or \fontsize\CTEX@fs@four\CTEX@fs@fourskip%
    \or \fontsize\CTEX@fs@five\CTEX@fs@fiveskip%
    \or \fontsize\CTEX@fs@six\CTEX@fs@sixskip%
    \or \fontsize\CTEX@fs@seven\CTEX@fs@sevenskip%
    \or \fontsize\CTEX@fs@eight\CTEX@fs@eightskip%
    \else \PackageError{ctex}{%
            Undefined Chinese font size in command \protect\zihao}{%
            The old font size is used if you continue.}%
    \fi%
  \fi%
  \selectfont\ignorespaces}

% About numbers

\newif\ifCTEX@zero@
\newif\ifCTEX@previous@
\newif\ifCTEX@null@
\newcount\CTEX@q
\newcount\CTEX@r
\def\CTEX@appendstring#1#2{%
  \expandafter\def\expandafter#1\expandafter{#1#2}}
\def\CTEX@appendnumber#1#2{%
  \ifcase #2\relax
    \ifCTEX@null@
      \CTEX@appendstring{#1}{\CTEX@null}%
    \else
      \CTEX@appendstring{#1}{\CTEX@zero}%
    \fi
  \or \CTEX@appendstring{#1}{\CTEX@one}%
  \or \CTEX@appendstring{#1}{\CTEX@two}%
  \or \CTEX@appendstring{#1}{\CTEX@three}%
  \or \CTEX@appendstring{#1}{\CTEX@four}%
  \or \CTEX@appendstring{#1}{\CTEX@five}%
  \or \CTEX@appendstring{#1}{\CTEX@six}%
  \or \CTEX@appendstring{#1}{\CTEX@seven}%
  \or \CTEX@appendstring{#1}{\CTEX@eight}%
  \or \CTEX@appendstring{#1}{\CTEX@nine}%
  \fi}
\def\CTEX@splitnumber#1{%
  \CTEX@q #1\relax
  \CTEX@r #1\relax
  \divide\CTEX@q \@M
  \begingroup
    \multiply\CTEX@q \@M
    \advance\CTEX@r -\CTEX@q
    \ifnum\CTEX@r = \z@
      \xdef\CTEX@low{}%
    \else
      \xdef\CTEX@low{\number\CTEX@r}%
    \fi
  \endgroup
  \ifnum\CTEX@q > \z@
    \CTEX@r \CTEX@q
    \divide\CTEX@q \@M
    \begingroup
      \multiply\CTEX@q \@M
      \advance\CTEX@r -\CTEX@q
      \ifnum\CTEX@r = \z@
        \xdef\CTEX@high{}%
      \else
        \xdef\CTEX@high{\number\CTEX@r}%
      \fi
    \endgroup
    \ifnum\CTEX@q > \z@
      \xdef\CTEX@yi{\number\CTEX@q}%
    \else
      \xdef\CTEX@yi{}%
    \fi
  \else
    \xdef\CTEX@high{}%
    \xdef\CTEX@yi{}%
  \fi
}
\def\CTEX@processnumber#1#2{%
  \CTEX@zero@false
  \CTEX@q #2\relax
  \CTEX@r #2\relax
  \divide\CTEX@q \@m
  \ifnum\CTEX@q = \z@
    \ifCTEX@previous@
      \CTEX@zero@true
    \fi
  \else
    \ifCTEX@zero@
      \CTEX@appendstring{#1}{\CTEX@zero}%
    \fi
    \CTEX@appendnumber{#1}{\CTEX@q}%
    \CTEX@appendstring{#1}{\CTEX@thousand}%
    \CTEX@previous@true
    \CTEX@zero@false
  \fi
  \multiply\CTEX@q \@m
  \advance\CTEX@r -\CTEX@q
  \CTEX@q \CTEX@r
  \divide\CTEX@q 100\relax
  \ifnum\CTEX@q = \z@
    \ifCTEX@previous@
      \CTEX@zero@true
    \fi
  \else
    \ifCTEX@zero@
      \CTEX@appendstring{#1}{\CTEX@zero}%
    \fi
    \CTEX@appendnumber{#1}{\CTEX@q}%
    \CTEX@appendstring{#1}{\CTEX@hundred}%
    \CTEX@previous@true
    \CTEX@zero@false
  \fi
  \multiply\CTEX@q 100
  \advance\CTEX@r -\CTEX@q
  \CTEX@q \CTEX@r
  \divide \CTEX@q 10\relax
  \ifnum\CTEX@q = \z@
    \ifCTEX@previous@
      \CTEX@zero@true
    \fi
  \else
    \ifCTEX@zero@
      \CTEX@appendstring{#1}{\CTEX@zero}%
    \fi
    \ifnum\CTEX@q = \@ne
      \ifCTEX@previous@
        \CTEX@appendstring{#1}{\CTEX@one}%
      \fi
    \else
      \CTEX@appendnumber{#1}{\CTEX@q}%
    \fi
    \CTEX@appendstring{#1}{\CTEX@ten}%
    \CTEX@previous@true
    \CTEX@zero@false
  \fi
  \multiply\CTEX@q 10
  \advance\CTEX@r -\CTEX@q
  \ifnum\CTEX@r = \z@
  \else
    \ifCTEX@zero@
      \CTEX@appendstring{#1}{\CTEX@zero}%
    \fi
    \CTEX@appendnumber{#1}{\CTEX@r}%
    \CTEX@previous@true
  \fi}
\DeclareRobustCommand\CTEXnumber[2]{%
  \def#1{}%
  \CTEX@null@false
  \CTEX@q #2\relax
  \ifnum\CTEX@q < \z@
    \multiply\CTEX@q \m@ne
    \CTEX@appendstring{#1}{\CTEX@minus}%
  \fi
  \CTEX@previous@false
  \CTEX@zero@false
  \ifnum\CTEX@q = \z@
    \CTEX@appendstring{#1}{\CTEX@zero}%
  \else
    \CTEX@splitnumber{\CTEX@q}%
    \ifx\CTEX@yi \@empty
    \else
      \CTEX@processnumber{#1}{\CTEX@yi}%
      \CTEX@appendstring{#1}{\CTEX@hundredmillion}%
    \fi
    \ifx\CTEX@high \@empty
    \else
      \CTEX@processnumber{#1}{\CTEX@high}%
      \CTEX@appendstring{#1}{\CTEX@tenthousand}%
    \fi
    \ifx\CTEX@low \@empty
    \else
      \ifx\CTEX@yi \@empty
      \else
        \ifx\CTEX@high \@empty
          \CTEX@appendstring{#1}{\CTEX@zero}% this catches 100002345
        \fi
      \fi
      \CTEX@processnumber{#1}{\CTEX@low}%
    \fi
  \fi}
\def\CTEX@getdigit#1#2\@nil{%
  \edef\CTEX@tempa{#1}%
  \edef\CTEX@tempb{#2}}
\DeclareRobustCommand\CTEXdigits[2]{%
  \def#1{}%
  \CTEX@null@true
  \edef\CTEX@tempa{}%
  \edef\CTEX@tempb{#2}%
  \ifx\CTEX@tempb \@empty
  \else
    \loop
      \expandafter\CTEX@getdigit\CTEX@tempb\@nil
      \CTEX@appendnumber{#1}{\CTEX@tempa}%
      \ifx\CTEX@tempb \@empty
      \else
    \repeat
  \fi}
\DeclareRobustCommand\CTEXcounter[1]{%
  \@ifundefined{c@#1}{}{%
    \CTEXnumber{\reserved@a}{\@arabic\csname c@#1\endcsname}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\csname cc@#1\endcsname%
    \expandafter{\reserved@a}}}
\AtBeginDocument{%
  \makeatletter%
  \@ifundefined{CTEX@save@setcounter}{%
    \let\CTEX@save@setcounter\setcounter%
    \def\setcounter#1#2{%
        \CTEX@save@setcounter{#1}{#2}%
        \CTEXcounter{#1}}}{}
  \@ifundefined{CTEX@save@addtocounter}{%
    \let\CTEX@save@addtocounter\addtocounter%
    \def\addtocounter#1#2{%
        \CTEX@save@addtocounter{#1}{#2}%
        \CTEXcounter{#1}}}{}
  \@ifundefined{CTEX@save@stepcounter}{%
    \let\CTEX@save@stepcounter\stepcounter%
    \def\stepcounter#1{%
        \CTEX@save@stepcounter{#1}%
        \CTEXcounter{#1}}}{}
  \makeatother}
\def\chinese#1{%
  \@ifundefined{cc@#1}{\CTEX@null}{\csname cc@#1\endcsname}}

% About caption

\ifCTEX@caption
  \let\CTEX@save@refstepcounter\refstepcounter
  \def\refstepcounter#1{\CTEX@save@refstepcounter{#1}%
    \protected@edef\@currentlabel
      {\csname p@#1\endcsname%
       \@ifundefined{CTEX@the#1}%
         {\csname the#1\endcsname}%
         {\csname CTEX@the#1\endcsname}%
       }}%
\fi

% `today' definitions

\let\CTEX@todayold\today
\ifCTEX@caption
  \renewcommand*\today{\CTEX@todaysmall}
\fi
\CTEX@key{today}{\CTEX@settoday{#1}}
\newcommand*\CTEX@settoday[1]{%
  \@ifundefined{CTEX@today#1}
    {\PackageError{ctex}{%
       unknown today format}{%
       Available today format are "old", "small", and "big".}}
    {\renewcommand*\today{\csname CTEX@today#1\endcsname}}}

% Put hyperref as bottom as possible, otherwise there may be page
% count issues
\ifCTEX@hyperref
  \ifxetex
    \RequirePackage{hyperref}
  \else
    \ifpdf % pdftex
      \RequirePackage[pdftex]{hyperref}
    \else % dvipdfmx
      \ifCTEX@GBK
        \AtBeginDvi{\special{pdf:tounicode GBK-EUC-UCS2}}
      \fi
      \RequirePackage[dvipdfmx]{hyperref}
    \fi
    \ifCTEX@GBK
      \hypersetup{CJKbookmarks=true}
    \else
      \hypersetup{unicode=true}
    \fi
  \fi
  \hypersetup{colorlinks=true}
\fi

\endinput

