# Substitute one glyph for an alternate glyph of another stylistic set

In LuaTeX, I want to do the following:

Having a text in the standard stylistic set of the font, but for a specific letter instead of having the glyph of the standard stylistic set, having it substituted by the glyph of an alternate stylistic set.

Example: in Arno Pro the Q of the first alternate stylistic set (Alternate=0) features a longer tail than the standard form. I would like that the compiler uses automatically the alternate Q glyph (and only the alternate Q glyph) for every occurence in my document.

Edit: Contextuals=Alternate or CharacterVariant={1-99} don't do the trick.

• @Thérèse Won't that activate everything enabled by the alternates feature of the font? That is, not only the Q? Although I don't know if the ligatures could work with just the alternate Q. – cfr Aug 23 '15 at 19:17
• It does indeed have contextual alternates, according to the PDF booklet about the font on Adobe’s web site, and according to the technical specifications given by myfonts.com. Maybe a minimal document showing how you’re invoking the font would help. – Thérèse Aug 23 '15 at 22:23
• I have no idea what your last comment is supposed to mean. – Thérèse Aug 23 '15 at 22:27
• Then the answer is that there’s no general answer. You need to examine each font to learn how to get the best results for your purposes. That’s especially the case because different designers have different ideas about what calt and other features mean. – Thérèse Aug 23 '15 at 22:30
• Please do not use non-freely available fonts in your code, especially not when the font is just a randomly picked example. It prevents the rest of us who don't have the font to run your code and help you. – Sverre Aug 23 '15 at 22:45

## 2 Answers

There is no general answer to this question, nor could there be. It’s not just that some designers misuse calt and other features, or disagree in their interpretation of the specification. Even if they all used features as they were meant to be used, fonts would still exhibit different behaviors.

Imagine that everyone agreed to use ss01 for Q, and ss02 for the ampersand. Now count the number of characters in human languages. If you still have time left, add the consideration that one font may have three alternate ampersands, two variations on Q, ornaments, etc. Clearly, there can be no universal agreement that ss03 controls this or that, because the possibilities are too numerous.

I’ve exaggerated the problem a little, because it would be odd to have separate stylistic sets for A, A with an acute accent, A with a grave accent, etc. And it may make sense to group alternates for different base characters in a single stylistic set; for example, a sans serif font may have all its humanist alternates in one set, and its geometric alternates in another. But the number of stylistic sets possible is limited, and it’s up to the designer to figure out the most useful way to arrange the stylistic alternates in a particular font into no more than twenty sets. A standard that made everything predictable to the user would hem in the unpredictable creativity of designers and fail to reflect the richness of human language and of its representation in type.

So when you acquire a new font, you need to study it and take notes on the features that interest you. I keep an Emacs org-mode file with tables of features and sample invocations for each of my fonts; however you prefer to keep your notes, you’ll waste a lot of time without notes.

If, upon examining a font, you discover that the designer has not provided a stylistic set affecting the one and only one character you have an interest in, then you can create a feature file to get what you want.

• It looks like you didn't exactly understood the question. I tried to reformulate it. – Kyle_the_hacker Aug 24 '15 at 1:36
• @Kyle_the_hacker Does my final paragraph address your reformulated question? If not, then I suspect that Arno Pro is your real concern and not just an example. In that case, you’ll have to hope that someone who owns the font demonstrates its use with fontspec. – Thérèse Aug 24 '15 at 1:47
• This problem is what the Character Variants feature was intended to solve (cv01-cv99) but the problem as always is font support. The use of a feature file as mentioned is the best solution. – Will Robertson Aug 24 '15 at 7:17
• Thanks, @WillRobertson I’ve noticed that none of many commercial fonts I’ve licensed defines Character Variants, whereas Brill, Charis SIL, EB Garamond, Gentium Plus, Pecita, and Unifracktur do have them. Perhaps many designers assume that licensees will use software which doesn’t support Character Variants well? They aren’t even listed among the registered features at partners.adobe.com/public/developer/opentype/index_tag3.html. – Thérèse Aug 24 '15 at 9:39
• @Thérèse — yeah, my understanding (I could be totally wrong) is that character variants were a later addition to the OpenType spec, and so the feature didn't make it into the standard GUI apps, and early fonts didn't use them, so the feature hasn't yet hit critical mass. Shame, because it's more useful… – Will Robertson Aug 24 '15 at 9:41

Here's how to do it (with the help of answers 1 and 2):

1. Obtain Adobe's AFDKO font tools and install them.

2. Put the font file into an empty directory.

3. Run tx -pdf -1 YOUR_FONT.otf > YOUR_FONT.pdf to map the glyphs of font.

4. Open YOUR_FONT.pdf, search for the wanted glyph and its glyph tag (the upper left number, we will name it GLYPH_TAG).

5. Create a dummy TeX file, set the with the following code:

\documentclass{article}
\usepackage{fontspec,luacode}
\setmainfont{YOUR_FONT}
\begin{luacode}
function luatexglyph(glyph)
local f  = fonts.hashes.identifiers[font.current()]
local ff = fontloader.open(f.filename)
local g  = ff.glyphs[glyph]
local n  = luaotfload.aux.slot_of_name(font.current(),g.name)
if n then
tex.sprint(n);
else
tex.error('font has no glyph '.. glyph)
end
fontloader.close(ff)
end
\end{luacode}
\def\LuaTeXglyph#1{\directlua{luatexglyph "#1"}}
\begin{document}
\LuaTeXglyph{GLYPH_TAG}
\end{document}

6. Look at the output, the number displayed is the caracter number in the LuaOTF table (we will name it CHAR_NUMBER).

7. In the document where you want the substitution to happen, add the following lines:

\usepackage{luacode,luatexbase}
\begin{luacode}
function dosub(s)
s = string.gsub(s, 'CHARACTER_YOU_WANT_TO_SUBSTITUTE', '\\charCHAR_NUMBER')
return(s)
end
\end{luacode}
\AtBeginDocument{%
\luaexec{luatexbase.add_to_callback("process_input_buffer", dosub, "dosub")}%
}

8. You're done.

Example: I want to substitute the standard Q of Arno Pro with the one of from the alternate stylistic set. Alternate Q has GLYPH_TAG 247. This glyph correspond to CHAR_NUMBER 983047 in the LuaOTF table. I just have to add the following to the beggining of my document:

\usepackage{luacode,luatexbase}
\begin{luacode}
function dosub(s)
s = string.gsub(s, 'Q', '\\char983047')
return(s)
end
\end{luacode}
\AtBeginDocument{%
\luaexec{luatexbase.add_to_callback("process_input_buffer", dosub, "dosub")}%
}