%%% lilyponddefs.tex -- TeX macros for LilyPond output.
%%%
%%%  source file of the GNU LilyPond music typesetter
%%% 
%%% (c)  1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
%%%                 Han-Wen Nienhuys <hanwen@cs.uu.nl>
%%%                 Mats Bengtsson <mats.bengtsson@s3.kth.se>
%%%
%%
%% Avoid \par while reading this file.
%%
\edef\lilyponddefsELC{\the\endlinechar}%
\endlinechar -1\relax

%% This runs with plain TeX, LaTeX, pdftex, and texinfo.
%%
%% To avoid interferences, lilyponddefs.tex must be loaded within a group.
%% It is loaded only once, so the definitions must be global.
%%
%% The overall structure of a file created by LilyPond is as follows:
%%
%%   <lilypond parameter definitions>
%%   <font setup>
%%   \ifx\lilypondstart \undefined
%%     \input lilyponddefs
%%   \fi
%%   \lilypondstart
%%   <note output>
%%   \lilypondend

%% A temporary variable.
%%
\newdimen\lytempdim

%% The scaling factor for all dimensions.
%%
\newdimen\outputscale

\long\gdef\lilypondfirst#1#2{#1}
\long\gdef\lilypondsecond#1#2{#2}

%% \lilypondundefined{xxx}{foo}{bar}
%%
%%   If `xxx' (without the leading backslash) is an undefined macro,
%%   execute block `foo'.  Otherwise, execute block `bar'.  Based on
%%   a similar macro from the LaTeX kernel.
%%
\gdef\lilypondifundefined#1{
  \expandafter\ifx\csname#1\endcsname\relax
    \expandafter\lilypondfirst
  \else
    \expandafter\lilypondsecond
  \fi
}

%% Urgh.  LilyPond uses EC fonts, but texinfo is based on CM.  We thus
%% have to handle T1 font encoding by ourselves; all manipulations are
%% collected in the macro \lilypondECencoding.  Note that the following
%% code only provides the texinfo interface, not complete access to all
%% EC glyphs.
%%
%% All definitions are taken from texinfo or LaTeX (with modifications
%% if necessary).
%%
\begingroup
\catcode `\@=11\relax
\gdef\lilypondECencoding{
  \def\"##1{
    {\accent4 ##1}}
  \def\'##1{
    {\accent1 ##1}}
  \def\,##1{
    {\leavevmode
     \setbox\z@\hbox{##1}
     \ifdim\ht\z@=1ex
       \accent11 ##1
     \else
       {\ooalign{
          \unhbox\z@
          \crcr
          \hidewidth
          \char11
          \hidewidth}}
     \fi}}
  \def\=##1{
    {\accent9 ##1}}
  \def\^##1{
    {\accent2 ##1}}
  \def\`##1{
    {\accent0 ##1}}
  \def\~##1{
    {\accent3 ##1}}
  \def\dotaccent##1{
    {\accent10 ##1}}
  \def\H##1{
    {\accent5 ##1}}
  \def\ringaccent##1{
    {\accent6 ##1}}
% \def\tieaccent##1{}        % unsupported: this is TS1
  \def\u##1{
    {\accent8 ##1}}
  \def\ubaraccent##1{
    {\o@lign{
       \relax
       ##1
       \crcr
       \hidewidth
       \sh@ft{29}\vbox to.2ex{
         \hbox{\char9}
         \vss}
       \hidewidth}}}
  \def\udotaccent##1{
    {\o@lign{
       \relax
       ##1
       \crcr
       \hidewidth
       \sh@ft{10}.
       \hidewidth}}}
  \def\v##1{
    {\accent7 ##1}}

  \chardef\exclamdown=189
  \chardef\questiondown=190

  \def\aa{
    \ringaccent{a}}
  \def\AA{
    \ringaccent{A}}
  \chardef\AE=198
  \chardef\ae=230
  \chardef\ptexi=25
  \chardef\j=26
  \chardef\L=138
  \chardef\l=170
  \chardef\O=216
  \chardef\o=248
  \chardef\OE=215
  \chardef\oe=247
  \chardef\ss=255
}
\endgroup

%% This macro provides the necessary setup to make the lilypond data
%% work with plain TeX, LaTeX, and texinfo.
%%
%% The reason of using \begingroup and \endgroup is to make the macro \x
%% immediately disappear after it has been executed.  Since we have \def
%% within \def within \gdef, four hash signs (`#') are needed for
%% parameters.
%%
%% \lilypondfontencoding is emitted by LilyPond to set the encoding of
%% text strings.
%%
\gdef\lilypondstart{
  \frenchspacing
  \outputscale \lilypondpaperoutputscale\lilypondpaperunit

  \begingroup
  \catcode `\@=11\relax

  %% \@nodocument is defined as \relax after `\begin{document}'
  \lilypondifundefined{@nodocument}
    {%% Either plain TeX or texinfo or not at the beginning of LaTeX input.
     \def\x{
       \endgroup

       \def\lilypondfontencoding####1{
         \lilypondECencoding}
       \def\lilypondpagebreak{
         \eject}
       \def\lilypondnopagebreak{
         \ifvmode
           \penalty 10000\relax
         \fi}}}

    {%% LaTeX mode: Provide a complete preamble.
     \def\x{
       \endgroup

       %% Indicate that we shall emit `\end{document}' while executing
       %% \lilypondend.
       \def\lilyponddocument{}

       \def\lilypondfontencoding####1{
         \fontencoding{####1}
         \selectfont}
       \def\lilypondpagebreak{
         \newpage}
       \def\lilypondnopagebreak{
         \nopagebreak}

       \documentclass[\lilyponddocumentclassoptions]{article}

       %% As a safety guard, don't produce auxiliary files.
       \nofiles

       \usepackage[\lilypondpaperinputencoding]{inputenc}
       \pagestyle{empty}

       \lilypondifundefined{lilypondclassic}
         {%% If not in `classic' mode, undo LaTeX's page layout settings
          %% since LilyPond does the layout by itself.
          \topmargin-1in
          \headheight0pt\headsep0pt
          \oddsidemargin-1in
          \evensidemargin\oddsidemargin}

         {%% Otherwise center output horizontally, without changing the
          %% vertical positioning.
          \hsize\lilypondpaperlinewidth\lilypondpaperunit
          \lytempdim \paperwidth
          \advance\lytempdim -\the\hsize
          \lytempdim 0.5\lytempdim
          \advance\lytempdim -1in
          \oddsidemargin \lytempdim
          \evensidemargin \lytempdim}

       \parindent 0pt

       %% We can't directly say `\begin{document}' in this macro since
       %% older versions of texinfo.tex define \begin as \outer; this
       %% means that it causes an error if \begin is found within another
       %% macro (even if the corresponding code will never be executed).
       %% As a workaround we use \csname to call \begin.
       \csname begin\endcsname{document}}}
  \x}

%% The opposite of \lilypondstart.
%%
\gdef\lilypondend{
  %% Handle the `lastpagefill' parameter from the \layout block.
  %% Ignore it if \lilypondbook is defined.
  \lilypondifundefined{lilypondbook}
    {\lilypondifundefined{lilypondpaperlastpagefill}
       {\vskip 0pt plus\lilypondpaperinterscorelinefill00 fill}
       {}}
    {}

  \begingroup
  \lilypondifundefined{lilyponddocument}
    {\def\x{
       \endgroup}}
    {\def\x{
       \endgroup
       \csname end\endcsname{document}}}
  \x}

%% Load the PostScript drawing routines.  This is done using \special.
%% To avoid multiple inclusions, redefine \lilypondspecial to a no-op
%% afterwards.
%%
\gdef\lilypondspecial{
  \special{header=music-drawing-routines.ps}
  \gdef\lilypondspecial{}}




%% Put the argument into a box which has zero height and depth (the
%% \vss forces the latter -- TeXbook, page 80).
%%
\gdef\topalign#1{
  \vbox to 0pt{
    \hbox{#1}
    \vss}}

%% Put the argument into a box which has zero width.
%%
\gdef\leftalign#1{
  \hbox to 0pt{
    #1
    \hss}}

%% The most used macro in LilyPond output.  Put #3 into a zero-width box
%% which is moved to the right by #1 (scaled by \outputscale) and moved
%% up by #2 (also scaled by \outputscale).  Note that negative values for
%% #2 change the depth of the box defined by \leftalign, not the height.
%%
\gdef\lyitem#1#2#3{
  \raise#2\outputscale \leftalign{
    \kern#1\outputscale
    #3}}

%% All LilyPond music data is enclosed in this macro (as fifth argument).
%% The data is first put into a box with zero height and depth which is
%% then moved to the right by #1 and moved down by #2.  After this, it is
%% put into a zero-width box, giving it a height #4 (and depth zero).
%% Finally, the box is raised so that its true height is exactly the
%% distance from one baseline to the next.
%%
%% Parameters #1 to #4 are scaled by \outputscale.
%%
\gdef\lybox#1#2#3#4#5{
  \lytempdim\baselineskip
  \advance\lytempdim -#4\outputscale
  \raise\lytempdim
  \vbox to #4\outputscale{
    \leftalign{
      \kern #1\outputscale
      \lower #2\outputscale \topalign{
        #5}}
    \vss}}

%% Produce a black bar (width #2, depth #3, height #4) with a vertical
%% offset #1.  Everything is scaled by \outputscale.
%%
\gdef\lyvrule#1#2#3#4{
  \kern#1\outputscale
  \vrule width #2\outputscale depth #3\outputscale height #4\outputscale}

%% FIXME: 'interscoreline' and 'lilypondPAPERinterscoreline
%%
\lilypondifundefined{lilypondpaperinterscorelinefill}
  {\gdef\lilypondpaperinterscorelinefill{0}}
  {\gdef\lilypondpaperinterscorelinefill{1}}

%% Allow overriding of interscoreline, e.g., for LilyPond's --preview
%%
\lilypondifundefined{interscoreline}
  {\lilypondifundefined{lilypondclassic}
     {\gdef\interscoreline{}}
     {\gdef\interscoreline{
        \vskip\lilypondpaperinterscoreline\lilypondpaperunit
        plus \lilypondpaperinterscorelinefill fill}}}
  {}

%% Include PostScript definitions (which are differently defined for
%% TeX and pdfTeX).  This is loaded once only because the inputted files
%% define \lilypondpostscript.
%%
%% (Don't remove the spaces after the arguments to \input!)
%%
\lilypondifundefined{lilypondpostscript}
  {\lilypondifundefined{pdfoutput}
     {\input lily-ps-defs }
     {\pdfoutput = 1
      \input lily-pdf-defs }}
  {}

%% Restore newline functionality (disabled to avoid \par).
%%
\endlinechar \lilyponddefsELC
\endinput

%% end lilyponddefs.tex
