Ejemplo n.º 1
0
 public static PosLookupRecord[] ReadPosLookupRecords(RandomAccessFileOrArray rf, int recordCount)
 {
     PosLookupRecord[] posLookUpRecords = new PosLookupRecord[recordCount];
     for (int i = 0; i < recordCount; ++i)
     {
         PosLookupRecord lookupRecord = new PosLookupRecord();
         lookupRecord.sequenceIndex   = rf.ReadUnsignedShort();
         lookupRecord.lookupListIndex = rf.ReadUnsignedShort();
         posLookUpRecords[i]          = lookupRecord;
     }
     return(posLookUpRecords);
 }
Ejemplo n.º 2
0
 public static SubstLookupRecord[] ReadSubstLookupRecords(RandomAccessFileOrArray rf, int substCount)
 {
     SubstLookupRecord[] substPosLookUpRecords = new SubstLookupRecord[substCount];
     for (int i = 0; i < substCount; ++i)
     {
         SubstLookupRecord slr = new SubstLookupRecord();
         slr.sequenceIndex        = rf.ReadUnsignedShort();
         slr.lookupListIndex      = rf.ReadUnsignedShort();
         substPosLookUpRecords[i] = slr;
     }
     return(substPosLookUpRecords);
 }
Ejemplo n.º 3
0
        /// <summary>Constructs a TIFFDirectory by reading a SeekableStream.</summary>
        /// <remarks>
        /// Constructs a TIFFDirectory by reading a SeekableStream.
        /// The ifd_offset parameter specifies the stream offset from which
        /// to begin reading; this mechanism is sometimes used to store
        /// private IFDs within a TIFF file that are not part of the normal
        /// sequence of IFDs.
        /// </remarks>
        /// <param name="stream">a SeekableStream to read from.</param>
        /// <param name="ifd_offset">the long byte offset of the directory.</param>
        /// <param name="directory">
        /// the index of the directory to read beyond the
        /// one at the current stream offset; zero indicates the IFD
        /// at the current offset.
        /// </param>
        /// <exception cref="System.IO.IOException"/>
        public TIFFDirectory(RandomAccessFileOrArray stream, long ifd_offset, int directory)
        {
            long global_save_offset = stream.GetPosition();

            stream.Seek(0L);
            int endian = stream.ReadUnsignedShort();

            if (!IsValidEndianTag(endian))
            {
                throw new iText.IO.IOException(iText.IO.IOException.BadEndiannessTagNot0x4949Or0x4d4d);
            }
            isBigEndian = endian == 0x4d4d;
            // Seek to the first IFD.
            stream.Seek(ifd_offset);
            // Seek to desired IFD if necessary.
            int dirNum = 0;

            while (dirNum < directory)
            {
                // Get the number of fields in the current IFD.
                int numEntries = ReadUnsignedShort(stream);
                // Skip to the next IFD offset value field.
                stream.Seek(ifd_offset + 12 * numEntries);
                // Read the offset to the next IFD beyond this one.
                ifd_offset = ReadUnsignedInt(stream);
                // Seek to the next IFD.
                stream.Seek(ifd_offset);
                // Increment the directory.
                dirNum++;
            }
            Initialize(stream);
            stream.Seek(global_save_offset);
        }
Ejemplo n.º 4
0
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual TagAndLocation[] ReadTagAndLocations(int baseLocation)
        {
            int count = rf.ReadUnsignedShort();

            TagAndLocation[] tagslLocs = new TagAndLocation[count];
            for (int k = 0; k < count; ++k)
            {
                TagAndLocation tl = new TagAndLocation();
                tl.tag       = rf.ReadString(4, "utf-8");
                tl.location  = rf.ReadUnsignedShort() + baseLocation;
                tagslLocs[k] = tl;
            }
            return(tagslLocs);
        }
Ejemplo n.º 5
0
 public static int[] ReadUShortArray(RandomAccessFileOrArray rf, int size, int location)
 {
     int[] ret = new int[size];
     for (int k = 0; k < size; ++k)
     {
         int offset = rf.ReadUnsignedShort();
         ret[k] = offset == 0 ? offset : offset + location;
     }
     return(ret);
 }
Ejemplo n.º 6
0
 private int ReadUnsignedShort(RandomAccessFileOrArray stream)
 {
     if (isBigEndian)
     {
         return(stream.ReadUnsignedShort());
     }
     else
     {
         return(stream.ReadUnsignedShortLE());
     }
 }
 private static int readUnsignedShort(RandomAccessFileOrArray stream,
                                      bool isBigEndian)
 {
     if (isBigEndian)
     {
         return(stream.ReadUnsignedShort());
     }
     else
     {
         return(stream.ReadUnsignedShortLe());
     }
 }
 /// <exception cref="System.IO.IOException"/>
 public virtual void ReadTable()
 {
     if (tableLocation > 0)
     {
         rf.Seek(tableLocation);
         rf.ReadUnsignedInt();
         //version, we only support 0x00010000
         int glyphClassDefOffset = rf.ReadUnsignedShort();
         rf.ReadUnsignedShort();
         //skip Attachment Point List Table
         rf.ReadUnsignedShort();
         //skip Ligature Caret List Table
         int markAttachClassDefOffset = rf.ReadUnsignedShort();
         if (glyphClassDefOffset > 0)
         {
             glyphClass = new OtfClass(rf, glyphClassDefOffset + tableLocation);
         }
         if (markAttachClassDefOffset > 0)
         {
             markAttachmentClass = new OtfClass(rf, markAttachClassDefOffset + tableLocation);
         }
     }
 }
Ejemplo n.º 9
0
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual void CreateTableDirectory()
        {
            tableDirectory = new Dictionary <String, int[]>();
            rf.Seek(directoryOffset);
            int id = rf.ReadInt();

            if (id != 0x00010000)
            {
                throw new iText.IO.IOException(iText.IO.IOException.NotAtTrueTypeFile).SetMessageParams(fileName);
            }
            int num_tables = rf.ReadUnsignedShort();

            rf.SkipBytes(6);
            for (int k = 0; k < num_tables; ++k)
            {
                String tag           = ReadStandardString(4);
                int[]  tableLocation = new int[3];
                tableLocation[TABLE_CHECKSUM] = rf.ReadInt();
                tableLocation[TABLE_OFFSET]   = rf.ReadInt();
                tableLocation[TABLE_LENGTH]   = rf.ReadInt();
                tableDirectory.Put(tag, tableLocation);
            }
        }
Ejemplo n.º 10
0
        /**
         * Constructs a TIFFDirectory from a SeekableStream.
         * The directory parameter specifies which directory to read from
         * the linked list present in the stream; directory 0 is normally
         * read but it is possible to store multiple images in a single
         * TIFF file by maintaing multiple directories.
         *
         * @param stream a SeekableStream to read from.
         * @param directory the index of the directory to read.
         */
        public TIFFDirectory(RandomAccessFileOrArray stream, int directory)
        {
            long global_save_offset = stream.FilePointer;
            long ifd_offset;

            // Read the TIFF header
            stream.Seek(0L);
            int endian = stream.ReadUnsignedShort();

            if (!IsValidEndianTag(endian))
            {
                throw new
                      ArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
            }
            isBigEndian = (endian == 0x4d4d);

            int magic = ReadUnsignedShort(stream);

            if (magic != 42)
            {
                throw new
                      ArgumentException("Bad magic number, should be 42.");
            }

            // Get the initial ifd offset as an unsigned int (using a long)
            ifd_offset = ReadUnsignedInt(stream);

            for (int i = 0; i < directory; i++)
            {
                if (ifd_offset == 0L)
                {
                    throw new
                          ArgumentException("Directory number too large.");
                }

                stream.Seek(ifd_offset);
                int entries = ReadUnsignedShort(stream);
                stream.Skip(12 * entries);

                ifd_offset = ReadUnsignedInt(stream);
            }

            stream.Seek(ifd_offset);
            Initialize(stream);
            stream.Seek(global_save_offset);
        }
        /// <summary>
        /// Returns the number of image directories (subimages) stored in a
        /// given TIFF file, represented by a  SeekableStream .
        /// </summary>
        public static int GetNumDirectories(RandomAccessFileOrArray stream)
        {
            long pointer = stream.FilePointer; // Save stream pointer

            stream.Seek(0L);
            var endian = stream.ReadUnsignedShort();

            if (!isValidEndianTag(endian))
            {
                throw new InvalidOperationException("Bad endianness tag (not 0x4949 or 0x4d4d).");
            }
            var isBigEndian = (endian == 0x4d4d);
            var magic       = readUnsignedShort(stream, isBigEndian);

            if (magic != 42)
            {
                throw new InvalidOperationException("Bad magic number, should be 42.");
            }

            stream.Seek(4L);
            var offset = readUnsignedInt(stream, isBigEndian);

            var numDirectories = 0;

            while (offset != 0L)
            {
                ++numDirectories;

                // EOFException means IFD was probably not properly terminated.
                try
                {
                    stream.Seek(offset);
                    var entries = readUnsignedShort(stream, isBigEndian);
                    stream.Skip(12 * entries);
                    offset = readUnsignedInt(stream, isBigEndian);
                }
                catch (EndOfStreamException)
                {
                    //numDirectories--;
                    break;
                }
            }

            stream.Seek(pointer); // Reset stream pointer
            return(numDirectories);
        }
Ejemplo n.º 12
0
        // Utilities

        /**
         * Returns the number of image directories (subimages) stored in a
         * given TIFF file, represented by a <code>SeekableStream</code>.
         */
        public static int GetNumDirectories(RandomAccessFileOrArray stream)
        {
            long pointer = stream.FilePointer; // Save stream pointer

            stream.Seek(0L);
            int endian = stream.ReadUnsignedShort();

            if (!IsValidEndianTag(endian))
            {
                throw new ArgumentException(MessageLocalization.GetComposedMessage("bad.endianness.tag.not.0x4949.or.0x4d4d"));
            }
            bool isBigEndian = (endian == 0x4d4d);
            int  magic       = ReadUnsignedShort(stream, isBigEndian);

            if (magic != 42)
            {
                throw new ArgumentException(MessageLocalization.GetComposedMessage("bad.magic.number.should.be.42"));
            }

            stream.Seek(4L);
            long offset = ReadUnsignedInt(stream, isBigEndian);

            int numDirectories = 0;

            while (offset != 0L)
            {
                ++numDirectories;

                // EOFException means IFD was probably not properly terminated.
                try {
                    stream.Seek(offset);
                    int entries = ReadUnsignedShort(stream, isBigEndian);
                    stream.Skip(12 * entries);
                    offset = ReadUnsignedInt(stream, isBigEndian);
                } catch (EndOfStreamException) {
                    numDirectories--;
                    break;
                }
            }

            stream.Seek(pointer); // Reset stream pointer
            return(numDirectories);
        }
Ejemplo n.º 13
0
        /**
         * Constructs a TIFFDirectory from a SeekableStream.
         * The directory parameter specifies which directory to read from
         * the linked list present in the stream; directory 0 is normally
         * read but it is possible to store multiple images in a single
         * TIFF file by maintaing multiple directories.
         *
         * @param stream a SeekableStream to read from.
         * @param directory the index of the directory to read.
         */
        public TIFFDirectory(RandomAccessFileOrArray stream, int directory)
        {
            long global_save_offset = stream.FilePointer;
            long ifd_offset;

            // Read the TIFF header
            stream.Seek(0L);
            int endian = stream.ReadUnsignedShort();

            if (!IsValidEndianTag(endian))
            {
                throw new ArgumentException(MessageLocalization.GetComposedMessage("bad.endianness.tag.not.0x4949.or.0x4d4d"));
            }
            isBigEndian = (endian == 0x4d4d);

            int magic = ReadUnsignedShort(stream);

            if (magic != 42)
            {
                throw new ArgumentException(MessageLocalization.GetComposedMessage("bad.magic.number.should.be.42"));
            }

            // Get the initial ifd offset as an unsigned int (using a long)
            ifd_offset = ReadUnsignedInt(stream);

            for (int i = 0; i < directory; i++)
            {
                if (ifd_offset == 0L)
                {
                    throw new ArgumentException(MessageLocalization.GetComposedMessage("directory.number.too.large"));
                }

                stream.Seek(ifd_offset);
                int entries = ReadUnsignedShort(stream);
                stream.Skip(12 * entries);

                ifd_offset = ReadUnsignedInt(stream);
            }

            stream.Seek(ifd_offset);
            Initialize(stream);
            stream.Seek(global_save_offset);
        }
Ejemplo n.º 14
0
        // Utilities
        /// <summary>
        /// Returns the number of image directories (subimages) stored in a
        /// given TIFF file, represented by a <code>SeekableStream</code>.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public static int GetNumDirectories(RandomAccessFileOrArray stream)
        {
            long pointer = stream.GetPosition();

            // Save stream pointer
            stream.Seek(0L);
            int endian = stream.ReadUnsignedShort();

            if (!IsValidEndianTag(endian))
            {
                throw new iText.IO.IOException(iText.IO.IOException.BadEndiannessTagNot0x4949Or0x4d4d);
            }
            bool isBigEndian = endian == 0x4d4d;
            int  magic       = ReadUnsignedShort(stream, isBigEndian);

            if (magic != 42)
            {
                throw new iText.IO.IOException(iText.IO.IOException.BadMagicNumberShouldBe42);
            }
            stream.Seek(4L);
            long offset         = ReadUnsignedInt(stream, isBigEndian);
            int  numDirectories = 0;

            while (offset != 0L)
            {
                ++numDirectories;
                // EOFException means IFD was probably not properly terminated.
                try {
                    stream.Seek(offset);
                    int entries = ReadUnsignedShort(stream, isBigEndian);
                    stream.Skip(12 * entries);
                    offset = ReadUnsignedInt(stream, isBigEndian);
                }
                catch (EndOfStreamException) {
                    numDirectories--;
                    break;
                }
            }
            stream.Seek(pointer);
            // Reset stream pointer
            return(numDirectories);
        }
Ejemplo n.º 15
0
        /// <exception cref="System.IO.IOException"/>
        public OtfClass(RandomAccessFileOrArray rf, int classLocation)
        {
            //key is glyph, value is class inside all 2
            rf.Seek(classLocation);
            int classFormat = rf.ReadUnsignedShort();

            if (classFormat == 1)
            {
                int startGlyph = rf.ReadUnsignedShort();
                int glyphCount = rf.ReadUnsignedShort();
                int endGlyph   = startGlyph + glyphCount;
                for (int k = startGlyph; k < endGlyph; ++k)
                {
                    int cl = rf.ReadUnsignedShort();
                    mapClass.Put(k, cl);
                }
            }
            else
            {
                if (classFormat == 2)
                {
                    int classRangeCount = rf.ReadUnsignedShort();
                    for (int k = 0; k < classRangeCount; ++k)
                    {
                        int glyphStart = rf.ReadUnsignedShort();
                        int glyphEnd   = rf.ReadUnsignedShort();
                        int cl         = rf.ReadUnsignedShort();
                        for (; glyphStart <= glyphEnd; ++glyphStart)
                        {
                            mapClass.Put(glyphStart, cl);
                        }
                    }
                }
                else
                {
                    throw new System.IO.IOException("Invalid class format " + classFormat);
                }
            }
        }
Ejemplo n.º 16
0
        /**
         * Constructs a TIFFDirectory by reading a SeekableStream.
         * The ifd_offset parameter specifies the stream offset from which
         * to begin reading; this mechanism is sometimes used to store
         * private IFDs within a TIFF file that are not part of the normal
         * sequence of IFDs.
         *
         * @param stream a SeekableStream to read from.
         * @param ifd_offset the long byte offset of the directory.
         * @param directory the index of the directory to read beyond the
         *        one at the current stream offset; zero indicates the IFD
         *        at the current offset.
         */
        public TIFFDirectory(RandomAccessFileOrArray stream, long ifd_offset, int directory)
        {
            long global_save_offset = stream.FilePointer;

            stream.Seek(0L);
            int endian = stream.ReadUnsignedShort();

            if (!IsValidEndianTag(endian))
            {
                throw new ArgumentException(MessageLocalization.GetComposedMessage("bad.endianness.tag.not.0x4949.or.0x4d4d"));
            }
            isBigEndian = (endian == 0x4d4d);

            // Seek to the first IFD.
            stream.Seek(ifd_offset);

            // Seek to desired IFD if necessary.
            int dirNum = 0;

            while (dirNum < directory)
            {
                // Get the number of fields in the current IFD.
                int numEntries = ReadUnsignedShort(stream);

                // Skip to the next IFD offset value field.
                stream.Seek(ifd_offset + 12 * numEntries);

                // Read the offset to the next IFD beyond this one.
                ifd_offset = ReadUnsignedInt(stream);

                // Seek to the next IFD.
                stream.Seek(ifd_offset);

                // Increment the directory.
                dirNum++;
            }

            Initialize(stream);
            stream.Seek(global_save_offset);
        }
        /// <summary>
        /// Constructs a TIFFDirectory by reading a SeekableStream.
        /// The ifd_offset parameter specifies the stream offset from which
        /// to begin reading; this mechanism is sometimes used to store
        /// private IFDs within a TIFF file that are not part of the normal
        /// sequence of IFDs.
        /// one at the current stream offset; zero indicates the IFD
        /// at the current offset.
        /// </summary>
        /// <param name="stream">a SeekableStream to read from.</param>
        /// <param name="ifdOffset">the long byte offset of the directory.</param>
        /// <param name="directory">the index of the directory to read beyond the</param>
        public TiffDirectory(RandomAccessFileOrArray stream, long ifdOffset, int directory)
        {
            long globalSaveOffset = stream.FilePointer;

            stream.Seek(0L);
            var endian = stream.ReadUnsignedShort();

            if (!isValidEndianTag(endian))
            {
                throw new InvalidOperationException("Bad endianness tag (not 0x4949 or 0x4d4d).");
            }
            _isBigEndian = (endian == 0x4d4d);

            // Seek to the first IFD.
            stream.Seek(ifdOffset);

            // Seek to desired IFD if necessary.
            var dirNum = 0;

            while (dirNum < directory)
            {
                // Get the number of fields in the current IFD.
                var numEntries = readUnsignedShort(stream);

                // Skip to the next IFD offset value field.
                stream.Seek(ifdOffset + 12 * numEntries);

                // Read the offset to the next IFD beyond this one.
                ifdOffset = readUnsignedInt(stream);

                // Seek to the next IFD.
                stream.Seek(ifdOffset);

                // Increment the directory.
                dirNum++;
            }

            initialize(stream);
            stream.Seek(globalSaveOffset);
        }
Ejemplo n.º 18
0
        /// <summary>Constructs a TIFFDirectory from a SeekableStream.</summary>
        /// <remarks>
        /// Constructs a TIFFDirectory from a SeekableStream.
        /// The directory parameter specifies which directory to read from
        /// the linked list present in the stream; directory 0 is normally
        /// read but it is possible to store multiple images in a single
        /// TIFF file by maintaining multiple directories.
        /// </remarks>
        /// <param name="stream">a SeekableStream to read from.</param>
        /// <param name="directory">the index of the directory to read.</param>
        /// <exception cref="System.IO.IOException"/>
        public TIFFDirectory(RandomAccessFileOrArray stream, int directory)
        {
            long global_save_offset = stream.GetPosition();
            long ifd_offset;

            // Read the TIFF header
            stream.Seek(0L);
            int endian = stream.ReadUnsignedShort();

            if (!IsValidEndianTag(endian))
            {
                throw new iText.IO.IOException(iText.IO.IOException.BadEndiannessTagNot0x4949Or0x4d4d);
            }
            isBigEndian = endian == 0x4d4d;
            int magic = ReadUnsignedShort(stream);

            if (magic != 42)
            {
                throw new iText.IO.IOException(iText.IO.IOException.BadMagicNumberShouldBe42);
            }
            // Get the initial ifd offset as an unsigned int (using a long)
            ifd_offset = ReadUnsignedInt(stream);
            for (int i = 0; i < directory; i++)
            {
                if (ifd_offset == 0L)
                {
                    throw new iText.IO.IOException(iText.IO.IOException.DirectoryNumberTooLarge);
                }
                stream.Seek(ifd_offset);
                int entries = ReadUnsignedShort(stream);
                stream.Skip(12 * entries);
                ifd_offset = ReadUnsignedInt(stream);
            }
            stream.Seek(ifd_offset);
            Initialize(stream);
            stream.Seek(global_save_offset);
        }
Ejemplo n.º 19
0
        private void InitializeSfntTables()
        {
            tables = new LinkedDictionary <String, int[]>();
            if (ttcIndex >= 0)
            {
                int dirIdx = ttcIndex;
                if (dirIdx < 0)
                {
                    if (fileName != null)
                    {
                        throw new iText.IO.IOException("The font index for {0} must be positive.").SetMessageParams(fileName);
                    }
                    else
                    {
                        throw new iText.IO.IOException("The font index must be positive.");
                    }
                }
                String mainTag = ReadStandardString(4);
                if (!mainTag.Equals("ttcf"))
                {
                    if (fileName != null)
                    {
                        throw new iText.IO.IOException("{0} is not a valid ttc file.").SetMessageParams(fileName);
                    }
                    else
                    {
                        throw new iText.IO.IOException("Not a valid ttc file.");
                    }
                }
                raf.SkipBytes(4);
                int dirCount = raf.ReadInt();
                if (dirIdx >= dirCount)
                {
                    if (fileName != null)
                    {
                        throw new iText.IO.IOException("The font index for {0} must be between 0 and {1}. It is {2}.").SetMessageParams
                                  (fileName, dirCount - 1, dirIdx);
                    }
                    else
                    {
                        throw new iText.IO.IOException("The font index must be between 0 and {0}. It is {1}.").SetMessageParams(dirCount
                                                                                                                                - 1, dirIdx);
                    }
                }
                raf.SkipBytes(dirIdx * 4);
                directoryOffset = raf.ReadInt();
            }
            raf.Seek(directoryOffset);
            int ttId = raf.ReadInt();

            if (ttId != 0x00010000 && ttId != 0x4F54544F)
            {
                if (fileName != null)
                {
                    throw new iText.IO.IOException("{0} is not a valid ttf or otf file.").SetMessageParams(fileName);
                }
                else
                {
                    throw new iText.IO.IOException("Not a valid ttf or otf file.");
                }
            }
            int num_tables = raf.ReadUnsignedShort();

            raf.SkipBytes(6);
            for (int k = 0; k < num_tables; ++k)
            {
                String tag = ReadStandardString(4);
                raf.SkipBytes(4);
                int[] table_location = new int[2];
                table_location[0] = raf.ReadInt();
                table_location[1] = raf.ReadInt();
                tables.Put(tag, table_location);
            }
        }
Ejemplo n.º 20
0
        JBIG2Segment ReadHeader()
        {
            int ptr = ra.FilePointer;
            // 7.2.1
            int          segment_number = ra.ReadInt();
            JBIG2Segment s = new JBIG2Segment(segment_number);

            // 7.2.3
            int  segment_header_flags = ra.Read();
            bool deferred_non_retain  = ((segment_header_flags & 0x80) == 0x80);

            s.deferredNonRetain = deferred_non_retain;
            bool page_association_size = ((segment_header_flags & 0x40) == 0x40);
            int  segment_type          = (segment_header_flags & 0x3f);

            s.type = segment_type;

            //7.2.4
            int referred_to_byte0             = ra.Read();
            int count_of_referred_to_segments = (referred_to_byte0 & 0xE0) >> 5;

            int[]  referred_to_segment_numbers = null;
            bool[] segment_retention_flags     = null;

            if (count_of_referred_to_segments == 7)
            {
                // at least five bytes
                ra.Seek(ra.FilePointer - 1);
                count_of_referred_to_segments = (ra.ReadInt() & 0x1fffffff);
                segment_retention_flags       = new bool[count_of_referred_to_segments + 1];
                int i = 0;
                int referred_to_current_byte = 0;
                do
                {
                    int j = i % 8;
                    if (j == 0)
                    {
                        referred_to_current_byte = ra.Read();
                    }
                    segment_retention_flags[i] = ((((0x1 << j) & referred_to_current_byte) >> j) == 0x1);
                    i++;
                } while (i <= count_of_referred_to_segments);
            }
            else if (count_of_referred_to_segments <= 4)
            {
                // only one byte
                segment_retention_flags = new bool[count_of_referred_to_segments + 1];
                referred_to_byte0      &= 0x1f;
                for (int i = 0; i <= count_of_referred_to_segments; i++)
                {
                    segment_retention_flags[i] = ((((0x1 << i) & referred_to_byte0) >> i) == 0x1);
                }
            }
            else if (count_of_referred_to_segments == 5 || count_of_referred_to_segments == 6)
            {
                throw new InvalidOperationException(MessageLocalization.GetComposedMessage("count.of.referred.to.segments.had.bad.value.in.header.for.segment.1.starting.at.2", segment_number, ptr));
            }
            s.segmentRetentionFlags     = segment_retention_flags;
            s.countOfReferredToSegments = count_of_referred_to_segments;

            // 7.2.5
            referred_to_segment_numbers = new int[count_of_referred_to_segments + 1];
            for (int i = 1; i <= count_of_referred_to_segments; i++)
            {
                if (segment_number <= 256)
                {
                    referred_to_segment_numbers[i] = ra.Read();
                }
                else if (segment_number <= 65536)
                {
                    referred_to_segment_numbers[i] = ra.ReadUnsignedShort();
                }
                else
                {
                    referred_to_segment_numbers[i] = (int)ra.ReadUnsignedInt(); // TODO wtf ack
                }
            }
            s.referredToSegmentNumbers = referred_to_segment_numbers;

            // 7.2.6
            int segment_page_association;
            int page_association_offset = ra.FilePointer - ptr;

            if (page_association_size)
            {
                segment_page_association = ra.ReadInt();
            }
            else
            {
                segment_page_association = ra.Read();
            }
            if (segment_page_association < 0)
            {
                throw new InvalidOperationException(MessageLocalization.GetComposedMessage("page.1.invalid.for.segment.2.starting.at.3", segment_page_association, segment_number, ptr));
            }
            s.page = segment_page_association;
            // so we can change the page association at embedding time.
            s.page_association_size   = page_association_size;
            s.page_association_offset = page_association_offset;

            if (segment_page_association > 0 && !pages.ContainsKey(segment_page_association))
            {
                pages[segment_page_association] = new JBIG2Page(segment_page_association, this);
            }
            if (segment_page_association > 0)
            {
                pages[segment_page_association].AddSegment(s);
            }
            else
            {
                globals[s] = null;
            }

            // 7.2.7
            long segment_data_length = ra.ReadUnsignedInt();

            // TODO the 0xffffffff value that might be here, and how to understand those afflicted segments
            s.dataLength = segment_data_length;

            int end_ptr = ra.FilePointer;

            ra.Seek(ptr);
            byte[] header_data = new byte[end_ptr - ptr];
            ra.Read(header_data);
            s.headerData = header_data;

            return(s);
        }
        internal virtual Jbig2SegmentReader.Jbig2Segment ReadHeader()
        {
            int ptr = (int)ra.GetPosition();
            // 7.2.1
            int segment_number = ra.ReadInt();

            Jbig2SegmentReader.Jbig2Segment s = new Jbig2SegmentReader.Jbig2Segment(segment_number);
            // 7.2.3
            int  segment_header_flags = ra.Read();
            bool deferred_non_retain  = (segment_header_flags & 0x80) == 0x80;

            s.deferredNonRetain = deferred_non_retain;
            bool page_association_size = (segment_header_flags & 0x40) == 0x40;
            int  segment_type          = segment_header_flags & 0x3f;

            s.type = segment_type;
            //7.2.4
            int referred_to_byte0             = ra.Read();
            int count_of_referred_to_segments = (referred_to_byte0 & 0xE0) >> 5;

            int[]  referred_to_segment_numbers = null;
            bool[] segment_retention_flags     = null;
            if (count_of_referred_to_segments == 7)
            {
                // at least five bytes
                ra.Seek(ra.GetPosition() - 1);
                count_of_referred_to_segments = ra.ReadInt() & 0x1fffffff;
                segment_retention_flags       = new bool[count_of_referred_to_segments + 1];
                int i = 0;
                int referred_to_current_byte = 0;
                do
                {
                    int j = i % 8;
                    if (j == 0)
                    {
                        referred_to_current_byte = ra.Read();
                    }
                    segment_retention_flags[i] = (0x1 << j & referred_to_current_byte) >> j == 0x1;
                    i++;
                }while (i <= count_of_referred_to_segments);
            }
            else
            {
                if (count_of_referred_to_segments <= 4)
                {
                    // only one byte
                    segment_retention_flags = new bool[count_of_referred_to_segments + 1];
                    referred_to_byte0      &= 0x1f;
                    for (int i = 0; i <= count_of_referred_to_segments; i++)
                    {
                        segment_retention_flags[i] = (0x1 << i & referred_to_byte0) >> i == 0x1;
                    }
                }
                else
                {
                    if (count_of_referred_to_segments == 5 || count_of_referred_to_segments == 6)
                    {
                        throw new iText.IO.IOException("Count of referred-to segments has forbidden value in the header for segment {0} starting at {1}"
                                                       ).SetMessageParams(segment_number, ptr);
                    }
                }
            }
            s.segmentRetentionFlags     = segment_retention_flags;
            s.countOfReferredToSegments = count_of_referred_to_segments;
            // 7.2.5
            referred_to_segment_numbers = new int[count_of_referred_to_segments + 1];
            for (int i = 1; i <= count_of_referred_to_segments; i++)
            {
                if (segment_number <= 256)
                {
                    referred_to_segment_numbers[i] = ra.Read();
                }
                else
                {
                    if (segment_number <= 65536)
                    {
                        referred_to_segment_numbers[i] = ra.ReadUnsignedShort();
                    }
                    else
                    {
                        // TODO wtf ack
                        referred_to_segment_numbers[i] = (int)ra.ReadUnsignedInt();
                    }
                }
            }
            s.referredToSegmentNumbers = referred_to_segment_numbers;
            // 7.2.6
            int segment_page_association;
            int page_association_offset = (int)ra.GetPosition() - ptr;

            if (page_association_size)
            {
                segment_page_association = ra.ReadInt();
            }
            else
            {
                segment_page_association = ra.Read();
            }
            if (segment_page_association < 0)
            {
                throw new iText.IO.IOException("Page {0} is invalid for segment {1} starting at {2}").SetMessageParams(segment_page_association
                                                                                                                       , segment_number, ptr);
            }
            s.page = segment_page_association;
            // so we can change the page association at embedding time.
            s.page_association_size   = page_association_size;
            s.page_association_offset = page_association_offset;
            if (segment_page_association > 0 && !pages.ContainsKey(segment_page_association))
            {
                pages.Put(segment_page_association, new Jbig2SegmentReader.Jbig2Page(segment_page_association, this));
            }
            if (segment_page_association > 0)
            {
                pages.Get(segment_page_association).AddSegment(s);
            }
            else
            {
                globals.Add(s);
            }
            // 7.2.7
            long segment_data_length = ra.ReadUnsignedInt();

            // TODO the 0xffffffff value that might be here, and how to understand those afflicted segments
            s.dataLength = segment_data_length;
            int end_ptr = (int)ra.GetPosition();

            ra.Seek(ptr);
            byte[] header_data = new byte[end_ptr - ptr];
            ra.Read(header_data);
            s.headerData = header_data;
            return(s);
        }
Ejemplo n.º 22
0
        /// <summary>Reads the font data.</summary>
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual void Process()
        {
            tables = new LinkedDictionary <String, int[]>();
            if (ttcIndex >= 0)
            {
                int dirIdx = ttcIndex;
                if (dirIdx < 0)
                {
                    if (fileName != null)
                    {
                        throw new iText.IO.IOException("the.font.index.for.1.must.be.positive").SetMessageParams(fileName);
                    }
                    else
                    {
                        throw new iText.IO.IOException("the.font.index.must.be.positive");
                    }
                }
                String mainTag = ReadStandardString(4);
                if (!mainTag.Equals("ttcf"))
                {
                    if (fileName != null)
                    {
                        throw new iText.IO.IOException("1.is.not.a.valid.ttc.file").SetMessageParams(fileName);
                    }
                    else
                    {
                        throw new iText.IO.IOException("not.a.valid.ttc.file");
                    }
                }
                raf.SkipBytes(4);
                int dirCount = raf.ReadInt();
                if (dirIdx >= dirCount)
                {
                    if (fileName != null)
                    {
                        throw new iText.IO.IOException("the.font.index.for.1.must.be.between.0.and.2.it.was.3").SetMessageParams(fileName
                                                                                                                                 , dirCount - 1, dirIdx);
                    }
                    else
                    {
                        throw new iText.IO.IOException("the.font.index.must.be.between.0.and.1.it.was.2").SetMessageParams(dirCount
                                                                                                                           - 1, dirIdx);
                    }
                }
                raf.SkipBytes(dirIdx * 4);
                directoryOffset = raf.ReadInt();
            }
            raf.Seek(directoryOffset);
            int ttId = raf.ReadInt();

            if (ttId != 0x00010000 && ttId != 0x4F54544F)
            {
                if (fileName != null)
                {
                    throw new iText.IO.IOException("1.is.not.a.valid.ttf.or.otf.file").SetMessageParams(fileName);
                }
                else
                {
                    throw new iText.IO.IOException("not.a.valid.ttf.or.otf.file");
                }
            }
            int num_tables = raf.ReadUnsignedShort();

            raf.SkipBytes(6);
            for (int k = 0; k < num_tables; ++k)
            {
                String tag = ReadStandardString(4);
                raf.SkipBytes(4);
                int[] table_location = new int[2];
                table_location[0] = raf.ReadInt();
                table_location[1] = raf.ReadInt();
                tables[tag]       = table_location;
            }
            CheckCff();
            ReadHheaTable();
            ReadNameTable();
            ReadHeadTable();
            ReadOs_2Table();
            ReadPostTable();
            ReadGlyphWidths();
            ReadCmapTable();
        }