Calculating Glyph Layout

The Cocoa text system handles many complex aspects of laying out glyphs. If you need to calculate layout for your own purposes, you can use methods defined by NSFont. There are three basic kinds of glyph layout, although Java supports only overstruck:

Sequential glyph layout

Sequential glyph layout is supported by the method positionOfGlyph:precededByGlyph:isNominal:, which is available only in Objective-C. This method calculates the position of a glyph relative to glyph preceding it, using the glyph’s width and kerning information if they’re available. This is the most straightforward kind of glyph layout.

Overstruck glyph layout

Overstruck glyph layout is the most complex, as it requires detailed information about placement of many kinds of modifying marks. Generally, you have two characters:

Cocoa gives you methods for combining the two characters, depending on whether the combination is a common one that the font has metrics for or whether the combination is an unusual one that you need to create on the fly. Try these methods in the following order, to get the best result:

In Objective-C, if you need to place several nonspacing marks with respect to a base glyph, use the method positionsForCompositeSequence:numberOfGlyphs:pointArray:. This method accepts a C array containing the base glyph followed by all of its nonspacing marks and calculates the positions for as many as of the marks as it can. To place the marks that this method can’t handle, use the methods described above.

Stacked glyph layout

Stacked glyph layout is supported by the method positionOfGlyph:withRelation:toBaseGlyph:totalAdvancement:metricsExist:, which is available only in Objective-C. Stacked glyphs often have special compressed forms, which standard font metrics don’t account for. NSFont’s implementation of this method simply abuts the bounding rectangles of the two glyphs for approximate layout of the individual glyphs. Subclasses of NSFont can override this method to access any extra metrics information for more sophisticated layout of stacked glyphs.