static TableEntryCollection CreateTableEntryCollection(WoffTableDirectory[] woffTableDirs) { TableEntryCollection tableEntryCollection = new TableEntryCollection(); for (int i = 0; i < woffTableDirs.Length; ++i) { WoffTableDirectory woffTableDir = woffTableDirs[i]; tableEntryCollection.AddEntry( new UnreadTableEntry( new TableHeader(woffTableDir.tag, woffTableDir.origChecksum, (uint)woffTableDir.ExpectedStartAt, woffTableDir.origLength))); } return(tableEntryCollection); }
WoffTableDirectory[] ReadTableDirectories(BinaryReader reader) { //The table directory is an array of WOFF table directory entries, as defined below. //The directory follows immediately after the WOFF file header; //therefore, there is no explicit offset in the header pointing to this block. //Its size is calculated by multiplying the numTables value in the WOFF header times the size of a single WOFF table directory. //Each table directory entry specifies the size and location of a single font data table. uint tableCount = (uint)_header.numTables; //? //tableDirs = new WoffTableDirectory[tableCount]; long expectedStartAt = 0; //simulate table entry collection //var tableEntryCollection = new TableEntryCollection((int)tableCount); WoffTableDirectory[] tableDirs = new WoffTableDirectory[tableCount]; for (int i = 0; i < tableCount; ++i) { //UInt32 tag 4-byte sfnt table identifier. //UInt32 offset Offset to the data, from beginning of WOFF file. //UInt32 compLength Length of the compressed data, excluding padding. //UInt32 origLength Length of the uncompressed table, excluding padding. //UInt32 origChecksum Checksum of the uncompressed table. WoffTableDirectory table = new WoffTableDirectory(); table.tag = reader.ReadUInt32(); table.offset = reader.ReadUInt32(); table.compLength = reader.ReadUInt32(); table.origLength = reader.ReadUInt32(); table.origChecksum = reader.ReadUInt32(); table.ExpectedStartAt = expectedStartAt; table.Name = Utils.TagToString(table.tag); //var tableHeader = new TableHeader(tag, origChecksum, (uint)expectedStartAt, origLength); //var unreadTable = new UnreadTableEntry(tableHeader); //tableEntryCollection.AddEntry(unreadTable); //table.unreadTableEntry = unreadTable; tableDirs[i] = table; expectedStartAt += table.origLength; } return(tableDirs); }
bool Extract(BinaryReader reader, WoffTableDirectory[] tables, Stream newDecompressedStream) { for (int i = 0; i < tables.Length; ++i) { //UInt32 tag 4-byte sfnt table identifier. //UInt32 offset Offset to the data, from beginning of WOFF file. //UInt32 compLength Length of the compressed data, excluding padding. //UInt32 origLength Length of the uncompressed table, excluding padding. //UInt32 origChecksum Checksum of the uncompressed table. WoffTableDirectory table = tables[i]; reader.BaseStream.Seek(table.offset, SeekOrigin.Begin); //indeed, table may be compress or not=> check length of before and after ... byte[] compressedBuffer = reader.ReadBytes((int)table.compLength); if (compressedBuffer.Length == table.origLength) { //not a compress buffer newDecompressedStream.Write(compressedBuffer, 0, compressedBuffer.Length); } else { var decompressedBuffer = new byte[table.origLength]; if (!DecompressHandler(compressedBuffer, decompressedBuffer)) { //if not pass set to null decompressedBuffer = null; } else { //pass newDecompressedStream.Write(decompressedBuffer, 0, decompressedBuffer.Length); } } } newDecompressedStream.Flush(); return(true); }