コード例 #1
0
ファイル: WoffReader.cs プロジェクト: ywscr/Typography
        internal bool Read(Typeface typeface, BinaryReader reader, RestoreTicket ticket)
        {
            //WOFF File
            //WOFFHeader        File header with basic font type and version, along with offsets to metadata and private data blocks.
            //TableDirectory    Directory of font tables, indicating the original size, compressed size and location of each table within the WOFF file.
            //FontTables        The font data tables from the input sfnt font, compressed to reduce bandwidth requirements.
            //ExtendedMetadata  An optional block of extended metadata, represented in XML format and compressed for storage in the WOFF file.
            //PrivateData       An optional block of private data for the font designer, foundry, or vendor to use.
            _header = ReadWOFFHeader(reader);
            if (_header == null)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine("can't read ");
#endif
                return(false);
            }

            //
            WoffTableDirectory[] woffTableDirs = ReadTableDirectories(reader);
            if (woffTableDirs == null)
            {
                return(false);
            }
            //
            //try read each compressed table
            if (DecompressHandler == null)
            {
                if (WoffDefaultZlibDecompressFunc.DecompressHandler != null)
                {
                    DecompressHandler = WoffDefaultZlibDecompressFunc.DecompressHandler;
                }
                else
                {
#if DEBUG
                    System.Diagnostics.Debug.WriteLine("no Zlib DecompressHandler ");
#endif
                    return(false);
                }
            }

            TableEntryCollection tableEntryCollection = CreateTableEntryCollection(woffTableDirs);

            using (MemoryStream decompressStream = new MemoryStream())
            {
                if (Extract(reader, woffTableDirs, decompressStream))
                {
                    using (ByteOrderSwappingBinaryReader reader2 = new ByteOrderSwappingBinaryReader(decompressStream))
                    {
                        decompressStream.Position = 0;
                        OpenFontReader openFontReader = new OpenFontReader();
                        return(openFontReader.ReadTableEntryCollection(typeface, ticket, tableEntryCollection, reader2));
                    }
                }
            }
            return(false);
        }
コード例 #2
0
ファイル: WoffReader.cs プロジェクト: ywscr/Typography
        static WoffHeader ReadWOFFHeader(BinaryReader reader)
        {
            //WOFFHeader
            //UInt32  signature         0x774F4646 'wOFF'
            //UInt32  flavor            The "sfnt version" of the input font.
            //UInt32  length            Total size of the WOFF file.
            //UInt16  numTables         Number of entries in directory of font tables.
            //UInt16  reserved          Reserved; set to zero.
            //UInt32  totalSfntSize     Total size needed for the uncompressed font data, including the sfnt header, directory, and font tables(including padding).
            //UInt16  majorVersion      Major version of the WOFF file.
            //UInt16  minorVersion      Minor version of the WOFF file.
            //UInt32  metaOffset        Offset to metadata block, from beginning of WOFF file.
            //UInt32  metaLength        Length of compressed metadata block.
            //UInt32  metaOrigLength    Uncompressed size of metadata block.
            //UInt32  privOffset        Offset to private data block, from beginning of WOFF file.
            //UInt32  privLength        Length of private data block.


            //signature
            byte b0 = reader.ReadByte();
            byte b1 = reader.ReadByte();
            byte b2 = reader.ReadByte();
            byte b3 = reader.ReadByte();

            if (!(b0 == 0x77 && b1 == 0x4f && b2 == 0x46 && b3 == 0x46))
            {
                return(null);
            }
            WoffHeader header = new WoffHeader();

            header.flavor    = reader.ReadUInt32();
            header.length    = reader.ReadUInt32();
            header.numTables = reader.ReadUInt16();
            ushort reserved = reader.ReadUInt16();

            header.totalSfntSize = reader.ReadUInt32();

            header.majorVersion = reader.ReadUInt16();
            header.minorVersion = reader.ReadUInt16();

            header.metaOffset         = reader.ReadUInt32();
            header.metaLength         = reader.ReadUInt32();
            header.metaOriginalLength = reader.ReadUInt32();

            header.privOffset = reader.ReadUInt32();
            header.privLength = reader.ReadUInt32();

            return(header);
        }
コード例 #3
0
ファイル: WoffReader.cs プロジェクト: ywscr/Typography
        public PreviewFontInfo ReadPreview(BinaryReader reader)
        {
            //read preview only
            //WOFF File
            //WOFFHeader        File header with basic font type and version, along with offsets to metadata and private data blocks.
            //TableDirectory    Directory of font tables, indicating the original size, compressed size and location of each table within the WOFF file.
            //FontTables        The font data tables from the input sfnt font, compressed to reduce bandwidth requirements.
            //ExtendedMetadata  An optional block of extended metadata, represented in XML format and compressed for storage in the WOFF file.
            //PrivateData       An optional block of private data for the font designer, foundry, or vendor to use.

            PreviewFontInfo fontPreviewInfo = null;

            _header = ReadWOFFHeader(reader);
            if (_header == null)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine("can't read ");
#endif
                return(null); //notify user too
            }

            //
            WoffTableDirectory[] woffTableDirs = ReadTableDirectories(reader);
            if (woffTableDirs == null)
            {
                return(null);
            }
            //try read each compressed table
            if (DecompressHandler == null)
            {
                if (WoffDefaultZlibDecompressFunc.DecompressHandler != null)
                {
                    DecompressHandler = WoffDefaultZlibDecompressFunc.DecompressHandler;
                }
                else
                {
#if DEBUG
                    System.Diagnostics.Debug.WriteLine("no Zlib DecompressHandler ");
#endif
                    return(null); //notify user too
                }
            }


            TableEntryCollection tableEntryCollection = CreateTableEntryCollection(woffTableDirs);
            //for font preview, we may not need to extract
            using (MemoryStream decompressStream = new MemoryStream())
            {
                if (Extract(reader, woffTableDirs, decompressStream))
                {
                    using (ByteOrderSwappingBinaryReader reader2 = new ByteOrderSwappingBinaryReader(decompressStream))
                    {
                        decompressStream.Position = 0;
                        OpenFontReader  openFontReader  = new OpenFontReader();
                        PreviewFontInfo previewFontInfo = openFontReader.ReadPreviewFontInfo(tableEntryCollection, reader2);
                        if (previewFontInfo != null)
                        {
                            //add webfont info to this preview font
                            previewFontInfo.IsWebFont = true;
                        }
                        return(previewFontInfo);
                    }
                }
            }

            return(fontPreviewInfo);
        }