/// <summary> /// Creates a new font image that is a subset of this font image containing only the specified glyphs. /// </summary> public FontData CreateFontSubSet(Dictionary <int, object> glyphs, bool cidFont) { // Create new font image FontData fontData = new FontData(this); // Create new loca and glyf table IndexToLocationTable loca = new IndexToLocationTable(); loca.ShortIndex = this.loca.ShortIndex; GlyphDataTable glyf = new GlyphDataTable(); // Add all required tables //fontData.AddTable(this.os2); if (!cidFont) { fontData.AddTable(this.cmap); } if (this.cvt != null) { fontData.AddTable(this.cvt); } if (this.fpgm != null) { fontData.AddTable(this.fpgm); } fontData.AddTable(glyf); fontData.AddTable(this.head); fontData.AddTable(this.hhea); fontData.AddTable(this.hmtx); fontData.AddTable(loca); if (this.maxp != null) { fontData.AddTable(this.maxp); } //fontData.AddTable(this.name); if (this.prep != null) { fontData.AddTable(this.prep); } // Get closure of used glyphs this.glyf.CompleteGlyphClosure(glyphs); // Create a sorted array of all used glyphs int glyphCount = glyphs.Count; int[] glyphArray = new int[glyphCount]; glyphs.Keys.CopyTo(glyphArray, 0); Array.Sort <int>(glyphArray); // Calculate new size of glyph table. int size = 0; for (int idx = 0; idx < glyphCount; idx++) { size += this.glyf.GetGlyphSize(glyphArray[idx]); } glyf.DirectoryEntry.Length = size; // Create new loca table int numGlyphs = this.maxp.numGlyphs; loca.locaTable = new int[numGlyphs + 1]; // Create new glyf table glyf.glyphTable = new byte[glyf.DirectoryEntry.PaddedLength]; // Fill new glyf and loca table int glyphOffset = 0; int glyphIndex = 0; for (int idx = 0; idx < numGlyphs; idx++) { loca.locaTable[idx] = glyphOffset; if (glyphIndex < glyphCount && glyphArray[glyphIndex] == idx) { glyphIndex++; byte[] bytes = this.glyf.GetGlyphData(idx); int length = bytes.Length; if (length > 0) { Buffer.BlockCopy(bytes, 0, glyf.glyphTable, glyphOffset, length); glyphOffset += length; } } } loca.locaTable[numGlyphs] = glyphOffset; // Compile font tables into byte array fontData.Compile(); return(fontData); }
/// <summary> /// Creates a new font image that is a subset of this font image containing only the specified glyphs. /// </summary> public FontData CreateFontSubSet(Dictionary<int, object> glyphs, bool cidFont) { // Create new font image FontData fontData = new FontData(this); // Create new loca and glyf table IndexToLocationTable loca = new IndexToLocationTable(); loca.ShortIndex = this.loca.ShortIndex; GlyphDataTable glyf = new GlyphDataTable(); // Add all required tables //fontData.AddTable(this.os2); if (!cidFont) fontData.AddTable(this.cmap); if (this.cvt != null) fontData.AddTable(this.cvt); if (this.fpgm != null) fontData.AddTable(this.fpgm); fontData.AddTable(glyf); fontData.AddTable(this.head); fontData.AddTable(this.hhea); fontData.AddTable(this.hmtx); fontData.AddTable(loca); if (this.maxp != null) fontData.AddTable(this.maxp); //fontData.AddTable(this.name); if (this.prep != null) fontData.AddTable(this.prep); // Get closure of used glyphs this.glyf.CompleteGlyphClosure(glyphs); // Create a sorted array of all used glyphs int glyphCount = glyphs.Count; int[] glyphArray = new int[glyphCount]; glyphs.Keys.CopyTo(glyphArray, 0); Array.Sort<int>(glyphArray); // Calculate new size of glyph table. int size = 0; for (int idx = 0; idx < glyphCount; idx++) size += this.glyf.GetGlyphSize(glyphArray[idx]); glyf.DirectoryEntry.Length = size; // Create new loca table int numGlyphs = this.maxp.numGlyphs; loca.locaTable = new int[numGlyphs + 1]; // Create new glyf table glyf.glyphTable = new byte[glyf.DirectoryEntry.PaddedLength]; // Fill new glyf and loca table int glyphOffset = 0; int glyphIndex = 0; for (int idx = 0; idx < numGlyphs; idx++) { loca.locaTable[idx] = glyphOffset; if (glyphIndex < glyphCount && glyphArray[glyphIndex] == idx) { glyphIndex++; byte[] bytes = this.glyf.GetGlyphData(idx); int length = bytes.Length; if (length > 0) { Buffer.BlockCopy(bytes, 0, glyf.glyphTable, glyphOffset, length); glyphOffset += length; } } } loca.locaTable[numGlyphs] = glyphOffset; // Compile font tables into byte array fontData.Compile(); return fontData; }