[Tex/LaTex] Can one set a specific font variant in xelatex for uppercase letters only

capitalizationfontsfontspecxetex

I would like to typeset a short, letter-like document using the Zapfino font, with the following twist: I'm OK using Zapfino's "standard" (i.e., Variant 0) glyphs for lowercase letters, but I'd like to use the Variant 1 glyphs for uppercase letters. (In case you're curious, they look more "swash-y" than the Variant 0 uppercase glyphs…) To use a concrete example, I'd like to get

enter image description here

instead of

enter image description here

Note the different appearance of the uppercase letters. As of now, I can only achieve this look using the brute-force approach, viz., by defining the command \newcommand{\varone}[1]{{\fontspec[Variant=1]{Zapfino}#1}} and then encasing each uppercase letter X inside \varone{X}.

I've perused the fontspec manual and found the \newfontfeature command on p. 55, but I can't seem to be able to figure out how to apply this method to my objective, which is to set the font variant separately for uppercase and lowercase letters. Anyone have an idea?

In case it matters, I'm running MacTeX2011 under MacOSX 10.6.8. The Zapfino font I have is the one that comes installed with the OS.

Answer, based on a minimally changed version of @AndreyVihrov's solution below:

    % !TEX TS-program = xelatex
% !TEX encoding = UTF-8
\documentclass[letterpaper]{article}

\usepackage{fontspec}
\setmainfont{Zapfino}

% Prepare a separate "font" for Latin uppercase letters
\newfontfamily\upfont[Ligatures=TeX,Variant=1]{Zapfino}

\XeTeXinterchartokenstate=1
\newXeTeXintercharclass \uppercaseclass

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

% Implement the font change
\XeTeXinterchartoks 0 \uppercaseclass   = {\begingroup\upfont}
\XeTeXinterchartoks \uppercaseclass 0   = {\endgroup}
\XeTeXinterchartoks 255 \uppercaseclass = {\begingroup\upfont}
\XeTeXinterchartoks \uppercaseclass 255 = {\endgroup}

\begin{document}
Once Upon a Time, Prince Charming saw \ldots
\end{document}

enter image description here

Best Answer

You can make use of the XeTeX character class mechanism, which allows to insert tokens into the input stream based on user-defined character classes. In this case, we define a new class for uppercase letters and prepare code to be inserted before and after these letters:

\documentclass{article}

\usepackage{fontspec}

% Our font for uppercase letters
\newfontfamily\upfont[Ligatures=TeX,Color=FF0000]{Latin Modern Roman}

\XeTeXinterchartokenstate=1
\newXeTeXintercharclass \uppercaseclass

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

% Setup font change
\XeTeXinterchartoks 0 \uppercaseclass   = {\begingroup\upfont}
\XeTeXinterchartoks \uppercaseclass 0   = {\endgroup}
\XeTeXinterchartoks 255 \uppercaseclass = {\begingroup\upfont}
\XeTeXinterchartoks \uppercaseclass 255 = {\endgroup}

\begin{document}

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

\end{document}

enter image description here

The mechanism is described in the XeTeX reference.

Related Question