// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.Fonts.Tables.AdvancedTypographic.Variations;
using System;
namespace SixLabors.Fonts.Tables.Cff {
///
/// Represents the Compact Font Format (CFF) version 1 table.
///
///
internal sealed class Cff1Table : Table, ICffTable
{
internal const string TableName = "CFF "; // 4 chars
private readonly CffGlyphData[] glyphs;
///
/// Initializes a new instance of the class.
///
/// The parsed CFF1 font.
public Cff1Table(CffFont cff1Font) => this.glyphs = cff1Font.Glyphs;
///
public int GlyphCount => this.glyphs.Length;
///
public ItemVariationStore? ItemVariationStore => null;
///
public CffGlyphData GetGlyph(int index)
=> this.glyphs[index];
///
/// Loads the CFF1 table from the specified font reader.
///
/// The font reader.
/// The , or if the table is not present.
public static Cff1Table? Load(FontReader fontReader)
{
if (!fontReader.TryGetReaderAtTablePosition(TableName, out BigEndianBinaryReader? binaryReader))
{
return null;
}
using (binaryReader)
{
return Load(binaryReader);
}
}
///
/// Loads the CFF1 table from the specified binary reader.
///
/// The big-endian binary reader positioned at the CFF1 table header.
/// The .
public static Cff1Table Load(BigEndianBinaryReader reader)
{
// +------+---------------+----------------------------------------+
// | Type | Name | Description |
// +======+===============+========================================+
// | byte | majorVersion | Format major version. Set to 1. |
// +------+---------------+----------------------------------------+
// | byte | minorVersion | Format minor version. Set to zero. |
// +------+---------------+----------------------------------------+
// | byte | headerSize | Header size (bytes). |
// +------+---------------+----------------------------------------+
// | byte | topDictLength | Length of Top DICT structure in bytes. |
// +------+---------------+----------------------------------------+
long position = reader.BaseStream.Position;
byte[] header = reader.ReadBytes(4);
byte major = header[0];
byte minor = header[1];
byte hdrSize = header[2];
byte offSize = header[3];
switch (major)
{
case 1:
Cff1Parser parser = new();
return new(parser.Load(reader, position));
default:
throw new NotSupportedException();
}
}
}
}