% This file is part of Violon, a violin score engine.
% Copyright (C) 2009-2012  Julien Moutinho
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published
% by the Free Software Foundation, either version 3 of the License,
% or any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty
% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

% Any comment or contribution is welcome
% at julm&autogeree.net.

% Déclaration
	\def\fileversion{1.0}%
	\def\filedate{2009/10/11}%
	\NeedsTeXFormat{LaTeX2e}%
	\ProvidesPackage{violon}[\filedate\space\fileversion\space%
		Violin score library (Julien Moutinho)]%
% Bibliothèques
	\usepackage{ifthen}%
	\usepackage{color}%
% Polices
	\gdef\ssssmall{%
		\@setfontsize\sssmall{5pt}{5pt}}%
	\gdef\sssmall{%
		\@setfontsize\sssmall{6pt}{6pt}}%
	\gdef\ssmall{%
		\@setfontsize\ssmall\@viiipt\@ixpt}%
	\gdef\vlnfingerfont{%
		\expandafter\color{colVlnBowFingerCord\vlnlastcord}%
		\fontfamily{pnc}\bfseries\sssmall}%
	\gdef\vlncordfont{%
		\color{colVlnBowCord}%
		\fontfamily{pnc}\bfseries\sssmall}%
	\gdef\vlnnamefont{%
		\expandafter\color{colVlnBowNameOctave\the\vlnlastposoctave}%
		\fontfamily{pnc}\scshape\ssssmall}%
	\gdef\vlnpartfont{%
		\color{colVlnPart}%
		\fontfamily{pnc}\bfseries\sssmall}%
	\gdef\vlntimefont{%
		\fontfamily{pnc}\bfseries\ssssmall}%
% Algèbre
	\newcount\@calc@mod@cnt%
	\def\modulo#1#2{%
		\@calc@mod@cnt=#1\relax%
		\divide\@calc@mod@cnt by#2\relax%
		\multiply\@calc@mod@cnt by#2\relax%
		\advance#1 by-\@calc@mod@cnt%
	 }%
% Convertisseurs
	\let\romannumeral@old\romannumeral%
	\def\romannumeral#1{%
		\ifnum#1>\z@\romannumeral@old#1\else\number#1\fi%
	 }%
% Moteur
	% Constantes
		% Couleurs
			%\definecolor{colVlnBowU}{rgb}{0.24,0.92,0.24}%
			%\definecolor{colVlnBowD}{rgb}{0.80,0,0}%
			%\definecolor{colVlnBowU}{rgb}{0.30,0.75,0.75}%
			\definecolor{colVlnBowU}{gray}{0.50}%
			\definecolor{colVlnBowD}{gray}{0}%
			\definecolor{colVlnBowN}{gray}{1}%
			\definecolor{colVlnBowCord}{rgb}{0,1,0}%
			\definecolor{colVlnBowFinger}{rgb}{0,0,0}%
			\definecolor{colVlnBowFingerCord0}{rgb}{0,0,0}%
			\definecolor{colVlnBowFingerCord1}{rgb}{0,0.66,0}%
			\definecolor{colVlnBowFingerCord2}{rgb}{0,0,1}%
			\definecolor{colVlnBowFingerCord3}{rgb}{1,0,0}%
			\definecolor{colVlnBowName}{rgb}{1,0,0}%
			\definecolor{colVlnBowNameOctave0}{rgb}{0,0,0}%
			\definecolor{colVlnBowNameOctave1}{rgb}{0,0.66,0}%
			\definecolor{colVlnBowNameOctave2}{rgb}{0,0,1}%
			\definecolor{colVlnBowNameOctave3}{rgb}{1,0,0}%
			\definecolor{colVlnLineHalfToneEven}{gray}{0.98}%
			\definecolor{colVlnLineHalfToneOdd}{gray}{1}%
			\definecolor{colVlnLineHalfToneFifth}{gray}{0.92}%
			\definecolor{colVlnPart}{gray}{0}%
			\definecolor{colVlnBar}{gray}{.85}%
			\definecolor{colVlnCrescD}{rgb}{0,0.66,0}%
			\definecolor{colVlnCrescU}{rgb}{1,0,0}%
	% Portée
		% Déclarations
			\newdimen\vlnstaffhalftonelinevdim%
			\newskip\vlnstaffvdim%
			\def\vlnstaffvdimpre{3.25pt}%
			\newskip\vlnstaffintervskip%
			\newskip\vlnstaffvskip%
		% Initialisations
			\def\vlnstaffhsize{\hsize}%
			\def\vlnstaffmaxvsize{\textheight}%
			\vlnstaffhalftonelinevdim=3pt%
			\vlnstaffvdim=0pt\relax%
			\vlnstaffvskip=0pt\relax%
			\vlnstaffintervskip=\vlnstaffhalftonelinevdim\relax%
			\multiply\vlnstaffintervskip by 2\relax%
		\def\vlnstaffhalftoneline#1{%
			{\color{colVlnLineHalfTone#1}\hrule depth 0pt height \vlnstaffhalftonelinevdim width \vlnstaffhsize }}%
		\def\vlnstafftoneskip{%
			\advance\vlnstaffvdim by\vlnstaffhalftonelinevdim%
			\nobreak%
		 }%
		\def\vlnstafffivehalftonelines{%
			\vlnstaffhalftoneline{Odd}\vlnstafftoneskip%
			\vlnstaffhalftoneline{Even}\vlnstafftoneskip%
			\vlnstaffhalftoneline{Odd}\vlnstafftoneskip%
			\vlnstaffhalftoneline{Even}\vlnstafftoneskip%
			\vlnstaffhalftoneline{Fifth}\vlnstafftoneskip%
		 }%
		\def\vlnstaffallhalftonelines{%
			\vlnstaffhalftoneline{Fifth}\vlnstafftoneskip%
			\vlnstafffivehalftonelines%
			\vlnstafffivehalftonelines%
			\vlnstafffivehalftonelines%
			\vlnstafffivehalftonelines%
			\vlnstafffivehalftonelines%
		 }%
		\def\vlnstaff{%
			\nobreak\vskip\vlnstaffvdimpre%
			\nobreak\vskip\vlnstaffvdim%
			\advance\vlnstaffvskip by\vlnstaffvdim\relax%
			\count@\vlnstaffvskip\relax%
			\advance\count@ by\vlnstaffvdim\relax%
			\ifnum\count@>\vlnstaffmaxvsize%
				\vlnstaffvskip=0pt\relax%
				\break%
			 \else%
				\advance\vlnstaffvskip by\vlnstaffintervskip\relax%
				\nobreak\vskip\vlnstaffintervskip%
			 \fi%
			\vlnnotehskip=0pt\relax%
			\vlnstaffvdim=0pt\relax%
			\vlnstaffallhalftonelines%
			\advance\vlnstaffvdim by\vlnstaffhalftonelinevdim%
			\nobreak\vskip-\vlnstaffvdim%
		 }%
	% Notes
		% Déclarations
			% Variables
				\newskip\vlnnotevskip%
				\newskip\vlnnotehskip%
				\newdimen\dimen@%
				\newdimen\dimen@@%
				\newdimen\vlnnotevdim%
				\newdimen\vlnnotevdimmore%
				\newdimen\vlnnotehdim%
				\newdimen\vlnnotehdimbase%
				\newcount\vlnlastfinger%
				\newcount\vlnlastposbase%
				\newcount\vlnlastposunit%
				\newcount\vlnlastposoctave%
				\def\vlnlastname{}%
				\def\vlnlastletter{}%
				\def\vlnlastshift{}%
				\newcount\vlnnotepos%
				\newcount\vlnlastpos%
				\gdef\vlncrescleft{}%
				\gdef\vlncrescright{}%
		% Initialisations
			\vlnnotevskip=0pt\relax%
			\vlnnotehskip=0pt\relax%
			\vlnnotevdim=\vlnstaffhalftonelinevdim%
			\vlnnotevdimmore=1pt\relax%
			\vlnnotehdimbase=2cm\relax%
			%\advance\vlnnotevdim by\vlnnotevdimmore%
			%\advance\vlnnotevdim by\vlnnotevdimmore%
			\def\vlnlastbow{}%
			\def\vlnlastrealbow{}%
			\def\vlnlastcord{}%
			\def\vlnlasttimenum{}%
			\def\vlnlasttimeden{}%
			\def\vlnnotehdimmodetim{t}%
			\def\vlnnotehdimmodetxt{}%
			\parindent=0pt\relax%
			\parskip=0pt\relax%
			\parfillskip=0pt\relax%
		\def\vlnlastposfinger{%
			\csname vlnlastposfinger\romannumeral\vlnlastfinger\endcsname%
		 }%
		\def\vlnlastfingerpos{%
			\csname vlnlastfingerpos\romannumeral\vlnlastpos\endcsname%
		 }%
		\def\vlnlastposname{%
			\csname vlnlastposname\romannumeral\vlnlastpos\endcsname%
		 }%
		\def\vlntimefrac#1/#2{%
			\leavevmode%
			\hbox{#1}%
			\kern-.4ex\lower.3ex\hbox{\bfseries\tiny /}%
			\kern-.3ex\lower.6ex\hbox{#2}%
		 }%
		\def\vlndoatnote#1#2#3#4{%
			\nointerlineskip%
			\vlnnotevskip=0pt\relax%
			\advance\vlnnotevskip by\vlnstaffhalftonelinevdim\relax%
			\multiply\vlnnotevskip by#1\relax%
			\advance\vlnnotevskip by\vlnstaffhalftonelinevdim\relax%
			%\advance\vlnnotevskip by-\vlnnotevdimmore\relax%
			\nobreak\vskip\vlnnotevskip\relax%
			\vrule depth 0pt height 0pt width \vlnnotehskip\relax%
			\setbox0=#3%
			\advance\vlnnotevskip by \ht0\relax%
			#2%
			\box0%
			#4%
			\nobreak\vskip-\vlnnotevskip\relax%
			\nointerlineskip%
		 }%
		\def\vlnnextnote{%
			\advance\vlnnotehskip by\vlnnotehdim\relax%
		 }%
		\def\vlnnotedraw crd#1pos#2fgr#3bow#4nam#5tim#6#7{%
			% Position
				\ifthenelse{\equal{#2}{.}}%
				 {}%
				 {\vlnlastpos#2\relax}%
			% Durée
				\ifthenelse{\equal{#6}{.}}%
				 {}%
				 {\def\vlnlasttimenum{#6}}%
				\ifthenelse{\equal{#7}{.}}%
				 {}%
				 {\def\vlnlasttimeden{#7}}%
				\vlnnotehdim\vlnnotehdimbase%
				\ifthenelse{\equal{}{\vlnnotehdimmodetim}}%
				 {}%
				 {\multiply\vlnnotehdim by\vlnlasttimenum\relax%
					\divide\vlnnotehdim by\vlnlasttimeden\relax%
				 }%
				% Automatic staff break
					\count@\vlnnotehskip\relax%
					\advance\count@ by\vlnnotehdim\relax%
					\ifnum\count@>\vlnstaffhsize\vlnstaff\fi%
				\def\vlnnotehdimmodetxtdo{%
					\vbox to0pt{%
						\ifthenelse{\equal{}{\vlnnotehdimmodetxt}}%
						 {\null}%
						 {%
							\setbox0=\hbox{%
								\ifthenelse{\equal{\vlnlasttimeden}{1}}%
								 {{\vlntimefont{\vlnlasttimenum}}}%
								 {\vlntimefrac{\vlntimefont{\vlnlasttimenum}}/{\vlntimefont{\vlnlasttimeden}}}%
							 }%
							\nobreak\vskip\vlnnotevdim\relax%
							\nobreak\vskip0.2mm\relax%
							\nobreak\hskip\vlnnotehdim\relax%
							\nobreak\hskip-\wd0\relax%
							\dp0=0pt\relax%
							\box0}%
					 }%
				 }%
				\ifthenelse{\equal{#6/#7}{\vlnlasttimenum/\vlnlasttimeden}}%
				 {}%
				 {\vlndoatnote{\vlnlastpos}%
					 {}%
					 {\vlnnotehdimmodetxtdo}%
					 {}%
				 }%
			% Archet
				\ifthenelse{\equal{#4}{C}}%
					 {\ifthenelse{\equal{U}{\vlnlastrealbow}}%
						 {\def\vlnlastbow{D}}%
						 {\def\vlnlastbow{U}}}%
					 {\ifthenelse{\equal{#4}{.}}%
						{\def\vlnlastbow{\vlnlastrealbow}}%
						{\def\vlnlastbow{#4}}}%
				\ifthenelse{\equal{\vlnlastbow}{N}}%
					 {}%
					 {\edef\vlnlastrealbow{\vlnlastbow}}%
				\vlndoatnote{\vlnlastpos}%
				 {\begingroup\expandafter\color{colVlnBow\vlnlastbow}}%
				 {%
					\hbox{%
						\ifthenelse{\equal{\vlnlastbow}{N}}
						 {}%
						 {%
							\vrule depth 0pt height \vlnnotevdim width \vlnnotehdim\relax%
							\nobreak\hskip-\vlnnotehdim\relax%
							\dimen@\vlnnotevdim%
							\divide\dimen@ by3\relax%
							\dimen@@\vlnnotehdim%
							\divide\dimen@@ by2\relax%
							\ifthenelse{\equal{\vlncrescleft}{}}%
							 {%
								\ifthenelse{\equal{\vlncrescright}{}}%
								 {%
								 }%
								 {%
									\vrule depth 0pt height 0pt width \dimen@@\relax%
									\color{colVlnCresc\vlncrescright}%
									\vrule depth 0pt height \dimen@ width \dimen@@\relax%
								 }%
							 }%
							 {%
								\ifthenelse{\equal{\vlncrescright}{}}%
								 {%
									\color{colVlnCresc\vlncrescleft}%
									\vrule depth 0pt height \dimen@ width \dimen@@\relax%
								 }%
								 {%
									\color{colVlnCresc\vlncrescleft}%
									\vrule depth 0pt height \dimen@ width \dimen@@\relax%
									\color{colVlnCresc\vlncrescright}%
									\vrule depth 0pt height \dimen@ width \dimen@@\relax%
								 }%
							 }%
							\global\let\vlncrescleft\vlncrescright%
						 }%
					 }%
				 }%
				 {\endgroup}%
			% Cordes
				\ifthenelse{\equal{#1}{\vlnlastcord}}%
				 {}%
				 {\ifthenelse{\equal{#1}{.}}%
					 {}%
					 {\def\vlnlastcord{#1}%
						%\vlndoatnote{\vlnlastpos}%
						% {}%
						% {\vbox to0pt{%
						%	\setbox0=\hbox{{\vlncordfont{%
						%		\count@#1\relax%
						%		\advance\count@ by1\relax%
						%		\ifcase\count@%
						%		\or 0\or 1\or 2\or 3%
						%		\fi}}}%
						%	%\nobreak\vskip\vlnnotevdim\relax%
						%	%\nobreak\vskip0.2mm\relax%
						%	\nobreak\vskip-0.2mm\relax%
						%	\nobreak\vskip-\ht0\relax%
						%	\nobreak\hskip\vlnnotehdim\relax%
						%	\nobreak\hskip-\wd0\relax%
						%	\box0}}%
						% {}%
					 }%
				 }%
			% Doigts
				\ifthenelse{\equal{#3}{.}}%
				 {}%
				 {\vlnlastfinger#3\relax}%
				\ifthenelse{\equal{\vlnlastbow}{N}%
				%	\OR\equal{\the\vlnlastfinger}{\vlnlastfingerpos}%
				%	\AND\equal{\the\vlnlastpos}{\vlnlastposfinger}%
				%	\AND\equal{\the\vlnlastcord}{\thevlnlastlastcord}%
				 }%
				 {}%
				 {%
					\expandafter\edef\csname vlnlastfingerpos\romannumeral\vlnlastpos\endcsname{\the\vlnlastfinger}%
					\expandafter\edef\csname vlnlastposfinger\romannumeral\vlnlastfinger\endcsname{\the\vlnlastpos}%
					\vlndoatnote{\vlnlastpos}%
					 {}%
					 {\vbox to0pt{%
						\setbox0=\hbox{{\vlnfingerfont{\the\vlnlastfinger}}}%
						\nobreak\vskip\vlnnotevdim\relax%
						\nobreak\vskip0.2mm\relax%
						\dp0=0pt\relax% NOTE: to prevent fonts like pnc to mess up the vertical alignment
						\box0}}%
					 {}%
				 }%
			% Nom
				\ifthenelse{\equal{#5}{\vlnlastname}\AND\equal{#2}{\vlnlastpos}}%
				 {}%
				 {%
					\def\vlnlastname{#5}%
					\expandafter\edef\csname vlnlastposname\romannumeral\vlnlastpos\endcsname{#5}%
					\ifthenelse{\equal{\vlnlastbow}{N}}%
					 {}%
					 {%\edef\vlnlastname{#5}%
						\vlndoatnote{\vlnlastpos}%
						 {}%
						 {\vbox to0pt{%
							\setbox0=\hbox{{\vlnnamefont{#5}}}%
							\nobreak\vskip-0.275mm\relax%
							\nobreak\vskip-\ht0\relax%
							%\vskip-\dp0\relax%
							\dp0=0pt\relax% NOTE: to prevent fonts like pnc to mess up the vertical alignment
							\box0}}%
						 {}%
					 }%
				 }%
			\@ifnextchar&{%
				\def\next&{%
					\nointerlineskip%
				 }%
				\next%
			 }{%
				\vlnnextnote%
				\nointerlineskip%
			 }%
		 }%
		% Cordes
			\def\vlncordposi{0}%    G
			\def\vlncordposii{7}%   D
			\def\vlncordposiii{14}% A
			\def\vlncordposiv{21}%  E
		% Crochets
			\def\vlnnotehook{}%
		% Demi-tons alphabétiques sans modification
			\expandafter\def\csname vlnnotetoneofnameG=\endcsname{0}%
			\expandafter\def\csname vlnnotetoneofnameG+\endcsname{1}%
			\expandafter\def\csname vlnnotetoneofnameA-\endcsname{1}%
			\expandafter\def\csname vlnnotetoneofnameA=\endcsname{2}%
			\expandafter\def\csname vlnnotetoneofnameA+\endcsname{3}%
			\expandafter\def\csname vlnnotetoneofnameB-\endcsname{3}%
			\expandafter\def\csname vlnnotetoneofnameB=\endcsname{4}%
			\expandafter\def\csname vlnnotetoneofnameB+\endcsname{5}%
			\expandafter\def\csname vlnnotetoneofnameC-\endcsname{4}%
			\expandafter\def\csname vlnnotetoneofnameC=\endcsname{5}%
			\expandafter\def\csname vlnnotetoneofnameC+\endcsname{6}%
			\expandafter\def\csname vlnnotetoneofnameD-\endcsname{6}%
			\expandafter\def\csname vlnnotetoneofnameD=\endcsname{7}%
			\expandafter\def\csname vlnnotetoneofnameD+\endcsname{8}%
			\expandafter\def\csname vlnnotetoneofnameE-\endcsname{8}%
			\expandafter\def\csname vlnnotetoneofnameE=\endcsname{9}%
			\expandafter\def\csname vlnnotetoneofnameE+\endcsname{10}%
			\expandafter\def\csname vlnnotetoneofnameF-\endcsname{9}%
			\expandafter\def\csname vlnnotetoneofnameF=\endcsname{10}%
			\expandafter\def\csname vlnnotetoneofnameF+\endcsname{11}%
			\expandafter\def\csname vlnnotetoneofnameG-\endcsname{11}%
		% Modifications
			\expandafter\def\csname vlnshiftsymbol-\endcsname{\kern-0.7pt$\flat$}%
			\expandafter\def\csname vlnshiftsymbol=\endcsname{}%
			\expandafter\def\csname vlnshiftsymbol+\endcsname{$\sharp$}%
		\def\vlnnotedrawtonebyname#1#2#3#4#5#6{%
			\ifthenelse{\equal{#1}{.}}%
			 {}%
			 {\vlnlastposoctave#1\relax}%
			\ifthenelse{\equal{#2}{.}}%
			 {}%
			 {\def\vlnlastletter{#2}}%
			\ifthenelse{\equal{#3}{.}}%
			 {}%
			 {\def\vlnlastshift{#3}}%
			\edef\tmp{\csname vlnnotetoneofname\vlnlastletter\vlnlastshift\endcsname}%
			\vlnnotepos\tmp%
			\count@\vlnlastposoctave\relax%
			\multiply\count@ by12\relax%
			\advance\vlnnotepos by\count@%
			\ifthenelse{\equal{#4}{.}}%
			 {\count@\vlnlastcord}%
			 {\count@#4}%
			\advance\count@ by1\relax%
			\advance\vlnnotepos by-\csname vlncordpos\romannumeral\count@\endcsname%
			\edef\nam{\vlnlastletter\csname vlnshiftsymbol\vlnlastshift\endcsname}%
			\vlnnotedraw crd{#4}pos{\the\vlnnotepos}fgr{#5}bow{#6}nam{\nam}tim%
		 }%
		\def\vlnpart#1{%
			\vlndoatnote{0}%
			 {}%
			 {\vbox to0pt{%
				\setbox0=\hbox{{\vlnpartfont{#1}}}%
				\nobreak\vskip-0.3mm\relax%
				\nobreak\vskip-\ht0\relax%
				\dp0=0pt\relax% NOTE: to prevent fonts like pnc to mess up the vertical alignment
				\box0}}%
			 {}%
		 }%
		\def\vlnbar{%
			\vlndoatnote{0}%
			 {}%
			 {\vbox to0pt{%
				\dimen@\vlnstaffvdim%
				\advance\dimen@ by -\vlnnotevdim\relax%
				\advance\dimen@ by -\vlnnotevdim\relax%
				\setbox0=\hbox{\color{colVlnBar}\vrule depth 0pt height \dimen@ width 0.1pt\relax}%
				\nobreak\vskip\vlnnotevdim\relax%
				\dp0=0pt\relax%
				\box0}}%
			 {}%
		 }%
		\def\vlncresc#1{%
			\global\let\vlncrescleft\vlncrescright%
			\xdef\vlncrescright{#1}%
		 }%
