% This file is part of Violon, a violin score engine.
% Copyright (C) 2009  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 arobase julm dot meeee dot eu.

% 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{%
		\fontfamily{pnc}\bfseries\sssmall}%
	\gdef\vlncordfont{%
		\fontfamily{pnc}\scshape\ssssmall}%
	\gdef\vlntimefont{%
		\fontfamily{pnc}\bfseries\ssssmall}%
% Moteur
	% Constantes
		% Couleurs
			\definecolor{colVlnBowUp}{rgb}{0.24,0.92,0.24}%
			\definecolor{colVlnBowDn}{rgb}{1,0,0}%
			\definecolor{colVlnBowU}{rgb}{0.24,0.92,0.24}%
			\definecolor{colVlnBowD}{rgb}{1,0,0}%
			\definecolor{colVlnLineHalfTone}{gray}{0.75}%
			\definecolor{colVlnLineHalfToneBase}{gray}{0.25}%
	% Portée
		% Déclarations
			\newdimen\vlnhalftonelinevdim
			\newskip\vlnhalftonelineskip%
			\newskip\vlntonesskip%
		% Initialisations
			\vlnhalftonelinevdim=0.5pt%
			\vlntonesskip=0pt\relax%
			\vlnhalftonelineskip=5pt\relax%
			\advance\vlnhalftonelineskip by -\vlnhalftonelinevdim%
		\def\vlnhalftoneline{%
			{\color{colVlnLineHalfTone}\hrule depth 0pt height \vlnhalftonelinevdim width \hsize }}%
		\def\vlnbasehalftoneline{%
			{\color{colVlnLineHalfToneBase}\hrule depth 0pt height \vlnhalftonelinevdim width \hsize }}%
		\def\toneskip{%
			\advance\vlntonesskip by\vlnhalftonelineskip%
			\advance\vlntonesskip by\vlnhalftonelinevdim%
			\vskip\vlnhalftonelineskip%
		 }%
		\def\vlnfivehalftonelines{%
			\toneskip\vlnhalftoneline%
			\toneskip\vlnhalftoneline%
			\toneskip\vlnhalftoneline%
			\toneskip\vlnhalftoneline%
			\toneskip\vlnbasehalftoneline%
		 }%
		\def\allhalftonelines{%
			\vlnfivehalftonelines%
			\vlnfivehalftonelines%
			\vlnfivehalftonelines%
			\vlnfivehalftonelines%
		 }%
		\gdef\vlnlines{%
			\vlnbasehalftoneline%
			\allhalftonelines%
			\advance\vlntonesskip by\vlnhalftonelinevdim%
			\vskip-\vlntonesskip%
		 }%
	% Notes
		% Déclarations
			% Variables
				\newskip\vlnnotevskip%
				\newskip\vlnnotevskipbase%
				\newskip\vlnnotehskip%
				\newskip\vlnnotehskipbase%
				\newdimen\vlnnotevdim%
				\newdimen\vlnnotevdimmore%
				\newdimen\vlnnotehdim%
				\newdimen\vlnnotehdimbase%
				\newcount\vlnlastfinger%
				\newcount\vlnlastposbase%
				\newcount\vlnlastposunit%
		% Initialisations
			\vlnnotevskip=0pt\relax%
			\vlnnotevskipbase=0pt\relax%
			\vlnnotehskip=0pt\relax%
			\vlnnotehskipbase=0pt\relax%
			\vlnnotevdim=\vlnhalftonelinevdim%
			\vlnnotevdimmore=1pt\relax%
			\vlnnotehdimbase=2cm\relax%
			\advance\vlnnotevdim by \vlnnotevdimmore%
			\advance\vlnnotevdim by \vlnnotevdimmore%
			\def\vlnlastbow{}%
			\def\vlnlastcord{}%
			\def\vlnlasttimenum{}%
			\def\vlnlasttimeden{}%
			\def\vlnnotehdimmodetim{1}%
			\def\vlnnotehdimmodetxt{}%
			\parindent=0pt\relax%
			\parskip=0pt\relax%
			\parfillskip=0pt\relax%
		\def\vlnlastfingerpos{%
			\csname vlnlastfingerpos\romannumeral\vlnlastfinger\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\vlnhalftonelineskip%
			\advance\vlnnotevskip by \vlnhalftonelinevdim\relax%
			\multiply\vlnnotevskip by #1\relax%
			\advance\vlnnotevskip by -\vlnnotevdimmore\relax%
			\vskip\vlnnotevskip\relax%
			\vskip-\vlnnotevskip\relax%
			\vskip\vlnnotevskip\relax%
			\vrule depth 0pt height 0pt width \vlnnotehskip\relax%
			\setbox0=#3%
			\advance\vlnnotevskip by \ht0\relax%
			#2%
			\box0%
			#4%
			\vskip-\vlnnotevskip\relax%
			\nointerlineskip%
		 }%
		\def\vlnnextnote{%
			\advance\vlnnotehskip by \vlnnotehdim\relax%
		 }%
		\def\vlnnote crd#1pos#2fgr#3bow#4tim#5#6{%
			% Durée
				\ifthenelse{\equal{#5/#6}{\vlnlasttimenum/\vlnlasttimeden}}%
				 {}%
				 {\ifthenelse{\equal{#5/#6}{./.}}%
					 {}%
					 {\vlndoatnote{#2}%
						 {}%
						 {\ifthenelse{\equal{#5}{.}}%
							 {}%
							 {\def\vlnlasttimenum{#5}}%
							\ifthenelse{\equal{#6}{.}}%
							 {}%
							 {\def\vlnlasttimeden{#6}}%
							\vlnnotehdim\vlnnotehdimbase%
							\ifthenelse{\equal{}{\vlnnotehdimmodetim}}%
							 {}%
							 {\multiply\vlnnotehdim by\vlnlasttimenum\relax%
								\divide\vlnnotehdim by\vlnlasttimeden\relax%
							 }%
							\ifthenelse{\equal{}{\vlnnotehdimmodetxt}}%
							 {}%
							 {\vbox to0pt{%
									\setbox0=\hbox{%
										\ifthenelse{\equal{\vlnlasttimeden}{1}}%
										 {{\vlntimefont{\vlnlasttimenum}}}%
										 {\vlntimefrac{\vlntimefont{\vlnlasttimenum}}/{\vlntimefont{\vlnlasttimeden}}}%
									 }%
									\vskip\vlnnotevdim\relax%
									\vskip0.2mm\relax%
									\dp0=0pt\relax%
									\box0}%
							 }%
						 }%
						 {}%
					 }%
				 }%
			% Archet
				\ifthenelse{\equal{#4}{C}}%
					 {\ifthenelse{\equal{U}{\vlnlastbow}}%
						 {\def\vlnlastbow{D}}%
						 {\def\vlnlastbow{U}}}%
					 {\ifthenelse{\equal{#4}{.}}%
						{}%
						{\def\vlnlastbow{#4}}}%
				\vlndoatnote{#2}%
				 {\begingroup\expandafter\color{colVlnBow\vlnlastbow}}%
				 {%
					\hbox{%
						\vrule depth 0pt height \vlnnotevdim width \vlnnotehdim\relax%
					 }%
				 }%
				 {\endgroup}%
			% Doigts
				\ifthenelse{\equal{#3}{.}}%
				 {}%
				 {\vlnlastfinger#3\relax}%
				\ifthenelse{\equal{#2}{\vlnlastfingerpos}}%
				 {}%
				 {%
					\expandafter\edef\csname vlnlastfingerpos\romannumeral\vlnlastfinger\endcsname{#2}\relax%
					\vlndoatnote{#2}%
					 {}%
					 {\vbox to0pt{%
						\setbox0=\hbox{{\vlnfingerfont{\the\vlnlastfinger}}}%
						\vskip-0.2mm\relax%
						\vskip-\ht0\relax%
						%\vskip-\dp0\relax%
						\dp0=0pt\relax% NOTE: to prevent fonts like pnc to mess up the vertical alignment
						\hskip\vlnnotehdim\relax%
						\hskip-\wd0\relax%
						\box0}}%
					 {}%
				 }%
			% Cordes
				\ifthenelse{\equal{#1}{\vlnlastcord}}%
				 {}%
				 {\ifthenelse{\equal{#1}{.}}%
					 {}%
					 {\def\vlnlastcord{#1}%
						\vlndoatnote{#2}%
						 {}%
						 {\vbox to0pt{%
							\count@#1\relax%
							\setbox0=\hbox{{\vlncordfont{\@Roman\count@}}}%
							\vskip\vlnnotevdim\relax%
							\vskip0.2mm\relax%
							\hskip\vlnnotehdim\relax%
							\hskip-\wd0\relax%
							\box0}}%
						 {}%
					 }%
				 }%
			\vlnnextnote%
			\nointerlineskip%
		 }%
		% Cordes
			\def\vlncordposi{0}%    G
			\def\vlncordposii{7}%   D
			\def\vlncordposiii{14}% A
			\def\vlncordposiv{21}%  E
		\def\n#1#2#3#4#5{%
			\ifthenelse{\equal{#1}{.}}%
			 {}%
			 {\vlnlastposbase#1\relax}%
		 \ifthenelse{\equal{#2}{.}}%
			 {}%
			 {\vlnlastposunit#2\relax}%
		 \ifthenelse{\equal{#3}{.}}%
			 {\count@\vlnlastcord\relax}%
			 {\count@#3\relax}%
			\advance\count@ by1\relax%
			\expandafter\count@-\csname vlncordpos\romannumeral\count@\endcsname\relax%
			\ifthenelse{\equal{\the\vlnlastposbase}{0}}%
			 {\advance\count@ by\vlnlastposunit\relax}%
			 {\edef\tmp{\the\vlnlastposbase\the\vlnlastposunit}%
				\advance\count@ by\tmp\relax}%
			\vlnnote crd{#3}pos{\the\count@}fgr{#4}bow{#5}tim%
		 }%

