// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.Fonts.Tables {
///
/// Represents a font collection header (for .ttc font collections).
/// A font collection contains one or more fonts where typically the glyf table is shared by multiple fonts to save space,
/// but other tables are not.
/// Each font in the collection has its own set of tables.
///
internal class TtcHeader
{
internal const string TableName = "ttcf";
///
/// Initializes a new instance of the class.
///
/// The TTC tag, expected to be "ttcf".
/// The major version of the TTC header (1 or 2).
/// The minor version of the TTC header.
/// The number of fonts in the collection.
/// Array of byte offsets to each font's offset table.
/// The DSIG table tag (version 2+ only, otherwise 0).
/// The length of the DSIG table in bytes (version 2+ only, otherwise 0).
/// The byte offset of the DSIG table (version 2+ only, otherwise 0).
public TtcHeader(string ttcTag, ushort majorVersion, ushort minorVersion, uint numFonts, uint[] offsetTable, uint dsigTag, uint dsigLength, uint dsigOffset)
{
this.TtcTag = ttcTag;
this.MajorVersion = majorVersion;
this.MinorVersion = minorVersion;
this.NumFonts = numFonts;
this.OffsetTable = offsetTable;
this.DsigTag = dsigTag;
this.DsigLength = dsigLength;
this.DsigOffset = dsigOffset;
}
///
/// Gets the tag, should be "ttcf".
///
public string TtcTag { get; }
///
/// Gets the major version of the TTC header. Version 1 has no DSIG; version 2 includes DSIG fields.
///
public ushort MajorVersion { get; }
///
/// Gets the minor version of the TTC header.
///
public ushort MinorVersion { get; }
///
/// Gets the number of fonts contained in the collection.
///
public uint NumFonts { get; }
///
/// Gets the array of offsets to the OffsetTable of each font. Use for each font.
///
public uint[] OffsetTable { get; }
///
/// Gets the tag of the DSIG (digital signature) table. Only present in version 2+ headers.
///
public uint DsigTag { get; }
///
/// Gets the length of the DSIG table in bytes. Only present in version 2+ headers.
///
public uint DsigLength { get; }
///
/// Gets the byte offset of the DSIG table from the beginning of the file. Only present in version 2+ headers.
///
public uint DsigOffset { get; }
///
/// Reads a from the given reader.
///
/// The binary reader positioned at the start of the TTC header.
/// The parsed .
/// Thrown when the tag is not "ttcf".
public static TtcHeader Read(BigEndianBinaryReader reader)
{
string tag = reader.ReadTag();
if (tag != TableName)
{
throw new InvalidFontTableException($"Expected tag = {TableName} found {tag}", TableName);
}
ushort majorVersion = reader.ReadUInt16();
ushort minorVersion = reader.ReadUInt16();
uint numFonts = reader.ReadUInt32();
uint[] offsetTable = new uint[numFonts];
for (int i = 0; i < numFonts; ++i)
{
offsetTable[i] = reader.ReadOffset32();
}
// Version 2 fields
uint dsigTag = 0;
uint dsigLength = 0;
uint dsigOffset = 0;
if (majorVersion >= 2)
{
dsigTag = reader.ReadUInt32();
dsigLength = reader.ReadUInt32();
dsigOffset = reader.ReadUInt32();
}
return new TtcHeader(tag, majorVersion, minorVersion, numFonts, offsetTable, dsigTag, dsigLength, dsigOffset);
}
}
}