cocoacore-textglyph

Core Text glyph placement and em values


I am trying to draw some musical notes using the Bravura font from http://blog.steinberg.net/2013/05/introducing-bravura-music-font/ using core text functions.

A metadata json file accompanying this font has an "attach" coordinate in em unit fractions for where stems should be attached to certain musical notes. This attachment point is relative to a bounding box around the note glyph again with the dimensions in fractions of em unit.

My crude ASCII picture shows the attach point of the stem to the musical note:

      |
      |
      |
 /---\* <- attach point here
|     |
 \---/

From wikipedia: "An em is a unit of width in the field of typography, equal to the currently specified point size. For example, one em in a 16-point typeface is 16 points wide. Therefore, this unit is the same for all typefaces at a given point size."

I would like to use the Core Text function CTFontDrawGlyphs which takes a coordinate in pixels to use for the glyph placement.

I would also like to use the Core Graphics functions CGContextMoveToPoint, CGContextAddLineToPoint to draw the stems that attach to the musical note glyphs.

I'm a bit unclear how to go from a coordinate in em unit fractions to a particular coordinate in pixels for a glyph of a particular point size so that I can accurately place these stems to the musical note glyph.


Solution

  • In the absence of a text transformation matrix (CGContextSetTextMatrix), 1 em will the be the same number of points as the font size. So for a 12 point font with the default identity text matrix, 1 em = 12 pt.

    Note that almost nothing in Core Graphics is given in pixels. They're in points (Core Graphics points, not PostScript points). For a retina display, 1 point contains 4 pixels (2x2). This is probably what you mean anyway, but if you interact with images, the point/pixel conversion is very important.

    (I kind of feel like I've repeated what you already know; is there more to the question? From a given origin, you would compute the coordinate by multiplying the em unit offset by the font size.)