Using Variable Glyphs with fontspec

fontsfontspecluatexopentypexetex

I would like to access some alternative glyph forms using fontspec. To be specific I am using the font Garamond Premier Pro and I would like to use the alternative forms of Q (Glyph 245), W (Glyph 246) and & (Glyph 301). The font has 4 stylistic sets but using them through fontspec does not impact the shape of the letter mentioned.

Here is minimal code that still has the issue:

\documentclass{article}

\usepackage[no-math]{fontspec}
    \setmainfont[Ligatures=Common, StylisticSet={1,2,3,4}]{Garamond Premier Pro}
        
\title{Title}

\begin{document}
\maketitle
Test: WQ\&.
\end{document}

PS: Using Style= Alternate in fontspec changes everything not just the three letters (it also picks an alternative form of & that I do not want).

PPS: The issue is present both with XeLaTeX and LuaLaTeX.

Best Answer

I don’t know whether we have the same version of the fonts, because the glyph numbers you mention don’t match what I see. But in LuaTeX, you can do this (adjust the name of the ampersand glyph to ampersand.alt2 or ampersand.alt3 if the one shown is not the one you wanted):

\documentclass[12pt]{article}
\usepackage[no-math]{fontspec}
\directlua{
  fonts.handlers.otf.addfeature{
    name = "malt",
    type = "alternate",
    data =
    {
      Q = "Q.alt",
      ["q.sc"] = "q.scalt",
      W = "W.alt",
      ["w.sc"] = "w.scalt",
      Wcircumflex = "Wcircumflex.alt",
      Wgrave = "Wgrave.alt",
      Wacute = "Wacute.alt",
      Wdieresis = "Wdieresis.alt",
      ["wacute.sc"] = "wacute.scalt",
      ["wcircumflex.sc"] = "wcircumflex.scalt",
      ["wdieresis.sc"] = "wdieresis.scalt",
      ["wgrave.sc"] = "wgrave.scalt",
      ampersand = "ampersand.alt1",
    },
  }
  fonts.handlers.otf.addfeature{
    name = "oops",
    type = "alternate",
    data =
    {
      ["ampersand.alt1"] = "ampersand.sc",
    },
  }
}
\setmainfont{Garamond Premier Pro}[RawFeature=+malt]% “malt” for “my alternates”; choose another name if you like
\begin{document}
Test: W Q \&.

Ẃ Ẁ Ŵ Ẅ

% because the font doesn’t have small cap ampersand variants:
{\addfontfeature{RawFeature=+oops}\textsc{Test: w q \& ẃ ẁ ŵ ẅ}}
\end{document}

output

If you’d rather not need to remember to add {\addfontfeature{RawFeature=+oops}...} whenever a small caps ampersand comes into view, just invoke fontspec like this in your preamble:

\setmainfont{Garamond Premier Pro}[
  RawFeature=+malt,
  SmallCapsFeatures={RawFeature=+oops}]

See How to adjust font features in LuaTeX? for more detail. For glyphs with long tails, defining a contextual alternate is often preferable.