%
% zxjafbfont.sty
%

%% package declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{zxjafbfont}[2011/02/20 v0.2]

%% preparations
\def\zxjx@pkgname{zxjafbfont}
\def\zxjx@error{\PackageError\zxjx@pkgname}
%
\RequirePackage{xeCJK}

%%------ User interface

%%<*> \setCJKfallbackfamily{<CJK_family_name>}
\newcommand*\setCJKfallbackfamily[1]{%
  \let\CJKsymbol\zxjx@CJKsymbol
  \edef\zxjx@fb@family{#1}}

%%<*> \unsetCJKfallbackfamily
\newcommand*\unsetCJKfallbackfamily{%
  \let\CJKsymbol\zxjx@org@CJKsymbol
  \let\zxjx@fb@family\relax}

%% \zxjx@fb@family
\let\zxjx@fb@family\relax

%%------ Patch to xeCJK procedures

%% \zxjx@enter@local
\def\zxjx@enter@local{%
  \bgroup \let\zxjx@maybe@egroup\egroup
  \zxjx@revoke@ingress}
\let\zxjx@maybe@egroup\@empty
\def\zxjx@preto@ictoks#1#2#3{%
  \chardef\zxjx@x=#1\chardef\zxjx@y=#2\relax
  \XeTeXinterchartoks\zxjx@x\zxjx@y\expandafter{%
    \expandafter#3\the\XeTeXinterchartoks\zxjx@x\zxjx@y}}
\@for\zxjx@a:=1,2,3\do{%
  \@for\zxjx@b:=0,1,2,3,4,5,6,255\do{%
    \zxjx@preto@ictoks\zxjx@a\zxjx@b{\zxjx@maybe@egroup}}}
\def\zxjx@revoke@ingress{%
  % Segfaults would occur unless \relax is there... (why?)
  \XeTeXinterchartoks 255 1{\relax}%
  \XeTeXinterchartoks 255 2{\relax}%
  \XeTeXinterchartoks 255 3{\relax}}
\chardef\zxjx@geta="3013

%% \zxjx@CJKsymbol
\def\zxjx@CJKsymbol#1{%
  \zxjx@enter@local
  \chardef\zxjx@uc=`#1\relax
  \unless\iffontchar\font\zxjx@uc
    \CJKfamily\zxjx@fb@family \xeCJK@setfont
  \fi
  \zxjx@uc}
\let\zxjx@org@CJKsymbol\CJKsymbol

%%------ all done
\endinput
%% EOF
