8

I am looking for a way to equire character's glyph descent as indicated on the picture:

enter image description here

The method needs to work for any given character (or at least all common unicode characters).

Here is my actual approach (in Swift, inspired by this and this question):

let char = "a"
let ctFont = CTFontCreateWithNameAndOptions("HelveticaNeue", 12, nil, nil)
var ctGlyph = CTFontGetGlyphWithName(ctFont, char)
let boundingBox = withUnsafePointer(&ctGlyph) { pointer -> CGRect in
    return CTFontGetBoundingRectsForGlyphs(ctFont, CTFontOrientation.OrientationDefault, pointer, nil, 1)
}

Descent I need is then simply equal to -boundingBox.origin.y. This approach works nicely for letter and number and number characters (see this answer for graphical representation).

The problem is that for everything other then letters or numbers (for example: .,#')*) it I get the same bounding rectangle: {x:0.612, y:0.012, w:4.908, h:8.532}. That is obviously incorrect.

How can I get the bonding rectangle of the descent directly for all characters?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Rasto
  • 17,204
  • 47
  • 154
  • 245
  • Code samples are very much prefered in Swift. Thank you – Rasto Nov 20 '14 at 20:23
  • Check the return argument of `CTFontGetGlyphWithName()` for `.notdef` as mentioned in the documentation: "if the glyph name is not recognized, the `.notdef` glyph [is returned]" – David Rönnqvist Nov 20 '14 at 22:22
  • @DavidRönnqvist You are right that is the bug. Make it an answer and I will accept it. – Rasto Nov 20 '14 at 22:47

0 Answers0