# Can one set a specific character variant in xelatex for the word first letter only?

I'd like to have some code to set, document wide, a specific character variant for the letter "v" only if it is the first in the word. The solution in the MWE is not a solution because I have a document of several hundreds of pages. Any suggestion? Thanks.

\documentclass[a4paper,14pt,openright,twoside]{memoir}
\usepackage[T1]{fontenc}
\usepackage[scale=1,osf,nf,lf]{ebgaramond}
\usepackage{fontspec}
\usepackage{microtype}
\usepackage{csquotes}
\usepackage{realscripts}

\begin{document}

Normal set:
{\fontshape{it}
\selectfont

Ha visto un valvassore vivere come un valvassino invidioso che gridava evviva!}

Nice to have:
{\fontshape{it}
\selectfont

\end{document}

• This should better be a stylistic variant for the font. Is this only for italic? – egreg Apr 22 '15 at 6:54
• – Mico Apr 22 '15 at 8:20

You may use a character class:

\documentclass[a4paper,14pt,openright,twoside]{memoir}
\usepackage{fontspec}

\setmainfont{EB Garamond}[
Numbers=Lining,
ItalicFeatures={
RawFeature=+hlig,
RawFeature=+liga,
RawFeature=+dlig,
RawFeature=+swsh,
Numbers=OldStyle,
},
]
\newfontfamily\varv{EB Garamond}[
Numbers=Lining,
ItalicFeatures={
RawFeature=+hlig,
RawFeature=+liga,
RawFeature=+dlig,
RawFeature=+swsh,
Numbers=OldStyle,
CharacterVariant=5:2,
},
]

\XeTeXinterchartokenstate=1
\newXeTeXintercharclass\vclass
\XeTeXcharclassv=\vclass
\XeTeXinterchartoks 255 \vclass = {\openswashv}

\makeatletter
\newcommand\openswashv{%
% by preceding the v with U+200C it's not at the boundary any more
\ifnum\strcmp{\f@shape}{it}=\z@
\begingroup\varv^^^^200cv\endgroup
\nobreak\hspace{0pt}% allow hyphenation in the rest of the word
\expandafter\@gobble
\fi
}
\makeatother

\begin{document}

Normal font:
Ha visto un valvassore vivere come un valvassino invidioso che gridava evviva!

Italic font: {\itshape
Ha visto un valvassore vivere come un valvassino invidioso che gridava evviva!
}

\end{document}


This allows hyphenation on the rest of the word (but could give some wrong hyphenation point with languages different from Italian). I checked and EB Garamond doesn't define any kerning pair between v` and another letter.

You should ask the font developer for a stylistic set.