title: how to convert a swift String to an array of CGGlyph
body: <p>This snippet can be used for drawing CGGlyphs with a CGContext.    </p>
  
  <pre><code>    //drawing
      let coreGraphicsFont = CTFontCopyGraphicsFont(coreTextFont, nil)
      CGContextSetFont(context, coreGraphicsFont);
      CGContextSetFontSize(context, CTFontGetSize(coreTextFont))
      CGContextSetFillColorWithColor(context, Color.blueColor().CGColor)          
      CGContextShowGlyphsAtPositions(context, glyphs, positions, length)
  </code></pre>
  
  <p>But how do I obtain the CGGlyphs from a swift string which contains emoji symbols like flags or accented characters?</p>
  
  <pre><code>let string = "swift: \u{1F496} \u{65}\u{301} \u{E9}\u{20DD} \u{1F1FA}\u{1F1F8}"
  </code></pre>
  
  <p>Neither of these approaches shows the special characters, even though they are correctly printed to the console. Note that this first approach returns NSGlyph but CGGlyph's are required for drawing.</p>
  
  <pre><code>    var progress = CGPointZero
      for character in string.characters
      {
          let glyph = font.glyphWithName(String(character))
          glyphs.append(CGGlyph(glyph))
          let advancement = font.advancementForGlyph(glyph)
          positions.append(progress)
          progress.x += advancement.width
      }
  </code></pre>
  
  <p>or this second approach which requires casting to NSString:</p>
  
  <pre><code>    var buffer = Array&lt;unichar&gt;(count: length, repeatedValue: 0)
      let range = NSRange(location: 0, length: length)
      (string as NSString).getCharacters(&amp;buffer, range: range)
  
      glyphs = Array&lt;CGGlyph&gt;(count: length, repeatedValue: 0)
      CTFontGetGlyphsForCharacters(coreTextFont, &amp;buffer, &amp;glyphs, length)
  
      //glyph positions
      advances = Array&lt;CGSize&gt;(count: length, repeatedValue: CGSize.zero)
      CTFontGetAdvancesForGlyphs(ctFont, CTFontOrientation.Default, glyphs, &amp;advances, length)
      positions = []
      var progress = CGPointZero
      for advance in advances
      {
          positions.append(progress)
          progress.x += advance.width
      }
  </code></pre>
  
  <p>Some of the characters are drawn as empty boxes with either approach. Kinda stuck here, hoping you can help.</p>
  
  <p><strong>edit:</strong></p>
  
  <p>Using CTFontDrawGlyphs renders the glyphs correctly, but setting the font, size and text matrix directly before calling CGContextShowGlyphsAtPositions draws nothing. I find that rather odd.</p>