Exemple #1
0
 public WoffFont(WoffHeader woffHeader, CollectionHeader collHeader, CollectionFontEntry collEntry)
     : this(woffHeader)
 {
     _collectionHeader = collHeader;
     _collectionEntry  = collEntry;
 }
        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);
        }