7

I found coloremoji which allows inserting apple's color emoji's into an uptex document. This library first extracts all emoji's as PDFs named according to their hex unicode character codes (e.g. 1F36E.pdf). Then there is a stylesheet (coloremoji.sty) which seemingly looks up an incoming emoji's hex code and includes the corresponding PDF.

However, this seems to require some upTeX specific features. I'd like to get this working with pdflatex using \usepackage{stringenc},\usepackage{utf8},\usepackage{fontenc}, etc.

Is this possible to look up a raw unicode character's hex code using pdflatex (TeXLive on Mac OS X)?

Here's the imagined interface (as a screenshot to preserve emoji)enter image description here

The coloremoji command should expand to an includegraphics so that the resulting document is a cheeseburger.

1 Answers1

4

If you have already the converted PDF files, here's a version that works with all engines:

\documentclass{article}
\usepackage{graphicx,stringenc}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\tl_const:Nn \c_coloremoji_dir_tl { ./emoji_images/hires }
\tl_new:N \l_coloremoji_input_tl

\cs_new_protected:Npn \coloremoji_unicode:nn #1 #2
 {
  \includegraphics[#1]{\c_coloremoji_dir_tl / \int_to_Hex:n{`#2}.pdf}
 }

\cs_new_protected:Npn \coloremoji_eightbit:nn #1 #2
 {
  \StringEncodingConvert \l_coloremoji_input_tl
    { \tl_to_str:n { #2 } } { utf8 } { utf32 }
  \tl_set:Nx \l_coloremoji_input_tl { \pdfescapehex\expandafter{\l_coloremoji_input_tl} }
  \regex_replace_once:nnN { \A 0* } { } \l_coloremoji_input_tl
  \includegraphics[#1]{\c_coloremoji_dir_tl / \l_coloremoji_input_tl.pdf}
 }

\NewDocumentCommand{\coloremoji}{O{}m}
 {
  \bool_if:nTF { \xetex_if_engine_p: || \luatex_if_engine_p: }
   {
    \coloremoji_unicode:nn { #1 } { #2 }
   }
  {
   \coloremoji_eightbit:nn { #1 } { #2 }
  }
 }

\ExplSyntaxOff

\begin{document}

\coloremoji{}

\end{document}

Compile with pdflatex, XeLaTeX or LuaLaTeX. Change the meaning of \c_coloremoji_dir_tl to point to the directory where the PDF files are.


This is a pdftex only solution:

\documentclass{article}
\usepackage{stringenc,graphicx}

\makeatletter
\newcommand{\gethex}[1]{%
  \StringEncodingConvert{\gh@tmp}{\detokenize{#1}}{utf8}{utf32}%
  \edef\gh@tmp{\pdfescapehex\expandafter{\gh@tmp}}%
  \expandafter\get@hex\gh@tmp\get@hex
}
\def\get@hex#1{%
  \if0#1%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\get@hex}{\get@hex@aux#1}%
}
\def\get@hex@aux#1\get@hex{%
   \def\gh@string{#1}%
}
\newcommand{\coloremoji}[2][]{%
  \gethex{#2}%
  \includegraphics[#1]{./emoji_images/hires/\gh@string.pdf}%
}
\makeatother

\begin{document}

\coloremoji{}

\end{document}

A version without stringenc that works with all engines. Note that the argument to \coloremoji must be a single Unicode character. The \newunicodechar part is just to show compatibility. The optional argument to \coloremoji is passed to \includegraphics.

\documentclass{article}

%\usepackage[utf8]{inputenc} % not necessary, but recommended if pdflatex is used
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{newunicodechar}

\newunicodechar{}{\coloremoji{}}

\ExplSyntaxOn

\NewDocumentCommand{\coloremoji}{O{}m}
 {
  \coloremoji_unicode:nn { #1 } { #2 }
 }

%%% the location where the images are found
\tl_const:Nn \c_coloremoji_dir_tl { ./emoji_images/hires }

\cs_new_protected:Npn \coloremoji_unicode:nn #1 #2
 {
  \includegraphics[#1]{\c_coloremoji_dir_tl / \manual_get_hex:n { #2 }.pdf}
 }

\cs_new:Npn \manual_get_hex_unicode:n #1
 {
  \int_to_Hex:n { `#1 }
 }

\cs_new:Npn \manual_utfviii_to_unicode:n #1
 {
  \tl_if_blank:oTF { \tl_to_str:n { #1 } }
   { 20 }
   { \__manual_utfviii_to_unicode_aux:o { \tl_to_str:n { #1 } } }
 }

\cs_new:Npn \__manual_utfviii_to_unicode_aux:n #1
 {
  \int_case:nn { \tl_count:n { #1 } }
   {
    {1}{ \int_to_Hex:n { `#1 } }
    {2}{ \__manual_utfviii_twobyte:f { #1 } }
    {3}{ \__manual_utfviii_threebyte:f { #1 } }
    {4}{ \__manual_utfviii_fourbyte:f { #1 } }
   }
 }
\cs_generate_variant:Nn \__manual_utfviii_to_unicode_aux:n { o }

\cs_new:Npn \__manual_utfviii_twobyte:n #1
 {
  \__manual_utfviii_twobyte_aux:nn #1
 }
\cs_generate_variant:Nn \__manual_utfviii_twobyte:n { f }

\cs_new:Npn \__manual_utfviii_twobyte_aux:nn #1 #2
 {
  \int_to_Hex:n { (`#1-192)*64 + (`#2-128) }
 }

\cs_new:Npn \__manual_utfviii_threebyte:n #1
 {
  \__manual_utfviii_threebyte_aux:nnn #1
 }
\cs_generate_variant:Nn \__manual_utfviii_threebyte:n { f }

\cs_new:Npn \__manual_utfviii_threebyte_aux:nnn #1 #2 #3
 {
  \int_to_Hex:n { (`#1-224)*4096 + (`#2-128)*64 + (`#3-128) }
 }

\cs_new:Npn \__manual_utfviii_fourbyte:n #1
 {
  \__manual_utfviii_fourbyte_aux:nnnn #1
 }
\cs_generate_variant:Nn \__manual_utfviii_fourbyte:n { f }

\cs_new:Npn \__manual_utfviii_fourbyte_aux:nnnn #1 #2 #3 #4
 {
  \int_to_Hex:n { (`#1-240)*262144 + (`#2-128)*4096 + (`#3-128)*64 + (`#4-128) }
 }

\bool_if:nTF { \xetex_if_engine_p: || \luatex_if_engine_p: }
 {
  \cs_set_eq:NN \manual_get_hex:n \manual_get_hex_unicode:n
 }
 {
  \cs_set_eq:NN \manual_get_hex:n \manual_utfviii_to_unicode:n
 }

\ExplSyntaxOff

\begin{document}
\coloremoji{}

\coloremoji[width=1cm]{}


\end{document}

enter image description here

egreg
  • 1,121,712
  • Perfect. This is a now a working, workaround for the true problem of directly including the apple emoji font and displaying emojis as glyphs: http://tex.stackexchange.com/questions/88305/emoji-characters Any thoughts on that one? – Alec Jacobson Aug 05 '14 at 15:51
  • @mangledorf Use \coloremoji{...} – egreg Aug 05 '14 at 16:04
  • I understand that. But that's just including the PDF, rather than rendering the character using the apple color emoji font (relying on viewer support of course). – Alec Jacobson Aug 05 '14 at 16:13
  • @mangledorf I don't see how XeTeX or LuaTeX can support the font, as it works by embedding a PDF file. One could emulate it with newunicodechar by doing \newunicodechar{}{\coloremoji{}}, but this needs knowing the characters one wants to use. – egreg Aug 05 '14 at 16:24
  • ooh burger time! :) – Paulo Cereda Aug 06 '14 at 11:13