\input fontinst.sty

\def\fontinstcc{%
  \catcode`\ =9
  \catcode`\^^M=9
  \catcode`\@=11
  \catcode`\_=11
  \catcode`\~=10
  }%

\def\normalcc{%
  \catcode`\@=\atcatcode
  \catcode`\^^M=\nlcatcode
  \catcode`\ =\spacecatcode
  \catcode`\~=\tildecatcode
  \catcode`\_=\underscorecatcode
  }%

\fontinstcc

\def\llx{\glyph_bb_parameter\first_of_four}
\def\lly{\glyph_bb_parameter\second_of_four}
\def\urx{\glyph_bb_parameter\third_of_four}
\def\ury{\glyph_bb_parameter\fourth_of_four}

\def\glyph_bb_parameter#1#2{
   \expandafter\expandafter\expandafter
         #1\csname gb-#2\endcsname
}

\def\first_of_four#1#2#3#4{#1}
\def\second_of_four#1#2#3#4{#2}
\def\third_of_four#1#2#3#4{#3}
\def\fourth_of_four#1#2#3#4{#4}

\def\afm_char{
   \a_count=-\bbox_lly
   \eval_expr{
      \italcorr_expression\x_width\bbox_llx\bbox_urx\bbox_lly\bbox_ury
   }
   \out_line{
      \ifnum -1<\char_slot
         \string\setrawglyph
      \else
         \string\setnotglyph
      \fi
      {\char_name}
      {\raw_font_name}
      {10pt}
      {\the\char_slot}
      {\the\x_width}
      {\the\bbox_ury}
      {\the\a_count}
      {\the\result}
   }
   \outglyphbb  % bbox support patch
}

\def\outglyphbb{
   \out_line{
      \string\setglyphbbox
      {\char_name}
      {\raw_font_name}
      {\the\bbox_llx}
      {\the\bbox_lly}
      {\the\bbox_urx}
      {\the\bbox_ury}
   }
}

\def\setglyphbbox#1#2#3#4#5#6{
   \if_undefined{gb-#1}\then
      \eval_expr_to\a_count{#3}
      \eval_expr_to\b_count{#4}
      \eval_expr_to\c_count{#5}
      \eval_expr_to\d_count{#6}
      \ifisint{i-rawscale}\then
         \e_count=\int{rawscale}
         \multiply \a_count \e_count  \l_rounded_thousandths\a_count
         \multiply \b_count \e_count  \l_rounded_thousandths\b_count
         \multiply \c_count \e_count  \l_rounded_thousandths\c_count
         \multiply \d_count \e_count  \l_rounded_thousandths\d_count
      \fi
      \x_cs\edef{gb-#1}{
         {\the\a_count}
         {\the\b_count}
         {\the\c_count}
         {\the\d_count}
      }
   \fi
}

\def\mtxtomtx_setglyphbbox#1#2#3#4#5#6{
   \eval_expr_to\a_count{\scale{#3}{\int{x-scale}}}
%    \eval_expr_to\a_count{\add{\scale{#3}{\int{x-scale}}}
%                              {\scale{#4}{\int{slant-scale}}}}
   \eval_expr_to\b_count{\scale{#4}{\int{y-scale}}}
   \eval_expr_to\c_count{\scale{#5}{\int{x-scale}}}
%    \eval_expr_to\c_count{\add{\scale{#5}{\int{x-scale}}}
%                              {\scale{#5}{\int{slant-scale}}}}
   \eval_expr_to\d_count{\scale{#6}{\int{y-scale}}}
   \out_line{
        \string\setglyphbbox
         {#1}{\raw_font_name}{\the\a_count}{\the\b_count}{\the\c_count}{\the\d_count}
   }
}

\def\mtxtomtx#1#2{{
   \ifisstr{etx-name}\then
      \def\do_slot{\x_cs\edef{slots-\slot_name}{\the\slot_number}}
      \inputetx{\str{etx-name}}
      \edef\a_macro{\string\reencodefont{\str{etx-name}}}
   \else
      \let\a_macro=\empty_command
   \fi
   \_a_true
   \ifnum \int{x-scale}=\int{y-scale}
      \ifnum \int{slant-scale}=\z@
            \_a_false
   \fi \fi
   \if_a_
      \a_count=\int{x-scale}
      \l_inv_scale\a_count{\int{y-scale}}
      \b_count=\int{slant-scale}
      \l_inv_scale\b_count{\int{y-scale}}
      \edef\a_macro{\a_macro
         \string\transformfont{\the\a_count}{\the\b_count}
      }
   \fi
   \open_out{\temp_prefix#2.mtx}
   \edef\raw_font_name{#2}
   \out_line{\percent_char~Filename:~#2.mtx}
   \out_line{\percent_char~Created~by:~tex~\jobname}
   \out_line{\percent_char~Created~using:~\string\mtxtomtx{#1}{#2}}
   \out_line{}
   \out_line{\percent_char~This~file~is~used~by~the~fontinst~package.}
   \out_line{}
   \out_line{\percent_char~THIS~FILE~CAN~BE~DELETED.}
   \out_line{}
   \out_line{\string\relax}
   \out_line{\string\metrics}
   \out_line{}
   \out_line{\string\needsfontinstversion{\fontinstversion}}
   \out_line{}
   \record_transform{#2}{\string\frommtx{#1}}{\a_macro}\iftrue
   \out_line{}
   \let\setint=\mtxtomtx_setint
   \let\setdim=\mtxtomtx_setdim
   \let\setstr=\mtxtomtx_setstr
   \let\setscaledrawglyph=\mtxtomtx_setscaledrawglyph
   \let\setscalednotglyph=\mtxtomtx_setscaledrawglyph
   \let\setglyphbbox=\mtxtomtx_setglyphbbox  % bbox support patch
   \let\setkern=\mtxtomtx_setkern
   \let\setglyph=\mtxtomtx_setglyph
   \let\glyphpcc=\mtxtomtx_glyphpcc
   \let\samesize=\mtxtomtx_samesize
   \let\endsetglyph=\mtxtomtx_endsetglyph
   \inputmtx{#1}
   \out_line{}
   \out_line{\endmetrics_text}
   \close_out{Transformed~metrics}
}}

\def\reglyph_font#1#2{{
   \open_out{\temp_prefix#1.mtx}
   \out_line{\percent_char~Filename:~#1.mtx}
   \out_line{\percent_char~Created~by:~tex~\jobname}
   \out_line{\percent_char~Created~using:~\string\reglyphfont{#1}{#2}}
   \out_line{}
   \out_line{\percent_char~This~file~is~used~by~the~fontinst~package.}
   \out_line{}
   \out_line{\percent_char~THIS~FILE~CAN~BE~DELETED.}
   \out_line{}
   \out_line{\string\relax}
   \out_line{\string\metrics}
   \out_line{}
   \out_line{\string\needsfontinstversion{\fontinstversion}}
   \out_line{}
   \record_transform{#1}{\string\frommtx{#2}}{\string\reglyphfont}
      \iftrue
   \out_line{}
   \let\setint=\reglyph_setint
   \let\setdim=\reglyph_setdim
   \let\setstr=\reglyph_setstr
   \let\setscaledrawglyph=\reglyph_setscaledrawglyph
   \let\setscalednotglyph=\reglyph_setscaledrawglyph
   \let\setglyphbbox=\reglyph_setglyphbbox  % bbox support patch
   \let\setkern=\reglyph_setkern
   \let\setglyph=\reglyph_setglyph
   \let\glyphpcc=\reglyph_glyphpcc
   \let\samesize=\reglyph_samesize
   \let\endsetglyph=\reglyph_endsetglyph
   \inputmtx{#2}
   \out_line{}
   \out_line{\endmetrics_text}
   \close_out{Reglyphed~metrics}
}}

\def\reglyph_setglyphbbox#1#2#3#4#5#6{
   \command_survivance=0
   \def\glyphname{#1}
   \csname slots-#1\endcsname
   \iftokeep\command_survivance\then
      \out_line{
        \string\setglyphbbox
         {\glyphname}{#2}{#3}{#4}{#5}{#6}
      }
   \fi
}

\def\bbw#1{
    \sub{\urx{#1}}{\llx{#1}}
}

\def\bbh#1{
    \sub{\ury{#1}}{\lly{#1}}
}

\setcommand\centeraccent#1#2#3#4{
    \push
        \moveup{#4}
        \movert{\add{\add{\scale{500}{\sub{\width{#1}}{\width{#2}}}}{#3}}
                    {\scale{#4}{\int{italicslant}}}}
        \glyph{#2}{1000}
    \pop
    \glyph{#1}{1000}
}

\def\composeglyphpac#1#2#3#4#5{
    \resetglyph{#1}
       \centeraccent{#2}{#3}{\add{\int{#3-sx}}{#4}}{\add{\int{#3-sy}}{#5}}
    \endresetglyph
    \setleftrightkerning{#1}{#2}{1000}
}

\def\putquoteright#1#2#3#4#5{
    \push
        \movert{\add{\urx{#1}}{#3}}
        \moveup{\sub{#5}{\scale{#2}{\lly{quoteright}}}}
        \glyph{quoteright}{#2}
    \pop
    \glyph{#1}{1000}
    \movert{#4}
}

\def\setsy#1#2#3{
    \setint{#1-sy}{\add{\sub{\int{#2}}{\lly{#1}}}{#3}}
}

\def\setAsx#1#2#3{
    \setint{#1-sx}
        {\add{\scale{\int{italicslant}}{\sub{\lly{#1}}{\lly{#2}}}}{#3}}
}

\let\iffileexists=\if_file_exists

\def\log#1{\immediate \write 16 {#1}}

\def\setkerns#1#2#3{
    \foreach(leftglyph){#1}
        \foreach(rightglyph){#2}
            \setkern{\str{leftglyph}}{\str{rightglyph}}{#3}
        \endfor(rightglyph)
    \endfor(leftglyph)
}

\def\resetkerns#1#2#3{
    \foreach(leftglyph){#1}
        \foreach(rightglyph){#2}
            \resetkern{\str{leftglyph}}{\str{rightglyph}}{#3}
        \endfor(rightglyph)
    \endfor(leftglyph)
}

\def\setnullglyph#1{
    \setglyph{#1}
    \endsetglyph
    \setint{#1-spacing}{0}
}

% \installcsfont{texbase}{psbase8r}{expansion}{psbase}
% e.g. \installcsfont{putr8z}{putr8r}{}{putr8a}
% or  \installcsfont{putr8z}{putr8r}{+20}{putr8a}

\def\installcsfont#1#2#3#4{
    \iffileexists{#4#3.afm}\then
        \transformfont{#2#3}%
            {\reencodefont{8r}{\fromafm{#4#3}}}
        \transformfont{#2x#3}%
            {\reencodefont{8r}{\yscalefont{700}{\fromafm{#4#3}}}}
    \else
        \transformfont{#2#3}%
            {\reencodefont{8r}{\frommtx{#4#3}}}
        \transformfont{#2x#3}%
            {\reencodefont{8r}{\yscalefont{700}{\frommtx{#4#3}}}}
    \fi
%     \transformfont{#2x#3}%
%         {\reencodefont{8r}{\yscalefont{700}{\fromafm{#4#3}}}}
    \reglyphfonts
    \renameglyph{Acute}{acute}
    \renameglyph{Caron}{caron}
    \renameglyph{Ring}{ring}
    \renameglyph{Grave}{grave}
    \renameglyph{Dieresis}{dieresis}
    \renameglyph{Circumflex}{circumflex}
%     \renameglyph{Breve}{breve}
%     \renameglyph{Cedilla}{cedilla}
%     \renameglyph{Dotaccent}{dotaccent}
%     \renameglyph{Hungarumlaut}{hungarumlaut}
%     \renameglyph{Ogonek}{ogonek}
%     \renameglyph{Slash}{slash}
    \resetcommand\iftokeep##1{\ifnumber{##1}>{0}}
    \offmtxcommand{\setkern}
    \offmtxcommand{\setglyph}
    \offmtxcommand{\setint}
    \reglyphfont{#2u#3}{#2x#3}
    \endreglyphfonts
    \iffileexists{mk#1.mtx}\then
        \installfont{#1#3}{#2#3,#2u#3,mk#1}{il2}{}{}{}{}{}
    \else
        \installfont{#1#3}{#2#3,#2u#3,cs}{il2}{}{}{}{}{}
    \fi
}

\def\writecc#1#2#3#4#5#6#7#8{
    \global\let\setsomething_global=\x_relax
    \input_metrics{}{#2}
    \etxtocc{#3}{#1}
    \global\let\setsomething_global=\global
}

\def\etxtocc#1#2{{
    \make_assignments{#1}
    \open_out{#2.cc}
    \bgroup
    \let\do_slot=\do_cc
    \let\end_do_slot=\end_do_cc
    \let\saved_raw\cc_raw
    \let\saved_scale\cc_scale
    \let\saved_movert\cc_movert
    \let\saved_moveup\cc_moveup
    \inputetx{#1}
    \egroup
    \close_out{CC~information}
}}


\def\do_cc{
    \if_undefined{g-\slot_name}\then
        \expandafter\gobble_setslot
    \Else
        \ifx\slot_name\notdef_name\else
            \do_cc_map
        \fi
    \Fi
}

\def\do_cc_map{
    \let\cc_list=\empty
    \pcc_count=0
    \shift_x=0
    \shift_y=0
    \mapcommands\slot_name
}

\def\end_do_cc{
    \ifisglyph\slot_name\then\ifnum\pcc_count > 1
        \out_line{CC~\slot_name\space\the\pcc_count\space;~\cc_list}
    \fi\fi
}

\newcount\shift_x
\newcount\shift_y
\newcount\pcc_count

\def\cc_raw#1#2#3{
    \edef\pcc_raw{PCC~#3~\the\shift_x\space\the\shift_y\space;\space}
    \prep_to\cc_list\pcc_raw
    \edef\x{\cc_list}
    \let\cc_list=\x
    \advance\pcc_count 1
    \shift_x=0
    \shift_y=0
}

\def\cc_movert#1{
   \advance\shift_x #1
}

\def\cc_moveup#1{
   \advance\shift_y #1
}

\def\cc_scale#1#2{
    #2
}

% \def\init_cc#1{%
%    \out_line{\string\setglyph{#1}}
%    \def\char_name{#1}
% }

\def\cc_char{%
%    \out_lline{\string\samesize{\char_name-not}}
   \out_line{\string\endsetglyph}
}

\def\setfontdimens{%
    \setint{italicslant}{0}
    \ifisglyph{x}\then
       \setint{xheight}{\height{x}}
    \Else
       \setint{xheight}{500}
    \Fi
    \ifisglyph{space}\then
       \setint{interword}{\width{space}}
    \Else\ifisglyph{i}\then
       \setint{interword}{\width{i}}
    \Else
       \setint{interword}{333}
    \Fi\Fi
    \setint{fontdimen(1)}{\int{italicslant}}              % italic slant
    \setint{fontdimen(2)}{\int{interword}}                % interword space
    \ifisint{monowidth}\then
       \setint{fontdimen(3)}{0}                           % interword stretch
       \setint{fontdimen(4)}{0}                           % interword shrink
    \Else
       \setint{fontdimen(3)}{\scale{\int{interword}}{600}}% interword stretch
       \setint{fontdimen(4)}{\scale{\int{interword}}{240}}% interword shrink
    \Fi
    \setint{fontdimen(5)}{\int{xheight}}                  % x-height
    \setint{fontdimen(6)}{1000}                           % quad
    \ifisint{typewriter}\then
       \setint{fontdimen(7)}{0}                           % extra space after .
    \Else\ifisint{monowidth}\then
       \setint{fontdimen(7)}{\int{interword}}             % extra space after .
    \Else
       \setint{fontdimen(7)}{\scale{\int{interword}}{240}}% extra space after .
    \Fi\Fi
}

\normalcc
