// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.IO;
namespace SixLabors.Fonts.Tables.General.Colr {
///
/// Represents the BaseGlyphList table in COLR v1, which maps glyph IDs to their root paint table offsets.
///
///
internal sealed class BaseGlyphList
{
///
/// Initializes a new instance of the class.
///
/// The array of base glyph paint records.
public BaseGlyphList(BaseGlyphPaintRecord[] records)
=> this.Records = records;
///
/// Gets the array of base glyph paint records, sorted by glyph ID.
///
public BaseGlyphPaintRecord[] Records { get; }
///
/// Gets the number of base glyph paint records.
///
public int Count => this.Records.Length;
///
/// Loads a from the given reader at the specified offset.
///
/// The binary reader positioned within the COLR table.
/// The offset from the beginning of the COLR table to the BaseGlyphList.
/// The loaded , or if the offset is zero or the list is empty.
public static BaseGlyphList? Load(BigEndianBinaryReader reader, uint offset)
{
if (offset == 0)
{
return null;
}
reader.Seek(offset, SeekOrigin.Begin);
uint count = reader.ReadUInt32();
if (count == 0)
{
return null;
}
// Offsets are relative to the table start; convert to COLR-relative.
BaseGlyphPaintRecord[] records = new BaseGlyphPaintRecord[count];
for (int i = 0; i < count; i++)
{
ushort glyphId = reader.ReadUInt16();
records[i] = new BaseGlyphPaintRecord(glyphId, offset + reader.ReadOffset32());
}
// Spec says records are sorted by glyphId; assume font is correct
return new BaseGlyphList(records);
}
}
}