Example #1
0
        private bool ReadWoff2Font(Stream stream)
        {
            var woffFont = new WoffFont(_woffHeader);

            _woffFonts.Add(woffFont);
            woffFont.BeginDirectory(_woffVersion);

            // 2. TableDirectory: Directory of font tables, containing size and other info.
            for (ushort i = 0; i < _woffHeader.NumTables; i++)
            {
                var woffDir = _woffDirs[i];

                woffFont.AddDirectory(woffDir);
            }

            // 4. CompressedFontData: Contents of font tables, compressed for storage in the WOFF2 file.
            int bytesRead  = 0;
            int bytesCount = (int)_woffHeader.TotalCompressedSize;

            if (bytesCount != 0)
            {
                byte[] compressedBuffer = WoffBuffer.ReadBytes(stream, bytesCount, out bytesRead);
                Debug.Assert(bytesRead == bytesCount);
                if (bytesRead != bytesCount)
                {
                    return(false);
                }

                bool errorOccurred = false;

                var memoryStream = new MemoryStream(compressedBuffer);

                using (var brotliStream = new BrotliInputStream(memoryStream))
                {
                    var decompressedStream = new MemoryStream();
                    brotliStream.CopyTo(decompressedStream);
                    decompressedStream.Seek(0, SeekOrigin.Begin);

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

                        try
                        {
                            // bytesCount = (int)woffDir.TransformLength;
                            bytesCount = (int)woffDir.CompLength;

                            decompressedStream.Seek(woffDir.Offset, SeekOrigin.Begin);

                            var tableBuffer = WoffBuffer.ReadBytes(decompressedStream, bytesCount, out bytesRead);

                            Debug.Assert(bytesRead == bytesCount);

                            if (bytesRead != bytesCount)
                            {
                                return(false);
                            }

                            woffDir.CompTable = tableBuffer;
                            woffDir.OrigTable = tableBuffer;
                        }
                        catch (Exception ex)
                        {
                            errorOccurred = true;
                            Trace.TraceError(ex.Message);
                        }
                    }
                }

                if (errorOccurred)
                {
                    return(false);
                }
            }

            woffFont.EndDirectory();
            return(true);
        }
Example #2
0
        private bool ReadWoff2Fonts(Stream stream)
        {
            // 1. Read the collection font header
            _collectionHeader = new CollectionHeader();
            if (_collectionHeader.Read(stream) == false)
            {
                Trace.TraceError("Collection Font Error: Reading of collection header failed.");
                return(false);
            }

            // 2. Read this collection font entries
            var numFonts = _collectionHeader.NumFonts;

            _collectionEntries = new List <CollectionFontEntry>(numFonts);
            for (ushort i = 0; i < numFonts; i++)
            {
                var collectionEntry = new CollectionFontEntry(i);
                if (collectionEntry.Read(stream, _woffDirs.Count) == false)
                {
                    return(false);
                }
                _collectionEntries.Add(collectionEntry);
            }

            // 3. TableDirectory: Directory of font tables, containing size and other info.
            for (ushort i = 0; i < numFonts; i++)
            {
                var collectionEntry = _collectionEntries[i];

                var woffFont = new WoffFont(_woffHeader, _collectionHeader, collectionEntry);
                _woffFonts.Add(woffFont);
                woffFont.BeginDirectory(_woffVersion);

                for (ushort j = 0; j < collectionEntry.NumTables; j++)
                {
                    var index = collectionEntry.TableIndices[j];

                    var woffDir = _woffDirs[index];

                    woffFont.AddDirectory(woffDir);
                }
            }

            // 4. CompressedFontData: Contents of font tables, compressed for storage in the WOFF2 file.
            int bytesRead  = 0;
            int bytesCount = (int)_woffHeader.TotalCompressedSize;

            if (bytesCount != 0)
            {
                byte[] compressedBuffer = WoffBuffer.ReadBytes(stream, bytesCount, out bytesRead);
                Debug.Assert(bytesRead == bytesCount);
                if (bytesRead != bytesCount)
                {
                    return(false);
                }

                bool errorOccurred = false;

                var memoryStream = new MemoryStream(compressedBuffer);

                using (var brotliStream = new BrotliInputStream(memoryStream))
                {
                    var decompressedStream = new MemoryStream();
                    brotliStream.CopyTo(decompressedStream);
                    decompressedStream.Seek(0, SeekOrigin.Begin);

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

                        try
                        {
                            // bytesCount = (int)woffDir.TransformLength;
                            bytesCount = (int)woffDir.CompLength;

                            decompressedStream.Seek(woffDir.Offset, SeekOrigin.Begin);

                            var tableBuffer = WoffBuffer.ReadBytes(decompressedStream, bytesCount, out bytesRead);

                            Debug.Assert(bytesRead == bytesCount);

                            if (bytesRead != bytesCount)
                            {
                                return(false);
                            }

                            woffDir.CompTable = tableBuffer;
                            woffDir.OrigTable = tableBuffer;
                        }
                        catch (Exception ex)
                        {
                            errorOccurred = true;
                            Trace.TraceError(ex.Message);
                        }
                    }
                }

                if (errorOccurred)
                {
                    return(false);
                }
            }

            for (ushort i = 0; i < numFonts; i++)
            {
                var collectionEntry = _collectionEntries[i];

                var woffFont = _woffFonts[i];
                _woffFonts.Add(woffFont);
                woffFont.EndDirectory();
            }

            return(true);
        }
Example #3
0
        private bool ReadWoff1(Stream stream)
        {
            _woffVersion = WoffUtils.Woff1Version;

            var woffFont = new WoffFont(_woffHeader);

            _woffFonts.Add(woffFont);
            woffFont.BeginDirectory(_woffVersion);

            _woffHeader = new WoffHeader(WoffUtils.Woff1Version);
            _woffDirs   = null;

            int sizeRead = 0;

            sizeRead = _woffHeader.HeaderSize;

            Debug.Assert(sizeRead == WoffUtils.Woff1HeaderSize);

            if (!_woffHeader.Read(stream))
            {
                return(false);
            }

            _woffDirs = new List <WoffTableDirectory>(_woffHeader.NumTables);

            // 2. TableDirectory: Directory of font tables, containing size and other info.
            for (ushort i = 0; i < _woffHeader.NumTables; i++)
            {
                var woffDir = new WoffTableDirectory(i, _woffVersion);
                if (woffDir.Read(stream))
                {
                    _woffDirs.Add(woffDir);

                    woffFont.AddDirectory(woffDir);
                }
            }

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

                stream.Seek(woffDir.Offset, SeekOrigin.Begin);

                int bytesRead  = 0;
                int bytesCount = (int)woffDir.CompLength;
                if (bytesCount == 0)
                {
                    continue;
                }
                var tableBuffer = WoffBuffer.ReadBytes(stream, bytesCount, out bytesRead);

                Debug.Assert(bytesRead == bytesCount);

                if (bytesRead != bytesCount)
                {
                    return(false);
                }

                woffDir.CompTable = tableBuffer;
                if (woffDir.CompLength == woffDir.OrigLength)
                {
                    // table data is not compressed
                    woffDir.OrigTable = tableBuffer;
                }
                else
                {
                    bytesCount = (int)woffDir.OrigLength;
                    var origBuffer = new byte[bytesCount];

                    using (var zlibStream = new ZLibStream(new MemoryStream(tableBuffer),
                                                           CompressionMode.Decompress, false))
                    {
                        int bytesStart = 0;

                        do
                        {
                            bytesRead = zlibStream.Read(origBuffer, bytesStart, bytesCount);
                            if (bytesRead == 0)
                            {
                                break;
                            }
                            bytesStart += bytesRead;
                            bytesCount -= bytesRead;
                        } while (bytesCount > 0);
                    }

                    woffDir.OrigTable = origBuffer;
                }
            }

            _metadata = new WoffMetadata(_woffHeader.MetaOffset,
                                         _woffHeader.MetaLength, _woffHeader.MetaOrigLength);
            if (_woffHeader.HasMetadata)
            {
                stream.Seek(_woffHeader.MetaOffset, SeekOrigin.Begin);

                int bytesRead  = 0;
                int bytesCount = (int)_woffHeader.MetaLength;

                var metaBuffer = WoffBuffer.ReadBytes(stream, bytesCount, out bytesRead);

                Debug.Assert(bytesRead == bytesCount);

                if (bytesRead != bytesCount)
                {
                    return(false);
                }

                _metadata.Data     = metaBuffer;
                _metadata.OrigData = metaBuffer;

                if (_woffHeader.MetaLength != _woffHeader.MetaOrigLength)
                {
                    bytesCount = (int)_woffHeader.MetaOrigLength;
                    var origBuffer = new byte[bytesCount];

                    using (var zlibStream = new ZLibStream(new MemoryStream(metaBuffer),
                                                           CompressionMode.Decompress, false))
                    {
                        int bytesStart = 0;

                        do
                        {
                            bytesRead = zlibStream.Read(origBuffer, bytesStart, bytesCount);
                            if (bytesRead == 0)
                            {
                                break;
                            }
                            bytesStart += bytesRead;
                            bytesCount -= bytesRead;
                        } while (bytesCount > 0);
                    }

                    _metadata.OrigData = origBuffer;
                }
            }

            _privateData = new WoffPrivateData(_woffHeader.PrivateOffset, _woffHeader.PrivateLength);
            if (_woffHeader.HasPrivateData)
            {
                stream.Seek(_woffHeader.PrivateOffset, SeekOrigin.Begin);

                int bytesRead  = 0;
                int bytesCount = (int)_woffHeader.PrivateLength;

                var privateBuffer = WoffBuffer.ReadBytes(stream, bytesCount, out bytesRead);

                Debug.Assert(bytesRead == bytesCount);

                if (bytesRead != bytesCount)
                {
                    return(false);
                }

                _privateData.Data = privateBuffer;
            }

            woffFont.EndDirectory();

            return(true);
        }