// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.Fonts.Tables.General {
///
/// Represents the maximum profile table, which establishes memory requirements for the font.
///
///
internal sealed class MaximumProfileTable : Table
{
///
/// The table name identifier.
///
internal const string TableName = "maxp";
///
/// Initializes a new instance of the class
/// for version 0.5 (CFF fonts specifying only the glyph count).
///
/// The number of glyphs in the font.
public MaximumProfileTable(ushort numGlyphs)
=> this.GlyphCount = numGlyphs;
///
/// Initializes a new instance of the class
/// for version 1.0 (TrueType fonts with all fields).
///
/// The number of glyphs in the font.
/// The maximum points in a non-composite glyph.
/// The maximum contours in a non-composite glyph.
/// The maximum points in a composite glyph.
/// The maximum contours in a composite glyph.
/// The maximum zones (1 if no twilight zone, 2 otherwise).
/// The maximum points used in the twilight zone (Z0).
/// The number of storage area locations.
/// The number of FDEFs.
/// The number of IDEFs.
/// The maximum stack depth.
/// The maximum byte count for glyph instructions.
/// The maximum number of components at top level for any composite glyph.
/// The maximum levels of recursion.
public MaximumProfileTable(ushort numGlyphs, ushort maxPoints, ushort maxContours, ushort maxCompositePoints, ushort maxCompositeContours, ushort maxZones, ushort maxTwilightPoints, ushort maxStorage, ushort maxFunctionDefs, ushort maxInstructionDefs, ushort maxStackElements, ushort maxSizeOfInstructions, ushort maxComponentElements, ushort maxComponentDepth)
: this(numGlyphs)
{
this.MaxPoints = maxPoints;
this.MaxContours = maxContours;
this.MaxCompositePoints = maxCompositePoints;
this.MaxCompositeContours = maxCompositeContours;
this.MaxZones = maxZones;
this.MaxTwilightPoints = maxTwilightPoints;
this.MaxStorage = maxStorage;
this.MaxFunctionDefs = maxFunctionDefs;
this.MaxInstructionDefs = maxInstructionDefs;
this.MaxStackElements = maxStackElements;
this.MaxSizeOfInstructions = maxSizeOfInstructions;
this.MaxComponentElements = maxComponentElements;
this.MaxComponentDepth = maxComponentDepth;
}
///
/// Gets the maximum points in a non-composite glyph.
///
public ushort MaxPoints { get; }
///
/// Gets the maximum contours in a non-composite glyph.
///
public ushort MaxContours { get; }
///
/// Gets the maximum points in a composite glyph.
///
public ushort MaxCompositePoints { get; }
///
/// Gets the maximum contours in a composite glyph.
///
public ushort MaxCompositeContours { get; }
///
/// Gets the maximum zones (1 if instructions do not use the twilight zone, otherwise 2).
///
public ushort MaxZones { get; }
///
/// Gets the maximum points used in the twilight zone (Z0).
///
public ushort MaxTwilightPoints { get; }
///
/// Gets the number of storage area locations.
///
public ushort MaxStorage { get; }
///
/// Gets the number of FDEFs (equals to the highest function number + 1).
///
public ushort MaxFunctionDefs { get; }
///
/// Gets the number of IDEFs.
///
public ushort MaxInstructionDefs { get; }
///
/// Gets the maximum stack depth.
///
public ushort MaxStackElements { get; }
///
/// Gets the maximum byte count for glyph instructions.
///
public ushort MaxSizeOfInstructions { get; }
///
/// Gets the maximum number of components referenced at top level for any composite glyph.
///
public ushort MaxComponentElements { get; }
///
/// Gets the maximum levels of recursion (1 for simple components).
///
public ushort MaxComponentDepth { get; }
///
/// Gets the number of glyphs in the font.
///
public ushort GlyphCount { get; }
///
/// Loads the from the specified font reader.
///
/// The font reader.
/// The .
public static MaximumProfileTable Load(FontReader reader)
{
using (BigEndianBinaryReader r = reader.GetReaderAtTablePosition(TableName))
{
return Load(r);
}
}
///
/// Loads the from the specified binary reader.
///
/// The big-endian binary reader.
/// The .
public static MaximumProfileTable Load(BigEndianBinaryReader reader)
{
// This table establishes the memory requirements for this font.Fonts with CFF data must use Version 0.5 of this table, specifying only the numGlyphs field.Fonts with TrueType outlines must use Version 1.0 of this table, where all data is required.
// Version 0.5
// Type | Name | Description
// -------|----------------------|---------------------------------------
// Fixed | Table version number | 0x00005000 for version 0.5 (Note the difference in the representation of a non - zero fractional part, in Fixed numbers.)
// uint16 | numGlyphs | The number of glyphs in the font.
float version = reader.ReadFixed();
ushort numGlyphs = reader.ReadUInt16();
if (version == 0.5)
{
return new MaximumProfileTable(numGlyphs);
}
// Version 1.0
// Type | Name | Description
// -------|-----------------------|---------------------------------------
// *Fixed | Table version number | 0x00010000 for version 1.0.
// *uint16| numGlyphs | The number of glyphs in the font.
// uint16 | maxPoints | Maximum points in a non - composite glyph.
// uint16 | maxContours | Maximum contours in a non - composite glyph.
// uint16 | maxCompositePoints | Maximum points in a composite glyph.
// uint16 | maxCompositeContours | Maximum contours in a composite glyph.
// uint16 | maxZones | 1 if instructions do not use the twilight zone (Z0), or 2 if instructions do use Z0; should be set to 2 in most cases.
// uint16 | maxTwilightPoints | Maximum points used in Z0.
// uint16 | maxStorage | Number of Storage Area locations.
// uint16 | maxFunctionDefs | Number of FDEFs, equals to the highest function number +1.
// uint16 | maxInstructionDefs | Number of IDEFs.
// uint16 | maxStackElements | Maximum stack depth2.
// uint16 | maxSizeOfInstructions | Maximum byte count for glyph instructions.
// uint16 | maxComponentElements | Maximum number of components referenced at "top level" for any composite glyph.
// uint16 | maxComponentDepth | Maximum levels of recursion; 1 for simple components.
ushort maxPoints = reader.ReadUInt16();
ushort maxContours = reader.ReadUInt16();
ushort maxCompositePoints = reader.ReadUInt16();
ushort maxCompositeContours = reader.ReadUInt16();
ushort maxZones = reader.ReadUInt16();
ushort maxTwilightPoints = reader.ReadUInt16();
ushort maxStorage = reader.ReadUInt16();
ushort maxFunctionDefs = reader.ReadUInt16();
ushort maxInstructionDefs = reader.ReadUInt16();
ushort maxStackElements = reader.ReadUInt16();
ushort maxSizeOfInstructions = reader.ReadUInt16();
ushort maxComponentElements = reader.ReadUInt16();
ushort maxComponentDepth = reader.ReadUInt16();
return new MaximumProfileTable(
numGlyphs,
maxPoints,
maxContours,
maxCompositePoints,
maxCompositeContours,
maxZones,
maxTwilightPoints,
maxStorage,
maxFunctionDefs,
maxInstructionDefs,
maxStackElements,
maxSizeOfInstructions,
maxComponentElements,
maxComponentDepth);
}
}
}