Ejemplo n.º 1
0
        protected virtual ITypefaceFont ReadFile(TrueTypeHeader header, IEnumerable <TrueTypeTableEntry> dirs, BigEndianReader reader, long startOffset, IFontInfo reference)
        {
            var entries = new TrueTypeTableEntryList(dirs);

            TrueTypeFile file = new TrueTypeFile(header, entries);

            TrueTypeTableFactory factory = this.GetTableFactory();

            foreach (TrueTypeTableEntry dir in dirs)
            {
                TrueTypeFontTable tbl = factory.ReadTable(dir, entries, reader);
                if (tbl != null)
                {
                    dir.SetTable(tbl);
                }
            }

            file.EnsureReferenceMatched(reference);


            byte[] data = CopyStreamData(reader.BaseStream, startOffset);
            file.SetFileData(data, this.DataFormat);

            return(file);
        }
Ejemplo n.º 2
0
 public GdiFontCreator(GdiDeviceContent dc)
 {
     this.dc     = dc;
     this.header = new TrueTypeHeader();
     this.ms     = new MemoryStream();
     this.fs     = new FontFileStream(ms);
 }
Ejemplo n.º 3
0
        public override ITypefaceFont ReadTypefaceAfterVersion(BigEndianReader reader, IFontInfo forReference, string source)
        {
            long startOffset = reader.BaseStream.Position - 4;

            TrueTypeHeader header;

            if (TrueTypeHeader.TryReadHeaderAfterVersion(reader, this, false, out header) == false)
            {
                throw new TypefaceReadException("Could not read the TrueType header for " + forReference.ToString() + " font");
            }

            List <TrueTypeTableEntry> dirs;

            try
            {
                dirs = new List <TrueTypeTableEntry>();

                for (int i = 0; i < header.NumberOfTables; i++)
                {
                    TrueTypeTableEntry dir = new TrueTypeTableEntry();
                    dir.Read(reader);
                    dirs.Add(dir);
                }

                dirs.Sort(delegate(TrueTypeTableEntry one, TrueTypeTableEntry two) { return(one.Offset.CompareTo(two.Offset)); });

                return(ReadFile(header, dirs, reader, startOffset, forReference));
            }
            catch (Exception ex) { throw new TypefaceReadException("Could not read the TTF File", ex); }
        }
        protected virtual TrueTypeTableFactory GetFactory(TrueTypeHeader header)
        {
            var vers = header.Version as TrueTypeVersionReader;

            if (null == vers)
            {
                throw new TypefaceReadException("Could not get the table reader from the header");
            }

            return(vers.GetTableFactory());
        }
        public static bool CanRead(BigEndianReader reader)
        {
            long oldpos = reader.Position;

            reader.Position = 0;
            TrueTypeHeader header;

            bool b = TrueTypeHeader.TryReadHeader(reader, out header);

            reader.Position = oldpos;

            return(b);
        }
        protected void Read(BigEndianReader reader)
        {
            TrueTypeHeader header;

            if (TrueTypeHeader.TryReadHeader(reader, out header) == false)
            {
                throw new NotSupportedException("The current stream is not a supported OpenType or TrueType font file");
            }

            List <TrueTypeTableEntry> dirs;

            try
            {
                dirs = new List <TrueTypeTableEntry>();

                for (int i = 0; i < header.NumberOfTables; i++)
                {
                    TrueTypeTableEntry dir = new TrueTypeTableEntry();
                    dir.Read(reader);
                    dirs.Add(dir);
                }

                dirs.Sort(delegate(TrueTypeTableEntry one, TrueTypeTableEntry two) { return(one.Offset.CompareTo(two.Offset)); });
                this._dirs = new TrueTypeTableEntryList(dirs);
                this._head = header;

                TrueTypeTableFactory factory = this.GetFactory(header);
                foreach (TrueTypeTableEntry dir in dirs)
                {
                    TrueTypeFontTable tbl = factory.ReadTable(dir, this, reader);
                    if (tbl != null)
                    {
                        dir.SetTable(tbl);
                    }
                }
            }
            catch (Exception ex) { throw new TypefaceReadException("Could not read the TTF File", ex); }
        }
        public static byte[] ExtractTTFfromTTC(System.IO.Stream ttc, int ttfHeadOffset)
        {
            using (System.IO.MemoryStream ttf = new System.IO.MemoryStream())
            {
                BigEndianReader reader = new BigEndianReader(ttc);
                reader.Position = ttfHeadOffset;

                TrueTypeHeader header;
                if (TrueTypeHeader.TryReadHeader(reader, out header) == false)
                {
                    throw new NotSupportedException("The current stream is not a supported OpenType or TrueType font file");
                }


                List <TrueTypeTableEntry> dirs;
                try
                {
                    dirs = new List <TrueTypeTableEntry>();

                    for (int i = 0; i < header.NumberOfTables; i++)
                    {
                        TrueTypeTableEntry dir = new TrueTypeTableEntry();
                        dir.Read(reader);
                        dirs.Add(dir);
                    }
                }
                catch (TypefaceReadException) { throw; }
                catch (Exception ex) { throw new TypefaceReadException("Could not read the TTF File", ex); }

                BigEndianWriter writer = new BigEndianWriter(ttf);
                writer.Write(header.Version.HeaderData);
                writer.WriteUInt16((ushort)header.NumberOfTables);
                writer.WriteUInt16((ushort)header.SearchRange);
                writer.WriteUInt16((ushort)header.EntrySelector);
                writer.WriteUInt16((ushort)header.RangeShift);

                long[] dirOffsets   = new long[dirs.Count]; //Set to the byte position of the Offset32 in the header to point to the table
                long[] tableOffsets = new long[dirs.Count]; //Set to the byte position of the table in the file

                for (var i = 0; i < dirs.Count; i++)
                {
                    var dir = dirs[i];

                    writer.WriteASCIIChars(dir.Tag);
                    writer.WriteUInt32(dir.CheckSum);

                    //Write zero as the offset initially and then we will come back and update
                    dirOffsets[i] = writer.Position;
                    writer.WriteUInt32(0);
                    writer.WriteUInt32(dir.Length);
                }

                for (var i = 0; i < dirs.Count; i++)
                {
                    var dir = dirs[i];

                    while (writer.Position % 4 != 0)
                    {
                        writer.WriteByte(Zero);
                    }

                    //Remember the start position of the table

                    tableOffsets[i] = writer.Position;
                    reader.Position = dir.Offset;

                    //we can improve this
                    var data = reader.Read((int)dir.Length);
                    writer.Write(data);
                }



                for (int i = 0; i < dirs.Count; i++)
                {
                    writer.Position = dirOffsets[i];
                    writer.WriteUInt32((uint)tableOffsets[i]);
                }



                writer.Position = 0;
                var fileData = ttf.ToArray();
                return(fileData);
            }
        }