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 AfmFontMetrics

Remarks

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

NameDescription
AscenderGets the top of the lowercase d (Ascender).
CapHeightGets the top of the capital H (CapHeight).
CharacterCountGets the number of characters of the font program (Characters).
CharacterMetricsGets the metrics of the characters, in the order of the file.
CharacterSetGets the character set of the font, for example "ExtendedRoman" (CharacterSet).
CommentsGets the comments of the file (Comment), in the order of the file.
CompositesGets the composite characters of the font (StartComposites), in the order of the file.
DescenderGets the bottom of the lowercase p (Descender).
EncodingSchemeGets the default encoding of the font, for example "AdobeStandardEncoding" or "FontSpecific" (EncodingScheme).
EscCharGets the escape character of a font with the escape mapping scheme (EscChar).
FamilyNameGets the name of the family of the font (FamilyName).
FontBBoxGets the bounding box of all the characters of the font (FontBBox).
FontNameGets the name of the font program, as given to the PostScript findfont operator (FontName).
FormatVersionGets the version of the AFM specification the file follows (the value of StartFontMetrics, for example "4.1").
FullNameGets the full name of the font (FullName).
HorizontalGets the metrics of writing direction 0 (horizontal), or null when the file does not describe it.
IsBaseFontGets a value indicating whether the font is a base font rather than a composite font (IsBaseFont, true when absent).
IsCIDFontGets a value indicating whether the font is a CID-keyed font, whose character codes are CIDs (IsCIDFont).
IsFixedVGets a value indicating whether every character has the same AfmFontMetrics.VVector (IsFixedV; when absent, whether AfmFontMetrics.VVector is present).
KerningPairsGets the kerning pairs of writing direction 0 (StartKernPairs or StartKernPairs0), in the order of the file.
MappingSchemeGets the mapping scheme of a composite font (MappingScheme).
MetricsSetsGets the writing directions the file describes: 0 for direction 0 only, 1 for direction 1 only, 2 for both (MetricsSets).
NoticeGets the trademark or copyright notice of the font (Notice).
StandardFontNamesGets 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.
StdHWGets the dominant width of the horizontal stems (StdHW).
StdVWGets the dominant width of the vertical stems (StdVW).
TrackKerningGets the tracks of the font (StartTrackKern), in the order of the file.
VVectorGets the vector from the origin of writing direction 0 to the origin of writing direction 1, when every character has the same one (VVector).
VersionGets the version of the font program (Version).
VerticalGets the metrics of writing direction 1 (vertical), or null when the file does not describe it.
VerticalKerningPairsGets the kerning pairs of writing direction 1 (StartKernPairs1), in the order of the file.
WeightGets the weight of the font, for example "Bold" (Weight).
XHeightGets the top of the lowercase x (XHeight).

Gets the top of the lowercase d (Ascender).

public double? Ascender { get; }

Property value

double?

Gets the top of the capital H (CapHeight).

public double? CapHeight { get; }

Property value

double?

CharacterCount

AfmFontMetrics.cs

Gets the number of characters of the font program (Characters).

public int? CharacterCount { get; }

Property value

int?

CharacterMetrics

AfmFontMetrics.cs

Gets the metrics of the characters, in the order of the file.

public IReadOnlyList<AfmCharacterMetrics> CharacterMetrics { get; }

CharacterSet

AfmFontMetrics.cs

Gets the character set of the font, for example "ExtendedRoman" (CharacterSet).

public string? CharacterSet { get; }

Property value

string?

Gets the comments of the file (Comment), in the order of the file.

public IReadOnlyList<string> Comments { get; }

Property value

IReadOnlyList<string>

Gets the composite characters of the font (StartComposites), in the order of the file.

public IReadOnlyList<AfmCompositeCharacter> Composites { get; }

Gets the bottom of the lowercase p (Descender).

public double? Descender { get; }

Property value

double?

EncodingScheme

AfmFontMetrics.cs

Gets the default encoding of the font, for example "AdobeStandardEncoding" or "FontSpecific" (EncodingScheme).

public string? EncodingScheme { get; }

Property value

string?

Gets the escape character of a font with the escape mapping scheme (EscChar).

public int? EscChar { get; }

Property value

int?

Gets the name of the family of the font (FamilyName).

public string? FamilyName { get; }

Property value

string?

Gets the bounding box of all the characters of the font (FontBBox).

public AfmBox? FontBBox { get; }

Property value

AfmBox?

Gets the name of the font program, as given to the PostScript findfont operator (FontName).

public string? FontName { get; }

Property value

string?

FormatVersion

AfmFontMetrics.cs

Gets the version of the AFM specification the file follows (the value of StartFontMetrics, for example "4.1").

public string FormatVersion { get; }

Property value

string

Gets the full name of the font (FullName).

public string? FullName { get; }

Property value

string?

Gets the metrics of writing direction 0 (horizontal), or null when the file does not describe it.

public AfmWritingDirection? Horizontal { get; }

Property value

AfmWritingDirection?

Gets a value indicating whether the font is a base font rather than a composite font (IsBaseFont, true when absent).

public bool IsBaseFont { get; }

Property value

bool

Gets a value indicating whether the font is a CID-keyed font, whose character codes are CIDs (IsCIDFont).

public bool IsCIDFont { get; }

Property value

bool

Gets 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

bool

KerningPairs

AfmFontMetrics.cs

Gets the kerning pairs of writing direction 0 (StartKernPairs or StartKernPairs0), in the order of the file.

public IReadOnlyList<AfmKerningPair> KerningPairs { get; }

MappingScheme

AfmFontMetrics.cs

Gets the mapping scheme of a composite font (MappingScheme).

public int? MappingScheme { get; }

Property value

int?

Gets the writing directions the file describes: 0 for direction 0 only, 1 for direction 1 only, 2 for both (MetricsSets).

public int MetricsSets { get; }

Property value

int

Gets the trademark or copyright notice of the font (Notice).

public string? Notice { get; }

Property value

string?

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.

public static IReadOnlyList<string> StandardFontNames { get; }

Property value

IReadOnlyList<string>

Gets the dominant width of the horizontal stems (StdHW).

public double? StdHW { get; }

Property value

double?

Gets the dominant width of the vertical stems (StdVW).

public double? StdVW { get; }

Property value

double?

TrackKerning

AfmFontMetrics.cs

Gets the tracks of the font (StartTrackKern), in the order of the file.

public IReadOnlyList<AfmTrackKerning> TrackKerning { get; }

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).

public AfmVector? VVector { get; }

Property value

AfmVector?

Gets the version of the font program (Version).

public string? Version { get; }

Property value

string?

Gets the metrics of writing direction 1 (vertical), or null when the file does not describe it.

public AfmWritingDirection? Vertical { get; }

Property value

AfmWritingDirection?

VerticalKerningPairs

AfmFontMetrics.cs

Gets the kerning pairs of writing direction 1 (StartKernPairs1), in the order of the file.

public IReadOnlyList<AfmKerningPair> VerticalKerningPairs { get; }

Gets the weight of the font, for example "Bold" (Weight).

public string? Weight { get; }

Property value

string?

Gets the top of the lowercase x (XHeight).

public double? XHeight { get; }

Property value

double?

Methods

NameDescription
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.cs

Gets the metrics of the character that has a name.

public AfmCharacterMetrics? GetCharacter(string name)

Parameters

name string
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.cs

Gets the metrics of the character that has a code in the default encoding of the font.

public AfmCharacterMetrics? GetCharacterByCode(int code)

Parameters

code int
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.cs

Gets the kerning of a pair of characters in writing direction 0.

public AfmVector GetKerning(int firstCode, int secondCode)
Parameters
firstCode int
The code of the first character.
secondCode int
The code of the second character.
Returns

AfmVector The vector by which the second character moves, or zero when the pair has no kerning. When the file has the pair twice, the first one applies.

GetKerning(string, string)

AfmFontMetrics.cs

Gets the kerning of a pair of characters in writing direction 0.

public AfmVector GetKerning(string first, string second)
Parameters
first string
The name of the first character.
second string
The name of the second character.
Returns

AfmVector The vector by which the second character moves, or zero when the pair has no kerning. When the file has the pair twice, the first one applies.

GetStandardFont(string)

AfmFontMetrics.StandardFonts.cs

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.

public static AfmFontMetrics? GetStandardFont(string fontName)

Parameters

fontName string
The PostScript name of the font, such as Helvetica-Bold (see AfmFontMetrics.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.cs

Gets the track kerning of a degree of tightness at a point size.

public double GetTrackKerning(int degree, double pointSize)

Parameters

degree int
The degree of tightness of the track.
pointSize double
The point size of the font.

Returns

double The amount, in points, to add to the space between characters, or zero when the font has no track of that degree. When the file has the degree twice, the first track applies.

GetVerticalKerning overloads

GetVerticalKerning(int, int)

AfmFontMetrics.cs

Gets the kerning of a pair of characters in writing direction 1.

public AfmVector GetVerticalKerning(int firstCode, int secondCode)
Parameters
firstCode int
The code of the first character.
secondCode int
The code of the second character.
Returns

AfmVector The vector by which the second character moves, or zero when the pair has no kerning. When the file has the pair twice, the first one applies.

GetVerticalKerning(string, string)

AfmFontMetrics.cs

Gets the kerning of a pair of characters in writing direction 1.

public AfmVector GetVerticalKerning(string first, string second)
Parameters
first string
The name of the first character.
second string
The name of the second character.
Returns

AfmVector The vector by which the second character moves, or zero when the pair has no kerning. When the file has the pair twice, the first one applies.

GetVerticalWidth overloads

GetVerticalWidth(AfmCharacterMetrics)

AfmFontMetrics.cs

Gets the width vector of a character in writing direction 1.

public AfmVector? GetVerticalWidth(AfmCharacterMetrics character)
Parameters
character AfmCharacterMetrics
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.cs

Gets the width vector of a character in writing direction 1.

public AfmVector? GetVerticalWidth(string name)
Parameters
name string
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.cs

Gets the width vector of a character in writing direction 0.

public AfmVector? GetWidth(AfmCharacterMetrics character)
Parameters
character AfmCharacterMetrics
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.cs

Gets the width vector of a character in writing direction 0.

public AfmVector? GetWidth(string name)
Parameters
name string
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.cs

Reads an AFM file from a stream.

public static AfmFontMetrics Load(Stream stream, FontResourceLimits? limits = null)
Parameters
stream Stream
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.
limits FontResourceLimits?
The limits of the font code, or null for FontResourceLimits.Default: the file is bounded by FontResourceLimits.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.cs

Reads an AFM file from its bytes.

public static AfmFontMetrics Load(ReadOnlySpan<byte> data, FontResourceLimits? limits = null)
Parameters
data ReadOnlySpan<byte>
The bytes of the file. The file is ASCII; the bytes above 127 found in some files are read as ISO-8859-1.
limits FontResourceLimits?
The limits of the font code, or null for FontResourceLimits.Default: the file is bounded by FontResourceLimits.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.cs

Reads 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

stream Stream
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.
limits FontResourceLimits?
The limits of the font code, or null for FontResourceLimits.Default: the file is bounded by FontResourceLimits.MaxEncodedBytes.
cancellationToken CancellationToken
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.cs

Reads an AFM file from its text.

public static AfmFontMetrics Parse(ReadOnlySpan<char> text)

Parameters

text ReadOnlySpan<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.