title: How are various glyphs encoded inside a PDF content stream?
body: <p>I am working on a program that outputs PDF documents. Given a sequence of UTF-8 encoded characters and the name of a font that shall be used to render it, I would like to show the appropriate glyphs that make the actual content of the document. I would like to be able to display national characters such as <em>č</em> or <em>ö</em>. It would be great to support ligatures like <em>ae</em> or <em>ffi</em>.</p>
  
  <p>The problem is, I do not know how the actual glyphs to be shown are specified (inside a content stream, for example).</p>
  
  <p>If I, for example, want to display the string "Hello World", I need not to worry about encoding, I simply write <code>(Hello World)Tj</code>. The PDF reader will then use the appropriate font to render this string.</p>
  
  <p>But what if I wanted to show the string
  <strong>It is difficult to read the PDF specification all day. Prostě dočista nemožné!</strong>
  with the ligatures <em>ffi</em>, <em>fi</em> and <em>ea</em> and the Czech national symbols <em>ě</em>, <em>č</em> and <em>é</em> in a given font, how would I proceed?</p>
  
  <p>I am trying to get through the PDF specification, but it is not easy.</p>
  
  <ul>
  <li>How do I find out the "code of the glyph" that corresponds to a given character or ligature?</li>
  <li>How is this code <em>encoded</em> within a PDF content stream?</li>
  </ul>
  
  <p>Help is much appreciated.</p>
  
  <hr>
  
  <p><strong>Edit:</strong> I may have overestimated the problem. Counting the glyphs that are needed to display a "common European document", I cannot think of a way how this number could exceed 256. If my assumptions are correct, I can remap the encoding of the font completely. This should be sufficient to cover all common symbols of the latin alphabet, numbers, punctuation, common symbols like <code>(</code> and <code>[</code> and still I would have plenty of room for national symbols, ligatures and other elements of high-quality typography. (I can implement a priority queue to select the most used ligatures if the total number of glyphs shall exceed 256.)</p>
  
  <p>That being said, I do not think I need to use the CID-keyed fonts.</p>
  
  <p>Still I wander how do I map UTF-8 encoded characters onto glyphs of an arbitrary font. I have the AFM of the font available. For the <em>DejaVu</em> font, for example, character information go like this:</p>
  
  <pre><code>C 63 ; WX 536 ; N question ; B 67 -15 488 743 ;
  C 64 ; WX 1000 ; N at ; B 65 -174 930 705 ;
  C 65 ; WX 722 ; N A ; B -6 0 732 730 ;
  </code></pre>
  
  <p>But after the 256<sup>th</sup> character is mapped, the codes are <code>-1</code>:</p>
  
  <pre><code>C 255 ; WX 564 ; N ydieresis ; B -3 -223 563 767 ;
  C -1 ; WX 722 ; N Amacron ; B -6 0 732 899 ;
  C -1 ; WX 596 ; N amacron ; B 49 -15 568 746 ;
  </code></pre>
  
  <p>For example, if I had the sequence <code>11100010 10000010 10101100</code> (Euro sign) in my input, how would I know what glyph name it corresponds to so that I can map it in the <code>/Encoding</code> dictionary?</p>