Example #1
0
        /// <summary>
        /// Writes the font to its associated file.
        /// </summary>
        public void Save()
        {
            this.cglpChunk.ReadFrom(this);
            this.cwdhChunk.ReadFrom(this);

            this.finfChunk.GlyphSectionOffset = this.header.Size + this.finfChunk.Size;
            this.finfChunk.WidthSectionOffset = this.finfChunk.GlyphSectionOffset + this.cglpChunk.Size;
            this.finfChunk.MapSectionOffset   = this.finfChunk.WidthSectionOffset + this.cwdhChunk.Size;

            this.cmapChunks.Clear();
            int cmapOffset = this.finfChunk.MapSectionOffset;

            foreach (CharacterSet characterSet in this.characterSets)
            {
                CharacterMapChunk cmapChunk = new CharacterMapChunk();
                cmapChunk.ReadFrom(characterSet, this.codes);
                cmapOffset += cmapChunk.Size;
                cmapChunk.NextChunkOffset = cmapOffset;
                this.cmapChunks.Add(cmapChunk);
            }

            this.cmapChunks[this.cmapChunks.Count - 1].NextChunkOffset = 0;

            this.header.SectionCount = 3 + this.cmapChunks.Count;
            this.header.FileSize     = cmapOffset;

            MemoryStream contents = new MemoryStream();

            this.header.WriteTo(contents);
            this.finfChunk.WriteTo(contents);
            this.cglpChunk.WriteTo(contents);
            this.cwdhChunk.WriteTo(contents);
            for (int i = 0; i < this.cmapChunks.Count; i++)
            {
                this.cmapChunks[i].WriteTo(contents);
            }

            StreamHelper.WriteFile(this.fileName, contents.ToArray());
        }
Example #2
0
        /// <summary>
        /// Reads the font from its associated file.
        /// </summary>
        public void Load()
        {
            byte[] fileData = StreamHelper.ReadFile(this.fileName);

            this.header = new NitroHeader("RTFN");
            this.header.ReadFrom(fileData, 0);

            this.finfChunk = new FontInfoSection();
            int finfOffset = this.header.Size;

            this.finfChunk.ReadFrom(fileData, finfOffset);

            this.cwdhChunk = new CharacterWidthChunk();
            this.cwdhChunk.ReadFrom(fileData, this.finfChunk.WidthSectionOffset);

            int characterCount = this.cwdhChunk.Widths.Count;

            this.cglpChunk = new CharacterGlyphChunk(characterCount);
            this.cglpChunk.ReadFrom(fileData, this.finfChunk.GlyphSectionOffset);

            int cmapCount  = this.header.SectionCount - 3;
            int cmapOffset = this.finfChunk.MapSectionOffset;

            this.cmapChunks    = new List <CharacterMapChunk>();
            this.characterSets = new List <CharacterSet>();
            for (int i = 0; i < cmapCount; i++)
            {
                CharacterMapChunk cmapChunk = new CharacterMapChunk();
                cmapChunk.ReadFrom(fileData, cmapOffset);
                cmapOffset = cmapChunk.NextChunkOffset;
                this.cmapChunks.Add(cmapChunk);
                this.characterSets.Add(new CharacterSet(cmapChunk, this.cwdhChunk, this.cglpChunk));
            }

            this.RebuildIndices();
        }