%%
%% Package 'ttfucs' to use Unicode through Truetype fonts
%%
%% Otfried Cheong
%% 
\ProvidesPackage{ttfucs}[2003/09/05 v2.1 Using Unicode with Truetype fonts]

\RequirePackage{ucs}

\def\Ipe@hexnumber@#1{\ifcase\number#1
  0\or 1\or 2\or 3\or 4\or 5\or 6\or 7\or 8\or
  9\or a\or b\or c\or d\or e\or f\fi}

%% \Ipe@family stores the current font family for Unicode characters,
%% \Ipe@plane is the subfont number in hex.

\def\Ipe@family{}
\def\Ipe@plane{}

%% The following macros are taken from the 'CJK' package
%% by Werner Lemberg (with names changed).

\def\Ipe@@empty#1{%
  \ifx\Ipe@plane\undefined\def\mandatory@arg{cmr10}\else
  \edef\mandatory@arg{\mandatory@arg\Ipe@plane}\fi%
  \empty@sfcnt#1%
  \xdef\font@name{\csname \curr@fontshape/\f@size/%
    \ifx\Ipe@plane\undefined\else\Ipe@plane\fi\endcsname}}

\DeclareSizeFunction{IpeUnicode}{\Ipe@@empty\@font@warning}

%% the auxiliary macro \Ipe@numbToHex converts number #2 into a hex string
%%     and assigns it globally to #1.

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

% \IpeTTFchar typesets a Unicode character.

\def\IpeTTFchar#1{%
  \@tempcnta #1\relax
  \divide\@tempcnta by 256\relax
  \Ipe@numbToHex\Ipe@plane{\@tempcnta}%
  \multiply \@tempcnta by 256\relax
  \@tempcntb #1\relax
  \advance\@tempcntb by -\@tempcnta\relax
  {\usefont{C70}{\Ipe@family}{m}{n}\symbol{\@tempcntb}}}

%% Rename \unichar command from ucs.sty

\let\Ipe@unichar\unichar

%% Route \unichar to original unichar

\def\ucsfamily{\renewcommand{\unichar}[1]{\Ipe@unichar{##1}}}
\ucsfamily

\def\TruetypeFont#1{%
  \renewcommand{\Ipe@family}{#1}%
  \renewcommand{\unichar}[1]{\IpeTTFchar{##1}}}

%% Declare a new Truetype font
%% \DeclareTruetypeFont{cyberb}

% The font encoding 'C70' is originally defined by the CJK package.

\begingroup
\nfss@catcodes
\gdef\DeclareTruetypeFont#1{%
  \def\Ipe@name{#1}
  \DeclareFontEncoding{C70}{}{}
  \DeclareFontSubstitution{C70}{\Ipe@name}{m}{n}
  \DeclareFontFamily{C70}{\Ipe@name}{\hyphenchar \font\m@ne}
  \DeclareFontShape{C70}{\Ipe@name}{m}{n}{<-> IpeUnicode * \Ipe@name}{}
  \TruetypeFont{#1}}
\endgroup

\@onlypreamble\DeclareTruetypeFont

%% --------------------------------------------------------------------
