% \newcommand{\lastedit}{2001-08-26 21:14:39 by stolfi}
% The classical IC-Unicamp abacus logo 

\makeatletter
\advance\endlinechar by -256%

  % Original design by Hans K. E. Liesenberg
  % Macros by Tomasz Kowaltowski - mar/1988, revised mar/1991
  % Usage: \Abacus{D1}{D2}{D3}{D4}{Width}
  %   where D1--D4 are the digits tobe displayed,
  %   and Width is the overall abacus width.

  \newsavebox{\Ab@cusO} % Occupied bead slot
  \newsavebox{\Ab@cusX} % Empty bead slot
  \newsavebox{\Ab@cusH} % Empty bead half-slot

  \newcommand{\Abacus}[5]{
    \setlength{\unitlength}{#5}
    \divide \unitlength by 24
    \begin{picture}(24,25)(0,0)
      
      % Empty bead half-slot
      \savebox{\Ab@cusH}(1,4){
        \begin{picture}(1,4)(0,0)
          \put(0,1.5){\line(1,0){1}}
          \put(0,2.5){\line(1,0){1}}
        \end{picture}
      }

      % Empty bead slot
      \savebox{\Ab@cusX}(2,4){
        \begin{picture}(2,4)(0,0)
          \put(0,1.5){\line(1,0){2}}
          \put(0,2.5){\line(1,0){2}}
        \end{picture}
      }

      % Occupied bead slot
      \savebox{\Ab@cusO}(2,4){
        \begin{picture}(2,4)(0,0)
          \put(1,2){\oval(2,4)}
        \end{picture}
      }

      \thinlines
      % Outer frame border
      \put(0,0)  {\line(0,1){25}}
      \put(0,0)  {\line(1,0){24}}
      \put(24,0) {\line(0,1){25}}
      \put(0,25) {\line(1,0){24}}

      % Inner frame border
      \put(1.9,1.9)  {\line(0,1){21.2}}
      \put(14.1,1.9) {\line(0,1){21.2}}
      \put(1.9,1.9)  {\line(1,0){12.2}}
      \put(1.9,23.1) {\line(1,0){12.2}}

      \put(15.9,1.9) {\line(0,1){21.2}}
      \put(22.1,1.9) {\line(0,1){21.2}}
      \put(15.9,1.9) {\line(1,0){6.2}}
      \put(15.9,23.1){\line(1,0){6.2}}

      % Joint lines
      % \put(14.1,1.9) {\line(1,-1){0.95}}
      % \put(15.9,23.1){\line(1,1){0.95}}
      % \put(17,24)    {\line(1,-1){0.95}}
      % \put(17,1)     {\line(1,1){0.95}}
      % 
      % \put(0,0)  {\line(1,1){1.9}}
      % \put(0,25) {\line(1,-1){1.9}}
      % \put(24,0) {\line(-1,1){1.9}}
      % \put(24,25){\line(-1,-1){1.9}}

      % Digits
      \thicklines
      \put(2,18) {\Ab@cusDigit{#1}}
      \put(2,13) {\Ab@cusDigit{#2}}
      \put(2,8)  {\Ab@cusDigit{#3}}
      \put(2,3)  {\Ab@cusDigit{#4}}

    \end{picture}
  }

  \newcommand{\Ab@cusDigit}[1]{
    \begin{picture}(22,4)(0,0)\relax
      \xdef\Ab@cusfiv{\ifcase#1 0\or0\or0\or0\or0\or1\or1\or1\or1\or1\fi}
      \xdef\Ab@cusnun{\ifcase#1 0\or1\or2\or3\or4\or0\or1\or2\or3\or4\fi}
      \put(0,0){\Ab@cusUnits{\Ab@cusnun}}
      \put(14,0){\Ab@cusFives{\Ab@cusfiv}}
    \end{picture}
  }
  
  \newcommand{\Ab@cusUnits}[1]{
    \begin{picture}(12,4)(0,0)\relax
      \xdef\Ab@cusxlo{0}
      \xdef\Ab@cusnlo{\ifcase#1 5\or4\or3\or2\or1\or0\fi}
      \multiput(\Ab@cusxlo,0)(2,0){\Ab@cusnlo}{\usebox{\Ab@cusO}}

      \xdef\Ab@cusxmd{\ifcase#1 10\or8\or6\or4\or2\or0\fi}
      \put(\Ab@cusxmd,0){\usebox{\Ab@cusX}}

      \xdef\Ab@cusxhi{\ifcase#1 12\or10\or8\or6\or4\or2\fi}
      \xdef\Ab@cusnhi{#1}
      \multiput(\Ab@cusxhi,0)(2,0){\Ab@cusnhi}{\usebox{\Ab@cusO}}
    \end{picture}
  }

  \newcommand{\Ab@cusFives}[1]{
    \begin{picture}(6,4)(0,0)\relax
      \xdef\Ab@cusxlo{0}
      \xdef\Ab@cusnlo{#1}
      \multiput(\Ab@cusxlo,0)(2,0){\Ab@cusnlo}{\usebox{\Ab@cusO}}

      \xdef\Ab@cusxmd{\ifcase#1 0\or2\or4\fi}
      \put(\Ab@cusxmd,0){\usebox{\Ab@cusX}}

      \xdef\Ab@cusxhi{\ifcase#1 2\or4\or6\fi}
      \xdef\Ab@cusnhi{\ifcase#1 2\or1\or0\fi}
      \multiput(\Ab@cusxhi,0)(2,0){\Ab@cusnhi}{\usebox{\Ab@cusO}}
    \end{picture}
  }

\advance\endlinechar by +256%
\makeatother
