[Tex/LaTex] Access a glyph on fontspec using a GID (Glyph ID)

fontsfontspecopentype

I'm trying to access a glyph, but it's only mentioned in the "Basic Latin and Latin 1" set for a font. The glyph has no name or unicode ID, but it does have a glyph ID or GID. fontspec doesn't mention possibilities for using this glyph. Neither does Adobe in the adobe feature file syntax: http://www.adobe.com/devnet/opentype/afdko/topic_feature_file_syntax.html#2.f

I'd still like to be able to access a glyph through its ID, since it's the only way I can access it (I think).

My specific case requires to access the glyph ID 554 from Stevens Titling Pro Sable Brush. I will only post this font if it's strictly necessary, since I don't like to distribute these fonts to the wide world.

I posted an MWE containing another (free) font. This is not the font I intend to use, but perhaps a starting point to try and access glyphs manually.

\documentclass{article}

\usepackage{fontspec}
\setmainfont{EB Garamond}

\parindent=0pt
\begin{document}

Some text int EB Garamond. How can I access glyph 123 now?

\end{document}

Best Answer

The method for accessing a specific glyph using fontspec depends on the engine used.

With XeTeX, an implementation is \XeTeXglyph554\relax (Thanks egreg).

An example document, using the inaccessible glyph with ID 554 from Stevens Titling Pro, Sable Brush, is shown below:

http://i.imgur.com/ALUoPcg.png?1

\documentclass{article}
\usepackage{fontspec}
\setmainfont{StevensTitlPro-SableBrush.otf}
\begin{document}
\XeTeXglyph554\relax
\end{document}

In LuaTeX, there appears to be no pre-defined high level command to access a glyph by its (OpenType) glyph number. However, one can create a Lua-based function to provide this access method.

Some example implementation for accessing the same glyph:

http://i.imgur.com/ALUoPcg.png?1

\documentclass{article}
\usepackage{fontspec}
\usepackage{luacode}  % provides 'luacode'  environment
\setmainfont{StevensTitlPro-SableBrush.otf}

\begin{luacode}
function LuaTeXglyph(charNo)
  local fontNo=font.current()
  local f=font.getfont(fontNo)
  local i
  local v
  local found=false
  for i,v in pairs(f.characters) do
   if v.index == charNo
   then
      tex.print( '\\char '..i..' ' )
      found=true
      break
    end
  end
  if not found
  then
    tex.error( 'font has no glyph '..charNo )
  end
end
\end{luacode}

\newcommand*{\LuaTeXglyph}[1]{%
  \directlua{LuaTeXglyph(#1)}
}

\begin{document}
\LuaTeXglyph{554}
\end{document}