[Tex/LaTex] Defining new font family with fontspec

fontspecxetex

I'm trying to reproduce historical examples of typography and there's something I don't know how to obtain: poems are typed in italic, except the initial capital letters of each verse is upshape, and there is some italic correction between the initial letter of the first word of the verse, and the following letter. Besides, all capital letters inside verses are upshape too.

I feel like I should define a new font family, but trying to adapt some code found on this site results in an error (!Extra \endgroup). I'd be quite grateful to anyone who could explain how to do it.

Best Answer

Adapting the code by Andrey Vihrov to your needs and to the changed number of character classes, I can offer

\documentclass{article}

\usepackage{fontspec}

\XeTeXinterchartokenstate=1
\newXeTeXintercharclass \uppercaseclass

\makeatletter
% Assign the new class to all Latin capital letters
\@tempcnta=`\A
\loop\unless\ifnum\@tempcnta>`\Z
  \XeTeXcharclass \@tempcnta \uppercaseclass
  \advance \@tempcnta by 1
\repeat
\makeatother

% Setup font change
\XeTeXinterchartoks 0 \uppercaseclass    = {\begingroup\upshape}
\XeTeXinterchartoks \uppercaseclass 0    = {\endgroup}
\XeTeXinterchartoks 4095 \uppercaseclass = {\begingroup\upshape}
\XeTeXinterchartoks \uppercaseclass 4095 = {\endgroup}

\begin{document}

\itshape
Once Upon a Time, there was a Beautiful Princess\ldots

\end{document}

enter image description here

Related Question