%
% pxgtfont.sty
%

%%%% package declaration
\NeedsTeXFormat{pLaTeX2e}
\ProvidesPackage{pxgtfont}[2009/06/14 v0.2 GT font setting]

%%%% process options
\newif\ifpxgf@main \pxgf@mainfalse
\DeclareOption{mainfont}{\pxgf@maintrue}
\newif\ifpxgf@fake@gtfonts
\DeclareOption{gtfonts}{\pxgf@fake@gtfontstrue}
\DeclareOption{gtftex}{\pxgf@fake@gtfontsfalse}
\ProcessOptions*

%%%% preparation
\def\pxgf@pkgname{pxgtfont}
\newif\ifpxgf@needclearup \pxgf@needclearupfalse
\newif\ifpxgf@ok
\newif\ifpxgf@asgok
\newif\ifpxgf@upTeX

%%%% error messages
\def\pxgf@error{\PackageError\pxgf@pkgname}
\def\pxgf@err@ivgtn{\pxgf@error{invalid GT number}%
  {GT number must be in range 1 .. \pxgf@max@gtno.}}
\def\pxgf@err@syner#1{\pxgf@error{syntax error in \string#1}%
  {Argument of \string#1 has unknown form.}}
\def\pxgf@err@syner@GI{\pxgf@err@syner\GI}
\def\pxgf@err@syner@GT{\pxgf@err@syner\GT}
\def\pxgf@err@syner@GSJ{\pxgf@err@syner\GSJ}
\let\pxgf@err@syner@Gx\pxgf@err@syner@GI

%%%% \pxgf@max@gtno : largest available GT Number
%% Currently this value is set to the really largest Number.
%% However, I know mapping to sjis form only for GT Number
%% up to 69263. Thus use of all numbers above this value
%% will only result in a geta mark.
\def\pxgf@max@gtno{78774}
%% N.B. Revisions of GT Kanji Set
%   - Numbers 69264--78774 added on 2005/07/29
%   - Numbers 67548--69263 added on 2002/11/15
%   - Numbers 66774--67547 added on 2001/09/14

%%-------- GT Font setup

%%%% upTeX detection
\pxgf@upTeXfalse \pxgf@oktrue
{\edef\pxgf@a{\string\ucs}\edef\pxgf@b{\meaning\ucs}
 \ifx\pxgf@a\pxgf@b \global\pxgf@upTeXtrue \fi}
\def\pxgf@nxt#1#2{%
  \def\pxgf@JY{#1}\def\pxgf@JT{#2}%
  \lowercase{\def\pxgf@jy{#1}\def\pxgf@jt{#2}}}
\ifpxgf@upTeX
  \ifnum\ucs"3000="3000\else \pxgf@okfalse \fi
  \expandafter\ifx\csname T@JY2\endcsname\relax \pxgf@okfalse \fi
  \pxgf@nxt{JY2}{JT2}%
\else \pxgf@nxt{JY1}{JT1}%
\fi
\ifpxgf@ok\else \pxgf@error{Incompatible settings, aborted}\@eha
  \endinput\fi

%%%% set scale factor
\chardef\pxgf@scl=0
\@ifclassloaded{jarticle}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{jreport}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{jbook}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{tarticle}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{treport}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{tbook}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{jsarticle}{\chardef\pxgf@scl=2 }{}
\@ifclassloaded{jsbook}{\chardef\pxgf@scl=2 }{}
\@ifclassloaded{ujarticle}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{ujreport}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{ujbook}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{utarticle}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{utreport}{\chardef\pxgf@scl=1 }{}
\@ifclassloaded{utbook}{\chardef\pxgf@scl=1 }{}
\edef\pxgf@scl{\ifcase\pxgf@scl 1\or
  0.962221\or 0.924683\fi}

%%%% \pxgf@clearup
\def\pxgf@clearup{%
  \normalfont\normalsize
  \setbox0\hbox{\char\euc"A1A1}%
  \setlength\Cht{\ht0}\setlength\Cdp{\dp0}%
  \setlength\Cwd{\wd0}\setlength\Cvs{\baselineskip}%
  \setlength\Chs{\wd0}\setlength\parindent{1\Cwd}%
}
\@onlypreamble\pxgf@clearup

%%%% gtmc: Zenkaku Mikachan family
\def\pxgf@@gtmc{gtmc}
\DeclareKanjiFamily{\pxgf@JY}{gtmc}{}
\DeclareKanjiFamily{\pxgf@JT}{gtmc}{}
\def\pxgf@decl@font@Y{\DeclareFontShape{\pxgf@JY}{gtmc}}
\def\pxgf@decl@font@T{\DeclareFontShape{\pxgf@JT}{gtmc}}
\def\pxgf@decl@sub#1#2#3{%
  \pxgf@decl@font@Y{#1}{#2}{<->ssub*gtmc/m/#3}{}%
  \pxgf@decl@font@T{#1}{#2}{<->ssub*gtmc/m/#3}{}}
\def\pxgf@decl@ex#1#2{%
  \pxgf@decl@font@Y{m}{#1}{<->s*[\pxgf@scl]gtmc-r-#2}{}%
  \pxgf@decl@font@T{m}{#1}{<->ssub*gtmc/m/n}{}%
  \pxgf@decl@sub{bx}{#1}{#1}}
%% n
\pxgf@decl@font@Y{m}{n}{<->s*[\pxgf@scl]gtmc-r-\pxgf@jy}{}
\pxgf@decl@font@T{m}{n}{<->s*[\pxgf@scl]gtmc-r-\pxgf@jt}{}
\pxgf@decl@sub{m}{it}{n}
\pxgf@decl@sub{bx}{n}{n}\pxgf@decl@sub{bx}{it}{n}
%% 01,02,...
\def\pxgf@c{"110000}\pxgf@oktrue
\@for\pxgf@x:={01,02,03,04,05,06,07,08,09,10,11,12,13,k1,k2}\do{%
  \ifpxgf@ok \pxgf@decl@ex{01}{jy1}\pxgf@okfalse %x=01
  \else \pxgf@decl@ex{\pxgf@x}{g\pxgf@x}\fi
  \expandafter\let\csname pxgf@FN/\pxgf@x\endcsname=t\relax
  \ifpxgf@upTeX
   {\@tempcnta\pxgf@c\relax
    \global\expandafter\kchardef\csname pxgf@CB/\pxgf@x\endcsname\@tempcnta
    \advance\@tempcnta"10000 \xdef\pxgf@c{\the\@tempcnta}}%
  \fi}
%% z0,z1,...
\def\pxgf@get@fn#1{\uppercase{\edef\pxgf@fn{\number"#1}}}
\@tfor\pxgf@x:=0123456789a\do{%
  \expandafter\pxgf@get@fn\pxgf@x
  \pxgf@decl@ex{z\pxgf@fn}{zg\pxgf@x}}
%% u1,u2
\pxgf@decl@ex{u1}{j48}
\pxgf@decl@ex{u2}{zgu}

%%%%<*>\gtmcfamily, \textgtmc
\DeclareRobustCommand\gtmcfamily{%
  \not@math@alphabet\gtmcfamily\relax
  \kanjifamily{gtmc}\selectfont}
\DeclareRobustCommand\textgtmc[1]{%
  \ifmmode\expandafter\nfss@text\fi{\gtmcfamily#1}}

%%%% 'mainfont' option
\ifpxgf@main
\renewcommand\mcdefault{gtmc}
\renewcommand\kanjifamilydefault{\mcdefault}
\pxgf@needclearuptrue
\fi

%%-------- Character input commands

%%%%<+> \GI{<spec>,...}
%% Here spec is either GT Number or FF-SSSS form (sjis form;
%% FF is font id and SSSS is sjis code point in hex).
\DeclareRobustCommand*\GI[1]{%
  \edef\pxgf@nxt{\noexpand\@tfor\noexpand\pxgf@x:={#1}}%
  \pxgf@nxt\do{{\expandafter\pxgf@GI@a\pxgf@x--\@nil}}}
\def\pxgf@GI@a#1-#2-#3\@nil{%
  \ifx @#3@\pxgf@gtno{#1}\else \pxgf@subchar{#1}{#2}\fi}

%%%% \GSJ{<id>}{<sjis>}
%% Specialized command for sjis form.
\DeclareRobustCommand*\GSJ[2]{{%
  \let\pxgf@err@syner@Gx\pxgf@err@syner@GSJ
  \lowercase{\pxgf@subchar{#1}}{#2}}}

%%% \pxgf@gtno
\def\pxgf@gtno#1{%
  \pxgf@chkasg\@tempcnta=#1\relax
  \ifpxgf@ok \pxgf@gtno@a
  \else \pxgf@err@syner\GI \fi}
\ifpxgf@upTeX       %<upTeX>
\chardef\pxgf@max@gtno@\pxgf@max@gtno
\chardef\pxgf@Csh="110000
\def\pxgf@gtno@a{%
  \ifnum\@tempcnta<\@ne \pxgf@err@ivgtn
  \else \ifnum\@tempcnta>\pxgf@max@gtno@ \pxgf@err@ivgtn
  \else \def\pxgf@fsty{u1}\advance\@tempcnta\pxgf@Csh
        \chardef\pxgf@char\@tempcnta \pxgf@show@char
  \fi\fi}
\else               %<pTeX>
\def\pxgf@gtno@a{%
  \ifnum\@tempcnta<\@ne \pxgf@err@ivgtn
  \else \ifnum\@tempcnta>\pxgf@max@gtno\space \pxgf@err@ivgtn
  \else \pxgf@get@code \pxgf@show@char \fi\fi}
\fi                 %</upTeX>

%%%% \pxgf@get@code
\ifpxgf@upTeX\else  %<pTeX>
\mathchardef\pxgf@Cma=7614
\chardef\pxgf@Cmb=94 \chardef\pxgf@Cmc=162
\mathchardef\pxgf@Csd="2E21
\def\pxgf@get@code{%
  \advance\@tempcnta\m@ne \@tempcntb\@tempcnta
  \divide\@tempcntb\pxgf@Cma \edef\pxgf@fsty{z\the\@tempcntb}%
  \multiply\@tempcntb-\pxgf@Cma \advance\@tempcnta\@tempcntb
  \@tempcntb\@tempcnta \divide\@tempcntb\pxgf@Cmb
  \multiply\@tempcntb\pxgf@Cmc \advance\@tempcnta\@tempcntb
  \advance\@tempcnta\pxgf@Csd \chardef\pxgf@char\jis\@tempcnta\relax}
\fi                 %</pTeX>

%%%% \pxgf@subchar, \pxgf@surrochar
\def\pxgf@subchar#1#2{%
  \uppercase{\pxgf@chkasg\@tempcnta="#2\relax}%
  \ifpxgf@ok \def\pxgf@fsty{#1}\pxgf@subchar@a
  \else \pxgf@err@syner@Gx \fi}
\ifpxgf@upTeX       %<upTeX>
\def\pxgf@subchar@a{%
  \@tempcnta\sjis\@tempcnta\relax \pxgf@subchar@b}
\def\pxgf@subchar@b{%
  \expandafter\advance\expandafter\@tempcnta
   \csname pxgf@CB/\pxgf@fsty\endcsname
  \def\pxgf@fsty{u2}\kchardef\pxgf@char\@tempcnta \pxgf@show@char}
\def\pxgf@surrochar#1#2{%
  \pxgf@chkasg\@tempcnta=`#2?\relax
  \def\pxgf@fsty{#1}\pxgf@subchar@b}
\else               %<pTeX>
\def\pxgf@subchar@a{%
  \expandafter\ifx\csname pxgf@FN/\pxgf@fsty\endcsname\relax
    \pxgf@err@syner@Gx
  \else \chardef\pxgf@char\sjis\@tempcnta\relax \pxgf@show@char \fi}
\def\pxgf@surrochar#1#2{%
  \expandafter\ifx\csname pxgf@FN/#1\endcsname\relax
    \pxgf@err@syner@Gx
  \else \def\pxgf@fsty{#1}\pxgf@show{#2}\fi}
\fi                 %</upTeX>

%%%% \pxgf@show@char, \pxgf@show
\def\pxgf@show#1{%
  \iftdir \expandafter\pxgf@puttate \fi
  {\kanjifamily{gtmc}\kanjishape{\pxgf@fsty}\selectfont#1}}
\def\pxgf@show@char{\pxgf@show\pxgf@char}

%%%% \pxgf@chkasg
\def\pxgf@chkasg{\pxgf@oktrue\afterassignment\pxgf@skiptorelax}
\def\pxgf@skiptorelax#1\relax{\ifx @#1@\else\pxgf@okfalse\fi}

%%%% \pxgf@puttate
\ifpxgf@upTeX \kchardef\pxgf@dmychar="FDD1
\else \chardef\pxgf@dmychar=\jis"2921 \fi
\def\pxgf@puttate#1{%
  \ifx\k@family\pxgf@@gtmc \pxgf@dmychar\kern-1zw\fi
  \hb@xt@1zw{\yoko\hss#1\hss}%
  \ifx\k@family\pxgf@@gtmc \kern-1zw\pxgf@dmychar \fi}

%%%% Dummy \pxpmb
\providecommand*\pxpmb[1]{#1}

%%%% \pxgf@pmb@GSJ
\def\pxgf@pmb@GSJ#1#2{\pxpmb{\GSJ{#1}{#2}}}

%%-------- Compatibility with oter packages
\ifpxgf@fake@gtfonts
%--- 'gtfonts' by Hotta

%%%% \GT, \bmGT, \bGT, \bwGT
\let\GT\GSJ \let\bmGT\pxgf@pmb@GSJ
\let\bGT\GSJ \let\bwGT\pxgf@pmb@GSJ
\newdimen\gtfwidth

\else
%--- 'gtftex' by Honda, Inagaki & Kakuto

\DeclareRobustCommand*\GT[1]{{%
  \let\pxgf@err@syner@Gx\pxgf@err@syner@GT
  \pxgf@chkasg\@tempcnta=0#1\relax
  \ifpxgf@ok \pxgf@gtno@a
  \else \pxgf@GT@a#1\@nilx GT2000-\@nil\fi}}
\def\pxgf@GT@a#1GT2000-#2\@nil{%
  \ifx @#1@\pxgf@GT@b#2\@nil
  \else \pxgf@err@syner@Gx \fi}
\def\pxgf@GT@b#1\@nilx#2\@nil{%
  \pxgf@GT@c#1 @ \@nil}
\def\pxgf@GT@c#1 #2 #3\@nil{%
  \ifcat _#3_\pxgf@err@syner@Gx \else
    \pxgf@chkasg\@tempcnta="0#2\relax
    \ifpxgf@ok \pxgf@subchar{#1}{#2}%
    \else \pxgf@surrochar{#1}{#2}\fi
  \fi}

\fi
%%--------

%%%%%%%% all done
\ifpxgf@needclearup \pxgf@clearup \fi
\endinput
%% EOF
