Building PDF text using glyph indices

Q: We work in our text engine with glyph indices. Putting out text to
PDF now we have to search the corresponding Unicode values. We do it
using a glyph index - Unicode map. We build this map walking through
Unicode ranges of font.

The problem is that glyphs of some ligatures in some fonts does not
have Unicode value. Two example are the Cambria and Calibri font

Do you know another way to use glyph indices when building text
elements?
------------------------

A: We are planing to add 'glyph index' encoding option in the near
future however, you can also output glyph indices using the current
API using the following approach:

- Embed the font (e.g. TrueType) as CIDFont with Identity-H encoding
- Set its "CIDToGIDMap" to "Identity"
- Add text to the document as usual (using eb.CreateUnicodeTextRun)
but output GIDs instead of Unicode.
- Add the mapping between GIDs and corresponding advance widths (and
Unicode) and optionally ToUnicode cmap. This is required unless you
are using explicit glyph positioning (i.e. SetTextMatrix() on each
character).

The pseudo-code looks as follows:

Font font=Font::CreateCIDTrueTypeFont(doc, font, true, false,
"Identity-H"); font.GetDescendant().GetSDFObj().PutName("CIDToGIDMap",
"Identity");

map<UInt16, double> width_map;

Element* element = element_builder.CreateTextBegin(font, font_sz);
element_writer->WriteElement(element);

foreach (i in indices) {
  width_map[i]= font.GetGIDHorzAdvance(i);
  element=m_builder->CreateUnicodeTextRun(indices[i], 1);
  element_writer->WriteElement(e);
  // you can also write full text-runs in a single shot instead of
writing character by character.
}
element_builder.CreateTextEnd();

….

// Just before saving the document, set the font Widths entry.
  Obj width_arr = font.GetDescendant().GetSDFObj().PutArray("W");
  foreach (i in width_map) {
     width_arr.PushBackNumber(witr->first);
     width_arr.PushBackNumber(witr->first);
     width_arr.PushBackNumber(witr->second);
   }

At this point you could also optionally add ToUnicode CMap (that maps
GIDs to Unicode) to allow for copy & paste in PDF viewer.