Inspect a font’s stored tables by tag, a short name such as head. The NotoSans-Regular.ttf input is included in the example.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; byte[] data = File.ReadAllBytes( "NotoSans-Regular.ttf"); var head = FontFace.Load(data).GetTable("head"); Console.WriteLine(Convert.ToHexString( head.Span[..4]));
Unzip the example, then dotnet run.
Prints:
00010000The full program
The download includes the unchanged Noto Sans font under the SIL Open Font License, with OFL.txt.
using OneFont; FontFace face = FontFace.Load( File.ReadAllBytes("NotoSans-Regular.ttf")); ReadOnlyMemory<byte> head = face.GetTable("head"); Console.WriteLine($"head: {head.Length} bytes"); Console.WriteLine(Convert.ToHexString( head.Span[..4])); Console.WriteLine($"COLR: {face.HasTable("COLR")}");
dotnet run prints:
head: 54 bytes 00010000 COLR: False
A missing table returns empty memory; use HasTable to distinguish it from a present, empty table.
Variations
Read units per em from the header with an explicit length check:
using OneFont; using System.Buffers.Binary; FontFace face = FontFace.Load( File.ReadAllBytes("NotoSans-Regular.ttf")); ReadOnlySpan<byte> head = face.GetTable("head").Span; if (head.Length < 20) throw new InvalidDataException("Short head table."); ushort units = BinaryPrimitives .ReadUInt16BigEndian(head.Slice(18, 2)); Console.WriteLine($"Units per em: {units}");
Units per em: 1000Related recipes
The short answer
Load the font with FontFace.Load, then call GetTable(tag) for the stored bytes of a named OpenType table. The result is ReadOnlyMemory<byte>. Call HasTable(tag) when you need to distinguish a missing table from a present table with no bytes.
This is read-only inspection. The shaping docs’ table-access section says: “The bytes are not validated.” Getting a table does not decode its fields or add support for rendering the feature it describes.
How do you read OpenType tables in an OTF font?
Use the same FontFace API for a supported TTF or OTF font. A table tag identifies which part of the font you want. For example, head contains the font header, while name contains naming information. Microsoft’s OpenType file specification describes the table directory and the meaning of its entries.
The sample deliberately uses a named table instead of treating the whole file as that table’s bytes. A file has a directory plus many tables; an offset within a table is not the same as an offset from the beginning of the font file. GetTable gives you the table’s stored bytes so field offsets can be interpreted relative to that table.
The supplied Noto Sans font produces a 54-byte head table. Its first four bytes print as 00010000. Those are hexadecimal bytes, not a string extracted from the font. Other tables have different layouts, so use each table’s specification rather than applying the head offsets everywhere.
How do you read the OpenType head table?
The variation reads units per em from offset 18 as an unsigned, big-endian 16-bit field. Microsoft documents that layout in the head table specification. The sample prints 1000 for the included font.
Check the available length before slicing. ReadUInt16BigEndian expresses the font’s byte order explicitly; reading a native-endian integer would make the result depend on the machine. Microsoft specifies big-endian byte ordering for OpenType data.
That single field read is not a complete header parser. If you build a parser for another table, check its version, lengths and offsets according to its specification before following references within the bytes. Raw access supplies data; it does not check the assumptions made by your parser.
What is the difference between OTF and TTF when reading tables?
Microsoft’s file specification explains that OpenType can contain TrueType or CFF outlines. The filename extension alone is not a complete table inventory: TrueType outlines can appear in either a .ttf or .otf file. Ask for the table you need and check its presence instead of inferring it from the suffix.
In the sample, HasTable("COLR") is false. GetTable("COLR") would therefore be empty, but an empty result alone cannot distinguish absence from a stored zero-length table. Nor does presence demonstrate that the library renders color glyphs or applies font variations. This API exposes bytes, not those rendering operations.
Use the conventional four-character tags, preserving case. onefont accepts tags of one through four Latin-1 characters and pads short tags on the right with spaces. A null tag throws ArgumentNullException; an empty, overlong or non-Latin-1 tag throws ArgumentException.