# How to loop through a range of Unicode characters to find which one is represented by a given glyph

I'm using XeLaTeX and OpenType fonts.

I would like to use a glyph with a given name, that may be assigned to different Unicode characters in different fonts (or may not exist in a font, or may exist in the font but may not be assigned to any character).

XeTeX provides the \XeTeXglyphindex command that will tell me if the glyph exists in the font, and if yes, will return its index.

The \XeTeXcharglyph command takes a character slot as input an provides the index of the glyph representing it, if any.

What I need is code that will

1. check if the glyph exists through \XeTeXglyphindex and store the index in a variable
2. run a loop through a given (potentially long) range of Unicode characters, apply \XeTeXcharglyph to each one, and compare it with the memorized index
3. if the loop finds the corresponding character, supply it to \hyphenchar
4. if not, supply a different character slot to \hyphenchar.

Do you know where I can find such code (potentially using LaTeX's new expl3 package?

(2 hours later) On the demand of Ulrike, here is some code for a single Unicode slot and a single glyph name. In font Amiri-Regular there is a (very nice) glyph called uni0606 for Unicode character U+0606. Here is a minimal XeLaTeX file providing the glyph index out of the name and out of the character slot:

\documentclass{article}
\usepackage{fontspec}
\newfontfamily{\arabicfont}[Script=Arabic,Extension=.ttf,Scale=1.2]{Amiri-Regular}
\begin{document}

\arabicfont
The glyph of name uni0606 is \the\XeTeXglyphindex "uni0606"

The glyph of character uni0606 is \the\XeTeXcharglyph"0606

If they are the same, assign this to hyphenchar
\end{document}


I need a loop where the second operation (\the\XeTeXcharglyph"0606) is applied, e.g., to all slots between "0000 and "FFFE, until one of them provides a glyph index equal to the one of the first operation. Notice that when a slot is not the font, the command \the\XeTeXcharglyph takes values 0.

• Not sure if this is related: tex.stackexchange.com/questions/235772/… Apr 29 at 18:22
• It's about the same issue, but no solution is given. I'm just hoping an expl3 expert will say "this is trivial" and write down the code in a few seconds. I know expl3 has tremenduous potential, you just need to master its syntax. Apr 29 at 19:58
• well writing a loop in expl3 is trivial. But the main part of your question is not expl3 related, but about \XeTeX commands. If you would write down the code for a small range of unicode, someone can tell you how to extend it to a large range. Apr 29 at 20:21
• you could do it with expl3 or just a plain tex \loop but isn't the answer always going to be it's not there or it is in slot hex 606? surely no font designer is going to give a glyph name uni0606 to a character assigned a different unicode? Apr 29 at 22:17
• I know, this was only an example… The problem is with a glyph (Uyghur Hyphen) for which there is no Unicode slot but we need to supply it to \hyphenchar, so we have to assign it to some other slot, and find it among all glyphs/chars of the font. Apr 30 at 7:28

If I understand correctly this:

\documentclass{article}
\usepackage{fontspec}
\newfontfamily{\arabicfont}[Script=Arabic,Extension=.ttf,Scale=1.2]{Amiri-Regular}
\begin{document}

\arabicfont
The glyph of name uni0606 is \the\XeTeXglyphindex "uni0606"

The glyph of character uni0606 is \the\XeTeXcharglyph"0606

If they are the same, assign this to hyphenchar

\newcount\zz

\loop
\ifnum\XeTeXglyphindex "uni0606" =\XeTeXcharglyph\zz
you are looking for: Character: \the\zz\ \chardef\zzz\zz\meaning\zzz
\fi

• Thank you David, but how do you apply the slot to \hyphenchar? Apr 30 at 6:10
• \hyhenchar\font=\zz instead of you are looing for... however as I commented under the question for the case of a name such as uni.... I can't see the loop is ever needed. non-standard names may be mapped to unexpected slots but uni0606 will either not be there or will be at hex 0606. @yannis Apr 30 at 8:23
• OK I found it: it is just \hyphenchar\font\zzz and it works, thanks David!!! Apr 30 at 8:42
• As I said, uni0606 was just an example. I'm looking for the non-standard name uyghur-hyphen for which there is no Unicode character but we do need a Unicode slot for \hyphenchar. And I can even ask the Unicode Consortium to add an ARABIC SOFT HYPHEN character because they will that it is functionally the same as the standard SOFT HYPHEN character. It is (Xe)TeX's problem that \hyphenchar cannot use a glyph variant… Apr 30 at 8:45
• Strange: you say \hyphenchar\font\zz and I used \hyphenchar\font\zzz and it works nevertheless. Is that normal? Apr 30 at 8:55