% avec fonds perdus 3mm
\usepackage[paperwidth=154mm, paperheight=216mm]{geometry}
% sans fonds perdus :
% \usepackage[paperwidth=148.5mm, paperheight=216mm]{geometry}

\usepackage{fontspec}
\usepackage[nolocalmarks]{polyglossia}
\usepackage[table]{xcolor}
\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{setspace}
\usepackage{expl3}
\usepackage{hyperref}
\usepackage{refcount}
\usepackage{needspace}
\usepackage{etoolbox}
\usepackage{enumitem}

\setdefaultlanguage[variant=french, frenchitemlabels=false]{french}

\geometry{
inner=27.5mm, % 25mm sans fonds perdus
outer=15mm, % 12mm sans fonds perdus
top=15mm, % 12mm sans fonds perdus
bottom=15mm, % 12mm sans fonds perdus
headsep=3mm,
}

%% General scale of all graphical elements.
%% Values different from 1 are largely untested.
%% Used in those commands (e.g. everything FontSpec) that use a scale parameter.
\newcommand{\customscale}{1}

%% Provide the command \fpevalc as a copy of the code-level \fp_eval:n.
%% \fpevalc allows to evaluate floating point calculation for scaled parameters, e.g. \setSomeStretchFactor{\fpevalc{0,6 * \customscale}}
\ExplSyntaxOn
\cs_new_eq:NN \fpevalc \fp_eval:n
\ExplSyntaxOff

%% No indentation of paragraphs
\setlength{\parindent}{0mm}

%% We want to allow large inter-words space 
%% to avoid overfull boxes in two-columns rubrics.
\sloppy


%%%%%%%%%%%%%%% GREGORIO CONFIG %%%%%%%%%%%%%%%

\usepackage[forcecompile]{gregoriotex}

%% this limits how much scores can stretch vertically
%% when they are forced to adhere to the bottom of a page (i.e followed by \pagebreak)
%\grechangedim{baselineskip}{65pt plus 1pt minus 5pt}{scalable}

%% text above lines shall be of color gregoriocolor
\grechangestyle{abovelinestext}{\color{gregoriocolor}\footnotesize\itshape}
%% fine-tuning of space beween the staff and the text above lines
\newcommand{\altraise}{0mm} %% default is -0.1cm
\grechangedim{abovelinestextraise}{\altraise}{scalable}
\grechangedim{abovelinestextheight}{10mm}{scalable}

%% fine-tuning of space between the staff and the lyrics
\newcommand{\textraise}{2.8ex} %% default is 3.48471 ex
\grechangedim{spacelinestext}{\textraise}{scalable}

%% fine-tuning of space between the initial and the annotations
\newcommand{\annraise}{0mm} %% default is -0.2mm
\grechangedim{annotationraise}{\annraise}{scalable}

%% fine-tuning the behavior of text placed under bars. We use the so-called "new algorithm" which
%% places the bar in the middle of surrounding notes, and the text in the middle of surrounding text.
%% however, we restrict drastically the deviation of the text from the position of the bar.
\grechangedim{maxbaroffsettextleft}{0.5mm}{scalable}
\grechangedim{maxbaroffsettextright}{0.5mm}{scalable}

%% in case we show NABC, font selection
\gresetnabcfont{gregall}{12} 

\newcommand{\smallscore}[1]{
  \gresetinitiallines{0}
  \gregorioscore{gabc/#1}
  \gresetinitiallines{1}
}

\newcommand{\gscore}[1]{
  \gregorioscore{gabc/#1}
}

% we want to print NABC 'cause this is the RG
\gresetnabc{1}{visible}

%correction of the space before and after the score by exchanging spaceabovelines for spacebeneathtext
\grechangedim{spaceabovelines}{-3mm}{scalable}
\grechangedim{spacebeneathtext}{3mm}{scalable}

%%%%%%%%%%%%%%% FONTS %%%%%%%%%%%%%%%

%%%%%%%%%%%%%%% Main font
\setmainfont[Ligatures=TeX, Scale=\customscale]{Charis SIL}
\setstretch{\fpevalc{1.05 * \customscale}}

%%%%%%%%%%%%%%% Score initials
%% \initialsize resizes the initials, with one argument (size in points)
\newcommand{\initialsize}[1]{
    \grechangestyle{initial}{\fontspec{Zallman Caps}\fontsize{#1}{#1}\selectfont}
}
%% default initial size is 32 points
\newcommand{\defaultinitialsize}{28}
\initialsize{\defaultinitialsize}

%% spacing before and after initials to kern the Zallman Caps.
%% this should be changed if we move away from Zallman Caps.
\newcommand{\initialspace}[2]{
  \grechangedim{afterinitialshift}{#2}{scalable}
  \grechangedim{beforeinitialshift}{#1}{scalable}
}
%% default space before and after initials is 0mm to the left and 2mm to the right.
\newcommand{\defaultannotationshift}{-2mm}
\newcommand{\defaultinitialspace}{\initialspace{0mm}{-\defaultannotationshift}}
\defaultinitialspace{}

%%%%%%%%%%%%%%% Score annotations
\grechangestyle{annotation}{\small}

%%%%%%%%%%%%%%% GRAPHICAL ELEMENTS %%%%%%%%%%%%%%%

%% V/, R/, A/ and + signs for in-line use (\vv \rr \aa \cc)
\newcommand{\specialcharhsep}{3mm} % space after invoking R/ or V/ or A/ outside rubrics
\newcommand{\vv}{\textcolor{gregoriocolor}{\fontspec[Scale=\customscale]{Charis SIL}℣.\nolinebreak[4]\hspace{\specialcharhsep}\nolinebreak[4]}}
\newcommand{\rr}{\textcolor{gregoriocolor}{\fontspec[Scale=\customscale]{Charis SIL}℟.\nolinebreak[4]\hspace{\specialcharhsep}\nolinebreak[4]}}
\renewcommand{\aa}{\textcolor{gregoriocolor}{\fontspec[Scale=\customscale]{Charis SIL}\Abar.\nolinebreak[4]\hspace{\specialcharhsep}\nolinebreak[4]}}
\newcommand{\cc}{\textcolor{gregoriocolor}{\fontspec[Scale=\customscale]{FreeSerif}\symbol{"2720}~}}
%% Same special characters, for in-score use (<sp>V/ R/ A/ +</sp>)
\gresetspecial{V/}{\textcolor{gregoriocolor}{\fontspec[Scale=\customscale]{Charis SIL}℣.~}}
\gresetspecial{R/}{\textcolor{gregoriocolor}{\fontspec[Scale=\customscale]{Charis SIL}℟.~}}
\gresetspecial{A/}{\textcolor{gregoriocolor}{\fontspec[Scale=\customscale]{Charis SIL}\Abar.~}}
\gresetspecial{+}{{\fontspec[Scale=\customscale]{FreeSerif}†~}}
\gresetspecial{*}{\gresixstar}
\gresetspecial{cross}{\textcolor{gregoriocolor}{\fontspec[Scale=\customscale]{FreeSerif}\symbol{"2720}}}
\gresetspecial{labiacross}{\textcolor{gregoriocolor}{+}}
%% Same special characters, for use in rubrics (no space, and no red command since it will be reddified with the rest)
\newcommand{\vvrub}{{\fontspec[Scale=\customscale]{Charis SIL}℣.~}}
\newcommand{\rrrub}{{\fontspec[Scale=\customscale]{Charis SIL}℟.~}}
\newcommand{\aarub}{{\fontspec[Scale=\customscale]{Charis SIL}\Abar.~}}

%% the asterisk as found in the mediants of text-only psalms
\newcommand{\psstar}{\GreSpecial{*}}
\newcommand{\pscross}{\GreSpecial{+}}
%% also, most psalms do not call those but use † and * - todo

%% Macro to print rubrics and normal text inside of them
\newcommand{\rubric}[1]{\textcolor{gregoriocolor}{\emph{#1}}}
\newcommand{\normaltext}[1]{{\normalfont\normalcolor #1}}

%%%%%%%%%%%%%%% COLUMN MANAGEMENT %%%%%%%%%%%%%%%

\usepackage{paracol}
\usepackage{multicol}
\setlength\columnseprule{0.4pt}
\setlength{\multicolsep}{6pt plus 2pt minus 1.5pt}

\newcommand{\psalm}[2]{
	\vspace{0.5\baselineskip}
	\begin{paracol}{2}
	\begin{itemize}[
		label=\null, 
		leftmargin=0pt, 
		itemindent=10pt, 
		labelsep=0pt, 
		labelwidth=0pt, 
		rightmargin=0pt, 
		parsep=0pt, 
		itemsep=0pt,
		topsep=-2mm]
	\input{psalmi_la/#1_#2.tex}
	\end{itemize}
	\switchcolumn
	\begin{itemize}[
		label=\null, 
		leftmargin=0pt, 
		itemindent=10pt, 
		labelsep=0pt, 
		labelwidth=0pt, 
		rightmargin=0pt, 
		parsep=0pt, 
		itemsep=0pt,
		topsep=-2mm]
	\input{psalmi_fr/#1.tex}
	\end{itemize}
	\end{paracol}
}

\newcommand{\twocoltext}[2]{
	\begin{paracol}{2}
#1
	\switchcolumn
#2
	\end{paracol}
}

\newcommand{\columnbalancinglatin}[1]{
\vspace{2mm}#1
}

\newcommand{\smalltitle}[1]{
  \vspace{0.5\baselineskip}
  \par{\centering\textbf{#1}\par}
  \vspace{0.5\baselineskip}
}

\newcommand{\bigtitle}[2]{
  \begin{center}
	{\fontspec[Scale=\customscale]{Futura Book}
	\MakeUppercase{\Large #1}\\
	{#2}
	}
  \end{center}
}

 \newcommand{\translation}[1]{
	\emph{#1}
 }
 
\makeatletter
\newcommand{\@printReading}[1]{
    \def\content{%
        {#1
        }
    \par\noindent}
    \content
\egroup\endgroup}
\newcommand\reading{\begingroup%
  \begingroup%
    \lccode`\~=9\relax%
    \lowercase{%
  \endgroup
    \def~%
  }{\hspace{8mm}}%
  \catcode9=\active%
  \bgroup\obeylines\@printReading}
\makeatother

\newcommand{\hymntranslation}[1]{
 	\begin{multicols}{2}
	\reading{#1}
	\end{multicols}
}
