Example #1
0
        /// <summary>
        /// Creates and reads a TableDirectoryEntry from the font image.
        /// </summary>
        public static TableDirectoryEntry ReadFrom(FontData fontData)
        {
            TableDirectoryEntry entry = new TableDirectoryEntry();

            entry.Tag      = fontData.ReadTag();
            entry.CheckSum = fontData.ReadULong();
            entry.Offset   = fontData.ReadLong();
            entry.Length   = (int)fontData.ReadULong();
            return(entry);
        }
Example #2
0
        /// <summary>
        /// Compiles the font to its binary representation.
        /// </summary>
        void Compile()
        {
            MemoryStream       stream = new MemoryStream();
            TrueTypeFontWriter writer = new TrueTypeFontWriter(stream);

            int tableCount = this.tableDictionary.Count;
            int selector   = entrySelectors[tableCount];

            this.offsetTable.Version       = 0x00010000;
            this.offsetTable.TableCount    = tableCount;
            this.offsetTable.SearchRange   = (ushort)((1 << selector) * 16);
            this.offsetTable.EntrySelector = (ushort)selector;
            this.offsetTable.RangeShift    = (ushort)((tableCount - (1 << selector)) * 16);
            this.offsetTable.Write(writer);

            // Sort tables by tag name
            string[] tags = new string[tableCount];
            this.tableDictionary.Keys.CopyTo(tags, 0);
            Array.Sort <string>(tags, StringComparer.Ordinal);

#if VERBOSE
            Debug.WriteLine("Start Compile");
#endif
            // Write tables in alphabetical order
            int tablePosition = 12 + 16 * tableCount;
            for (int idx = 0; idx < tableCount; idx++)
            {
                TableDirectoryEntry entry = this.tableDictionary[tags[idx]];
#if DEBUG
                if (entry.Tag == "glyf" || entry.Tag == "loca")
                {
                    GetType();
                }
#endif
                entry.FontTable.PrepareForCompilation();
                entry.Offset    = tablePosition;
                writer.Position = tablePosition;
                entry.FontTable.Write(writer);
                int endPosition = writer.Position;
                tablePosition   = endPosition;
                writer.Position = 12 + 16 * idx;
                entry.Write(writer);
#if VERBOSE
                Debug.WriteLine(String.Format("  Write Table '{0}', offset={1}, length={2}, checksum={3}, ", entry.Tag, entry.Offset, entry.Length, entry.CheckSum));
#endif
            }
#if VERBOSE
            Debug.WriteLine("End Compile");
#endif
            writer.Stream.Flush();
            int l = (int)writer.Stream.Length;
            l.GetType();
            this.data = stream.ToArray();
        }
Example #3
0
        /// <summary>
        /// Reads all required tables from the font data.
        /// </summary>
        internal void Read()
        {
            try
            {
                // Read offset table
                this.offsetTable.Version       = ReadULong();
                this.offsetTable.TableCount    = ReadUShort();
                this.offsetTable.SearchRange   = ReadUShort();
                this.offsetTable.EntrySelector = ReadUShort();
                this.offsetTable.RangeShift    = ReadUShort();

                // Move to table dictionary at position 12
                Debug.Assert(this.pos == 12);
                //this.tableDictionary = (this.offsetTable.TableCount);
                for (int idx = 0; idx < this.offsetTable.TableCount; idx++)
                {
                    TableDirectoryEntry entry = TableDirectoryEntry.ReadFrom(this);
                    this.tableDictionary.Add(entry.Tag, entry);
#if VERBOSE
                    Debug.WriteLine(String.Format("Font table: {0}", entry.Tag));
#endif
                }

                // PDFlib checks this, but it is not part of the OpenType spec anymore
                if (this.tableDictionary.ContainsKey("bhed"))
                {
                    throw new NotSupportedException("Bitmap fonts are not supported by PDFsharp.");
                }

                // Read required tables
                if (Seek(CMapTable.Tag) != -1)
                {
                    this.cmap = new CMapTable(this);
                }

                if (Seek(ControlValueTable.Tag) != -1)
                {
                    this.cvt = new ControlValueTable(this);
                }

                if (Seek(FontProgram.Tag) != -1)
                {
                    this.fpgm = new FontProgram(this);
                }

                if (Seek(MaximumProfileTable.Tag) != -1)
                {
                    this.maxp = new MaximumProfileTable(this);
                }

                if (Seek(NameTable.Tag) != -1)
                {
                    this.name = new NameTable(this);
                }

                if (Seek(FontHeaderTable.Tag) != -1)
                {
                    this.head = new FontHeaderTable(this);
                }

                if (Seek(HorizontalHeaderTable.Tag) != -1)
                {
                    this.hhea = new HorizontalHeaderTable(this);
                }

                if (Seek(HorizontalMetricsTable.Tag) != -1)
                {
                    this.hmtx = new HorizontalMetricsTable(this);
                }

                if (Seek(OS2Table.Tag) != -1)
                {
                    this.os2 = new OS2Table(this);
                }

                if (Seek(PostScriptTable.Tag) != -1)
                {
                    this.post = new PostScriptTable(this);
                }

                if (Seek(GlyphDataTable.Tag) != -1)
                {
                    this.glyf = new GlyphDataTable(this);
                }

                if (Seek(IndexToLocationTable.Tag) != -1)
                {
                    this.loca = new IndexToLocationTable(this);
                }

                if (Seek(GlyphSubstitutionTable.Tag) != -1)
                {
                    this.gsub = new GlyphSubstitutionTable(this);
                }

                if (Seek(ControlValueProgram.Tag) != -1)
                {
                    this.prep = new ControlValueProgram(this);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }