Пример #1
0
            public bool Read(Stream stream, int directoryCount)
            {
                int bytesRead = 0;

                this.NumTables = WoffBuffer.Read255UInt16(stream);
                if (this.NumTables == 0)
                {
                    return(false);
                }

                this.Flavor = WoffBuffer.ReadUInt32BE(WoffBuffer.ReadBytes(stream, WoffBuffer.SizeOfUInt, out bytesRead), 0);

                this.TableIndices = new ushort[this.NumTables];

                for (ushort i = 0; i < this.NumTables; i++)
                {
                    this.TableIndices[i] = WoffBuffer.Read255UInt16(stream);
                    if (this.TableIndices[i] >= directoryCount)
                    {
                        Trace.TraceError("Invalid collection font entry: " + this.TableIndices[i]);
                        return(false);
                    }
                }
                return(true);
            }
Пример #2
0
            public bool Read(Stream stream)
            {
                int bytesRead = 0;

                this.Version = WoffBuffer.ReadUInt32BE(WoffBuffer.ReadBytes(stream,
                                                                            WoffBuffer.SizeOfUInt, out bytesRead), 0);
                this.NumFonts = WoffBuffer.Read255UInt16(stream);

                if (this.Version == 0x00010000 || this.Version == 0x00020000)
                {
                    TtcTag            = WoffUtils.TtcSignature;
                    this.MajorVersion = (ushort)(this.Version == 0x00010000 ? 1 : 2);
                    return(true);
                }

                return(false);
            }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
0
        public bool Read(Stream stream)
        {
            if (stream == null)
            {
                return(false);
            }
            Debug.Assert(_woffVersion == WoffUtils.Woff1Version || _woffVersion == WoffUtils.Woff2Version);

            var headerSize = this.HeaderSize;

            var header = new byte[headerSize];

            var sizeRead = stream.Read(header, 0, headerSize);

            Debug.Assert(sizeRead == headerSize);

            var bufferSize = header.Length;

            _signature = WoffBuffer.ReadUInt32BE(header, 0);
            _flavor    = WoffBuffer.ReadUInt32BE(header, 4);
            _length    = WoffBuffer.ReadUInt32BE(header, 8);

            _numTables = WoffBuffer.ReadUInt16BE(header, 12);
            _reserved  = WoffBuffer.ReadUInt16BE(header, 14);

            _totalSfntSize = WoffBuffer.ReadUInt32BE(header, 16);
            if (_woffVersion == WoffUtils.Woff1Version)
            {
                _majorVersion = WoffBuffer.ReadUInt16BE(header, 20);
                _minorVersion = WoffBuffer.ReadUInt16BE(header, 22);

                _metaOffset     = WoffBuffer.ReadUInt32BE(header, 24);
                _metaLength     = WoffBuffer.ReadUInt32BE(header, 28);
                _metaOrigLength = WoffBuffer.ReadUInt32BE(header, 32);
                _privateOffset  = WoffBuffer.ReadUInt32BE(header, 36);
                _privateLength  = WoffBuffer.ReadUInt32BE(header, 40);
            }
            else
            {
                _totalCompressedSize = WoffBuffer.ReadUInt32BE(header, 20);

                _majorVersion = WoffBuffer.ReadUInt16BE(header, 24);
                _minorVersion = WoffBuffer.ReadUInt16BE(header, 26);

                _metaOffset     = WoffBuffer.ReadUInt32BE(header, 28);
                _metaLength     = WoffBuffer.ReadUInt32BE(header, 32);
                _metaOrigLength = WoffBuffer.ReadUInt32BE(header, 36);
                _privateOffset  = WoffBuffer.ReadUInt32BE(header, 40);
                _privateLength  = WoffBuffer.ReadUInt32BE(header, 44);
            }

            // The signature field in the WOFF-1 header must contain the "magic number" 0x774F4646.
            // If the field does not contain this value, user agents must reject the file as invalid.
            Debug.Assert((bufferSize == WoffUtils.Woff1HeaderSize)
                ? _signature == WoffUtils.Woff1Signature : _signature == WoffUtils.Woff2Signature);

            // The header includes a reserved field; this must be set to zero.
            // If this field is non-zero, a conforming user agent must reject the file as invalid.
            Debug.Assert(_reserved == 0);

            return(true);
        }