# Insert characters via CID in XeTeX

In LuaTeX, we can use \CID and \UTF in luatexja-otf to insert characters via their CIDs:

\documentclass{ctexart}
\usepackage{luatexja-otf}
\setCJKmainfont{KozMinPr6N-Regular.otf}

\begin{document}

\CID{7652}飾区の\CID{13706}野家，\CID{1481}城市，葛西駅，高崎と\CID{8705}\UTF{FA11}
\end{document}


They are inherited from upTeX. So how to use CID-key to insert some character in XeTeX?

For Kozuka Mincho Pr6N, the glyph order is the same as CID order (Adobe Japan1-6). But other fonts may not use the same order, so \XeTeXglyph etc cannot be (at least, directly) used here.

A proof of concept using the cidmap files in TeX Live.

\documentclass{article}
\usepackage{xparse,xeCJK}

\setCJKmainfont{Hiragino Mincho ProN}

\ExplSyntaxOn

{
}

\NewDocumentCommand{\CID}{m}
{
\use:c { CID#1 }
}
\NewDocumentCommand{\UTF}{m}
{
\symbol { \int_from_hex:n { #1 } }
}

{
{
}
}

{
}

\cs_new_protected:Npn \__readcid_line_aux:w #1 ~ #2 \q_stop
{
\str_if_eq:nnF { #1 } { #2 }
{
\str_case_e:nnF { \str_range:nnn { #2 } { 1 } { 3 } }
{
{/un}{ \__readcid_def_uni:nn { #1 } { #2 } }
{/Ja}{ \__readcid_def_japan:nn { #1 } { #2 } }
{./n}{ }
}
{ \__readcid_def:nn { #1 } { #2 } }
}
}

{
\str_if_in:nnTF { #1 } { . }
{
\__readcid_def:w #1 - { #2 }
}
{
\__readcid_def_do:nn { #1 } { #2 }
}
}
{
\int_step_inline:nnn { #1 } { #2 }
{
\__readcid_def_do:nn { ##1 } { #3 }
}
}
{
\cs_new:cpx {CID#1} { \UTF { #2 } }
}

{
\str_if_in:nnTF { #1 } { . }
{
\__readcid_def_uni_multi:w #1 - { #2 }
}
{
\__readcid_def_uni_do:nn { #1 } { #2 }
}
}
{
\int_step_inline:nnn { #1 } { #2 }
{
\__readcid_def_uni_do:nn { ##1 } { #3 }
}
}
{
\cs_new:cpx {CID#1} { \UTF { \str_range:nnn { #2 } { 5 } { 8 } } }
}

{
\str_if_in:nnTF { #1 } { . }
{
\__readcid_def_japan_multi:w #1 - { #2 }
}
{
\__readcid_def_japan_do:nn { #1 } { #2 }
}
}
{
\int_step_inline:nnn { #1 } { #2 }
{
\__readcid_def_japan_do:nn { ##1 } { #3 }
}
}
{
\cs_new:cpx {CID#1} { \CID { \__readcid_japan_strip:w #2 \q_stop } }
}
\cs_new:Npn \__readcid_japan_strip:w #1.#2.#3 \q_stop { #2 }

\ExplSyntaxOff

\begin{document}

\CID{7652}飾区の\CID{13706}野家，\CID{1481}城市，葛西駅，高崎と\CID{8705}\UTF{FA11}

\CID{7652}飾区の𠮷野家，葛城市，葛西駅，高崎と髙﨑

\end{document}


There is no CID 7652, as far as I can see in Adobe-Japan1-6.

Some explanations: the first in the .cidmap line contains the number of entries and we don't need it. The other lines have the form

<CID> <translation>


or

<CID>..<CID> <translation>


for multiple entries pointing to the same UTF character.

Many of the translations are just a Unicode code point in hexadecimal, others have the form

/uni<pqrs>.<other specs>


or

/Japan1.<CID>.<other specs>


so these macros strip /uni and the part from the period on. In the Unicode or /uni cases, I define \CID<CID> to do \UTF{<code>}. Otherwise \CID<CID> does \CID{<CID>}.

The macro \CID{<number>} expands to \CID<number> (define via the analog of \csname...\endcsname).

A full specification of the .cidmap files might improve the translation.

• The point is that several CIDs may map to a single Unicode code point. Here, cid1481 and cid7652 both map to U+845B 葛, but they are different glyphs (look different). That's why I need to use CID rather than UTF. – stone-zeng Apr 1 '19 at 8:29
• @stone-zeng I said it is a proof of concept. – egreg Apr 1 '19 at 10:43