%%
%% This is file `pgf-soroban.sty',
%% 
%%% ====================================================================
%%% @LaTeX-file{
%%%   filename  = "pgf-soroban.sty",
%%%   version   = "1.1",
%%%   date      = "2013/11/27",
%%%   author    = "Alain Delmotte",
%%%   address   = "Alain Delmotte,
%%%                rampe du Couvent, 11a,
%%%                B-1348 Louvain-la-Neuve
%%%                BELGIUM",
%%%   email     = "esperanto@swing.be",
%%%   codetable = "ISO/ASCII",
%%%   keywords  = "soroban, abacus, japanese",
%%%   supported = "yes",
%%% }
%

\RequirePackage{calc,ifthen}
\RequirePackage{tikz}

\definecolor{brun}{rgb}{.55 .27 .08}         % nouvelle couleur brune
\newcommand{\colbil}{yellow}                 % couleur par défaut des billes
\newcommand{\coltig}{brun}                   % couleur par défaut des billes

\newcounter{xx}
\newcounter{yy}
\newlength{\unba}\setlength{\unba}{1mm}      % unité de base, par défaut 1mm
\newlength{\eplia}\setlength{\eplia}{3.9mm}  % support et ligne centrale
\newlength{\eplib}\setlength{\eplib}{2pt}    % deux barres autour ligne centrale
\newlength{\eplic}\setlength{\eplic}{6pt}    % cadre et barres extérieures
\newlength{\eplid}\setlength{\eplid}{1pt}    % arrondi des billes

\newcommand{\ladj}[1]{%                        ajustement des longueurs 
\setlength{\unba}{1mm * \real{#1}}
\setlength{\eplia}{3.9mm * \real{#1}}
\setlength{\eplib}{2pt * \real{#1}}
\setlength{\eplic}{6pt * \real{#1}}
\setlength{\eplid}{1pt * \real{#1}}
}

\newcommand{\bille}[2]{\filldraw[fill=#2, draw=\coltig, line width=\eplib,x=\unba, y=\unba,rounded corners=\eplid ](#1)+(-8.7,0){-- +(-2.2,5.3)-- +(2.2,5.3)}-- +(8.7,0){-- +(2.2,-5.3)-- +(-2.2,-5.3)}--cycle; %[rounded corners, very thick]
\draw[draw=\coltig, cap=round, line width=\eplib,x=\unba, y=\unba ] (#1)+(-8.6,0)-- +(8.6,0);
}

\newcommand{\support}[1]{%
\draw[line width=\eplia, \coltig,x=\unba, y=\unba ](#1,0)-- +(0,74);
\draw[line width=\eplia, white,x=\unba, y=\unba ](#1,0)+(-10,54.4)-- +(10,54.4);
\draw[line width=\eplib, x=\unba, y=\unba ](#1,0)+(-11,52.8)-- +(11,52.8);
\draw[line width=\eplib, x=\unba, y=\unba ](#1,0)+(-11,56.1)-- +(11,56.1);
}

\newcommand{\tige}[4][1]{\setcounter{xx}{20*(#2-1+#1) - 10}%
\support{\value{xx}}
\ifthenelse{#3>4}{\bille{\value{xx},62}{\colbil}}{\bille{\value{xx},68.2}{\colbil}}
\ifthenelse{\(#3=0\)\or\(#3=5\)}{\foreach \m in {5.3,16,26.7,37.4} {\bille{\value{xx},\m}{\colbil}}}{%
\ifthenelse{\(#3=1\)\or\(#3=6\)}{\foreach \m in {5.3,16,26.7,46.9} {\bille{\value{xx},\m}{\colbil}}}{%
\ifthenelse{\(#3=2\)\or\(#3=7\)}{\foreach \m in {5.3,16,36.3,46.9} {\bille{\value{xx},\m}{\colbil}}}{%
\ifthenelse{\(#3=3\)\or\(#3=8\)}{\foreach \m in {5.3,25.6,36.3,46.9} {\bille{\value{xx},\m}{\colbil}}}{%
\ifthenelse{\(#3=4\)\or\(#3=9\)}{\foreach \m in {14.9,25.6,36.3,46.9} {\bille{\value{xx},\m}{\colbil}}}{%
}}}}}
\ifthenelse{#4=1}{\draw[fill=black,x= \unba, y= \unba] (\value{xx},54.4) circle(\unba);}{} 
}

\newcommand{\binoire}[4][1]{%
\setcounter{xx}{20*(#2-1+#1)-10}
\ifthenelse{#3=1}{\bille{\value{xx},5.3}{#4}}{%    1 en bas   xooo   o
\ifthenelse{#3=2}{\bille{\value{xx},16}{#4}}{%     2          oxoo   o
\ifthenelse{#3=3}{\bille{\value{xx},26.7}{#4}}{%   3          ooxo   o
\ifthenelse{#3=4}{\bille{\value{xx},37.4}{#4}}{%   4          ooox   o
\ifthenelse{#3=5}{\bille{\value{xx},46.9}{#4}}{%   6          ooo x  o
\ifthenelse{#3=6}{\bille{\value{xx},36.3}{#4}}{%   7          oo xo  o
\ifthenelse{#3=7}{\bille{\value{xx},25.6}{#4}}{%   8          o xoo  o
\ifthenelse{#3=8}{\bille{\value{xx},14.9}{#4}}{%   9           xooo  o
\ifthenelse{#3=9}{\bille{\value{xx},68.2}{#4}}{%   5 * 0      oooo   x
\ifthenelse{#3=10}{\bille{\value{xx},62}{#4}}{%    5 * 1      oooo  x
}}}}}}}}}}
}

\newcommand{\barbil}[3][1]{%
\setcounter{xx}{20*(#2-1+#1)-10}
\ifthenelse{#3=1}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},5.3)+(-8.6,-5.3)-- +(8.6,5.3);}{     %   1 = 1 en bas  xooo  oo
\ifthenelse{#3=2}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},16)+(-8.6,-5.3)-- +(8.6,5.3);}{      %   2 = 2         oxoo  oo
\ifthenelse{#3=3}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},26.7)+(-8.6,-5.3)-- +(8.6,5.3);}{    %   3 = 3         ooxo  oo
\ifthenelse{#3=4}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},37.4)+(-8.6,-5.3)-- +(8.6,5.3);}{    %   4 = 4         ooox  oo
\ifthenelse{#3=5}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},46.9)+(-8.6,-5.3)-- +(8.6,5.3);}{    %   5 = 6         ooo x oo
\ifthenelse{#3=6}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},36.3)+(-8.6,-5.3)-- +(8.6,5.3);}{    %   6 = 7         oo xo oo
\ifthenelse{#3=7}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},25.6)+(-8.6,-5.3)-- +(8.6,5.3);}{    %   7 = 8         o xoo oo
\ifthenelse{#3=8}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},14.9)+(-8.6,-5.3)-- +(8.6,5.3);}{    %   8 = 9          xooo oo
\ifthenelse{#3=9}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},68.2)+(-8.6,-5.3)-- +(8.6,5.3);}{    %   9 = 5 * 0     oooo  ox
\ifthenelse{#3=10}{\draw[draw=\coltig, cap=round, line width=\unba,x=\unba, y=\unba ] (\value{xx},62)+(-8.6,-5.3)-- +(8.6,5.3);}{      %  10 = 5 * 1     oooo  xo
}}}}}}}}}}
}

\newcommand{\cadre}[2][1]{\setcounter{xx}{20*(#2-1+#1) + 1}%
\draw[black, line width=\eplic,x=\unba, y=\unba, rounded corners=\eplia](-1,-1.2)-- (\value{xx},-1.2)-- (\value{xx},74.65)-- (-1, 74.65)--cycle;
}

\newcommand{\barres}[2][1]{%
\setcounter{xx}{20*(#1) -21}%
\setcounter{yy}{20*(#2-1+#1) + 2}%
\draw[black, line width=\eplic,x=\unba, y=\unba](\value{xx},-1.2) -- (\value{yy},-1.2);
\draw[black, line width=\eplic,x=\unba, y=\unba](\value{xx}, 74.65) -- (\value{yy}, 74.65);
}
