%% tolkienfonts.sty
%% Part of the tolkienfonts package
%% Copyright 2010 Ariel Barton
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% 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 Ariel Barton.
%
% This work consists of all the files listed in Appendix F of the file
% `tolkienfontsdoc.pdf'.
%
% Version: 2

\ProvidesPackage{tolkienfonts}[2010/08/29]

\DeclareTextFontCommand {\saratifont}{\fontencoding{T1}\fontfamily{saratie}\selectfont}
\DeclareTextFontCommand {\valmarfont}{\fontencoding{T1}\fontfamily{valmaric}\selectfont}

\DeclareTextFontCommand {\cirthS}{\fontencoding{T1}\fontfamily{cirthsxx}\selectfont}
\newcommand{\sunrune}{%
	\def \eregionfamily{cirthsqe}%
	\def  \daeronfamily{cirthssd}%
	\def   \moriafamily{cirthsdm}%
	\def  \ereborfamily{cirthsee}%
	\def  \hobbitfamily{cirthseh}%
	\let\cirthfont\cirthS
}
\DeclareTextFontCommand {\cirthE}{\fontencoding{T1}\fontfamily{cirthexx}\selectfont}
\newcommand{\fonterebor}{%
	\def \eregionfamily{cirtheqe}%
	\def  \daeronfamily{cirthesd}%
	\def   \moriafamily{cirthedm}%
	\def  \ereborfamily{cirtheee}%
	\def  \hobbitfamily{cirtheeh}%
	\let\cirthfont\cirthE
}
\fonterebor
\def  \hobbitfamily{cirthseh}%

\DeclareTextFontCommand {\TengwarA}{\fontencoding{T1}\fontfamily{tengwara}\selectfont}
\DeclareTextFontCommand{\TengwarAA}{\fontencoding{T1}\fontfamily{tengwaraa}\selectfont}
\newcommand{\annatar}{%
	\def   \quenyafamily{tengwaraqq}%
	\def\quenyagenfamily{tengwaraqg}%
	\def\quenyaredfamily{tengwaraqr}%
	\def  \quenyaafamily{tengwaraqa}%
	\def   \gondorfamily{tengwarasg}%
	\def    \arnorfamily{tengwarasa}%
	\def\beleriandfamily{tengwarasb}%
	\def    \orthfamily{tengwaraeo}%
	\def\mazarbulfamily{tengwaraem}%
	\def   \tehtafamily{tengwaraet}%
	\def\phonemicfamily{tengwaraep}%
	\let\tengfont\TengwarA
	\let\tengalt\TengwarAA
}
\DeclareTextFontCommand {\TengwarP}{\fontencoding{T1}\fontfamily{tengwarp}\selectfont}
\DeclareTextFontCommand{\TengwarPA}{\fontencoding{T1}\fontfamily{tengwarpa}\selectfont}
\newcommand{\parmaite}{%
	\def   \quenyafamily{tengwarpqq}%
	\def\quenyagenfamily{tengwarpqg}%
	\def\quenyaredfamily{tengwarpqr}%
	\def  \quenyaafamily{tengwarpqa}%
	\def   \gondorfamily{tengwarpsg}%
	\def    \arnorfamily{tengwarpsa}%
	\def\beleriandfamily{tengwarpsb}%
	\def    \orthfamily{tengwarpeo}%
	\def\mazarbulfamily{tengwarpem}%
	\def   \tehtafamily{tengwarpet}%
	\def\phonemicfamily{tengwarpep}%
	\let\tengfont\TengwarP
	\let\tengalt\TengwarPA
}
\DeclareTextFontCommand {\TengwarE}{\fontencoding{T1}\fontfamily{tengware}\selectfont}
\DeclareTextFontCommand{\TengwarEA}{\fontencoding{T1}\fontfamily{tengwarea}\selectfont}
\newcommand{\eldamar}{%
	\def   \quenyafamily{tengwareqq}%
	\def\quenyagenfamily{tengwareqg}%
	\def\quenyaredfamily{tengwareqr}%
	\def  \quenyaafamily{tengwareqa}%
	\def   \gondorfamily{tengwaresg}%
	\def    \arnorfamily{tengwaresa}%
	\def\beleriandfamily{tengwaresb}%
	\def    \orthfamily{tengwareeo}%
	\def\mazarbulfamily{tengwareem}%
	\def   \tehtafamily{tengwareet}%
	\def\phonemicfamily{tengwareep}%
	\let\tengfont\TengwarE
	\let\tengalt\TengwarEA
}
\DeclareTextFontCommand {\TengwarN}{\fontencoding{T1}\fontfamily{tengwarn}\selectfont}
\DeclareTextFontCommand{\TengwarNA}{\fontencoding{T1}\fontfamily{tengwarna}\selectfont}
\newcommand{\noldor}{%
	\def   \quenyafamily{tengwarnqq}%
	\def\quenyagenfamily{tengwarnqg}%
	\def\quenyaredfamily{tengwarnqr}%
	\def  \quenyaafamily{tengwarnqa}%
	\def   \gondorfamily{tengwarnsg}%
	\def    \arnorfamily{tengwarnsa}%
	\def\beleriandfamily{tengwarnsb}%
	\def    \orthfamily{tengwarneo}%
	\def\mazarbulfamily{tengwarnem}%
	\def   \tehtafamily{tengwarnet}%
	\def\phonemicfamily{tengwarnep}%
	\let\tengfont\TengwarN
	\let\tengalt\TengwarNA
}
\DeclareTextFontCommand {\TengwarQ}{\fontencoding{T1}\fontfamily{tengwarq}\selectfont}
\DeclareTextFontCommand{\TengwarQA}{\fontencoding{T1}\fontfamily{tengwarqa}\selectfont}
\newcommand{\fontquenya}{%
	\def   \quenyafamily{tengwarqqq}%
	\def\quenyagenfamily{tengwarqqg}%
	\def\quenyaredfamily{tengwarqqr}%
	\def  \quenyaafamily{tengwarqqa}%
	\def   \gondorfamily{tengwarqsg}%
	\def    \arnorfamily{tengwarqsa}%
	\def\beleriandfamily{tengwarqsb}%
	\def    \orthfamily{tengwarqeo}%
	\def\mazarbulfamily{tengwarqem}%
	\def   \tehtafamily{tengwarqet}%
	\def\phonemicfamily{tengwarqep}%
	\let\tengfont\TengwarQ
	\let\tengalt\TengwarQA
}
\DeclareTextFontCommand {\TengwarS}{\fontencoding{T1}\fontfamily{tengwars}\selectfont}
\DeclareTextFontCommand{\TengwarSA}{\fontencoding{T1}\fontfamily{tengwarsa}\selectfont}
\newcommand{\fontsindarin}{%
	\def   \quenyafamily{tengwarsqq}%
	\def\quenyagenfamily{tengwarsqg}%
	\def\quenyaredfamily{tengwarsqr}%
	\def  \quenyaafamily{tengwarsqa}%
	\def   \gondorfamily{tengwarssg}%
	\def    \arnorfamily{tengwarssa}%
	\def\beleriandfamily{tengwarssb}%
	\def    \orthfamily{tengwarseo}%
	\def\mazarbulfamily{tengwarsem}%
	\def   \tehtafamily{tengwarset}%
	\def\phonemicfamily{tengwarsep}%
	\let\tengfont\TengwarS
	\let\tengalt\TengwarSA
}

\annatar

\newcommand{\defaultbase}{10}
\newcommand{\donothing}[1]{#1}

\let \savehat \^

\newcommand{\tengwarabbrevs}{%
	\sfcode`\,=2000 \sfcode`\;=2000 \sfcode`)=1000 \sfcode`]=1000 \sfcode`?=1999 \sfcode`!=1999 
	\count255=`A
	\loop\ifnum\count255<`Z \sfcode\the\count255 = 1000 \advance\count255 1\repeat
	\sfcode`Z=1000
	\let\^\'\let\R+\chardef\s 0
	\let\ten<\let\eleven>\chardef\twelve `\%
	\def\ldots{,{},{}, }%
}

\newcommand{\cirthabbrevs}{%
	\sfcode`\,=2000 \sfcode`\;=2000
	\count255=`A
	\loop\ifnum\count255<`Z \sfcode\the\count255 = 1000 \advance\count255 1\repeat
	\sfcode`Z=1000
	\let\^\'%
	\def\ldots{,{},{}, }%
	\let\ \cirth@dotspace%
}

\newcommand{\saratiabbrevs}{%
	\sfcode`\,=2000 \sfcode`\;=2000
	\count255=`A
	\loop\ifnum\count255<`Z \sfcode\the\count255 = 1000 \advance\count255 1\repeat
	\sfcode`Z=1000
	\let\^\'%
	\let\ten<\let\eleven>\chardef\twelve `\%
	\def\ldots{,{},{}, }%
	\chardef \s 0
}

\newcommand{\quenyaabbrevs}{%
	\tengwarabbrevs
	\chardef\r 15
	\chardef\sa 16
	\chardef\se 17
	\chardef\si 18
	\chardef\so 19
	\chardef\su 20
	\chardef\sy 21
}

\newcommand{\sindarinabbrevs}{\tengwarabbrevs}

\newcommand{\englishabbrevs}{%
	\tengwarabbrevs%
	\chardef\es 15
	\def\The{dh|}\def\THE{dh|}%
	\def  \of{v|}\def\ofthe{vv|}%
	\def  \&{ndE}%
}

\newcommand{\tengwarnumbering}{
  \renewcommand{\thepage}{\reversedigits{\arabic{page}}}
  \renewcommand{\MakeUppercase}{\relax} 
  \@ifundefined{chapter}
	  {
		 \@ifundefined{thesection}{}{\renewcommand{\thesection}{\reversedigits{\arabic{section}}}}
		 \@ifundefined{thesubsection}{}{\renewcommand{\thesubsection}{\reversedigits{\arabic{subsection}},\reversedigits{\arabic{section}}}}
		 \@ifundefined{thesubsubsection}{}{\renewcommand{\thesubsubsection}{\reversedigits{\arabic{subsubsection}},\reversedigits{\arabic{subsection}},\reversedigits{\arabic{section}}}}
	  }
	  {	 \renewcommand{\thechapter}{\reversedigits{\arabic{chapter}}}
		 \renewcommand{\thesection}{\reversedigits{\arabic{section}},\reversedigits{\arabic{chapter}}}
		 \renewcommand{\thesubsection}{\reversedigits{\arabic{subsection}},\reversedigits{\arabic{section}},\reversedigits{\arabic{chapter}}}
		 \renewcommand{\thesubsubsection}{\reversedigits{\arabic{subsubsection}},\reversedigits{\arabic{subsection}},\reversedigits{\arabic{section}},\reversedigits{\arabic{chapter}}}
	  }
}

\newcommand{\elvishoption}{
  \DeclareTextFontCommand{\textlatin}{\fontencoding{T1}\fontfamily{cmr}\selectfont}
  \renewcommand{\defaultbase}{12}\tengwarnumbering}

\DeclareOption{sarati}{
  \renewcommand{\rmdefault}{saralqq}\AtBeginDocument\saratiabbrevs\elvishoption}
\DeclareOption{valmaric}{
  \renewcommand{\rmdefault}{valmlqq}\AtBeginDocument\saratiabbrevs\elvishoption}
\DeclareOption{eregion}{
  \renewcommand{\rmdefault}{\eregionfamily}\AtBeginDocument\cirthabbrevs\elvishoption}
\DeclareOption{daeron}{
  \renewcommand{\rmdefault}{\daeronfamily}\AtBeginDocument\cirthabbrevs\elvishoption}
\DeclareOption{moria}{
  \renewcommand{\rmdefault}{\moriafamily}\AtBeginDocument\cirthabbrevs\elvishoption}
\DeclareOption{quenya}{
  \renewcommand{\rmdefault}{\quenyafamily}\AtBeginDocument\quenyaabbrevs\elvishoption}
\DeclareOption{quenyagen}{
  \renewcommand{\rmdefault}{\quenyagenfamily}\AtBeginDocument\quenyaabrevs\elvishoption}
\DeclareOption{quenyared}{
  \renewcommand{\rmdefault}{\quenyaredfamily}\AtBeginDocument\quenyaabrevs\elvishoption}
\DeclareOption{quenyaa}{
  \renewcommand{\rmdefault}{\quenyaafamily}\AtBeginDocument\quenyaabrevs\elvishoption}
\DeclareOption{gondor}{
  \renewcommand{\rmdefault}{\gondorfamily}\AtBeginDocument\sindarinabbrevs\elvishoption}
\DeclareOption{arnor}{
  \renewcommand{\rmdefault}{\arnorfamily}\AtBeginDocument\sindarinabbrevs\elvishoption}
\DeclareOption{beleriand}{
  \renewcommand{\rmdefault}{\beleriandfamily}\AtBeginDocument\sindarinabbrevs\elvishoption}
  
\newcommand{\englishoption}{
  \DeclareTextFontCommand{\textlatin}{\fontencoding{T1}\fontfamily{cmr}\selectfont}
  \tengwarnumbering
   \def\today{%
    \reversedigits{\number\day}%   
    \space 
    \ifcase\month
    \or january\or february\or march\or april\or may\or junE\or
    july\or aUgust\or septembEr\or octobEr\or novembEr\or
    decembEr\fi
     \space \reversedigits{\number\year}}
  \@ifundefined{abstractname}{}{\renewcommand{\abstractname}{abstract}}
  \@ifundefined{appendixname}{}{\renewcommand{\appendixname}{appendix}}
  \@ifundefined{bibname}{}{\renewcommand{\bibname}{bibliography}}
  \@ifundefined{ccname}{}{\renewcommand{\ccname}{cc}}
  \@ifundefined{chaptername}{}{\renewcommand{\chaptername}{chaptEr}}
  \@ifundefined{contentsname}{}{\renewcommand{\contentsname}{contents}}
  \@ifundefined{enclname}{}{\renewcommand{\enclname}{enclosurE}}
  \@ifundefined{figurename}{}{\renewcommand{\figurename}{figurE}}
  \@ifundefined{headtoname}{}{\renewcommand{\headtoname}{to}}
  \@ifundefined{indexname}{}{\renewcommand{\indexname}{index}}
  \ifx\ \cirth@dotspace
  \@ifundefined{listfigurename}{}{\renewcommand{\listfigurename}{list of figurEs}}
  \@ifundefined{listtablename}{}{\renewcommand{\listtablename}{list of tablEs}}
  \else
  \@ifundefined{listfigurename}{}{\renewcommand{\listfigurename}{list v| figurEs}}
  \@ifundefined{listtablename}{}{\renewcommand{\listtablename}{list v| tablEs}}
  \fi
  \@ifundefined{pagename}{}{\renewcommand{\pagename}{pagE}}
  \@ifundefined{partname}{}{\renewcommand{\partname}{part}}
  \@ifundefined{refname}{}{\renewcommand{\refname}{refErenCEs}}
  \@ifundefined{tablename}{}{\renewcommand{\tablename}{tablE}}
}
  
\DeclareOption{orthmode}{\renewcommand{\rmdefault}{\orthfamily}\englishabbrevs\englishoption}
\DeclareOption{mazarbul}{\renewcommand{\rmdefault}{\mazarbulfamily}\englishabbrevs\englishoption}
\DeclareOption{tehtamode}{\renewcommand{\rmdefault}{\tehtafamily}\englishabbrevs\englishoption}
\DeclareOption{phonemic}{\renewcommand{\rmdefault}{\phonemicfamily}\englishabbrevs \let\^\savehat \englishoption}
\DeclareOption{erebor}{\renewcommand{\rmdefault}{\ereborfamily}\cirthabbrevs\englishoption}
\DeclareOption{hobbit}{\renewcommand{\rmdefault}{\hobbitfamily}\cirthabbrevs\englishoption}

\ProcessOptions

\RequirePackage[T1]{fontenc}

\RequirePackage{ifthen,calc}

%%%%%% Numbers %%%%%%


\newcounter{tengwarnumctrA}
\newcounter{tengwarnumctrB}
\newcounter{tengwarnumctrC}

\newcommand{\reversedigits}[2][\defaultbase]{%
	\setcounter{tengwarnumctrB}{0}\setcounter{tengwarnumctrC}{0}\setcounter{tengwarnumctrA}{#2}%
	\ifthenelse{\equal{#2}{0}}{0}{%
	\whiledo{\value{tengwarnumctrA}>0}{%
	\setcounter{tengwarnumctrB}{\value{tengwarnumctrA}-#1*(\value{tengwarnumctrA}/#1)}%
	\ifthenelse{\equal{\value{tengwarnumctrB}}{0}\and\equal{\value{tengwarnumctrC}}{0}}{0}{}%
	\ifthenelse{\equal{\value{tengwarnumctrB}}{10}}{%
		\ifthenelse {\equal{\value{tengwarnumctrC}}{0}}{}{\arabic{tengwarnumctrC}}\ten \setcounter{tengwarnumctrC}{0}}%
		{}%
	\ifthenelse{\equal{\value{tengwarnumctrB}}{11}}{%
		\ifthenelse {\equal{\value{tengwarnumctrC}}{0}}{}{\arabic{tengwarnumctrC}}\eleven \setcounter{tengwarnumctrC}{0}}%
		{}%
	\ifthenelse{\value{tengwarnumctrB}<10}{%
		\setcounter{tengwarnumctrC}{10*\value{tengwarnumctrC}}%
		\addtocounter{tengwarnumctrC}{\value{tengwarnumctrB}}}{}%
	\setcounter{tengwarnumctrA}{\value{tengwarnumctrA}/#1}%
	}%
	\ifthenelse{\equal{\value{tengwarnumctrC}}{0}}{}{\arabic{tengwarnumctrC}}%
	}}

%%%%% Tengwar commands and environments %%%%%

\newenvironment{quenya}{\fontencoding{T1}\fontfamily{\quenyafamily}\fontshape{n}\selectfont\quenyaabbrevs\ignorespaces}{}
\newenvironment{quenyagen}{\fontencoding{T1}\fontfamily{\quenyagenfamily}\fontshape{n}\selectfont\quenyaabbrevs\ignorespaces}{}
\newenvironment{quenyared}{\fontencoding{T1}\fontfamily{\quenyaredfamily}\fontshape{n}\selectfont\quenyaabbrevs\special{pdf: 0 g 0 G}\ignorespaces}{}
\newenvironment{quenyaa}{\fontencoding{T1}\fontfamily{\quenyaafamily}\fontshape{n}\selectfont\quenyaabbrevs\special{pdf: 0 g 0 G}\ignorespaces}{}

\newenvironment{gondor}{\fontencoding{T1}\fontfamily{\gondorfamily}\fontshape{n}\selectfont\sindarinabbrevs\ignorespaces}{}
\newenvironment{arnor}{\fontencoding{T1}\fontfamily{\arnorfamily}\fontshape{n}\selectfont\sindarinabbrevs\ignorespaces}{}
\newenvironment{beleriand}{\fontencoding{T1}\fontfamily{\beleriandfamily}\fontshape{n}\selectfont\sindarinabbrevs\ignorespaces}{}

\newenvironment{orthmode}{\fontencoding{T1}\fontfamily{\orthfamily}\fontshape{n}\selectfont\englishabbrevs\ignorespaces}{}
\newenvironment{mazarbul}{\fontencoding{T1}\fontfamily{\mazarbulfamily}\fontshape{n}\selectfont\englishabbrevs\ignorespaces}{}
\newenvironment{tehtamode}{\fontencoding{T1}\fontfamily{\tehtafamily}\fontshape{n}\selectfont\englishabbrevs\ignorespaces}{}
\newenvironment{phonemic}{\fontencoding{T1}\fontfamily{\phonemicfamily}\fontshape{n}\selectfont\englishabbrevs \let\^\savehat \ignorespaces}{}

\def   \Quenya#1{\begin{quenya}#1\end{quenya}}
\def\Quenyagen#1{\begin{quenyagen}#1\end{quenyagen}}
\def\Quenyared#1{\begin{quenyared}#1\end{quenyared}}
\def\QuenyaA#1{\begin{quenyaa}#1\end{quenyaa}}

\def   \Gondor#1{\begin{gondor}#1\end{gondor}}
\def    \Arnor#1{\begin{arnor}#1\end{arnor}}
\def\Beleriand#1{\begin{beleriand}#1\end{beleriand}}
\def \Orthmode#1{\begin{orthmode}#1\end{orthmode}}
\def \Mazarbul#1{\begin{mazarbul}#1\end{mazarbul}}
\def\Tehtamode#1{\begin{tehtamode}#1\end{tehtamode}}
\def \Phonemic#1{\begin{phonemic}#1\end{phonemic}}

%%%%% Cirth environments and commands %%%%%

\newenvironment{daeron}{\fontencoding{T1}\fontfamily{\daeronfamily}\fontshape{n}\selectfont\cirthabbrevs\ignorespaces}{}
\newenvironment{erebor}{\fontencoding{T1}\fontfamily{\ereborfamily}\fontshape{n}\selectfont\cirthabbrevs \def\The{\char25}\def\THE{\char25}\ignorespaces}{}
\newenvironment{eregion}{\fontencoding{T1}\fontfamily{\eregionfamily}\fontshape{n}\selectfont\cirthabbrevs\ignorespaces}{}
\newenvironment{moria}{\fontencoding{T1}\fontfamily{\moriafamily}\fontshape{n}\selectfont\cirthabbrevs\ignorespaces}{}
\newenvironment{hobbit}{\fontencoding{T1}\fontfamily{\hobbitfamily}\fontshape{n}\selectfont\cirthabbrevs\def\p{\char1}\def\P{\char1}\ignorespaces}{}

\newcommand{\Daeron}[1]{\begin{daeron}#1\end{daeron}}
\newcommand{\Erebor}[1]{\begin{erebor}#1\end{erebor}}
\newcommand{\Eregion}[1]{\begin{eregion}#1\end{eregion}}
\newcommand{\Moria}[1]{\begin{moria}#1\end{moria}}
\newcommand{\Hobbit}[1]{\begin{hobbit}#1\end{hobbit}}

%%%%% Sarati environments and commands %%%%%

\newcommand{\Sarati}[1]{\begin{sarati}#1\end{sarati}}
\newcommand{\Valmaric}[1]{\begin{valmaric}#1\end{valmaric}}
\newcommand{\SaratiA}[1]{\begin{saratia}#1\end{saratia}}
\newcommand{\ValmaricA}[1]{\begin{valmarica}#1\end{valmarica}}

\newenvironment{sarati}{\fontencoding{T1}\fontfamily{saratieqq}\fontshape{n}\selectfont\saratiabbrevs\ignorespaces}{}
\newenvironment{valmaric}{\fontencoding{T1}\fontfamily{valmariceqq}\fontshape{n}\selectfont\saratiabbrevs\ignorespaces}{}

\newenvironment{saratia}{\fontencoding{T1}\fontfamily{saratieqa}\fontshape{n}\selectfont\saratiabbrevs\ignorespaces}{}
\newenvironment{valmarica}{\fontencoding{T1}\fontfamily{valmariceqa}\fontshape{n}\selectfont\saratiabbrevs\ignorespaces}{}

%%%% Fancy space and punctuation %%%%%
	
% \fontdimen1 = italic slant
% \fontdimen2 = font spacing
% \fontdimen3 = space stretch
% \fontdimen4 = space shrink
% \fontdimen7 = ``extra'' space after a period

\newdimen\cirth@prepunct
	
\def\cirth@dotspace{\ifnum\spacefactor<2000 \hskip 0pt plus\fontdimen3\the\font minus 1.5\fontdimen4\the\font\setbox0=\hbox{\char0}\cleaders\copy0\hskip\wd0\fi%
	\hskip\fontdimen7\the\font\hskip\fontdimen2\the\font plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\relax}

\def\cirth@period{\ifnum\spacefactor<2000\nobreak\hskip 0pt plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`.\spacefactor=2000}
\def \cirth@comma{\ifnum\spacefactor<2000\nobreak\hskip 0pt plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`,\spacefactor=2000}
\def \cirth@colon{\ifnum\spacefactor<2000\nobreak\hskip 0pt plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`:\spacefactor=2000}
\def\cirth@scolon{\ifnum\spacefactor<2000\nobreak\hskip 0pt plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`;\spacefactor=2000}
\def \cirth@quest{\ifnum\spacefactor<2000\nobreak\hskip 0pt plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`?\spacefactor=2000}
\def\cirth@exclam{\ifnum\spacefactor<2000\nobreak\hskip 0pt plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`!\spacefactor=2000}

\def  \cirth@left{\ifnum\spacefactor<2000\hskip 0pt plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\else\hskip\fontdimen7\the\font\fi\char`(\spacefactor=2000}
\def \cirth@right{\ifnum\spacefactor<2000\nobreak\hskip 0pt plus \fontdimen3\the\font minus 1.5\fontdimen4\the\font\else\hskip\fontdimen7\the\font\fi\char`)\spacefactor=2000}

\def\sarat@space{\cleaders\hbox to 10sp{\char32\hss}\hskip10sp\ifnum\spacefactor>1999\hskip\fontdimen7\the\font\fi
	\hskip\fontdimen2\the\font plus\fontdimen3\the\font minus\fontdimen4\the\font\relax}

\def\sarat@period{\ifnum\spacefactor<2000\rlap{\char32}\nobreak\hskip 0pt plus \fontdimen3\the\font minus \fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`.\spacefactor=2000}
\def \sarat@comma{\ifnum\spacefactor<2000\rlap{\char32}\nobreak\hskip 0pt plus \fontdimen3\the\font minus \fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`,\spacefactor=2000}
\def \sarat@colon{\ifnum\spacefactor<2000\rlap{\char32}\nobreak\hskip 0pt plus \fontdimen3\the\font minus \fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`:\spacefactor=2000}
\def\sarat@scolon{\ifnum\spacefactor<2000\rlap{\char32}\nobreak\hskip 0pt plus \fontdimen3\the\font minus \fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`;\spacefactor=2000}
\def \sarat@quest{\ifnum\spacefactor<2000\rlap{\char32}\nobreak\hskip 0pt plus \fontdimen3\the\font minus \fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`?\spacefactor=2000}
\def\sarat@exclam{\ifnum\spacefactor<2000\rlap{\char32}\nobreak\hskip 0pt plus \fontdimen3\the\font minus \fontdimen4\the\font\else\kern\cirth@prepunct\fi\char`!\spacefactor=2000}


\def\cirth@latinspace{\ifnum\spacefactor<2000 
	\hskip 0.6\fontdimen2\the\font plus\fontdimen3\the\font minus\fontdimen4\the\font%
	\setbox0=\hbox{\char0}\cleaders\copy0\hskip\wd0%
	\hskip 0.6\fontdimen2\the\font plus\fontdimen3\the\font minus\fontdimen4\the\font%
	\else
	\hskip\fontdimen7\the\font\hskip\fontdimen2\the\font plus 2\fontdimen3\the\font minus 0.5\fontdimen4\the\font
	\fi\relax}

\newif\ifcirth@type \cirth@typefalse
{\obeyspaces\global\let\savespace= %
\gdef {\ifcirth@type\cirth@space\else\savespace\fi}}%
{\catcode`\.=\active\catcode`\,=\active\catcode`\:=\active\catcode`\;=\active\catcode`\?=\active\catcode`\!=\active\catcode`\(=\active\catcode`\)=\active%
 \global\let\saveperiod=.\global\let\savecomma=,\global\let\savecolon=:\global\let\savescolon=;\global\let\savequest=?\global\let\saveexclam=!\global\let\saveleftparen=(\global\let\saverightparen=)%
\gdef.{\ifcirth@type\cirth@period\else\saveperiod\fi}%
\gdef,{\ifcirth@type\cirth@comma\else\savecomma\fi}%
\gdef:{\ifcirth@type\cirth@colon\else\savecolon\fi}%
\gdef;{\ifcirth@type\cirth@scolon\else\savescolon\fi}%
\gdef?{\ifcirth@type\cirth@quest\else\savequest\fi}%
\gdef!{\ifcirth@type\cirth@exclam\else\saveexclam\fi}%
\gdef({\ifcirth@type\cirth@left\else\saveleftparen\fi}%
\gdef){\ifcirth@type\cirth@right\else\saverightparen\fi}%
}

\newenvironment{barsarati}{%
\fontencoding{T1}\fontfamily{saratibqq}\fontshape{n}\selectfont\saratiabbrevs%
\cirth@typetrue%
\ifdim\fontdimen7\the\font < 0pt
\let\cirth@period=\sarat@period%
\let\cirth@comma=\sarat@comma%
\let\cirth@colon=\sarat@colon%
\let\cirth@scolon=\sarat@scolon%
\let\cirth@quest=\sarat@quest%
\let\cirth@exclam=\sarat@exclam%
\catcode`\!=\active\catcode`\.=\active\catcode`\,=\active\relax%
\catcode`\:=\active\catcode`\;=\active\catcode`\?=\active\relax%
\fi
\let\cirth@space=\sarat@space%
\catcode`\ =\active\relax%
\saratiabbrevs\leavevmode\spacefactor=2000
}{}
\newenvironment{barsaratia}{%
\fontencoding{T1}\fontfamily{saratibqa}\fontshape{n}\selectfont\saratiabbrevs%
\cirth@typetrue%
\ifdim\fontdimen7\the\font < 0pt
\let\cirth@period=\sarat@period%
\let\cirth@comma=\sarat@comma%
\let\cirth@colon=\sarat@colon%
\let\cirth@scolon=\sarat@scolon%
\let\cirth@quest=\sarat@quest%
\let\cirth@exclam=\sarat@exclam%
\catcode`\!=\active\catcode`\.=\active\catcode`\,=\active\relax%
\catcode`\:=\active\catcode`\;=\active\catcode`\?=\active\relax%
\fi
\let\cirth@space=\sarat@space%
\catcode`\ =\active\relax%
\saratiabbrevs\leavevmode\spacefactor=2000
}{}

\def\cirthfancysetup#1{%
\fontencoding{T1}\fontfamily{#1}\fontshape{n}\selectfont%
\cirth@typetrue%
\ifdim\fontdimen7\the\font < 0pt
\let\cirth@space=\cirth@dotspace%
\cirth@prepunct=-\fontdimen2\the\font
\addtolength{\cirth@prepunct}{-\fontdimen7\the\font}
\sfcode`,=2000\relax\sfcode`;=2000\relax%
\catcode`\!=\active\catcode`\.=\active\catcode`\,=\active\relax%
\catcode`\:=\active\catcode`\;=\active\catcode`\?=\active\relax%
\else
\let\cirth@space=\cirth@latinspace%
\fi
\catcode`\ =\active\relax%
\cirthabbrevs
\leavevmode\spacefactor=2000
}


\newenvironment{fancydaeron}{\cirthfancysetup{\daeronfamily}}{}
\newenvironment{fancyhobbit}{\cirthfancysetup{\hobbitfamily}}{}
\newenvironment{fancyerebor}{\cirthfancysetup{\ereborfamily}}{}
\newenvironment{fancyeregion}{\cirthfancysetup{\eregionfamily}}{}
\newenvironment{fancymoria}{\cirthfancysetup{\moriafamily}}{}