AfmFontMetrics Class
The metrics of a PostScript font read from an AFM file (Adobe Font Metrics, Adobe Technical Note #5004, version 4.1): the global metrics of the font, the widths, bounding boxes and ligatures of its characters, its kerning pairs and tracks, and its composite characters.
public sealed class AfmFontMetricsRemarks
An AFM file has no glyphs: it gives what text layout needs without the font program, such as the metrics of the 14 standard fonts of PDF. Its values are in units of 1/1000 of the point size, except the kerning amounts of the tracks, which are in points.
The keys the reader does not know are ignored, as the specification requires. The multiple master (AMFM) and the composite (ACFM) metrics files are not supported. The object is immutable and can be used from several threads.
Example
// An AFM file gives the metrics of a PostScript font without its glyphs, here two characters of Helvetica.
var metrics = AfmFontMetrics.Parse("""
StartFontMetrics 4.1
FontName Helvetica
StartCharMetrics 2
C 65 ; WX 667 ; N A ; B 14 0 654 718 ;
C 86 ; WX 667 ; N V ; B 20 0 647 718 ;
EndCharMetrics
StartKernData
StartKernPairs 1
KPX A V -70
EndKernPairs
EndKernData
EndFontMetrics
""");
// The metrics are in 1/1000 of the point size: the width of "AV" at 12 points, with its kerning.
var units = metrics.GetWidth("A")!.Value.X + metrics.GetKerning("A", "V").X + metrics.GetWidth("V")!.Value.X;
var width = units * 12 / 1000;
Assert.Equal(15.168, width);Properties
| Name | Description |
|---|---|
| Ascender | Gets the top of the lowercase d (Ascender). |
| CapHeight | Gets the top of the capital H (CapHeight). |
| CharacterCount | Gets the number of characters of the font program (Characters). |
| CharacterMetrics | Gets the metrics of the characters, in the order of the file. |
| CharacterSet | Gets the character set of the font, for example "ExtendedRoman" (CharacterSet). |
| Comments | Gets the comments of the file (Comment), in the order of the file. |
| Composites | Gets the composite characters of the font (StartComposites), in the order of the file. |
| Descender | Gets the bottom of the lowercase p (Descender). |
| EncodingScheme | Gets the default encoding of the font, for example "AdobeStandardEncoding" or "FontSpecific" (EncodingScheme). |
| EscChar | Gets the escape character of a font with the escape mapping scheme (EscChar). |
| FamilyName | Gets the name of the family of the font (FamilyName). |
| FontBBox | Gets the bounding box of all the characters of the font (FontBBox). |
| FontName | Gets the name of the font program, as given to the PostScript findfont operator (FontName). |
| FormatVersion | Gets the version of the AFM specification the file follows (the value of StartFontMetrics, for example "4.1"). |
| FullName | Gets the full name of the font (FullName). |
| Horizontal | Gets the metrics of writing direction 0 (horizontal), or null when the file does not describe it. |
| IsBaseFont | Gets a value indicating whether the font is a base font rather than a composite font (IsBaseFont, true when absent). |
| IsCIDFont | Gets a value indicating whether the font is a CID-keyed font, whose character codes are CIDs (IsCIDFont). |
| IsFixedV | Gets a value indicating whether every character has the same AfmFontMetrics.VVector (IsFixedV; when absent, whether AfmFontMetrics.VVector is present). |
| KerningPairs | Gets the kerning pairs of writing direction 0 (StartKernPairs or StartKernPairs0), in the order of the file. |
| MappingScheme | Gets the mapping scheme of a composite font (MappingScheme). |
| MetricsSets | Gets the writing directions the file describes: 0 for direction 0 only, 1 for direction 1 only, 2 for both (MetricsSets). |
| Notice | Gets the trademark or copyright notice of the font (Notice). |
| StandardFontNames | Gets the PostScript names of the 14 standard fonts of PDF (Courier, Helvetica and Times in four styles, Symbol and ZapfDingbats), whose metrics AfmFontMetrics.GetStandardFont returns. |
| StdHW | Gets the dominant width of the horizontal stems (StdHW). |
| StdVW | Gets the dominant width of the vertical stems (StdVW). |
| TrackKerning | Gets the tracks of the font (StartTrackKern), in the order of the file. |
| VVector | Gets the vector from the origin of writing direction 0 to the origin of writing direction 1, when every character has the same one (VVector). |
| Version | Gets the version of the font program (Version). |
| Vertical | Gets the metrics of writing direction 1 (vertical), or null when the file does not describe it. |
| VerticalKerningPairs | Gets the kerning pairs of writing direction 1 (StartKernPairs1), in the order of the file. |
| Weight | Gets the weight of the font, for example "Bold" (Weight). |
| XHeight | Gets the top of the lowercase x (XHeight). |
Ascender
AfmFontMetrics.csCapHeight
AfmFontMetrics.csCharacterCount
AfmFontMetrics.csCharacterMetrics
AfmFontMetrics.csGets the metrics of the characters, in the order of the file.
public IReadOnlyList<AfmCharacterMetrics> CharacterMetrics { get; }Property value
CharacterSet
AfmFontMetrics.csComments
AfmFontMetrics.csGets the comments of the file (Comment), in the order of the file.
public IReadOnlyList<string> Comments { get; }Property value
Composites
AfmFontMetrics.csGets the composite characters of the font (StartComposites), in the order of the file.
public IReadOnlyList<AfmCompositeCharacter> Composites { get; }Property value
Descender
AfmFontMetrics.csEncodingScheme
AfmFontMetrics.csEscChar
AfmFontMetrics.csFamilyName
AfmFontMetrics.csFontBBox
AfmFontMetrics.csFontName
AfmFontMetrics.csFormatVersion
AfmFontMetrics.csFullName
AfmFontMetrics.csHorizontal
AfmFontMetrics.csGets the metrics of writing direction 0 (horizontal), or null when the file does not describe it.
public AfmWritingDirection? Horizontal { get; }Property value
IsBaseFont
AfmFontMetrics.csIsCIDFont
AfmFontMetrics.csIsFixedV
AfmFontMetrics.csGets a value indicating whether every character has the same AfmFontMetrics.VVector (IsFixedV; when absent, whether AfmFontMetrics.VVector is present).
public bool IsFixedV { get; }Property value
KerningPairs
AfmFontMetrics.csGets the kerning pairs of writing direction 0 (StartKernPairs or StartKernPairs0), in the order of the file.
public IReadOnlyList<AfmKerningPair> KerningPairs { get; }Property value
MappingScheme
AfmFontMetrics.csMetricsSets
AfmFontMetrics.csNotice
AfmFontMetrics.csStandardFontNames
AfmFontMetrics.StandardFonts.csGets the PostScript names of the 14 standard fonts of PDF (Courier, Helvetica and Times in four styles, Symbol and ZapfDingbats), whose metrics AfmFontMetrics.GetStandardFont returns.
public static IReadOnlyList<string> StandardFontNames { get; }Property value
StdHW
AfmFontMetrics.csStdVW
AfmFontMetrics.csTrackKerning
AfmFontMetrics.csGets the tracks of the font (StartTrackKern), in the order of the file.
public IReadOnlyList<AfmTrackKerning> TrackKerning { get; }Property value
VVector
AfmFontMetrics.csGets the vector from the origin of writing direction 0 to the origin of writing direction 1, when every character has the same one (VVector).
public AfmVector? VVector { get; }Property value
Version
AfmFontMetrics.csVertical
AfmFontMetrics.csGets the metrics of writing direction 1 (vertical), or null when the file does not describe it.
public AfmWritingDirection? Vertical { get; }Property value
VerticalKerningPairs
AfmFontMetrics.csGets the kerning pairs of writing direction 1 (StartKernPairs1), in the order of the file.
public IReadOnlyList<AfmKerningPair> VerticalKerningPairs { get; }Property value
Weight
AfmFontMetrics.csXHeight
AfmFontMetrics.csMethods
| Name | Description |
|---|---|
| GetCharacter(string) | Gets the metrics of the character that has a name. |
| GetCharacterByCode(int) | Gets the metrics of the character that has a code in the default encoding of the font. |
| GetKerning(int, int) | Gets the kerning of a pair of characters in writing direction 0. |
| GetKerning(string, string) | Gets the kerning of a pair of characters in writing direction 0. |
| GetStandardFont(string) | Gets the metrics of one of the 14 standard fonts of PDF (ISO 32000-2, 9.6.2.2), read from the AFM files Adobe published (Core14_AFMs.zip), which the library embeds. |
| GetTrackKerning(int, double) | Gets the track kerning of a degree of tightness at a point size. |
| GetVerticalKerning(int, int) | Gets the kerning of a pair of characters in writing direction 1. |
| GetVerticalKerning(string, string) | Gets the kerning of a pair of characters in writing direction 1. |
| GetVerticalWidth(AfmCharacterMetrics) | Gets the width vector of a character in writing direction 1. |
| GetVerticalWidth(string) | Gets the width vector of a character in writing direction 1. |
| GetWidth(AfmCharacterMetrics) | Gets the width vector of a character in writing direction 0. |
| GetWidth(string) | Gets the width vector of a character in writing direction 0. |
| Load(Stream, FontResourceLimits?) | Reads an AFM file from a stream. |
| Load(ReadOnlySpan<byte>, FontResourceLimits?) | Reads an AFM file from its bytes. |
| LoadAsync(Stream, FontResourceLimits?, CancellationToken) | Reads an AFM file from a stream, asynchronously. |
| Parse(ReadOnlySpan<char>) | Reads an AFM file from its text. |
GetCharacter(string)
AfmFontMetrics.csGets the metrics of the character that has a name.
public AfmCharacterMetrics? GetCharacter(string name)Parameters
namestring- The PostScript name of the character.
Returns
AfmCharacterMetrics? The metrics of the first character of the file with that name, or null when there is none.
GetCharacterByCode(int)
AfmFontMetrics.csGets the metrics of the character that has a code in the default encoding of the font.
public AfmCharacterMetrics? GetCharacterByCode(int code)Parameters
codeint- The code of the character (the CID in a CID-keyed font).
Returns
AfmCharacterMetrics? The metrics of the first character of the file with that code, or null when there is none.
GetKerning overloads
GetKerning(int, int)
AfmFontMetrics.csGets the kerning of a pair of characters in writing direction 0.
GetKerning(string, string)
AfmFontMetrics.csGets the kerning of a pair of characters in writing direction 0.
GetStandardFont(string)
AfmFontMetrics.StandardFonts.csGets the metrics of one of the 14 standard fonts of PDF (ISO 32000-2, 9.6.2.2), read from the AFM files Adobe published (Core14_AFMs.zip), which the library embeds.
public static AfmFontMetrics? GetStandardFont(string fontName)Parameters
fontNamestring- The PostScript name of the font, such as
Helvetica-Bold(seeAfmFontMetrics.StandardFontNames). The comparison is ordinal.
Returns
AfmFontMetrics? The metrics, read once and shared, or null when the name is not the name of a standard font.
GetTrackKerning(int, double)
AfmFontMetrics.csGets the track kerning of a degree of tightness at a point size.
GetVerticalKerning overloads
GetVerticalKerning(int, int)
AfmFontMetrics.csGets the kerning of a pair of characters in writing direction 1.
GetVerticalKerning(string, string)
AfmFontMetrics.csGets the kerning of a pair of characters in writing direction 1.
GetVerticalWidth overloads
GetVerticalWidth(AfmCharacterMetrics)
AfmFontMetrics.csGets the width vector of a character in writing direction 1.
public AfmVector? GetVerticalWidth(AfmCharacterMetrics character)Parameters
characterAfmCharacterMetrics- The metrics of a character of this font.
Returns
AfmVector? The vertical width of the character, the AfmWritingDirection.CharWidth of the direction when the character has none, or null when there is neither.
GetVerticalWidth(string)
AfmFontMetrics.csGets the width vector of a character in writing direction 1.
Parameters
namestring- The PostScript name of the character.
Returns
AfmVector? The vertical width of the character, the AfmWritingDirection.CharWidth of the direction when the character has none, or null when the font has no such character or no width for it.
GetWidth overloads
GetWidth(AfmCharacterMetrics)
AfmFontMetrics.csGets the width vector of a character in writing direction 0.
public AfmVector? GetWidth(AfmCharacterMetrics character)Parameters
characterAfmCharacterMetrics- The metrics of a character of this font.
Returns
AfmVector? The width of the character, the AfmWritingDirection.CharWidth of the direction when the character has none, or null when there is neither.
GetWidth(string)
AfmFontMetrics.csGets the width vector of a character in writing direction 0.
Parameters
namestring- The PostScript name of the character.
Returns
AfmVector? The width of the character, the AfmWritingDirection.CharWidth of the direction when the character has none, or null when the font has no such character or no width for it.
Load overloads
Load(Stream, FontResourceLimits?)
AfmFontMetrics.csReads an AFM file from a stream.
public static AfmFontMetrics Load(Stream stream, FontResourceLimits? limits = null)Parameters
streamStream- The stream of the file, read to its end. The file is ASCII; the bytes above 127 found in some files are read as ISO-8859-1.
limitsFontResourceLimits?- The limits of the font code, or
nullforFontResourceLimits.Default: the file is bounded byFontResourceLimits.MaxEncodedBytes.
Returns
AfmFontMetrics The metrics of the file.
Exceptions
InvalidFontException- The data is not a valid AFM file.
UnsupportedFontFeatureException- The data is a multiple master (AMFM) or a composite (ACFM) metrics file.
FontResourceLimitException- The stream has more bytes than
FontResourceLimits.MaxEncodedBytes.
Load(ReadOnlySpan<byte>, FontResourceLimits?)
AfmFontMetrics.csReads an AFM file from its bytes.
public static AfmFontMetrics Load(ReadOnlySpan<byte> data, FontResourceLimits? limits = null)Parameters
dataReadOnlySpan<byte>- The bytes of the file. The file is ASCII; the bytes above 127 found in some files are read as ISO-8859-1.
limitsFontResourceLimits?- The limits of the font code, or
nullforFontResourceLimits.Default: the file is bounded byFontResourceLimits.MaxEncodedBytes.
Returns
AfmFontMetrics The metrics of the file.
Exceptions
InvalidFontException- The data is not a valid AFM file.
UnsupportedFontFeatureException- The data is a multiple master (AMFM) or a composite (ACFM) metrics file.
FontResourceLimitException- The data is larger than
FontResourceLimits.MaxEncodedBytes.
LoadAsync(Stream, FontResourceLimits?, CancellationToken)
AfmFontMetrics.csReads an AFM file from a stream, asynchronously.
net10.0
public static Task<AfmFontMetrics> LoadAsync(Stream stream, FontResourceLimits? limits = null, CancellationToken cancellationToken = default)net11.0
public static Task<AfmFontMetrics> LoadAsync(Stream stream, FontResourceLimits? limits = null, CancellationToken cancellationToken = default)Parameters
streamStream- The stream of the file, read to its end. The file is ASCII; the bytes above 127 found in some files are read as ISO-8859-1.
limitsFontResourceLimits?- The limits of the font code, or
nullforFontResourceLimits.Default: the file is bounded byFontResourceLimits.MaxEncodedBytes. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task<AfmFontMetrics> The metrics of the file.
Exceptions
InvalidFontException- The data is not a valid AFM file.
UnsupportedFontFeatureException- The data is a multiple master (AMFM) or a composite (ACFM) metrics file.
FontResourceLimitException- The stream has more bytes than
FontResourceLimits.MaxEncodedBytes.
Parse(ReadOnlySpan<char>)
AfmFontMetrics.csReads an AFM file from its text.
public static AfmFontMetrics Parse(ReadOnlySpan<char> text)Parameters
textReadOnlySpan<char>- The text of the file.
Returns
AfmFontMetrics The metrics of the file.
Exceptions
InvalidFontException- The text is not a valid AFM file.
UnsupportedFontFeatureException- The text is a multiple master (AMFM) or a composite (ACFM) metrics file.