Example #1
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();
        }