%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%      This file is part of the 'lilyglyphs' LaTeX package.              %
%                                ==========                              %
%                                                                        %
%              https://github.com/openlilylib/lilyglyphs                 %
%               http://www.openlilylib.org/lilyglyphs                    %
%                                                                        %
%  Copyright 2012-2020 Urs Liska and others, ul@openlilylib.org          %
%                                                                        %
%  'lilyglyphs' is free software: you can redistribute it and/or modify  %
%  it under the terms of the LaTeX Project Public License, either        %
%  version 1.3 of this license or (at your option) any later version.    %
%  You may find the latest version of this license at                    %
%               http://www.latex-project.org/lppl.txt                    %
%  more information on                                                   %
%               http://latex-project.org/lppl/                           %
%  and version 1.3 or later is part of all distributions of LaTeX        %
%  version 2005/12/01 or later.                                          %
%                                                                        %
%  This work has the LPPL maintenance status 'maintained'.               %
%  The Current Maintainer of this work is Urs Liska (see above).         %
%                                                                        %
%  This work consists of the files listed in the file 'manifest.txt'     %
%  which can be found in the 'license' directory.                        %
%                                                                        %
%  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.                  %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This file is part of the lilyglyphs package    %
% and defines the generic printing functionality %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is the internal command that actually prints the glyph(s)%
% Users aren't intended to use it directly                      %
% #1 are key-value options
% #2 the content to be printed
\newcommand*{\lilyPrint}[2][]{%
	% interpret optional argument
	\interpretLilyOptions{#1}%
	% print the glyph in a raisebox
	\raisebox{\lilyEffectiveRaise ex}{%
		{\fontspec[Scale=\lilyEffectiveScale]{emmentaler-\lilyOpticalSuffix.otf}#2}%
	}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Helper functions for \lilyPrintImage below

% Calculate the (point) ratio between the current font size
% and the size of \normalsize fonts.
% This is used to scale the output of \lilyPrintImage
% as images aren't scaled automatically with font size commands.
\makeatletter
\newcommand*{\currentFontRatio}{%
	% store point size of current font
	\xdef\currentFontSize{\f@size}%
	{%
		% Temporarily switch to normal size
		\normalsize%
		% and store normal point size
		\xdef\normalFontSize{\f@size}%
	}%
	% Calculate ratio and store the value
	\pgfmathsetmacro{\getCurrentFontRatio}{%
		\currentFontSize / \normalFontSize}%
}
\makeatother

% Apply the scaling factor that is necessary to
% accomodate the current font size.
% The accumulated scaling factor of the other sources
% is taken into account through \lilyEffectiveScale.
\newcommand*{\lilyScaleImage}{%
	\currentFontRatio%
	\pgfmathsetmacro{\lilyImageEffectiveScale}{%
		\lilyEffectiveScale * \getCurrentFontRatio}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load 'glyph' from an image (preferrably PDF).                 %
% Used for items that are created using LilyPond itself         %
% Users aren't intended to use it directly                      %
% #1 are key-value options
% #2 the content to be printed
\newcommand*{\lilyPrintImage}[2][]{%
	% interpret optional argument
	\interpretLilyOptions{#1}%
	% determine scaling factor to accomodate the current font size 
	% (as images don't scale automatically with the font)
	\lilyScaleImage%
	% Print the image in a raisebox
	\raisebox{\lilyEffectiveRaise ex}{%
		\includegraphics[scale=\lilyImageEffectiveScale]{#2.pdf}%
	}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generic command(s) to select a LilyPond Glyph %
% These shouldn't be used by end users but      %
% for designing predefined commands.            %
% They select a glyph by a specific method and  %
% return the #2 to be passed to \lilyPrint      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Determine the glyph by its OpenType glyphName

\usepackage{ifluatex,ifxetex}

\ifluatex
\usepackage{luaotfload,luacode} 
\begin{luacode}
documentdata = documentdata or { }

local stringformat = string.format
local texsprint = tex.sprint
local slot_of_name = luaotfload.aux.slot_of_name

documentdata.fontchar = function (chr)
local chr = slot_of_name(font.current(), chr, false)
if chr and type(chr) == "number" then
texsprint
 (\the\CatcodeTableLaTeX, 
  stringformat ([[\char"%X]], chr))
end
end
\end{luacode}

\def\lilyGetGlyph#1{\directlua{documentdata.fontchar "#1"}}
\fi

\ifxetex
\newcommand*{\lilyGetGlyph}[1]{\XeTeXglyph\XeTeXglyphindex"#1" }
\fi

% Determine the glyph by its OpenType character number
% (these may change with new versions of the fonts!)
\newcommand*{\lilyGetGlyphByNumber}[1]{\char"#1 }


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generic commands to print glyphs that aren't %
% covered by predefined commands yet.          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print a glyph by its glyph name. %
% Glyph names have to be looked up %
% in the docs.                     %

\newcommand*{\lilyGlyph}[2][]{%
	% set defaults
	\setkeys{lilyDesignOptions}{scale=1,raise=0}%
	\lilyPrint[#1]{\lilyGetGlyph{#2}}%
}	

% Determine the glyph by its Unicode number 
% (works for those glyphs that reside in 'standard' places)
\newcommand*{\lilyGlyphByNumber}[2][]{%
	% set defaults
	\setkeys{lilyDesignOptions}{scale=1,raise=0}%
	\lilyPrint[#1]{\lilyGetGlyphByNumber{#2}}%
}

% Don't 'determine' glyph positions but just use plain text.
% This works for dynamics letters, numbers and + - . ,
\newcommand*{\lilyText}[2][]{%
	% set defaults
	\setkeys{lilyDesignOptions}{scale=1,raise=0}%
	\lilyPrint[#1]{#2}%
}

% Load 'glyph' from an image (preferrably PDF).
% Used for items that are created using LilyPond itself.
\newcommand*{\lilyImage}[2][]{%
	% set defaults
	\setkeys{lilyDesignOptions}{scale=1, raise=0}%
	% interpret optional argument
	\interpretLilyOptions{#1}%
	\raisebox{\lilyEffectiveRaise ex}{%
		\includegraphics[scale=\lilyEffectiveScale]{#2}%
	}%
}