Inherits from: NSObject
Package: com.apple.yellow.application
NSFont objects represent PostScript fonts to an application, providing access to characteristics of the font and assistance in laying out glyphs relative to one another. Font objects are also used to establish the current font when drawing in an NSView, using the set method.
You don't create Font objects using a constructor. Instead, you use either fontWithNameAndMatrix or fontWithNameAndSize to look up an available font and alter its size or matrix to your needs. These methods check for an existing font object with the specified characteristics, returning it if there is one. Otherwise, they look up the font data requested and create the appropriate object. NSFont also defines a number of methods for getting standard system fonts, such as systemFontOfSize, userFontOfSize, and messageFontOfSize.
In most cases you draw text using an NSTextView object. You can also draw an NSString directly in an NSView using the methods drawAtPoint:withAttributes: and drawInRect:withAttributes:, which the Application Kit adds to NSString. These methods take an NSDictionary of attributes, as used by the NSAttributedString class, and apply them when drawing the string.
If you need to draw text using PostScript operators such as show
,
it's recommended that you set the current font using NSFont's set method, rather than
the PostScript operators setfont
or selectfont
.
This allows the Application Kit printing mechanism to record the
fonts used in the PostScript output. If you absolutely must set
the font using a PostScript operator, you can record the font with
the Application Kit using the class method useFont. See the description of that
method for more information.
NSFont defines a number of methods for accessing a font's metrics information, when that information is available. Methods such as boundingRectForGlyph, boundingRectForFont, xHeight, and so on, all correspond to standard font metrics information. See the various method descriptions for specific information. You can also get a complete dictionary of font metrics using the afmDictionary method, or retrieve the original contents of the metrics file using afmFileContents.
NSFont defines two special glyphs. NSNullGlyph
indicates
no glyph at all, and is useful in some layout methods for calculating
information that isn't relative to another glyph. For example,
with positionOfGlyph:precededByGlyph:isNominal:,
you can specify NSNullGlyph
as the
first glyph to get the nominal advancement of the preceding glyph.
The other special glyph is NSControlGlyph
,
which the text system maps onto control functions such as linefeed
and tab. This glyph has no graphic representation and has no inherent
advancement of its own. Instead, the text system examines the control
character underlying the glyph to determine what kind of special
layout it needs to perform.
These constants describe how to encode the font's glyphs into an array of bytes. They're used by glyphPacking.
Constant | Description |
OneByteGlyphPacking |
<<Description Forthcoming>> |
JapaneseEUCGlyphPacking |
<<Description Forthcoming>> |
AsciiWithDoubleByteEUCGlyphPacking |
<<Description Forthcoming>> |
AsciiWithDoubleByteEUCGlyphPacking |
<<Description Forthcoming>> |
TwoByteGlyphPacking |
<<Description Forthcoming>> |
FourByteGlyphPacking |
<<Description Forthcoming>> |
- NSCoding
- - encodeWithCoder:
- - initWithCoder:
- NSCopying
- - copyWithZone:
- Creating arbitrary fonts
- fontWithNameAndSize
- fontWithNameAndMatrix
- Creating user fonts
- userFontOfSize
- userFixedPitchFontOfSize
- Creating system fonts
- boldSystemFontOfSize
- controlContentFontOfSize
- menuFontOfSize
- messageFontOfSize
- paletteFontOfSize
- systemFontOfSize
- titleBarFontOfSize
- toolTipsFontOfSize
- Getting preferred fonts
- setPreferredFontNames
- preferredFontNames
- Using a font to draw
- set
- Adding fonts to print operations
- useFont
- Getting general font information
- encodingScheme
- isBaseFont
- isFixedPitch
- mostCompatibleStringEncoding
- Getting information about glyphs
- glyphIsEncoded
- glyphPacking
- glyphWithName
- Getting metrics information
- advancementForGlyph
- afmDictionary
- afmFileContents
- ascender
- boundingRectForFont
- boundingRectForGlyph
- capHeight
- descender
- italicAngle
- matrix
- maximumAdvancement
- pointSize
- underlinePosition
- underlineThickness
- widthOfString
- widths
- xHeight
- Getting font names
- displayName
- familyName
- fontName
- Laying out sequential glyphs
- positionOfGlyph:precededByGlyph:isNominal:
- positionsForCompositeSequence:numberOfGlyphs:pointArray:
- Laying out overstruck glyphs
- positionOfGlyphForCharacterStruckOverRect
- positionOfGlyph:struckOverGlyph:metricsExist:
- positionOfGlyph:struckOverRect:metricsExist:
- Laying out stacked glyphs
- positionOfGlyph:withRelation:toBaseGlyph:totalAdvancement:metricsExist:
- Setting user fonts
- setUserFont
- setUserFixedPitchFont
- Getting corresponding device fonts
- printerFont
- screenFont
public static NSFont boldSystemFontOfSize(float fontSize)
See Also: fontWithNameAndSize
public static NSFont controlContentFontOfSize(float fontSize)
See Also: fontWithNameAndSize
public static Object fontWithNameAndMatrix(String typeface, NSArray fontMatrix)
makefont
operator.
In most cases, you can simply use fontWithNameAndSize to create standard
scaled fonts.See Also: - isFlipped (NSView)
public static NSFont fontWithNameAndSize(String fontName, float fontSize)
Fonts created with this method automatically flip themselves in flipped views. This method is the preferred means for creating fonts.
public static NSFont menuFontOfSize(float fontSize)
See Also: fontWithNameAndSize
public static NSFont messageFontOfSize(float fontSize)
See Also: fontWithNameAndSize
public static NSFont paletteFontOfSize(float fontSize)
See Also: fontWithNameAndSize, titleBarFontOfSize
public static NSArray preferredFontNames()
See Also: setPreferredFontNames
public static void setPreferredFontNames(NSArray fontNames)
This method is useful for optimizing glyph rendering for uncommon scripts, by guaranteeing that appropriate fonts are searched first. For example, suppose you have three hundred Latin alphabet fonts and one Cyrillic alphabet font. When you read a document in Russian, you want it to find the Cyrillic font quickly. Ordinarily, the Application Kit will search for the Cyrillic font among all three hundred and one fonts. But if it is in the list of preferred fonts, the Cyrillic font will be one of the first searched.
See Also: preferredFontNames
public static void setUserFixedPitchFont(NSFont aFont)
See Also: setUserFont, userFixedPitchFontOfSize
public static void setUserFont(NSFont aFont)
See Also: setUserFixedPitchFont, userFontOfSize
public static NSFont systemFontOfSize(float fontSize)
See Also: boldSystemFontOfSize, userFontOfSize, userFixedPitchFontOfSize, fontWithNameAndSize
public static NSFont titleBarFontOfSize(float fontSize)
See Also: paletteFontOfSize
public static NSFont toolTipsFontOfSize(float fontSize)
See Also: fontWithNameAndSize
public static void useFont(String fontName)
The
NSFont class object keeps track of the fonts used in an NSView by
recording each one that receives a set message. When the view is called
upon to generate conforming PostScript language output (such as
during printing), the NSFont class provides the list of fonts required
for the %%DocumentFonts
comment,
as required by Adobe's Document Structuring Conventions.
useFont augments
this system by providing a way to register fonts that are included
in the document but not set using NSFont's set method. For example, you might
set a font by executing the setfont
operator
within a function created by the pswrap
utility.
In such a case, be sure to pair the use of the font with a useFont message
to register the font for listing in the document comments.
public static NSFont userFixedPitchFontOfSize(float fontSize)
The system does not guarantee that all the glyphs in a fixed-pitch font are the same width. For example certain Japanese fonts are dual-pitch, and other fonts may have non-spacing marks which can affect the display of other glyphs.
See Also: userFontOfSize, fontWithNameAndSize, setUserFixedPitchFont
public static NSFont userFontOfSize(float fontSize)
See Also: userFixedPitchFontOfSize, fontWithNameAndSize, setUserFont
public NSSize advancementForGlyph(int aGlyph)
See Also: boundingRectForGlyph, maximumAdvancement
- (NSDictionary *)afmDictionary
For any other items, use the AFM file contents, as returned by afmFileContents.
public String afmFileContents()
public float ascender()
See Also: descender, capHeight, xHeight
public NSRect boundingRectForFont()
See Also: boundingRectForGlyph
public NSRect boundingRectForGlyph(int aGlyph)
Japanese fonts encoded with the scheme "EUC12-NJE-CFEncoding" do not have individual metrics or bounding boxes available for the glyphs above 127. For those glyphs, this method returns the bounding rectangle for the font, instead.
See Also: boundingRectForFont
public float capHeight()
See Also: ascender, descender, xHeight
public float descender()
public String displayName()
public String encodingScheme()
public String familyName()
See Also: fontName
public String fontName()
See Also: - familyName
public boolean glyphIsEncoded(int aGlyph)
true
if
the receiving font encodes aGlyph, false
if
it doesn't contain it.public int glyphPacking()
OneByteGlyphPacking
JapaneseEUCGlyphPacking
AsciiWithDoubleByteEUCGlyphPacking
TwoByteGlyphPacking
FourByteGlyphPacking
public int glyphWithName(String glyphName)
Glyph names in fonts do not always accurately identify the glyph. If possible, look up the appropriate glyph on your own.
public boolean isBaseFont()
true
if
the receiver is a PostScript base font, false
if
it's a PostScript composite font composed of other base fonts.public boolean isFixedPitch()
true
if
all glyphs in the receiving font have the same advancement, false
if
any advancements differ.On the Mach platform, some Japanese fonts encoded with the scheme "EUC12-NJE-CFEncoding" return that they have the same advancement, but actually encode glyphs with one of two advancements. This is for historical compatibility. You may need to handle such fonts specially for some applications.
See Also: advancementForGlyph
public float italicAngle()
public NSSize maximumAdvancement()
See Also: advancementForGlyph
public int mostCompatibleStringEncoding()
If this method returns NSStringReference.ASCIIStringEncoding
,
it could not determine the correct encoding and assumed that the
font can render only ASCII characters.
This method works heuristically using well-known font encodings, so for nonstandard encodings it may not in fact return the optimal string encoding.
See Also: widthOfString
public float pointSize()
public NSPoint positionOfGlyphForCharacterStruckOverRect(int aGlyph, char aChar, NSRect aRect)
NSPoint.ZeroPoint
if
the location can't be calculated. The nature of aChar as
one appearing above or below its base character determines the location
returned. For example, in the first figure below, the gray tilde
and box represent aGlyph and aRect,
and the black dot is the point returned (defined relative to the
origin of the aRect).To place multiple glyphs with respect to a rectangle, work from the innermost glyphs to the outermost. As you calculate the position of each glyph, enlarge the rectangle to include the bounding rectangle of the glyph in preparation for the next glyph. The second figure shows a tilde, acute accent, and cedilla all placed in their appropriate positions with respect to a rectangle, with the acute accent placed relative to the expanded bounding box of the base rectangle and the tilde.
Point.
public NSFont printerFont()
See Also: screenFont
public NSFont screenFont()
null
if
such a font can't be found. When sent to a font object representing
a bitmapped screen font, returns null
. Screen fonts are for direct use with the Window Server only. Never use them with Application Kit objects, such as in setFont: methods. Internally, the Application Kit automatically uses the corresponding screen font for a font object as long as the view is not rotated or scaled.
See Also: printerFont
public void set()
show
and
other text-drawing operators. During a print operation,
also records the font as used in the PostScript code emitted.See Also: useFont
public float underlinePosition()
See Also: underlineThickness
public float underlineThickness()
See Also: underlinePosition
public float widthOfString(String aString)
show
operator
in the receiving font. This method performs lossy conversion
of aString to the most compatible
encoding for the receiving font.Use this method only when you're sure all of aString can be rendered with the receiving font.
This method is for backwards compatibility only. In new code, use the Application Kit's string-drawing methods
See Also: mostCompatibleStringEncoding
public float xHeight()