views:

687

answers:

1

Hello everyone,

I'm trying to render a bit of text using Core Graphics APIs and I'm running into some conceptual difficulties.

I'm trying to specify font size using CGContextSetFontSize. The size parameter is in something called "text space units". What is that? How does it map to "em" units?

Thanks

+2  A: 

I'm trying to specify font size using CGContextSetFontSize. The size parameter is in something called "text space units". What is that? How does it map to "em" units?

To answer your second question first, 1 em (following the CSS definition) will equal the amount you specify there.

Core Graphics' “text space” is derived from user space. There's an additional transformation matrix, the text matrix, which transforms user space into text space. See CGContextSetTextMatrix, How Quartz 2D Draws Text, and Drawing Text.

By default, the text matrix is the identity matrix, so text space = user space.

Peter Hosey
In other words, by default this is the point size of the font (ex: 12 pt) unless you change the transformation matrix.
Brad Larson
No. Font sizes in Quartz are in text space, which is *after* both transformation matrices are applied. 1 em = font size, no matter what.
Peter Hosey
Thanks Peter. Lets say the text matrix = identity matrix (ie no transformations). Would 1em = 1pixel?
EightyEight
There are no pixels. Welcome to the resolution-independent world of PostScript and its descendants, including PDF.
Peter Hosey
More to the point: The CTM can change the aspect ratio or size of the point, but there's no changing the iron law that 1 em = the number of points you set as the font size. And you generally shouldn't worry about the CTM when drawing, anyway—that's usually the point of changing the CTM.
Peter Hosey
Fair enough, then what units is the glyph advance width in? I request font bounding box width and it's approx. 2,000 units. That's clearly not pixels.
EightyEight
Maybe you're using a font that has a huge glyph in it (and “huge” doesn't need to be 2000 points—100 points would be enough for a font at 20-pt size). At any rate, advance widths are not constant in all glyphs in most fonts, so try advancementForGlyph: instead.
Peter Hosey