Ejemplo n.º 1
0
        private bool SerializeGlyf(ushort indexFormat)
        {
            Debug.Assert(_glyphs != null && _glyphs.Length != 0);
            Debug.Assert(indexFormat == 0 || indexFormat == 1);

            uint locaOffset  = 0;
            var  glyphWriter = new WoffWriter();

            uint maxCapacity  = 5 * _maxNumPoints;                 // Size without the application of packing.
            var  pointsWriter = new WoffIndexer((int)maxCapacity); // Created here for reuse

            int glyphCount = _glyphs.Length;

            for (ushort i = 0; i < glyphCount; i++)
            {
                var  glyph     = _glyphs[i];
                uint glyphSize = 0;
                pointsWriter.Offset = 0; // Reset the offset, it is non-resizable buffer (size is fixed).
                if (glyph.Serialize(glyphWriter, pointsWriter, out glyphSize) == false)
                {
                    Trace.TraceError("Glyph Serialization Error: Glyph Index = {0}.", glyph.GlyphIndex);
                    return(false);
                }
                _glyphsLocations.Add(locaOffset);
                locaOffset += glyphSize;
            }

            var glyphBuffer = glyphWriter.GetBuffer();

            _woffDir.OrigTable  = glyphBuffer;
            _woffDir.OrigLength = (uint)glyphBuffer.Length;

            return(true);
        }
Ejemplo n.º 2
0
 public byte this[ushort index]
 {
     get {
         if (_writer != null)
         {
             var buffer = _writer.GetBuffer();
             return(buffer[index]);
         }
         throw new InvalidOperationException();
     }
     set {
         if (_writer != null)
         {
             _writer.Offset = index;
             _writer.Write(value);
         }
     }
 }
Ejemplo n.º 3
0
        private bool SerializeHmtx(IList <ushort> advanceWidths, IList <short> lsbs, int numOfHMetrics, int numOfGlyphs)
        {
            var hmtxBufferSize = 2 * numOfGlyphs + 2 * numOfHMetrics;

            var writer = new WoffWriter(hmtxBufferSize);

            for (int i = 0; i < numOfGlyphs; i++)
            {
                if (i < numOfHMetrics)
                {
                    writer.WriteUInt16(advanceWidths[i]);
                }
                writer.WriteInt16(lsbs[i]);
            }

            var tableBuffer = writer.GetBuffer();
            var hmtxBuffer  = new WoffBuffer(tableBuffer);

            _woffDir.OrigTable  = tableBuffer;
            _woffDir.OrigLength = (uint)tableBuffer.Length;

            return(true);
        }
Ejemplo n.º 4
0
        private bool SerializeLoca()
        {
            var glyfTable = _woffFont.Tables[_glyfIndex] as WoffTableGlyf;

            Debug.Assert(glyfTable != null);
            if (glyfTable == null)
            {
                return(false);
            }

            // indexToLocFormat	0 for short offsets (Offset16), 1 for long (Offset32).
            ushort indexFormat = glyfTable.IndexFormat;

            if (indexFormat != 0 && indexFormat != 1)
            {
                return(false);
            }

            var glyphs          = glyfTable.Glyphs;
            var glyphsLocations = glyfTable.GlyphsLocations;

            var  locaWriter = new WoffWriter();
            uint locaOffset = 0;

            int glyphCount = glyphs.Length;

            for (ushort i = 0; i < glyphCount; i++)
            {
                locaOffset = glyphsLocations[i];

                // indexToLocFormat	0 for short offsets (Offset16), 1 for long (Offset32).
                if (indexFormat == 1)
                {
                    locaWriter.WriteUInt32(locaOffset);
                }
                else
                {
                    var shortOffset = (ushort)(locaOffset >> 1);
                    locaWriter.WriteUInt16(shortOffset);
                }
            }

            //if (locaOffset != glyfDir.OrigLength) //TODO: Needed?
            locaOffset = glyfTable.Length;
            if (indexFormat == 1)
            {
                locaWriter.WriteUInt32(locaOffset);
            }
            else
            {
                var shortOffset = (ushort)(locaOffset >> 1);
                locaWriter.WriteUInt16(shortOffset);
            }

            var locaBuffer = locaWriter.GetBuffer();

            _woffDir.OrigTable  = locaBuffer;
            _woffDir.OrigLength = (uint)locaBuffer.Length;

            return(true);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }