# Glyph alternates break hyphenation

Alan Munn explined in Use stylistic set variations only for certain glyphs how to enable glyph alternates for a certain subset of glyphs.

I'm using the second method he described (the other method isn't viable for me) and it seems to break automatic hyphenation.

Consider this example:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage[paperwidth =   6 em,right=   1 em,left=    1 em,]{geometry}
\begin{document}
A a h coherence
\end{document}


Here coherence is correctly hyphenated.

Now the same with alernates switched on:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage[paperwidth =   6 em,right=   1 em,left=    1 em,]{geometry}
% Now the more complicated version:
\XeTeXinterchartokenstate=1 % enable character classes
\newXeTeXintercharclass\myalt % create a new class
\XeTeXcharclass \h \myalt % add h to the class
% between any character of class 0 and \myalt add the alternate feature
% between \myalt and any character end the group
\XeTeXinterchartoks  \myalt 0 = {\endgroup}
% between a word boundary and \myalt add the alternate feature
% between \myalt and the word boundary end the group
\XeTeXinterchartoks  \myalt 255 = {\endgroup}
\begin{document}
A a h coherence
\end{document}


Now hyphenation is broken.

Note that this is not an artifact of the specific example, I also tried it with a longer text.

Is this a bug?

EDIT: egreg explained that this is indeed not a bug.

Is a workaround possible? One obvious possibility is to mark hyphenation points explicitly (e.g. co-herence), but that is unpractical in a longer document.

-
It's not a bug: TeX and XeTeX don't try hyphenation on sequences of letters interrupted by font changes. Can't you apply the "alternation" to the whole word? –  egreg Mar 24 '12 at 15:45
Unfortunately that's not viable: I want to globally use alternates for a certain subset of glyphs. –  Emerson Mar 25 '12 at 12:17
So the answer is: it's not a bug. I believe that this restriction can be lifted with Lua(La)TeX, which unfortunately hasn't the "interchartoks" feature. –  egreg Mar 25 '12 at 12:22

It's not a bug. What you get when typesetting "coherence", after the interchartoks feature has acted, is equivalent to

co\begingroup\addfontfeature{Alternate=0}h\endgroup erence


and the problem is exactly here: XeTeX won't try hyphenation past a font change.

You may "solve" it with a nasty trick:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}

\makeatletter
\providecommand{\allowhyphens}{\nobreak\hskip\z@skip}
\makeatother
% Now the more complicated version:
\XeTeXinterchartokenstate=1 % enable character classes
\newXeTeXintercharclass\myalt % create a new class
\XeTeXcharclass \h \myalt % add h to the class
% between any character of class 0 and \myalt add the alternate feature
% between \myalt and any character end the group
\XeTeXinterchartoks  \myalt 0 = {\endgroup\allowhyphens}
% between a word boundary and \myalt add the alternate feature