user965972
user965972

Reputation: 2587

Does CGGlyph depend on the font size?

CGGlyph is a typedef for unsigned short. Every font can (or at least could) specify a different CGGlyph value for the same character. But within the same font, will the value of CGGlyph depend on the font size?

Basically I want to show the same character with different font sizes. Can I set the font size with CGContextSetFontSize() and reuse the CGGlyph value?

Upvotes: 2

Views: 484

Answers (1)

Jongware
Jongware

Reputation: 22447

CGGlyph holds the index of a glyph inside a font. This is because inside a font, the order of glyphs (individual character designs) can be anything -- in particular, unrelated to the character code it represents.

That is, the order of characters in fonts is unrelated to any encoding. The character code of an "A" is 65 (in ASCII) but that does not mean "the 65th character in a font is the glyph "A". For Unicode it's nearly impossible to make it work that way: the Unicode of a curly left double quote, for example, is U+201C, or 8220 in decimal.

So there are one or more tables inside each font that translate between the logical index of a glyph (simply counting characters as they appear in the file itself) and one or more encodings. (Where an encoding is a predefined set, such as "Unicode", "MacRoman", or "Windows Latin1".)

This has several advantages: first of all, the characters in a font file don't need to appear in any particular order. The font designer can add as many encodings to the file as needed. Also, it's perfectly possible for a font to contain glyphs that appear in no encoding at all (think of rare ligatures).

All said and done:

Every font can (or at least could) specify a different CGGlyph value for the same character.

Neither can nor should. Since CGGlyph is an index value, it simply always starts at 0 and the last valid valid is the number of characters in the font, minus 1.

The index of a glyph is not determined by its size, which is set at a very different level. So indeed you can re-use the value for different sizes.
But, noteworthy: the index of a particular character is uniquely tied to one font only. The ordering of individual characters inside each font should be considered unique (if only because one font may contain more or less charcaters than another).

See CGGlyph toUniChar for a related discussion.

Upvotes: 4

Related Questions