How do I add an emoji character using C++ API?

Question:

I am using ElementBuilder and want to add an emoji to the page.

For instance I want to add U+1F608 Smiling Face With Horns.

Answer:

The following code will work on Windows, though should also work on other platforms as long as there is a font registered with the OS that contains the desired character.

Note, the UTF8 encoding of U+1F608 is 0xF0 0x9F 0x98 0x88

char utf8_emoji[] = { 0xF0, 0x9F, 0x98, 0x88 };
UString u_emoji(utf8_emoji, 4);
Font fnt = Font::Create(doc, “Segoe UI Emoji”, u_emoji);
element = eb.CreateTextBegin(fnt, 12);
writer.WriteElement(element);
writer.WriteElement(eb.CreateUnicodeTextRun(u_emoji.GetBuffer(), u_emoji.GetLength()));
element.SetTextMatrix(10, 0, 0, 10, 0, 600);
writer.WriteElement(element);
writer.WriteElement(eb.CreateTextEnd());