예제 #1
0
        private bool ReadData(Stream stream)
        {
            if (stream == null)
            {
                return(false);
            }

            int bytesRead = 0;

            byte[] header = WoffBuffer.ReadBytes(stream, WoffUtils.Woff1DirSize, out bytesRead);

            _tag          = WoffBuffer.ReadUInt32BE(header, 0);
            _offset       = WoffBuffer.ReadUInt32BE(header, 4);
            _compLength   = WoffBuffer.ReadUInt32BE(header, 8);
            _origLength   = WoffBuffer.ReadUInt32BE(header, 12);
            _origChecksum = WoffBuffer.ReadUInt32BE(header, 16);

            if (_origLength > 0)
            {
                _padding = WoffUtils.CalculatePadding(_origLength);
            }

            return(true);
        }
예제 #2
0
        private bool ReadData(Stream stream, bool isTrueType)
        {
            if (stream == null)
            {
                return(false);
            }
            var tableFlags = WoffTable.TableFlags;

            int bytesRead = 0;

            // UInt8: flags	table type and flags
            _flags = (byte)stream.ReadByte();
            // The interpretation of the flags field is as follows:
            // 1. Bits [0..5] contain an index to the "known tag" table, which represents tags likely to appear in fonts
            int tagIndex = _flags & 0x3F;

            // If the tag is not present in this table, then the value of this bit field is 63.
            if (tagIndex >= 63)
            {
                // UInt32: tag	4-byte tag (optional)
                _tag = WoffBuffer.ReadUInt32BE(WoffBuffer.ReadBytes(stream, 4, out bytesRead), 0);
                var name = WoffBuffer.TagString(_tag);

                for (int i = 0; i < tableFlags.Count; i++)
                {
                    if (string.Equals(name, tableFlags[i], StringComparison.Ordinal))
                    {
                        tagIndex = i;
                        break;
                    }
                }
            }
            else
            {
                _tag = WoffBuffer.TagInt(tableFlags[tagIndex]);
            }

            // 2. Bits 6 and 7 indicate the preprocessing transformation version number (0-3) that was applied to each table.
            int transformVersion = (_flags >> 5) & 0x3;

            // UIntBase128:	origLength	length of original table
            if (!WoffBuffer.ReadUIntBase128(stream, out _origLength))
            {
                return(false);
            }

            // UIntBase128	transformLength	transformed length (if applicable)
            //            _transformLength = _origLength;
            _compLength      = _origLength;
            _transformLength = 0;

            // For all tables in a font, except for 'glyf' and 'loca' tables, transformation version 0
            // indicates the null transform where the original table data is passed directly to the
            // Brotli compressor for inclusion in the compressed data stream.
            if (transformVersion == 0)
            {
                // "glyf" : 10,  "loca" : 11
                if (tagIndex == 10 || tagIndex == 11)
                {
                    if (!WoffBuffer.ReadUIntBase128(stream, out _transformLength))
                    {
                        return(false);
                    }
                    _compLength = _transformLength;
                }
            }
            // The transformation version "1", specified below, is optional and can be applied to
            // eliminate certain redundancies in the hmtx table data.
            if (transformVersion == 1)
            {
                // The transformation version 1 described in this subclause is optional and can only
                // be used when an input font is TrueType-flavored (i.e. has a glyf table)
                // "hmtx" : 3
                if (tagIndex == 3 && isTrueType)
                {
                    if (!WoffBuffer.ReadUIntBase128(stream, out _transformLength))
                    {
                        return(false);
                    }
                    _compLength = _transformLength;
                }
            }

            if (_origLength > 0)
            {
                _padding = WoffUtils.CalculatePadding(_origLength);
            }

            return(true);
        }
예제 #3
0
        private bool WriteWoff2(Stream stream)
        {
            if (_woffHeader.IsCollection)
            {
                return(WriteWoff2Collection(stream));
            }

            if (stream == null || _woffHeader == null || _woffDirs == null || _woffDirs.Count == 0)
            {
                return(false);
            }

            var numTables = _woffHeader.NumTables;

            // Re-order tables in output order (by the OpenType Specifications)
            var sortedDirs = _woffDirs.ToArray();

            Array.Sort(sortedDirs, WoffTableDirectory.GetComparer());

            //-------------------------------------------------------------------------------------------------
            // 1. Compute the checksums, the paddings of the tables will be recalculated after the transformations.
            uint tablesChecksum = 0; // We have only one font here...

            for (int i = 0; i < numTables; i++)
            {
                tablesChecksum += sortedDirs[i].RecalculateChecksum();
            }

            //-------------------------------------------------------------------------------------------------
            // Build the smaller headers + directories data in memory...
            var writer = new WoffWriter();

            var searchRange   = (ushort)(WoffUtils.MaxPower2LE(numTables) * TableSize);
            var entrySelector = WoffUtils.Log2(WoffUtils.MaxPower2LE(numTables));
            var rangeShift    = (ushort)(numTables * TableSize - searchRange);

            // Write the font offset table...
            // uint32:	sfntVersion	0x00010000 or 0x4F54544F ('OTTO')
            writer.WriteUInt32(_woffHeader.Flavor);
            // uint16:	numTables	Number of tables.
            writer.WriteUInt16(_woffHeader.NumTables);
            // uint16	searchRange	(Maximum power of 2 <= numTables) x 16.
            writer.WriteUInt16(searchRange);
            // uint16:	entrySelector	Log2(maximum power of 2 <= numTables).
            writer.WriteUInt16(entrySelector);
            // uint16:	rangeShift	NumTables x 16-searchRange.
            writer.WriteUInt16(rangeShift);

            uint tablesOffset = HeaderSize + numTables * TableSize;

            for (int i = 0; i < numTables; i++)
            {
                var woffDir = sortedDirs[i];
                // Tag:	tableTag	Table identifier.
                writer.WriteUInt32(woffDir.Tag);
                // uint32:	checkSum	CheckSum for this table.
                writer.WriteUInt32(woffDir.OrigChecksum);
                // Offset32	offset	Offset from beginning of TrueType font file.
                writer.WriteUInt32(tablesOffset);
                // uint32	length	Length of this table.
                writer.WriteUInt32(woffDir.OrigLength);

                tablesOffset += woffDir.OrigLength;
                if (tablesOffset % 4 != 0)
                {
                    tablesOffset += 4 - (tablesOffset % 4);
                }
            }

            // Get the buffer of the collection header and font headers...
            var fontBuffer = writer.GetBuffer();

            // For the font headers, calculate the checksum to create the font checksum
            _woffFonts[0].ChecksumAdjustment(fontBuffer, 0, (uint)fontBuffer.Length, tablesChecksum);

            //-------------------------------------------------------------------------------------------------
            // Write the actual tables data to the stream, with the required offsets...
            stream.Write(fontBuffer, 0, fontBuffer.Length);

            var paddingBuffer = new byte[4];

            for (int i = 0; i < numTables; i++)
            {
                var woffDir = sortedDirs[i];

                stream.Write(woffDir.OrigTable, 0, (int)woffDir.OrigLength);
                stream.Write(paddingBuffer, 0, (int)woffDir.Padding);
            }

            return(true);
        }
예제 #4
0
        /// <summary>
        /// This writes the transformed WOFF2 font collection data to the TTC OpenType.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        /// <remarks>
        /// The strategy is to produce a layout similar to the simple layout used in the WOFF2 data structure and
        /// still confirm to the OpenType format.
        /// <code>
        /// <![CDATA[
        ///  +---------------------+
        ///  |      TTC Header     |
        ///  +---------------------+
        ///  | All TTC Directories |
        ///  +---------------------+
        ///  |  All the Table Data |
        ///  +---------------------+
        /// ]]>
        /// </code>
        /// </remarks>
        private bool WriteWoff2Collection(Stream stream)
        {
            if (stream == null || _woffHeader == null || _woffDirs == null || _woffDirs.Count == 0)
            {
                return(false);
            }

            var numFonts = _collectionHeader.NumFonts;

            //-------------------------------------------------------------------------------------------------
            // 1. Compute the checksums, the paddings of the tables will be recalculated after the transformations.
            for (int i = 0; i < _woffDirs.Count; i++)
            {
                _woffDirs[i].RecalculateChecksum();
            }

            // Compute the sizes of the data structures to determine the offsets...
            // 2. Compute the font collection header size
            int collectionOffset = 0;

            collectionOffset += WoffBuffer.SizeOfUInt;            // TAG	  ttcTag
            collectionOffset += WoffBuffer.SizeOfUShort;          // uint16	  majorVersion
            collectionOffset += WoffBuffer.SizeOfUShort;          // uint16	  minorVersion
            collectionOffset += WoffBuffer.SizeOfUInt;            // uint32	  numFonts
            collectionOffset += WoffBuffer.SizeOfUInt * numFonts; // Offset32 tableDirectoryOffsets[numFonts]
            if (_collectionHeader.IsVersion(2))
            {
                collectionOffset += WoffBuffer.SizeOfUInt;        // uint32	dsigTag
                collectionOffset += WoffBuffer.SizeOfUInt;        // uint32	dsigLength
                collectionOffset += WoffBuffer.SizeOfUInt;        // uint32	dsigOffset
            }

            var headerOffset = (uint)collectionOffset;

            // 3. Compute for each for in the collection the (Header + Directories) size
            // and sum all to get the size/offset of the fonts
            var  directoryOffsets = new uint[numFonts];
            uint directoryOffset  = headerOffset;

            for (ushort i = 0; i < numFonts; i++)
            {
                directoryOffsets[i] = directoryOffset;
                // TableDirectory = HeaderSize + Total Table Records
                var HeaderDirSize = HeaderSize + _collectionEntries[i].NumTables * TableSize;
                directoryOffset += HeaderDirSize;
            }

            // 4. Compute for each directory in the collection the offsets
            uint[] tablesOffsets = new uint[_woffDirs.Count];
            uint   tablesOffset  = directoryOffset;

            for (ushort i = 0; i < _woffDirs.Count; i++)
            {
                var woffDir = _woffDirs[i];
                tablesOffsets[i] = tablesOffset;

                tablesOffset += woffDir.OrigLength;
                if (tablesOffset % 4 != 0)
                {
                    tablesOffset += 4 - (tablesOffset % 4);
                }
            }

            //-------------------------------------------------------------------------------------------------
            // Build the smaller headers + directories data in memory...
            var writer = new WoffWriter();

            // 1. The Font Collection File Structure
            // A font collection file consists of a single TTC Header table, one or more Table Directories
            // (each corresponding to a different font resource), and a number of OpenType tables.
            // The TTC Header must be located at the beginning of the TTC file.
            // TAG	     ttcTag	- Font Collection ID string: 'ttcf'
            writer.WriteUInt32(_collectionHeader.TtcTag);
            // uint16	majorVersion - Major version of the TTC Header.
            writer.WriteUInt16(_collectionHeader.MajorVersion);
            // uint16	minorVersion - Minor version of the TTC Header.
            writer.WriteUInt16(_collectionHeader.MinorVersion);
            // uint32	numFonts - Number of fonts in TTC
            writer.WriteUInt32(_collectionHeader.NumFonts);

            // Offset32	tableDirectoryOffsets[numFonts]	Array of offsets to the TableDirectory for each font from the beginning of the file
            for (ushort i = 0; i < numFonts; i++)
            {
                writer.WriteUInt32(directoryOffsets[i]);
            }

            // For the version 2.0 only add the empty DSIG information.
            if (_collectionHeader.IsVersion(2))
            {
                // uint32	dsigTag - Tag indicating that a DSIG table exists, 0x44534947 ('DSIG') (null if no signature)
                writer.WriteUInt32(_collectionHeader.DsigTag);
                // uint32	dsigLength - The length (in bytes) of the DSIG table (null if no signature)
                writer.WriteUInt32(_collectionHeader.DsigLength);
                // uint32	dsigOffset - The offset (in bytes) of the DSIG table from the beginning of the TTC file (null if no signature)
                writer.WriteUInt32(_collectionHeader.DsigOffset);
            }

            // Write out the header for each font
            uint fontHeaderOffset  = writer.Offset;
            var  fontHeaderOffsets = new List <uint>(numFonts + 1);

            for (ushort j = 0; j < numFonts; j++)
            {
                fontHeaderOffsets.Add(fontHeaderOffset);
                var collectionEntry = _collectionEntries[j];
                var numTables       = collectionEntry.NumTables;

                var searchRange   = (ushort)(WoffUtils.MaxPower2LE(numTables) * TableSize);
                var entrySelector = WoffUtils.Log2(WoffUtils.MaxPower2LE(numTables));
                var rangeShift    = (ushort)(numTables * TableSize - searchRange);

                // Write the font offset table...
                // uint32:	sfntVersion	0x00010000 or 0x4F54544F ('OTTO')
                writer.WriteUInt32(collectionEntry.Flavor);
                // uint16:	numTables	Number of tables.
                writer.WriteUInt16(numTables);
                // uint16	searchRange	(Maximum power of 2 <= numTables) x 16.
                writer.WriteUInt16(searchRange);
                // uint16:	entrySelector	Log2(maximum power of 2 <= numTables).
                writer.WriteUInt16(entrySelector);
                // uint16:	rangeShift	NumTables x 16-searchRange.
                writer.WriteUInt16(rangeShift);

                var sortedIndices = collectionEntry.SortedIndices;
                for (ushort i = 0; i < numTables; i++)
                {
                    var dirIndex = sortedIndices[i];
                    var woffDir  = _woffDirs[dirIndex];
                    // Tag:	tableTag	Table identifier.
                    writer.WriteUInt32(woffDir.Tag);
                    // uint32:	checkSum	CheckSum for this table.
                    writer.WriteUInt32(woffDir.OrigChecksum);
                    // Offset32	offset	Offset from beginning of TrueType font file.
                    writer.WriteUInt32(tablesOffsets[dirIndex]);
                    // uint32	length	Length of this table.
                    writer.WriteUInt32(woffDir.OrigLength);
                }

                fontHeaderOffset += writer.Offset;
            }
            fontHeaderOffsets.Add(fontHeaderOffset);

            // Get the buffer of the collection header and font headers...
            var fontBuffer = writer.GetBuffer();

            // For the font headers, calculate the checksum to create the font checksum
            for (ushort i = 0; i < numFonts; i += 2)
            {
                uint fontOffset = fontHeaderOffsets[i];
                uint fontLength = fontHeaderOffsets[i + 1] - fontOffset;

                _woffFonts[i].ChecksumAdjustment(fontBuffer, fontOffset, fontLength);
            }

            //-------------------------------------------------------------------------------------------------
            // Write the actual tables data to the stream, with the required offsets...
            stream.Write(fontBuffer, 0, fontBuffer.Length);

            var paddingBuffer = new byte[4];

            for (int i = 0; i < _woffDirs.Count; i++)
            {
                var woffDir = _woffDirs[i];

                stream.Write(woffDir.OrigTable, 0, (int)woffDir.OrigLength);
                stream.Write(paddingBuffer, 0, (int)woffDir.Padding);
            }

            return(true);
        }
예제 #5
0
        private bool WriteWoff1(Stream stream)
        {
            if (stream == null || _woffHeader == null || _woffDirs == null || _woffDirs.Count == 0)
            {
                return(false);
            }

            var numTables     = _woffHeader.NumTables;
            var searchRange   = (ushort)(WoffUtils.MaxPower2LE(numTables) * TableSize);
            var entrySelector = WoffUtils.Log2(WoffUtils.MaxPower2LE(numTables));
            var rangeShift    = (ushort)(numTables * TableSize - searchRange);

            int offset = 0;
            // Write the font offset table...
            var headerBuffer = new byte[HeaderSize];

            // uint32:	sfntVersion	0x00010000 or 0x4F54544F ('OTTO')
            offset += WoffBuffer.WriteUInt32BE(headerBuffer, offset, _woffHeader.Flavor);
            // uint16:	numTables	Number of tables.
            offset += WoffBuffer.WriteUInt16BE(headerBuffer, offset, _woffHeader.NumTables);
            // uint16	searchRange	(Maximum power of 2 <= numTables) x 16.
            offset += WoffBuffer.WriteUInt16BE(headerBuffer, offset, searchRange);
            // uint16:	entrySelector	Log2(maximum power of 2 <= numTables).
            offset += WoffBuffer.WriteUInt16BE(headerBuffer, offset, entrySelector);
            // uint16:	rangeShift	NumTables x 16-searchRange.
            offset += WoffBuffer.WriteUInt16BE(headerBuffer, offset, rangeShift);

            stream.Write(headerBuffer, 0, (int)HeaderSize);

            uint tablesOffset = HeaderSize + numTables * TableSize;

            var tableBuffer = new byte[TableSize];

            for (int i = 0; i < numTables; i++)
            {
                offset = 0;
                var woffDir = _woffDirs[i];
                // Tag:	tableTag	Table identifier.
                offset += WoffBuffer.WriteUInt32BE(tableBuffer, offset, woffDir.Tag);
                // uint32:	checkSum	CheckSum for this table.
                offset += WoffBuffer.WriteUInt32BE(tableBuffer, offset, woffDir.OrigChecksum);
                // Offset32	offset	Offset from beginning of TrueType font file.
                offset += WoffBuffer.WriteUInt32BE(tableBuffer, offset, tablesOffset);
                // uint32	length	Length of this table.
                offset += WoffBuffer.WriteUInt32BE(tableBuffer, offset, woffDir.OrigLength);

                stream.Write(tableBuffer, 0, (int)TableSize);

                tablesOffset += woffDir.OrigLength;
                if (tablesOffset % 4 != 0)
                {
                    tablesOffset += 4 - (tablesOffset % 4);
                }
            }

            var paddingBuffer = new byte[4];

            for (int i = 0; i < numTables; i++)
            {
                var woffDir = _woffDirs[i];

                stream.Write(woffDir.OrigTable, 0, (int)woffDir.OrigLength);
                stream.Write(paddingBuffer, 0, (int)woffDir.Padding);
            }

            return(true);
        }