# Access a glyph on fontspec using a GID (Glyph ID)

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}

• What's the ID of the glyph? – egreg Nov 10 '14 at 23:58
• Surely "Basic Latin and Latin 1" is entirely covered by unicode? Can something be in these ranges and not have a unicode ID? – cfr Nov 11 '14 at 0:24
• @egreg The glyph is not from EB Garamond. This probably led to confusion. The glyph ID is 554 (as taken from Adobe InDesign), but neither FontForge nor FontExplorer Pro can confirm this (they both show an undefined glyph for this ID). The font furthermore isn't made by Adobe which may or may not contribute to this situation. – 1010011010 Nov 11 '14 at 8:51
• And how are we supposed to help if you hide all information? If you use XeLaTeX, then \XeTeXglyph554\relax could be the answer. I'm downvoting this question, hoping it will be improved. – egreg Nov 11 '14 at 8:56
• @cfr This may clarify your questions: i.imgur.com/R7ysuPZ.png – 1010011010 Nov 11 '14 at 8:58

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:

\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:

\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
then
tex.error( 'font has no glyph '..charNo )
end
end
\end{luacode}

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

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

• Very nice! This is a really useful posting, as I've noticed that there are quite a few Opentype fonts and font families that feature glyphs that are neither UTF8-encoded nor feature an Opentype-style "handle". – Mico Nov 11 '14 at 9:42
• I've taken the liberty of editing your write-up on the Lua-side of things. I hope you don't mind. – Mico Nov 11 '14 at 9:42

This is just an alternative to the above luatex solution of looping over the font table. Instead, we may also use the information from the raw font (with fontloader), where the glyphs are still accessible on the top level, and translate the internal glyph name into a char code via luaotfload's function slot_of_name:

\documentclass{article}
\usepackage{fontspec,luacode}
\setmainfont{EB Garamond 12}
\begin{luacode}
function luatexglyph(glyph)
local f  = fonts.hashes.identifiers[font.current()]
local g  = ff.glyphs[glyph]
if n then
tex.sprint('\\char' ..n.. ' ');
else
tex.error('font has no glyph '.. glyph)
end
end
\end{luacode}
\def\LuaTeXglyph#1{\directlua{luatexglyph "#1"}}
\begin{document}
\LuaTeXglyph{1875}
\LuaTeXglyph{1895}
\LuaTeXglyph{1897}
\end{document}


• I really like your solution! Very minimalist. – Mico Nov 12 '14 at 6:44
• I had some trouble using this method in a real document (?) – 1010011010 Nov 12 '14 at 9:59
• @1010011010 what was the problem? – Robert Nov 13 '14 at 1:46
• This definition of \LuaTeXglyph needs an \ignorespaces in its definition, to function the same way as my solution. – 1010011010 Nov 13 '14 at 8:22
• @1010011010 Why would you want to ignore spaces? – Robert Nov 13 '14 at 16:28