%
% GCHORDS, a LaTeX2e package to draw guitar chord diagrams.
%
% $Id: gchords.sty,v 1.20 2009/02/04 20:25:59 peekas Exp $
% 
% Copyright (C) 2001-2009 Kasper Peeters <kasper.peeters@aei.mpg.de>
% Available under the GNU General Public License.
%
%    - minor additions by D. R. Lovell, 05-nov-1998
%    - bug fixes by Yotam Medini <yotam_medini@yahoo.com>, 11-aug-2001
%    - bug fix by Brent Lievers, 23-sep-2002
%    - minor additions by P. Margarita, 14-aug-2003
%    - bug fix with f0 (thumb), thanks to Andreas Griewank, 15-jan-2005
%    - added optional open string marker, 3-feb-2008
%
% See ChangeLog for a list of changes. 
%
% The homepage for this macro package is
%
%     http://www.aei.mpg.de/~peekas/gchords/
% 
%-------------------------------------------------------------------------
% Variable, user definable stuff is below.


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{gchords}

% Your guitar
\newcommand\strings{6}          % number of strings on your guitar
\newcommand\numfrets{5}         % length (no of frets) of a diagram

% These should be modified if you want chords in a different size
\newcommand\mediumchords{
\def\chordsize{3.0mm}    % distance between two frets (and two strings)
\font\fingerfont=cmr5    % font used for numbering fingers
\font\namefont=cmr10     % font used for labeling of the chord
\font\fretposfont=cmr7   % font used for the fret position marker
\def\dampsymbol{$\scriptstyle\times$}    % symbol of the `damp this % string' marker
\def\opensymbol{$\scriptstyle\circ$}     % symbol of the `open string' marker
}
\newcommand\smallchords{
\def\chordsize{2.0mm}    % distance between two frets (and two strings)
\font\fingerfont=cmr5    % font used for numbering fingers
\font\namefont=cmr10     % font used for labeling of the chord
\font\fretposfont=cmr7   % font used for the fret position marker
\def\dampsymbol{$\scriptstyle\times$}    % symbol of the `damp this string' marker
\def\opensymbol{$\scriptstyle\circ$}     % symbol of the `open string' marker
}

\smallchords

% Dot and line sizes (which scale with the chordsize macro)

\newcommand\fatsiz{1.6}          % size of a fat note
% Changed by Margarita
%\newcommand\lenbar{2.0}         % lenght of sixth part of barré
\newcommand\normalsiz{1.2}       % size of a normal note
\newcommand\fingsiz{1.4}         % size of the circle around a numbered finger
\newcommand\fatfingsiz{1.8}      % size of the circle around a numbered finger
\newcommand\topfretsiz{2.0pt}    % thickness of top fret

% Position of the chord in its bounding box
\newcommand\xoff{2}             % pos(\xoff,\yoff) is left lower corner of diagram
\newcommand\yoff{2}

%-------------------------------------------------------------------------
\def\@forc#1#2#3{\expandafter\f@rc\expandafter#1{#2 K}{#3}}
\def\f@@rc#1#2#3\f@@rc#4{\def#1{#2}#4\f@rc#1{#3}{#4}}

% Chords above lyrics, macro by Yotam Medini.
\newlength{\cwidth}%
\newcommand{\upchord}[1]{% 
    \settowidth{\cwidth}{#1}%
    \raisebox{\baselineskip}{#1}\hspace{-\cwidth}}

% The chord typesetting macros.
\makeatletter
\newcommand\basenote{\circle*{\fatsiz}}
% Changed by Margarita
% \newcommand\basenote{\line(1,0){\lenbar}}
\newcommand\stdnote{\circle*{\normalsiz}}
\newcommand\fingnote{\circle{\fingsiz}}
\newcommand\fatfingnote{\circle{\fatfingsiz}}
\newcommand\cvline{\line(0,1)}
\newcommand\chline{\line(1,0)}

\newcommand\chords[1]{$$\hbox{#1}$$}
\newcommand\chtext[1]{\vbox{\settowidth{\hsize}{#1 xxxx} {#1
  \ascale=\chordsize\vskip8\ascale}}}

% #1 modifiers (like num, or fretbar0)
% #2 positions
% #3 chordname

\newcount\truewidth                     % the true width of the diagram, computed from strings
\newcount\topline
\newcount\myvpos
\newcount\mylength
\def\fnum{ }                % current f{...} symbol (number of finger)
\newcount\fpos              % current p{...} value
\newcount\ghor              % string number currently being drawn
\newcount\prevpos           % previous value of p{...}, zero if no previous dot on string
\newcount\dlen              % length of the vertical line to draw between dots
\newcount\cpos              % scratch variable to hold vertical position of dot
\newcount\stpos
\newdimen\needsize
\newdimen\ascale

\newcommand\chord[3]{\vbox{\hbox{\ascale=\chordsize
\truewidth=\strings\advance\truewidth by -1
\advance\truewidth by \xoff
\topline=\numfrets
\advance\topline by \yoff
\advance\topline by 2

%%% The Diagram itself %%%%%%%%%
\unitlength\ascale
\begin{picture}(\truewidth,\topline)
\advance\topline by -1
\advance\truewidth by -\xoff
\myvpos=\yoff\advance\myvpos by \numfrets
%%% Parse the modifier string %%
\newcommand\topbar{n}
\@tfor\modstr:=#1\do 
   { \def\ttest{t}%
     \ifx\modstr\ttest{\linethickness{\topfretsiz}\put(\xoff,\myvpos){\chline{\truewidth}}\thinlines}%
     \else {\advance\topline by -1%
            \put(\xoff,\topline){\hbox to 0pt{\hss\fretposfont\modstr~}}%
           }%
     \fi%
   }
% \@tfor\modstr:=#1\do 
%    { \if\modstr t{\thicklines\put(\xoff,\myvpos){\chline{\truewidth}}\thinlines}%
%      \else {\advance\topline by -2%
%             \put(\xoff,\topline){\hbox to 0pt{\hss\fretposfont\modstr\ }}%
%            }%
%      \fi%
%    }

%%% Horizontal lines (frets)%%%%
\mylength=0
\myvpos=\yoff
\advance\myvpos by 1  % first fret starts a bit higher
\@whilenum \mylength<\numfrets \do {  
  \put(\xoff,\myvpos){\chline{\truewidth}}
  \advance\mylength by 1
  \advance\myvpos   by 1}

%%% The Dots %%%%%%%%%%%%%%%%%%%
\newcommand\fnow{n}
\newcommand\pnow{n}
\newcommand\snow{n}
\newcommand\Lnow{n}

\edef\notelabel{}

\putdots{#2}{\chordsize}
\put(\xoff,0){\hbox to \truewidth\ascale{\hss\namefont #3\hss}}
\end{picture}
}}}


\long\def\ReturnAfterFi#1\fi{\fi#1}
    \def\f@rc#1#2#3{%
      \def\temp@ty{#2}%
      \ifx\@empty\temp@ty
      \else
        \ReturnAfterFi{%
          \f@@rc#1#2\f@@rc{#3}%
        }%
      \fi
    }%

\newcommand{\putdots}[2]{
  \prevpos=0
  \fpos=0
  \ghor=\xoff
  \let\curnote\stdnote
  \let\curfing\fingnote
  \def\Ktest{K}\def\cotest{,}\def\btest{b}\def\ftest{f}
  \def\stest{s}\def\ptest{p}\def\xtest{x}\def\otest{o}\def\etest{e}\def\ntest{n}\def\Ltest{t}
  \def\attest{ }
  \@forc\thisel{#1}{
      \ifx\thisel\Ktest{}
      \else\ifx\thisel\cotest\cpos=-\fpos\advance\cpos by \numfrets\advance\cpos by \yoff
         \dlen=\cpos
         \advance\dlen by -\yoff
         \put(\ghor,\yoff){\cvline{\dlen}}
         \prevpos=0
         \fpos=0
         \def\fnum{ }
         \renewcommand\pnow{n}
         \renewcommand\fnow{n}
         \renewcommand\snow{n}
			\renewcommand\Lnow{n}
         \advance\ghor by 1
      \else\if\fnow y \edef\fnum{\thisel}\renewcommand\fnow{n}
      \else\ifx\thisel\btest\let\curnote\basenote\let\curfing\fatfingnote
      \else\ifx\thisel\ftest\renewcommand\snow{n}\renewcommand\pnow{n}\renewcommand\fnow{y}\renewcommand\Lnow{n}
      \else\ifx\thisel\stest\renewcommand\snow{y}\renewcommand\pnow{n}\renewcommand\fnow{n}\renewcommand\Lnow{n}
      \else\ifx\thisel\ptest\renewcommand\snow{n}\renewcommand\pnow{y}\renewcommand\fnow{n}\renewcommand\Lnow{n}
      \else\ifx\thisel\Ltest\renewcommand\snow{n}\renewcommand\pnow{n}\renewcommand\fnow{n}\renewcommand\Lnow{y}
      \else\ifx\thisel\xtest{\put(\ghor,\yoff){\cvline{\numfrets}}
          \needsize=\ascale
          \divide\needsize by 2
          \unitlength\needsize
          \multiply\ghor by 2
          \multiply\topline by 2
          \advance\topline by -1
          \put(\ghor,\topline){\hbox to 0pt{\hss{\dampsymbol}\hss}}}
      \else\ifx\thisel\otest{\put(\ghor,\yoff){\cvline{\numfrets}}
          \needsize=\ascale
          \divide\needsize by 2
          \unitlength\needsize
          \multiply\ghor by 2
          \multiply\topline by 2
          \advance\topline by -1
          \put(\ghor,\topline){\hbox to 0pt{\hss{\opensymbol}\hss}}}
      \else\ifx\thisel\ntest\put(\ghor,\yoff){\cvline{\numfrets}}
				 {\needsize=\ascale
				  \divide\needsize by 2
              \unitlength\needsize
              \multiply\ghor by 2
              \multiply\topline by 2
              \advance\topline by -1
              \put(\ghor,\topline){\hbox to 0pt{\hss{\notelabel}\hss}}}
              \edef\notelabel{}
      \else\if\snow y \ghor=\thisel\advance\ghor by \xoff \advance\ghor by -1
		\else\if\Lnow y \edef\notelabel{\thisel}\renewcommand\Lnow{n}
      \else\prevpos=\fpos\advance\prevpos by -1
          \fpos=\thisel
          \ifx\fnum\attest
              \put(\ghor,\yoff){\cvline{\numfrets}}
          \else
              \cpos=\fpos\advance\cpos by -1
              \ifnum\cpos > \prevpos
                   \dlen=\fpos\advance\dlen by -\prevpos
                   \advance\dlen by -2
                   \stpos=\topline \advance\stpos by -\fpos
                   \put(\ghor,\stpos){\cvline{\dlen}}
              \fi
          \fi
          \cpos=-\fpos\advance\cpos by \numfrets\advance\cpos by \yoff
             {\needsize=\ascale
              \divide\needsize by 2
              \unitlength\needsize
              \multiply\ghor by 2
              \multiply\cpos by 2
              \advance\cpos by 1
              \ifx\fnum\attest
              % Line added by Margarita
%                 \linethickness{2.0pt}
              	  \put(\ghor,\cpos){\curnote}
              \else
              	  \put(\ghor,\cpos){\curfing}
              	  \put(\ghor,\cpos){$\vcenter{\vskip 0.5em\vbox{\hbox to 0pt{\hss{\fingerfont\fnum}\hss}}}$}
              \fi}
				 {\needsize=\ascale
				  \divide\needsize by 2
              \unitlength\needsize
              \multiply\ghor by 2
              \multiply\topline by 2
              \advance\topline by -1
              \put(\ghor,\topline){\hbox to 0pt{\hss{\notelabel}\hss}}}
             \let\curnote\stdnote
             \let\curfing\fingnote
             \edef\notelabel{}
      \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi % one for each in the 'select' loop
  } % end forc loop
  \ifnum\cpos > \yoff  % draw the last bit of vertical line
     \dlen=\cpos
     \advance\dlen by -\yoff
     \put(\ghor,\yoff){\cvline{\dlen}}
  \fi
}

\makeatother

% That's all folks!
%-------------------------------------------------------------------------
